[Notification] Fixed empty notification when queue is cleared

This commit is contained in:
Prathamesh More 2021-12-25 17:00:20 +05:30
parent e5cb0dc241
commit 89d1b7e835
3 changed files with 28 additions and 44 deletions

View file

@ -24,8 +24,6 @@ import static code.name.monkey.retromusic.ConstantsKt.COLORED_NOTIFICATION;
import static code.name.monkey.retromusic.ConstantsKt.CROSS_FADE_DURATION; import static code.name.monkey.retromusic.ConstantsKt.CROSS_FADE_DURATION;
import static code.name.monkey.retromusic.ConstantsKt.TOGGLE_HEADSET; import static code.name.monkey.retromusic.ConstantsKt.TOGGLE_HEADSET;
import static code.name.monkey.retromusic.service.AudioFader.startFadeAnimator; import static code.name.monkey.retromusic.service.AudioFader.startFadeAnimator;
import static code.name.monkey.retromusic.service.notification.PlayingNotification.NOTIFY_MODE_BACKGROUND;
import static code.name.monkey.retromusic.service.notification.PlayingNotification.NOTIFY_MODE_FOREGROUND;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
@ -371,7 +369,6 @@ public class MusicService extends MediaBrowserServiceCompat
private PowerManager.WakeLock wakeLock; private PowerManager.WakeLock wakeLock;
private NotificationManager notificationManager; private NotificationManager notificationManager;
private boolean isForeground = false; private boolean isForeground = false;
private int notifyMode = NOTIFY_MODE_BACKGROUND;
private static Bitmap copy(Bitmap bitmap) { private static Bitmap copy(Bitmap bitmap) {
Bitmap.Config config = bitmap.getConfig(); Bitmap.Config config = bitmap.getConfig();
@ -1460,34 +1457,38 @@ public class MusicService extends MediaBrowserServiceCompat
} }
private Unit startForegroundOrNotify() { private Unit startForegroundOrNotify() {
int newNotifyMode = isPlaying() ? NOTIFY_MODE_FOREGROUND : NOTIFY_MODE_BACKGROUND; if (playingNotification != null && getCurrentSong().getId() != -1) {
boolean isPlaying = isPlaying();
if (notifyMode != newNotifyMode && newNotifyMode == NOTIFY_MODE_BACKGROUND) { if ((isForeground != isPlaying) && !isPlaying) {
// This makes the notification dismissible // This makes the notification dismissible
// We can't call stopForeground(false) on A12 though, which may result in crashes // We can't call stopForeground(false) on A12 though, which may result in crashes
// when we call startForeground after that e.g. when Alarm goes off, // when we call startForeground after that e.g. when Alarm goes off,
if (Build.VERSION.SDK_INT < VERSION_CODES.S) stopForeground(false); if (Build.VERSION.SDK_INT < VERSION_CODES.S) {
} stopForeground(false);
isForeground = false;
if (newNotifyMode == NOTIFY_MODE_FOREGROUND) { }
// Specify that this is a media service, if supported.
if (VersionUtils.hasQ()) {
startForeground(
PlayingNotification.NOTIFICATION_ID, playingNotification.build(),
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
);
} else {
startForeground(PlayingNotification.NOTIFICATION_ID, playingNotification.build());
} }
isForeground = true; if (!isForeground && isPlaying) {
} else { // Specify that this is a media service, if supported.
// If we are already in foreground just update the notification if (VersionUtils.hasQ()) {
notificationManager.notify( startForeground(
PlayingNotification.NOTIFICATION_ID, playingNotification.build() PlayingNotification.NOTIFICATION_ID, playingNotification.build(),
); ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
);
} else {
startForeground(PlayingNotification.NOTIFICATION_ID, playingNotification.build());
}
isForeground = true;
} else {
// If we are already in foreground just update the notification
notificationManager.notify(
PlayingNotification.NOTIFICATION_ID, playingNotification.build()
);
}
} }
notifyMode = newNotifyMode;
return Unit.INSTANCE; return Unit.INSTANCE;
} }

View file

@ -37,8 +37,6 @@ abstract class PlayingNotification(context: Context) :
const val NOTIFICATION_CONTROLS_SIZE_MULTIPLIER = 1.0f const val NOTIFICATION_CONTROLS_SIZE_MULTIPLIER = 1.0f
internal const val NOTIFICATION_CHANNEL_ID = "playing_notification" internal const val NOTIFICATION_CHANNEL_ID = "playing_notification"
const val NOTIFICATION_ID = 1 const val NOTIFICATION_ID = 1
const val NOTIFY_MODE_FOREGROUND = 1
const val NOTIFY_MODE_BACKGROUND = 0
@RequiresApi(26) @RequiresApi(26)

View file

@ -184,23 +184,8 @@ class PlayingNotificationImpl(
).build() ).build()
} }
private fun buildDismissAction(): NotificationCompat.Action {
return NotificationCompat.Action.Builder(
R.drawable.ic_close,
context.getString(R.string.customactivityoncrash_error_activity_error_details_close),
retrievePlaybackAction(ACTION_QUIT)
).build()
}
override fun setPlaying(isPlaying: Boolean) { override fun setPlaying(isPlaying: Boolean) {
mActions[2] = buildPlayAction(isPlaying) mActions[2] = buildPlayAction(isPlaying)
// Show dismiss action if we are not playing but only for A12+, as we can't call stopForeground(false)
// on A12 which would result in crashes when we call startForeground after that
if (!isPlaying) {
addAction(buildDismissAction())
} else {
if (mActions.size == 5) mActions.removeAt(4)
}
} }
override fun updateFavorite(song: Song, onUpdate: () -> Unit) { override fun updateFavorite(song: Song, onUpdate: () -> Unit) {