mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-09-04 11:57:57 +00:00
Player/handleIntent: start converting intent data to enum
The goal here is to convert all player intents to use a single enum with extra data for each case. The queue ones are pretty easy, they don’t carry any extra data. We fall through for everything else for now.
This commit is contained in:
@@ -93,6 +93,7 @@ import org.schabi.newpipe.local.dialog.PlaylistDialog;
|
|||||||
import org.schabi.newpipe.local.history.HistoryRecordManager;
|
import org.schabi.newpipe.local.history.HistoryRecordManager;
|
||||||
import org.schabi.newpipe.local.playlist.LocalPlaylistFragment;
|
import org.schabi.newpipe.local.playlist.LocalPlaylistFragment;
|
||||||
import org.schabi.newpipe.player.Player;
|
import org.schabi.newpipe.player.Player;
|
||||||
|
import org.schabi.newpipe.player.PlayerIntentType;
|
||||||
import org.schabi.newpipe.player.PlayerService;
|
import org.schabi.newpipe.player.PlayerService;
|
||||||
import org.schabi.newpipe.player.PlayerType;
|
import org.schabi.newpipe.player.PlayerType;
|
||||||
import org.schabi.newpipe.player.event.OnKeyDownListener;
|
import org.schabi.newpipe.player.event.OnKeyDownListener;
|
||||||
@@ -1168,7 +1169,8 @@ public final class VideoDetailFragment
|
|||||||
|
|
||||||
final Context context = requireContext();
|
final Context context = requireContext();
|
||||||
final Intent playerIntent =
|
final Intent playerIntent =
|
||||||
NavigationHelper.getPlayerIntent(context, PlayerService.class, queue)
|
NavigationHelper.getPlayerIntent(context, PlayerService.class, queue,
|
||||||
|
PlayerIntentType.AllOthers)
|
||||||
.putExtra(Player.PLAY_WHEN_READY, autoPlayEnabled)
|
.putExtra(Player.PLAY_WHEN_READY, autoPlayEnabled)
|
||||||
.putExtra(Player.RESUME_PLAYBACK, true);
|
.putExtra(Player.RESUME_PLAYBACK, true);
|
||||||
ContextCompat.startForegroundService(activity, playerIntent);
|
ContextCompat.startForegroundService(activity, playerIntent);
|
||||||
|
@@ -157,11 +157,10 @@ public final class Player implements PlaybackListener, Listener {
|
|||||||
public static final String REPEAT_MODE = "repeat_mode";
|
public static final String REPEAT_MODE = "repeat_mode";
|
||||||
public static final String PLAYBACK_QUALITY = "playback_quality";
|
public static final String PLAYBACK_QUALITY = "playback_quality";
|
||||||
public static final String PLAY_QUEUE_KEY = "play_queue_key";
|
public static final String PLAY_QUEUE_KEY = "play_queue_key";
|
||||||
public static final String ENQUEUE = "enqueue";
|
|
||||||
public static final String ENQUEUE_NEXT = "enqueue_next";
|
|
||||||
public static final String RESUME_PLAYBACK = "resume_playback";
|
public static final String RESUME_PLAYBACK = "resume_playback";
|
||||||
public static final String PLAY_WHEN_READY = "play_when_ready";
|
public static final String PLAY_WHEN_READY = "play_when_ready";
|
||||||
public static final String PLAYER_TYPE = "player_type";
|
public static final String PLAYER_TYPE = "player_type";
|
||||||
|
public static final String PLAYER_INTENT_TYPE = "player_intent_type";
|
||||||
|
|
||||||
/*//////////////////////////////////////////////////////////////////////////
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
// Time constants
|
// Time constants
|
||||||
@@ -367,22 +366,26 @@ public final class Player implements PlaybackListener, Listener {
|
|||||||
videoResolver.setPlaybackQuality(intent.getStringExtra(PLAYBACK_QUALITY));
|
videoResolver.setPlaybackQuality(intent.getStringExtra(PLAYBACK_QUALITY));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve enqueue intents
|
final PlayerIntentType playerIntentType = intent.getParcelableExtra(PLAYER_INTENT_TYPE);
|
||||||
if (intent.getBooleanExtra(ENQUEUE, false)) {
|
|
||||||
if (playQueue != null) {
|
|
||||||
playQueue.append(newQueue.getStreams());
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Resolve enqueue next intents
|
switch (playerIntentType) {
|
||||||
if (intent.getBooleanExtra(ENQUEUE_NEXT, false)) {
|
case Enqueue -> {
|
||||||
if (playQueue != null) {
|
if (playQueue != null) {
|
||||||
final int currentIndex = playQueue.getIndex();
|
playQueue.append(newQueue.getStreams());
|
||||||
playQueue.append(newQueue.getStreams());
|
}
|
||||||
playQueue.move(playQueue.size() - 1, currentIndex + 1);
|
return;
|
||||||
|
}
|
||||||
|
case EnqueueNext -> {
|
||||||
|
if (playQueue != null) {
|
||||||
|
final int currentIndex = playQueue.getIndex();
|
||||||
|
playQueue.append(newQueue.getStreams());
|
||||||
|
playQueue.move(playQueue.size() - 1, currentIndex + 1);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case AllOthers -> {
|
||||||
|
// fallthrough; TODO: put other intent data in separate cases
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// initPlayback Parameters
|
// initPlayback Parameters
|
||||||
|
@@ -0,0 +1,15 @@
|
|||||||
|
package org.schabi.newpipe.player
|
||||||
|
|
||||||
|
import android.os.Parcelable
|
||||||
|
import kotlinx.parcelize.Parcelize
|
||||||
|
|
||||||
|
// We model this as an enum class plus one struct for each enum value
|
||||||
|
// so we can consume it from Java properly. After converting to Kotlin,
|
||||||
|
// we could switch to a sealed enum class & a proper Kotlin `when` match.
|
||||||
|
|
||||||
|
@Parcelize
|
||||||
|
enum class PlayerIntentType : Parcelable {
|
||||||
|
Enqueue,
|
||||||
|
EnqueueNext,
|
||||||
|
AllOthers
|
||||||
|
}
|
@@ -23,6 +23,7 @@ import androidx.core.content.ContextCompat;
|
|||||||
import org.schabi.newpipe.MainActivity;
|
import org.schabi.newpipe.MainActivity;
|
||||||
import org.schabi.newpipe.R;
|
import org.schabi.newpipe.R;
|
||||||
import org.schabi.newpipe.player.Player;
|
import org.schabi.newpipe.player.Player;
|
||||||
|
import org.schabi.newpipe.player.PlayerIntentType;
|
||||||
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
|
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
|
||||||
import org.schabi.newpipe.util.NavigationHelper;
|
import org.schabi.newpipe.util.NavigationHelper;
|
||||||
|
|
||||||
@@ -256,7 +257,8 @@ public final class NotificationUtil {
|
|||||||
} else {
|
} else {
|
||||||
// We are playing in fragment. Don't open another activity just show fragment. That's it
|
// We are playing in fragment. Don't open another activity just show fragment. That's it
|
||||||
final Intent intent = NavigationHelper.getPlayerIntent(
|
final Intent intent = NavigationHelper.getPlayerIntent(
|
||||||
player.getContext(), MainActivity.class, null);
|
player.getContext(), MainActivity.class, null,
|
||||||
|
PlayerIntentType.AllOthers);
|
||||||
intent.putExtra(Player.RESUME_PLAYBACK, true);
|
intent.putExtra(Player.RESUME_PLAYBACK, true);
|
||||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
intent.setAction(Intent.ACTION_MAIN);
|
intent.setAction(Intent.ACTION_MAIN);
|
||||||
|
@@ -9,6 +9,7 @@ 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 android.os.Parcelable;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
@@ -57,6 +58,7 @@ import org.schabi.newpipe.local.subscription.SubscriptionFragment;
|
|||||||
import org.schabi.newpipe.local.subscription.SubscriptionsImportFragment;
|
import org.schabi.newpipe.local.subscription.SubscriptionsImportFragment;
|
||||||
import org.schabi.newpipe.player.PlayQueueActivity;
|
import org.schabi.newpipe.player.PlayQueueActivity;
|
||||||
import org.schabi.newpipe.player.Player;
|
import org.schabi.newpipe.player.Player;
|
||||||
|
import org.schabi.newpipe.player.PlayerIntentType;
|
||||||
import org.schabi.newpipe.player.PlayerService;
|
import org.schabi.newpipe.player.PlayerService;
|
||||||
import org.schabi.newpipe.player.PlayerType;
|
import org.schabi.newpipe.player.PlayerType;
|
||||||
import org.schabi.newpipe.player.helper.PlayerHelper;
|
import org.schabi.newpipe.player.helper.PlayerHelper;
|
||||||
@@ -84,7 +86,8 @@ public final class NavigationHelper {
|
|||||||
@NonNull
|
@NonNull
|
||||||
public static <T> Intent getPlayerIntent(@NonNull final Context context,
|
public static <T> Intent getPlayerIntent(@NonNull final Context context,
|
||||||
@NonNull final Class<T> targetClazz,
|
@NonNull final Class<T> targetClazz,
|
||||||
@Nullable final PlayQueue playQueue) {
|
@Nullable final PlayQueue playQueue,
|
||||||
|
@NonNull final PlayerIntentType playerIntentType) {
|
||||||
final Intent intent = new Intent(context, targetClazz);
|
final Intent intent = new Intent(context, targetClazz);
|
||||||
|
|
||||||
if (playQueue != null) {
|
if (playQueue != null) {
|
||||||
@@ -95,6 +98,7 @@ public final class NavigationHelper {
|
|||||||
}
|
}
|
||||||
intent.putExtra(Player.PLAYER_TYPE, PlayerType.MAIN.valueForIntent());
|
intent.putExtra(Player.PLAYER_TYPE, PlayerType.MAIN.valueForIntent());
|
||||||
intent.putExtra(PlayerService.SHOULD_START_FOREGROUND_EXTRA, true);
|
intent.putExtra(PlayerService.SHOULD_START_FOREGROUND_EXTRA, true);
|
||||||
|
intent.putExtra(Player.PLAYER_INTENT_TYPE, (Parcelable) playerIntentType);
|
||||||
|
|
||||||
return intent;
|
return intent;
|
||||||
}
|
}
|
||||||
@@ -103,8 +107,7 @@ public final class NavigationHelper {
|
|||||||
public static <T> Intent getPlayerEnqueueNextIntent(@NonNull final Context context,
|
public static <T> Intent getPlayerEnqueueNextIntent(@NonNull final Context context,
|
||||||
@NonNull final Class<T> targetClazz,
|
@NonNull final Class<T> targetClazz,
|
||||||
@Nullable final PlayQueue playQueue) {
|
@Nullable final PlayQueue playQueue) {
|
||||||
return getPlayerIntent(context, targetClazz, playQueue)
|
return getPlayerIntent(context, targetClazz, playQueue, PlayerIntentType.EnqueueNext)
|
||||||
.putExtra(Player.ENQUEUE_NEXT, true)
|
|
||||||
// see comment in `getPlayerEnqueueIntent` as to why `resumePlayback` is false
|
// see comment in `getPlayerEnqueueIntent` as to why `resumePlayback` is false
|
||||||
.putExtra(Player.RESUME_PLAYBACK, false);
|
.putExtra(Player.RESUME_PLAYBACK, false);
|
||||||
}
|
}
|
||||||
@@ -140,7 +143,8 @@ public final class NavigationHelper {
|
|||||||
|
|
||||||
Toast.makeText(context, R.string.popup_playing_toast, Toast.LENGTH_SHORT).show();
|
Toast.makeText(context, R.string.popup_playing_toast, Toast.LENGTH_SHORT).show();
|
||||||
|
|
||||||
final Intent intent = getPlayerIntent(context, PlayerService.class, queue);
|
final Intent intent = getPlayerIntent(context, PlayerService.class, queue,
|
||||||
|
PlayerIntentType.AllOthers);
|
||||||
intent.putExtra(Player.PLAYER_TYPE, PlayerType.POPUP.valueForIntent())
|
intent.putExtra(Player.PLAYER_TYPE, PlayerType.POPUP.valueForIntent())
|
||||||
.putExtra(Player.RESUME_PLAYBACK, resumePlayback);
|
.putExtra(Player.RESUME_PLAYBACK, resumePlayback);
|
||||||
ContextCompat.startForegroundService(context, intent);
|
ContextCompat.startForegroundService(context, intent);
|
||||||
@@ -152,7 +156,8 @@ public final class NavigationHelper {
|
|||||||
Toast.makeText(context, R.string.background_player_playing_toast, Toast.LENGTH_SHORT)
|
Toast.makeText(context, R.string.background_player_playing_toast, Toast.LENGTH_SHORT)
|
||||||
.show();
|
.show();
|
||||||
|
|
||||||
final Intent intent = getPlayerIntent(context, PlayerService.class, queue);
|
final Intent intent = getPlayerIntent(context, PlayerService.class, queue,
|
||||||
|
PlayerIntentType.AllOthers);
|
||||||
intent.putExtra(Player.PLAYER_TYPE, PlayerType.AUDIO.valueForIntent());
|
intent.putExtra(Player.PLAYER_TYPE, PlayerType.AUDIO.valueForIntent());
|
||||||
intent.putExtra(Player.RESUME_PLAYBACK, resumePlayback);
|
intent.putExtra(Player.RESUME_PLAYBACK, resumePlayback);
|
||||||
ContextCompat.startForegroundService(context, intent);
|
ContextCompat.startForegroundService(context, intent);
|
||||||
@@ -175,8 +180,8 @@ public final class NavigationHelper {
|
|||||||
// slightly different behaviour than the normal play action: the latter resumes playback,
|
// slightly different behaviour than the normal play action: the latter resumes playback,
|
||||||
// the former doesn't. (note that enqueue can be triggered when nothing is playing only
|
// the former doesn't. (note that enqueue can be triggered when nothing is playing only
|
||||||
// by long pressing the video detail fragment, playlist or channel controls
|
// by long pressing the video detail fragment, playlist or channel controls
|
||||||
final Intent intent = getPlayerIntent(context, PlayerService.class, queue)
|
final Intent intent = getPlayerIntent(context, PlayerService.class, queue,
|
||||||
.putExtra(Player.ENQUEUE, true)
|
PlayerIntentType.Enqueue)
|
||||||
.putExtra(Player.RESUME_PLAYBACK, false);
|
.putExtra(Player.RESUME_PLAYBACK, false);
|
||||||
|
|
||||||
intent.putExtra(Player.PLAYER_TYPE, playerType.valueForIntent());
|
intent.putExtra(Player.PLAYER_TYPE, playerType.valueForIntent());
|
||||||
|
Reference in New Issue
Block a user