From e83867270bec91fe3891b92fc82b487d289b1b12 Mon Sep 17 00:00:00 2001 From: OxygenCobalt Date: Sat, 21 Aug 2021 16:53:54 -0600 Subject: [PATCH] playback: fix android 11 notification issue Fix a problem where the colorize notification option would never have any effect on Android 11 since the system would default to the media session if there was no icon. --- .../playback/system/PlaybackNotification.kt | 2 +- .../auxio/playback/system/PlaybackService.kt | 3 +++ .../system/PlaybackSessionConnector.kt | 21 +++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackNotification.kt b/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackNotification.kt index 65d75103e..0e546445c 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackNotification.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackNotification.kt @@ -46,7 +46,7 @@ class PlaybackNotification private constructor( private val context: Context, mediaToken: MediaSessionCompat.Token ) : NotificationCompat.Builder(context, CHANNEL_ID) { - val settingsManager = SettingsManager.getInstance() + private val settingsManager = SettingsManager.getInstance() init { setSmallIcon(R.drawable.ic_song) diff --git a/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt b/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt index 177f87bc2..760b92f29 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt @@ -313,6 +313,7 @@ class PlaybackService : Service(), Player.Listener, PlaybackStateManager.Callbac override fun onColorizeNotifUpdate(doColorize: Boolean) { playbackManager.song?.let { song -> + connector.onSongUpdate(song) notification.setMetadata(song, ::startForegroundOrNotify) } } @@ -416,6 +417,8 @@ class PlaybackService : Service(), Player.Listener, PlaybackStateManager.Callbac } else { startForeground(PlaybackNotification.NOTIFICATION_ID, notification.build()) } + + isForeground = true } else { // If we are already in foreground just update the notification notificationManager.notify( diff --git a/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackSessionConnector.kt b/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackSessionConnector.kt index e6bcd933f..1ae65ebee 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackSessionConnector.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackSessionConnector.kt @@ -20,15 +20,18 @@ package org.oxycblt.auxio.playback.system import android.content.Context import android.content.Intent +import android.os.Build import android.os.SystemClock import android.support.v4.media.MediaMetadataCompat import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.PlaybackStateCompat import com.google.android.exoplayer2.Player import org.oxycblt.auxio.coil.loadBitmap +import org.oxycblt.auxio.logD import org.oxycblt.auxio.music.Song import org.oxycblt.auxio.playback.state.LoopMode import org.oxycblt.auxio.playback.state.PlaybackStateManager +import org.oxycblt.auxio.settings.SettingsManager /** * Nightmarish class that coordinates communication between [MediaSessionCompat], [Player], @@ -124,6 +127,24 @@ class PlaybackSessionConnector( .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, song.album.name) .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, song.duration) + // Oh my god. I don't like swearing in my comments, but I have no other way to describe my + // attitudes by the insane change Android 11 made to MediaStyle. Basically, they changed + // the notification code to FIRST look for a large icon [like previous versions], but THEN + // TO LOOK TO THE MEDIA SESSION SECOND. Cue me having to debug this issue for over 3 hours + // thinking it was some kind of strange bytecode problem, only to realize that it was this + // undocumented behavior change. + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + logD("Doing colorizeNotif R+ hack") + + val settingsManager = SettingsManager.getInstance() + + if (!settingsManager.colorizeNotif) { + mediaSession.setMetadata(builder.build()) + return + } + } + // Load the cover asynchronously. This is the entire reason I don't use a plain // MediaSessionConnector, which AFAIK makes it impossible to load this the way I do // without a bunch of stupid race conditions.