Skip to content

Native Android client for Spotify, built with Jetpack Compose

Logout: clear history, download sessions and Web storage; one logout path

liliapk6b51d8b71793
diff --git a/base/app/src/main/java/com/music/spotui/data/api/ScreenCache.kt b/base/app/src/main/java/com/music/spotui/data/api/ScreenCache.kt
index 4a360177..d00594d4 100644
--- a/base/app/src/main/java/com/music/spotui/data/api/ScreenCache.kt
+++ b/base/app/src/main/java/com/music/spotui/data/api/ScreenCache.kt
@@ -25,7 +25,9 @@ object ScreenCache {
// Bumped when a cached model changes shape.
private const val DIR = "screen_cache1"
private val json = Json { ignoreUnknownKeys = true; encodeDefaults = false }
- private val io = CoroutineScope(SupervisorJob() + Dispatchers.IO)
+ // One thread: writes and clear() run in call order, so a write already in
+ // flight at logout cannot land after the clear and restore the old account.
+ private val io = CoroutineScope(SupervisorJob() + Dispatchers.IO.limitedParallelism(1))
private val dir: File by lazy { File(Graph.app.filesDir, DIR).also { it.mkdirs() } }
val home = Entry("home", HomeFeedModel.serializer())
diff --git a/base/app/src/main/java/com/music/spotui/di/SessionActions.kt b/base/app/src/main/java/com/music/spotui/di/SessionActions.kt
index 5cc54bbd..dcc5b175 100644
--- a/base/app/src/main/java/com/music/spotui/di/SessionActions.kt
+++ b/base/app/src/main/java/com/music/spotui/di/SessionActions.kt
@@ -10,8 +10,26 @@ object SessionActions {
/** True from a logout until the next sign-in: the activity shows the login page, then starts over. */
val signedOut: StateFlow<Boolean> = _signedOut
- /** Signs out of Spotify in the Web Player; the login page shows next. */
- fun logout() {
+ /**
+ * Signs out and drops everything tied to the account, so the next account on
+ * this phone starts clean: prefs and in-memory caches, the saved screens,
+ * the listen history (the Recap), the SpotiFLAC download sessions, and the
+ * Web Player's cookies, Web storage and cache (in the host).
+ */
+ fun logout(context: android.content.Context) {
+ val app = context.applicationContext
+ app.getSharedPreferences("myspoti_prefs", android.content.Context.MODE_PRIVATE)
+ .edit().putBoolean("LoggedIn", false).remove("CurrentAccountName").apply()
+ com.music.spotui.data.api.Api.HomeCache.clear()
+ com.music.spotui.data.api.Api.LikedCache.tracks = null
+ com.music.spotui.data.api.ProfileCache.clearSession()
+ SongPlayer.clearSession()
+ listOf("PlaybackState", "LibraryCache", "RecentItems", "LikedSongs",
+ "LikedSongsFromSpotify", "LikedAlbums", "FollowedArtists").forEach { name ->
+ app.getSharedPreferences(name, android.content.Context.MODE_PRIVATE).edit().clear().apply()
+ }
+ com.music.spotui.data.local.ListenHistory.clear()
+ com.music.spotui.data.preferences.clearSpotiflacSessions(app)
_signedOut.value = true
MyspotiRuntime.host.logout()
}
diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/HomeScreen.kt b/base/app/src/main/java/com/music/spotui/ui/screens/HomeScreen.kt
index e2afa73b..6a0358ce 100644
--- a/base/app/src/main/java/com/music/spotui/ui/screens/HomeScreen.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/screens/HomeScreen.kt
@@ -486,18 +486,7 @@ private fun ProfileSheet(onOpen: (route: String) -> Unit = {}, onDismiss: () ->
.fillMaxWidth()
.clickable {
onDismiss()
- val app = com.music.spotui.di.Graph.app
- app.getSharedPreferences("myspoti_prefs", android.content.Context.MODE_PRIVATE)
- .edit().putBoolean("LoggedIn", false).remove("CurrentAccountName").apply()
- com.music.spotui.data.api.Api.HomeCache.clear()
- com.music.spotui.data.api.Api.LikedCache.tracks = null
- com.music.spotui.data.api.ProfileCache.clearSession()
- com.music.spotui.di.SongPlayer.clearSession()
- listOf("PlaybackState", "LibraryCache", "RecentItems", "LikedSongs",
- "LikedSongsFromSpotify", "LikedAlbums", "FollowedArtists").forEach { name ->
- app.getSharedPreferences(name, android.content.Context.MODE_PRIVATE).edit().clear().apply()
- }
- com.music.spotui.di.SessionActions.logout()
+ com.music.spotui.di.SessionActions.logout(com.music.spotui.di.Graph.app)
}
.padding(horizontal = 20.dp, vertical = 14.dp),
)
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 5e6003d0..e54e8ca3 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
@@ -164,7 +164,15 @@ class WebPlayerHost(context: Context) : BridgeHost {
/** Signs out of Spotify in this WebView, then shows the login page. */
fun logout() = main.post {
- CookieManager.getInstance().removeAllCookies { main.post { view.clearCache(false); view.loadUrl(LOGIN_URL) } }
+ CookieManager.getInstance().removeAllCookies {
+ main.post {
+ // Web storage too: localStorage/IndexedDB hold the player's session
+ // state, and clearCache(true) also drops the disk cache (false kept it).
+ android.webkit.WebStorage.getInstance().deleteAllData()
+ view.clearCache(true)
+ view.loadUrl(LOGIN_URL)
+ }
+ }
}
override suspend fun evaluate(script: String) {