package eu.friedcutlet.yokan.services.updates import android.content.Intent import android.net.Uri import android.os.Build import androidx.core.content.FileProvider import eu.friedcutlet.yokan.AndroidAppContext import eu.friedcutlet.yokan.services.logging.AppLogger import java.io.File actual fun installPackageFromFile(path: String): Boolean { val context = AndroidAppContext.appContext ?: return false if (path.isBlank()) return false val file = File(path.removePrefix("file://")) if (!file.exists() || !file.isFile) return false return runCatching { val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { val authority = "${context.packageName}.fileprovider" runCatching { FileProvider.getUriForFile(context, authority, file) } .getOrElse { Uri.fromFile(file) } } else { Uri.fromFile(file) } val intent = Intent(Intent.ACTION_VIEW).apply { setDataAndType(uri, "application/vnd.android.package-archive") addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) } context.startActivity(intent) true }.onFailure { AppLogger.warn("[update] launching package installer failed: ${it.message?.lineSequence()?.firstOrNull() ?: it::class.simpleName}") } .getOrDefault(false) }