Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
A page that moves on by itself through songs right after a play is stopped: a command explains only one change of song (its first, or the one onto the song it asked for), so the skips that follow count for the skip chain, which pauses after three; never while the page only mirrors a song of an extension; each track change line names the list it moves in [skip ci]
kota-zo8997eb214e19
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 1b6e1233..cf948502 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@@ -993,6 +993,7 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop}private val skipChain = TrackChanges.Chain()+ private val commandsExplained = TrackChanges.Commands()private val _skipChained = kotlinx.coroutines.flow.MutableSharedFlow<Unit>(extraBufferCapacity = 1)/*** The page moved on by itself through several songs after a few seconds each (it cannot play@@ -1011,10 +1012,17 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScopif (from.uri == to.uri || from.kind == ItemKind.AD || remoteActive) returnval now = System.currentTimeMillis()val played = old.positionAt(now)- val sinceCommand = now - maxOf(userMoveAt, lastPlay?.third ?: 0L)- val cause = TrackChanges.cause(played, from.durationMs, sinceCommand, adJustEnded, crossfadeArmed)- bridge.logger?.invoke("track change: ${from.name} -> ${to.name} (${cause.text}, left at ${TrackChanges.clock(played)} of ${TrackChanges.clock(from.durationMs)}${if (playbackHeld()) ", playback held" else ""})")- if (playbackHeld()) { skipChain.reset(); return }+ val play = lastPlay+ val commandAt = maxOf(userMoveAt, play?.third ?: 0L)+ // The song a play asked for; a skip (newer than the last play) names none.+ val target = play?.takeIf { it.third >= userMoveAt }?.second+ val used = commandsExplained.used(commandAt, to.uri, target)+ val cause = TrackChanges.cause(played, from.durationMs, now - commandAt, adJustEnded, crossfadeArmed, commandUsed = used)+ val list = if (old.contextUri == s.contextUri) "in ${s.contextUri}" else "${old.contextUri} -> ${s.contextUri}"+ bridge.logger?.invoke("track change: ${from.name} -> ${to.name} (${cause.text}, left at ${TrackChanges.clock(played)} of ${TrackChanges.clock(from.durationMs)}, $list${if (playbackHeld()) ", playback held" else ""}${if (mirroring) ", mirroring" else ""})")+ // The page only mirrors a song of an extension (heard from the phone's own player): pausing+ // it would not stop what the user hears, and the mirror sends its song again.+ if (playbackHeld() || mirroring) { skipChain.reset(); return }if (skipChain.record(cause, played, now)) {bridge.logger?.invoke("skip chain: the page moved on by itself three times in a row after a few seconds each (now on ${to.name}): paused")_skipChained.tryEmit(Unit)diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/TrackChanges.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/TrackChanges.ktindex f838425f..b5730fa7 100644--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/TrackChanges.kt+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/TrackChanges.kt@@ -18,13 +18,34 @@ internal object TrackChanges {OWN("the page moved on by itself"),}- fun cause(playedMs: Long, durationMs: Long, sinceCommandMs: Long, afterAd: Boolean, crossfading: Boolean): Cause = when {- sinceCommandMs in 0..COMMAND_MS -> Cause.COMMAND+ /** [commandUsed]: the last command already explained an earlier change (see [Commands]). */+ fun cause(playedMs: Long, durationMs: Long, sinceCommandMs: Long, afterAd: Boolean, crossfading: Boolean, commandUsed: Boolean = false): Cause = when {+ !commandUsed && sinceCommandMs in 0..COMMAND_MS -> Cause.COMMANDafterAd -> Cause.ADcrossfading || (durationMs > 0 && durationMs - playedMs <= ENDED_TAIL_MS) -> Cause.ENDEDelse -> Cause.OWN}+ /**+ * A command explains one change of song, not every change in the [COMMAND_MS] after it: the+ * first one, or the one onto the song it asked for. Reported (2.2.0, logs of 30/10): a page+ * refused every licence and moved on about every second after a play, four or five songs in a+ * row "after a command", and the [Chain] never counted one of them.+ */+ class Commands {+ private var usedAt = Long.MIN_VALUE++ /**+ * A change onto [to] after the command sent at [commandAt] ([target]: the song a play asked+ * for, null for a skip): true when that command already explained an earlier change.+ */+ fun used(commandAt: Long, to: String, target: String?): Boolean {+ val used = usedAt == commandAt && to != target+ usedAt = commandAt+ return used+ }+ }+fun clock(ms: Long): String = "%d:%02d".format(ms / 60_000, ms / 1000 % 60)/**diff --git a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/TrackChangesTest.kt b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/TrackChangesTest.ktindex bcd10a97..6a42d7cc 100644--- a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/TrackChangesTest.kt+++ b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/TrackChangesTest.kt@@ -51,5 +51,36 @@ class TrackChangesTest {assertFalse(c.record(Cause.OWN, 400, 40_000))}+ @Test fun `a command already used does not explain another change`() =+ assertEquals(Cause.OWN, TrackChanges.cause(400, 200_000, 1_500, afterAd = false, crossfading = false, commandUsed = true))++ @Test fun `a command explains only its first change`() {+ val c = TrackChanges.Commands()+ assertFalse(c.used(1_000, "a", target = null))+ assertTrue(c.used(1_000, "b", target = null))+ assertTrue(c.used(1_000, "c", target = null))+ // A new command explains its own first change.+ assertFalse(c.used(2_000, "d", target = null))+ }++ @Test fun `a change onto the song played is the command's`() {+ val c = TrackChanges.Commands()+ assertFalse(c.used(1_000, "x", target = "t"))+ assertFalse(c.used(1_000, "t", target = "t"))+ assertTrue(c.used(1_000, "y", target = "t"))+ }++ // The 30/10 log (kekstar63): a play, then the page moving on every second, licences refused.+ @Test fun `a page skipping after a play is a chain`() {+ val commands = TrackChanges.Commands()+ val chain = TrackChanges.Chain()+ val at = 28_156L+ val fired = listOf("t" to 29_456L, "a" to 30_283L, "b" to 31_359L, "c" to 32_014L).map { (to, now) ->+ val cause = TrackChanges.cause(0, 200_000, now - at, false, false, commands.used(at, to, target = "t"))+ chain.record(cause, 0, now)+ }+ assertEquals(listOf(false, false, false, true), fired)+ }+@Test fun `clock`() = assertEquals("3:21", TrackChanges.clock(201_500))}