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 435cb9f9e..36ccbce61 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,6 +24,8 @@ 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; @@ -369,6 +371,7 @@ 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(); @@ -1457,9 +1460,18 @@ public class MusicService extends MediaBrowserServiceCompat } private Unit startForegroundOrNotify() { - if (!isForeground) { + int newNotifyMode = isPlaying() ? NOTIFY_MODE_FOREGROUND : NOTIFY_MODE_BACKGROUND; + + 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 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + if (VersionUtils.hasQ()) { startForeground( PlayingNotification.NOTIFICATION_ID, playingNotification.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK @@ -1475,6 +1487,7 @@ public class MusicService extends MediaBrowserServiceCompat 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 162775f06..3abb52a6e 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,6 +37,8 @@ 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 b8ef0cca6..bbeafa735 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 @@ -186,8 +186,23 @@ 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 { + mActions.removeAt(4) + } } override fun updateFavorite(song: Song, onUpdate: () -> Unit) {