Make notification dismissible when playback is paused

This commit is contained in:
Samriddha Basu 2017-03-13 05:58:23 +05:30
commit 70396deaf3

View file

@ -120,6 +120,8 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
public static final int REPEAT_MODE_THIS = 2; public static final int REPEAT_MODE_THIS = 2;
public static final int SAVE_QUEUES = 0; public static final int SAVE_QUEUES = 0;
private static final int NOTIFY_MODE_FOREGROUND = 1;
private static final int NOTIFY_MODE_BACKGROUND = 0;
private final IBinder musicBind = new MusicBinder(); private final IBinder musicBind = new MusicBinder();
@ -169,6 +171,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
private boolean isServiceBound; private boolean isServiceBound;
private Handler uiThreadHandler; private Handler uiThreadHandler;
private int mNotifyMode = NOTIFY_MODE_BACKGROUND;
private static String getTrackUri(@NonNull Song song) { private static String getTrackUri(@NonNull Song song) {
return MusicUtil.getSongFileUri(song.id).toString(); return MusicUtil.getSongFileUri(song.id).toString();
@ -970,7 +973,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
switch (what) { switch (what) {
case PLAY_STATE_CHANGED: case PLAY_STATE_CHANGED:
final boolean isPlaying = isPlaying(); final boolean isPlaying = isPlaying();
updateNotification(); buildNotification();
//noinspection deprecation //noinspection deprecation
remoteControlClient.setPlaybackState(isPlaying ? RemoteControlClient.PLAYSTATE_PLAYING : RemoteControlClient.PLAYSTATE_PAUSED); remoteControlClient.setPlaybackState(isPlaying ? RemoteControlClient.PLAYSTATE_PLAYING : RemoteControlClient.PLAYSTATE_PAUSED);
if (!isPlaying && getSongProgressMillis() > 0) { if (!isPlaying && getSongProgressMillis() > 0) {
@ -979,7 +982,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
songPlayCountHelper.notifyPlayStateChanged(isPlaying); songPlayCountHelper.notifyPlayStateChanged(isPlaying);
break; break;
case META_CHANGED: case META_CHANGED:
updateNotification(); buildNotification();
updateRemoteControlClient(); updateRemoteControlClient();
savePosition(); savePosition();
savePositionInTrack(); savePositionInTrack();
@ -1030,7 +1033,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
updateRemoteControlClient(); updateRemoteControlClient();
break; break;
case PreferenceUtil.COLORED_NOTIFICATION: case PreferenceUtil.COLORED_NOTIFICATION:
updateNotification(); buildNotification();
break; break;
} }
} }
@ -1087,12 +1090,8 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
} }
} }
private synchronized void updateNotification() { private synchronized void buildNotification() {
final Song song = getCurrentSong(); final Song song = getCurrentSong();
if (song.id == -1) {
killNotification();
return;
}
final String albumName = song.albumName; final String albumName = song.albumName;
final String artistName = song.artistName; final String artistName = song.artistName;
@ -1160,13 +1159,34 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
} }
notification = builder.build(); notification = builder.build();
updateNotification(notification);
}
});
}
});
}
private void updateNotification(Notification notification) {
int newNotifyMode;
if (isPlaying()) {
newNotifyMode = NOTIFY_MODE_FOREGROUND;
} else {
newNotifyMode = NOTIFY_MODE_BACKGROUND;
}
if (mNotifyMode != newNotifyMode && mNotifyMode == NOTIFY_MODE_FOREGROUND) {
stopForeground(false);
}
if (newNotifyMode == NOTIFY_MODE_FOREGROUND) {
startForeground(1, notification); startForeground(1, notification);
} else if (newNotifyMode == NOTIFY_MODE_BACKGROUND) {
notificationManager.notify(1, notification);
} }
}); mNotifyMode = newNotifyMode;
}
});
} }
private PendingIntent retrievePlaybackAction(final String action) { private PendingIntent retrievePlaybackAction(final String action) {
@ -1179,6 +1199,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
public synchronized void killNotification() { public synchronized void killNotification() {
this.stopForeground(true); this.stopForeground(true);
notificationManager.cancelAll();
} }
private static final class PlaybackHandler extends Handler { private static final class PlaybackHandler extends Handler {