45 lines
1.5 KiB
Kotlin
45 lines
1.5 KiB
Kotlin
package com.openless.app
|
|
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.net.Uri
|
|
import android.os.Build
|
|
import android.provider.Settings
|
|
import androidx.annotation.Keep
|
|
import androidx.core.content.FileProvider
|
|
import java.io.File
|
|
|
|
/** Triggers system package installer for a downloaded APK via FileProvider. */
|
|
@Keep
|
|
object OpenLessUpdateInstaller {
|
|
@Keep
|
|
@JvmStatic
|
|
fun installApk(context: Context, apkPath: String): Boolean {
|
|
val apkFile = File(apkPath)
|
|
if (!apkFile.exists()) {
|
|
return false
|
|
}
|
|
if (
|
|
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
|
|
!context.packageManager.canRequestPackageInstalls()
|
|
) {
|
|
val intent =
|
|
Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).apply {
|
|
data = Uri.parse("package:${context.packageName}")
|
|
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
}
|
|
context.startActivity(intent)
|
|
return false
|
|
}
|
|
val authority = "${context.packageName}.fileprovider"
|
|
val uri: Uri = FileProvider.getUriForFile(context, authority, apkFile)
|
|
val install =
|
|
Intent(Intent.ACTION_VIEW).apply {
|
|
setDataAndType(uri, "application/vnd.android.package-archive")
|
|
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
}
|
|
context.startActivity(install)
|
|
return true
|
|
}
|
|
}
|