[Notification] Made notification dismissible on API 31 and lower

This commit is contained in:
Prathamesh More 2021-12-10 20:18:18 +05:30
parent 6dc152b911
commit 0cd1b5d3bd
3 changed files with 32 additions and 2 deletions

View file

@ -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;
}

View file

@ -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)

View file

@ -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) {