playback: fix broken mediasession lifecycle

This commit is contained in:
Alexander Capehart 2024-08-29 21:03:26 -06:00
parent 6ff2d55a68
commit 2bc4ed020b
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47

View file

@ -43,8 +43,6 @@ import org.oxycblt.auxio.music.service.MediaSessionUID
import org.oxycblt.auxio.music.service.toMediaDescription import org.oxycblt.auxio.music.service.toMediaDescription
import org.oxycblt.auxio.playback.ActionMode import org.oxycblt.auxio.playback.ActionMode
import org.oxycblt.auxio.playback.PlaybackSettings import org.oxycblt.auxio.playback.PlaybackSettings
import org.oxycblt.auxio.playback.service.MediaSessionInterface
import org.oxycblt.auxio.playback.service.PlaybackActions
import org.oxycblt.auxio.playback.state.PlaybackStateManager import org.oxycblt.auxio.playback.state.PlaybackStateManager
import org.oxycblt.auxio.playback.state.Progression import org.oxycblt.auxio.playback.state.Progression
import org.oxycblt.auxio.playback.state.QueueChange import org.oxycblt.auxio.playback.state.QueueChange
@ -62,34 +60,32 @@ import org.oxycblt.auxio.util.newMainPendingIntent
class MediaSessionHolder class MediaSessionHolder
private constructor( private constructor(
private val context: Context, private val context: Context,
private val sessionInterface: MediaSessionInterface, private val foregroundListener: ForegroundListener,
private val playbackManager: PlaybackStateManager, private val playbackManager: PlaybackStateManager,
private val playbackSettings: PlaybackSettings, private val playbackSettings: PlaybackSettings,
private val bitmapProvider: BitmapProvider, private val bitmapProvider: BitmapProvider,
private val imageSettings: ImageSettings private val imageSettings: ImageSettings,
) : private val mediaSessionInterface: MediaSessionInterface
MediaSessionCompat.Callback(), ) : PlaybackStateManager.Listener, ImageSettings.Listener, PlaybackSettings.Listener {
PlaybackStateManager.Listener,
ImageSettings.Listener,
PlaybackSettings.Listener {
class Factory class Factory
@Inject @Inject
constructor( constructor(
private val sessionInterface: MediaSessionInterface,
private val playbackManager: PlaybackStateManager, private val playbackManager: PlaybackStateManager,
private val playbackSettings: PlaybackSettings, private val playbackSettings: PlaybackSettings,
private val bitmapProvider: BitmapProvider, private val bitmapProvider: BitmapProvider,
private val imageSettings: ImageSettings, private val imageSettings: ImageSettings,
private val mediaSessionInterface: MediaSessionInterface
) { ) {
fun create(context: Context) = fun create(context: Context, foregroundListener: ForegroundListener) =
MediaSessionHolder( MediaSessionHolder(
context, context,
sessionInterface, foregroundListener,
playbackManager, playbackManager,
playbackSettings, playbackSettings,
bitmapProvider, bitmapProvider,
imageSettings) imageSettings,
mediaSessionInterface)
} }
private val mediaSession = MediaSessionCompat(context, context.packageName) private val mediaSession = MediaSessionCompat(context, context.packageName)
@ -100,20 +96,15 @@ private constructor(
val notification: ForegroundServiceNotification val notification: ForegroundServiceNotification
get() = _notification get() = _notification
private var foregroundListener: ForegroundListener? = null init {
fun attach(foregroundListener: ForegroundListener) {
mediaSession.apply {
isActive = true
setQueueTitle(context.getString(R.string.lbl_queue))
setCallback(sessionInterface)
setFlags(MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS)
}
this.foregroundListener = foregroundListener
playbackManager.addListener(this) playbackManager.addListener(this)
playbackSettings.registerListener(this) playbackSettings.registerListener(this)
imageSettings.registerListener(this) imageSettings.registerListener(this)
mediaSession.setCallback(this) mediaSession.apply {
isActive = true
setQueueTitle(context.getString(R.string.lbl_queue))
setCallback(mediaSessionInterface)
}
} }
/** /**
@ -121,7 +112,6 @@ private constructor(
* the [NotificationComponent]. * the [NotificationComponent].
*/ */
fun release() { fun release() {
foregroundListener = null
bitmapProvider.release() bitmapProvider.release()
playbackSettings.unregisterListener(this) playbackSettings.unregisterListener(this)
imageSettings.unregisterListener(this) imageSettings.unregisterListener(this)
@ -179,7 +169,7 @@ private constructor(
invalidateSessionState() invalidateSessionState()
_notification.updatePlaying(playbackManager.progression.isPlaying) _notification.updatePlaying(playbackManager.progression.isPlaying)
if (!bitmapProvider.isBusy) { if (!bitmapProvider.isBusy) {
foregroundListener?.updateForeground(ForegroundListener.Change.MEDIA_SESSION) foregroundListener.updateForeground(ForegroundListener.Change.MEDIA_SESSION)
} }
} }
@ -272,7 +262,7 @@ private constructor(
song.date?.let { song.date?.let {
logD("Adding date information") logD("Adding date information")
builder.putString(MediaMetadataCompat.METADATA_KEY_DATE, it.toString()) builder.putString(MediaMetadataCompat.METADATA_KEY_DATE, it.toString())
builder.putString(MediaMetadataCompat.METADATA_KEY_YEAR, it.year.toString()) builder.putLong(MediaMetadataCompat.METADATA_KEY_YEAR, it.year.toLong())
} }
// We are normally supposed to use URIs for album art, but that removes some of the // We are normally supposed to use URIs for album art, but that removes some of the
@ -290,7 +280,7 @@ private constructor(
val metadata = builder.build() val metadata = builder.build()
mediaSession.setMetadata(metadata) mediaSession.setMetadata(metadata)
_notification.updateMetadata(metadata) _notification.updateMetadata(metadata)
foregroundListener?.updateForeground(ForegroundListener.Change.MEDIA_SESSION) foregroundListener.updateForeground(ForegroundListener.Change.MEDIA_SESSION)
} }
}) })
} }
@ -382,7 +372,7 @@ private constructor(
if (!bitmapProvider.isBusy) { if (!bitmapProvider.isBusy) {
logD("Not loading a bitmap, post the notification") logD("Not loading a bitmap, post the notification")
foregroundListener?.updateForeground(ForegroundListener.Change.MEDIA_SESSION) foregroundListener.updateForeground(ForegroundListener.Change.MEDIA_SESSION)
} }
} }