From 5750ef6aa8b0d3dbee9c589699cd1e5e1d001027 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sun, 4 May 2025 23:46:29 +0200 Subject: [PATCH] Player/handleIntent: start converting intent data to enum MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../fragments/detail/VideoDetailFragment.java | 4 ++- .../org/schabi/newpipe/player/Player.java | 35 ++++++++++--------- .../schabi/newpipe/player/PlayerIntentType.kt | 15 ++++++++ .../player/notification/NotificationUtil.java | 4 ++- .../schabi/newpipe/util/NavigationHelper.java | 19 ++++++---- 5 files changed, 52 insertions(+), 25 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/player/PlayerIntentType.kt diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 4d273a859..c43007da4 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -93,6 +93,7 @@ import org.schabi.newpipe.local.dialog.PlaylistDialog; import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.local.playlist.LocalPlaylistFragment; import org.schabi.newpipe.player.Player; +import org.schabi.newpipe.player.PlayerIntentType; import org.schabi.newpipe.player.PlayerService; import org.schabi.newpipe.player.PlayerType; import org.schabi.newpipe.player.event.OnKeyDownListener; @@ -1168,7 +1169,8 @@ public final class VideoDetailFragment final Context context = requireContext(); 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.RESUME_PLAYBACK, true); ContextCompat.startForegroundService(activity, playerIntent); diff --git a/app/src/main/java/org/schabi/newpipe/player/Player.java b/app/src/main/java/org/schabi/newpipe/player/Player.java index 87f0025cf..e3143fddb 100644 --- a/app/src/main/java/org/schabi/newpipe/player/Player.java +++ b/app/src/main/java/org/schabi/newpipe/player/Player.java @@ -157,11 +157,10 @@ public final class Player implements PlaybackListener, Listener { public static final String REPEAT_MODE = "repeat_mode"; public static final String PLAYBACK_QUALITY = "playback_quality"; 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 PLAY_WHEN_READY = "play_when_ready"; public static final String PLAYER_TYPE = "player_type"; + public static final String PLAYER_INTENT_TYPE = "player_intent_type"; /*////////////////////////////////////////////////////////////////////////// // Time constants @@ -365,22 +364,26 @@ public final class Player implements PlaybackListener, Listener { videoResolver.setPlaybackQuality(intent.getStringExtra(PLAYBACK_QUALITY)); } - // Resolve enqueue intents - if (intent.getBooleanExtra(ENQUEUE, false)) { - if (playQueue != null) { - playQueue.append(newQueue.getStreams()); - } - return; - } + final PlayerIntentType playerIntentType = intent.getParcelableExtra(PLAYER_INTENT_TYPE); - // Resolve enqueue next intents - if (intent.getBooleanExtra(ENQUEUE_NEXT, false)) { - if (playQueue != null) { - final int currentIndex = playQueue.getIndex(); - playQueue.append(newQueue.getStreams()); - playQueue.move(playQueue.size() - 1, currentIndex + 1); + switch (playerIntentType) { + case Enqueue -> { + if (playQueue != null) { + playQueue.append(newQueue.getStreams()); + } + 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 diff --git a/app/src/main/java/org/schabi/newpipe/player/PlayerIntentType.kt b/app/src/main/java/org/schabi/newpipe/player/PlayerIntentType.kt new file mode 100644 index 000000000..9d5e4531a --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/player/PlayerIntentType.kt @@ -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 +} diff --git a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java index c5e8af03f..79ae81de2 100644 --- a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java +++ b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java @@ -23,6 +23,7 @@ import androidx.core.content.ContextCompat; import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.R; import org.schabi.newpipe.player.Player; +import org.schabi.newpipe.player.PlayerIntentType; import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi; import org.schabi.newpipe.util.NavigationHelper; @@ -254,7 +255,8 @@ public final class NotificationUtil { } else { // We are playing in fragment. Don't open another activity just show fragment. That's it final Intent intent = NavigationHelper.getPlayerIntent( - player.getContext(), MainActivity.class, null); + player.getContext(), MainActivity.class, null, + PlayerIntentType.AllOthers); intent.putExtra(Player.RESUME_PLAYBACK, true); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_MAIN); diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index 1dccf501f..a63fcb0c6 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -9,6 +9,7 @@ import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Build; +import android.os.Parcelable; import android.util.Log; 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.player.PlayQueueActivity; import org.schabi.newpipe.player.Player; +import org.schabi.newpipe.player.PlayerIntentType; import org.schabi.newpipe.player.PlayerService; import org.schabi.newpipe.player.PlayerType; import org.schabi.newpipe.player.helper.PlayerHelper; @@ -84,7 +86,8 @@ public final class NavigationHelper { @NonNull public static Intent getPlayerIntent(@NonNull final Context context, @NonNull final Class targetClazz, - @Nullable final PlayQueue playQueue) { + @Nullable final PlayQueue playQueue, + @NonNull final PlayerIntentType playerIntentType) { final Intent intent = new Intent(context, targetClazz); if (playQueue != null) { @@ -95,6 +98,7 @@ public final class NavigationHelper { } intent.putExtra(Player.PLAYER_TYPE, PlayerType.MAIN.valueForIntent()); intent.putExtra(PlayerService.SHOULD_START_FOREGROUND_EXTRA, true); + intent.putExtra(Player.PLAYER_INTENT_TYPE, (Parcelable) playerIntentType); return intent; } @@ -103,8 +107,7 @@ public final class NavigationHelper { public static Intent getPlayerEnqueueNextIntent(@NonNull final Context context, @NonNull final Class targetClazz, @Nullable final PlayQueue playQueue) { - return getPlayerIntent(context, targetClazz, playQueue) - .putExtra(Player.ENQUEUE_NEXT, true) + return getPlayerIntent(context, targetClazz, playQueue, PlayerIntentType.EnqueueNext) // see comment in `getPlayerEnqueueIntent` as to why `resumePlayback` is 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(); - 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()) .putExtra(Player.RESUME_PLAYBACK, resumePlayback); ContextCompat.startForegroundService(context, intent); @@ -152,7 +156,8 @@ public final class NavigationHelper { Toast.makeText(context, R.string.background_player_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.AUDIO.valueForIntent()); intent.putExtra(Player.RESUME_PLAYBACK, resumePlayback); ContextCompat.startForegroundService(context, intent); @@ -175,8 +180,8 @@ public final class NavigationHelper { // 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 // by long pressing the video detail fragment, playlist or channel controls - final Intent intent = getPlayerIntent(context, PlayerService.class, queue) - .putExtra(Player.ENQUEUE, true) + final Intent intent = getPlayerIntent(context, PlayerService.class, queue, + PlayerIntentType.Enqueue) .putExtra(Player.RESUME_PLAYBACK, false); intent.putExtra(Player.PLAYER_TYPE, playerType.valueForIntent());