Skip to content

Commit 5750ef6

Browse files
committed
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.
1 parent ab7d137 commit 5750ef6

5 files changed

Lines changed: 52 additions & 25 deletions

File tree

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
import org.schabi.newpipe.local.history.HistoryRecordManager;
9494
import org.schabi.newpipe.local.playlist.LocalPlaylistFragment;
9595
import org.schabi.newpipe.player.Player;
96+
import org.schabi.newpipe.player.PlayerIntentType;
9697
import org.schabi.newpipe.player.PlayerService;
9798
import org.schabi.newpipe.player.PlayerType;
9899
import org.schabi.newpipe.player.event.OnKeyDownListener;
@@ -1168,7 +1169,8 @@ private void openMainPlayer() {
11681169

11691170
final Context context = requireContext();
11701171
final Intent playerIntent =
1171-
NavigationHelper.getPlayerIntent(context, PlayerService.class, queue)
1172+
NavigationHelper.getPlayerIntent(context, PlayerService.class, queue,
1173+
PlayerIntentType.AllOthers)
11721174
.putExtra(Player.PLAY_WHEN_READY, autoPlayEnabled)
11731175
.putExtra(Player.RESUME_PLAYBACK, true);
11741176
ContextCompat.startForegroundService(activity, playerIntent);

app/src/main/java/org/schabi/newpipe/player/Player.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,10 @@ public final class Player implements PlaybackListener, Listener {
157157
public static final String REPEAT_MODE = "repeat_mode";
158158
public static final String PLAYBACK_QUALITY = "playback_quality";
159159
public static final String PLAY_QUEUE_KEY = "play_queue_key";
160-
public static final String ENQUEUE = "enqueue";
161-
public static final String ENQUEUE_NEXT = "enqueue_next";
162160
public static final String RESUME_PLAYBACK = "resume_playback";
163161
public static final String PLAY_WHEN_READY = "play_when_ready";
164162
public static final String PLAYER_TYPE = "player_type";
163+
public static final String PLAYER_INTENT_TYPE = "player_intent_type";
165164

166165
/*//////////////////////////////////////////////////////////////////////////
167166
// Time constants
@@ -365,22 +364,26 @@ public void handleIntent(@NonNull final Intent intent) {
365364
videoResolver.setPlaybackQuality(intent.getStringExtra(PLAYBACK_QUALITY));
366365
}
367366

368-
// Resolve enqueue intents
369-
if (intent.getBooleanExtra(ENQUEUE, false)) {
370-
if (playQueue != null) {
371-
playQueue.append(newQueue.getStreams());
372-
}
373-
return;
374-
}
367+
final PlayerIntentType playerIntentType = intent.getParcelableExtra(PLAYER_INTENT_TYPE);
375368

376-
// Resolve enqueue next intents
377-
if (intent.getBooleanExtra(ENQUEUE_NEXT, false)) {
378-
if (playQueue != null) {
379-
final int currentIndex = playQueue.getIndex();
380-
playQueue.append(newQueue.getStreams());
381-
playQueue.move(playQueue.size() - 1, currentIndex + 1);
369+
switch (playerIntentType) {
370+
case Enqueue -> {
371+
if (playQueue != null) {
372+
playQueue.append(newQueue.getStreams());
373+
}
374+
return;
375+
}
376+
case EnqueueNext -> {
377+
if (playQueue != null) {
378+
final int currentIndex = playQueue.getIndex();
379+
playQueue.append(newQueue.getStreams());
380+
playQueue.move(playQueue.size() - 1, currentIndex + 1);
381+
}
382+
return;
383+
}
384+
case AllOthers -> {
385+
// fallthrough; TODO: put other intent data in separate cases
382386
}
383-
return;
384387
}
385388

386389
// initPlayback Parameters
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.schabi.newpipe.player
2+
3+
import android.os.Parcelable
4+
import kotlinx.parcelize.Parcelize
5+
6+
// We model this as an enum class plus one struct for each enum value
7+
// so we can consume it from Java properly. After converting to Kotlin,
8+
// we could switch to a sealed enum class & a proper Kotlin `when` match.
9+
10+
@Parcelize
11+
enum class PlayerIntentType : Parcelable {
12+
Enqueue,
13+
EnqueueNext,
14+
AllOthers
15+
}

app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.schabi.newpipe.MainActivity;
2424
import org.schabi.newpipe.R;
2525
import org.schabi.newpipe.player.Player;
26+
import org.schabi.newpipe.player.PlayerIntentType;
2627
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
2728
import org.schabi.newpipe.util.NavigationHelper;
2829

@@ -254,7 +255,8 @@ private Intent getIntentForNotification() {
254255
} else {
255256
// We are playing in fragment. Don't open another activity just show fragment. That's it
256257
final Intent intent = NavigationHelper.getPlayerIntent(
257-
player.getContext(), MainActivity.class, null);
258+
player.getContext(), MainActivity.class, null,
259+
PlayerIntentType.AllOthers);
258260
intent.putExtra(Player.RESUME_PLAYBACK, true);
259261
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
260262
intent.setAction(Intent.ACTION_MAIN);

app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.content.Intent;
1010
import android.net.Uri;
1111
import android.os.Build;
12+
import android.os.Parcelable;
1213
import android.util.Log;
1314
import android.widget.Toast;
1415

@@ -57,6 +58,7 @@
5758
import org.schabi.newpipe.local.subscription.SubscriptionsImportFragment;
5859
import org.schabi.newpipe.player.PlayQueueActivity;
5960
import org.schabi.newpipe.player.Player;
61+
import org.schabi.newpipe.player.PlayerIntentType;
6062
import org.schabi.newpipe.player.PlayerService;
6163
import org.schabi.newpipe.player.PlayerType;
6264
import org.schabi.newpipe.player.helper.PlayerHelper;
@@ -84,7 +86,8 @@ private NavigationHelper() {
8486
@NonNull
8587
public static <T> Intent getPlayerIntent(@NonNull final Context context,
8688
@NonNull final Class<T> targetClazz,
87-
@Nullable final PlayQueue playQueue) {
89+
@Nullable final PlayQueue playQueue,
90+
@NonNull final PlayerIntentType playerIntentType) {
8891
final Intent intent = new Intent(context, targetClazz);
8992

9093
if (playQueue != null) {
@@ -95,6 +98,7 @@ public static <T> Intent getPlayerIntent(@NonNull final Context context,
9598
}
9699
intent.putExtra(Player.PLAYER_TYPE, PlayerType.MAIN.valueForIntent());
97100
intent.putExtra(PlayerService.SHOULD_START_FOREGROUND_EXTRA, true);
101+
intent.putExtra(Player.PLAYER_INTENT_TYPE, (Parcelable) playerIntentType);
98102

99103
return intent;
100104
}
@@ -103,8 +107,7 @@ public static <T> Intent getPlayerIntent(@NonNull final Context context,
103107
public static <T> Intent getPlayerEnqueueNextIntent(@NonNull final Context context,
104108
@NonNull final Class<T> targetClazz,
105109
@Nullable final PlayQueue playQueue) {
106-
return getPlayerIntent(context, targetClazz, playQueue)
107-
.putExtra(Player.ENQUEUE_NEXT, true)
110+
return getPlayerIntent(context, targetClazz, playQueue, PlayerIntentType.EnqueueNext)
108111
// see comment in `getPlayerEnqueueIntent` as to why `resumePlayback` is false
109112
.putExtra(Player.RESUME_PLAYBACK, false);
110113
}
@@ -140,7 +143,8 @@ public static void playOnPopupPlayer(final Context context,
140143

141144
Toast.makeText(context, R.string.popup_playing_toast, Toast.LENGTH_SHORT).show();
142145

143-
final Intent intent = getPlayerIntent(context, PlayerService.class, queue);
146+
final Intent intent = getPlayerIntent(context, PlayerService.class, queue,
147+
PlayerIntentType.AllOthers);
144148
intent.putExtra(Player.PLAYER_TYPE, PlayerType.POPUP.valueForIntent())
145149
.putExtra(Player.RESUME_PLAYBACK, resumePlayback);
146150
ContextCompat.startForegroundService(context, intent);
@@ -152,7 +156,8 @@ public static void playOnBackgroundPlayer(final Context context,
152156
Toast.makeText(context, R.string.background_player_playing_toast, Toast.LENGTH_SHORT)
153157
.show();
154158

155-
final Intent intent = getPlayerIntent(context, PlayerService.class, queue);
159+
final Intent intent = getPlayerIntent(context, PlayerService.class, queue,
160+
PlayerIntentType.AllOthers);
156161
intent.putExtra(Player.PLAYER_TYPE, PlayerType.AUDIO.valueForIntent());
157162
intent.putExtra(Player.RESUME_PLAYBACK, resumePlayback);
158163
ContextCompat.startForegroundService(context, intent);
@@ -175,8 +180,8 @@ public static void enqueueOnPlayer(final Context context,
175180
// slightly different behaviour than the normal play action: the latter resumes playback,
176181
// the former doesn't. (note that enqueue can be triggered when nothing is playing only
177182
// by long pressing the video detail fragment, playlist or channel controls
178-
final Intent intent = getPlayerIntent(context, PlayerService.class, queue)
179-
.putExtra(Player.ENQUEUE, true)
183+
final Intent intent = getPlayerIntent(context, PlayerService.class, queue,
184+
PlayerIntentType.Enqueue)
180185
.putExtra(Player.RESUME_PLAYBACK, false);
181186

182187
intent.putExtra(Player.PLAYER_TYPE, playerType.valueForIntent());

0 commit comments

Comments
 (0)