Skip to content

Commit 32e2053

Browse files
committed
PlayerUIList: remove remaining uses of getOpt
mediaSession is now `@NonNull`, so the getter is as well.
1 parent afdf700 commit 32e2053

4 files changed

Lines changed: 23 additions & 29 deletions

File tree

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -477,22 +477,23 @@ public void handleIntent(@NonNull final Intent intent) {
477477
}
478478

479479
private void initUIsForCurrentPlayerType() {
480-
if ((UIs.getOpt(MainPlayerUi.class).isPresent() && playerType == PlayerType.MAIN)
481-
|| (UIs.getOpt(PopupPlayerUi.class).isPresent()
480+
if ((UIs.get(MainPlayerUi.class) != null && playerType == PlayerType.MAIN)
481+
|| (UIs.get(PopupPlayerUi.class) != null
482482
&& playerType == PlayerType.POPUP)) {
483483
// correct UI already in place
484484
return;
485485
}
486486

487487
// try to reuse binding if possible
488-
final PlayerBinding binding = UIs.getOpt(VideoPlayerUi.class).map(VideoPlayerUi::getBinding)
489-
.orElseGet(() -> {
490-
if (playerType == PlayerType.AUDIO) {
491-
return null;
492-
} else {
493-
return PlayerBinding.inflate(LayoutInflater.from(context));
494-
}
495-
});
488+
@Nullable final VideoPlayerUi ui = UIs.get(VideoPlayerUi.class);
489+
final PlayerBinding binding;
490+
if (ui != null) {
491+
binding = ui.getBinding();
492+
} else if (playerType == PlayerType.AUDIO) {
493+
binding = null;
494+
} else {
495+
binding = PlayerBinding.inflate(LayoutInflater.from(context));
496+
}
496497

497498
switch (playerType) {
498499
case MAIN:

app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ public void handleMediaButtonIntent(final Intent intent) {
124124
MediaButtonReceiver.handleIntent(mediaSession, intent);
125125
}
126126

127-
public Optional<MediaSessionCompat.Token> getSessionToken() {
128-
return Optional.ofNullable(mediaSession).map(MediaSessionCompat::getSessionToken);
127+
128+
@NonNull
129+
public MediaSessionCompat.Token getSessionToken() {
130+
return mediaSession.getSessionToken();
129131
}
130132

131133

@@ -138,7 +140,10 @@ private ForwardingPlayer getForwardingPlayer() {
138140
public void play() {
139141
player.play();
140142
// hide the player controls even if the play command came from the media session
141-
player.UIs().getOpt(VideoPlayerUi.class).ifPresent(ui -> ui.hideControls(0, 0));
143+
final VideoPlayerUi ui = player.UIs().get(VideoPlayerUi.class);
144+
if (ui != null) {
145+
ui.hideControls(0, 0);
146+
}
142147
}
143148

144149
@Override

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ private synchronized NotificationCompat.Builder createNotification() {
101101
final int[] compactSlots = initializeNotificationSlots();
102102
mediaStyle.setShowActionsInCompactView(compactSlots);
103103
}
104-
player.UIs()
105-
.getOpt(MediaSessionPlayerUi.class)
106-
.flatMap(MediaSessionPlayerUi::getSessionToken)
107-
.ifPresent(mediaStyle::setMediaSession);
104+
@Nullable final MediaSessionPlayerUi ui = player.UIs().get(MediaSessionPlayerUi.class);
105+
if (ui != null) {
106+
mediaStyle.setMediaSession(ui.getSessionToken());
107+
}
108108

109109
// setup notification builder
110110
builder.setStyle(mediaStyle)

app/src/main/java/org/schabi/newpipe/player/ui/PlayerUiList.kt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.schabi.newpipe.player.ui
22

33
import org.schabi.newpipe.util.GuardedByMutex
4-
import java.util.Optional
54

65
/**
76
* Creates a [PlayerUiList] starting with the provided player uis. The provided player uis
@@ -99,17 +98,6 @@ class PlayerUiList(vararg initialPlayerUis: PlayerUi) {
9998
return@runWithLockSync null
10099
}
101100

102-
/**
103-
* @param playerUiType the class of the player UI to return;
104-
* the [Class.isInstance] method will be used, so even subclasses could be returned
105-
* @param T the class type parameter
106-
* @return the first player UI of the required type found in the list, or an empty
107-
* [Optional] otherwise
108-
</T> */
109-
@Deprecated("use get", ReplaceWith("get(playerUiType)"))
110-
fun <T : PlayerUi> getOpt(playerUiType: Class<T>): Optional<T> =
111-
Optional.ofNullable(get(playerUiType))
112-
113101
/**
114102
* Calls the provided consumer on all player UIs in the list, in order of addition.
115103
* @param consumer the consumer to call with player UIs

0 commit comments

Comments
 (0)