Skip to content

Commit 9a55848

Browse files
author
John Zhen M
committed
-Fixed TransactionTooLarge due to notification being shown for too long.
-Fixed Play Queue rewinding to last played video upon removing the currently playing.
1 parent 13f1e8e commit 9a55848

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public final class BackgroundPlayer extends Service {
8383
//////////////////////////////////////////////////////////////////////////*/
8484
private static final int NOTIFICATION_ID = 123789;
8585

86+
private boolean shouldUpdateNotification;
87+
8688
private NotificationManager notificationManager;
8789
private NotificationCompat.Builder notBuilder;
8890
private RemoteViews notRemoteView;
@@ -150,16 +152,27 @@ private void onClose() {
150152

151153
private void onScreenOnOff(boolean on) {
152154
if (DEBUG) Log.d(TAG, "onScreenOnOff() called with: on = [" + on + "]");
153-
if (on) {
154-
if (basePlayerImpl.isPlaying() && !basePlayerImpl.isProgressLoopRunning()) basePlayerImpl.startProgressLoop();
155-
} else basePlayerImpl.stopProgressLoop();
155+
shouldUpdateNotification = on;
156156

157+
if (on) {
158+
if (basePlayerImpl.isPlaying() && !basePlayerImpl.isProgressLoopRunning()) {
159+
basePlayerImpl.startProgressLoop();
160+
}
161+
} else {
162+
basePlayerImpl.stopProgressLoop();
163+
}
157164
}
158165

159166
/*//////////////////////////////////////////////////////////////////////////
160167
// Notification
161168
//////////////////////////////////////////////////////////////////////////*/
162169

170+
private void resetNotification() {
171+
if (shouldUpdateNotification) {
172+
notBuilder = createNotification();
173+
}
174+
}
175+
163176
private NotificationCompat.Builder createNotification() {
164177
notRemoteView = new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.player_notification);
165178
bigNotRemoteView = new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.player_notification_expanded);
@@ -214,7 +227,7 @@ private void setupNotification(RemoteViews remoteViews) {
214227
*/
215228
private synchronized void updateNotification(int drawableId) {
216229
//if (DEBUG) Log.d(TAG, "updateNotification() called with: drawableId = [" + drawableId + "]");
217-
if (notBuilder == null) return;
230+
if (notBuilder == null || !shouldUpdateNotification) return;
218231
if (drawableId != -1) {
219232
if (notRemoteView != null) notRemoteView.setImageViewResource(R.id.notificationPlayPause, drawableId);
220233
if (bigNotRemoteView != null) bigNotRemoteView.setImageViewResource(R.id.notificationPlayPause, drawableId);
@@ -267,6 +280,7 @@ private class BasePlayerImpl extends BasePlayer {
267280
public void handleIntent(Intent intent) {
268281
super.handleIntent(intent);
269282

283+
shouldUpdateNotification = true;
270284
notBuilder = createNotification();
271285
startForeground(NOTIFICATION_ID, notBuilder.build());
272286

@@ -276,6 +290,7 @@ public void handleIntent(Intent intent) {
276290

277291
@Override
278292
public void initThumbnail(final String url) {
293+
resetNotification();
279294
if (notRemoteView != null) notRemoteView.setImageViewResource(R.id.notificationCover, R.drawable.dummy_thumbnail);
280295
if (bigNotRemoteView != null) bigNotRemoteView.setImageViewResource(R.id.notificationCover, R.drawable.dummy_thumbnail);
281296
updateNotification(-1);
@@ -288,7 +303,7 @@ public void onThumbnailReceived(Bitmap thumbnail) {
288303

289304
if (thumbnail != null) {
290305
// rebuild notification here since remote view does not release bitmaps, causing memory leaks
291-
notBuilder = createNotification();
306+
resetNotification();
292307

293308
if (notRemoteView != null) notRemoteView.setImageViewBitmap(R.id.notificationCover, thumbnail);
294309
if (bigNotRemoteView != null) bigNotRemoteView.setImageViewBitmap(R.id.notificationCover, thumbnail);
@@ -335,6 +350,7 @@ public void onRepeatClicked() {
335350

336351
@Override
337352
public void onUpdateProgress(int currentProgress, int duration, int bufferPercent) {
353+
resetNotification();
338354
if (bigNotRemoteView != null) {
339355
if (currentInfo != null) {
340356
bigNotRemoteView.setTextViewText(R.id.notificationSongName, getVideoTitle());
@@ -400,6 +416,7 @@ public void onError(Exception exception) {
400416
public void sync(@Nullable final StreamInfo info) {
401417
super.sync(info);
402418

419+
resetNotification();
403420
notRemoteView.setTextViewText(R.id.notificationSongName, getVideoTitle());
404421
notRemoteView.setTextViewText(R.id.notificationArtist, getUploaderName());
405422
bigNotRemoteView.setTextViewText(R.id.notificationSongName, getVideoTitle());

app/src/main/java/org/schabi/newpipe/playlist/PlayQueue.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,22 @@ protected synchronized void append(final Collection<PlayQueueItem> items) {
231231
/**
232232
* Removes the item at the given index from the play queue.
233233
*
234-
* The current playing index will decrement if greater than or equal to the index being removed.
234+
* The current playing index will decrement if it is greater than the index being removed.
235+
* On cases where the current playing index exceeds the playlist range, it is set to 0.
235236
*
236237
* Will emit a {@link RemoveEvent} if the index is within the play queue index range.
237238
*
238239
* */
239240
public synchronized void remove(final int index) {
240241
if (index >= streams.size() || index < 0) return;
241242

243+
final int currentIndex = queueIndex.get();
242244
final boolean isCurrent = index == getIndex();
243245

244-
if (queueIndex.get() >= index) {
246+
if (currentIndex > index) {
245247
queueIndex.decrementAndGet();
248+
} else if (currentIndex >= size()) {
249+
queueIndex.set(0);
246250
}
247251
streams.remove(index);
248252

0 commit comments

Comments
 (0)