From 73305414994f32e39dd20088f677a71cf61b9560 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Tue, 13 May 2025 19:08:23 +0200 Subject: [PATCH] PlayerUIList: remove remaining uses of getOpt mediaSession is now `@NonNull`, so the getter is as well. --- .../org/schabi/newpipe/player/Player.java | 21 ++++++++++--------- .../mediasession/MediaSessionPlayerUi.java | 11 +++++++--- .../player/notification/NotificationUtil.java | 8 +++---- .../schabi/newpipe/player/ui/PlayerUiList.kt | 12 ----------- 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/Player.java b/app/src/main/java/org/schabi/newpipe/player/Player.java index 49f02efeb..57cdd081e 100644 --- a/app/src/main/java/org/schabi/newpipe/player/Player.java +++ b/app/src/main/java/org/schabi/newpipe/player/Player.java @@ -477,22 +477,23 @@ public final class Player implements PlaybackListener, Listener { } private void initUIsForCurrentPlayerType() { - if ((UIs.getOpt(MainPlayerUi.class).isPresent() && playerType == PlayerType.MAIN) - || (UIs.getOpt(PopupPlayerUi.class).isPresent() + if ((UIs.get(MainPlayerUi.class) != null && playerType == PlayerType.MAIN) + || (UIs.get(PopupPlayerUi.class) != null && playerType == PlayerType.POPUP)) { // correct UI already in place return; } // try to reuse binding if possible - final PlayerBinding binding = UIs.getOpt(VideoPlayerUi.class).map(VideoPlayerUi::getBinding) - .orElseGet(() -> { - if (playerType == PlayerType.AUDIO) { - return null; - } else { - return PlayerBinding.inflate(LayoutInflater.from(context)); - } - }); + @Nullable final VideoPlayerUi ui = UIs.get(VideoPlayerUi.class); + final PlayerBinding binding; + if (ui != null) { + binding = ui.getBinding(); + } else if (playerType == PlayerType.AUDIO) { + binding = null; + } else { + binding = PlayerBinding.inflate(LayoutInflater.from(context)); + } switch (playerType) { case MAIN: diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java b/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java index 085da5eb7..850dd02e3 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java @@ -124,8 +124,10 @@ public class MediaSessionPlayerUi extends PlayerUi MediaButtonReceiver.handleIntent(mediaSession, intent); } - public Optional getSessionToken() { - return Optional.ofNullable(mediaSession).map(MediaSessionCompat::getSessionToken); + + @NonNull + public MediaSessionCompat.Token getSessionToken() { + return mediaSession.getSessionToken(); } @@ -138,7 +140,10 @@ public class MediaSessionPlayerUi extends PlayerUi public void play() { player.play(); // hide the player controls even if the play command came from the media session - player.UIs().getOpt(VideoPlayerUi.class).ifPresent(ui -> ui.hideControls(0, 0)); + final VideoPlayerUi ui = player.UIs().get(VideoPlayerUi.class); + if (ui != null) { + ui.hideControls(0, 0); + } } @Override diff --git a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java index 5658693f2..cc3889973 100644 --- a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java +++ b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java @@ -101,10 +101,10 @@ public final class NotificationUtil { final int[] compactSlots = initializeNotificationSlots(); mediaStyle.setShowActionsInCompactView(compactSlots); } - player.UIs() - .getOpt(MediaSessionPlayerUi.class) - .flatMap(MediaSessionPlayerUi::getSessionToken) - .ifPresent(mediaStyle::setMediaSession); + @Nullable final MediaSessionPlayerUi ui = player.UIs().get(MediaSessionPlayerUi.class); + if (ui != null) { + mediaStyle.setMediaSession(ui.getSessionToken()); + } // setup notification builder builder.setStyle(mediaStyle) diff --git a/app/src/main/java/org/schabi/newpipe/player/ui/PlayerUiList.kt b/app/src/main/java/org/schabi/newpipe/player/ui/PlayerUiList.kt index ec0c85c93..ef9c6f3c2 100644 --- a/app/src/main/java/org/schabi/newpipe/player/ui/PlayerUiList.kt +++ b/app/src/main/java/org/schabi/newpipe/player/ui/PlayerUiList.kt @@ -1,7 +1,6 @@ package org.schabi.newpipe.player.ui import org.schabi.newpipe.util.GuardedByMutex -import java.util.Optional /** * Creates a [PlayerUiList] starting with the provided player uis. The provided player uis @@ -99,17 +98,6 @@ class PlayerUiList(vararg initialPlayerUis: PlayerUi) { return@runWithLockSync null } - /** - * @param playerUiType the class of the player UI to return; - * the [Class.isInstance] method will be used, so even subclasses could be returned - * @param T the class type parameter - * @return the first player UI of the required type found in the list, or an empty - * [Optional] otherwise - */ - @Deprecated("use get", ReplaceWith("get(playerUiType)")) - fun getOpt(playerUiType: Class): Optional = - Optional.ofNullable(get(playerUiType)) - /** * Calls the provided consumer on all player UIs in the list, in order of addition. * @param consumer the consumer to call with player UIs