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
This commit is contained in:
Christopher Eby 2010-03-06 14:45:33 -06:00
parent 00aedee9ad
commit e37d3d149d

View File

@ -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) { if (mStopForeground == null) {
setForeground(false); setForeground(false);
} else { } else {
try { try {
mStopForeground.invoke(this, Boolean.FALSE); mStopForeground.invoke(this, cancelNotification);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
Log.w("VanillaMusic", e); Log.w("VanillaMusic", e);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
@ -454,6 +454,8 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
} else if ("notify_while_paused".equals(key)){ } else if ("notify_while_paused".equals(key)){
mNotifyWhilePaused = mSettings.getBoolean(key, true); mNotifyWhilePaused = mSettings.getBoolean(key, true);
updateNotification(); updateNotification();
if (!mNotifyWhilePaused && mState != STATE_PLAYING)
stopForegroundCompat(true);
} else if ("scrobble".equals(key)) { } else if ("scrobble".equals(key)) {
mScrobble = mSettings.getBoolean("scrobble", false); mScrobble = mSettings.getBoolean("scrobble", false);
} }
@ -479,9 +481,16 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
Song song = getSong(0); Song song = getSong(0);
broadcastSongChange(song); broadcastSongChange(song);
updateNotification(); boolean cancelNotification = updateNotification();
updateWidgets(); updateWidgets();
if (mState != oldState) {
if (mState == STATE_PLAYING)
startForegroundCompat(NOTIFICATION_ID, mNotification);
else
stopForegroundCompat(cancelNotification);
}
if (mScrobble) { if (mScrobble) {
Intent intent = new Intent("net.jjc1138.android.scrobbler.action.MUSIC_STATUS"); Intent intent = new Intent("net.jjc1138.android.scrobbler.action.MUSIC_STATUS");
intent.putExtra("playing", mState == STATE_PLAYING); intent.putExtra("playing", mState == STATE_PLAYING);
@ -499,13 +508,13 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
updateState(STATE_NORMAL); updateState(STATE_NORMAL);
} }
private void updateNotification() private boolean updateNotification()
{ {
Song song = getSong(0); Song song = getSong(0);
if (song == null || !mNotifyWhilePaused && mState == STATE_NORMAL) { if (song == null || !mNotifyWhilePaused && mState == STATE_NORMAL) {
mNotificationManager.cancel(NOTIFICATION_ID); mNotificationManager.cancel(NOTIFICATION_ID);
return; return true;
} }
String title = song.title; String title = song.title;
@ -530,6 +539,8 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
mNotification = notification; mNotification = notification;
mNotificationManager.notify(NOTIFICATION_ID, mNotification); mNotificationManager.notify(NOTIFICATION_ID, mNotification);
return false;
} }
private boolean isSpeakerOn() private boolean isSpeakerOn()
@ -562,14 +573,12 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
mMediaPlayer.start(); mMediaPlayer.start();
updateState(STATE_PLAYING); updateState(STATE_PLAYING);
startForegroundCompat(NOTIFICATION_ID, mNotification);
} }
private void pause() private void pause()
{ {
mMediaPlayer.pause(); mMediaPlayer.pause();
updateState(STATE_NORMAL); updateState(STATE_NORMAL);
stopForegroundCompat(false);
} }
private void setPlaying(boolean play) private void setPlaying(boolean play)