android tv for updates support
This commit is contained in:
+113
-16
@@ -1,25 +1,37 @@
|
||||
package ru.shadowsparky.updater.data
|
||||
|
||||
import android.app.Application
|
||||
import android.app.PendingIntent
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.content.pm.PackageInstaller
|
||||
import android.net.Uri
|
||||
import android.provider.Settings
|
||||
import androidx.core.content.FileProvider
|
||||
import androidx.core.net.toUri
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.content.IntentCompat
|
||||
import kotlinx.coroutines.CancellableContinuation
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import org.koin.core.annotation.Factory
|
||||
import ru.shadowsparky.updater.domain.UpdateInstaller
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.resumeWithException
|
||||
|
||||
@Factory
|
||||
class AndroidUpdateInstaller(private val app: Application) : UpdateInstaller {
|
||||
class AndroidUpdateInstaller(
|
||||
private val app: Application
|
||||
) : UpdateInstaller {
|
||||
private val actionInstallStatus = "${app.packageName}.ACTION_INSTALL_STATUS"
|
||||
|
||||
override suspend fun hasInstallPermission(): Boolean {
|
||||
return app.packageManager.canRequestPackageInstalls()
|
||||
}
|
||||
override suspend fun hasInstallPermission(): Boolean =
|
||||
app.packageManager.canRequestPackageInstalls()
|
||||
|
||||
override suspend fun requestInstallPermission() {
|
||||
val intent = Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).apply {
|
||||
data = "package:${app.packageName}".toUri()
|
||||
data = Uri.parse("package:${app.packageName}")
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
}
|
||||
app.startActivity(intent)
|
||||
@@ -27,17 +39,102 @@ class AndroidUpdateInstaller(private val app: Application) : UpdateInstaller {
|
||||
|
||||
override suspend fun install(path: String) {
|
||||
val file = File(path)
|
||||
if (!file.exists()) return
|
||||
val intent = Intent(Intent.ACTION_VIEW).apply {
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
if (!file.exists()) {
|
||||
error("APK file not found at: $path")
|
||||
}
|
||||
val uri: Uri = FileProvider.getUriForFile(
|
||||
|
||||
val packageInstaller = app.packageManager.packageInstaller
|
||||
val sessionId = createSession(packageInstaller)
|
||||
try {
|
||||
writeApkToSession(packageInstaller, sessionId, file)
|
||||
awaitInstallationResult(packageInstaller, sessionId)
|
||||
} catch (e: Exception) {
|
||||
runCatching { packageInstaller.abandonSession(sessionId) }
|
||||
throw e
|
||||
} finally {
|
||||
file.delete()
|
||||
}
|
||||
}
|
||||
|
||||
private fun createSession(packageInstaller: PackageInstaller): Int {
|
||||
val params = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
|
||||
return packageInstaller.createSession(params)
|
||||
}
|
||||
|
||||
private fun writeApkToSession(packageInstaller: PackageInstaller, sessionId: Int, file: File) {
|
||||
packageInstaller.openSession(sessionId).use { session ->
|
||||
session.openWrite("update_stream", 0, file.length()).use { outputStream ->
|
||||
FileInputStream(file).use { inputStream ->
|
||||
val buffer = ByteArray(65536)
|
||||
var read: Int
|
||||
while (inputStream.read(buffer).also { read = it } != -1) {
|
||||
outputStream.write(buffer, 0, read)
|
||||
}
|
||||
session.fsync(outputStream)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun awaitInstallationResult(packageInstaller: PackageInstaller, sessionId: Int) {
|
||||
suspendCancellableCoroutine<Unit> { continuation ->
|
||||
val receiver = createStatusReceiver(continuation)
|
||||
registerStatusReceiver(receiver)
|
||||
|
||||
continuation.invokeOnCancellation {
|
||||
runCatching { app.unregisterReceiver(receiver) }
|
||||
runCatching { packageInstaller.abandonSession(sessionId) }
|
||||
}
|
||||
|
||||
val intent = Intent(actionInstallStatus)
|
||||
.setPackage(app.packageName)
|
||||
.addFlags(Intent.FLAG_RECEIVER_FOREGROUND)
|
||||
|
||||
val pendingIntent = PendingIntent.getBroadcast(
|
||||
app,
|
||||
sessionId,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
|
||||
)
|
||||
packageInstaller.openSession(sessionId).use { session ->
|
||||
session.commit(pendingIntent.intentSender)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun createStatusReceiver(
|
||||
continuation: CancellableContinuation<Unit>
|
||||
): BroadcastReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE)
|
||||
val message = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE)
|
||||
|
||||
when (status) {
|
||||
PackageInstaller.STATUS_PENDING_USER_ACTION -> {
|
||||
val userAction = IntentCompat.getParcelableExtra(intent, Intent.EXTRA_INTENT, Intent::class.java)
|
||||
userAction?.let {
|
||||
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
context.startActivity(it)
|
||||
}
|
||||
}
|
||||
PackageInstaller.STATUS_SUCCESS -> {
|
||||
app.unregisterReceiver(this)
|
||||
continuation.resume(Unit)
|
||||
}
|
||||
else -> {
|
||||
app.unregisterReceiver(this)
|
||||
continuation.resumeWithException(IllegalStateException("Installation failed ($status): $message"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun registerStatusReceiver(receiver: BroadcastReceiver) {
|
||||
ContextCompat.registerReceiver(
|
||||
app,
|
||||
"${app.packageName}.provider",
|
||||
file
|
||||
receiver,
|
||||
IntentFilter(actionInstallStatus),
|
||||
ContextCompat.RECEIVER_NOT_EXPORTED
|
||||
)
|
||||
intent.setDataAndType(uri, "application/vnd.android.package-archive")
|
||||
app.startActivity(intent)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user