Skip to content

Commit 3d069cd

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 eccedc0 commit 3d069cd

6 files changed

Lines changed: 39 additions & 27 deletions

File tree

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
@@ -246,7 +246,7 @@ public void onPlayerConnected(@NonNull final Player connectedPlayer,
246246
// It will do nothing if the player is not in fullscreen mode
247247
hideSystemUiIfNeeded();
248248

249-
final Optional<MainPlayerUi> playerUi = player.UIs().get(MainPlayerUi.class);
249+
final Optional<MainPlayerUi> playerUi = player.UIs().getOpt(MainPlayerUi.class);
250250
if (!player.videoPlayerSelected() && !playAfterConnect) {
251251
return;
252252
}
@@ -529,7 +529,7 @@ private void setOnClickListeners() {
529529
binding.overlayPlayPauseButton.setOnClickListener(v -> {
530530
if (playerIsNotStopped()) {
531531
player.playPause();
532-
player.UIs().get(VideoPlayerUi.class).ifPresent(ui -> ui.hideControls(0, 0));
532+
player.UIs().getOpt(VideoPlayerUi.class).ifPresent(ui -> ui.hideControls(0, 0));
533533
showSystemUi();
534534
} else {
535535
autoPlayEnabled = true; // forcefully start playing
@@ -688,7 +688,7 @@ protected void initListeners() {
688688
@Override
689689
public boolean onKeyDown(final int keyCode) {
690690
return isPlayerAvailable()
691-
&& player.UIs().get(VideoPlayerUi.class)
691+
&& player.UIs().getOpt(VideoPlayerUi.class)
692692
.map(playerUi -> playerUi.onKeyDown(keyCode)).orElse(false);
693693
}
694694

@@ -1028,7 +1028,7 @@ private void toggleFullscreenIfInFullscreenMode() {
10281028
// If a user watched video inside fullscreen mode and than chose another player
10291029
// return to non-fullscreen mode
10301030
if (isPlayerAvailable()) {
1031-
player.UIs().get(MainPlayerUi.class).ifPresent(playerUi -> {
1031+
player.UIs().getOpt(MainPlayerUi.class).ifPresent(playerUi -> {
10321032
if (playerUi.isFullscreen()) {
10331033
playerUi.toggleFullscreen();
10341034
}
@@ -1244,7 +1244,7 @@ private void tryAddVideoPlayerView() {
12441244
// setup the surface view height, so that it fits the video correctly
12451245
setHeightThumbnail();
12461246

1247-
player.UIs().get(MainPlayerUi.class).ifPresent(playerUi -> {
1247+
player.UIs().getOpt(MainPlayerUi.class).ifPresent(playerUi -> {
12481248
// sometimes binding would be null here, even though getView() != null above u.u
12491249
if (binding != null) {
12501250
// prevent from re-adding a view multiple times
@@ -1260,7 +1260,7 @@ private void removeVideoPlayerView() {
12601260
makeDefaultHeightForVideoPlaceholder();
12611261

12621262
if (player != null) {
1263-
player.UIs().get(VideoPlayerUi.class).ifPresent(VideoPlayerUi::removeViewFromParent);
1263+
player.UIs().getOpt(VideoPlayerUi.class).ifPresent(VideoPlayerUi::removeViewFromParent);
12641264
}
12651265
}
12661266

@@ -1327,7 +1327,7 @@ private void setHeightThumbnail(final int newHeight, final DisplayMetrics metric
13271327
binding.detailThumbnailImageView.setMinimumHeight(newHeight);
13281328
if (isPlayerAvailable()) {
13291329
final int maxHeight = (int) (metrics.heightPixels * MAX_PLAYER_HEIGHT);
1330-
player.UIs().get(VideoPlayerUi.class).ifPresent(ui ->
1330+
player.UIs().getOpt(VideoPlayerUi.class).ifPresent(ui ->
13311331
ui.getBinding().surfaceView.setHeights(newHeight,
13321332
ui.isFullscreen() ? newHeight : maxHeight));
13331333
}
@@ -1861,7 +1861,7 @@ public void onServiceStopped() {
18611861
public void onFullscreenStateChanged(final boolean fullscreen) {
18621862
setupBrightness();
18631863
if (!isPlayerAndPlayerServiceAvailable()
1864-
|| player.UIs().get(MainPlayerUi.class).isEmpty()
1864+
|| player.UIs().getOpt(MainPlayerUi.class).isEmpty()
18651865
|| getRoot().map(View::getParent).isEmpty()) {
18661866
return;
18671867
}
@@ -1890,7 +1890,7 @@ public void onScreenRotationButtonClicked() {
18901890
final boolean isLandscape = DeviceUtils.isLandscape(requireContext());
18911891
if (DeviceUtils.isTablet(activity)
18921892
&& (!globalScreenOrientationLocked(activity) || isLandscape)) {
1893-
player.UIs().get(MainPlayerUi.class).ifPresent(MainPlayerUi::toggleFullscreen);
1893+
player.UIs().getOpt(MainPlayerUi.class).ifPresent(MainPlayerUi::toggleFullscreen);
18941894
return;
18951895
}
18961896

@@ -1990,7 +1990,7 @@ public void hideSystemUiIfNeeded() {
19901990
}
19911991

19921992
private boolean isFullscreen() {
1993-
return isPlayerAvailable() && player.UIs().get(VideoPlayerUi.class)
1993+
return isPlayerAvailable() && player.UIs().getOpt(VideoPlayerUi.class)
19941994
.map(VideoPlayerUi::isFullscreen).orElse(false);
19951995
}
19961996

@@ -2067,7 +2067,7 @@ private void checkLandscape() {
20672067
setAutoPlay(true);
20682068
}
20692069

2070-
player.UIs().get(MainPlayerUi.class).ifPresent(MainPlayerUi::checkLandscape);
2070+
player.UIs().getOpt(MainPlayerUi.class).ifPresent(MainPlayerUi::checkLandscape);
20712071
// Let's give a user time to look at video information page if video is not playing
20722072
if (globalScreenOrientationLocked(activity) && !player.isPlaying()) {
20732073
player.play();
@@ -2332,7 +2332,7 @@ && isPlayerAvailable()
23322332
&& player.isPlaying()
23332333
&& !isFullscreen()
23342334
&& !DeviceUtils.isTablet(activity)) {
2335-
player.UIs().get(MainPlayerUi.class)
2335+
player.UIs().getOpt(MainPlayerUi.class)
23362336
.ifPresent(MainPlayerUi::toggleFullscreen);
23372337
}
23382338
setOverlayLook(binding.appBarLayout, behavior, 1);
@@ -2346,7 +2346,7 @@ && isPlayerAvailable()
23462346
// Re-enable clicks
23472347
setOverlayElementsClickable(true);
23482348
if (isPlayerAvailable()) {
2349-
player.UIs().get(MainPlayerUi.class)
2349+
player.UIs().getOpt(MainPlayerUi.class)
23502350
.ifPresent(MainPlayerUi::closeItemsList);
23512351
}
23522352
setOverlayLook(binding.appBarLayout, behavior, 0);
@@ -2357,7 +2357,7 @@ && isPlayerAvailable()
23572357
showSystemUi();
23582358
}
23592359
if (isPlayerAvailable()) {
2360-
player.UIs().get(MainPlayerUi.class).ifPresent(ui -> {
2360+
player.UIs().getOpt(MainPlayerUi.class).ifPresent(ui -> {
23612361
if (ui.isControlsVisible()) {
23622362
ui.hideControls(0, 0);
23632363
}
@@ -2454,7 +2454,7 @@ boolean isPlayerAndPlayerServiceAvailable() {
24542454

24552455
public Optional<View> getRoot() {
24562456
return Optional.ofNullable(player)
2457-
.flatMap(player1 -> player1.UIs().get(VideoPlayerUi.class))
2457+
.flatMap(player1 -> player1.UIs().getOpt(VideoPlayerUi.class))
24582458
.map(playerUi -> playerUi.getBinding().getRoot());
24592459
}
24602460

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

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

475475
private void initUIsForCurrentPlayerType() {
476-
if ((UIs.get(MainPlayerUi.class).isPresent() && playerType == PlayerType.MAIN)
477-
|| (UIs.get(PopupPlayerUi.class).isPresent() && playerType == PlayerType.POPUP)) {
476+
if ((UIs.getOpt(MainPlayerUi.class).isPresent() && playerType == PlayerType.MAIN)
477+
|| (UIs.getOpt(PopupPlayerUi.class).isPresent()
478+
&& playerType == PlayerType.POPUP)) {
478479
// correct UI already in place
479480
return;
480481
}
481482

482483
// try to reuse binding if possible
483-
final PlayerBinding binding = UIs.get(VideoPlayerUi.class).map(VideoPlayerUi::getBinding)
484+
final PlayerBinding binding = UIs.getOpt(VideoPlayerUi.class).map(VideoPlayerUi::getBinding)
484485
.orElseGet(() -> {
485486
if (playerType == PlayerType.AUDIO) {
486487
return null;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public int onStartCommand(final Intent intent, final int flags, final int startI
148148
// no one already and starting the service in foreground should not create any issues.
149149
// If the service is already started in foreground, requesting it to be started
150150
// shouldn't do anything.
151-
player.UIs().get(NotificationPlayerUi.class)
151+
player.UIs().getOpt(NotificationPlayerUi.class)
152152
.ifPresent(NotificationPlayerUi::createNotificationAndStartForeground);
153153

154154
if (playerWasNull && onPlayerStartedOrStopped != null) {
@@ -173,7 +173,7 @@ public int onStartCommand(final Intent intent, final int flags, final int startI
173173

174174
if (player != null) {
175175
player.handleIntent(intent);
176-
player.UIs().get(MediaSessionPlayerUi.class)
176+
player.UIs().getOpt(MediaSessionPlayerUi.class)
177177
.ifPresent(ui -> ui.handleMediaButtonIntent(intent));
178178
}
179179

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
@@ -138,7 +138,7 @@ private ForwardingPlayer getForwardingPlayer() {
138138
public void play() {
139139
player.play();
140140
// hide the player controls even if the play command came from the media session
141-
player.UIs().get(VideoPlayerUi.class).ifPresent(ui -> ui.hideControls(0, 0));
141+
player.UIs().getOpt(VideoPlayerUi.class).ifPresent(ui -> ui.hideControls(0, 0));
142142
}
143143

144144
@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+
* [Optional] 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)