Handle notifications better

Don't update them when unecessary. Fix an edge case where the notification
wouldn't update from the paused to playing style.
This commit is contained in:
Christopher Eby 2011-08-29 01:32:59 -05:00
parent a4ea3089f6
commit cc1a5cb448

View File

@ -164,7 +164,6 @@ public final class PlaybackService extends Service implements Handler.Callback,
MediaPlayer mMediaPlayer; MediaPlayer mMediaPlayer;
private boolean mMediaPlayerInitialized; private boolean mMediaPlayerInitialized;
private PowerManager.WakeLock mWakeLock; private PowerManager.WakeLock mWakeLock;
private Notification mNotification;
private SharedPreferences mSettings; private SharedPreferences mSettings;
private NotificationManager mNotificationManager; private NotificationManager mNotificationManager;
@ -398,6 +397,10 @@ public final class PlaybackService extends Service implements Handler.Callback,
updateNotification(); updateNotification();
} else if ("notification_mode".equals(key)){ } else if ("notification_mode".equals(key)){
mNotificationMode = Integer.parseInt(settings.getString("notification_mode", "1")); mNotificationMode = Integer.parseInt(settings.getString("notification_mode", "1"));
// This is the only way to remove a notification created by
// startForeground(), even if we are not currently in foreground
// mode.
stopForegroundCompat(true);
updateNotification(); updateNotification();
} else if ("scrobble".equals(key)) { } else if ("scrobble".equals(key)) {
mScrobble = settings.getBoolean("scrobble", false); mScrobble = settings.getBoolean("scrobble", false);
@ -496,14 +499,19 @@ public final class PlaybackService extends Service implements Handler.Callback,
} }
} }
if (mNotificationMode != NEVER) if (mNotificationMode != NEVER)
startForegroundCompat(NOTIFICATION_ID, mNotification); startForegroundCompat(NOTIFICATION_ID, new SongNotification(getSong(0), true));
} else { } else {
if (mMediaPlayerInitialized) { if (mMediaPlayerInitialized) {
synchronized (mMediaPlayer) { synchronized (mMediaPlayer) {
mMediaPlayer.pause(); mMediaPlayer.pause();
} }
} }
if (mNotificationMode == ALWAYS) {
stopForegroundCompat(false); stopForegroundCompat(false);
mNotificationManager.notify(NOTIFICATION_ID, new SongNotification(getSong(0), false));
} else {
stopForegroundCompat(true);
}
} }
} }
@ -530,8 +538,6 @@ public final class PlaybackService extends Service implements Handler.Callback,
private void broadcastChange(int state, Song song, long uptime) private void broadcastChange(int state, Song song, long uptime)
{ {
updateNotification();
Intent intent = new Intent(EVENT_CHANGED); Intent intent = new Intent(EVENT_CHANGED);
if (state != -1) if (state != -1)
intent.putExtra("state", state); intent.putExtra("state", state);
@ -560,14 +566,10 @@ public final class PlaybackService extends Service implements Handler.Callback,
private void updateNotification() private void updateNotification()
{ {
boolean shouldNotify = mNotificationMode == ALWAYS || mNotificationMode == WHEN_PLAYING && (mState & FLAG_PLAYING) != 0; if (mNotificationMode == ALWAYS || mNotificationMode == WHEN_PLAYING && (mState & FLAG_PLAYING) != 0)
Song song = getSong(0); mNotificationManager.notify(NOTIFICATION_ID, new SongNotification(getSong(0), (mState & FLAG_PLAYING) != 0));
if (song != null && shouldNotify) { else
mNotification = new SongNotification(song, (mState & FLAG_PLAYING) != 0); mNotificationManager.cancel(NOTIFICATION_ID);
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
} else {
stopForegroundCompat(true);
}
} }
/** /**
@ -642,6 +644,8 @@ public final class PlaybackService extends Service implements Handler.Callback,
Log.e("VanillaMusic", "IOException", e); Log.e("VanillaMusic", "IOException", e);
} }
updateNotification();
getSong(+2); getSong(+2);
mTimeline.purge(); mTimeline.purge();
mHandler.removeMessages(SAVE_STATE); mHandler.removeMessages(SAVE_STATE);