2019-07-25 00:44:12 +02:00
|
|
|
package org.schabi.newpipe.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2020-12-30 03:17:36 +01:00
|
|
|
import android.net.Uri;
|
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-09-29 00:32:24 +02:00
|
|
|
import org.schabi.newpipe.local.dialog.PlaylistCreationDialog;
|
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;
|
|
|
|
|
|
|
|
import java.util.Collections;
|
2020-10-06 17:22:12 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import static org.schabi.newpipe.player.MainPlayer.PlayerType.AUDIO;
|
|
|
|
import static org.schabi.newpipe.player.MainPlayer.PlayerType.POPUP;
|
2019-07-25 00:44:12 +02:00
|
|
|
|
|
|
|
public enum StreamDialogEntry {
|
2019-07-25 00:53:13 +02:00
|
|
|
//////////////////////////////////////
|
|
|
|
// enum values with DEFAULT actions //
|
|
|
|
//////////////////////////////////////
|
2019-07-25 00:44:12 +02:00
|
|
|
|
2021-03-28 18:32:40 +02:00
|
|
|
show_channel_details(R.string.show_channel_details, (fragment, item) ->
|
|
|
|
// For some reason `getParentFragmentManager()` doesn't work, but this does.
|
|
|
|
NavigationHelper.openChannelFragment(fragment.getActivity().getSupportFragmentManager(),
|
|
|
|
item.getServiceId(), item.getUploaderUrl(), item.getUploaderName())
|
|
|
|
),
|
|
|
|
|
2020-10-06 13:33:44 +02:00
|
|
|
/**
|
|
|
|
* Enqueues the stream automatically to the current PlayerType.<br>
|
|
|
|
* <br>
|
|
|
|
* Info: Add this entry within showStreamDialog.
|
|
|
|
*/
|
2020-10-06 17:22:12 +02:00
|
|
|
enqueue(R.string.enqueue_stream, (fragment, item) -> {
|
2020-10-06 13:33:44 +02:00
|
|
|
final MainPlayer.PlayerType type = PlayerHolder.getType();
|
|
|
|
|
2020-10-06 17:22:12 +02:00
|
|
|
if (type == AUDIO) {
|
2020-03-31 19:20:15 +02:00
|
|
|
NavigationHelper.enqueueOnBackgroundPlayer(fragment.getContext(),
|
2020-10-06 17:22:12 +02:00
|
|
|
new SinglePlayQueue(item), false);
|
|
|
|
} else if (type == POPUP) {
|
2020-03-31 19:20:15 +02:00
|
|
|
NavigationHelper.enqueueOnPopupPlayer(fragment.getContext(),
|
2020-10-06 17:22:12 +02:00
|
|
|
new SinglePlayQueue(item), false);
|
|
|
|
} else /* type == VIDEO */ {
|
|
|
|
NavigationHelper.enqueueOnVideoPlayer(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) {
|
2020-09-29 00:32:24 +02:00
|
|
|
final PlaylistAppendDialog d = PlaylistAppendDialog
|
|
|
|
.fromStreamInfoItems(Collections.singletonList(item));
|
|
|
|
|
2020-09-29 14:04:14 +02:00
|
|
|
PlaylistAppendDialog.onPlaylistFound(fragment.getContext(),
|
2020-09-29 00:32:24 +02:00
|
|
|
() -> d.show(fragment.getFragmentManager(), "StreamDialogEntry@append_playlist"),
|
|
|
|
() -> PlaylistCreationDialog.newInstance(d)
|
|
|
|
.show(fragment.getFragmentManager(), "StreamDialogEntry@create_playlist")
|
|
|
|
);
|
2020-03-31 19:20:15 +02:00
|
|
|
}
|
|
|
|
}),
|
2019-07-25 00:44:12 +02:00
|
|
|
|
2020-12-30 23:40:21 +01:00
|
|
|
play_with_kodi(R.string.play_with_kodi_title, (fragment, item) -> {
|
2020-12-30 03:17:36 +01:00
|
|
|
final Uri videoUrl = Uri.parse(item.getUrl());
|
|
|
|
try {
|
|
|
|
NavigationHelper.playWithKore(fragment.getContext(), videoUrl);
|
|
|
|
} catch (final Exception e) {
|
|
|
|
KoreUtil.showInstallKoreDialog(fragment.getActivity());
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2019-07-25 00:44:12 +02:00
|
|
|
share(R.string.share, (fragment, item) ->
|
2021-01-15 17:11:04 +01:00
|
|
|
ShareUtils.shareText(fragment.getContext(), item.getName(), item.getUrl()));
|
2019-07-25 00:44:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
///////////////
|
|
|
|
// 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 17:22:12 +02:00
|
|
|
public static void setEnabledEntries(final List<StreamDialogEntry> entries) {
|
2020-10-06 14:38:48 +02:00
|
|
|
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
|
|
|
}
|