Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
A song you skip to while an ad is due after it no longer gets stuck on the ad, and the reloaded page keeps the playlist (#144)
kota-zo701f762172df
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/AdRecoveryRules.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/AdRecoveryRules.ktindex 37770234..7504d596 100644--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/AdRecoveryRules.kt+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/AdRecoveryRules.kt@@ -43,6 +43,22 @@ object AdRecoveryRules {*/fun keepsPickBeforeAd(mirrored: Boolean): Boolean = !mirrored+ /** A mirror play this close to the start is the local player's new song; further in, it is a position to seek to. */+ const val MIRROR_SKIP_MAX_MS = 1_500L++ /**+ * Whether a play the mirror sends is the step onto the song an ad is [due] before, the page+ * being on the song that ad follows ([playing]): the local player's Next. Spotify sends a skip+ * of that song, and a plain play of the next one, to the ad (reported, log seen: the ad stayed+ * current with its audio refused, the page was reloaded and lost its list, #144); only a skip+ * within the page's queue gets past it (see [SpotifyPlayer.next]).+ */+ fun mirrorSkipsPastAd(+ mirrored: Boolean, due: app.myspoti.spotify.bridge.AdBlock.AdAhead?, playing: app.myspoti.api.NowPlaying?,+ trackUri: Uri?, positionMs: Long?,+ ): Boolean =+ mirrored && due != null && trackUri == due.nextUri && SpotifyPlayer.adDueFor(due, playing) && (positionMs ?: 0L) <= MIRROR_SKIP_MAX_MS+/** What a page that is ready again plays. */enum class PageBack {/** Nothing (our own reload says itself what plays, or nothing played). */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 a725421d..ef50ada6 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@@ -1749,7 +1749,18 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop// The ad due after the song that played before: a new play brings its own queue, and the// put-back of an earlier ad skip would queue that old song after the pick.adRestore?.cancel()- app.myspoti.spotify.bridge.AdBlock.ahead.value = null+ // The mirror's play keeps it: it sends the same song again (a resend, the page back), after+ // which that ad is still due; its Next is the step onto the song after the ad (below).+ val due = app.myspoti.spotify.bridge.AdBlock.ahead.value+ if (origin != "mirror") app.myspoti.spotify.bridge.AdBlock.ahead.value = null+ // The mirror's Next with an ad due: a plain play of the next song makes Spotify play the ad,+ // which the host refuses and only a reload leaves (the list is lost with it, #144).+ if (due != null && !_adPlaying.value && _state.value?.contextUri == contextUri &&+ AdRecoveryRules.mirrorSkipsPastAd(origin == "mirror", due, _state.value?.item, trackUri, positionMs)) {+ app.myspoti.spotify.bridge.AdBlock.ahead.compareAndSet(due, null)+ bridge.logger?.invoke("mirror: ${due.nextUri} skipped to, an ad due after ${_state.value?.item?.name}")+ if (skipPastAd(due)) return+ }// Held whether or not the song comes with its place in the list (SongPlayer.rowIndex).if (_adPlaying.value) {if (trackUri == null || trackUri != liveIntent()?.item?.uri) intent = nulldiff --git a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/AdRecoveryRulesTest.kt b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/AdRecoveryRulesTest.ktindex ff124eba..e9870bb5 100644--- a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/AdRecoveryRulesTest.kt+++ b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/AdRecoveryRulesTest.kt@@ -1,7 +1,10 @@package app.myspoti.spotify+import app.myspoti.api.ItemKind+import app.myspoti.api.NowPlayingimport app.myspoti.spotify.AdRecoveryRules.PageBackimport app.myspoti.spotify.AdRecoveryRules.Stuck+import app.myspoti.spotify.bridge.AdBlockimport org.junit.jupiter.api.Assertions.assertEqualsimport org.junit.jupiter.api.Assertions.assertFalseimport org.junit.jupiter.api.Assertions.assertTrue@@ -41,6 +44,19 @@ class AdRecoveryRulesTest {assertEquals(PageBack.NOTHING, AdRecoveryRules.pageBack(ownReload = false, mirrored = false, pickHeld = false, lost = false, intent = false))}+ @Test fun theMirrorsNextWithAnAdDueSkipsPastIt() {+ val due = AdBlock.AdAhead("uid-a", "spotify:track:a", "spotify:track:b")+ val song = NowPlaying(ItemKind.TRACK, "spotify:track:a", "uid-a", "A", emptyList(), null, null, 200_000)+ // The local player's Next while the page is on the song the ad follows: a skip within the queue.+ assertTrue(AdRecoveryRules.mirrorSkipsPastAd(true, due, song, "spotify:track:b", 260))+ // Not another song, a position far in, no ad due, the page on another song, or not a mirror.+ assertFalse(AdRecoveryRules.mirrorSkipsPastAd(true, due, song, "spotify:track:c", 0))+ assertFalse(AdRecoveryRules.mirrorSkipsPastAd(true, due, song, "spotify:track:b", 60_000))+ assertFalse(AdRecoveryRules.mirrorSkipsPastAd(true, null, song, "spotify:track:b", 0))+ assertFalse(AdRecoveryRules.mirrorSkipsPastAd(true, due, song.copy(uid = "uid-x", uri = "spotify:track:x"), "spotify:track:b", 0))+ assertFalse(AdRecoveryRules.mirrorSkipsPastAd(false, due, song, "spotify:track:b", 0))+ }+@Test fun theUsersQueueIsAddedAgainAfterOurReload() {val q = listOf("t:a", "t:b", "t:c")// Played on from the queue's first song: the others come again, in their order.diff --git a/base/app/src/main/java/com/music/spotui/di/ConnectMirror.kt b/base/app/src/main/java/com/music/spotui/di/ConnectMirror.ktindex c4d388bf..878abdd4 100644--- a/base/app/src/main/java/com/music/spotui/di/ConnectMirror.kt+++ b/base/app/src/main/java/com/music/spotui/di/ConnectMirror.kt@@ -358,8 +358,9 @@ object ConnectMirror {log("the page is back (ad over or reloaded): ${w.uri} played there again")landed = false; retries = 0; resends = 0; lastWeb = null; gapFrom = nullrecoverUntil = SystemClock.elapsedRealtime() + RECOVER_MS- // The track alone: the list the page had went with the ad's or the reload's own plays.- pendingContext = w.uri; newSong = false+ // In the list the local queue came from, as at the start: the track alone would replace the+ // list on Spotify for every other device (#144).+ pendingContext = MirrorRules.playContext(LocalPlayer.contextUri, w.uri, missedContexts); newSong = falseecho.sent(PLAY_ECHO_MS)main.removeCallbacks(sendPlay); main.postDelayed(sendPlay, START_COALESCE_MS)}diff --git a/base/app/src/test/java/com/music/spotui/di/MirrorRulesTest.kt b/base/app/src/test/java/com/music/spotui/di/MirrorRulesTest.ktindex 16426105..c7f4b852 100644--- a/base/app/src/test/java/com/music/spotui/di/MirrorRulesTest.kt+++ b/base/app/src/test/java/com/music/spotui/di/MirrorRulesTest.kt@@ -264,7 +264,15 @@ class MirrorRulesTest {assertEquals(a, MirrorRules.playContext("", a, emptySet()))}- @Test fun `a play, a resume or a seek of the local player is a move, its clock running on is not`() {+ @Test fun `a page back after an ad or a reload keeps the list of the local queue (#144)`() {+ val playlist = "spotify:playlist:37i9dQZF1DXcBWIGoYBM5M"+ // The same context the start used: the song sent again is played in its list, not alone.+ assertEquals(playlist, MirrorRules.playContext(playlist, a, emptySet()))+ // A list that put the page on another song stays out of it, as before the reload.+ assertEquals(a, MirrorRules.playContext(playlist, a, setOf(playlist)))+ }++ @Test fun `a play, a resume or a seek of the local player is a move,its clock running on is not`() {assertTrue(MirrorRules.localMoved(wasPlaying = false, playing = true, expectedMs = 30_000, positionMs = 30_000))assertTrue(MirrorRules.localMoved(wasPlaying = true, playing = true, expectedMs = 30_000, positionMs = 90_000))assertFalse(MirrorRules.localMoved(wasPlaying = true, playing = true, expectedMs = 30_000, positionMs = 30_400))