diff --git a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java index 2f3da3b25..3483f5eb0 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java @@ -83,6 +83,8 @@ public final class BackgroundPlayer extends Service { //////////////////////////////////////////////////////////////////////////*/ private static final int NOTIFICATION_ID = 123789; + private boolean shouldUpdateNotification; + private NotificationManager notificationManager; private NotificationCompat.Builder notBuilder; private RemoteViews notRemoteView; @@ -150,16 +152,27 @@ public final class BackgroundPlayer extends Service { private void onScreenOnOff(boolean on) { if (DEBUG) Log.d(TAG, "onScreenOnOff() called with: on = [" + on + "]"); - if (on) { - if (basePlayerImpl.isPlaying() && !basePlayerImpl.isProgressLoopRunning()) basePlayerImpl.startProgressLoop(); - } else basePlayerImpl.stopProgressLoop(); + shouldUpdateNotification = on; + if (on) { + if (basePlayerImpl.isPlaying() && !basePlayerImpl.isProgressLoopRunning()) { + basePlayerImpl.startProgressLoop(); + } + } else { + basePlayerImpl.stopProgressLoop(); + } } /*////////////////////////////////////////////////////////////////////////// // Notification //////////////////////////////////////////////////////////////////////////*/ + private void resetNotification() { + if (shouldUpdateNotification) { + notBuilder = createNotification(); + } + } + private NotificationCompat.Builder createNotification() { notRemoteView = new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.player_notification); bigNotRemoteView = new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.player_notification_expanded); @@ -214,7 +227,7 @@ public final class BackgroundPlayer extends Service { */ private synchronized void updateNotification(int drawableId) { //if (DEBUG) Log.d(TAG, "updateNotification() called with: drawableId = [" + drawableId + "]"); - if (notBuilder == null) return; + if (notBuilder == null || !shouldUpdateNotification) return; if (drawableId != -1) { if (notRemoteView != null) notRemoteView.setImageViewResource(R.id.notificationPlayPause, drawableId); if (bigNotRemoteView != null) bigNotRemoteView.setImageViewResource(R.id.notificationPlayPause, drawableId); @@ -267,6 +280,7 @@ public final class BackgroundPlayer extends Service { public void handleIntent(Intent intent) { super.handleIntent(intent); + shouldUpdateNotification = true; notBuilder = createNotification(); startForeground(NOTIFICATION_ID, notBuilder.build()); @@ -276,6 +290,7 @@ public final class BackgroundPlayer extends Service { @Override public void initThumbnail(final String url) { + resetNotification(); if (notRemoteView != null) notRemoteView.setImageViewResource(R.id.notificationCover, R.drawable.dummy_thumbnail); if (bigNotRemoteView != null) bigNotRemoteView.setImageViewResource(R.id.notificationCover, R.drawable.dummy_thumbnail); updateNotification(-1); @@ -288,7 +303,7 @@ public final class BackgroundPlayer extends Service { if (thumbnail != null) { // rebuild notification here since remote view does not release bitmaps, causing memory leaks - notBuilder = createNotification(); + resetNotification(); if (notRemoteView != null) notRemoteView.setImageViewBitmap(R.id.notificationCover, thumbnail); if (bigNotRemoteView != null) bigNotRemoteView.setImageViewBitmap(R.id.notificationCover, thumbnail); @@ -335,6 +350,7 @@ public final class BackgroundPlayer extends Service { @Override public void onUpdateProgress(int currentProgress, int duration, int bufferPercent) { + resetNotification(); if (bigNotRemoteView != null) { if (currentInfo != null) { bigNotRemoteView.setTextViewText(R.id.notificationSongName, getVideoTitle()); @@ -400,6 +416,7 @@ public final class BackgroundPlayer extends Service { public void sync(@Nullable final StreamInfo info) { super.sync(info); + resetNotification(); notRemoteView.setTextViewText(R.id.notificationSongName, getVideoTitle()); notRemoteView.setTextViewText(R.id.notificationArtist, getUploaderName()); bigNotRemoteView.setTextViewText(R.id.notificationSongName, getVideoTitle()); diff --git a/app/src/main/java/org/schabi/newpipe/playlist/PlayQueue.java b/app/src/main/java/org/schabi/newpipe/playlist/PlayQueue.java index 5b32ddfb2..b159a354d 100644 --- a/app/src/main/java/org/schabi/newpipe/playlist/PlayQueue.java +++ b/app/src/main/java/org/schabi/newpipe/playlist/PlayQueue.java @@ -231,7 +231,8 @@ public abstract class PlayQueue implements Serializable { /** * Removes the item at the given index from the play queue. * - * The current playing index will decrement if greater than or equal to the index being removed. + * The current playing index will decrement if it is greater than the index being removed. + * On cases where the current playing index exceeds the playlist range, it is set to 0. * * Will emit a {@link RemoveEvent} if the index is within the play queue index range. * @@ -239,10 +240,13 @@ public abstract class PlayQueue implements Serializable { public synchronized void remove(final int index) { if (index >= streams.size() || index < 0) return; + final int currentIndex = queueIndex.get(); final boolean isCurrent = index == getIndex(); - if (queueIndex.get() >= index) { + if (currentIndex > index) { queueIndex.decrementAndGet(); + } else if (currentIndex >= size()) { + queueIndex.set(0); } streams.remove(index);