Skip to content

Commit

Permalink
fix(installer): sign and install on threads
Browse files Browse the repository at this point in the history
This is needed to avoid ANRs because it takes a while if the Apk is 100+
MB.
  • Loading branch information
Axelen123 committed Jul 7, 2023
1 parent fe5e191 commit 4ae9904
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.map
import androidx.lifecycle.viewModelScope
import androidx.work.WorkInfo
import androidx.work.WorkManager
import app.revanced.manager.domain.manager.KeystoreManager
Expand All @@ -32,8 +33,11 @@ import app.revanced.manager.util.toast
import app.revanced.patcher.logging.Logger
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.io.File
Expand Down Expand Up @@ -146,10 +150,12 @@ class InstallerViewModel(input: Destination.Installer) : ViewModel(), KoinCompon
signedFile.delete()
}

private fun signApk(): Boolean {
private suspend fun signApk(): Boolean {
if (!hasSigned) {
try {
keystoreManager.sign(outputFile, signedFile)
withContext(Dispatchers.Default) {
keystoreManager.sign(outputFile, signedFile)
}
} catch (e: Exception) {
Log.e(tag, "Got exception while signing", e)
app.toast(app.getString(R.string.sign_fail, e::class.simpleName))
Expand All @@ -160,22 +166,27 @@ class InstallerViewModel(input: Destination.Installer) : ViewModel(), KoinCompon
return true
}

fun export(uri: Uri?) = uri?.let {
if (signApk()) {
Files.copy(signedFile.toPath(), app.contentResolver.openOutputStream(it))
app.toast(app.getString(R.string.export_app_success))
fun export(uri: Uri?) = viewModelScope.launch {
uri?.let {
if (signApk()) {
withContext(Dispatchers.IO) {
app.contentResolver.openOutputStream(it)
.use { stream -> Files.copy(signedFile.toPath(), stream) }
}
app.toast(app.getString(R.string.export_app_success))
}
}
}

fun installOrOpen() {
fun installOrOpen() = viewModelScope.launch {
installedPackageName?.let {
pm.launch(it)
return
return@launch
}

isInstalling = true
try {
if (!signApk()) return
if (!signApk()) return@launch
pm.installApp(listOf(signedFile))
} finally {
isInstalling = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class UpdateProgressViewModel(
}
}

fun installUpdate() {
fun installUpdate() = viewModelScope.launch {
pm.installApp(listOf(location))
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/app/revanced/manager/util/PM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class PM(
})
}

fun installApp(apks: List<File>) {
suspend fun installApp(apks: List<File>) = withContext(Dispatchers.IO) {
val packageInstaller = app.packageManager.packageInstaller
packageInstaller.openSession(packageInstaller.createSession(sessionParams)).use { session ->
apks.forEach { apk -> session.writeApk(apk) }
Expand Down

0 comments on commit 4ae9904

Please sign in to comment.