Kore/app/src/main/java/org/xbmc/kore/ui/sections/audio/AudioGenresListFragment.java

226 lines
8.1 KiB
Java
Raw Normal View History

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.
*/
package org.xbmc.kore.ui.sections.audio;
2015-01-14 12:12:47 +01:00
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupMenu;
2015-01-14 12:12:47 +01:00
import android.widget.TextView;
import androidx.loader.content.CursorLoader;
import org.xbmc.kore.R;
import org.xbmc.kore.host.HostInfo;
import org.xbmc.kore.host.HostManager;
import org.xbmc.kore.jsonrpc.type.PlaylistType;
import org.xbmc.kore.provider.MediaContract;
import org.xbmc.kore.service.library.LibrarySyncService;
import org.xbmc.kore.ui.AbstractCursorListFragment;
import org.xbmc.kore.ui.RecyclerViewCursorAdapter;
import org.xbmc.kore.utils.LogUtils;
import org.xbmc.kore.utils.MediaPlayerUtils;
import org.xbmc.kore.utils.UIUtils;
2015-01-14 12:12:47 +01:00
/**
* Fragment that presents the album genres list
*/
public class AudioGenresListFragment extends AbstractCursorListFragment {
2015-01-14 12:12:47 +01:00
private static final String TAG = LogUtils.makeLogTag(AudioGenresListFragment.class);
public interface OnAudioGenreSelectedListener {
public void onAudioGenreSelected(int genreId, String genreTitle);
}
// Activity listener
private OnAudioGenreSelectedListener listenerActivity;
@Override
protected String getListSyncType() { return LibrarySyncService.SYNC_ALL_MUSIC; }
2015-01-14 12:12:47 +01:00
@Override
protected void onListItemClicked(View view) {
// Get the movie id from the tag
ViewHolder tag = (ViewHolder) view.getTag();
// Notify the activity
listenerActivity.onAudioGenreSelected(tag.genreId, tag.genreTitle);
}
@Override
protected RecyclerViewCursorAdapter createCursorAdapter() {
return new AudioGenresAdapter(getActivity());
}
2015-01-14 12:12:47 +01:00
@Override
protected CursorLoader createCursorLoader() {
HostInfo hostInfo = HostManager.getInstance(getActivity()).getHostInfo();
Uri uri = MediaContract.AudioGenres.buildAudioGenresListUri(hostInfo != null ? hostInfo.getId() : -1);
2015-01-14 12:12:47 +01:00
String selection = null;
String selectionArgs[] = null;
String searchFilter = getSearchFilter();
if (!TextUtils.isEmpty(searchFilter)) {
selection = MediaContract.AudioGenres.TITLE + " LIKE ?";
selectionArgs = new String[] {"%" + searchFilter + "%"};
}
return new CursorLoader(getActivity(), uri,
AudioGenreListQuery.PROJECTION, selection, selectionArgs, AudioGenreListQuery.SORT);
2015-01-14 12:12:47 +01:00
}
@Override
public void onAttach(Context ctx) {
super.onAttach(ctx);
2015-01-14 12:12:47 +01:00
try {
listenerActivity = (OnAudioGenreSelectedListener) ctx;
2015-01-14 12:12:47 +01:00
} catch (ClassCastException e) {
throw new ClassCastException(ctx.toString() + " must implement OnAudioGenreSelectedListener");
2015-01-14 12:12:47 +01:00
}
setSupportsSearch(true);
2015-01-14 12:12:47 +01:00
}
@Override
public void onDetach() {
super.onDetach();
listenerActivity = null;
}
/**
* Audio genres list query parameters.
*/
public interface AudioGenreListQuery {
2015-01-14 12:12:47 +01:00
String[] PROJECTION = {
BaseColumns._ID,
MediaContract.AudioGenres.GENREID,
MediaContract.AudioGenres.TITLE,
MediaContract.AudioGenres.THUMBNAIL,
};
String SORT = MediaContract.AudioGenres.TITLE + " COLLATE NOCASE ASC";
2015-01-14 12:12:47 +01:00
int ID = 0;
int GENREID = 1;
int TITLE = 2;
int THUMBNAIL = 3;
2015-01-14 12:12:47 +01:00
}
private class AudioGenresAdapter extends RecyclerViewCursorAdapter {
2015-01-14 12:12:47 +01:00
private HostManager hostManager;
private int artWidth, artHeight;
public AudioGenresAdapter(Context context) {
this.hostManager = HostManager.getInstance(context);
// Get the art dimensions
Resources resources = context.getResources();
artWidth = (int)(resources.getDimension(R.dimen.audiogenrelist_art_width) /
UIUtils.IMAGE_RESIZE_FACTOR);
artHeight = (int)(resources.getDimension(R.dimen.audiogenrelist_art_heigth) /
UIUtils.IMAGE_RESIZE_FACTOR);
}
@Override
public CursorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(getContext())
.inflate(R.layout.grid_item_audio_genre, parent, false);
2015-01-14 12:12:47 +01:00
return new ViewHolder(view, getContext(), hostManager, artWidth, artHeight, genrelistItemMenuClickListener);
2015-01-14 12:12:47 +01:00
}
protected int getSectionColumnIdx() { return AudioGenreListQuery.TITLE; }
2015-01-14 12:12:47 +01:00
}
/**
* View holder pattern
*/
private static class ViewHolder extends RecyclerViewCursorAdapter.CursorViewHolder {
2015-01-14 12:12:47 +01:00
TextView titleView;
ImageView artView;
HostManager hostManager;
int artWidth;
int artHeight;
Context context;
2015-01-14 12:12:47 +01:00
int genreId;
String genreTitle;
ViewHolder(View itemView, Context context, HostManager hostManager, int artWidth, int artHeight,
View.OnClickListener contextMenuClickListener) {
super(itemView);
this.context = context;
this.hostManager = hostManager;
this.artWidth = artWidth;
this.artHeight = artHeight;
titleView = itemView.findViewById(R.id.title);
artView = itemView.findViewById(R.id.art);
ImageView contextMenu = itemView.findViewById(R.id.list_context_menu);
contextMenu.setTag(this);
contextMenu.setOnClickListener(contextMenuClickListener);
}
@Override
public void bindView(Cursor cursor) {
genreId = cursor.getInt(AudioGenreListQuery.GENREID);
genreTitle = cursor.getString(AudioGenreListQuery.TITLE);
titleView.setText(genreTitle);
String thumbnail = cursor.getString(AudioGenreListQuery.THUMBNAIL);
UIUtils.loadImageWithCharacterAvatar(context, hostManager,
thumbnail, genreTitle, artView, artWidth, artHeight);
}
2015-01-14 12:12:47 +01:00
}
private View.OnClickListener genrelistItemMenuClickListener = new View.OnClickListener() {
@Override
public void onClick(final View v) {
final ViewHolder viewHolder = (ViewHolder)v.getTag();
final PlaylistType.Item playListItem = new PlaylistType.Item();
playListItem.genreid = viewHolder.genreId;
final PopupMenu popupMenu = new PopupMenu(getActivity(), v);
popupMenu.getMenuInflater().inflate(R.menu.musiclist_item, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_play:
MediaPlayerUtils.play(AudioGenresListFragment.this, playListItem);
return true;
case R.id.action_queue:
MediaPlayerUtils.queue(AudioGenresListFragment.this, playListItem, PlaylistType.GetPlaylistsReturnType.AUDIO);
return true;
}
return false;
}
});
popupMenu.show();
}
};
2015-01-14 12:12:47 +01:00
}