Skip to content

Commit 5b92de4

Browse files
committed
PlayerUIList: rename get to getOpt and make get nullable
In Kotlin, dealing with nulls works better so we don’t need optional.
1 parent 6830890 commit 5b92de4

File tree

6 files changed

+40
-28
lines changed

6 files changed

+40
-28
lines changed

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public void onServiceConnected(final Player connectedPlayer,
243243
// It will do nothing if the player is not in fullscreen mode
244244
hideSystemUiIfNeeded();
245245

246-
final Optional<MainPlayerUi> playerUi = player.UIs().get(MainPlayerUi.class);
246+
final Optional<MainPlayerUi> playerUi = player.UIs().getOpt(MainPlayerUi.class);
247247
if (!player.videoPlayerSelected() && !playAfterConnect) {
248248
return;
249249
}
@@ -519,7 +519,7 @@ private void setOnClickListeners() {
519519
binding.overlayPlayPauseButton.setOnClickListener(v -> {
520520
if (playerIsNotStopped()) {
521521
player.playPause();
522-
player.UIs().get(VideoPlayerUi.class).ifPresent(ui -> ui.hideControls(0, 0));
522+
player.UIs().getOpt(VideoPlayerUi.class).ifPresent(ui -> ui.hideControls(0, 0));
523523
showSystemUi();
524524
} else {
525525
autoPlayEnabled = true; // forcefully start playing
@@ -678,7 +678,7 @@ protected void initListeners() {
678678
@Override
679679
public boolean onKeyDown(final int keyCode) {
680680
return isPlayerAvailable()
681-
&& player.UIs().get(VideoPlayerUi.class)
681+
&& player.UIs().getOpt(VideoPlayerUi.class)
682682
.map(playerUi -> playerUi.onKeyDown(keyCode)).orElse(false);
683683
}
684684

@@ -1018,7 +1018,7 @@ private void toggleFullscreenIfInFullscreenMode() {
10181018
// If a user watched video inside fullscreen mode and than chose another player
10191019
// return to non-fullscreen mode
10201020
if (isPlayerAvailable()) {
1021-
player.UIs().get(MainPlayerUi.class).ifPresent(playerUi -> {
1021+
player.UIs().getOpt(MainPlayerUi.class).ifPresent(playerUi -> {
10221022
if (playerUi.isFullscreen()) {
10231023
playerUi.toggleFullscreen();
10241024
}
@@ -1234,7 +1234,7 @@ private void tryAddVideoPlayerView() {
12341234
// setup the surface view height, so that it fits the video correctly
12351235
setHeightThumbnail();
12361236

1237-
player.UIs().get(MainPlayerUi.class).ifPresent(playerUi -> {
1237+
player.UIs().getOpt(MainPlayerUi.class).ifPresent(playerUi -> {
12381238
// sometimes binding would be null here, even though getView() != null above u.u
12391239
if (binding != null) {
12401240
// prevent from re-adding a view multiple times
@@ -1250,7 +1250,7 @@ private void removeVideoPlayerView() {
12501250
makeDefaultHeightForVideoPlaceholder();
12511251

12521252
if (player != null) {
1253-
player.UIs().get(VideoPlayerUi.class).ifPresent(VideoPlayerUi::removeViewFromParent);
1253+
player.UIs().getOpt(VideoPlayerUi.class).ifPresent(VideoPlayerUi::removeViewFromParent);
12541254
}
12551255
}
12561256

@@ -1317,7 +1317,7 @@ private void setHeightThumbnail(final int newHeight, final DisplayMetrics metric
13171317
binding.detailThumbnailImageView.setMinimumHeight(newHeight);
13181318
if (isPlayerAvailable()) {
13191319
final int maxHeight = (int) (metrics.heightPixels * MAX_PLAYER_HEIGHT);
1320-
player.UIs().get(VideoPlayerUi.class).ifPresent(ui ->
1320+
player.UIs().getOpt(VideoPlayerUi.class).ifPresent(ui ->
13211321
ui.getBinding().surfaceView.setHeights(newHeight,
13221322
ui.isFullscreen() ? newHeight : maxHeight));
13231323
}
@@ -1848,7 +1848,7 @@ public void onServiceStopped() {
18481848
public void onFullscreenStateChanged(final boolean fullscreen) {
18491849
setupBrightness();
18501850
if (!isPlayerAndPlayerServiceAvailable()
1851-
|| player.UIs().get(MainPlayerUi.class).isEmpty()
1851+
|| player.UIs().getOpt(MainPlayerUi.class).isEmpty()
18521852
|| getRoot().map(View::getParent).isEmpty()) {
18531853
return;
18541854
}
@@ -1877,7 +1877,7 @@ public void onScreenRotationButtonClicked() {
18771877
final boolean isLandscape = DeviceUtils.isLandscape(requireContext());
18781878
if (DeviceUtils.isTablet(activity)
18791879
&& (!globalScreenOrientationLocked(activity) || isLandscape)) {
1880-
player.UIs().get(MainPlayerUi.class).ifPresent(MainPlayerUi::toggleFullscreen);
1880+
player.UIs().getOpt(MainPlayerUi.class).ifPresent(MainPlayerUi::toggleFullscreen);
18811881
return;
18821882
}
18831883

@@ -1977,7 +1977,7 @@ public void hideSystemUiIfNeeded() {
19771977
}
19781978

19791979
private boolean isFullscreen() {
1980-
return isPlayerAvailable() && player.UIs().get(VideoPlayerUi.class)
1980+
return isPlayerAvailable() && player.UIs().getOpt(VideoPlayerUi.class)
19811981
.map(VideoPlayerUi::isFullscreen).orElse(false);
19821982
}
19831983

@@ -2054,7 +2054,7 @@ private void checkLandscape() {
20542054
setAutoPlay(true);
20552055
}
20562056

2057-
player.UIs().get(MainPlayerUi.class).ifPresent(MainPlayerUi::checkLandscape);
2057+
player.UIs().getOpt(MainPlayerUi.class).ifPresent(MainPlayerUi::checkLandscape);
20582058
// Let's give a user time to look at video information page if video is not playing
20592059
if (globalScreenOrientationLocked(activity) && !player.isPlaying()) {
20602060
player.play();
@@ -2319,7 +2319,7 @@ && isPlayerAvailable()
23192319
&& player.isPlaying()
23202320
&& !isFullscreen()
23212321
&& !DeviceUtils.isTablet(activity)) {
2322-
player.UIs().get(MainPlayerUi.class)
2322+
player.UIs().getOpt(MainPlayerUi.class)
23232323
.ifPresent(MainPlayerUi::toggleFullscreen);
23242324
}
23252325
setOverlayLook(binding.appBarLayout, behavior, 1);
@@ -2333,7 +2333,7 @@ && isPlayerAvailable()
23332333
// Re-enable clicks
23342334
setOverlayElementsClickable(true);
23352335
if (isPlayerAvailable()) {
2336-
player.UIs().get(MainPlayerUi.class)
2336+
player.UIs().getOpt(MainPlayerUi.class)
23372337
.ifPresent(MainPlayerUi::closeItemsList);
23382338
}
23392339
setOverlayLook(binding.appBarLayout, behavior, 0);
@@ -2344,7 +2344,7 @@ && isPlayerAvailable()
23442344
showSystemUi();
23452345
}
23462346
if (isPlayerAvailable()) {
2347-
player.UIs().get(MainPlayerUi.class).ifPresent(ui -> {
2347+
player.UIs().getOpt(MainPlayerUi.class).ifPresent(ui -> {
23482348
if (ui.isControlsVisible()) {
23492349
ui.hideControls(0, 0);
23502350
}
@@ -2441,7 +2441,7 @@ boolean isPlayerAndPlayerServiceAvailable() {
24412441

24422442
public Optional<View> getRoot() {
24432443
return Optional.ofNullable(player)
2444-
.flatMap(player1 -> player1.UIs().get(VideoPlayerUi.class))
2444+
.flatMap(player1 -> player1.UIs().getOpt(VideoPlayerUi.class))
24452445
.map(playerUi -> playerUi.getBinding().getRoot());
24462446
}
24472447

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,14 +463,15 @@ public void handleIntent(@NonNull final Intent intent) {
463463
}
464464

465465
private void initUIsForCurrentPlayerType() {
466-
if ((UIs.get(MainPlayerUi.class).isPresent() && playerType == PlayerType.MAIN)
467-
|| (UIs.get(PopupPlayerUi.class).isPresent() && playerType == PlayerType.POPUP)) {
466+
if ((UIs.getOpt(MainPlayerUi.class).isPresent() && playerType == PlayerType.MAIN)
467+
|| (UIs.getOpt(PopupPlayerUi.class).isPresent()
468+
&& playerType == PlayerType.POPUP)) {
468469
// correct UI already in place
469470
return;
470471
}
471472

472473
// try to reuse binding if possible
473-
final PlayerBinding binding = UIs.get(VideoPlayerUi.class).map(VideoPlayerUi::getBinding)
474+
final PlayerBinding binding = UIs.getOpt(VideoPlayerUi.class).map(VideoPlayerUi::getBinding)
474475
.orElseGet(() -> {
475476
if (playerType == PlayerType.AUDIO) {
476477
return null;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ otherwise if nothing is played or initializing the player and its components (es
6666
loading stream metadata) takes a lot of time, the app would crash on Android 8+ as the
6767
service would never be put in the foreground while we said to the system we would do so
6868
*/
69-
player.UIs().get(NotificationPlayerUi.class)
69+
player.UIs().getOpt(NotificationPlayerUi.class)
7070
.ifPresent(NotificationPlayerUi::createNotificationAndStartForeground);
7171
}
7272

@@ -88,7 +88,7 @@ public int onStartCommand(final Intent intent, final int flags, final int startI
8888
do anything
8989
*/
9090
if (player != null) {
91-
player.UIs().get(NotificationPlayerUi.class)
91+
player.UIs().getOpt(NotificationPlayerUi.class)
9292
.ifPresent(NotificationPlayerUi::createNotificationAndStartForeground);
9393
}
9494

@@ -106,7 +106,7 @@ public int onStartCommand(final Intent intent, final int flags, final int startI
106106

107107
if (player != null) {
108108
player.handleIntent(intent);
109-
player.UIs().get(MediaSessionPlayerUi.class)
109+
player.UIs().getOpt(MediaSessionPlayerUi.class)
110110
.ifPresent(ui -> ui.handleMediaButtonIntent(intent));
111111
}
112112

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private ForwardingPlayer getForwardingPlayer() {
145145
public void play() {
146146
player.play();
147147
// hide the player controls even if the play command came from the media session
148-
player.UIs().get(VideoPlayerUi.class).ifPresent(ui -> ui.hideControls(0, 0));
148+
player.UIs().getOpt(VideoPlayerUi.class).ifPresent(ui -> ui.hideControls(0, 0));
149149
}
150150

151151
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private synchronized NotificationCompat.Builder createNotification() {
102102
mediaStyle.setShowActionsInCompactView(compactSlots);
103103
}
104104
player.UIs()
105-
.get(MediaSessionPlayerUi.class)
105+
.getOpt(MediaSessionPlayerUi.class)
106106
.flatMap(MediaSessionPlayerUi::getSessionToken)
107107
.ifPresent(mediaStyle::setMediaSession);
108108

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,32 @@ class PlayerUiList(vararg initialPlayerUis: PlayerUi) {
6565
* @param playerUiType the class of the player UI to return;
6666
* the [Class.isInstance] method will be used, so even subclasses could be returned
6767
* @param T the class type parameter
68-
* @return the first player UI of the required type found in the list, or an empty
69-
* [ ] otherwise
68+
* @return the first player UI of the required type found in the list, or null
7069
</T> */
71-
fun <T> get(playerUiType: Class<T>): Optional<T & Any> {
70+
fun <T> get(playerUiType: Class<T>): T? {
7271
for (ui in playerUis) {
7372
if (playerUiType.isInstance(ui)) {
7473
when (val r = playerUiType.cast(ui)) {
74+
// try all UIs before returning null
7575
null -> continue
76-
else -> return Optional.of(r)
76+
else -> return r
7777
}
7878
}
7979
}
80-
return Optional.empty()
80+
return null
8181
}
8282

83+
/**
84+
* @param playerUiType the class of the player UI to return;
85+
* the [Class.isInstance] method will be used, so even subclasses could be returned
86+
* @param T the class type parameter
87+
* @return the first player UI of the required type found in the list, or an empty
88+
* [ ] otherwise
89+
</T> */
90+
@Deprecated("use get", ReplaceWith("get(playerUiType)"))
91+
fun <T> getOpt(playerUiType: Class<T>): Optional<T & Any> =
92+
Optional.ofNullable(get(playerUiType))
93+
8394
/**
8495
* Calls the provided consumer on all player UIs in the list, in order of addition.
8596
* @param consumer the consumer to call with player UIs

0 commit comments

Comments
 (0)