Skip to content

Commit f06d960

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 4b99cdc commit f06d960

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;
@@ -1158,7 +1159,8 @@ private void openMainPlayer() {
11581159

11591160
final Context context = requireContext();
11601161
final Intent playerIntent =
1161-
NavigationHelper.getPlayerIntent(context, PlayerService.class, queue)
1162+
NavigationHelper.getPlayerIntent(context, PlayerService.class, queue,
1163+
PlayerIntentType.AllOthers)
11621164
.putExtra(Player.PLAY_WHEN_READY, autoPlayEnabled)
11631165
.putExtra(Player.RESUME_PLAYBACK, true);
11641166
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
@@ -155,11 +155,10 @@ public final class Player implements PlaybackListener, Listener {
155155
public static final String REPEAT_MODE = "repeat_mode";
156156
public static final String PLAYBACK_QUALITY = "playback_quality";
157157
public static final String PLAY_QUEUE_KEY = "play_queue_key";
158-
public static final String ENQUEUE = "enqueue";
159-
public static final String ENQUEUE_NEXT = "enqueue_next";
160158
public static final String RESUME_PLAYBACK = "resume_playback";
161159
public static final String PLAY_WHEN_READY = "play_when_ready";
162160
public static final String PLAYER_TYPE = "player_type";
161+
public static final String PLAYER_INTENT_TYPE = "player_intent_type";
163162

164163
/*//////////////////////////////////////////////////////////////////////////
165164
// Time constants
@@ -356,22 +355,26 @@ public void handleIntent(@NonNull final Intent intent) {
356355
videoResolver.setPlaybackQuality(intent.getStringExtra(PLAYBACK_QUALITY));
357356
}
358357

359-
// Resolve enqueue intents
360-
if (intent.getBooleanExtra(ENQUEUE, false)) {
361-
if (playQueue != null) {
362-
playQueue.append(newQueue.getStreams());
363-
}
364-
return;
365-
}
358+
final PlayerIntentType playerIntentType = intent.getParcelableExtra(PLAYER_INTENT_TYPE);
366359

367-
// Resolve enqueue next intents
368-
if (intent.getBooleanExtra(ENQUEUE_NEXT, false)) {
369-
if (playQueue != null) {
370-
final int currentIndex = playQueue.getIndex();
371-
playQueue.append(newQueue.getStreams());
372-
playQueue.move(playQueue.size() - 1, currentIndex + 1);
360+
switch (playerIntentType) {
361+
case Enqueue -> {
362+
if (playQueue != null) {
363+
playQueue.append(newQueue.getStreams());
364+
}
365+
return;
366+
}
367+
case EnqueueNext -> {
368+
if (playQueue != null) {
369+
final int currentIndex = playQueue.getIndex();
370+
playQueue.append(newQueue.getStreams());
371+
playQueue.move(playQueue.size() - 1, currentIndex + 1);
372+
}
373+
return;
374+
}
375+
case AllOthers -> {
376+
// fallthrough; TODO: put other intent data in separate cases
373377
}
374-
return;
375378
}
376379

377380
// 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

@@ -256,7 +257,8 @@ private Intent getIntentForNotification() {
256257
} else {
257258
// We are playing in fragment. Don't open another activity just show fragment. That's it
258259
final Intent intent = NavigationHelper.getPlayerIntent(
259-
player.getContext(), MainActivity.class, null);
260+
player.getContext(), MainActivity.class, null,
261+
PlayerIntentType.AllOthers);
260262
intent.putExtra(Player.RESUME_PLAYBACK, true);
261263
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
262264
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) {
@@ -94,6 +97,7 @@ public static <T> Intent getPlayerIntent(@NonNull final Context context,
9497
}
9598
}
9699
intent.putExtra(Player.PLAYER_TYPE, PlayerType.MAIN.valueForIntent());
100+
intent.putExtra(Player.PLAYER_INTENT_TYPE, (Parcelable) playerIntentType);
97101

98102
return intent;
99103
}
@@ -102,8 +106,7 @@ public static <T> Intent getPlayerIntent(@NonNull final Context context,
102106
public static <T> Intent getPlayerEnqueueNextIntent(@NonNull final Context context,
103107
@NonNull final Class<T> targetClazz,
104108
@Nullable final PlayQueue playQueue) {
105-
return getPlayerIntent(context, targetClazz, playQueue)
106-
.putExtra(Player.ENQUEUE_NEXT, true)
109+
return getPlayerIntent(context, targetClazz, playQueue, PlayerIntentType.EnqueueNext)
107110
// see comment in `getPlayerEnqueueIntent` as to why `resumePlayback` is false
108111
.putExtra(Player.RESUME_PLAYBACK, false);
109112
}
@@ -139,7 +142,8 @@ public static void playOnPopupPlayer(final Context context,
139142

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

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

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

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

0 commit comments

Comments
 (0)