From 009c49cd8941db28246d83b720048ad3a70438e9 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Sun, 28 Apr 2024 14:28:52 +0800 Subject: [PATCH] Don't fail silently if can't install update --- .../com/hippo/ehviewer/util/LauncherUtils.kt | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/util/LauncherUtils.kt b/app/src/main/kotlin/com/hippo/ehviewer/util/LauncherUtils.kt index 675d7f0e0..6e74ebbd7 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/util/LauncherUtils.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/util/LauncherUtils.kt @@ -21,6 +21,7 @@ import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleEventObserver import androidx.lifecycle.LifecycleOwner import arrow.atomic.AtomicInt +import com.hippo.ehviewer.R import eu.kanade.tachiyomi.util.lang.withUIContext import java.io.File import kotlin.coroutines.resume @@ -93,15 +94,12 @@ suspend fun Context.requestInstallPermission(): Boolean { suspend fun Context.installPackage(file: File) { val canInstall = !isAtLeastO || requestInstallPermission() - if (canInstall) { - val contentUri = FileProvider.getUriForFile(this, "$packageName.fileprovider", file) - val intent = Intent(Intent.ACTION_VIEW).apply { - addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - setDataAndType(contentUri, "application/vnd.android.package-archive") - } - if (packageManager.queryIntentActivities(intent, 0).size > 0) { - startActivity(intent) - } + check(canInstall) { getString(R.string.permission_denied) } + val contentUri = FileProvider.getUriForFile(this, "$packageName.fileprovider", file) + val intent = Intent(Intent.ACTION_VIEW).apply { + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + setDataAndType(contentUri, "application/vnd.android.package-archive") } + startActivity(intent) }