Add auto stop after no playback activity

This commit is contained in:
Le Zhang
2026-01-19 07:46:55 +09:00
parent 593c55845f
commit b4f464ef0b
@@ -23,7 +23,11 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.MediaSessionCompat
import javax.inject.Inject import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.oxycblt.auxio.AuxioService.Companion.INTENT_KEY_START_ID import org.oxycblt.auxio.AuxioService.Companion.INTENT_KEY_START_ID
import org.oxycblt.auxio.ForegroundListener import org.oxycblt.auxio.ForegroundListener
import org.oxycblt.auxio.ForegroundServiceNotification import org.oxycblt.auxio.ForegroundServiceNotification
@@ -31,6 +35,7 @@ import org.oxycblt.auxio.IntegerTable
import org.oxycblt.auxio.playback.PlaybackSettings import org.oxycblt.auxio.playback.PlaybackSettings
import org.oxycblt.auxio.playback.state.DeferredPlayback import org.oxycblt.auxio.playback.state.DeferredPlayback
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.widgets.WidgetComponent import org.oxycblt.auxio.widgets.WidgetComponent
import org.oxycblt.musikr.MusicParent import org.oxycblt.musikr.MusicParent
import org.oxycblt.musikr.Song import org.oxycblt.musikr.Song
@@ -71,6 +76,8 @@ private constructor(
} }
private val waitJob = Job() private val waitJob = Job()
private val scope = CoroutineScope(Dispatchers.Main + waitJob)
private var autoStopJob: Job? = null
private val exoHolder = exoHolderFactory.create() private val exoHolder = exoHolderFactory.create()
private val sessionHolder = sessionHolderFactory.create(context, foregroundListener) private val sessionHolder = sessionHolderFactory.create(context, foregroundListener)
private val widgetComponent = widgetComponentFactory.create(context) private val widgetComponent = widgetComponentFactory.create(context)
@@ -101,15 +108,39 @@ private constructor(
} }
private fun handleExitRequest() { private fun handleExitRequest() {
if (isAppForegrounded()) { val foregrounded = isAppForegrounded()
// (x) in notification no longer exists, but it might still exitRequested = !foregrounded
// be triggered by other ways, e.g. Bluetooth devices. L.d("Exit requested: foregrounded=$foregrounded, exitRequested=$exitRequested")
L.d("Exit Requested: App visible, pausing") // possibly redundant?
playbackManager.playing(false) // although endSession() should end up calling scheduleAutoStop() due to
} else { // onProgressionChanged() override, we use it just to be safe, in case it is
L.d("Exit Requested: App not visible, exiting") // not called if the state was already paused.
exitRequested = true scheduleAutoStop()
playbackManager.endSession() playbackManager.endSession()
}
private fun scheduleAutoStop() {
autoStopJob?.cancel()
autoStopJob =
scope.launch {
delay(AUTO_STOP_DELAY_MS)
L.d(
"Auto-stop timer expired after ${AUTO_STOP_DELAY_MS / 60000} minutes of inactivity"
)
handleExitRequest()
}
}
private fun cancelAutoStop() {
autoStopJob?.cancel()
autoStopJob = null
}
private fun updateAutoStopTimer(isPlaying: Boolean) {
if (isPlaying) {
cancelAutoStop()
} else if (playbackManager.currentSong != null) {
scheduleAutoStop()
} }
} }
@@ -121,6 +152,7 @@ private constructor(
widgetComponent.attach() widgetComponent.attach()
systemReceiver.attach() systemReceiver.attach()
playbackManager.addListener(this) playbackManager.addListener(this)
updateAutoStopTimer(playbackManager.progression.isPlaying)
return sessionHolder.token return sessionHolder.token
} }
@@ -180,6 +212,7 @@ private constructor(
get() = exitRequested && !exoHolder.sessionOngoing get() = exitRequested && !exoHolder.sessionOngoing
fun release() { fun release() {
autoStopJob?.cancel()
waitJob.cancel() waitJob.cancel()
playbackManager.removeListener(this) playbackManager.removeListener(this)
systemReceiver.release() systemReceiver.release()
@@ -195,9 +228,19 @@ private constructor(
isShuffled: Boolean, isShuffled: Boolean,
) { ) {
exitRequested = false exitRequested = false
cancelAutoStop()
}
override fun onProgressionChanged(progression: Progression) {
// Update timer whenever play/pause state changes
updateAutoStopTimer(progression.isPlaying)
} }
override fun onSessionEnded() { override fun onSessionEnded() {
foregroundListener.updateForeground(ForegroundListener.Change.MEDIA_SESSION) foregroundListener.updateForeground(ForegroundListener.Change.MEDIA_SESSION)
} }
private companion object {
private const val AUTO_STOP_DELAY_MS = 30L * 60L * 1000L
}
} }