Enqueue: Add auto-select StreamDialogEntry for current PlayerType

This commit is contained in:
vkay94 2020-10-06 13:33:44 +02:00
parent dd55ad61f4
commit cd515993f5
4 changed files with 78 additions and 2 deletions

View File

@ -6,8 +6,12 @@ import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.PlaybackParameters; import com.google.android.exoplayer2.PlaybackParameters;
import org.schabi.newpipe.App; import org.schabi.newpipe.App;
import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.MainActivity;
import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.StreamInfo;
@ -31,6 +35,22 @@ public final class PlayerHolder {
private static MainPlayer playerService; private static MainPlayer playerService;
private static VideoPlayerImpl player; private static VideoPlayerImpl player;
/**
* Returns the current {@link MainPlayer.PlayerType} of the {@link MainPlayer} service,
* otherwise `null` if no service running.
*/
@Nullable
public static MainPlayer.PlayerType getType() {
if (player == null) {
return null;
}
return player.videoPlayerSelected() ? MainPlayer.PlayerType.VIDEO
: player.popupPlayerSelected() ? MainPlayer.PlayerType.POPUP
: player.audioPlayerSelected() ? MainPlayer.PlayerType.AUDIO
: null;
}
public static void setListener(final PlayerServiceExtendedEventListener newListener) { public static void setListener(final PlayerServiceExtendedEventListener newListener) {
listener = newListener; listener = newListener;
// Force reload data from service // Force reload data from service

View File

@ -7,17 +7,17 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import androidx.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
import androidx.preference.PreferenceManager;
import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoader;
@ -187,6 +187,22 @@ public final class NavigationHelper {
startService(context, intent); startService(context, intent);
} }
public static void enqueueOnVideoPlayer(final Context context, final PlayQueue queue,
final boolean resumePlayback) {
enqueueOnVideoPlayer(context, queue, false, resumePlayback);
}
public static void enqueueOnVideoPlayer(final Context context, final PlayQueue queue,
final boolean selectOnAppend,
final boolean resumePlayback) {
final Intent intent = getPlayerEnqueueIntent(
context, MainPlayer.class, queue, selectOnAppend, resumePlayback);
intent.putExtra(VideoPlayer.PLAYER_TYPE, VideoPlayer.PLAYER_TYPE_VIDEO);
startService(context, intent);
}
public static void enqueueOnPopupPlayer(final Context context, final PlayQueue queue, public static void enqueueOnPopupPlayer(final Context context, final PlayQueue queue,
final boolean resumePlayback) { final boolean resumePlayback) {
enqueueOnPopupPlayer(context, queue, false, resumePlayback); enqueueOnPopupPlayer(context, queue, false, resumePlayback);

View File

@ -1,12 +1,15 @@
package org.schabi.newpipe.util; package org.schabi.newpipe.util;
import android.content.Context; import android.content.Context;
import android.widget.Toast;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.local.dialog.PlaylistAppendDialog; import org.schabi.newpipe.local.dialog.PlaylistAppendDialog;
import org.schabi.newpipe.player.MainPlayer;
import org.schabi.newpipe.player.helper.PlayerHolder;
import org.schabi.newpipe.player.playqueue.SinglePlayQueue; import org.schabi.newpipe.player.playqueue.SinglePlayQueue;
import java.util.Collections; import java.util.Collections;
@ -16,6 +19,42 @@ public enum StreamDialogEntry {
// enum values with DEFAULT actions // // enum values with DEFAULT actions //
////////////////////////////////////// //////////////////////////////////////
/**
* 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;
}
}),
enqueue_on_background(R.string.enqueue_on_background, (fragment, item) -> enqueue_on_background(R.string.enqueue_on_background, (fragment, item) ->
NavigationHelper.enqueueOnBackgroundPlayer(fragment.getContext(), NavigationHelper.enqueueOnBackgroundPlayer(fragment.getContext(),
new SinglePlayQueue(item), false)), new SinglePlayQueue(item), false)),

View File

@ -445,6 +445,7 @@
<string name="play_queue_stream_detail">Details</string> <string name="play_queue_stream_detail">Details</string>
<string name="play_queue_audio_settings">Audio Settings</string> <string name="play_queue_audio_settings">Audio Settings</string>
<string name="hold_to_append">Hold to enqueue</string> <string name="hold_to_append">Hold to enqueue</string>
<string name="enqueue_stream">Enqueue stream</string>
<string name="enqueue_on_background">Enqueue in the background</string> <string name="enqueue_on_background">Enqueue in the background</string>
<string name="enqueue_on_popup">Enqueue in a popup</string> <string name="enqueue_on_popup">Enqueue in a popup</string>
<string name="start_here_on_main">Start playing here</string> <string name="start_here_on_main">Start playing here</string>