mirror of
https://github.com/twinksoftware/Gearslip.git
synced 2026-11-10 16:02:25 +01:00
media: opening another media app keeps the player on the one still playing
Opening AntennaPod while Metrolist played switched Gearslip's player to AntennaPod, so the nav bar lost the music and its buttons drove the wrong app. Leaving an app where nothing started goes back to the one that kept playing.
This commit is contained in:
1 parent
b0ffcf2b54
commit
c888d41d51
2 files changed
+24
No files matched your search
@@ -60,6 +60,13 @@ object CarServices {
|
|||||||
private var navStopped = false
|
private var navStopped = false
|
||||||
private var autoplayPending = false
|
private var autoplayPending = false
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The app that was playing when the driver opened another one. Opening an app is only a look
|
||||||
|
* at its library: until something plays there, the music carries on in this one, so leaving
|
||||||
|
* the new app's screen goes back to it, as Android Auto does.
|
||||||
|
*/
|
||||||
|
private var stillPlaying: MediaApp? = null
|
||||||
|
|
||||||
fun init(appContext: Context) {
|
fun init(appContext: Context) {
|
||||||
context = appContext.applicationContext
|
context = appContext.applicationContext
|
||||||
app.seb3thehacker.gearslip.notify.CarMotion.start(appContext)
|
app.seb3thehacker.gearslip.notify.CarMotion.start(appContext)
|
||||||
@@ -130,6 +137,8 @@ object CarServices {
|
|||||||
val live = phase == CarMedia.Phase.READY || phase == CarMedia.Phase.CONNECTING
|
val live = phase == CarMedia.Phase.READY || phase == CarMedia.Phase.CONNECTING
|
||||||
if (_mediaApp.value?.component == app.component && live) return
|
if (_mediaApp.value?.component == app.component && live) return
|
||||||
|
|
||||||
|
val previous = _mediaApp.value
|
||||||
|
stillPlaying = previous?.takeIf { it.component != app.component && media.now.value.playing && !autoplay }
|
||||||
_mediaApp.value = app
|
_mediaApp.value = app
|
||||||
autoplayPending = autoplay
|
autoplayPending = autoplay
|
||||||
media.connect(app)
|
media.connect(app)
|
||||||
@@ -151,12 +160,26 @@ object CarServices {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The media screen closed. If nothing started in the app it showed, go back to the one that
|
||||||
|
* kept playing meanwhile, so the player and its buttons follow the music you can hear.
|
||||||
|
*/
|
||||||
|
fun mediaScreenClosed() {
|
||||||
|
val back = stillPlaying ?: return
|
||||||
|
stillPlaying = null
|
||||||
|
// A track loaded here, even one paused since, means the driver used this app.
|
||||||
|
if (media.now.value.playing || media.now.value.title.isNotEmpty()) return
|
||||||
|
app.seb3thehacker.gearslip.GearslipLog.i("media: nothing played in ${_mediaApp.value?.label}, back to ${back.label}")
|
||||||
|
openMedia(back)
|
||||||
|
}
|
||||||
|
|
||||||
/** Closes the media app from the car: stops what it's playing and drops the connection. */
|
/** Closes the media app from the car: stops what it's playing and drops the connection. */
|
||||||
fun closeMedia() {
|
fun closeMedia() {
|
||||||
if (media.now.value.playing) media.togglePlay()
|
if (media.now.value.playing) media.togglePlay()
|
||||||
media.disconnect()
|
media.disconnect()
|
||||||
_mediaApp.value = null
|
_mediaApp.value = null
|
||||||
autoplayPending = false
|
autoplayPending = false
|
||||||
|
stillPlaying = null
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Called once the media app is connected: starts playback if that was asked for. */
|
/** Called once the media app is connected: starts playback if that was asked for. */
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ fun MediaScreen(app: MediaApp, onExit: () -> Unit) {
|
|||||||
|
|
||||||
// The connection outlives this screen: the home screen's player is the same one.
|
// The connection outlives this screen: the home screen's player is the same one.
|
||||||
LaunchedEffect(app) { CarServices.openMedia(app) }
|
LaunchedEffect(app) { CarServices.openMedia(app) }
|
||||||
|
androidx.compose.runtime.DisposableEffect(Unit) { onDispose { CarServices.mediaScreenClosed() } }
|
||||||
|
|
||||||
val art = rememberArt(now)
|
val art = rememberArt(now)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user