Skip to content

Native Android client for Spotify, built with Jetpack Compose

Downloads belong to their account: on sign-out they are put aside under it (not deleted) and the next account starts with none, signing in again brings them back, older downloads go to the first account seen; a list or song is marked downloaded only when its download can start; the download index, the downloaded lists and local playlists are written whole or not at all; downloads resumed at launch wait for the extensions still loading instead of being marked unavailable [skip ci]

kota-zo42da94adafbe
diff --git a/base/app/src/main/java/com/music/spotui/data/SafeWrite.kt b/base/app/src/main/java/com/music/spotui/data/SafeWrite.kt
new file mode 100644
index 00000000..ad490bfd
--- /dev/null
+++ b/base/app/src/main/java/com/music/spotui/data/SafeWrite.kt
@@ -0,0 +1,24 @@
+package com.music.spotui.data
+
+import java.io.File
+import java.io.FileOutputStream
+import java.io.IOException
+
+/**
+ * Replaces [file] with [text] whole or not at all: written to a temporary file next to it, flushed
+ * to the disk, then moved over it. A move that fails (the target in the way on some file systems)
+ * is tried once more after deleting the target; failing again, the old file is left as it was and
+ * an [IOException] says so, instead of the app carrying on with a state it never saved.
+ */
+fun writeTextSafely(file: File, text: String) {
+ file.parentFile?.mkdirs()
+ val tmp = File(file.parentFile, file.name + ".tmp")
+ FileOutputStream(tmp).use { out ->
+ out.write(text.toByteArray())
+ out.fd.sync()
+ }
+ if (tmp.renameTo(file)) return
+ if (file.delete() && tmp.renameTo(file)) return
+ tmp.delete()
+ throw IOException("could not replace ${file.name}")
+}
diff --git a/base/app/src/main/java/com/music/spotui/data/api/ProfileCache.kt b/base/app/src/main/java/com/music/spotui/data/api/ProfileCache.kt
index b4746e2c..dca01e94 100644
--- a/base/app/src/main/java/com/music/spotui/data/api/ProfileCache.kt
+++ b/base/app/src/main/java/com/music/spotui/data/api/ProfileCache.kt
@@ -42,6 +42,8 @@ object ProfileCache {
name = me.name
imageUrl = me.images.best(160)?.url
id = me.username.takeIf { it.isNotBlank() }
+ // The downloads shown are this account's own (see OfflineAccounts).
+ id?.let { com.music.spotui.data.offline.OfflineAccounts.claim(context.applicationContext, it) }
} finally {
fetching.set(false)
}
diff --git a/base/app/src/main/java/com/music/spotui/data/local/LocalPlaylists.kt b/base/app/src/main/java/com/music/spotui/data/local/LocalPlaylists.kt
index 91ac3bdb..6d633624 100644
--- a/base/app/src/main/java/com/music/spotui/data/local/LocalPlaylists.kt
+++ b/base/app/src/main/java/com/music/spotui/data/local/LocalPlaylists.kt
@@ -104,9 +104,8 @@ object LocalPlaylists {
ensureLoaded()
change()
if (::file.isInitialized) {
- val tmp = File(file.parentFile, file.name + ".tmp")
- tmp.writeText(write())
- tmp.renameTo(file)
+ runCatching { com.music.spotui.data.writeTextSafely(file, write()) }
+ .onFailure { app.myspoti.host.android.Logbook.log("local playlists not saved: ${it.message}") }
}
}
}
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/OfflineAccounts.kt b/base/app/src/main/java/com/music/spotui/data/offline/OfflineAccounts.kt
new file mode 100644
index 00000000..3c4aa73a
--- /dev/null
+++ b/base/app/src/main/java/com/music/spotui/data/offline/OfflineAccounts.kt
@@ -0,0 +1,101 @@
+package com.music.spotui.data.offline
+
+import android.content.Context
+import app.myspoti.host.android.Logbook
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.sync.Mutex
+import kotlinx.coroutines.sync.withLock
+import kotlinx.coroutines.withContext
+import java.io.File
+import java.security.MessageDigest
+
+/**
+ * Downloads belong to the Spotify account that made them. The downloads folder (`offline/`: songs,
+ * episodes, lists, queues) is stamped with its account; on sign-out it is put aside under that
+ * account (`offline-accounts/<key>/`) and the next account starts with none. Signing in again
+ * brings that account's own folder back. Nothing is deleted: a phone shared by two accounts keeps
+ * both sets, each seen only by its own account. Downloads made before the stamp existed go to the
+ * first account seen signed in.
+ */
+object OfflineAccounts {
+ private const val OWNER = "owner"
+ private val lock = Mutex()
+
+ private fun live(context: Context) = File(context.filesDir, "offline")
+ private fun parked(context: Context, key: String) = File(context.filesDir, "offline-accounts/$key")
+ private fun ownerOf(dir: File): String? = File(dir, OWNER).takeIf { it.isFile }?.readText()?.trim()?.ifBlank { null }
+
+ /** A stable, opaque folder name for a Spotify user id (the id itself is not written down). */
+ internal fun keyOf(userId: String): String =
+ MessageDigest.getInstance("SHA-256").digest(userId.toByteArray()).take(12).joinToString("") { "%02x".format(it) }
+
+ /** Signing out: this account's downloads go aside; [userId] names them when the folder has no stamp yet. */
+ suspend fun park(context: Context, userId: String?) = lock.withLock {
+ val dir = live(context)
+ val owner = ownerOf(dir) ?: userId?.let(::keyOf)
+ if (owner == null && !hasDownloads(dir)) return@withLock
+ // Whose they are is unknown (never signed in online since): kept aside, never given to anyone.
+ val key = owner ?: "unclaimed-${System.currentTimeMillis()}"
+ stopDownloads(dir)
+ val to = parked(context, key)
+ withContext(Dispatchers.IO) {
+ to.parentFile?.mkdirs()
+ if (to.exists()) to.deleteRecursively()
+ if (!dir.renameTo(to)) Logbook.log("offline: downloads not put aside for sign-out")
+ }
+ reopen(context)
+ Logbook.log("offline: downloads put aside for sign-out")
+ }
+
+ /** [userId] is signed in: its downloads are the ones shown; downloads with no stamp yet become its own. */
+ suspend fun claim(context: Context, userId: String) = lock.withLock {
+ val key = keyOf(userId)
+ val dir = live(context)
+ val owner = ownerOf(dir)
+ if (owner == key) return@withLock
+ if (owner == null && hasDownloads(dir)) {
+ stamp(dir, key)
+ return@withLock
+ }
+ // Another account's downloads are in place (a sign-out that could not put them aside), or none.
+ stopDownloads(dir)
+ withContext(Dispatchers.IO) {
+ if (owner != null) {
+ val to = parked(context, owner)
+ to.parentFile?.mkdirs()
+ if (to.exists()) to.deleteRecursively()
+ dir.renameTo(to)
+ } else {
+ dir.deleteRecursively()
+ }
+ val back = parked(context, key)
+ if (back.isDirectory && !back.renameTo(dir)) Logbook.log("offline: this account's downloads could not be brought back")
+ stamp(dir, key)
+ }
+ reopen(context)
+ Logbook.log("offline: this account's downloads in place (${OfflineStore.tracks.size} songs)")
+ }
+
+ private fun stamp(dir: File, key: String) {
+ dir.mkdirs()
+ runCatching { com.music.spotui.data.writeTextSafely(File(dir, OWNER), key) }
+ }
+
+ private fun hasDownloads(dir: File): Boolean =
+ File(dir, "index.json").length() > 2 || File(dir, "collections.json").length() > 2 ||
+ File(dir, "pending.json").length() > 2 || File(dir, "podcasts-pending.json").length() > 2
+
+ /** Running downloads stop; the queues on disk are kept as they were, so they resume with their account. */
+ private suspend fun stopDownloads(dir: File) {
+ val queues = listOf(File(dir, "pending.json"), File(dir, "podcasts-pending.json"))
+ val saved = withContext(Dispatchers.IO) { queues.map { f -> f to f.takeIf { it.isFile }?.readText() } }
+ OfflineDownloader.forgetSession()
+ PodcastDownloader.forgetSession()
+ withContext(Dispatchers.IO) { saved.forEach { (f, text) -> if (text != null) runCatching { f.writeText(text) } } }
+ }
+
+ private suspend fun reopen(context: Context) = withContext(Dispatchers.Main) {
+ OfflineStore.reopen(context)
+ OfflineCollections.reopen()
+ }
+}
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/OfflineCollections.kt b/base/app/src/main/java/com/music/spotui/data/offline/OfflineCollections.kt
index 2a445404..c8c9c615 100644
--- a/base/app/src/main/java/com/music/spotui/data/offline/OfflineCollections.kt
+++ b/base/app/src/main/java/com/music/spotui/data/offline/OfflineCollections.kt
@@ -58,6 +58,12 @@ object OfflineCollections {
runCatching { read(file.readText()) }
}
+ /** Reads the downloaded lists again (main thread): another account's downloads were put in place. */
+ fun reopen() {
+ synchronized(lock) { collections.clear(); singles.clear() }
+ runCatching { read(file.readText()) }
+ }
+
fun isDownloaded(uri: String) = collections.containsKey(uri)
/** A song still has a reason to stay on the phone. */
@@ -81,6 +87,8 @@ object OfflineCollections {
// Episodes have no track id and their own downloader (PodcastDownloader): sending one
// here would only be dropped, or worse, hit the track download path.
if (song.spotifyTrackId.isBlank() || episodeKeyOf(song) != null) return
+ // Kept only when the download can start: otherwise it would show as downloaded with no file.
+ if (!OfflineDownloader.ready()) return
synchronized(lock) { singles += song.spotifyTrackId }
save()
OfflineDownloader.enqueue(listOf(song), quality)
@@ -103,6 +111,8 @@ object OfflineCollections {
// An episode in a list is not a track: it stays out of the collection, and the
// list download never enqueues it (its own menu row is the way to download an episode).
val ids = page.tracks.map { it.spotifyTrackId }.filter { it.isNotBlank() && !isEpisodeKey(it) }.toSet()
+ // Marked downloaded only when the download can start (downloads on, a download extension).
+ if (!OfflineDownloader.ready()) return
collections[page.uri] = OfflineCollection(page.uri, page.type, page.name, ids, System.currentTimeMillis(), quality?.name.orEmpty())
save()
keepPage(page)
@@ -220,12 +230,8 @@ object OfflineCollections {
}
JSONObject().put("version", 1).put("lists", lists).put("singles", JSONArray(singles.toList())).toString()
}
- runCatching {
- file.parentFile?.mkdirs()
- val tmp = File(file.parentFile, file.name + ".tmp")
- tmp.writeText(text)
- tmp.renameTo(file)
- }
+ runCatching { com.music.spotui.data.writeTextSafely(file, text) }
+ .onFailure { app.myspoti.host.android.Logbook.log("offline: downloaded lists not saved: ${it.message}") }
}
private fun read(text: String) {
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/OfflineDownloader.kt b/base/app/src/main/java/com/music/spotui/data/offline/OfflineDownloader.kt
index 4d0204de..321a4d7e 100644
--- a/base/app/src/main/java/com/music/spotui/data/offline/OfflineDownloader.kt
+++ b/base/app/src/main/java/com/music/spotui/data/offline/OfflineDownloader.kt
@@ -119,26 +119,34 @@ object OfflineDownloader {
* Downloads [songs] in [quality] (asked when the setting is "ask each time"), else in
* the quality chosen in Settings.
*/
- fun enqueue(
- songs: List<SongsModel>,
- quality: com.music.spotui.data.preferences.DownloadQuality? = null,
- /** Started by the app (a downloaded list keeping in step), not by a tap. */
- auto: Boolean = false,
- ) {
- // Downloads switched off in Settings: say so, instead of a list of failed songs.
+ /**
+ * Whether a download can start now, said in a toast when not: downloads switched off in
+ * Settings (instead of a list of failed songs), or no extension giving download files (where to
+ * get one, for a download the user asked for). A list is only marked downloaded when this holds.
+ */
+ fun ready(auto: Boolean = false): Boolean {
if (!isDownloadsEnabled(Graph.app)) {
scope.launch(Dispatchers.Main) {
android.widget.Toast.makeText(Graph.app, app.myspoti.engine.R.string.downloads_off_toast, android.widget.Toast.LENGTH_LONG).show()
}
- return
+ return false
}
- // No extension gives download files: say where to get one, instead of a list of failures.
if (!com.music.spotui.extensions.Extensions.hasDownloadSource()) {
if (!auto) scope.launch(Dispatchers.Main) {
android.widget.Toast.makeText(Graph.app, app.myspoti.engine.R.string.downloads_need_source, android.widget.Toast.LENGTH_LONG).show()
}
- return
+ return false
}
+ return true
+ }
+
+ fun enqueue(
+ songs: List<SongsModel>,
+ quality: com.music.spotui.data.preferences.DownloadQuality? = null,
+ /** Started by the app (a downloaded list keeping in step), not by a tap. */
+ auto: Boolean = false,
+ ) {
+ if (!ready(auto)) return
if (auto) {
// Automatic downloads wait for Wi-Fi (never the user's mobile data): the next sync takes them.
if (com.music.spotui.data.DataSaver.metered || !OfflineStore.isOnline(Graph.app)) { OfflineCollections.waitForWifi(); return }
@@ -170,6 +178,20 @@ object OfflineDownloader {
added.forEach { queue.trySend(it) }
}
+ /**
+ * The account's downloads are put away (sign-out, another account signing in): the queue is
+ * dropped from memory only (its file goes with the account's folder) and the running downloads
+ * stop, waited for a moment so none finishes into the next account's folder.
+ */
+ internal suspend fun forgetSession() {
+ synchronized(pending) { pending.clear() }
+ val jobs = running.values.toList()
+ running.clear()
+ jobs.forEach { it.cancel() }
+ kotlinx.coroutines.withTimeoutOrNull(5_000) { jobs.forEach { it.join() } }
+ withContext(Dispatchers.Main) { requests.clear() }
+ }
+
/** Stops a queued or running download (and forgets a failed one). */
fun cancel(trackId: String) {
synchronized(pending) { if (pending.remove(trackId) != null) savePending() }
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/OfflineStore.kt b/base/app/src/main/java/com/music/spotui/data/offline/OfflineStore.kt
index 649f8aa0..500f1c37 100644
--- a/base/app/src/main/java/com/music/spotui/data/offline/OfflineStore.kt
+++ b/base/app/src/main/java/com/music/spotui/data/offline/OfflineStore.kt
@@ -102,6 +102,12 @@ object OfflineStore {
}.apply { name = "offline-encrypt"; priority = Thread.MIN_PRIORITY }.start()
}
+ /** Reads the downloads folder again (main thread): another account's downloads were put in its place. */
+ fun reopen(context: Context) {
+ status.clear()
+ load(File(context.filesDir, "offline"))
+ }
+
/** Reads the downloads in [dir] into [tracks]; returns the audio files of the songs found. */
internal fun load(dir: File): List<File> {
root = dir
@@ -238,9 +244,9 @@ object OfflineStore {
private fun writeIndex() {
val a = JSONArray()
tracks.values.forEach { a.put(it.toJson()) }
- val tmp = File(root, "index.json.tmp")
- tmp.writeText(a.toString())
if (indexFile.exists() && readIndex(indexFile)?.complete == true) indexFile.copyTo(backupFile, overwrite = true)
- tmp.renameTo(indexFile)
+ // A failed write keeps the last good index (and its backup) rather than a half-moved one.
+ runCatching { com.music.spotui.data.writeTextSafely(indexFile, a.toString()) }
+ .onFailure { app.myspoti.host.android.Logbook.log("offline: index not saved: ${it.message}") }
}
}
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/PodcastDownloader.kt b/base/app/src/main/java/com/music/spotui/data/offline/PodcastDownloader.kt
index adeaf216..0520a490 100644
--- a/base/app/src/main/java/com/music/spotui/data/offline/PodcastDownloader.kt
+++ b/base/app/src/main/java/com/music/spotui/data/offline/PodcastDownloader.kt
@@ -148,6 +148,20 @@ object PodcastDownloader {
}
/** Stops a queued or running download (and forgets a refused one). */
+ /**
+ * The account's downloads are put away (sign-out, another account signing in): the queue is
+ * dropped from memory only (its file goes with the account's folder) and the running downloads
+ * stop, waited for a moment so none finishes into the next account's folder.
+ */
+ internal suspend fun forgetSession() {
+ synchronized(pending) { pending.clear() }
+ val jobs = running.values.toList()
+ running.clear()
+ jobs.forEach { it.cancel() }
+ kotlinx.coroutines.withTimeoutOrNull(5_000) { jobs.forEach { it.join() } }
+ withContext(Dispatchers.Main) { requests.clear() }
+ }
+
fun cancel(trackId: String) {
synchronized(pending) { if (pending.remove(trackId) != null) savePending() }
running.remove(trackId)?.cancel()
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 a2610029..7601967b 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
@@ -1,5 +1,6 @@
package com.music.spotui.di
+import app.myspoti.host.android.Logbook
import app.myspoti.host.android.MyspotiRuntime
import app.myspoti.host.android.PlaybackService
import kotlinx.coroutines.flow.MutableStateFlow
@@ -16,10 +17,16 @@ object SessionActions {
* 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), and the Web Player's cookies, Web storage
- * and cache (in the host).
+ * and cache (in the host). Downloads are put aside under the account (OfflineAccounts), not deleted.
*/
fun logout(context: android.content.Context) {
val app = context.applicationContext
+ // This account's downloads go aside: the next account on the phone sees none of them.
+ val account = com.music.spotui.data.api.ProfileCache.id
+ MyspotiRuntime.scope.launch {
+ runCatching { com.music.spotui.data.offline.OfflineAccounts.park(app, account) }
+ .onFailure { Logbook.log("offline: sign-out park failed: ${it.message}") }
+ }
app.getSharedPreferences("myspoti_prefs", android.content.Context.MODE_PRIVATE)
.edit().putBoolean("LoggedIn", false).remove("CurrentAccountName").apply()
// Nothing of the account keeps playing or showing: local songs, the clip, the notification.
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.kt
index 173d8eda..a110412f 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
@@ -661,7 +661,11 @@ object Extensions {
fun statusOf(id: String): app.myspoti.extensions.ExtensionStatus? = health.last(id)
/** An enabled extension gives songs' download files (without one, downloads say to install one). */
- fun hasDownloadSource(): Boolean { providedState.value; return isReady && providing(Hook.DOWNLOAD_SOURCE).isNotEmpty() }
+ fun hasDownloadSource(): Boolean { providedState.value; return isReady && (providing(Hook.DOWNLOAD_SOURCE).isNotEmpty() || startingDownloadSource()) }
+
+ /** The app is still starting the extensions on at launch, a download source among them. */
+ private fun startingDownloadSource(): Boolean =
+ !started.isCompleted && _registry.value.extensions.any { it.enabled && Action.DOWNLOAD_SOURCE.key in it.granted }
/**
* The files [track] can be downloaded from in [quality] (`hi-res`, `cd`, `small`), best first,
@@ -670,6 +674,13 @@ object Extensions {
*/
suspend fun downloadFiles(track: TrackInfo, quality: String): Triple<String, String, List<DownloadFile>>? {
if (!isReady) return null
+ // Asked right after launch (downloads resumed from the last session): the extensions still
+ // loading are waited for, rather than the song marked unavailable for want of a source.
+ if (!started.isCompleted) {
+ val t0 = System.currentTimeMillis()
+ val loaded = withTimeoutOrNull(START_WAIT_MS) { started.await() } != null
+ Logbook.log("download: extensions not loaded yet, waited ${System.currentTimeMillis() - t0} ms${if (loaded) "" else " (still loading)"}")
+ }
val input = DownloadQuery.toJson(track, quality)
probeStale(Hook.DOWNLOAD_SOURCE)
return HookDispatch.firstInOrder(available(Hook.DOWNLOAD_SOURCE)) { (id, rt) ->
diff --git a/base/app/src/test/java/com/music/spotui/data/SafeWriteTest.kt b/base/app/src/test/java/com/music/spotui/data/SafeWriteTest.kt
new file mode 100644
index 00000000..99930a5c
--- /dev/null
+++ b/base/app/src/test/java/com/music/spotui/data/SafeWriteTest.kt
@@ -0,0 +1,39 @@
+package com.music.spotui.data
+
+import com.music.spotui.data.offline.OfflineAccounts
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertNotEquals
+import org.junit.Test
+import java.io.File
+import java.nio.file.Files
+
+class SafeWriteTest {
+ @Test
+ fun replacesTheFileWholeAndLeavesNoTemporary() {
+ val dir = Files.createTempDirectory("safe").toFile()
+ val f = File(dir, "index.json")
+ f.writeText("old")
+ writeTextSafely(f, "new")
+ assertEquals("new", f.readText())
+ assertFalse(File(dir, "index.json.tmp").exists())
+ dir.deleteRecursively()
+ }
+
+ @Test
+ fun createsMissingFolders() {
+ val dir = Files.createTempDirectory("safe").toFile()
+ val f = File(dir, "a/b/collections.json")
+ writeTextSafely(f, "{}")
+ assertEquals("{}", f.readText())
+ dir.deleteRecursively()
+ }
+
+ @Test
+ fun accountKeyIsStableOpaqueAndDistinct() {
+ assertEquals(OfflineAccounts.keyOf("alice"), OfflineAccounts.keyOf("alice"))
+ assertNotEquals(OfflineAccounts.keyOf("alice"), OfflineAccounts.keyOf("bob"))
+ assertEquals(24, OfflineAccounts.keyOf("alice").length)
+ assertFalse(OfflineAccounts.keyOf("alice").contains("alice"))
+ }
+}