Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
A skip on another device no longer brings the sound back to the phone
kota-zo183abb8c9400
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.ktindex 15a53aa3..d7cfa6a3 100644--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt@@ -2202,9 +2202,12 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop// steps onto an ad that way stays on it (its audio refused). Going to the next song by name// gets past it within a second (measured 20/10, desktop), as at the end of the song.// Played as a skip within the queue now, not a new play (that reshuffles; see [skipPastAd]).+ // Another device plays: a play by name pulls the sound back to the phone (reported), only the+ // plain skip reaches that device, however long it takes to report the next song.+ val elsewhere = remoteActiveval due = app.myspoti.spotify.bridge.AdBlock.ahead.valueval ctx = _state.value?.contextUri- if (due != null && ctx != null && adDueFor(due, _state.value?.item)) {+ if (!elsewhere && due != null && ctx != null && adDueFor(due, _state.value?.item)) {app.myspoti.spotify.bridge.AdBlock.ahead.compareAndSet(due, null)bridge.logger?.invoke("next with an ad due: ${due.nextUri} skipped to instead of a plain skip")if (!skipPastAd(due)) playNow(ctx, trackUri = due.nextUri, order = lastOrder?.takeIf { it.first == ctx }?.second, origin = "next, an ad due")@@ -2213,8 +2216,8 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop// Remembered: should the page go into an ad break instead, this song comes after it.val base = lastWithSong()val after = base?.next?.takeIf { it.kind == ItemKind.TRACK || it.kind == ItemKind.EPISODE }- if (after != null && base.item?.kind != ItemKind.AD) intent = Intent(base.contextUri, after, base.item?.uri, System.currentTimeMillis())- val sent = intent+ if (!elsewhere && after != null && base.item?.kind != ItemKind.AD) intent = Intent(base.contextUri, after, base.item?.uri, System.currentTimeMillis())+ val sent = intent.takeUnless { elsewhere }runCatching { withFade { command("player.next") } }.onFailure { e ->if (!heldError(e, base?.restrictions?.canSkipNext != false)) throw estepIntent(1)@@ -2226,7 +2229,7 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScopif (sent != null) scope.launch {delay(NEXT_LOST_MS)val i = liveIntent()- if (i == null || i !== sent || skipsHeld || pausedByUser) return@launch+ if (i == null || i !== sent || skipsHeld || pausedByUser || remoteActive) return@launchval now = lastTrackState?.item?.uriif (now != null && now == i.from) {bridge.logger?.invoke("skip lost by the page: playing ${i.item.name}")diff --git a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/RemoteSkipTest.kt b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/RemoteSkipTest.ktnew file mode 100644index 00000000..2aa6f5ce--- /dev/null+++ b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/RemoteSkipTest.kt@@ -0,0 +1,83 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package app.myspoti.spotify++import app.myspoti.spotify.bridge.AdBlock+import app.myspoti.spotify.bridge.Bridge+import app.myspoti.spotify.bridge.BridgeHost+import kotlinx.coroutines.CoroutineScope+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.SupervisorJob+import kotlinx.coroutines.cancel+import kotlinx.coroutines.delay+import kotlinx.coroutines.flow.MutableSharedFlow+import kotlinx.coroutines.runBlocking+import org.junit.jupiter.api.AfterEach+import org.junit.jupiter.api.Assertions.assertFalse+import org.junit.jupiter.api.Assertions.assertTrue+import org.junit.jupiter.api.BeforeEach+import org.junit.jupiter.api.Test+import java.util.concurrent.CopyOnWriteArrayList++/** Reported: on another device, a skip moved the sound back to the phone. Only a plain skip reaches that device. */+class RemoteSkipTest {+ private class FakeHost(val active: Boolean) : BridgeHost {+ override val messages = MutableSharedFlow<String>(extraBufferCapacity = 64)+ val calls = CopyOnWriteArrayList<String>()+ override suspend fun evaluate(script: String) {+ val m = Regex("""Myspoti\.call\((\d+), "([^"]+)", (.*)\)\.catch""", RegexOption.DOT_MATCHES_ALL).find(script) ?: return+ calls += m.groupValues[2]+ val value = if (m.groupValues[2] == "media.state") """{"paused":${!active},"muted":false,"level":1,"own":1,"time":0,"active":$active}""" else "null"+ messages.emit("""{"type":"result","id":${m.groupValues[1]},"ok":true,"value":$value}""")+ }+ }++ private lateinit var scope: CoroutineScope++ @BeforeEach fun up() {+ AdBlock.ahead.value = null+ scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)+ }++ @AfterEach fun down() {+ AdBlock.ahead.value = null+ scope.cancel()+ }++ private fun track(name: String) = """{"kind":"track","uri":"spotify:track:$name","uid":"u$name","name":"$name","durationMs":200000}"""++ /** A skip the page answers without ever reporting the next song, long enough for the by-name play. */+ private fun skipUnanswered(active: Boolean): List<String> = runBlocking {+ val host = FakeHost(active)+ val player = SpotifyPlayer(Bridge(host, scope), scope)+ while (host.messages.subscriptionCount.value == 0) delay(10)+ host.messages.emit("""{"type":"ready"}""")+ host.messages.emit("""{"type":"state","state":{"track":${track("A")},"contextUri":"spotify:playlist:C","next":${track("B")},"isPaused":false,"isBuffering":false,+ |"repeat":0,"positionMs":0,"timestamp":${System.currentTimeMillis()},"restrictions":{}}}""".trimMargin())+ val end = System.currentTimeMillis() + 8_000+ while (player.remoteActive != !active || "media.state" !in host.calls) {+ check(System.currentTimeMillis() < end) { "the page's device was not read" }+ delay(50)+ }+ player.next()+ delay(SpotifyPlayer.NEXT_LOST_MS + 1_000)+ host.calls.toList()+ }++ @Test fun `a skip on another device never plays on this page`() {+ val calls = skipUnanswered(active = false)+ assertTrue("player.next" in calls, calls.toString())+ assertFalse("player.play" in calls, calls.toString())+ }++ @Test fun `an ad due does not bring a skip back from another device`() {+ AdBlock.ahead.value = AdBlock.AdAhead("uA", "spotify:track:A", "spotify:track:B")+ val calls = skipUnanswered(active = false)+ assertTrue("player.next" in calls, calls.toString())+ assertFalse("player.play" in calls || "queue.skipTo" in calls, calls.toString())+ }++ @Test fun `a skip the page lost here is still played by name`() {+ val calls = skipUnanswered(active = true)+ assertTrue("player.play" in calls, calls.toString())+ }+}