Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
Updates: only the APK of the installed channel is offered (beta or stable, or the older single-name APK), never another channel's build; the download stops over 200 MB, the partial file is deleted, and an installer session that fails before its commit is abandoned [skip ci]
kota-zoa6acafadb78b
diff --git a/base/app/src/main/java/com/music/spotui/data/api/AppUpdater.kt b/base/app/src/main/java/com/music/spotui/data/api/AppUpdater.ktindex 4f1edbab..dc76af1d 100644--- a/base/app/src/main/java/com/music/spotui/data/api/AppUpdater.kt+++ b/base/app/src/main/java/com/music/spotui/data/api/AppUpdater.kt@@ -58,10 +58,13 @@ object AppUpdater {val app = context.applicationContextif (!canInstall(app)) { _state.value = State.NeedsPermission; return }_state.value = State.Downloading(-1f)+ val apk = File(File(app.cacheDir, "updates"), "myspoti-${release.version}.apk")val result = runCatching {- val apk = download(app, release)+ download(release, apk)install(app, apk)}+ // The installer session holds its own copy once committed; a failed download left a partial one.+ runCatching { apk.delete() }result.exceptionOrNull()?.let {android.util.Log.w("AppUpdater", "update failed", it)_state.value = State.Failed(it.message ?: it.javaClass.simpleName)@@ -70,13 +73,16 @@ object AppUpdater {fun reset() { _state.value = State.Idle }- private suspend fun download(context: Context, release: UpdateChecker.Release): File = withContext(Dispatchers.IO) {- val dir = File(context.cacheDir, "updates").apply { mkdirs() }+ /** No release of this app comes near it; a bigger answer is not an APK of ours. */+ private const val MAX_APK_BYTES = 200L * 1024 * 1024++ private suspend fun download(release: UpdateChecker.Release, file: File): Unit = withContext(Dispatchers.IO) {+ val dir = file.parentFile!!.apply { mkdirs() }dir.listFiles()?.forEach { it.delete() }- val file = File(dir, "myspoti-${release.version}.apk")http.newCall(Request.Builder().url(release.apkUrl).build()).execute().use { r ->if (!r.isSuccessful) throw java.io.IOException("HTTP ${r.code}")val total = r.body.contentLength()+ if (total > MAX_APK_BYTES) throw java.io.IOException("the download is too big ($total bytes)")var done = 0Lvar shown = -1r.body.byteStream().use { input ->@@ -87,6 +93,7 @@ object AppUpdater {if (n < 0) breakout.write(buf, 0, n)done += n+ if (done > MAX_APK_BYTES) throw java.io.IOException("the download is too big (over $MAX_APK_BYTES bytes)")if (total > 0) {val pct = (done * 100 / total).toInt()if (pct != shown) { shown = pct; _state.value = State.Downloading(pct / 100f) }@@ -96,7 +103,6 @@ object AppUpdater {}if (total > 0 && done != total) throw java.io.IOException("download cut at $done / $total bytes")}- file}/** Streams the APK into an installer session; [StatusReceiver] gets the outcome. */@@ -111,18 +117,25 @@ object AppUpdater {// would be ignored anyway; the confirmation is the user's last check.}val id = installer.createSession(params)- installer.openSession(id).use { session ->- session.openWrite("myspoti.apk", 0, apk.length()).use { out ->- apk.inputStream().use { it.copyTo(out) }- session.fsync(out)+ var committed = false+ try {+ installer.openSession(id).use { session ->+ session.openWrite("myspoti.apk", 0, apk.length()).use { out ->+ apk.inputStream().use { it.copyTo(out) }+ session.fsync(out)+ }+ val callback = PendingIntent.getBroadcast(+ context, id,+ Intent(ACTION).setPackage(context.packageName).setClass(context, StatusReceiver::class.java),+ // Mutable: the installer adds the status extras.+ PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE,+ )+ session.commit(callback.intentSender)+ committed = true}- val callback = PendingIntent.getBroadcast(- context, id,- Intent(ACTION).setPackage(context.packageName).setClass(context, StatusReceiver::class.java),- // Mutable: the installer adds the status extras.- PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE,- )- session.commit(callback.intentSender)+ } finally {+ // A session that never reached the commit would stay open, holding a copy of the APK.+ if (!committed) runCatching { installer.abandonSession(id) }}}diff --git a/base/app/src/main/java/com/music/spotui/data/api/UpdateChecker.kt b/base/app/src/main/java/com/music/spotui/data/api/UpdateChecker.ktindex 748de272..b4c8eb14 100644--- a/base/app/src/main/java/com/music/spotui/data/api/UpdateChecker.kt+++ b/base/app/src/main/java/com/music/spotui/data/api/UpdateChecker.kt@@ -119,16 +119,18 @@ object UpdateChecker {return Release(version, url, prefs.getString("notes", null).orEmpty(), prefs.getString("apk", null).orEmpty()).takeIf { isNewer(version, BuildConfig.VERSION_NAME) }}- /** The release's APK download link (the CI uploads one `myspoti-<beta|stable>-<version>.apk`; older releases `myspoti-<version>.apk`). */- internal fun apkAsset(release: JSONObject): String {+ /**+ * The release's APK download link for this app's channel (the CI uploads one `myspoti-<beta|stable>-<version>.apk`;+ * older releases `myspoti-<version>.apk`, which is the app's own channel since [fetch] only offers a release of it).+ * Only this channel's APK: a release carrying both must not hand a phone the other channel's build+ * (a different package, it could not update this one), and a name that matches none gives no link.+ */+ internal fun apkAsset(release: JSONObject, beta: Boolean = isBeta): String {val assets = release.optJSONArray("assets") ?: return ""- val all = (0 until assets.length()).map { assets.getJSONObject(it) }- // This release's own APK first: a release carrying several APKs (both- // channels) must not hand a phone the other channel's build.val version = release.optString("tag_name").removePrefix("v")- val want = setOf("myspoti-beta-$version.apk", "myspoti-stable-$version.apk", "myspoti-$version.apk")- return (all.firstOrNull { it.optString("name") in want }- ?: all.firstOrNull { it.optString("name").endsWith(".apk", ignoreCase = true) })+ val want = setOf("myspoti-${if (beta) "beta" else "stable"}-$version.apk", "myspoti-$version.apk")+ return (0 until assets.length()).map { assets.getJSONObject(it) }+ .firstOrNull { it.optString("name") in want }?.optString("browser_download_url").orEmpty()}diff --git a/base/app/src/test/java/com/music/spotui/data/api/UpdateCheckerTest.kt b/base/app/src/test/java/com/music/spotui/data/api/UpdateCheckerTest.ktindex b84a7374..bd075b7a 100644--- a/base/app/src/test/java/com/music/spotui/data/api/UpdateCheckerTest.kt+++ b/base/app/src/test/java/com/music/spotui/data/api/UpdateCheckerTest.kt@@ -35,15 +35,23 @@ class UpdateCheckerTest {val body = "- **Local Files**: a folder\n - nested [link](https://x.y)\n\n**Full Changelog**: https://github.com/a/b/compare/v1...v2"assertEquals("• Local Files: a folder\n • nested link", UpdateChecker.cleanNotes(body))}- @Test fun findsTheReleaseApk() {- val release = org.json.JSONObject("""{"assets":[+ @Test fun findsTheReleaseApkOfThisChannelOnly() {+ // An older release: one APK named after the version, whichever channel the release is.+ val legacy = org.json.JSONObject("""{"tag_name":"v2.0.0-beta.4","assets":[{"name":"checksums.txt","browser_download_url":"https://x/c.txt"},{"name":"myspoti-2.0.0-beta.4.apk","browser_download_url":"https://x/myspoti.apk"}]}""")- assertEquals("https://x/myspoti.apk", UpdateChecker.apkAsset(release))- val both = org.json.JSONObject("""{"tag_name":"v2.2.1-beta.1","assets":[- {"name":"myspoti-stable-2.2.0.apk","browser_download_url":"https://x/stable.apk"},- {"name":"myspoti-beta-2.2.1-beta.1.apk","browser_download_url":"https://x/beta.apk"}]}""")- assertEquals("https://x/beta.apk", UpdateChecker.apkAsset(both))- assertEquals("", UpdateChecker.apkAsset(org.json.JSONObject("{}")))+ assertEquals("https://x/myspoti.apk", UpdateChecker.apkAsset(legacy, beta = true))+ assertEquals("https://x/myspoti.apk", UpdateChecker.apkAsset(legacy, beta = false))+ val both = org.json.JSONObject("""{"tag_name":"v2.2.1","assets":[+ {"name":"myspoti-stable-2.2.1.apk","browser_download_url":"https://x/stable.apk"},+ {"name":"myspoti-beta-2.2.1.apk","browser_download_url":"https://x/beta.apk"}]}""")+ assertEquals("https://x/beta.apk", UpdateChecker.apkAsset(both, beta = true))+ assertEquals("https://x/stable.apk", UpdateChecker.apkAsset(both, beta = false))+ // Only the other channel's APK, or an unrelated .apk: no link, never the wrong build.+ val other = org.json.JSONObject("""{"tag_name":"v2.2.1","assets":[+ {"name":"myspoti-stable-2.2.1.apk","browser_download_url":"https://x/stable.apk"},+ {"name":"something-else.apk","browser_download_url":"https://x/else.apk"}]}""")+ assertEquals("", UpdateChecker.apkAsset(other, beta = true))+ assertEquals("", UpdateChecker.apkAsset(org.json.JSONObject("{}"), beta = true))}}