PackageInstaller.android.kt
composeApp/src/androidMain/kotlin/eu/friedcutlet/yokan/services/updates/PackageInstaller.android.kt
1package eu.friedcutlet.yokan.services.updates23import android.content.Intent4import android.net.Uri5import android.os.Build6import androidx.core.content.FileProvider7import eu.friedcutlet.yokan.AndroidAppContext8import eu.friedcutlet.yokan.services.logging.AppLogger9import java.io.File1011actual fun installPackageFromFile(path: String): Boolean {12 val context = AndroidAppContext.appContext ?: return false13 if (path.isBlank()) return false14 val file = File(path.removePrefix("file://"))15 if (!file.exists() || !file.isFile) return false16 return runCatching {17 val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {18 val authority = "${context.packageName}.fileprovider"19 runCatching { FileProvider.getUriForFile(context, authority, file) }20 .getOrElse { Uri.fromFile(file) }21 } else {22 Uri.fromFile(file)23 }24 val intent = Intent(Intent.ACTION_VIEW).apply {25 setDataAndType(uri, "application/vnd.android.package-archive")26 addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)27 addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)28 }29 context.startActivity(intent)30 true31 }.onFailure { AppLogger.warn("[update] launching package installer failed: ${it.message?.lineSequence()?.firstOrNull() ?: it::class.simpleName}") }32 .getOrDefault(false)33}34