From 5108d75682bf5cbb142ca4be8796daab02b9fb3f Mon Sep 17 00:00:00 2001 From: Stypox Date: Tue, 25 Jan 2022 17:37:15 +0100 Subject: [PATCH 1/2] Fix NPE and add some `@Nullable`s Fix NullPointerException in PlayerHolder.getQueueSize() and add `@Nullable` here and there so that the linter reports risks of NPEs --- .../main/java/org/schabi/newpipe/player/Player.java | 4 +++- .../schabi/newpipe/player/helper/PlayerHolder.java | 12 ++++++++---- 2 files changed, 11 insertions(+), 5 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 81ef25db1..c002ba16d 100644 --- a/app/src/main/java/org/schabi/newpipe/player/Player.java +++ b/app/src/main/java/org/schabi/newpipe/player/Player.java @@ -260,7 +260,8 @@ public final class Player implements // Playback //////////////////////////////////////////////////////////////////////////*/ - private PlayQueue playQueue; + // play queue might be null e.g. while player is starting + @Nullable private PlayQueue playQueue; private PlayQueueAdapter playQueueAdapter; private StreamSegmentAdapter segmentAdapter; @@ -4202,6 +4203,7 @@ public final class Player implements } + @Nullable public PlayQueue getPlayQueue() { return playQueue; } diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/PlayerHolder.java b/app/src/main/java/org/schabi/newpipe/player/helper/PlayerHolder.java index 10e315667..42865564a 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/PlayerHolder.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/PlayerHolder.java @@ -38,12 +38,12 @@ public final class PlayerHolder { private static final boolean DEBUG = MainActivity.DEBUG; private static final String TAG = PlayerHolder.class.getSimpleName(); - private PlayerServiceExtendedEventListener listener; + @Nullable private PlayerServiceExtendedEventListener listener; private final PlayerServiceConnection serviceConnection = new PlayerServiceConnection(); private boolean bound; - private MainPlayer playerService; - private Player player; + @Nullable private MainPlayer playerService; + @Nullable private Player player; /** * Returns the current {@link MainPlayer.PlayerType} of the {@link MainPlayer} service, @@ -75,7 +75,11 @@ public final class PlayerHolder { } public int getQueueSize() { - return isPlayerOpen() ? player.getPlayQueue().size() : 0; + if (player == null || player.getPlayQueue() == null) { + // player play queue might be null e.g. while player is starting + return 0; + } + return player.getPlayQueue().size(); } public void setListener(@Nullable final PlayerServiceExtendedEventListener newListener) { From 71c5aaa11ec020fa9b03a619758943ce01424683 Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 27 Jan 2022 17:11:16 +0100 Subject: [PATCH 2/2] Do not show enqueue button if play queue not ready --- .../schabi/newpipe/fragments/list/BaseListFragment.java | 2 +- .../fragments/list/playlist/PlaylistFragment.java | 2 +- .../java/org/schabi/newpipe/local/feed/FeedFragment.kt | 2 +- .../local/history/StatisticsPlaylistFragment.java | 2 +- .../newpipe/local/playlist/LocalPlaylistFragment.java | 2 +- .../org/schabi/newpipe/player/helper/PlayerHolder.java | 9 +++++++++ 6 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java index 4319d42ee..3c2e65bb7 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java @@ -352,7 +352,7 @@ public abstract class BaseListFragment extends BaseStateFragment } final List entries = new ArrayList<>(); - if (PlayerHolder.getInstance().isPlayerOpen()) { + if (PlayerHolder.getInstance().isPlayQueueReady()) { entries.add(StreamDialogEntry.enqueue); if (PlayerHolder.getInstance().getQueueSize() > 1) { diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/playlist/PlaylistFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/playlist/PlaylistFragment.java index 85c47ec74..640d08064 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/playlist/PlaylistFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/playlist/PlaylistFragment.java @@ -149,7 +149,7 @@ public class PlaylistFragment extends BaseListInfoFragment { final ArrayList entries = new ArrayList<>(); - if (PlayerHolder.getInstance().isPlayerOpen()) { + if (PlayerHolder.getInstance().isPlayQueueReady()) { entries.add(StreamDialogEntry.enqueue); if (PlayerHolder.getInstance().getQueueSize() > 1) { diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt b/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt index 905290b48..e6da0d545 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt @@ -362,7 +362,7 @@ class FeedFragment : BaseStateFragment() { if (context == null || context.resources == null || activity == null) return val entries = ArrayList() - if (PlayerHolder.getInstance().isPlayerOpen) { + if (PlayerHolder.getInstance().isPlayQueueReady) { entries.add(StreamDialogEntry.enqueue) if (PlayerHolder.getInstance().queueSize > 1) { diff --git a/app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java b/app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java index 43a5fcf3c..73682d5d5 100644 --- a/app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java @@ -338,7 +338,7 @@ public class StatisticsPlaylistFragment final ArrayList entries = new ArrayList<>(); - if (PlayerHolder.getInstance().isPlayerOpen()) { + if (PlayerHolder.getInstance().isPlayQueueReady()) { entries.add(StreamDialogEntry.enqueue); if (PlayerHolder.getInstance().getQueueSize() > 1) { diff --git a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java index 87d913b3b..feb5b2f96 100644 --- a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java @@ -753,7 +753,7 @@ public class LocalPlaylistFragment extends BaseLocalListFragment entries = new ArrayList<>(); - if (PlayerHolder.getInstance().isPlayerOpen()) { + if (PlayerHolder.getInstance().isPlayQueueReady()) { entries.add(StreamDialogEntry.enqueue); if (PlayerHolder.getInstance().getQueueSize() > 1) { diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/PlayerHolder.java b/app/src/main/java/org/schabi/newpipe/player/helper/PlayerHolder.java index 42865564a..06a2e52ab 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/PlayerHolder.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/PlayerHolder.java @@ -70,6 +70,15 @@ public final class PlayerHolder { return player != null; } + /** + * Use this method to only allow the user to manipulate the play queue (e.g. by enqueueing via + * the stream long press menu) when there actually is a play queue to manipulate. + * @return true only if the player is open and its play queue is ready (i.e. it is not null) + */ + public boolean isPlayQueueReady() { + return player != null && player.getPlayQueue() != null; + } + public boolean isBound() { return bound; }