[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.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,16 +1457,20 @@ 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) {
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);
if (Build.VERSION.SDK_INT < VERSION_CODES.S) {
stopForeground(false);
isForeground = false;
}
}
if (newNotifyMode == NOTIFY_MODE_FOREGROUND) {
if (!isForeground && isPlaying) {
// Specify that this is a media service, if supported.
if (VersionUtils.hasQ()) {
startForeground(
@ -1487,7 +1488,7 @@ public class MusicService extends MediaBrowserServiceCompat
PlayingNotification.NOTIFICATION_ID, playingNotification.build()
);
}
notifyMode = newNotifyMode;
}
return Unit.INSTANCE;
}

View file

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

View file

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