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.
This commit is contained in:
OxygenCobalt 2021-08-21 16:53:54 -06:00
parent 2358be7ba4
commit e83867270b
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
3 changed files with 25 additions and 1 deletions

View file

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

View file

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

View file

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