Skip to content

Commit 45c22c0

Browse files
authored
Merge pull request #12615 from Isira-Seneviratne/Player-intent-refactor
Refactor player intent logic
2 parents 803aba4 + 89c4eb5 commit 45c22c0

File tree

4 files changed

+39
-83
lines changed

4 files changed

+39
-83
lines changed

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

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import androidx.annotation.NonNull;
6262
import androidx.annotation.Nullable;
6363
import androidx.core.content.ContextCompat;
64+
import androidx.core.content.IntentCompat;
6465
import androidx.core.math.MathUtils;
6566
import androidx.preference.PreferenceManager;
6667

@@ -127,6 +128,7 @@
127128
import org.schabi.newpipe.util.image.PicassoHelper;
128129

129130
import java.util.List;
131+
import java.util.Objects;
130132
import java.util.Optional;
131133
import java.util.stream.IntStream;
132134

@@ -351,25 +353,17 @@ public int getOverrideResolutionIndex(final List<VideoStream> sortedVideos,
351353

352354
@SuppressWarnings("MethodLength")
353355
public void handleIntent(@NonNull final Intent intent) {
354-
355-
final PlayerIntentType playerIntentType = intent.getParcelableExtra(PLAYER_INTENT_TYPE);
356+
final var playerIntentType = IntentCompat.getSerializableExtra(intent, PLAYER_INTENT_TYPE,
357+
PlayerIntentType.class);
356358
if (playerIntentType == null) {
357359
return;
358360
}
359-
final PlayerType newPlayerType;
360361
// TODO: this should be in the second switch below, but I’m not sure whether I
361362
// can move the initUIs stuff without breaking the setup for edge cases somehow.
362-
switch (playerIntentType) {
363-
case TimestampChange -> {
364-
// when playing from a timestamp, keep the current player as-is.
365-
newPlayerType = playerType;
366-
}
367-
default -> {
368-
newPlayerType = PlayerType.retrieveFromIntent(intent);
369-
}
363+
// when playing from a timestamp, keep the current player as-is.
364+
if (playerIntentType != PlayerIntentType.TimestampChange) {
365+
playerType = IntentCompat.getSerializableExtra(intent, PLAYER_TYPE, PlayerType.class);
370366
}
371-
372-
playerType = newPlayerType;
373367
initUIsForCurrentPlayerType();
374368
isAudioOnly = audioPlayerSelected();
375369

@@ -410,15 +404,15 @@ public void handleIntent(@NonNull final Intent intent) {
410404
break;
411405
}
412406
case TimestampChange -> {
413-
final TimestampChangeData dat = intent.getParcelableExtra(PLAYER_INTENT_DATA);
414-
assert dat != null;
407+
final var data = Objects.requireNonNull(IntentCompat.getParcelableExtra(intent,
408+
PLAYER_INTENT_DATA, TimestampChangeData.class));
415409
final Single<StreamInfo> single =
416-
ExtractorHelper.getStreamInfo(dat.getServiceId(), dat.getUrl(), false);
410+
ExtractorHelper.getStreamInfo(data.getServiceId(), data.getUrl(), false);
417411
streamItemDisposable.add(single.subscribeOn(Schedulers.io())
418412
.observeOn(AndroidSchedulers.mainThread())
419413
.subscribe(info -> {
420414
final @Nullable PlayQueue oldPlayQueue = playQueue;
421-
info.setStartPosition(dat.getSeconds());
415+
info.setStartPosition(data.getSeconds());
422416
final PlayQueueItem playQueueItem = new PlayQueueItem(info);
423417

424418
// If the stream is already playing,
@@ -432,7 +426,7 @@ public void handleIntent(@NonNull final Intent intent) {
432426
simpleExoPlayer.prepare();
433427
}
434428
simpleExoPlayer.seekTo(oldPlayQueue.getIndex(),
435-
dat.getSeconds() * 1000L);
429+
data.getSeconds() * 1000L);
436430
simpleExoPlayer.setPlayWhenReady(playWhenReady);
437431

438432
} else {
@@ -456,9 +450,9 @@ public void handleIntent(@NonNull final Intent intent) {
456450
// This will only show a snackbar if the passed context has a root view:
457451
// otherwise it will resort to showing a notification, so we are safe
458452
// here.
459-
ErrorUtil.createNotification(context,
460-
new ErrorInfo(throwable, UserAction.PLAY_ON_POPUP, dat.getUrl(),
461-
null, dat.getUrl()));
453+
final var info = new ErrorInfo(throwable, UserAction.PLAY_ON_POPUP,
454+
data.getUrl(), null, data.getUrl());
455+
ErrorUtil.createNotification(context, info);
462456
}));
463457
return;
464458
}

app/src/main/java/org/schabi/newpipe/player/PlayerIntentType.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import kotlinx.parcelize.Parcelize
66
// We model this as an enum class plus one struct for each enum value
77
// so we can consume it from Java properly. After converting to Kotlin,
88
// we could switch to a sealed enum class & a proper Kotlin `when` match.
9-
10-
@Parcelize
11-
enum class PlayerIntentType : Parcelable {
9+
enum class PlayerIntentType {
1210
Enqueue,
1311
EnqueueNext,
1412
TimestampChange,
Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,7 @@
11
package org.schabi.newpipe.player;
22

3-
import static org.schabi.newpipe.player.Player.PLAYER_TYPE;
4-
5-
import android.content.Intent;
6-
73
public enum PlayerType {
84
MAIN,
95
AUDIO,
106
POPUP;
11-
12-
/**
13-
* @return an integer representing this {@link PlayerType}, to be used to save it in intents
14-
* @see #retrieveFromIntent(Intent) Use retrieveFromIntent() to retrieve and convert player type
15-
* integers from an intent
16-
*/
17-
public int valueForIntent() {
18-
return ordinal();
19-
}
20-
21-
/**
22-
* @param intent the intent to retrieve a player type from
23-
* @return the player type integer retrieved from the intent, converted back into a {@link
24-
* PlayerType}, or {@link PlayerType#MAIN} if there is no player type extra in the
25-
* intent
26-
* @throws ArrayIndexOutOfBoundsException if the intent contains an invalid player type integer
27-
* @see #valueForIntent() Use valueForIntent() to obtain valid player type integers
28-
*/
29-
public static PlayerType retrieveFromIntent(final Intent intent) {
30-
return values()[intent.getIntExtra(PLAYER_TYPE, MAIN.valueForIntent())];
31-
}
327
}

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

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

@@ -70,6 +69,7 @@
7069
import org.schabi.newpipe.util.external_communication.ShareUtils;
7170

7271
import java.util.List;
72+
import java.util.Optional;
7373

7474
public final class NavigationHelper {
7575
public static final String MAIN_FRAGMENT_TAG = "main_fragment_tag";
@@ -89,31 +89,22 @@ public static <T> Intent getPlayerIntent(@NonNull final Context context,
8989
@NonNull final Class<T> targetClazz,
9090
@Nullable final PlayQueue playQueue,
9191
@NonNull final PlayerIntentType playerIntentType) {
92-
final Intent intent = new Intent(context, targetClazz);
93-
94-
if (playQueue != null) {
95-
final String cacheKey = SerializedCache.getInstance().put(playQueue, PlayQueue.class);
96-
if (cacheKey != null) {
97-
intent.putExtra(Player.PLAY_QUEUE_KEY, cacheKey);
98-
}
99-
}
100-
intent.putExtra(Player.PLAYER_TYPE, PlayerType.MAIN.valueForIntent());
101-
intent.putExtra(PlayerService.SHOULD_START_FOREGROUND_EXTRA, true);
102-
intent.putExtra(Player.PLAYER_INTENT_TYPE, (Parcelable) playerIntentType);
103-
104-
return intent;
92+
final String cacheKey = Optional.ofNullable(playQueue)
93+
.map(queue -> SerializedCache.getInstance().put(queue, PlayQueue.class))
94+
.orElse(null);
95+
return new Intent(context, targetClazz)
96+
.putExtra(Player.PLAY_QUEUE_KEY, cacheKey)
97+
.putExtra(Player.PLAYER_TYPE, PlayerType.MAIN)
98+
.putExtra(PlayerService.SHOULD_START_FOREGROUND_EXTRA, true)
99+
.putExtra(Player.PLAYER_INTENT_TYPE, playerIntentType);
105100
}
106101

107102
@NonNull
108103
public static Intent getPlayerTimestampIntent(@NonNull final Context context,
109-
@NonNull final TimestampChangeData
110-
timestampChangeData) {
111-
final Intent intent = new Intent(context, PlayerService.class);
112-
113-
intent.putExtra(Player.PLAYER_INTENT_TYPE, (Parcelable) PlayerIntentType.TimestampChange);
114-
intent.putExtra(Player.PLAYER_INTENT_DATA, timestampChangeData);
115-
116-
return intent;
104+
@NonNull final TimestampChangeData data) {
105+
return new Intent(context, PlayerService.class)
106+
.putExtra(Player.PLAYER_INTENT_TYPE, PlayerIntentType.TimestampChange)
107+
.putExtra(Player.PLAYER_INTENT_DATA, data);
117108
}
118109

119110
@NonNull
@@ -156,9 +147,9 @@ public static void playOnPopupPlayer(final Context context,
156147

157148
Toast.makeText(context, R.string.popup_playing_toast, Toast.LENGTH_SHORT).show();
158149

159-
final Intent intent = getPlayerIntent(context, PlayerService.class, queue,
160-
PlayerIntentType.AllOthers);
161-
intent.putExtra(Player.PLAYER_TYPE, PlayerType.POPUP.valueForIntent())
150+
final var intent = getPlayerIntent(context, PlayerService.class, queue,
151+
PlayerIntentType.AllOthers)
152+
.putExtra(Player.PLAYER_TYPE, PlayerType.POPUP)
162153
.putExtra(Player.RESUME_PLAYBACK, resumePlayback);
163154
ContextCompat.startForegroundService(context, intent);
164155
}
@@ -170,9 +161,9 @@ public static void playOnBackgroundPlayer(final Context context,
170161
.show();
171162

172163
final Intent intent = getPlayerIntent(context, PlayerService.class, queue,
173-
PlayerIntentType.AllOthers);
174-
intent.putExtra(Player.PLAYER_TYPE, PlayerType.AUDIO.valueForIntent());
175-
intent.putExtra(Player.RESUME_PLAYBACK, resumePlayback);
164+
PlayerIntentType.AllOthers)
165+
.putExtra(Player.PLAYER_TYPE, PlayerType.AUDIO)
166+
.putExtra(Player.RESUME_PLAYBACK, resumePlayback);
176167
ContextCompat.startForegroundService(context, intent);
177168
}
178169

@@ -195,9 +186,8 @@ public static void enqueueOnPlayer(final Context context,
195186
// by long pressing the video detail fragment, playlist or channel controls
196187
final Intent intent = getPlayerIntent(context, PlayerService.class, queue,
197188
PlayerIntentType.Enqueue)
198-
.putExtra(Player.RESUME_PLAYBACK, false);
199-
200-
intent.putExtra(Player.PLAYER_TYPE, playerType.valueForIntent());
189+
.putExtra(Player.RESUME_PLAYBACK, false)
190+
.putExtra(Player.PLAYER_TYPE, playerType);
201191
ContextCompat.startForegroundService(context, intent);
202192
}
203193

@@ -219,9 +209,8 @@ public static void enqueueNextOnPlayer(final Context context, final PlayQueue qu
219209
playerType = PlayerType.AUDIO;
220210
}
221211
Toast.makeText(context, R.string.enqueued_next, Toast.LENGTH_SHORT).show();
222-
final Intent intent = getPlayerEnqueueNextIntent(context, PlayerService.class, queue);
223-
224-
intent.putExtra(Player.PLAYER_TYPE, playerType.valueForIntent());
212+
final Intent intent = getPlayerEnqueueNextIntent(context, PlayerService.class, queue)
213+
.putExtra(Player.PLAYER_TYPE, playerType);
225214
ContextCompat.startForegroundService(context, intent);
226215
}
227216

0 commit comments

Comments
 (0)