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

406 lines
14 KiB
Java

/*
* 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;
import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.melnykov.fab.FloatingActionButton;
import com.melnykov.fab.ObservableScrollView;
import org.xbmc.kore.R;
import org.xbmc.kore.Settings;
import org.xbmc.kore.host.HostInfo;
import org.xbmc.kore.host.HostManager;
import org.xbmc.kore.jsonrpc.ApiCallback;
import org.xbmc.kore.jsonrpc.event.MediaSyncEvent;
import org.xbmc.kore.jsonrpc.method.Player;
import org.xbmc.kore.jsonrpc.type.PlaylistType;
import org.xbmc.kore.provider.MediaContract;
import org.xbmc.kore.provider.MediaDatabase;
import org.xbmc.kore.provider.MediaProvider;
import org.xbmc.kore.ui.AbstractDetailsFragment;
import org.xbmc.kore.utils.FileDownloadHelper;
import org.xbmc.kore.utils.LogUtils;
import org.xbmc.kore.utils.MediaPlayerUtils;
import org.xbmc.kore.utils.UIUtils;
import org.xbmc.kore.utils.Utils;
import java.util.ArrayList;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
public class ArtistOverviewFragment extends AbstractDetailsFragment
implements LoaderManager.LoaderCallbacks<Cursor> {
private static final String TAG = LogUtils.makeLogTag(ArtistOverviewFragment.class);
public static final String BUNDLE_KEY_ARTISTID = "id";
public static final String POSTER_TRANS_NAME = "POSTER_TRANS_NAME";
public static final String BUNDLE_KEY_TITLE = "title";
public static final String BUNDLE_KEY_GENRE = "genre";
public static final String BUNDLE_KEY_DESCRIPTION = "description";
public static final String BUNDLE_KEY_FANART = "fanart";
public static final String BUNDLE_KEY_POSTER = "poster";
// Loader IDs
private static final int LOADER_ARTIST = 0,
LOADER_SONGS = 1;
private HostManager hostManager;
private HostInfo hostInfo;
/**
* Handler on which to post RPC callbacks
*/
private Handler callbackHandler = new Handler();
private int artistId = -1;
private String artistTitle;
@InjectView(R.id.exit_transition_view) View exitTransitionView;
// Buttons
@InjectView(R.id.fab) ImageButton fabButton;
// Detail views
@InjectView(R.id.media_panel) ScrollView mediaPanel;
@InjectView(R.id.art) ImageView mediaArt;
@InjectView(R.id.poster) ImageView mediaPoster;
@InjectView(R.id.media_title) TextView mediaTitle;
@InjectView(R.id.media_undertitle) TextView mediaUndertitle;
@InjectView(R.id.media_description) TextView mediaDescription;
@TargetApi(21)
@Override
protected View createView(LayoutInflater inflater, ViewGroup container) {
Bundle bundle = getArguments();
artistId = bundle.getInt(BUNDLE_KEY_ARTISTID, -1);
if ((container == null) || (artistId == -1)) {
// We're not being shown or there's nothing to show
return null;
}
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_artist_details, container, false);
ButterKnife.inject(this, root);
hostManager = HostManager.getInstance(getActivity());
hostInfo = hostManager.getHostInfo();
// Setup dim the fanart when scroll changes. Full dim on 4 * iconSize dp
Resources resources = getActivity().getResources();
final int pixelsToTransparent = 4 * resources.getDimensionPixelSize(R.dimen.default_icon_size);
mediaPanel.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
float y = mediaPanel.getScrollY();
float newAlpha = Math.min(1, Math.max(0, 1 - (y / pixelsToTransparent)));
mediaArt.setAlpha(newAlpha);
}
});
((FloatingActionButton) fabButton).attachToScrollView((ObservableScrollView) mediaPanel);
if(Utils.isLollipopOrLater()) {
mediaPoster.setTransitionName(bundle.getString(POSTER_TRANS_NAME));
}
artistTitle = bundle.getString(BUNDLE_KEY_TITLE);
mediaTitle.setText(artistTitle);
mediaUndertitle.setText(bundle.getString(BUNDLE_KEY_GENRE));
mediaDescription.setText(bundle.getString(BUNDLE_KEY_DESCRIPTION));
setArt(bundle.getString(BUNDLE_KEY_POSTER), bundle.getString(BUNDLE_KEY_FANART));
return root;
}
@Override
protected String getSyncType() {
return null;
}
@Override
protected String getSyncID() {
return null;
}
@Override
protected int getSyncItemID() {
return 0;
}
@Override
protected SwipeRefreshLayout getSwipeRefreshLayout() {
return null;
}
@Override
protected void onDownload() {
getLoaderManager().initLoader(LOADER_SONGS, null, this);
}
@Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(false);
}
@Override
public void onResume() {
// Force the exit view to invisible
exitTransitionView.setVisibility(View.INVISIBLE);
super.onResume();
}
@Override
public void onPause() {
//Make sure loader is not reloaded for albums and songs when we return
//These loaders should only be activated by the user pressing the download button
getLoaderManager().destroyLoader(LOADER_SONGS);
super.onPause();
}
@Override
protected void onSyncProcessEnded(MediaSyncEvent event) {
}
/**
* Loader callbacks
*/
/** {@inheritDoc} */
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
Uri uri;
switch (i) {
case LOADER_ARTIST:
uri = MediaContract.Artists.buildArtistUri(hostInfo.getId(), artistId);
return new CursorLoader(getActivity(), uri,
DetailsQuery.PROJECTION, null, null, null);
case LOADER_SONGS:
uri = MediaContract.Songs.buildArtistSongsListUri(hostInfo.getId(), artistId);
return new CursorLoader(getActivity(), uri,
SongsListQuery.PROJECTION, null, null, SongsListQuery.SORT);
default:
return null;
}
}
/** {@inheritDoc} */
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
if (cursor != null && cursor.getCount() > 0) {
switch (cursorLoader.getId()) {
case LOADER_ARTIST:
displayArtistDetails(cursor);
break;
case LOADER_SONGS:
downloadSongs(cursor);
}
}
}
/** {@inheritDoc} */
@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
// Release loader's data
}
@OnClick(R.id.fab)
public void onFabClicked(View v) {
PlaylistType.Item item = new PlaylistType.Item();
item.artistid = artistId;
Player.Open action = new Player.Open(item);
action.execute(hostManager.getConnection(), new ApiCallback<String>() {
@Override
public void onSuccess(String result) {
if (!isAdded()) return;
// Check whether we should switch to the remote
boolean switchToRemote = PreferenceManager
.getDefaultSharedPreferences(getActivity())
.getBoolean(Settings.KEY_PREF_SWITCH_TO_REMOTE_AFTER_MEDIA_START,
Settings.DEFAULT_PREF_SWITCH_TO_REMOTE_AFTER_MEDIA_START);
if (switchToRemote) {
int cx = (fabButton.getLeft() + fabButton.getRight()) / 2;
int cy = (fabButton.getTop() + fabButton.getBottom()) / 2;
UIUtils.switchToRemoteWithAnimation(getActivity(), cx, cy, exitTransitionView);
}
}
@Override
public void onError(int errorCode, String description) {
if (!isAdded()) return;
// Got an error, show toast
Toast.makeText(getActivity(), R.string.unable_to_connect_to_xbmc, Toast.LENGTH_SHORT)
.show();
}
}, callbackHandler);
}
@OnClick(R.id.add_to_playlist)
public void onAddToPlaylistClicked(View v) {
final PlaylistType.Item playListItem = new PlaylistType.Item();
playListItem.artistid = artistId;
MediaPlayerUtils.queue(this, playListItem, PlaylistType.GetPlaylistsReturnType.AUDIO);
}
private FileDownloadHelper.SongInfo createSongInfo(Cursor cursor) {
return new FileDownloadHelper.SongInfo(
cursor.getString(SongsListQuery.DISPLAYARTIST),
cursor.getString(SongsListQuery.ALBUMTITLE),
cursor.getInt(SongsListQuery.SONGID),
cursor.getInt(SongsListQuery.TRACK),
cursor.getString(SongsListQuery.TITLE),
cursor.getString(SongsListQuery.FILE));
}
private void downloadSongs(Cursor cursor) {
DialogInterface.OnClickListener noopClickListener =
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { }
};
final ArrayList<FileDownloadHelper.SongInfo> songInfoList = new ArrayList<>(cursor.getCount());
if (cursor.moveToFirst()) {
do {
FileDownloadHelper.SongInfo songInfo = createSongInfo(cursor);
if (songInfo != null) {
songInfoList.add(songInfo);
}
} while (cursor.moveToNext());
}
UIUtils.downloadSongs(getActivity(), songInfoList, hostInfo, callbackHandler);
}
private void displayArtistDetails(Cursor cursor) {
cursor.moveToFirst();
artistTitle = cursor.getString(DetailsQuery.ARTIST);
mediaTitle.setText(artistTitle);
mediaUndertitle.setText(cursor.getString(DetailsQuery.GENRE));
mediaDescription.setText(cursor.getString(DetailsQuery.DESCRIPTION));
setArt(cursor.getString(DetailsQuery.THUMBNAIL), cursor.getString(DetailsQuery.FANART));
}
private void setArt(String poster, String fanart) {
final Resources resources = getActivity().getResources();
// Images
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int artHeight = resources.getDimensionPixelOffset(R.dimen.now_playing_art_height),
artWidth = displayMetrics.widthPixels;
int posterWidth = resources.getDimensionPixelOffset(R.dimen.albumdetail_poster_width);
int posterHeight = resources.getDimensionPixelOffset(R.dimen.albumdetail_poster_heigth);
UIUtils.loadImageWithCharacterAvatar(getActivity(), hostManager,
poster, artistTitle,
mediaPoster, posterWidth, posterHeight);
UIUtils.loadImageIntoImageview(hostManager,
TextUtils.isEmpty(fanart) ? poster : fanart,
mediaArt, artWidth, artHeight);
}
/**
* Returns the shared element if visible
* @return View if visible, null otherwise
*/
public View getSharedElement() {
if (UIUtils.isViewInBounds(mediaPanel, mediaPoster)) {
return mediaPoster;
}
return null;
}
private interface DetailsQuery {
String[] PROJECTION = {
BaseColumns._ID,
MediaContract.Artists.ARTISTID,
MediaContract.Artists.ARTIST,
MediaContract.Artists.GENRE,
MediaContract.Artists.THUMBNAIL,
MediaContract.Artists.DESCRIPTION,
MediaContract.Artists.FANART
};
int ID = 0;
int ARTISTID = 1;
int ARTIST = 2;
int GENRE = 3;
int THUMBNAIL = 4;
int DESCRIPTION = 5;
int FANART = 6;
}
/**
* Song list query parameters.
*/
private interface SongsListQuery {
String[] PROJECTION = {
MediaDatabase.Tables.SONGS + "." + BaseColumns._ID,
MediaProvider.Qualified.SONGS_TITLE,
MediaProvider.Qualified.SONGS_TRACK,
MediaProvider.Qualified.SONGS_DURATION,
MediaProvider.Qualified.SONGS_FILE,
MediaProvider.Qualified.SONGS_SONGID,
MediaProvider.Qualified.SONGS_ALBUMID,
MediaProvider.Qualified.ALBUMS_TITLE,
MediaProvider.Qualified.SONGS_DISPLAYARTIST
};
String SORT = MediaContract.Songs.TRACK + " ASC";
int ID = 0;
int TITLE = 1;
int TRACK = 2;
int DURATION = 3;
int FILE = 4;
int SONGID = 5;
int ALBUMID = 6;
int ALBUMTITLE = 7;
int DISPLAYARTIST = 8;
}
}