Fixed a bug where the notification wouldn't disappear when the queue is cleared. Fixes #97

This commit is contained in:
Karim Abou Zeid 2017-03-23 13:13:49 +01:00
commit d294822e7c
4 changed files with 35 additions and 19 deletions

View file

@ -30,7 +30,6 @@ import android.provider.MediaStore;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaButtonReceiver;
import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat; import android.support.v4.media.session.PlaybackStateCompat;
import android.widget.Toast; import android.widget.Toast;
@ -522,9 +521,29 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
return (getAudioManager().requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED); return (getAudioManager().requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED);
} }
private void updateMediaSession() { private void updateNotification() {
if (getCurrentSong().id != -1) {
playingNotification.update();
}
}
private void updateMediaSessionPlaybackState() {
mediaSession.setPlaybackState(
new PlaybackStateCompat.Builder()
.setActions(MEDIA_SESSION_ACTIONS)
.setState(isPlaying() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED,
getPosition(), 1)
.build());
}
private void updateMediaSessionMetaData() {
final Song song = getCurrentSong(); final Song song = getCurrentSong();
if (song.id == -1) {
mediaSession.setMetadata(null);
return;
}
final MediaMetadataCompat.Builder metaData = new MediaMetadataCompat.Builder() final MediaMetadataCompat.Builder metaData = new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, song.artistName) .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, song.artistName)
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, song.artistName) .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, song.artistName)
@ -568,7 +587,6 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
} else { } else {
mediaSession.setMetadata(metaData.build()); mediaSession.setMetadata(metaData.build());
} }
} }
private static Bitmap copy(Bitmap bitmap) { private static Bitmap copy(Bitmap bitmap) {
@ -985,22 +1003,17 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
private void handleChangeInternal(@NonNull final String what) { private void handleChangeInternal(@NonNull final String what) {
switch (what) { switch (what) {
case PLAY_STATE_CHANGED: case PLAY_STATE_CHANGED:
updateNotification();
updateMediaSessionPlaybackState();
final boolean isPlaying = isPlaying(); final boolean isPlaying = isPlaying();
playingNotification.update();
mediaSession.setPlaybackState(
new PlaybackStateCompat.Builder()
.setActions(MEDIA_SESSION_ACTIONS)
.setState(isPlaying ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED,
getPosition(), 1)
.build());
if (!isPlaying && getSongProgressMillis() > 0) { if (!isPlaying && getSongProgressMillis() > 0) {
savePositionInTrack(); savePositionInTrack();
} }
songPlayCountHelper.notifyPlayStateChanged(isPlaying); songPlayCountHelper.notifyPlayStateChanged(isPlaying);
break; break;
case META_CHANGED: case META_CHANGED:
playingNotification.update(); updateNotification();
updateMediaSession(); updateMediaSessionMetaData();
savePosition(); savePosition();
savePositionInTrack(); savePositionInTrack();
final Song currentSong = getCurrentSong(); final Song currentSong = getCurrentSong();
@ -1011,12 +1024,12 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
songPlayCountHelper.notifySongChanged(currentSong); songPlayCountHelper.notifySongChanged(currentSong);
break; break;
case QUEUE_CHANGED: case QUEUE_CHANGED:
updateMediaSession(); updateMediaSessionMetaData(); // because playing queue size might have changed
saveState(); saveState();
if (playingQueue.size() > 0) { if (playingQueue.size() > 0) {
prepareNext(); prepareNext();
} else { } else {
quit(); playingNotification.stop();
} }
break; break;
} }
@ -1052,10 +1065,10 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
break; break;
case PreferenceUtil.ALBUM_ART_ON_LOCKSCREEN: case PreferenceUtil.ALBUM_ART_ON_LOCKSCREEN:
case PreferenceUtil.BLURRED_ALBUM_ART: case PreferenceUtil.BLURRED_ALBUM_ART:
updateMediaSession(); updateMediaSessionMetaData();
break; break;
case PreferenceUtil.COLORED_NOTIFICATION: case PreferenceUtil.COLORED_NOTIFICATION:
playingNotification.update(); updateNotification();
break; break;
} }
} }

View file

@ -7,6 +7,8 @@ import com.kabouzeid.gramophone.service.MusicService;
*/ */
public interface PlayingNotification { public interface PlayingNotification {
int NOTIFICATION_ID = 1;
void init(MusicService service); void init(MusicService service);
void update(); void update();

View file

@ -133,7 +133,7 @@ public class PlayingNotificationImpl implements PlayingNotification {
if (stopped) if (stopped)
return; // notification has been stopped before loading was finished return; // notification has been stopped before loading was finished
service.startForeground(1, notification); service.startForeground(NOTIFICATION_ID, notification);
} }
private void setBackgroundColor(int color) { private void setBackgroundColor(int color) {

View file

@ -157,9 +157,9 @@ public class PlayingNotificationImpl24 implements PlayingNotification {
} }
if (newNotifyMode == NOTIFY_MODE_FOREGROUND) { if (newNotifyMode == NOTIFY_MODE_FOREGROUND) {
service.startForeground(1, notification); service.startForeground(NOTIFICATION_ID, notification);
} else if (newNotifyMode == NOTIFY_MODE_BACKGROUND) { } else if (newNotifyMode == NOTIFY_MODE_BACKGROUND) {
notificationManager.notify(1, notification); notificationManager.notify(NOTIFICATION_ID, notification);
} }
notifyMode = newNotifyMode; notifyMode = newNotifyMode;
@ -169,5 +169,6 @@ public class PlayingNotificationImpl24 implements PlayingNotification {
public synchronized void stop() { public synchronized void stop() {
stopped = true; stopped = true;
service.stopForeground(true); service.stopForeground(true);
notificationManager.cancel(NOTIFICATION_ID);
} }
} }