diff --git a/app/src/main/java/code/name/monkey/retromusic/service/MusicService.java b/app/src/main/java/code/name/monkey/retromusic/service/MusicService.java index 36ccbce61..651bb605f 100644 --- a/app/src/main/java/code/name/monkey/retromusic/service/MusicService.java +++ b/app/src/main/java/code/name/monkey/retromusic/service/MusicService.java @@ -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.TOGGLE_HEADSET; 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.PendingIntent; @@ -371,7 +369,6 @@ public class MusicService extends MediaBrowserServiceCompat private PowerManager.WakeLock wakeLock; private NotificationManager notificationManager; private boolean isForeground = false; - private int notifyMode = NOTIFY_MODE_BACKGROUND; private static Bitmap copy(Bitmap bitmap) { Bitmap.Config config = bitmap.getConfig(); @@ -1460,34 +1457,38 @@ public class MusicService extends MediaBrowserServiceCompat } 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) { - // This makes the notification dismissible - // 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, - if (Build.VERSION.SDK_INT < VERSION_CODES.S) stopForeground(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()); + if ((isForeground != isPlaying) && !isPlaying) { + // This makes the notification dismissible + // 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, + if (Build.VERSION.SDK_INT < VERSION_CODES.S) { + stopForeground(false); + isForeground = false; + } } - isForeground = true; - } else { - // If we are already in foreground just update the notification - notificationManager.notify( - PlayingNotification.NOTIFICATION_ID, playingNotification.build() - ); + if (!isForeground && isPlaying) { + // 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; + } else { + // If we are already in foreground just update the notification + notificationManager.notify( + PlayingNotification.NOTIFICATION_ID, playingNotification.build() + ); + } } - notifyMode = newNotifyMode; return Unit.INSTANCE; } diff --git a/app/src/main/java/code/name/monkey/retromusic/service/notification/PlayingNotification.kt b/app/src/main/java/code/name/monkey/retromusic/service/notification/PlayingNotification.kt index 3abb52a6e..162775f06 100644 --- a/app/src/main/java/code/name/monkey/retromusic/service/notification/PlayingNotification.kt +++ b/app/src/main/java/code/name/monkey/retromusic/service/notification/PlayingNotification.kt @@ -37,8 +37,6 @@ abstract class PlayingNotification(context: Context) : const val NOTIFICATION_CONTROLS_SIZE_MULTIPLIER = 1.0f internal const val NOTIFICATION_CHANNEL_ID = "playing_notification" const val NOTIFICATION_ID = 1 - const val NOTIFY_MODE_FOREGROUND = 1 - const val NOTIFY_MODE_BACKGROUND = 0 @RequiresApi(26) diff --git a/app/src/main/java/code/name/monkey/retromusic/service/notification/PlayingNotificationImpl.kt b/app/src/main/java/code/name/monkey/retromusic/service/notification/PlayingNotificationImpl.kt index b897066fe..57bf5685d 100644 --- a/app/src/main/java/code/name/monkey/retromusic/service/notification/PlayingNotificationImpl.kt +++ b/app/src/main/java/code/name/monkey/retromusic/service/notification/PlayingNotificationImpl.kt @@ -184,23 +184,8 @@ class PlayingNotificationImpl( ).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) { 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) {