Fix NullPointerException in queue handling

This commit is contained in:
Stypox 2020-10-18 09:50:42 +02:00
parent 6c4920949d
commit e406b6f780
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
2 changed files with 29 additions and 25 deletions

View File

@ -1728,18 +1728,27 @@ public class VideoDetailFragment
@Override @Override
public void onQueueUpdate(final PlayQueue queue) { public void onQueueUpdate(final PlayQueue queue) {
playQueue = queue; playQueue = 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. // 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 // 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 // deleted/added items inside Channel/Playlist queue and makes possible to have
// a history of played items // a history of played items
if ((stack.isEmpty() || !stack.peek().getPlayQueue().equals(queue) @Nullable final StackItem stackPeek = stack.peek();
&& queue.getItem() != null)) { if (stackPeek != null && stackPeek.getPlayQueue().equals(queue)) {
stack.push(new StackItem(queue.getItem().getServiceId(), @Nullable final PlayQueueItem playQueueItem = queue.getItem();
queue.getItem().getUrl(), if (playQueueItem != null) {
queue.getItem().getTitle(), stack.push(new StackItem(playQueueItem.getServiceId(), playQueueItem.getUrl(),
queue)); playQueueItem.getTitle(), queue));
} else { return;
final StackItem stackWithQueue = findQueueInStack(queue); } // else continue below
}
@Nullable final StackItem stackWithQueue = findQueueInStack(queue);
if (stackWithQueue != null) { if (stackWithQueue != null) {
// On every MainPlayer service's destroy() playQueue gets disposed and // On every MainPlayer service's destroy() playQueue gets disposed and
// no longer able to track progress. That's why we update our cached disposed // no longer able to track progress. That's why we update our cached disposed
@ -1749,13 +1758,6 @@ public class VideoDetailFragment
} }
} }
if (DEBUG) {
Log.d(TAG, "onQueueUpdate() called with: serviceId = ["
+ serviceId + "], videoUrl = [" + url + "], name = ["
+ name + "], playQueue = [" + playQueue + "]");
}
}
@Override @Override
public void onPlaybackUpdate(final int state, public void onPlaybackUpdate(final int state,
final int repeatMode, final int repeatMode,
@ -2055,6 +2057,7 @@ public class VideoDetailFragment
return url == null; return url == null;
} }
@Nullable
private StackItem findQueueInStack(final PlayQueue queue) { private StackItem findQueueInStack(final PlayQueue queue) {
StackItem item = null; StackItem item = null;
final Iterator<StackItem> iterator = stack.descendingIterator(); final Iterator<StackItem> iterator = stack.descendingIterator();

View File

@ -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() { public PlayQueueItem getItem() {
return getItem(getIndex()); return getItem(getIndex());
} }
/** /**
* @param index the index of the item to return * @param index the index of the item to return
* @return the item at the given index * @return the item at the given index, or null if the index is out of bounds
* @throws IndexOutOfBoundsException
*/ */
@Nullable
public PlayQueueItem getItem(final int index) { 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 null;
} }
return streams.get(index); return streams.get(index);