From e37d3d149d2aaa33523809e29c1e29491dce2f5e Mon Sep 17 00:00:00 2001 From: Christopher Eby Date: Sat, 6 Mar 2010 14:45:33 -0600 Subject: [PATCH] Ensure the notification is canceled when persistence is disabled startForeground will keep a hold on the notification passed to it. It seems that stopForeground is the only way to cancel the notification then --- src/org/kreed/vanilla/PlaybackService.java | 23 +++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/org/kreed/vanilla/PlaybackService.java b/src/org/kreed/vanilla/PlaybackService.java index 23a331bb..1482b518 100644 --- a/src/org/kreed/vanilla/PlaybackService.java +++ b/src/org/kreed/vanilla/PlaybackService.java @@ -207,13 +207,13 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On } } - public void stopForegroundCompat(boolean cancelNotification) + public void stopForegroundCompat(Boolean cancelNotification) { if (mStopForeground == null) { setForeground(false); } else { try { - mStopForeground.invoke(this, Boolean.FALSE); + mStopForeground.invoke(this, cancelNotification); } catch (InvocationTargetException e) { Log.w("VanillaMusic", e); } catch (IllegalAccessException e) { @@ -454,6 +454,8 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On } else if ("notify_while_paused".equals(key)){ mNotifyWhilePaused = mSettings.getBoolean(key, true); updateNotification(); + if (!mNotifyWhilePaused && mState != STATE_PLAYING) + stopForegroundCompat(true); } else if ("scrobble".equals(key)) { mScrobble = mSettings.getBoolean("scrobble", false); } @@ -479,9 +481,16 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On Song song = getSong(0); broadcastSongChange(song); - updateNotification(); + boolean cancelNotification = updateNotification(); updateWidgets(); + if (mState != oldState) { + if (mState == STATE_PLAYING) + startForegroundCompat(NOTIFICATION_ID, mNotification); + else + stopForegroundCompat(cancelNotification); + } + if (mScrobble) { Intent intent = new Intent("net.jjc1138.android.scrobbler.action.MUSIC_STATUS"); intent.putExtra("playing", mState == STATE_PLAYING); @@ -499,13 +508,13 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On updateState(STATE_NORMAL); } - private void updateNotification() + private boolean updateNotification() { Song song = getSong(0); if (song == null || !mNotifyWhilePaused && mState == STATE_NORMAL) { mNotificationManager.cancel(NOTIFICATION_ID); - return; + return true; } String title = song.title; @@ -530,6 +539,8 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On mNotification = notification; mNotificationManager.notify(NOTIFICATION_ID, mNotification); + + return false; } private boolean isSpeakerOn() @@ -562,14 +573,12 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On mMediaPlayer.start(); updateState(STATE_PLAYING); - startForegroundCompat(NOTIFICATION_ID, mNotification); } private void pause() { mMediaPlayer.pause(); updateState(STATE_NORMAL); - stopForegroundCompat(false); } private void setPlaying(boolean play)