From e406b6f780131875d80218eb214d736af3196f87 Mon Sep 17 00:00:00 2001 From: Stypox Date: Sun, 18 Oct 2020 09:50:42 +0200 Subject: [PATCH] Fix NullPointerException in queue handling --- .../fragments/detail/VideoDetailFragment.java | 45 ++++++++++--------- .../newpipe/player/playqueue/PlayQueue.java | 9 ++-- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 536c3b35c..f41fbbb72 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -1728,32 +1728,34 @@ public class VideoDetailFragment @Override public void onQueueUpdate(final PlayQueue queue) { playQueue = queue; - // This should be the only place where we push data to stack. - // It will allow to have live instance of PlayQueue with actual information about - // deleted/added items inside Channel/Playlist queue and makes possible to have - // a history of played items - if ((stack.isEmpty() || !stack.peek().getPlayQueue().equals(queue) - && queue.getItem() != null)) { - stack.push(new StackItem(queue.getItem().getServiceId(), - queue.getItem().getUrl(), - queue.getItem().getTitle(), - queue)); - } else { - final StackItem stackWithQueue = findQueueInStack(queue); - if (stackWithQueue != null) { - // On every MainPlayer service's destroy() playQueue gets disposed and - // no longer able to track progress. That's why we update our cached disposed - // queue with the new one that is active and have the same history. - // Without that the cached playQueue will have an old recovery position - stackWithQueue.setPlayQueue(queue); - } - } - if (DEBUG) { Log.d(TAG, "onQueueUpdate() called with: serviceId = [" + serviceId + "], videoUrl = [" + url + "], name = [" + name + "], playQueue = [" + playQueue + "]"); } + + // This should be the only place where we push data to stack. + // It will allow to have live instance of PlayQueue with actual information about + // deleted/added items inside Channel/Playlist queue and makes possible to have + // a history of played items + @Nullable final StackItem stackPeek = stack.peek(); + if (stackPeek != null && stackPeek.getPlayQueue().equals(queue)) { + @Nullable final PlayQueueItem playQueueItem = queue.getItem(); + if (playQueueItem != null) { + stack.push(new StackItem(playQueueItem.getServiceId(), playQueueItem.getUrl(), + playQueueItem.getTitle(), queue)); + return; + } // else continue below + } + + @Nullable final StackItem stackWithQueue = findQueueInStack(queue); + if (stackWithQueue != null) { + // On every MainPlayer service's destroy() playQueue gets disposed and + // no longer able to track progress. That's why we update our cached disposed + // queue with the new one that is active and have the same history. + // Without that the cached playQueue will have an old recovery position + stackWithQueue.setPlayQueue(queue); + } } @Override @@ -2055,6 +2057,7 @@ public class VideoDetailFragment return url == null; } + @Nullable private StackItem findQueueInStack(final PlayQueue queue) { StackItem item = null; final Iterator iterator = stack.descendingIterator(); diff --git a/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueue.java b/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueue.java index 8bef0b2e0..4f35f98f5 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueue.java +++ b/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueue.java @@ -167,19 +167,20 @@ public abstract class PlayQueue implements Serializable { } /** - * @return the current item that should be played + * @return the current item that should be played, or null if the queue is empty */ + @Nullable public PlayQueueItem getItem() { return getItem(getIndex()); } /** * @param index the index of the item to return - * @return the item at the given index - * @throws IndexOutOfBoundsException + * @return the item at the given index, or null if the index is out of bounds */ + @Nullable public PlayQueueItem getItem(final int index) { - if (index < 0 || index >= streams.size() || streams.get(index) == null) { + if (index < 0 || index >= streams.size()) { return null; } return streams.get(index);