Skip to content

Native Android client for Spotify, built with Jetpack Compose

A sign-in that never counts an old token, no reload storm on a page Android froze, no blink on the player's opening

kota-zo7a6b26a80431
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.kt
index 261423ee..68b8d205 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
@@ -326,6 +326,11 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
// at once, the song going on where it was.
val read = runCatching { readMedia() }
if (read.isFailure && bridge.ready.value && !bridge.ping() && bridge.ready.value) {
+ // Paused: a page that does not answer is most often one Android froze with the
+ // app in the background (seen: "freezing <renderer pid>" in the system log, then
+ // a reload every minute that could not help). Nothing is lost while paused: the
+ // page is only marked, and the next resume loads it again at once ([resume]).
+ if (quiet) { pageSuspect = true; frozenMisses = 0; key = null; silentSince = 0L; continue }
// One silent page is not a dead one: a busy main thread (a long render, a reload
// still settling, an ad break starting) also holds the bridge for seconds, and
// reloading on the first miss turned a short hiccup into a full page reload — the
@@ -345,6 +350,7 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
}
// The page answered: it is alive, whatever happened before.
frozenMisses = 0
+ pageSuspect = false
val media = read.getOrNull()
lastMedia = media
val s = _state.value
@@ -1030,8 +1036,22 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
* [fadeBackTo]/[fadeBack] mark it as a fade meanwhile, so the silence watchdog and the volume
* guard leave it be.
*/
+ /** The page stopped answering while paused (see the watcher): checked before the next resume. */
+ @Volatile private var pageSuspect = false
+
override suspend fun resume() {
pausedByUser = false
+ // A page that went silent while paused (frozen in the background): a quick ping, and if it
+ // still says nothing, the page is loaded again and plays on where it was, instead of the
+ // resume waiting out its 20 s timeout.
+ if (pageSuspect && !bridge.ping(RESUME_PING_MS)) {
+ pageSuspect = false
+ val s = lastTrackState
+ bridge.logger?.invoke("resume: the page stopped answering while paused: loading it again")
+ reloadAndPlayOn(s?.copy(isPaused = false, positionMs = s.positionMs, timestampMs = System.currentTimeMillis()), next = false)
+ return
+ }
+ pageSuspect = false
val quiet = _adPlaying.value || adMuted || crossfadeArmed || _state.value?.isPaused == false || lastMedia?.active == false
if (quiet) { command("player.resume"); return }
fadeBackTo = lastMedia?.own?.takeIf { it > 0.01 } ?: 1.0
@@ -1279,6 +1299,8 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
const val PICK_AD_MS = 8_000L
/** After this the user has moved on: a pick is no longer replayed after its ad. */
const val PICK_REPLAY_MS = 120_000L
+ /** How long a resume waits for a page that went silent while paused before loading it again. */
+ const val RESUME_PING_MS = 1_500L
/** A resumed song rises from silence to its level in this long, along the ear's curve. */
const val RISE_MS = 500L
/** How long the rise waits for sound before the level simply comes back. */
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/gql/Gql.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/gql/Gql.kt
index b3dbaef8..7f641742 100644
--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/gql/Gql.kt
+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/gql/Gql.kt
@@ -94,7 +94,12 @@ class SpotifyHttp(
suspend fun forgetWebToken() = lock.withLock { web = null }
/** True when the page's token belongs to a signed-in user (the cached one is reused). */
- suspend fun isSignedIn(): Boolean = lock.withLock { !webToken().anonymous }
+ /**
+ * Asks the page, never the cached token: a token cached before a logout (valid up to an hour)
+ * said "signed in" while the user was still on the e-mail code step, the sign-in page was put
+ * away under them and the code was lost (log seen). A fresh answer also refreshes the cache.
+ */
+ suspend fun isSignedIn(): Boolean = lock.withLock { web = null; !webToken().anonymous }
/** The account's token, fetched again only when the cached one is missing or stale. */
private suspend fun webToken(): WebToken {
diff --git a/base/app/src/main/java/com/music/spotui/ui/navigation/PlayerSheet.kt b/base/app/src/main/java/com/music/spotui/ui/navigation/PlayerSheet.kt
index f4d41711..4a2386d5 100644
--- a/base/app/src/main/java/com/music/spotui/ui/navigation/PlayerSheet.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/navigation/PlayerSheet.kt
@@ -4,7 +4,6 @@ import androidx.activity.compose.BackHandler
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.CubicBezierEasing
import androidx.compose.animation.core.tween
-import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
@@ -47,10 +46,11 @@ val LocalPlayerReveal = androidx.compose.runtime.staticCompositionLocalOf<() ->
/*
* Opening, as the official app: the player grows out of the mini player. Its background rises
- * from the mini player's top edge and fades in over the first part of the way while the page
- * behind dims, then the cover, titles and controls fade in and settle a few dp up. Closing plays
- * it backwards: the content fades first, then the sheet sinks into the mini player and fades
- * into it as the page lights up again.
+ * from the mini player's top edge and fades in over the first part of the way, then the cover,
+ * titles and controls fade in and settle a few dp up. Closing plays it backwards: the content
+ * fades first, then the sheet sinks into the mini player and fades into it. No dimming of the
+ * page behind: darker than the player's own background, it made the screen blink dark then light
+ * on the first and last frames (measured on a recording, reported).
*
* It replaces a cover flown between the two players (a copy that ran out of step with the real
* one: a ghost square during the slide, a lone cover crossing the page on the way down) and a
@@ -61,8 +61,6 @@ private const val OPEN_MS = 380
private const val CLOSE_MS = 300
/** The sheet's background is fully there by this share of the way. */
private const val SHEET_FADE = 0.3f
-/** How dark the page behind gets under the open player. */
-private const val SCRIM = 0.6f
private val CloseEasing = CubicBezierEasing(0.4f, 0f, 0.2f, 1f)
@Composable
@@ -92,13 +90,6 @@ fun PlayerOverlay(navController: NavController) {
if (!shown) return
BackHandler(enabled = open) { PlayerSheet.close() }
var heightPx by remember { mutableIntStateOf(0) }
- // The page behind dims as the player rises and comes back as it goes: the player is seen to
- // come over the page instead of being pasted on it. Only while it moves (covered once open).
- if (!settled.value) Box(
- Modifier.fillMaxSize()
- .graphicsLayer { alpha = progress.value * SCRIM }
- .background(androidx.compose.ui.graphics.Color.Black),
- )
val reveal = remember { { ((progress.value - 0.25f) / 0.6f).coerceIn(0f, 1f) } }
Box(
Modifier.fillMaxSize().onSizeChanged { heightPx = it.height }.graphicsLayer {
diff --git a/base/host-android/src/main/kotlin/app/myspoti/host/android/MyspotiRuntime.kt b/base/host-android/src/main/kotlin/app/myspoti/host/android/MyspotiRuntime.kt
index ed8875da..ca8af779 100644
--- a/base/host-android/src/main/kotlin/app/myspoti/host/android/MyspotiRuntime.kt
+++ b/base/host-android/src/main/kotlin/app/myspoti/host/android/MyspotiRuntime.kt
@@ -105,7 +105,8 @@ object MyspotiRuntime {
}
/** True when the Web Player holds a signed-in user's token (not an anonymous one). */
- suspend fun isSignedIn(): Boolean = spotify.isSignedIn()
+ /** Never while a Spotify sign-in page is open: whatever a token says, the user is mid sign-in. */
+ suspend fun isSignedIn(): Boolean = !host.loginRequired.value && spotify.isSignedIn()
/**
* Waits for a signed-in session (a non-anonymous token) up to [timeoutMs]. The login flow is
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 3902ef58..ecd182c4 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
@@ -294,7 +294,9 @@ class WebPlayerHost(private val context: Context) : BridgeHost {
}
private fun isLoginPage(url: String?) =
- url != null && (url.contains("accounts.spotify.com") || url.contains("/login") || url.contains("challenge.spotify.com"))
+ url != null && (url.contains("accounts.spotify.com") || url.contains("/login") || url.contains("challenge.spotify.com") ||
+ // "Continue with Google / Facebook / Apple": still mid sign-in on their pages.
+ url.contains("accounts.google.com") || url.contains("facebook.com") || url.contains("appleid.apple.com"))
fun load() = main.post {
pageStarts = 0