Fix incomplete close & Add playback stop switch
This commit is contained in:
@@ -152,6 +152,11 @@ class AuxioService :
|
|||||||
} else {
|
} else {
|
||||||
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
||||||
isForeground = false
|
isForeground = false
|
||||||
|
|
||||||
|
if (playbackFragment.shouldStopService) {
|
||||||
|
Timber.d("Exit requested, stopping service")
|
||||||
|
stopSelf()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,6 +60,8 @@ interface PlaybackSettings : Settings<PlaybackSettings.Listener> {
|
|||||||
val pauseOnRepeat: Boolean
|
val pauseOnRepeat: Boolean
|
||||||
/** Whether to maintain the play/pause state when skipping or editing the queue */
|
/** Whether to maintain the play/pause state when skipping or editing the queue */
|
||||||
val rememberPause: Boolean
|
val rememberPause: Boolean
|
||||||
|
/** Whether to always exit when task is removed, even if playing. */
|
||||||
|
val exitOnTaskRemoval: Boolean
|
||||||
|
|
||||||
interface Listener {
|
interface Listener {
|
||||||
/** Called when one of the ReplayGain configurations have changed. */
|
/** Called when one of the ReplayGain configurations have changed. */
|
||||||
@@ -143,6 +145,9 @@ class PlaybackSettingsImpl @Inject constructor(@ApplicationContext context: Cont
|
|||||||
override val rememberPause: Boolean
|
override val rememberPause: Boolean
|
||||||
get() = sharedPreferences.getBoolean(getString(R.string.set_key_remember_pause), false)
|
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() {
|
override fun migrate() {
|
||||||
// MusicMode was converted to PlaySong in 3.2.0
|
// MusicMode was converted to PlaySong in 3.2.0
|
||||||
fun Int.migrateMusicMode() =
|
fun Int.migrateMusicMode() =
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
package org.oxycblt.auxio.playback.service
|
package org.oxycblt.auxio.playback.service
|
||||||
|
|
||||||
|
import android.app.ActivityManager
|
||||||
import android.content.Context
|
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
|
||||||
@@ -27,16 +28,20 @@ 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
|
||||||
import org.oxycblt.auxio.IntegerTable
|
import org.oxycblt.auxio.IntegerTable
|
||||||
|
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.widgets.WidgetComponent
|
import org.oxycblt.auxio.widgets.WidgetComponent
|
||||||
|
import org.oxycblt.musikr.MusicParent
|
||||||
|
import org.oxycblt.musikr.Song
|
||||||
import timber.log.Timber as L
|
import timber.log.Timber as L
|
||||||
|
|
||||||
class PlaybackServiceFragment
|
class PlaybackServiceFragment
|
||||||
private constructor(
|
private constructor(
|
||||||
context: Context,
|
private val context: Context,
|
||||||
private val foregroundListener: ForegroundListener,
|
private val foregroundListener: ForegroundListener,
|
||||||
private val playbackManager: PlaybackStateManager,
|
private val playbackManager: PlaybackStateManager,
|
||||||
|
private val playbackSettings: PlaybackSettings,
|
||||||
exoHolderFactory: ExoPlaybackStateHolder.Factory,
|
exoHolderFactory: ExoPlaybackStateHolder.Factory,
|
||||||
sessionHolderFactory: MediaSessionHolder.Factory,
|
sessionHolderFactory: MediaSessionHolder.Factory,
|
||||||
widgetComponentFactory: WidgetComponent.Factory,
|
widgetComponentFactory: WidgetComponent.Factory,
|
||||||
@@ -46,6 +51,7 @@ private constructor(
|
|||||||
@Inject
|
@Inject
|
||||||
constructor(
|
constructor(
|
||||||
private val playbackManager: PlaybackStateManager,
|
private val playbackManager: PlaybackStateManager,
|
||||||
|
private val playbackSettings: PlaybackSettings,
|
||||||
private val exoHolderFactory: ExoPlaybackStateHolder.Factory,
|
private val exoHolderFactory: ExoPlaybackStateHolder.Factory,
|
||||||
private val sessionHolderFactory: MediaSessionHolder.Factory,
|
private val sessionHolderFactory: MediaSessionHolder.Factory,
|
||||||
private val widgetComponentFactory: WidgetComponent.Factory,
|
private val widgetComponentFactory: WidgetComponent.Factory,
|
||||||
@@ -56,6 +62,7 @@ private constructor(
|
|||||||
context,
|
context,
|
||||||
foregroundListener,
|
foregroundListener,
|
||||||
playbackManager,
|
playbackManager,
|
||||||
|
playbackSettings,
|
||||||
exoHolderFactory,
|
exoHolderFactory,
|
||||||
sessionHolderFactory,
|
sessionHolderFactory,
|
||||||
widgetComponentFactory,
|
widgetComponentFactory,
|
||||||
@@ -67,7 +74,44 @@ private constructor(
|
|||||||
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)
|
||||||
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 ---
|
// --- MEDIASESSION CALLBACKS ---
|
||||||
|
|
||||||
@@ -81,7 +125,11 @@ private constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun handleTaskRemoved() {
|
fun handleTaskRemoved() {
|
||||||
if (!playbackManager.progression.isPlaying) {
|
val shouldExit =
|
||||||
|
!playbackManager.progression.isPlaying || playbackSettings.exitOnTaskRemoval
|
||||||
|
|
||||||
|
if (shouldExit) {
|
||||||
|
exitRequested = true
|
||||||
playbackManager.endSession()
|
playbackManager.endSession()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -128,6 +176,9 @@ private constructor(
|
|||||||
val notification: ForegroundServiceNotification?
|
val notification: ForegroundServiceNotification?
|
||||||
get() = if (exoHolder.sessionOngoing) sessionHolder.notification else null
|
get() = if (exoHolder.sessionOngoing) sessionHolder.notification else null
|
||||||
|
|
||||||
|
val shouldStopService: Boolean
|
||||||
|
get() = exitRequested && !exoHolder.sessionOngoing
|
||||||
|
|
||||||
fun release() {
|
fun release() {
|
||||||
waitJob.cancel()
|
waitJob.cancel()
|
||||||
playbackManager.removeListener(this)
|
playbackManager.removeListener(this)
|
||||||
@@ -137,6 +188,15 @@ private constructor(
|
|||||||
exoHolder.release()
|
exoHolder.release()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onNewPlayback(
|
||||||
|
parent: MusicParent?,
|
||||||
|
queue: List<Song>,
|
||||||
|
index: Int,
|
||||||
|
isShuffled: Boolean,
|
||||||
|
) {
|
||||||
|
exitRequested = false
|
||||||
|
}
|
||||||
|
|
||||||
override fun onSessionEnded() {
|
override fun onSessionEnded() {
|
||||||
foregroundListener.updateForeground(ForegroundListener.Change.MEDIA_SESSION)
|
foregroundListener.updateForeground(ForegroundListener.Change.MEDIA_SESSION)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ private constructor(
|
|||||||
private val playbackManager: PlaybackStateManager,
|
private val playbackManager: PlaybackStateManager,
|
||||||
private val playbackSettings: PlaybackSettings,
|
private val playbackSettings: PlaybackSettings,
|
||||||
private val widgetComponent: WidgetComponent,
|
private val widgetComponent: WidgetComponent,
|
||||||
|
private val onExitRequested: () -> Unit,
|
||||||
) : BroadcastReceiver() {
|
) : BroadcastReceiver() {
|
||||||
private var initialHeadsetPlugEventHandled = false
|
private var initialHeadsetPlugEventHandled = false
|
||||||
|
|
||||||
@@ -50,8 +51,18 @@ private constructor(
|
|||||||
private val playbackManager: PlaybackStateManager,
|
private val playbackManager: PlaybackStateManager,
|
||||||
private val playbackSettings: PlaybackSettings,
|
private val playbackSettings: PlaybackSettings,
|
||||||
) {
|
) {
|
||||||
fun create(context: Context, widgetComponent: WidgetComponent) =
|
fun create(
|
||||||
SystemPlaybackReceiver(context, playbackManager, playbackSettings, widgetComponent)
|
context: Context,
|
||||||
|
widgetComponent: WidgetComponent,
|
||||||
|
onExitRequested: () -> Unit,
|
||||||
|
) =
|
||||||
|
SystemPlaybackReceiver(
|
||||||
|
context,
|
||||||
|
playbackManager,
|
||||||
|
playbackSettings,
|
||||||
|
widgetComponent,
|
||||||
|
onExitRequested,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("WrongConstant")
|
@Suppress("WrongConstant")
|
||||||
@@ -116,7 +127,7 @@ private constructor(
|
|||||||
}
|
}
|
||||||
PlaybackActions.ACTION_EXIT -> {
|
PlaybackActions.ACTION_EXIT -> {
|
||||||
L.d("Received exit event")
|
L.d("Received exit event")
|
||||||
playbackManager.endSession()
|
onExitRequested()
|
||||||
}
|
}
|
||||||
WidgetProvider.ACTION_WIDGET_UPDATE -> {
|
WidgetProvider.ACTION_WIDGET_UPDATE -> {
|
||||||
L.d("Received widget update event")
|
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_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_songs_sort" translatable="false">auxio_songs_sort</string>
|
||||||
<string name="set_key_albums_sort" translatable="false">auxio_albums_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>
|
<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_play_song_by_itself">Play song by itself</string>
|
||||||
<string name="set_keep_shuffle">Remember shuffle</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_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">Content</string>
|
||||||
<string name="set_content_desc">Control how music and images are loaded</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:summary="@string/set_keep_shuffle_desc"
|
||||||
app:title="@string/set_keep_shuffle" />
|
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>
|
</PreferenceCategory>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user