Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
Quality and extension settings follow the Spotify account in use
kota-zofb320c19dba7
diff --git a/base/app/src/main/java/app/myspoti/MyspotiApplication.kt b/base/app/src/main/java/app/myspoti/MyspotiApplication.ktindex b53296a3..6089f30c 100644--- a/base/app/src/main/java/app/myspoti/MyspotiApplication.kt+++ b/base/app/src/main/java/app/myspoti/MyspotiApplication.kt@@ -12,6 +12,9 @@ import com.music.spotui.di.AutoBrowserimport com.music.spotui.di.Graphimport com.music.spotui.di.SongPlayerimport kotlinx.coroutines.flow.first+import kotlinx.coroutines.flow.drop+import kotlinx.coroutines.flow.distinctUntilChanged+import kotlinx.coroutines.flow.mapimport kotlinx.coroutines.launch/** Starts the Spotify session, the UI singletons and the playback service bindings. */@@ -44,6 +47,27 @@ class MyspotiApplication : Application() {prefetch()// New releases of the artists followed, said in a notification (a setting).com.music.spotui.data.NewReleaseNotifier.start(this)+ // Another account kept on the phone takes over: its own quality and extension settings.+ followAccount()+ }++ /**+ * The quality and the extensions' settings are the account's (see ProfilePrefs): when another+ * account becomes the active one, its streaming level goes to the player and each extension+ * hears its settings as that account has them.+ */+ private fun followAccount() {+ if (!app.myspoti.host.android.MyspotiRuntime.isStarted) return+ val ctx = this+ app.myspoti.host.android.MyspotiRuntime.scope.launch {+ app.myspoti.host.android.MyspotiRuntime.host.accounts.list+ .map { it.active }.distinctUntilChanged().drop(1)+ .collect {+ app.myspoti.host.android.Logbook.log("account: settings of the account now in use (quality, extensions)")+ com.music.spotui.data.preferences.applyStreamingQuality(ctx)+ com.music.spotui.extensions.Extensions.profileChanged()+ }+ }}/**diff --git a/base/app/src/main/java/com/music/spotui/data/preferences/DownloadPref.kt b/base/app/src/main/java/com/music/spotui/data/preferences/DownloadPref.ktindex b667b21d..e8b238ca 100644--- a/base/app/src/main/java/com/music/spotui/data/preferences/DownloadPref.kt+++ b/base/app/src/main/java/com/music/spotui/data/preferences/DownloadPref.kt@@ -17,23 +17,23 @@ private const val KEY_ASK = "ask_each_time"* Hi-res downloads (24-bit when the source has it: bigger files, ~3× a CD-quality FLAC).* Default off: CD quality is asked for.*/-fun isHiRes(context: Context): Boolean = prefs(context).getBoolean(KEY_HI_RES, false)+fun isHiRes(context: Context): Boolean = ProfilePrefs.getBoolean(prefs(context), KEY_HI_RES, false)-fun setHiRes(context: Context, on: Boolean) = prefs(context).edit().putBoolean(KEY_HI_RES, on).apply()+fun setHiRes(context: Context, on: Boolean) = prefs(context).edit().putBoolean(ProfilePrefs.key(KEY_HI_RES), on).apply()/** Downloads on. Off: no downloads at all, "Downloaded" becomes Local Files only. */-fun isDownloadsEnabled(context: Context): Boolean = prefs(context).getBoolean(KEY_ENABLED, true)+fun isDownloadsEnabled(context: Context): Boolean = ProfilePrefs.getBoolean(prefs(context), KEY_ENABLED, true)-fun setDownloadsEnabled(context: Context, on: Boolean) = prefs(context).edit().putBoolean(KEY_ENABLED, on).apply()+fun setDownloadsEnabled(context: Context, on: Boolean) = prefs(context).edit().putBoolean(ProfilePrefs.key(KEY_ENABLED), on).apply()/** Downloads kept small: the checked FLAC re-encoded to AAC on the phone. */-fun isSmallDownloads(context: Context): Boolean = prefs(context).getBoolean(KEY_SMALL, false)+fun isSmallDownloads(context: Context): Boolean = ProfilePrefs.getBoolean(prefs(context), KEY_SMALL, false)-fun setSmallDownloads(context: Context, on: Boolean) = prefs(context).edit().putBoolean(KEY_SMALL, on).apply()+fun setSmallDownloads(context: Context, on: Boolean) = prefs(context).edit().putBoolean(ProfilePrefs.key(KEY_SMALL), on).apply()/** The quality is asked at each download (once for a whole list). */-fun isAskDownloadQuality(context: Context): Boolean = prefs(context).getBoolean(KEY_ASK, false)+fun isAskDownloadQuality(context: Context): Boolean = ProfilePrefs.getBoolean(prefs(context), KEY_ASK, false)-fun setAskDownloadQuality(context: Context, on: Boolean) = prefs(context).edit().putBoolean(KEY_ASK, on).apply()+fun setAskDownloadQuality(context: Context, on: Boolean) = prefs(context).edit().putBoolean(ProfilePrefs.key(KEY_ASK), on).apply()private fun prefs(c: Context) = c.getSharedPreferences(PREF, Context.MODE_PRIVATE)diff --git a/base/app/src/main/java/com/music/spotui/data/preferences/ProfilePrefs.kt b/base/app/src/main/java/com/music/spotui/data/preferences/ProfilePrefs.ktnew file mode 100644index 00000000..64384cae--- /dev/null+++ b/base/app/src/main/java/com/music/spotui/data/preferences/ProfilePrefs.kt@@ -0,0 +1,33 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package com.music.spotui.data.preferences++import android.content.SharedPreferences++/**+ * Settings that belong to the Spotify account in use (the accounts kept on the phone, each its+ * WebView profile): the first account keeps the keys it always had, another one reads and writes+ * `key@profile`, and goes on with the first account's value until it sets its own. Asked: the+ * quality and the extensions' settings follow the account, not the phone.+ */+object ProfilePrefs {+ /** The active account's profile; the first one before the engine has started. */+ fun profile(): String = runCatching {+ val runtime = app.myspoti.host.android.MyspotiRuntime+ if (runtime.isStarted) runtime.host.accounts.list.value.active else null+ }.getOrNull() ?: app.myspoti.host.android.AccountList.DEFAULT_PROFILE++ val isDefault: Boolean get() = profile() == app.myspoti.host.android.AccountList.DEFAULT_PROFILE++ /** [base] as the active account stores it. */+ fun key(base: String): String = profile().let { if (it == app.myspoti.host.android.AccountList.DEFAULT_PROFILE) base else "$base@$it" }++ fun getString(p: SharedPreferences, base: String, default: String?): String? {+ val k = key(base)+ return if (k != base && p.contains(k)) p.getString(k, default) else p.getString(base, default)+ }++ fun getBoolean(p: SharedPreferences, base: String, default: Boolean): Boolean {+ val k = key(base)+ return if (k != base && p.contains(k)) p.getBoolean(k, default) else p.getBoolean(base, default)+ }+}diff --git a/base/app/src/main/java/com/music/spotui/data/preferences/QualityPref.kt b/base/app/src/main/java/com/music/spotui/data/preferences/QualityPref.ktindex b782f4de..c4beeef4 100644--- a/base/app/src/main/java/com/music/spotui/data/preferences/QualityPref.kt+++ b/base/app/src/main/java/com/music/spotui/data/preferences/QualityPref.kt@@ -47,13 +47,14 @@ enum class DownloadQuality {* Mobile data follows the Wi-Fi level until it is set on its own.*/fun getStreamingQuality(c: Context, cellular: Boolean = false): StreamingQuality {- val wifi = prefs(c).getString(KEY_STREAMING, null)- return StreamingQuality.of(if (cellular) prefs(c).getString(KEY_STREAMING_CELL, null) ?: wifi else wifi)+ // The account's own levels (see ProfilePrefs).+ val wifi = ProfilePrefs.getString(prefs(c), KEY_STREAMING, null)+ return StreamingQuality.of(if (cellular) ProfilePrefs.getString(prefs(c), KEY_STREAMING_CELL, null) ?: wifi else wifi)}/** Saves the level for that network and hands the one for the current network to the player. */fun setStreamingQuality(c: Context, q: StreamingQuality, cellular: Boolean = false) {- prefs(c).edit().putString(if (cellular) KEY_STREAMING_CELL else KEY_STREAMING, q.level).apply()+ prefs(c).edit().putString(ProfilePrefs.key(if (cellular) KEY_STREAMING_CELL else KEY_STREAMING), q.level).apply()applyStreamingQuality(c)}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.ktindex 53f42167..311b1e1a 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@@ -43,7 +43,7 @@ object SessionActions {* Everything that follows the account leaves with it: nothing of it keeps playing or showing,* its caches are dropped, its downloads put aside under it (OfflineAccounts). [forget]: a sign-out* or a removal, its listen history goes; otherwise (a switch) it is put aside under it too.- * Extensions are the app's, not the account's: they keep running.+ * Extensions are the app's and keep running; their settings and the quality are the account's (ProfilePrefs).*/private fun leaveAccount(app: android.content.Context, forget: Boolean) {// This account's downloads go aside: the next account on the phone sees none of them.diff --git a/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt b/base/app/src/main/java/com/music/spotui/extensions/Extensions.ktindex 1d98839f..247cb9ad 100644--- a/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt+++ b/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt@@ -397,7 +397,7 @@ object Extensions {suspend fun uninstall(id: String) = withContext(Dispatchers.IO) {stop(id)update { it.remove(id) }- packages.remove(id); stores.remove(id); settings.remove(id); logs.remove(id)+ packages.remove(id); stores.remove(id); settings.keys.removeAll { it.endsWith("\u0000$id") }; logs.remove(id)clearError(id)runningSha.remove(id)ExtensionAudio.forgetAll()@@ -1544,7 +1544,38 @@ object Extensions {}private fun logOf(id: String) = logs.getOrPut(id) { ExtensionLog(File(dir, "$id/log.txt")) }- private fun settingsOf(id: String) = settings.getOrPut(id) { FileStore(File(dir, "$id/settings.json")) }+ /**+ * [id]'s settings as the Spotify account in use has them (asked: the extensions' settings follow+ * the account): the first account's `settings.json`, another's `settings-<profile>.json`, made+ * from the first account's at its first read so the extension goes on as it was set.+ */+ private fun settingsOf(id: String): FileStore {+ val profile = com.music.spotui.data.preferences.ProfilePrefs.profile()+ return settings.getOrPut("$profile\u0000$id") {+ val first = File(dir, "$id/settings.json")+ if (profile == app.myspoti.host.android.AccountList.DEFAULT_PROFILE) return@getOrPut FileStore(first)+ val own = File(dir, "$id/settings-" + profile.filter { it.isLetterOrDigit() || it == '-' || it == '_' } + ".json")+ if (!own.exists() && first.exists()) runCatching { first.copyTo(own) }+ FileStore(own)+ }+ }++ /**+ * Another Spotify account took over: each extension on hears its settings as that account has+ * them (as it hears a change made in its page), its status is asked again (its sign-in may+ * differ), and who gives what is read again.+ */+ fun profileChanged() {+ if (!isReady) return+ _registry.value.extensions.filter { it.enabled }.forEach { e ->+ val keys = settingsOf(e.id).all().keys.map { app.myspoti.extensions.Setting.baseKey(it) }.distinct()+ if (keys.isNotEmpty()) scope.launch { keys.forEach { sendSetting(e.id, it, pressed = false) } }+ health.forget(e.id)+ runtimes[e.id]?.let { rt -> scope.launch { probe(e.id, rt) } }+ }+ _statuses.value = health.inactiveNow()+ refreshProvided()+ }private fun storeOf(id: String) = stores.getOrPut(id) { FileStore(File(dir, "$id/storage.json")) }/** Replaces [file] whole or throws, never losing the old one (see [com.music.spotui.data.writeBytesSafely]). */