2015-01-14 12:12:47 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Synced Synapse. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2015-03-09 22:35:18 +01:00
|
|
|
package org.xbmc.kore.ui;
|
2015-01-14 12:12:47 +01:00
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.provider.BaseColumns;
|
|
|
|
import android.support.v4.content.CursorLoader;
|
|
|
|
import android.support.v4.view.MenuItemCompat;
|
|
|
|
import android.support.v7.widget.SearchView;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.CursorAdapter;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2015-03-09 22:35:18 +01:00
|
|
|
import org.xbmc.kore.R;
|
|
|
|
import org.xbmc.kore.host.HostInfo;
|
|
|
|
import org.xbmc.kore.host.HostManager;
|
|
|
|
import org.xbmc.kore.provider.MediaContract;
|
|
|
|
import org.xbmc.kore.provider.MediaDatabase;
|
|
|
|
import org.xbmc.kore.service.LibrarySyncService;
|
|
|
|
import org.xbmc.kore.utils.LogUtils;
|
|
|
|
import org.xbmc.kore.utils.UIUtils;
|
2015-01-14 12:12:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fragment that presents the artists list
|
|
|
|
*/
|
2015-08-22 20:03:09 +02:00
|
|
|
public class MusicVideoListFragment extends AbstractListFragment {
|
2015-01-14 12:12:47 +01:00
|
|
|
private static final String TAG = LogUtils.makeLogTag(MusicVideoListFragment.class);
|
|
|
|
|
|
|
|
public interface OnMusicVideoSelectedListener {
|
|
|
|
public void onMusicVideoSelected(int musicVideoId, String musicVideoTitle);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Activity listener
|
|
|
|
private OnMusicVideoSelectedListener listenerActivity;
|
|
|
|
|
2015-10-14 00:05:50 +02:00
|
|
|
@Override
|
|
|
|
protected String getListSyncType() { return LibrarySyncService.SYNC_ALL_MUSIC_VIDEOS; }
|
|
|
|
|
2015-01-14 12:12:47 +01:00
|
|
|
@Override
|
2015-08-22 20:03:09 +02:00
|
|
|
protected AdapterView.OnItemClickListener createOnItemClickListener() {
|
|
|
|
return new AdapterView.OnItemClickListener() {
|
2015-01-14 12:12:47 +01:00
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
// Get the movie id from the tag
|
|
|
|
ViewHolder tag = (ViewHolder)view.getTag();
|
|
|
|
// Notify the activity
|
|
|
|
listenerActivity.onMusicVideoSelected(tag.musicVideoId, tag.musicVideoTitle);
|
|
|
|
}
|
2015-08-22 20:03:09 +02:00
|
|
|
};
|
2015-01-14 12:12:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-08-22 20:03:09 +02:00
|
|
|
protected CursorAdapter createAdapter() {
|
|
|
|
return new MusicVideosAdapter(getActivity());
|
2015-01-14 12:12:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-08-22 20:03:09 +02:00
|
|
|
protected CursorLoader createCursorLoader() {
|
|
|
|
HostInfo hostInfo = HostManager.getInstance(getActivity()).getHostInfo();
|
|
|
|
Uri uri = MediaContract.MusicVideos.buildMusicVideosListUri(hostInfo != null ? hostInfo.getId() : -1);
|
2015-01-14 12:12:47 +01:00
|
|
|
|
2015-08-22 20:03:09 +02:00
|
|
|
String selection = null;
|
|
|
|
String selectionArgs[] = null;
|
|
|
|
String searchFilter = getSearchFilter();
|
|
|
|
if (!TextUtils.isEmpty(searchFilter)) {
|
|
|
|
selection = MediaContract.MusicVideosColumns.TITLE + " LIKE ?";
|
|
|
|
selectionArgs = new String[] {"%" + searchFilter + "%"};
|
|
|
|
}
|
2015-01-14 12:12:47 +01:00
|
|
|
|
2015-08-22 20:03:09 +02:00
|
|
|
return new CursorLoader(getActivity(), uri,
|
|
|
|
MusicVideosListQuery.PROJECTION, selection, selectionArgs, MusicVideosListQuery.SORT);
|
2015-01-14 12:12:47 +01:00
|
|
|
}
|
|
|
|
|
2015-08-22 20:03:09 +02:00
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
|
|
|
super.onAttach(activity);
|
|
|
|
try {
|
|
|
|
listenerActivity = (OnMusicVideoSelectedListener) activity;
|
|
|
|
} catch (ClassCastException e) {
|
|
|
|
throw new ClassCastException(activity.toString() + " must implement OnMusicVideoSelectedListener");
|
|
|
|
}
|
2015-01-14 12:12:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-08-22 20:03:09 +02:00
|
|
|
public void onDetach() {
|
|
|
|
super.onDetach();
|
|
|
|
listenerActivity = null;
|
2015-01-14 12:12:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-08-22 20:03:09 +02:00
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
2015-12-02 20:35:40 +01:00
|
|
|
if (!isAdded()) {
|
|
|
|
// HACK: Fix crash reported on Play Store. Why does this is necessary is beyond me
|
|
|
|
super.onCreateOptionsMenu(menu, inflater);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-22 20:03:09 +02:00
|
|
|
inflater.inflate(R.menu.media_search, menu);
|
|
|
|
MenuItem searchItem = menu.findItem(R.id.action_search);
|
|
|
|
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
|
|
|
|
searchView.setOnQueryTextListener(this);
|
|
|
|
searchView.setQueryHint(getString(R.string.action_search_music_videos));
|
|
|
|
super.onCreateOptionsMenu(menu, inflater);
|
2015-01-14 12:12:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Videos list query parameters.
|
|
|
|
*/
|
|
|
|
private interface MusicVideosListQuery {
|
|
|
|
String[] PROJECTION = {
|
|
|
|
BaseColumns._ID,
|
|
|
|
MediaContract.MusicVideos.MUSICVIDEOID,
|
|
|
|
MediaContract.MusicVideos.TITLE,
|
|
|
|
MediaContract.MusicVideos.ARTIST,
|
|
|
|
MediaContract.MusicVideos.ALBUM,
|
|
|
|
MediaContract.MusicVideos.THUMBNAIL,
|
|
|
|
MediaContract.MusicVideos.RUNTIME,
|
|
|
|
MediaContract.MusicVideos.GENRES,
|
|
|
|
};
|
|
|
|
|
2015-01-30 06:00:43 +01:00
|
|
|
String SORT = MediaDatabase.sortCommonTokens(MediaContract.MusicVideos.TITLE) + " ASC";
|
2015-01-14 12:12:47 +01:00
|
|
|
|
|
|
|
final int ID = 0;
|
|
|
|
final int MUSICVIDEOID = 1;
|
|
|
|
final int TITLE = 2;
|
|
|
|
final int ARTIST = 3;
|
|
|
|
final int ALBUM = 4;
|
|
|
|
final int THUMBNAIL = 5;
|
|
|
|
final int RUNTIME = 6;
|
|
|
|
final int GENRES = 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class MusicVideosAdapter extends CursorAdapter {
|
|
|
|
|
|
|
|
private HostManager hostManager;
|
|
|
|
private int artWidth, artHeight;
|
|
|
|
|
|
|
|
public MusicVideosAdapter(Context context) {
|
|
|
|
super(context, null, false);
|
|
|
|
this.hostManager = HostManager.getInstance(context);
|
|
|
|
|
|
|
|
// Get the art dimensions
|
|
|
|
Resources resources = context.getResources();
|
|
|
|
artWidth = (int)(resources.getDimension(R.dimen.musicvideolist_art_width) /
|
|
|
|
UIUtils.IMAGE_RESIZE_FACTOR);
|
|
|
|
artHeight = (int)(resources.getDimension(R.dimen.musicvideolist_art_heigth) /
|
|
|
|
UIUtils.IMAGE_RESIZE_FACTOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritDoc} */
|
|
|
|
@Override
|
|
|
|
public View newView(Context context, final Cursor cursor, ViewGroup parent) {
|
|
|
|
final View view = LayoutInflater.from(context)
|
|
|
|
.inflate(R.layout.grid_item_music_video, parent, false);
|
|
|
|
|
|
|
|
// Setup View holder pattern
|
|
|
|
ViewHolder viewHolder = new ViewHolder();
|
|
|
|
viewHolder.titleView = (TextView)view.findViewById(R.id.title);
|
|
|
|
viewHolder.artistAlbumView = (TextView)view.findViewById(R.id.details);
|
|
|
|
viewHolder.durationGenresView = (TextView)view.findViewById(R.id.duration);
|
|
|
|
viewHolder.artView = (ImageView)view.findViewById(R.id.art);
|
|
|
|
|
|
|
|
view.setTag(viewHolder);
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritDoc} */
|
|
|
|
@Override
|
|
|
|
public void bindView(View view, Context context, Cursor cursor) {
|
|
|
|
final ViewHolder viewHolder = (ViewHolder)view.getTag();
|
|
|
|
|
|
|
|
// Save the movie id
|
|
|
|
viewHolder.musicVideoId = cursor.getInt(MusicVideosListQuery.MUSICVIDEOID);
|
|
|
|
viewHolder.musicVideoTitle = cursor.getString(MusicVideosListQuery.TITLE);
|
|
|
|
|
|
|
|
viewHolder.titleView.setText(viewHolder.musicVideoTitle);
|
|
|
|
String artistAlbum = cursor.getString(MusicVideosListQuery.ARTIST) + " | " +
|
|
|
|
cursor.getString(MusicVideosListQuery.ALBUM);
|
|
|
|
viewHolder.artistAlbumView.setText(artistAlbum);
|
|
|
|
|
|
|
|
int runtime = cursor.getInt(MusicVideosListQuery.RUNTIME);
|
|
|
|
String durationGenres =
|
|
|
|
runtime > 0 ?
|
|
|
|
UIUtils.formatTime(runtime) + " | " + cursor.getString(MusicVideosListQuery.GENRES) :
|
|
|
|
cursor.getString(MusicVideosListQuery.GENRES);
|
|
|
|
viewHolder.durationGenresView.setText(durationGenres);
|
|
|
|
UIUtils.loadImageWithCharacterAvatar(context, hostManager,
|
|
|
|
cursor.getString(MusicVideosListQuery.THUMBNAIL), viewHolder.musicVideoTitle,
|
|
|
|
viewHolder.artView, artWidth, artHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* View holder pattern
|
|
|
|
*/
|
|
|
|
private static class ViewHolder {
|
|
|
|
TextView titleView;
|
|
|
|
TextView artistAlbumView;
|
|
|
|
TextView durationGenresView;
|
|
|
|
ImageView artView;
|
|
|
|
|
|
|
|
int musicVideoId;
|
|
|
|
String musicVideoTitle;
|
|
|
|
}
|
|
|
|
}
|