2017-09-03 08:04:18 +02:00
|
|
|
package org.schabi.newpipe.fragments.list.playlist;
|
|
|
|
|
2018-01-04 07:53:31 +01:00
|
|
|
import android.app.Activity;
|
2017-11-11 23:47:34 +01:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
2017-09-03 08:04:18 +02:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
2018-01-04 05:28:01 +01:00
|
|
|
import android.view.MenuItem;
|
2017-09-03 08:04:18 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import org.schabi.newpipe.R;
|
|
|
|
import org.schabi.newpipe.extractor.ListExtractor;
|
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|
|
|
import org.schabi.newpipe.extractor.playlist.PlaylistInfo;
|
2017-11-11 23:47:34 +01:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
2017-09-03 08:04:18 +02:00
|
|
|
import org.schabi.newpipe.fragments.list.BaseListInfoFragment;
|
2017-11-11 23:47:34 +01:00
|
|
|
import org.schabi.newpipe.info_list.InfoItemDialog;
|
2017-09-25 05:14:58 +02:00
|
|
|
import org.schabi.newpipe.playlist.PlayQueue;
|
2017-11-11 23:47:34 +01:00
|
|
|
import org.schabi.newpipe.playlist.PlaylistPlayQueue;
|
|
|
|
import org.schabi.newpipe.playlist.SinglePlayQueue;
|
2017-09-03 08:04:18 +02:00
|
|
|
import org.schabi.newpipe.report.UserAction;
|
|
|
|
import org.schabi.newpipe.util.ExtractorHelper;
|
|
|
|
import org.schabi.newpipe.util.NavigationHelper;
|
|
|
|
|
|
|
|
import io.reactivex.Single;
|
|
|
|
|
|
|
|
import static org.schabi.newpipe.util.AnimationUtils.animateView;
|
|
|
|
|
|
|
|
public class PlaylistFragment extends BaseListInfoFragment<PlaylistInfo> {
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Views
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
private View headerRootLayout;
|
|
|
|
private TextView headerTitleView;
|
|
|
|
private View headerUploaderLayout;
|
|
|
|
private TextView headerUploaderName;
|
|
|
|
private ImageView headerUploaderAvatar;
|
|
|
|
private TextView headerStreamCount;
|
2017-11-12 09:16:51 +01:00
|
|
|
private View playlistCtrl;
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2017-11-12 09:16:51 +01:00
|
|
|
private View headerPlayAllButton;
|
|
|
|
private View headerPopupButton;
|
|
|
|
private View headerBackgroundButton;
|
2017-09-04 19:23:56 +02:00
|
|
|
|
2018-01-29 08:01:06 +01:00
|
|
|
private MenuItem playlistAppendButton;
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
public static PlaylistFragment getInstance(int serviceId, String url, String name) {
|
|
|
|
PlaylistFragment instance = new PlaylistFragment();
|
|
|
|
instance.setInitialData(serviceId, url, name);
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// LifeCycle
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
|
|
return inflater.inflate(R.layout.fragment_playlist, container, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Init
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
protected View getListHeader() {
|
|
|
|
headerRootLayout = activity.getLayoutInflater().inflate(R.layout.playlist_header, itemsList, false);
|
|
|
|
headerTitleView = headerRootLayout.findViewById(R.id.playlist_title_view);
|
|
|
|
headerUploaderLayout = headerRootLayout.findViewById(R.id.uploader_layout);
|
|
|
|
headerUploaderName = headerRootLayout.findViewById(R.id.uploader_name);
|
|
|
|
headerUploaderAvatar = headerRootLayout.findViewById(R.id.uploader_avatar_view);
|
|
|
|
headerStreamCount = headerRootLayout.findViewById(R.id.playlist_stream_count);
|
2017-11-12 09:16:51 +01:00
|
|
|
playlistCtrl = headerRootLayout.findViewById(R.id.playlist_control);
|
2017-09-06 02:48:48 +02:00
|
|
|
|
2017-11-12 09:16:51 +01:00
|
|
|
headerPlayAllButton = headerRootLayout.findViewById(R.id.playlist_ctrl_play_all_button);
|
|
|
|
headerPopupButton = headerRootLayout.findViewById(R.id.playlist_ctrl_play_popup_button);
|
|
|
|
headerBackgroundButton = headerRootLayout.findViewById(R.id.playlist_ctrl_play_bg_button);
|
2017-09-03 08:04:18 +02:00
|
|
|
|
|
|
|
return headerRootLayout;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void initViews(View rootView, Bundle savedInstanceState) {
|
|
|
|
super.initViews(rootView, savedInstanceState);
|
|
|
|
|
|
|
|
infoListAdapter.useMiniItemVariants(true);
|
|
|
|
}
|
|
|
|
|
2017-11-11 23:47:34 +01:00
|
|
|
@Override
|
|
|
|
protected void showStreamDialog(final StreamInfoItem item) {
|
|
|
|
final Context context = getContext();
|
2018-01-04 07:53:31 +01:00
|
|
|
final Activity activity = getActivity();
|
2017-11-14 06:13:52 +01:00
|
|
|
if (context == null || context.getResources() == null || getActivity() == null) return;
|
|
|
|
|
2017-11-11 23:47:34 +01:00
|
|
|
final String[] commands = new String[]{
|
|
|
|
context.getResources().getString(R.string.enqueue_on_background),
|
|
|
|
context.getResources().getString(R.string.enqueue_on_popup),
|
|
|
|
context.getResources().getString(R.string.start_here_on_main),
|
|
|
|
context.getResources().getString(R.string.start_here_on_background),
|
|
|
|
context.getResources().getString(R.string.start_here_on_popup),
|
|
|
|
};
|
|
|
|
|
|
|
|
final DialogInterface.OnClickListener actions = new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialogInterface, int i) {
|
|
|
|
final int index = Math.max(infoListAdapter.getItemsList().indexOf(item), 0);
|
|
|
|
switch (i) {
|
|
|
|
case 0:
|
|
|
|
NavigationHelper.enqueueOnBackgroundPlayer(context, new SinglePlayQueue(item));
|
|
|
|
break;
|
|
|
|
case 1:
|
2018-01-04 07:53:31 +01:00
|
|
|
NavigationHelper.enqueueOnPopupPlayer(activity, new SinglePlayQueue(item));
|
2017-11-11 23:47:34 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
NavigationHelper.playOnMainPlayer(context, getPlayQueue(index));
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
NavigationHelper.playOnBackgroundPlayer(context, getPlayQueue(index));
|
|
|
|
break;
|
|
|
|
case 4:
|
2018-01-04 07:53:31 +01:00
|
|
|
NavigationHelper.playOnPopupPlayer(activity, getPlayQueue(index));
|
2017-11-11 23:47:34 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
new InfoItemDialog(getActivity(), item, commands, actions).show();
|
|
|
|
}
|
2018-01-04 05:28:01 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
2018-01-29 08:01:06 +01:00
|
|
|
if (DEBUG) Log.d(TAG, "onCreateOptionsMenu() called with: menu = [" + menu +
|
|
|
|
"], inflater = [" + inflater + "]");
|
2018-01-04 05:28:01 +01:00
|
|
|
super.onCreateOptionsMenu(menu, inflater);
|
|
|
|
inflater.inflate(R.menu.menu_playlist, menu);
|
2018-01-29 08:01:06 +01:00
|
|
|
|
|
|
|
playlistAppendButton = menu.findItem(R.id.menu_append_playlist);
|
|
|
|
if (currentInfo != null) {
|
|
|
|
playlistAppendButton.setVisible(!currentInfo.getRelatedStreams().isEmpty());
|
|
|
|
}
|
2018-01-04 05:28:01 +01:00
|
|
|
}
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Load and handle
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Single<ListExtractor.NextItemsResult> loadMoreItemsLogic() {
|
2017-09-11 16:21:01 +02:00
|
|
|
return ExtractorHelper.getMorePlaylistItems(serviceId, url, currentNextItemsUrl);
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Single<PlaylistInfo> loadResult(boolean forceLoad) {
|
|
|
|
return ExtractorHelper.getPlaylistInfo(serviceId, url, forceLoad);
|
|
|
|
}
|
|
|
|
|
2018-01-04 05:28:01 +01:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.menu_item_openInBrowser:
|
|
|
|
openUrlInBrowser(url);
|
|
|
|
break;
|
2018-01-29 08:01:06 +01:00
|
|
|
case R.id.menu_item_share:
|
2018-01-09 12:41:30 +01:00
|
|
|
shareUrl(name, url);
|
2018-01-04 05:28:01 +01:00
|
|
|
break;
|
2018-01-29 08:01:06 +01:00
|
|
|
case R.id.menu_append_playlist:
|
|
|
|
appendToPlaylist(getFragmentManager(), TAG);
|
|
|
|
break;
|
2018-01-04 05:28:01 +01:00
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Contract
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void showLoading() {
|
|
|
|
super.showLoading();
|
|
|
|
animateView(headerRootLayout, false, 200);
|
|
|
|
animateView(itemsList, false, 100);
|
|
|
|
|
|
|
|
imageLoader.cancelDisplayTask(headerUploaderAvatar);
|
|
|
|
animateView(headerUploaderLayout, false, 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleResult(@NonNull final PlaylistInfo result) {
|
|
|
|
super.handleResult(result);
|
|
|
|
|
|
|
|
animateView(headerRootLayout, true, 100);
|
|
|
|
animateView(headerUploaderLayout, true, 300);
|
|
|
|
headerUploaderLayout.setOnClickListener(null);
|
2017-12-08 15:05:08 +01:00
|
|
|
if (!TextUtils.isEmpty(result.getUploaderName())) {
|
|
|
|
headerUploaderName.setText(result.getUploaderName());
|
|
|
|
if (!TextUtils.isEmpty(result.getUploaderUrl())) {
|
2017-09-03 08:04:18 +02:00
|
|
|
headerUploaderLayout.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2017-12-08 15:05:08 +01:00
|
|
|
NavigationHelper.openChannelFragment(getFragmentManager(), result.getServiceId(), result.getUploaderUrl(), result.getUploaderName());
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 09:16:51 +01:00
|
|
|
playlistCtrl.setVisibility(View.VISIBLE);
|
|
|
|
|
2017-12-08 15:05:08 +01:00
|
|
|
imageLoader.displayImage(result.getUploaderAvatarUrl(), headerUploaderAvatar, DISPLAY_AVATAR_OPTIONS);
|
2017-09-06 02:48:48 +02:00
|
|
|
headerStreamCount.setText(getResources().getQuantityString(R.plurals.videos, (int) result.stream_count, (int) result.stream_count));
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2018-01-29 08:01:06 +01:00
|
|
|
if (playlistAppendButton != null) playlistAppendButton
|
|
|
|
.setVisible(!currentInfo.getRelatedStreams().isEmpty());
|
|
|
|
|
2017-12-08 15:05:08 +01:00
|
|
|
if (!result.getErrors().isEmpty()) {
|
|
|
|
showSnackBarError(result.getErrors(), UserAction.REQUESTED_PLAYLIST, NewPipe.getNameOfService(result.getServiceId()), result.getUrl(), 0);
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
2017-09-04 19:23:56 +02:00
|
|
|
|
|
|
|
headerPlayAllButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
2017-11-02 06:33:13 +01:00
|
|
|
NavigationHelper.playOnMainPlayer(activity, getPlayQueue());
|
2017-09-06 02:48:48 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
headerPopupButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
2017-11-02 06:33:13 +01:00
|
|
|
NavigationHelper.playOnPopupPlayer(activity, getPlayQueue());
|
2017-09-06 02:48:48 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
headerBackgroundButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
2017-11-02 06:33:13 +01:00
|
|
|
NavigationHelper.playOnBackgroundPlayer(activity, getPlayQueue());
|
2017-09-04 19:23:56 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-02 06:33:13 +01:00
|
|
|
private PlayQueue getPlayQueue() {
|
2017-11-11 23:47:34 +01:00
|
|
|
return getPlayQueue(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private PlayQueue getPlayQueue(final int index) {
|
2017-11-02 07:38:18 +01:00
|
|
|
return new PlaylistPlayQueue(
|
2017-12-08 15:05:08 +01:00
|
|
|
currentInfo.getServiceId(),
|
|
|
|
currentInfo.getUrl(),
|
|
|
|
currentInfo.getNextStreamsUrl(),
|
2017-09-25 05:14:58 +02:00
|
|
|
infoListAdapter.getItemsList(),
|
2017-11-11 23:47:34 +01:00
|
|
|
index
|
2017-09-06 02:48:48 +02:00
|
|
|
);
|
|
|
|
}
|
2017-09-03 08:04:18 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleNextItems(ListExtractor.NextItemsResult result) {
|
|
|
|
super.handleNextItems(result);
|
|
|
|
|
2017-12-08 15:05:08 +01:00
|
|
|
if (!result.getErrors().isEmpty()) {
|
|
|
|
showSnackBarError(result.getErrors(), UserAction.REQUESTED_PLAYLIST, NewPipe.getNameOfService(serviceId)
|
2017-09-03 08:04:18 +02:00
|
|
|
, "Get next page of: " + url, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// OnError
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean onError(Throwable exception) {
|
|
|
|
if (super.onError(exception)) return true;
|
|
|
|
|
|
|
|
int errorId = exception instanceof ExtractionException ? R.string.parsing_error : R.string.general_error;
|
|
|
|
onUnrecoverableError(exception, UserAction.REQUESTED_PLAYLIST, NewPipe.getNameOfService(serviceId), url, errorId);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utils
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setTitle(String title) {
|
|
|
|
super.setTitle(title);
|
|
|
|
headerTitleView.setText(title);
|
|
|
|
}
|
|
|
|
}
|