2019-07-25 00:44:12 +02:00
|
|
|
package org.schabi.newpipe.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2020-10-06 13:33:44 +02:00
|
|
|
import android.widget.Toast;
|
2020-03-31 19:20:15 +02:00
|
|
|
|
2019-10-04 14:59:08 +02:00
|
|
|
import androidx.fragment.app.Fragment;
|
2019-07-25 00:44:12 +02:00
|
|
|
|
|
|
|
import org.schabi.newpipe.R;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
|
|
|
import org.schabi.newpipe.local.dialog.PlaylistAppendDialog;
|
2020-10-06 13:33:44 +02:00
|
|
|
import org.schabi.newpipe.player.MainPlayer;
|
|
|
|
import org.schabi.newpipe.player.helper.PlayerHolder;
|
2019-07-25 00:44:12 +02:00
|
|
|
import org.schabi.newpipe.player.playqueue.SinglePlayQueue;
|
|
|
|
|
2020-10-06 14:38:48 +02:00
|
|
|
import java.util.ArrayList;
|
2019-07-25 00:44:12 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
public enum StreamDialogEntry {
|
2019-07-25 00:53:13 +02:00
|
|
|
//////////////////////////////////////
|
|
|
|
// enum values with DEFAULT actions //
|
|
|
|
//////////////////////////////////////
|
2019-07-25 00:44:12 +02:00
|
|
|
|
2020-10-06 13:33:44 +02:00
|
|
|
/**
|
|
|
|
* Enqueues the stream automatically to the current PlayerType.<br>
|
|
|
|
* <br>
|
|
|
|
* Info: Add this entry within showStreamDialog.
|
|
|
|
*/
|
|
|
|
enqueue_stream(R.string.enqueue_stream, (fragment, item) -> {
|
|
|
|
final MainPlayer.PlayerType type = PlayerHolder.getType();
|
|
|
|
|
|
|
|
if (type == null) {
|
|
|
|
// This code shouldn't be reached since the checks for appending this entry should be
|
|
|
|
// done within the showStreamDialog calls.
|
|
|
|
Toast.makeText(fragment.getContext(),
|
|
|
|
"No player currently playing", Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (type) {
|
|
|
|
case AUDIO:
|
|
|
|
NavigationHelper.enqueueOnBackgroundPlayer(fragment.getContext(),
|
|
|
|
new SinglePlayQueue(item), false);
|
|
|
|
break;
|
|
|
|
case POPUP:
|
|
|
|
NavigationHelper.enqueueOnPopupPlayer(fragment.getContext(),
|
|
|
|
new SinglePlayQueue(item), false);
|
|
|
|
break;
|
|
|
|
case VIDEO:
|
|
|
|
NavigationHelper.enqueueOnVideoPlayer(fragment.getContext(),
|
|
|
|
new SinglePlayQueue(item), false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Same as above, but keep it for now for debugging.
|
|
|
|
Toast.makeText(fragment.getContext(),
|
|
|
|
"Unreachable code executed", Toast.LENGTH_SHORT).show();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2019-07-25 00:44:12 +02:00
|
|
|
enqueue_on_background(R.string.enqueue_on_background, (fragment, item) ->
|
2020-03-31 19:20:15 +02:00
|
|
|
NavigationHelper.enqueueOnBackgroundPlayer(fragment.getContext(),
|
|
|
|
new SinglePlayQueue(item), false)),
|
2019-07-25 00:44:12 +02:00
|
|
|
|
|
|
|
enqueue_on_popup(R.string.enqueue_on_popup, (fragment, item) ->
|
2020-03-31 19:20:15 +02:00
|
|
|
NavigationHelper.enqueueOnPopupPlayer(fragment.getContext(),
|
|
|
|
new SinglePlayQueue(item), false)),
|
2019-07-25 00:44:12 +02:00
|
|
|
|
|
|
|
start_here_on_background(R.string.start_here_on_background, (fragment, item) ->
|
2020-03-31 19:20:15 +02:00
|
|
|
NavigationHelper.playOnBackgroundPlayer(fragment.getContext(),
|
|
|
|
new SinglePlayQueue(item), true)),
|
2019-07-25 00:44:12 +02:00
|
|
|
|
|
|
|
start_here_on_popup(R.string.start_here_on_popup, (fragment, item) ->
|
2020-03-31 19:20:15 +02:00
|
|
|
NavigationHelper.playOnPopupPlayer(fragment.getContext(),
|
|
|
|
new SinglePlayQueue(item), true)),
|
2019-07-25 00:44:12 +02:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
set_as_playlist_thumbnail(R.string.set_as_playlist_thumbnail, (fragment, item) -> {
|
|
|
|
}), // has to be set manually
|
2019-07-25 00:44:12 +02:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
delete(R.string.delete, (fragment, item) -> {
|
|
|
|
}), // has to be set manually
|
2019-07-25 00:44:12 +02:00
|
|
|
|
|
|
|
append_playlist(R.string.append_playlist, (fragment, item) -> {
|
|
|
|
if (fragment.getFragmentManager() != null) {
|
|
|
|
PlaylistAppendDialog.fromStreamInfoItems(Collections.singletonList(item))
|
|
|
|
.show(fragment.getFragmentManager(), "StreamDialogEntry@append_playlist");
|
2020-03-31 19:20:15 +02:00
|
|
|
}
|
|
|
|
}),
|
2019-07-25 00:44:12 +02:00
|
|
|
|
|
|
|
share(R.string.share, (fragment, item) ->
|
|
|
|
ShareUtils.shareUrl(fragment.getContext(), item.getName(), item.getUrl()));
|
|
|
|
|
|
|
|
|
|
|
|
///////////////
|
|
|
|
// variables //
|
|
|
|
///////////////
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
private static StreamDialogEntry[] enabledEntries;
|
2019-07-25 00:44:12 +02:00
|
|
|
private final int resource;
|
2019-07-25 01:07:51 +02:00
|
|
|
private final StreamDialogEntryAction defaultAction;
|
2019-07-25 00:53:13 +02:00
|
|
|
private StreamDialogEntryAction customAction;
|
2019-07-25 00:44:12 +02:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
StreamDialogEntry(final int resource, final StreamDialogEntryAction defaultAction) {
|
2019-07-25 00:44:12 +02:00
|
|
|
this.resource = resource;
|
2019-07-25 01:07:51 +02:00
|
|
|
this.defaultAction = defaultAction;
|
2019-07-25 00:53:13 +02:00
|
|
|
this.customAction = null;
|
2019-07-25 00:44:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
///////////////////////////////////////////////////////
|
|
|
|
// non-static methods to initialize and edit entries //
|
|
|
|
///////////////////////////////////////////////////////
|
2019-07-25 00:44:12 +02:00
|
|
|
|
2020-10-06 14:38:48 +02:00
|
|
|
public static void setEnabledEntries(final ArrayList<StreamDialogEntry> entries) {
|
|
|
|
setEnabledEntries(entries.toArray(new StreamDialogEntry[0]));
|
|
|
|
}
|
|
|
|
|
2019-07-25 00:53:13 +02:00
|
|
|
/**
|
2020-03-31 19:20:15 +02:00
|
|
|
* To be called before using {@link #setCustomAction(StreamDialogEntryAction)}.
|
|
|
|
*
|
|
|
|
* @param entries the entries to be enabled
|
2019-07-25 00:53:13 +02:00
|
|
|
*/
|
2020-03-31 19:20:15 +02:00
|
|
|
public static void setEnabledEntries(final StreamDialogEntry... entries) {
|
2019-07-25 00:53:13 +02:00
|
|
|
// cleanup from last time StreamDialogEntry was used
|
2020-08-16 10:24:58 +02:00
|
|
|
for (final StreamDialogEntry streamDialogEntry : values()) {
|
2019-07-25 00:53:13 +02:00
|
|
|
streamDialogEntry.customAction = null;
|
|
|
|
}
|
|
|
|
|
2019-07-25 00:44:12 +02:00
|
|
|
enabledEntries = entries;
|
|
|
|
}
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
public static String[] getCommands(final Context context) {
|
2020-08-16 10:24:58 +02:00
|
|
|
final String[] commands = new String[enabledEntries.length];
|
2019-07-25 00:44:12 +02:00
|
|
|
for (int i = 0; i != enabledEntries.length; ++i) {
|
|
|
|
commands[i] = context.getResources().getString(enabledEntries[i].resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
return commands;
|
|
|
|
}
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
// static methods that act on enabled entries //
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public static void clickOn(final int which, final Fragment fragment,
|
|
|
|
final StreamInfoItem infoItem) {
|
2019-07-25 00:53:13 +02:00
|
|
|
if (enabledEntries[which].customAction == null) {
|
2019-07-25 01:07:51 +02:00
|
|
|
enabledEntries[which].defaultAction.onClick(fragment, infoItem);
|
2019-07-25 00:53:13 +02:00
|
|
|
} else {
|
|
|
|
enabledEntries[which].customAction.onClick(fragment, infoItem);
|
|
|
|
}
|
2019-07-25 00:44:12 +02:00
|
|
|
}
|
2020-03-31 19:20:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Can be used after {@link #setEnabledEntries(StreamDialogEntry...)} has been called.
|
|
|
|
*
|
|
|
|
* @param action the action to be set
|
|
|
|
*/
|
|
|
|
public void setCustomAction(final StreamDialogEntryAction action) {
|
|
|
|
this.customAction = action;
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface StreamDialogEntryAction {
|
|
|
|
void onClick(Fragment fragment, StreamInfoItem infoItem);
|
|
|
|
}
|
2019-07-25 00:44:12 +02:00
|
|
|
}
|