Fix incomplete close & Add playback stop switch
This commit is contained in:
@@ -152,6 +152,11 @@ class AuxioService :
|
||||
} else {
|
||||
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
||||
isForeground = false
|
||||
|
||||
if (playbackFragment.shouldStopService) {
|
||||
Timber.d("Exit requested, stopping service")
|
||||
stopSelf()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +60,8 @@ interface PlaybackSettings : Settings<PlaybackSettings.Listener> {
|
||||
val pauseOnRepeat: Boolean
|
||||
/** Whether to maintain the play/pause state when skipping or editing the queue */
|
||||
val rememberPause: Boolean
|
||||
/** Whether to always exit when task is removed, even if playing. */
|
||||
val exitOnTaskRemoval: Boolean
|
||||
|
||||
interface Listener {
|
||||
/** Called when one of the ReplayGain configurations have changed. */
|
||||
@@ -143,6 +145,9 @@ class PlaybackSettingsImpl @Inject constructor(@ApplicationContext context: Cont
|
||||
override val rememberPause: Boolean
|
||||
get() = sharedPreferences.getBoolean(getString(R.string.set_key_remember_pause), false)
|
||||
|
||||
override val exitOnTaskRemoval: Boolean
|
||||
get() = sharedPreferences.getBoolean(getString(R.string.set_key_task_exit), false)
|
||||
|
||||
override fun migrate() {
|
||||
// MusicMode was converted to PlaySong in 3.2.0
|
||||
fun Int.migrateMusicMode() =
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
package org.oxycblt.auxio.playback.service
|
||||
|
||||
import android.app.ActivityManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.support.v4.media.session.MediaSessionCompat
|
||||
@@ -27,16 +28,20 @@ import org.oxycblt.auxio.AuxioService.Companion.INTENT_KEY_START_ID
|
||||
import org.oxycblt.auxio.ForegroundListener
|
||||
import org.oxycblt.auxio.ForegroundServiceNotification
|
||||
import org.oxycblt.auxio.IntegerTable
|
||||
import org.oxycblt.auxio.playback.PlaybackSettings
|
||||
import org.oxycblt.auxio.playback.state.DeferredPlayback
|
||||
import org.oxycblt.auxio.playback.state.PlaybackStateManager
|
||||
import org.oxycblt.auxio.widgets.WidgetComponent
|
||||
import org.oxycblt.musikr.MusicParent
|
||||
import org.oxycblt.musikr.Song
|
||||
import timber.log.Timber as L
|
||||
|
||||
class PlaybackServiceFragment
|
||||
private constructor(
|
||||
context: Context,
|
||||
private val context: Context,
|
||||
private val foregroundListener: ForegroundListener,
|
||||
private val playbackManager: PlaybackStateManager,
|
||||
private val playbackSettings: PlaybackSettings,
|
||||
exoHolderFactory: ExoPlaybackStateHolder.Factory,
|
||||
sessionHolderFactory: MediaSessionHolder.Factory,
|
||||
widgetComponentFactory: WidgetComponent.Factory,
|
||||
@@ -46,6 +51,7 @@ private constructor(
|
||||
@Inject
|
||||
constructor(
|
||||
private val playbackManager: PlaybackStateManager,
|
||||
private val playbackSettings: PlaybackSettings,
|
||||
private val exoHolderFactory: ExoPlaybackStateHolder.Factory,
|
||||
private val sessionHolderFactory: MediaSessionHolder.Factory,
|
||||
private val widgetComponentFactory: WidgetComponent.Factory,
|
||||
@@ -56,6 +62,7 @@ private constructor(
|
||||
context,
|
||||
foregroundListener,
|
||||
playbackManager,
|
||||
playbackSettings,
|
||||
exoHolderFactory,
|
||||
sessionHolderFactory,
|
||||
widgetComponentFactory,
|
||||
@@ -67,7 +74,44 @@ private constructor(
|
||||
private val exoHolder = exoHolderFactory.create()
|
||||
private val sessionHolder = sessionHolderFactory.create(context, foregroundListener)
|
||||
private val widgetComponent = widgetComponentFactory.create(context)
|
||||
private val systemReceiver = systemReceiverFactory.create(context, widgetComponent)
|
||||
private val activityManager =
|
||||
context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
||||
private val systemReceiver =
|
||||
systemReceiverFactory.create(
|
||||
context,
|
||||
widgetComponent,
|
||||
onExitRequested = { handleExitRequest() },
|
||||
)
|
||||
|
||||
// Tracks whether an intentional exit has been requested.
|
||||
private var exitRequested = false
|
||||
|
||||
private fun isAppForegrounded(): Boolean {
|
||||
val processes = activityManager.runningAppProcesses ?: return false
|
||||
val pkg = context.packageName
|
||||
val own = processes.find { it.processName == pkg } ?: return false
|
||||
val imp = own.importance
|
||||
val visible =
|
||||
// IMPORTANCE_FOREGROUND (100): Process in foreground UI, user is interacting.
|
||||
// IMPORTANCE_VISIBLE (200): Process visible, but not in immediate foreground.
|
||||
imp == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND ||
|
||||
imp == ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE
|
||||
L.d("Importance: $imp, visible: $visible")
|
||||
return visible
|
||||
}
|
||||
|
||||
private fun handleExitRequest() {
|
||||
if (isAppForegrounded()) {
|
||||
// (x) in notification will no longer exist, but it might still
|
||||
// be triggered by other ways, e.g. Bluetooth devices.
|
||||
L.d("Exit Requested: App visible, pausing")
|
||||
playbackManager.playing(false)
|
||||
} else {
|
||||
L.d("Exit Requested: App not visible, exiting")
|
||||
exitRequested = true
|
||||
playbackManager.endSession()
|
||||
}
|
||||
}
|
||||
|
||||
// --- MEDIASESSION CALLBACKS ---
|
||||
|
||||
@@ -81,7 +125,11 @@ private constructor(
|
||||
}
|
||||
|
||||
fun handleTaskRemoved() {
|
||||
if (!playbackManager.progression.isPlaying) {
|
||||
val shouldExit =
|
||||
!playbackManager.progression.isPlaying || playbackSettings.exitOnTaskRemoval
|
||||
|
||||
if (shouldExit) {
|
||||
exitRequested = true
|
||||
playbackManager.endSession()
|
||||
}
|
||||
}
|
||||
@@ -128,6 +176,9 @@ private constructor(
|
||||
val notification: ForegroundServiceNotification?
|
||||
get() = if (exoHolder.sessionOngoing) sessionHolder.notification else null
|
||||
|
||||
val shouldStopService: Boolean
|
||||
get() = exitRequested && !exoHolder.sessionOngoing
|
||||
|
||||
fun release() {
|
||||
waitJob.cancel()
|
||||
playbackManager.removeListener(this)
|
||||
@@ -137,6 +188,15 @@ private constructor(
|
||||
exoHolder.release()
|
||||
}
|
||||
|
||||
override fun onNewPlayback(
|
||||
parent: MusicParent?,
|
||||
queue: List<Song>,
|
||||
index: Int,
|
||||
isShuffled: Boolean,
|
||||
) {
|
||||
exitRequested = false
|
||||
}
|
||||
|
||||
override fun onSessionEnded() {
|
||||
foregroundListener.updateForeground(ForegroundListener.Change.MEDIA_SESSION)
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ private constructor(
|
||||
private val playbackManager: PlaybackStateManager,
|
||||
private val playbackSettings: PlaybackSettings,
|
||||
private val widgetComponent: WidgetComponent,
|
||||
private val onExitRequested: () -> Unit,
|
||||
) : BroadcastReceiver() {
|
||||
private var initialHeadsetPlugEventHandled = false
|
||||
|
||||
@@ -50,8 +51,18 @@ private constructor(
|
||||
private val playbackManager: PlaybackStateManager,
|
||||
private val playbackSettings: PlaybackSettings,
|
||||
) {
|
||||
fun create(context: Context, widgetComponent: WidgetComponent) =
|
||||
SystemPlaybackReceiver(context, playbackManager, playbackSettings, widgetComponent)
|
||||
fun create(
|
||||
context: Context,
|
||||
widgetComponent: WidgetComponent,
|
||||
onExitRequested: () -> Unit,
|
||||
) =
|
||||
SystemPlaybackReceiver(
|
||||
context,
|
||||
playbackManager,
|
||||
playbackSettings,
|
||||
widgetComponent,
|
||||
onExitRequested,
|
||||
)
|
||||
}
|
||||
|
||||
@Suppress("WrongConstant")
|
||||
@@ -116,7 +127,7 @@ private constructor(
|
||||
}
|
||||
PlaybackActions.ACTION_EXIT -> {
|
||||
L.d("Received exit event")
|
||||
playbackManager.endSession()
|
||||
onExitRequested()
|
||||
}
|
||||
WidgetProvider.ACTION_WIDGET_UPDATE -> {
|
||||
L.d("Received widget update event")
|
||||
|
||||
@@ -49,6 +49,8 @@
|
||||
|
||||
<string name="set_key_search_filter_to" translatable="false">KEY_SEARCH_FILTER</string>
|
||||
|
||||
<string name="set_key_task_exit" translatable="false">auxio_task_exit</string>
|
||||
|
||||
<string name="set_key_songs_sort" translatable="false">auxio_songs_sort</string>
|
||||
<string name="set_key_albums_sort" translatable="false">auxio_albums_sort</string>
|
||||
<string name="set_key_artists_sort" translatable="false">auxio_artists_sort</string>
|
||||
|
||||
@@ -260,6 +260,8 @@
|
||||
<string name="set_play_song_by_itself">Play song by itself</string>
|
||||
<string name="set_keep_shuffle">Remember shuffle</string>
|
||||
<string name="set_keep_shuffle_desc">Keep shuffle on when playing a new song</string>
|
||||
<string name="set_task_exit">Always exit on close</string>
|
||||
<string name="set_task_exit_desc">Stop playback when removed from recents, even if playing</string>
|
||||
|
||||
<string name="set_content">Content</string>
|
||||
<string name="set_content_desc">Control how music and images are loaded</string>
|
||||
|
||||
@@ -50,6 +50,12 @@
|
||||
app:summary="@string/set_keep_shuffle_desc"
|
||||
app:title="@string/set_keep_shuffle" />
|
||||
|
||||
<SwitchPreferenceCompat
|
||||
app:defaultValue="false"
|
||||
app:key="@string/set_key_task_exit"
|
||||
app:summary="@string/set_task_exit_desc"
|
||||
app:title="@string/set_task_exit" />
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user