feat: downloads resume where they stopped, and fall back to another source (#623)

This commit is contained in:
kingokksa authored and GitHub committed 2026-10-16 13:45:35 -04:00
1 parent 5e57d78b5d
commit 7c90879b39
53 files changed
+784 -221

No files matched your search

@@ -3,8 +3,10 @@ package com.droiddeck.launcher.update
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import com.droiddeck.launcher.BuildConfig
import com.droiddeck.launcher.R
import com.droiddeck.launcher.core.DownloadSource
import org.json.JSONArray
import org.json.JSONObject
import java.io.IOException
@@ -13,6 +15,7 @@ import java.net.URL
import java.security.MessageDigest
object AppUpdates {
private const val TAG = "AppUpdates"
private const val REPO = "Droid-Deck/DroidDeck"
private const val CI_REPO = "Droid-Deck/DroidDeck-CI"
private const val CATALOG_URL = "https://raw.githubusercontent.com/$CI_REPO/main/catalog.json"
@@ -353,7 +356,24 @@ object AppUpdates {
}
private fun get(context: Context, url: String): String {
val c = URL(url).openConnection() as HttpURLConnection
val sources = DownloadSource.candidates(url)
var last: IOException? = null
for ((index, source) in sources.withIndex()) {
try {
val body = get(context, source, url)
DownloadSource.succeeded(source)
return body
} catch (e: IOException) {
last = e
if (index < sources.lastIndex) Log.i(TAG, "retrying $url via $source")
}
}
DownloadSource.failed()
throw last ?: IOException(context.getString(R.string.appupd_http, 0))
}
private fun get(context: Context, source: String, url: String): String {
val c = URL(source).openConnection() as HttpURLConnection
c.connectTimeout = 15_000
c.readTimeout = 20_000
c.setRequestProperty("Accept", "application/json")
@@ -10,13 +10,11 @@ import android.os.Build
import android.provider.Settings
import com.droiddeck.launcher.BuildConfig
import com.droiddeck.launcher.R
import com.droiddeck.launcher.core.Downloader
import com.droiddeck.launcher.core.FileUtils
import com.droiddeck.launcher.core.Hashes
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL
import java.security.MessageDigest
object SelfInstaller {
@@ -30,32 +28,12 @@ object SelfInstaller {
fun download(context: Context, apk: AppUpdates.Apk, progress: (Int) -> Unit): File {
val dir = File(context.cacheDir, "updates")
FileUtils.delete(dir)
dir.listFiles()?.forEach { if (it.name != apk.name) FileUtils.delete(it) }
dir.mkdirs()
val target = File(dir, apk.name)
val c = URL(apk.url).openConnection() as HttpURLConnection
c.connectTimeout = 15_000
c.readTimeout = 60_000
c.setRequestProperty("User-Agent", "DroidDeck-app")
c.instanceFollowRedirects = true
val ok = Downloader.downloadFile(apk.url, target, true, apk.sha256) { f -> progress(if (f < 0) -1 else (f * 100).toInt()) }
if (!ok) throw IOException(context.getString(R.string.selfinst_download_failed))
try {
if (c.responseCode != 200) throw IOException(context.getString(R.string.selfinst_http, c.responseCode))
val total = c.contentLengthLong.takeIf { it > 0 } ?: apk.size
c.inputStream.use { input ->
FileOutputStream(target).use { out ->
val buf = ByteArray(1 shl 16)
var done = 0L
var last = -1
while (true) {
val r = input.read(buf)
if (r <= 0) break
out.write(buf, 0, r)
done += r
val pct = if (total > 0) (done * 100 / total).toInt() else -1
if (pct != last) { last = pct; progress(pct) }
}
}
}
if (apk.size > 0 && target.length() != apk.size) {
throw IOException(context.getString(R.string.selfinst_size_mismatch))
}
@@ -63,13 +41,11 @@ object SelfInstaller {
throw IOException(context.getString(R.string.selfinst_checksum_mismatch))
}
validateDownloaded(context, target, apk)
return target
} catch (e: Exception) {
FileUtils.delete(target)
throw e
} finally {
c.disconnect()
}
return target
}
private fun validateDownloaded(context: Context, file: File, apk: AppUpdates.Apk) {