isolate launcher download from release fetch

This commit is contained in:
Chloe committed 2026-02-15 04:29:08 -07:00
1 parent eab0de3a34
commit e6bac1e25a
3 files changed
+32 -37

No files matched your search

@@ -114,7 +114,7 @@ object LauncherUpdater {
_uiState.emit(LauncherUpdateState.Inactive) _uiState.emit(LauncherUpdateState.Inactive)
} }
suspend fun downloadUpdate(context: Context): File? { suspend fun downloadUpdate(context: Context): Result<File> {
_uiState.emit(LauncherUpdateState.Downloading) _uiState.emit(LauncherUpdateState.Downloading)
return withContext(Dispatchers.IO) { return withContext(Dispatchers.IO) {
@@ -188,25 +188,31 @@ object LauncherUpdater {
suspend fun installLauncherUpdate(context: Context) { suspend fun installLauncherUpdate(context: Context) {
val launcherDownload = downloadUpdate(context) val launcherDownload = downloadUpdate(context)
.getOrElse { e ->
if (launcherDownload != null) { e.printStackTrace()
if (!launcherDownload.exists()) { Toast.makeText(
context, context.getString(
R.string.launcher_update_download_failed,
e.cause
), Toast.LENGTH_SHORT
).show()
return return
} }
PreferenceUtils.get(context).setBoolean(PreferenceUtils.Key.CLEANUP_APKS, true) if (!launcherDownload.exists()) {
return
}
try { PreferenceUtils.get(context).setBoolean(PreferenceUtils.Key.CLEANUP_APKS, true)
LauncherUpdater.installPackage(context, launcherDownload)
} catch (e: Exception) {
e.printStackTrace()
val msg = context.getString(R.string.launcher_self_update_failed, e.message)
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
LauncherUpdater.resetState() try {
} LauncherUpdater.installPackage(context, launcherDownload)
} else { } catch (e: Exception) {
Toast.makeText(context, context.getString(R.string.release_fetch_no_releases), Toast.LENGTH_SHORT).show() e.printStackTrace()
val msg = context.getString(R.string.launcher_self_update_failed, e.message)
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
LauncherUpdater.resetState()
} }
} }
@@ -214,8 +220,6 @@ suspend fun installLauncherUpdate(context: Context) {
fun LauncherUpdateDialog(isCancelling: Boolean, onDismiss: () -> Unit) { fun LauncherUpdateDialog(isCancelling: Boolean, onDismiss: () -> Unit) {
val updateState by LauncherUpdater.uiState.collectAsState() val updateState by LauncherUpdater.uiState.collectAsState()
val cancellable = updateState != LauncherUpdater.LauncherUpdateState.Active
LaunchedEffect(updateState) { LaunchedEffect(updateState) {
if (updateState == LauncherUpdater.LauncherUpdateState.Finished) { if (updateState == LauncherUpdater.LauncherUpdateState.Finished) {
onDismiss() onDismiss()
@@ -242,7 +246,7 @@ fun LauncherUpdateDialog(isCancelling: Boolean, onDismiss: () -> Unit) {
LinearProgressIndicator() LinearProgressIndicator()
}, },
confirmButton = { confirmButton = {
TextButton(onClick = onDismiss, enabled = cancellable && !isCancelling) { TextButton(onClick = onDismiss, enabled = !isCancelling) {
Text(stringResource(R.string.message_box_cancel)) Text(stringResource(R.string.message_box_cancel))
} }
}, },
@@ -21,6 +21,7 @@ import okhttp3.OkHttpClient
import okio.buffer import okio.buffer
import okio.source import okio.source
import java.io.File import java.io.File
import java.io.FileNotFoundException
import java.io.InterruptedIOException import java.io.InterruptedIOException
import kotlin.time.Clock import kotlin.time.Clock
import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.minutes
@@ -136,38 +137,27 @@ class ReleaseManager private constructor(
} }
} }
private suspend fun downloadLauncherUpdate(release: Downloadable): File? { private suspend fun downloadLauncherUpdate(release: Downloadable): Result<File> {
val download = release.getDownload() ?: return null val download = release.getDownload()
?: return Result.failure(FileNotFoundException())
val outputDirectory = applicationContext.filesDir val outputDirectory = applicationContext.filesDir
val outputFile = File(outputDirectory, download.filename) val outputFile = File(outputDirectory, download.filename)
/*
if (outputFile.exists()) {
// only download the apk once
return
}
*/
_uiState.value = ReleaseManagerState.InDownload(0, download.size)
try { try {
DownloadUtils.downloadStream( DownloadUtils.downloadStream(
httpClient, httpClient,
download.url download.url
) { progress, outOf -> ).use { fileStream ->
_uiState.value = ReleaseManagerState.InDownload(progress, outOf)
}.use { fileStream ->
outputFile.outputStream().use { outputFile.outputStream().use {
fileStream.copyTo(it) fileStream.copyTo(it)
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
sendError(e) return Result.failure(e)
return null
} }
return outputFile return Result.success(outputFile)
} }
private suspend fun performUpdate(release: Downloadable) { private suspend fun performUpdate(release: Downloadable) {
@@ -275,10 +265,10 @@ class ReleaseManager private constructor(
} }
} }
suspend fun downloadLatestLauncherUpdate(): File? { suspend fun downloadLatestLauncherUpdate(): Result<File> {
val update = _availableLauncherUpdateDetails.value val update = _availableLauncherUpdateDetails.value
?: getLatestLauncherUpdate() ?: getLatestLauncherUpdate()
?: return null ?: return Result.failure(FileNotFoundException())
return downloadLauncherUpdate(update) return downloadLauncherUpdate(update)
} }
+1
View File
@@ -70,6 +70,7 @@
<string name="launcher_self_update_failed_title">Update failed</string> <string name="launcher_self_update_failed_title">Update failed</string>
<string name="launcher_self_update_failed">Failed to install launcher update: %1$s.</string> <string name="launcher_self_update_failed">Failed to install launcher update: %1$s.</string>
<string name="launcher_update_download_failed">Downloading update failed: %1$s</string>
<string name="launcher_self_update_progress">Installing update…</string> <string name="launcher_self_update_progress">Installing update…</string>
<string name="launcher_starting_game">Starting Geometry Dash…</string> <string name="launcher_starting_game">Starting Geometry Dash…</string>