Skip to content

Commit 23aa1f8

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 749c710 commit 23aa1f8

6 files changed

Lines changed: 40 additions & 28 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
@@ -244,7 +244,7 @@ public void onServiceConnected(final PlayerService connectedPlayerService,
244244
// It will do nothing if the player is not in fullscreen mode
245245
hideSystemUiIfNeeded();
246246

247-
final Optional<MainPlayerUi> playerUi = player.UIs().get(MainPlayerUi.class);
247+
final Optional<MainPlayerUi> playerUi = player.UIs().getOpt(MainPlayerUi.class);
248248
if (!player.videoPlayerSelected() && !playAfterConnect) {
249249
return;
250250
}
@@ -520,7 +520,7 @@ private void setOnClickListeners() {
520520
binding.overlayPlayPauseButton.setOnClickListener(v -> {
521521
if (playerIsNotStopped()) {
522522
player.playPause();
523-
player.UIs().get(VideoPlayerUi.class).ifPresent(ui -> ui.hideControls(0, 0));
523+
player.UIs().getOpt(VideoPlayerUi.class).ifPresent(ui -> ui.hideControls(0, 0));
524524
showSystemUi();
525525
} else {
526526
autoPlayEnabled = true; // forcefully start playing
@@ -679,7 +679,7 @@ protected void initListeners() {
679679
@Override
680680
public boolean onKeyDown(final int keyCode) {
681681
return isPlayerAvailable()
682-
&& player.UIs().get(VideoPlayerUi.class)
682+
&& player.UIs().getOpt(VideoPlayerUi.class)
683683
.map(playerUi -> playerUi.onKeyDown(keyCode)).orElse(false);
684684
}
685685

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

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

12531253
if (player != null) {
1254-
player.UIs().get(VideoPlayerUi.class).ifPresent(VideoPlayerUi::removeViewFromParent);
1254+
player.UIs().getOpt(VideoPlayerUi.class).ifPresent(VideoPlayerUi::removeViewFromParent);
12551255
}
12561256
}
12571257

@@ -1318,7 +1318,7 @@ private void setHeightThumbnail(final int newHeight, final DisplayMetrics metric
13181318
binding.detailThumbnailImageView.setMinimumHeight(newHeight);
13191319
if (isPlayerAvailable()) {
13201320
final int maxHeight = (int) (metrics.heightPixels * MAX_PLAYER_HEIGHT);
1321-
player.UIs().get(VideoPlayerUi.class).ifPresent(ui ->
1321+
player.UIs().getOpt(VideoPlayerUi.class).ifPresent(ui ->
13221322
ui.getBinding().surfaceView.setHeights(newHeight,
13231323
ui.isFullscreen() ? newHeight : maxHeight));
13241324
}
@@ -1851,7 +1851,7 @@ public void onServiceStopped() {
18511851
public void onFullscreenStateChanged(final boolean fullscreen) {
18521852
setupBrightness();
18531853
if (!isPlayerAndPlayerServiceAvailable()
1854-
|| player.UIs().get(MainPlayerUi.class).isEmpty()
1854+
|| player.UIs().getOpt(MainPlayerUi.class).isEmpty()
18551855
|| getRoot().map(View::getParent).isEmpty()) {
18561856
return;
18571857
}
@@ -1880,7 +1880,7 @@ public void onScreenRotationButtonClicked() {
18801880
final boolean isLandscape = DeviceUtils.isLandscape(requireContext());
18811881
if (DeviceUtils.isTablet(activity)
18821882
&& (!globalScreenOrientationLocked(activity) || isLandscape)) {
1883-
player.UIs().get(MainPlayerUi.class).ifPresent(MainPlayerUi::toggleFullscreen);
1883+
player.UIs().getOpt(MainPlayerUi.class).ifPresent(MainPlayerUi::toggleFullscreen);
18841884
return;
18851885
}
18861886

@@ -1980,7 +1980,7 @@ public void hideSystemUiIfNeeded() {
19801980
}
19811981

19821982
private boolean isFullscreen() {
1983-
return isPlayerAvailable() && player.UIs().get(VideoPlayerUi.class)
1983+
return isPlayerAvailable() && player.UIs().getOpt(VideoPlayerUi.class)
19841984
.map(VideoPlayerUi::isFullscreen).orElse(false);
19851985
}
19861986

@@ -2057,7 +2057,7 @@ private void checkLandscape() {
20572057
setAutoPlay(true);
20582058
}
20592059

2060-
player.UIs().get(MainPlayerUi.class).ifPresent(MainPlayerUi::checkLandscape);
2060+
player.UIs().getOpt(MainPlayerUi.class).ifPresent(MainPlayerUi::checkLandscape);
20612061
// Let's give a user time to look at video information page if video is not playing
20622062
if (globalScreenOrientationLocked(activity) && !player.isPlaying()) {
20632063
player.play();
@@ -2322,7 +2322,7 @@ && isPlayerAvailable()
23222322
&& player.isPlaying()
23232323
&& !isFullscreen()
23242324
&& !DeviceUtils.isTablet(activity)) {
2325-
player.UIs().get(MainPlayerUi.class)
2325+
player.UIs().getOpt(MainPlayerUi.class)
23262326
.ifPresent(MainPlayerUi::toggleFullscreen);
23272327
}
23282328
setOverlayLook(binding.appBarLayout, behavior, 1);
@@ -2336,7 +2336,7 @@ && isPlayerAvailable()
23362336
// Re-enable clicks
23372337
setOverlayElementsClickable(true);
23382338
if (isPlayerAvailable()) {
2339-
player.UIs().get(MainPlayerUi.class)
2339+
player.UIs().getOpt(MainPlayerUi.class)
23402340
.ifPresent(MainPlayerUi::closeItemsList);
23412341
}
23422342
setOverlayLook(binding.appBarLayout, behavior, 0);
@@ -2347,7 +2347,7 @@ && isPlayerAvailable()
23472347
showSystemUi();
23482348
}
23492349
if (isPlayerAvailable()) {
2350-
player.UIs().get(MainPlayerUi.class).ifPresent(ui -> {
2350+
player.UIs().getOpt(MainPlayerUi.class).ifPresent(ui -> {
23512351
if (ui.isControlsVisible()) {
23522352
ui.hideControls(0, 0);
23532353
}
@@ -2444,7 +2444,7 @@ boolean isPlayerAndPlayerServiceAvailable() {
24442444

24452445
public Optional<View> getRoot() {
24462446
return Optional.ofNullable(player)
2447-
.flatMap(player1 -> player1.UIs().get(VideoPlayerUi.class))
2447+
.flatMap(player1 -> player1.UIs().getOpt(VideoPlayerUi.class))
24482448
.map(playerUi -> playerUi.getBinding().getRoot());
24492449
}
24502450

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

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

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

471472
// try to reuse binding if possible
472-
final PlayerBinding binding = UIs.get(VideoPlayerUi.class).map(VideoPlayerUi::getBinding)
473+
final PlayerBinding binding = UIs.getOpt(VideoPlayerUi.class).map(VideoPlayerUi::getBinding)
473474
.orElseGet(() -> {
474475
if (playerType == PlayerType.AUDIO) {
475476
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
@@ -73,7 +73,7 @@ otherwise if nothing is played or initializing the player and its components (es
7373
loading stream metadata) takes a lot of time, the app would crash on Android 8+ as the
7474
service would never be put in the foreground while we said to the system we would do so
7575
*/
76-
player.UIs().get(NotificationPlayerUi.class)
76+
player.UIs().getOpt(NotificationPlayerUi.class)
7777
.ifPresent(NotificationPlayerUi::createNotificationAndStartForeground);
7878
}
7979

@@ -95,7 +95,7 @@ public int onStartCommand(final Intent intent, final int flags, final int startI
9595
do anything
9696
*/
9797
if (player != null) {
98-
player.UIs().get(NotificationPlayerUi.class)
98+
player.UIs().getOpt(NotificationPlayerUi.class)
9999
.ifPresent(NotificationPlayerUi::createNotificationAndStartForeground);
100100
}
101101

@@ -113,7 +113,7 @@ public int onStartCommand(final Intent intent, final int flags, final int startI
113113

114114
if (player != null) {
115115
player.handleIntent(intent);
116-
player.UIs().get(MediaSessionPlayerUi.class)
116+
player.UIs().getOpt(MediaSessionPlayerUi.class)
117117
.ifPresent(ui -> ui.handleMediaButtonIntent(intent));
118118
}
119119

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)