forked from geode-sdk/android-launcher
isolate launcher download from release fetch
This commit is contained in:
3 files changed
+32
-37
No files matched your search
@@ -114,7 +114,7 @@ object LauncherUpdater {
|
||||
_uiState.emit(LauncherUpdateState.Inactive)
|
||||
}
|
||||
|
||||
suspend fun downloadUpdate(context: Context): File? {
|
||||
suspend fun downloadUpdate(context: Context): Result<File> {
|
||||
_uiState.emit(LauncherUpdateState.Downloading)
|
||||
|
||||
return withContext(Dispatchers.IO) {
|
||||
@@ -188,25 +188,31 @@ object LauncherUpdater {
|
||||
|
||||
suspend fun installLauncherUpdate(context: Context) {
|
||||
val launcherDownload = downloadUpdate(context)
|
||||
|
||||
if (launcherDownload != null) {
|
||||
if (!launcherDownload.exists()) {
|
||||
.getOrElse { e ->
|
||||
e.printStackTrace()
|
||||
Toast.makeText(
|
||||
context, context.getString(
|
||||
R.string.launcher_update_download_failed,
|
||||
e.cause
|
||||
), Toast.LENGTH_SHORT
|
||||
).show()
|
||||
return
|
||||
}
|
||||
|
||||
PreferenceUtils.get(context).setBoolean(PreferenceUtils.Key.CLEANUP_APKS, true)
|
||||
if (!launcherDownload.exists()) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
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()
|
||||
PreferenceUtils.get(context).setBoolean(PreferenceUtils.Key.CLEANUP_APKS, true)
|
||||
|
||||
LauncherUpdater.resetState()
|
||||
}
|
||||
} else {
|
||||
Toast.makeText(context, context.getString(R.string.release_fetch_no_releases), Toast.LENGTH_SHORT).show()
|
||||
try {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,8 +220,6 @@ suspend fun installLauncherUpdate(context: Context) {
|
||||
fun LauncherUpdateDialog(isCancelling: Boolean, onDismiss: () -> Unit) {
|
||||
val updateState by LauncherUpdater.uiState.collectAsState()
|
||||
|
||||
val cancellable = updateState != LauncherUpdater.LauncherUpdateState.Active
|
||||
|
||||
LaunchedEffect(updateState) {
|
||||
if (updateState == LauncherUpdater.LauncherUpdateState.Finished) {
|
||||
onDismiss()
|
||||
@@ -242,7 +246,7 @@ fun LauncherUpdateDialog(isCancelling: Boolean, onDismiss: () -> Unit) {
|
||||
LinearProgressIndicator()
|
||||
},
|
||||
confirmButton = {
|
||||
TextButton(onClick = onDismiss, enabled = cancellable && !isCancelling) {
|
||||
TextButton(onClick = onDismiss, enabled = !isCancelling) {
|
||||
Text(stringResource(R.string.message_box_cancel))
|
||||
}
|
||||
},
|
||||
|
||||
@@ -21,6 +21,7 @@ import okhttp3.OkHttpClient
|
||||
import okio.buffer
|
||||
import okio.source
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.InterruptedIOException
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
@@ -136,38 +137,27 @@ class ReleaseManager private constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun downloadLauncherUpdate(release: Downloadable): File? {
|
||||
val download = release.getDownload() ?: return null
|
||||
private suspend fun downloadLauncherUpdate(release: Downloadable): Result<File> {
|
||||
val download = release.getDownload()
|
||||
?: return Result.failure(FileNotFoundException())
|
||||
|
||||
val outputDirectory = applicationContext.filesDir
|
||||
val outputFile = File(outputDirectory, download.filename)
|
||||
|
||||
/*
|
||||
if (outputFile.exists()) {
|
||||
// only download the apk once
|
||||
return
|
||||
}
|
||||
*/
|
||||
|
||||
_uiState.value = ReleaseManagerState.InDownload(0, download.size)
|
||||
|
||||
try {
|
||||
DownloadUtils.downloadStream(
|
||||
httpClient,
|
||||
download.url
|
||||
) { progress, outOf ->
|
||||
_uiState.value = ReleaseManagerState.InDownload(progress, outOf)
|
||||
}.use { fileStream ->
|
||||
).use { fileStream ->
|
||||
outputFile.outputStream().use {
|
||||
fileStream.copyTo(it)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
sendError(e)
|
||||
return null
|
||||
return Result.failure(e)
|
||||
}
|
||||
|
||||
return outputFile
|
||||
return Result.success(outputFile)
|
||||
}
|
||||
|
||||
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
|
||||
?: getLatestLauncherUpdate()
|
||||
?: return null
|
||||
?: return Result.failure(FileNotFoundException())
|
||||
|
||||
return downloadLauncherUpdate(update)
|
||||
}
|
||||
|
||||
@@ -70,6 +70,7 @@
|
||||
<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_update_download_failed">Downloading update failed: %1$s</string>
|
||||
<string name="launcher_self_update_progress">Installing update…</string>
|
||||
|
||||
<string name="launcher_starting_game">Starting Geometry Dash…</string>
|
||||
|
||||
Reference in new issue
Block a user