Skip to content

Native Android client for Spotify, built with Jetpack Compose

Mirror: songs no longer switch at random after a refused licence, and the unheard page's ads are not shown

kota-zo2fc923089d42
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.kt
index d392711f..90a05770 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
@@ -33,6 +33,10 @@ import kotlinx.coroutines.sync.withLock
* Everything runs on the main thread, on the two players' own events (and one slow drift check).
*/
object ConnectMirror {
+ /** How long after a refused licence the page's own song changes are not followed. */
+ private const val LICENCE_SKIP_MS = 10_000L
+ private var ignoredLicenceSkips = 0
+
private const val SPOTIFY_TRACK_PREFIX = "spotify:track:"
/** A play is answered within that (the page loads the song, then reports it). */
private const val PLAY_ECHO_MS = 6_000L
@@ -264,7 +268,15 @@ object ConnectMirror {
// Recovering from an ad or a reload: a song other than the asked one is the player's own play, sent back.
val sure = landed && SystemClock.elapsedRealtime() >= recoverUntil
val repeats = LocalPlayer.repeatsCurrent()
- when (val f = MirrorRules.onWeb(web, w, local, quiet, sure, predicted, previousUri, gap, wantAt, wantLength, recentUris(), repeats)) {
+ val f = MirrorRules.onWeb(web, w, local, quiet, sure, predicted, previousUri, gap, wantAt, wantLength, recentUris(), repeats)
+ // A song refused its licence makes the page skip on by itself, a song a second or so: taken for
+ // picks on another device, the local player followed and its songs switched at random (reported,
+ // free account, 403 ×42). Those moves stay the page's; the local song plays on.
+ if (MirrorRules.movesSong(f) && MyspotiRuntime.host.licenceRefusedWithin(LICENCE_SKIP_MS)) {
+ if (ignoredLicenceSkips++ % 10 == 0) log("the page went to ${web.uri} after a refused licence: not followed")
+ return
+ }
+ when (f) {
Follow.Nothing -> {}
// A play waits already (coalescing, backing off): not another one.
Follow.Retry -> if (pendingContext != null) Unit else if (retries++ < MAX_RETRIES) {
diff --git a/base/app/src/main/java/com/music/spotui/di/MirrorRules.kt b/base/app/src/main/java/com/music/spotui/di/MirrorRules.kt
index cd5a1019..966fb680 100644
--- a/base/app/src/main/java/com/music/spotui/di/MirrorRules.kt
+++ b/base/app/src/main/java/com/music/spotui/di/MirrorRules.kt
@@ -82,6 +82,9 @@ object MirrorRules {
* [repeats]: the local song plays again at its end (repeat once still to replay it, or a queue
* of one song repeated): the Web Player going on at the end is never followed, it is put back.
*/
+ /** Whether [f] takes the local player to another song (a pick, a next or a previous seen on the page). */
+ fun movesSong(f: Follow): Boolean = f is Follow.Play || f == Follow.Next || f == Follow.Previous
+
fun onWeb(
web: Web, want: Want, local: Local, quiet: Boolean, landed: Boolean, predictedMs: Long?,
previousUri: String? = null, gapFromMs: Long? = null, wantAtMs: Long? = null, wantLengthMs: Long = 0,
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.kt
index b81438ae..3211c1d5 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
@@ -321,6 +321,9 @@ object SongPlayer {
// The same when it refuses the account its licences (#134): the ad it is stuck on
// stays out of the screens, the toast said why nothing plays.
while (MyspotiRuntime.host.licenseLimited() || MyspotiRuntime.spotify.player.licencesHeld) kotlinx.coroutines.delay(1_000)
+ // The local player plays (an extension's or a downloaded song): the ad is on the unheard
+ // page that mirrors it, not what is heard. Shown, the song on screen became the ad (reported).
+ if (LocalPlayer.active.value) return@collectLatest
}
onAdPlaying(on)
}
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.kt
index c7f4b852..bbd30749 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
@@ -307,4 +307,12 @@ class MirrorRulesTest {
assertTrue(MirrorRules.followsRemote(activeElsewhere = false))
assertFalse(MirrorRules.followsRemote(activeElsewhere = true))
}
+
+ @Test fun onlyPicksNextsAndPreviousesMoveTheLocalSong() {
+ assertTrue(MirrorRules.movesSong(MirrorRules.Follow.Play("spotify:track:x", 0)))
+ assertTrue(MirrorRules.movesSong(MirrorRules.Follow.Next))
+ assertTrue(MirrorRules.movesSong(MirrorRules.Follow.Previous))
+ for (f in listOf(MirrorRules.Follow.Pause, MirrorRules.Follow.Resume, MirrorRules.Follow.Nothing, MirrorRules.Follow.Back, MirrorRules.Follow.Retry))
+ assertTrue(!MirrorRules.movesSong(f))
+ }
}
diff --git a/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt b/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt
index 1d33ef89..490b210b 100644
--- a/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt
+++ b/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt
@@ -70,6 +70,15 @@ class WebPlayerHost(private val context: Context) : BridgeHost {
val licenseLimitedUntil: StateFlow<Long> = _licenseLimitedUntil
fun licenseLimited(): Boolean = android.os.SystemClock.elapsedRealtime() < _licenseLimitedUntil.value
+ /**
+ * When Spotify last refused this page an audio licence (403 and the like, not a 429 limit). The
+ * page then skips the song by itself, and on to the next one, a second or so each: those moves are
+ * not the user's (see ConnectMirror).
+ */
+ @Volatile private var licenceRefusedAt = 0L
+ fun licenceRefusedWithin(ms: Long): Boolean =
+ licenceRefusedAt != 0L && android.os.SystemClock.elapsedRealtime() - licenceRefusedAt < ms
+
private fun onLicenseLimited() = synchronized(license429) {
val now = android.os.SystemClock.elapsedRealtime()
license429.addLast(now)
@@ -395,6 +404,7 @@ class WebPlayerHost(private val context: Context) : BridgeHost {
loginFailed("http ${response.statusCode}")
if (request.url.path?.contains("widevine-license") == true) {
if (response.statusCode == 429) onLicenseLimited()
+ else if (response.statusCode in 400..499) licenceRefusedAt = android.os.SystemClock.elapsedRealtime()
response.responseHeaders.entries.firstOrNull { it.key.equals("retry-after", true) }
?.let { app.myspoti.spotify.report.Refusal402.licenceRetryAfter(it.value) }
// A refused licence (403 seen on a new sign-in, 429 on others) says why in its