fix(installer): progress tracking

This commit is contained in:
Ax333l 2023-07-31 12:14:51 +02:00
parent 6beb34baa8
commit 272d911464
3 changed files with 23 additions and 50 deletions

View file

@ -1,8 +1,5 @@
package app.revanced.manager.patcher package app.revanced.manager.patcher
import android.util.Log
import app.revanced.manager.patcher.worker.Progress
import app.revanced.manager.util.tag
import app.revanced.patcher.Patcher import app.revanced.patcher.Patcher
import app.revanced.patcher.PatcherOptions import app.revanced.patcher.PatcherOptions
import app.revanced.patcher.data.Context import app.revanced.patcher.data.Context
@ -24,7 +21,7 @@ class Session(
aaptPath: String, aaptPath: String,
private val logger: Logger, private val logger: Logger,
private val input: File, private val input: File,
private val onProgress: suspend (Progress) -> Unit = { } private val onStepSucceeded: suspend () -> Unit
) : Closeable { ) : Closeable {
private val temporary = File(cacheDir).resolve("manager").also { it.mkdirs() } private val temporary = File(cacheDir).resolve("manager").also { it.mkdirs() }
private val patcher = Patcher( private val patcher = Patcher(
@ -41,7 +38,7 @@ class Session(
this.executePatches(true).forEach { (patch, result) -> this.executePatches(true).forEach { (patch, result) ->
if (result.isSuccess) { if (result.isSuccess) {
logger.info("$patch succeeded") logger.info("$patch succeeded")
onProgress(Progress.PatchSuccess(patch)) onStepSucceeded()
return@forEach return@forEach
} }
logger.error("$patch failed:") logger.error("$patch failed:")
@ -54,20 +51,17 @@ class Session(
} }
suspend fun run(output: File, selectedPatches: PatchList, integrations: List<File>) { suspend fun run(output: File, selectedPatches: PatchList, integrations: List<File>) {
onProgress(Progress.Merging) onStepSucceeded() // Unpacking
with(patcher) { with(patcher) {
logger.info("Merging integrations") logger.info("Merging integrations")
addIntegrations(integrations) {} addIntegrations(integrations) {}
addPatches(selectedPatches) addPatches(selectedPatches)
onStepSucceeded() // Merging
logger.info("Applying patches...") logger.info("Applying patches...")
onProgress(Progress.PatchingStart)
applyPatchesVerbose() applyPatchesVerbose()
} }
onProgress(Progress.Saving)
logger.info("Writing patched files...") logger.info("Writing patched files...")
val result = patcher.save() val result = patcher.save()
@ -78,6 +72,7 @@ class Session(
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
Files.move(aligned.toPath(), output.toPath(), StandardCopyOption.REPLACE_EXISTING) Files.move(aligned.toPath(), output.toPath(), StandardCopyOption.REPLACE_EXISTING)
} }
onStepSucceeded() // Saving
} }
override fun close() { override fun close() {

View file

@ -9,17 +9,6 @@ import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
sealed class Progress {
object Downloading : Progress()
object Unpacking : Progress()
object Merging : Progress()
object PatchingStart : Progress()
data class PatchSuccess(val patchName: String) : Progress()
object Saving : Progress()
}
enum class State { enum class State {
WAITING, COMPLETED, FAILED WAITING, COMPLETED, FAILED
} }
@ -76,20 +65,13 @@ class PatcherProgressManager(context: Context, selectedPatches: List<String>, se
} }
fun replacePatchesList(newList: List<String>) { fun replacePatchesList(newList: List<String>) {
steps[stepKeyMap[Progress.PatchingStart]!!.step] = generatePatchesStep(newList) steps[1] = generatePatchesStep(newList)
} }
private fun updateCurrent(newState: State, message: String? = null) { private fun updateCurrent(newState: State, message: String? = null) {
currentStep?.let { update(it, newState, message) } currentStep?.let { update(it, newState, message) }
} }
fun handle(progress: Progress) = when (val step = stepKeyMap[progress]) {
null -> success()
currentStep -> {}
else -> success().also { currentStep = step }
}
fun failure(error: Throwable) = updateCurrent( fun failure(error: Throwable) = updateCurrent(
State.FAILED, State.FAILED,
error.stackTraceToString() error.stackTraceToString()
@ -100,17 +82,6 @@ class PatcherProgressManager(context: Context, selectedPatches: List<String>, se
fun getProgress(): List<Step> = steps fun getProgress(): List<Step> = steps
companion object { companion object {
/**
* A map of [Progress] to the corresponding position in [steps]
*/
private val stepKeyMap = mapOf(
//Progress.Downloading to StepKey(0, 1),
//Progress.Unpacking to StepKey(0, 2),
//Progress.Merging to StepKey(0, 3),
Progress.PatchingStart to StepKey(1, 0),
//Progress.Saving to StepKey(2, 0),
)
private fun generatePatchesStep(selectedPatches: List<String>) = Step( private fun generatePatchesStep(selectedPatches: List<String>) = Step(
R.string.patcher_step_group_patching, R.string.patcher_step_group_patching,
selectedPatches.map { SubStep(it) }.toImmutableList() selectedPatches.map { SubStep(it) }.toImmutableList()

View file

@ -130,12 +130,19 @@ class PatcherWorker(
val downloadProgress = MutableStateFlow<Pair<Float, Float>?>(null) val downloadProgress = MutableStateFlow<Pair<Float, Float>?>(null)
val progressManager = val progressManager =
PatcherProgressManager(applicationContext, args.selectedPatches.flatMap { it.value }, args.input, downloadProgress) PatcherProgressManager(
applicationContext,
args.selectedPatches.flatMap { it.value },
args.input,
downloadProgress
)
val progressFlow = args.progress val progressFlow = args.progress
fun updateProgress(progress: Progress?) { fun updateProgress(advanceCounter: Boolean = true) {
progress?.let { progressManager.handle(it) } if (advanceCounter) {
progressManager.success()
}
progressFlow.value = progressManager.getProgress().toImmutableList() progressFlow.value = progressManager.getProgress().toImmutableList()
} }
@ -165,12 +172,12 @@ class PatcherWorker(
// Ensure they are in the correct order so we can track progress properly. // Ensure they are in the correct order so we can track progress properly.
progressManager.replacePatchesList(patches.map { it.patchName }) progressManager.replacePatchesList(patches.map { it.patchName })
updateProgress() // Loading patches
val inputFile = when (val selectedApp = args.input) { val inputFile = when (val selectedApp = args.input) {
is SelectedApp.Download -> { is SelectedApp.Download -> {
updateProgress(Progress.Downloading) val savePath = applicationContext.filesDir.resolve("downloaded-apps")
.resolve(args.input.packageName).also { it.mkdirs() }
val savePath = applicationContext.filesDir.resolve("downloaded-apps").resolve(args.input.packageName).also { it.mkdirs() }
selectedApp.app.download( selectedApp.app.download(
savePath, savePath,
@ -182,21 +189,21 @@ class PatcherWorker(
args.input.version, args.input.version,
it it
) )
updateProgress() // Downloading
} }
} }
is SelectedApp.Local -> selectedApp.file is SelectedApp.Local -> selectedApp.file
is SelectedApp.Installed -> File(pm.getPackageInfo(selectedApp.packageName)!!.applicationInfo.sourceDir) is SelectedApp.Installed -> File(pm.getPackageInfo(selectedApp.packageName)!!.applicationInfo.sourceDir)
} }
updateProgress(Progress.Unpacking)
Session( Session(
applicationContext.cacheDir.absolutePath, applicationContext.cacheDir.absolutePath,
frameworkPath, frameworkPath,
aaptPath, aaptPath,
args.logger, args.logger,
inputFile, inputFile,
onProgress = { updateProgress(it) } onStepSucceeded = ::updateProgress
).use { session -> ).use { session ->
session.run(File(args.output), patches, integrations) session.run(File(args.output), patches, integrations)
} }
@ -209,7 +216,7 @@ class PatcherWorker(
progressManager.failure(e) progressManager.failure(e)
Result.failure() Result.failure()
} finally { } finally {
updateProgress(null) updateProgress(false)
} }
} }
} }