Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
A phone call pauses the music and it plays again after
kota-zo18c92e0a58fe
diff --git a/base/app/src/main/java/com/music/spotui/di/CallPause.kt b/base/app/src/main/java/com/music/spotui/di/CallPause.ktnew file mode 100644index 00000000..b4ec314d--- /dev/null+++ b/base/app/src/main/java/com/music/spotui/di/CallPause.kt@@ -0,0 +1,25 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package com.music.spotui.di++/**+ * Music through a call. The web player's audio holds no audio focus, so a call only muted it: the+ * song went on unheard (reported). It pauses when the call comes and plays again after, unless the+ * user touched the player meanwhile or the call lasted so long that music coming back would surprise.+ */+internal class CallPause(private val longestMs: Long = 30 * 60_000L) {+ enum class Action { PAUSE, RESUME, NONE }++ private var heldAt: Long? = null++ fun modeChanged(busy: Boolean, playing: Boolean, nowMs: Long): Action {+ val held = heldAt+ return when {+ busy && held == null && playing -> { heldAt = nowMs; Action.PAUSE }+ busy -> Action.NONE+ held == null -> Action.NONE+ else -> { heldAt = null; if (nowMs - held <= longestMs) Action.RESUME else Action.NONE }+ }+ }++ fun userActed() { heldAt = null }+}diff --git a/base/app/src/main/java/com/music/spotui/di/LocalPlayer.kt b/base/app/src/main/java/com/music/spotui/di/LocalPlayer.ktindex 1554038c..92dbdf83 100644--- a/base/app/src/main/java/com/music/spotui/di/LocalPlayer.kt+++ b/base/app/src/main/java/com/music/spotui/di/LocalPlayer.kt@@ -55,6 +55,8 @@ object LocalPlayer {* pause it again when it ends.*/@Volatile private var mirrorActive = false+ /** This player answers calls by itself through the audio focus. */+ val holdsFocus: Boolean get() = active.value && !mirrorActivefun setMirrorFocus(on: Boolean) {mirrorActive = onandroid.os.Handler(android.os.Looper.getMainLooper()).post { exo?.setAudioAttributes(audioAttributes, !on) }diff --git a/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt b/base/app/src/main/java/com/music/spotui/di/SongPlayer.ktindex d0cf4d46..d713ae36 100644--- a/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt+++ b/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt@@ -244,6 +244,7 @@ object SongPlayer {/** Wires the API's flows into the UI's now-playing state. Call once, on the main thread. */fun attachEngine(state: CurrentSongState) {boundState = state+ app.myspoti.host.android.PhoneCall.listener = { busy -> main.post { phoneCall(busy) } }com.music.spotui.data.api.LyricsApi.extraSources =com.music.spotui.data.preferences.isLyricsExtraSources(Graph.app)com.music.spotui.data.api.LyricsApi.extraSourcesOff =@@ -1712,6 +1713,7 @@ object SongPlayer {/** [by]: who asked, for the diagnostic (the app's screens, the media session, an extension...). */fun play(by: String = "user") {app.myspoti.host.android.Logbook.log("play asked by $by")+ if (by != BY_CALL) callPause.userActed()userActed()if (routing) return // the song asked for starts by itselfif (LocalPlayer.active.value) { Haptics.click(); if (!playDueOnSpotify()) LocalPlayer.resume(); return }@@ -1748,7 +1750,21 @@ object SongPlayer {}}+ private const val BY_CALL = "phone call"+ private val callPause = CallPause()++ /** The phone rings or a call ends, on the main thread. ExoPlayer deals with its own audio focus. */+ private fun phoneCall(busy: Boolean) {+ if (LocalPlayer.holdsFocus || MyspotiRuntime.spotify.player.playsElsewhere.value) return+ when (callPause.modeChanged(busy, isPlaying(), SystemClock.elapsedRealtime())) {+ CallPause.Action.PAUSE -> pause(by = BY_CALL)+ CallPause.Action.RESUME -> play(by = BY_CALL)+ CallPause.Action.NONE -> Unit+ }+ }+fun pause(by: String = "user") {+ if (by != BY_CALL) callPause.userActed()app.myspoti.host.android.Logbook.log("pause asked by $by")userActed()Haptics.click(); cancelBurst(); pauseSilently()diff --git a/base/app/src/test/java/com/music/spotui/di/CallPauseTest.kt b/base/app/src/test/java/com/music/spotui/di/CallPauseTest.ktnew file mode 100644index 00000000..e9c24b4e--- /dev/null+++ b/base/app/src/test/java/com/music/spotui/di/CallPauseTest.kt@@ -0,0 +1,36 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package com.music.spotui.di++import com.music.spotui.di.CallPause.Action+import org.junit.Assert.assertEquals+import org.junit.Test++class CallPauseTest {+ @Test fun musicPausesForTheCallAndComesBackAfter() {+ val c = CallPause()+ assertEquals(Action.PAUSE, c.modeChanged(busy = true, playing = true, nowMs = 0))+ // Ringing, then in the call: one pause.+ assertEquals(Action.NONE, c.modeChanged(busy = true, playing = false, nowMs = 5_000))+ assertEquals(Action.RESUME, c.modeChanged(busy = false, playing = false, nowMs = 60_000))+ assertEquals(Action.NONE, c.modeChanged(busy = false, playing = true, nowMs = 61_000))+ }++ @Test fun whatWasPausedStaysPaused() {+ val c = CallPause()+ assertEquals(Action.NONE, c.modeChanged(busy = true, playing = false, nowMs = 0))+ assertEquals(Action.NONE, c.modeChanged(busy = false, playing = false, nowMs = 60_000))+ }++ @Test fun aTouchDuringTheCallIsTheLastWord() {+ val c = CallPause()+ c.modeChanged(busy = true, playing = true, nowMs = 0)+ c.userActed()+ assertEquals(Action.NONE, c.modeChanged(busy = false, playing = false, nowMs = 60_000))+ }++ @Test fun aVeryLongCallDoesNotBringTheMusicBack() {+ val c = CallPause(longestMs = 1_000)+ c.modeChanged(busy = true, playing = true, nowMs = 0)+ assertEquals(Action.NONE, c.modeChanged(busy = false, playing = false, nowMs = 2_000))+ }+}diff --git a/base/host-android/src/main/kotlin/app/myspoti/host/android/RelayNetwork.kt b/base/host-android/src/main/kotlin/app/myspoti/host/android/RelayNetwork.ktindex a9666ada..d1159a89 100644--- a/base/host-android/src/main/kotlin/app/myspoti/host/android/RelayNetwork.kt+++ b/base/host-android/src/main/kotlin/app/myspoti/host/android/RelayNetwork.kt@@ -85,6 +85,11 @@ internal object NetworkReport {}}+/** Told when the phone starts or stops ringing or being in a call (Android 12+), on a thread of its own. */+object PhoneCall {+ @Volatile var listener: ((busy: Boolean) -> Unit)? = null+}+/*** Calls in the timeline: the phone's audio mode (ringing, in a call, back to normal), the moments* a report of "the music did not come back after a call" turns on. Android 12+.@@ -107,6 +112,7 @@ internal object AudioModeWatch {else -> "mode $mode"}Logbook.log("audio mode: $name")+ PhoneCall.listener?.invoke(mode != android.media.AudioManager.MODE_NORMAL)}}}