From a44f0bce353e00e9120ee72f1dd907c2175ecc3e Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Mon, 15 Jan 2024 16:23:04 -0700 Subject: [PATCH] playback: stop playback on task removal if paused This is apparently the standard behavior that media apps should use to allow the foreground state to be exited. I personally don't want to make it really unilateral like that, so if playback is already ongoing I'll keep the foreground state going. --- .../auxio/playback/system/PlaybackService.kt | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) 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 0981a24ee..72c0e9de4 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 @@ -191,7 +191,12 @@ class PlaybackService : override fun onBind(intent: Intent): IBinder? = null - // TODO: Implement task removal (Have to radically alter state saving to occur at runtime) + override fun onTaskRemoved(rootIntent: Intent?) { + super.onTaskRemoved(rootIntent) + if (!playbackManager.progression.isPlaying) { + endSession() + } + } override fun onDestroy() { super.onDestroy() @@ -631,6 +636,13 @@ class PlaybackService : .putExtra(AudioEffect.EXTRA_CONTENT_TYPE, AudioEffect.CONTENT_TYPE_MUSIC)) } + private fun endSession() { + // This session has ended, so we need to reset this flag for when the next + // session starts. + hasPlayed = false + foregroundManager.tryStopForeground() + } + /** * A [BroadcastReceiver] for receiving playback-specific [Intent]s from the system that require * an active [IntentFilter] to be registered. @@ -687,10 +699,7 @@ class PlaybackService : ACTION_EXIT -> { logD("Received exit event") playbackManager.playing(false) - // This session has ended, so we need to reset this flag for when the next - // session starts. - hasPlayed = false - foregroundManager.tryStopForeground() + endSession() } WidgetProvider.ACTION_WIDGET_UPDATE -> { logD("Received widget update event")