-Fixed TransactionTooLarge due to notification being shown for too long.

-Fixed Play Queue rewinding to last played video upon removing the currently playing.
This commit is contained in:
John Zhen M 2017-09-29 20:38:26 -07:00 committed by John Zhen Mo
parent c75c2d0f21
commit bd9ee18e56
2 changed files with 28 additions and 7 deletions

View File

@ -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());

View File

@ -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);