Fixed a bug where the notification wouldn't disappear when the queue is cleared. Fixes #97
This commit is contained in:
parent
c136c2e056
commit
d294822e7c
4 changed files with 35 additions and 19 deletions
|
|
@ -30,7 +30,6 @@ import android.provider.MediaStore;
|
|||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
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.PlaybackStateCompat;
|
||||
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);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
if (song.id == -1) {
|
||||
mediaSession.setMetadata(null);
|
||||
return;
|
||||
}
|
||||
|
||||
final MediaMetadataCompat.Builder metaData = new MediaMetadataCompat.Builder()
|
||||
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, song.artistName)
|
||||
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, song.artistName)
|
||||
|
|
@ -568,7 +587,6 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
|||
} else {
|
||||
mediaSession.setMetadata(metaData.build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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) {
|
||||
switch (what) {
|
||||
case PLAY_STATE_CHANGED:
|
||||
updateNotification();
|
||||
updateMediaSessionPlaybackState();
|
||||
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) {
|
||||
savePositionInTrack();
|
||||
}
|
||||
songPlayCountHelper.notifyPlayStateChanged(isPlaying);
|
||||
break;
|
||||
case META_CHANGED:
|
||||
playingNotification.update();
|
||||
updateMediaSession();
|
||||
updateNotification();
|
||||
updateMediaSessionMetaData();
|
||||
savePosition();
|
||||
savePositionInTrack();
|
||||
final Song currentSong = getCurrentSong();
|
||||
|
|
@ -1011,12 +1024,12 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
|||
songPlayCountHelper.notifySongChanged(currentSong);
|
||||
break;
|
||||
case QUEUE_CHANGED:
|
||||
updateMediaSession();
|
||||
updateMediaSessionMetaData(); // because playing queue size might have changed
|
||||
saveState();
|
||||
if (playingQueue.size() > 0) {
|
||||
prepareNext();
|
||||
} else {
|
||||
quit();
|
||||
playingNotification.stop();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
@ -1052,10 +1065,10 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
|||
break;
|
||||
case PreferenceUtil.ALBUM_ART_ON_LOCKSCREEN:
|
||||
case PreferenceUtil.BLURRED_ALBUM_ART:
|
||||
updateMediaSession();
|
||||
updateMediaSessionMetaData();
|
||||
break;
|
||||
case PreferenceUtil.COLORED_NOTIFICATION:
|
||||
playingNotification.update();
|
||||
updateNotification();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ import com.kabouzeid.gramophone.service.MusicService;
|
|||
*/
|
||||
|
||||
public interface PlayingNotification {
|
||||
int NOTIFICATION_ID = 1;
|
||||
|
||||
void init(MusicService service);
|
||||
|
||||
void update();
|
||||
|
|
|
|||
|
|
@ -133,7 +133,7 @@ public class PlayingNotificationImpl implements PlayingNotification {
|
|||
|
||||
if (stopped)
|
||||
return; // notification has been stopped before loading was finished
|
||||
service.startForeground(1, notification);
|
||||
service.startForeground(NOTIFICATION_ID, notification);
|
||||
}
|
||||
|
||||
private void setBackgroundColor(int color) {
|
||||
|
|
|
|||
|
|
@ -157,9 +157,9 @@ public class PlayingNotificationImpl24 implements PlayingNotification {
|
|||
}
|
||||
|
||||
if (newNotifyMode == NOTIFY_MODE_FOREGROUND) {
|
||||
service.startForeground(1, notification);
|
||||
service.startForeground(NOTIFICATION_ID, notification);
|
||||
} else if (newNotifyMode == NOTIFY_MODE_BACKGROUND) {
|
||||
notificationManager.notify(1, notification);
|
||||
notificationManager.notify(NOTIFICATION_ID, notification);
|
||||
}
|
||||
|
||||
notifyMode = newNotifyMode;
|
||||
|
|
@ -169,5 +169,6 @@ public class PlayingNotificationImpl24 implements PlayingNotification {
|
|||
public synchronized void stop() {
|
||||
stopped = true;
|
||||
service.stopForeground(true);
|
||||
notificationManager.cancel(NOTIFICATION_ID);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue