Skip to content

Native Android client for Spotify, built with Jetpack Compose

A token check that fails or times out (offline, a bridge error) no longer signs the user out as if the token were anonymous: only an anonymous token or a sign-in page does, a failed check keeps the session and is logged, which ends the offline relogin loop

kota-zo46f5495f35e4
diff --git a/base/app/src/main/java/app/myspoti/MainActivity.kt b/base/app/src/main/java/app/myspoti/MainActivity.kt
index a4c5ef19..f4ea0f45 100644
--- a/base/app/src/main/java/app/myspoti/MainActivity.kt
+++ b/base/app/src/main/java/app/myspoti/MainActivity.kt
@@ -60,6 +60,21 @@ private const val SESSION_WAIT_MS = 6_000L
/** The most a returning page may take to get its bridge ready before the wait gives up anyway. */
private const val SESSION_LOAD_MAX_MS = 60_000L
+/** What one token check says of the session. */
+internal enum class SessionCheck { SIGNED_IN, SIGNED_OUT, UNKNOWN }
+
+/**
+ * Reads one [MyspotiRuntime.isSignedIn] answer. Only an answer signs out: an anonymous token, or a
+ * Spotify sign-in page open. A check that failed (a bridge error, a token.get left waiting its 20 s,
+ * a page offline) says nothing of the session. Taken for "anonymous", it signed the user out with
+ * no network and sent them to a sign-in page that could not load either: the reported relogin loop.
+ */
+internal fun sessionCheck(answer: Result<Boolean>): SessionCheck = answer.fold(
+ onSuccess = { if (it) SessionCheck.SIGNED_IN else SessionCheck.SIGNED_OUT },
+ // The activity going away is not a failed check: the collect must end there.
+ onFailure = { if (it is kotlinx.coroutines.CancellationException) throw it else SessionCheck.UNKNOWN },
+)
+
/**
* The app's only screen host: the Compose UI, with the Web Player page attached behind it.
* The page comes to the front only to sign in, on Spotify's own login pages.
@@ -485,20 +500,23 @@ class MainActivity : AppCompatActivity() {
showLogin()
}
ready && !login -> {
- val user = runCatching { MyspotiRuntime.isSignedIn() }.getOrDefault(false)
- if (user) {
- sessionConfirmed()
- } else if (signedIn) {
- // The pref said "signed in", the token says anonymous: only the token
- // counts. This is what left the app on its full UI (or on Spotify's
- // anonymous page) with nothing playable — reported after leaving the app
- // to read the sign-in e-mail, where the session never completed.
- Logbook.log("anonymous token while the app thought it was signed in: back to the sign-in page")
- prefs.edit().putBoolean("LoggedIn", false).apply()
- signedIn = false
- connecting = false
- com.music.spotui.di.SessionActions.sessionLost()
- showLogin()
+ val answer = runCatching { MyspotiRuntime.isSignedIn() }
+ when (sessionCheck(answer)) {
+ SessionCheck.SIGNED_IN -> sessionConfirmed()
+ // The session is kept as it is: the next page load asks again.
+ SessionCheck.UNKNOWN -> Logbook.log("session check failed (${answer.exceptionOrNull()?.message}): session kept")
+ SessionCheck.SIGNED_OUT -> if (signedIn) {
+ // The pref said "signed in", the token says anonymous: only the token
+ // counts. This is what left the app on its full UI (or on Spotify's
+ // anonymous page) with nothing playable — reported after leaving the app
+ // to read the sign-in e-mail, where the session never completed.
+ Logbook.log("anonymous token while the app thought it was signed in: back to the sign-in page")
+ prefs.edit().putBoolean("LoggedIn", false).apply()
+ signedIn = false
+ connecting = false
+ com.music.spotui.di.SessionActions.sessionLost()
+ showLogin()
+ }
}
}
}
diff --git a/base/app/src/test/java/app/myspoti/SessionCheckTest.kt b/base/app/src/test/java/app/myspoti/SessionCheckTest.kt
new file mode 100644
index 00000000..1f721e64
--- /dev/null
+++ b/base/app/src/test/java/app/myspoti/SessionCheckTest.kt
@@ -0,0 +1,26 @@
+package app.myspoti
+
+import app.myspoti.spotify.bridge.BridgeException
+import org.junit.Assert.*
+import org.junit.Test
+
+class SessionCheckTest {
+ @Test fun aSignedInTokenConfirmsTheSession() {
+ assertEquals(SessionCheck.SIGNED_IN, sessionCheck(Result.success(true)))
+ }
+ @Test fun anAnonymousTokenOrASignInPageSignsOut() {
+ // MyspotiRuntime.isSignedIn answers false for both.
+ assertEquals(SessionCheck.SIGNED_OUT, sessionCheck(Result.success(false)))
+ }
+ @Test fun aFailedCheckKeepsTheSession() {
+ // Offline, token.get fails or times out: that is no answer, not an anonymous token (issue #137).
+ assertEquals(SessionCheck.UNKNOWN, sessionCheck(Result.failure(BridgeException("timeout after 20000 ms: token.get"))))
+ assertEquals(SessionCheck.UNKNOWN, sessionCheck(Result.failure(BridgeException("token.get: no accessToken"))))
+ assertEquals(SessionCheck.UNKNOWN, sessionCheck(Result.failure(IllegalStateException("page gone"))))
+ }
+ @Test fun cancellationIsNotACheck() {
+ assertThrows(kotlinx.coroutines.CancellationException::class.java) {
+ sessionCheck(Result.failure(kotlinx.coroutines.CancellationException("activity destroyed")))
+ }
+ }
+}