Skip to content

Commit 272d911

Browse files
committed
fix(installer): progress tracking
1 parent 6beb34b commit 272d911

3 files changed

Lines changed: 23 additions & 50 deletions

File tree

app/src/main/java/app/revanced/manager/patcher/Session.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package app.revanced.manager.patcher
22

3-
import android.util.Log
4-
import app.revanced.manager.patcher.worker.Progress
5-
import app.revanced.manager.util.tag
63
import app.revanced.patcher.Patcher
74
import app.revanced.patcher.PatcherOptions
85
import app.revanced.patcher.data.Context
@@ -24,7 +21,7 @@ class Session(
2421
aaptPath: String,
2522
private val logger: Logger,
2623
private val input: File,
27-
private val onProgress: suspend (Progress) -> Unit = { }
24+
private val onStepSucceeded: suspend () -> Unit
2825
) : Closeable {
2926
private val temporary = File(cacheDir).resolve("manager").also { it.mkdirs() }
3027
private val patcher = Patcher(
@@ -41,7 +38,7 @@ class Session(
4138
this.executePatches(true).forEach { (patch, result) ->
4239
if (result.isSuccess) {
4340
logger.info("$patch succeeded")
44-
onProgress(Progress.PatchSuccess(patch))
41+
onStepSucceeded()
4542
return@forEach
4643
}
4744
logger.error("$patch failed:")
@@ -54,20 +51,17 @@ class Session(
5451
}
5552

5653
suspend fun run(output: File, selectedPatches: PatchList, integrations: List<File>) {
57-
onProgress(Progress.Merging)
58-
54+
onStepSucceeded() // Unpacking
5955
with(patcher) {
6056
logger.info("Merging integrations")
6157
addIntegrations(integrations) {}
6258
addPatches(selectedPatches)
59+
onStepSucceeded() // Merging
6360

6461
logger.info("Applying patches...")
65-
onProgress(Progress.PatchingStart)
66-
6762
applyPatchesVerbose()
6863
}
6964

70-
onProgress(Progress.Saving)
7165
logger.info("Writing patched files...")
7266
val result = patcher.save()
7367

@@ -78,6 +72,7 @@ class Session(
7872
withContext(Dispatchers.IO) {
7973
Files.move(aligned.toPath(), output.toPath(), StandardCopyOption.REPLACE_EXISTING)
8074
}
75+
onStepSucceeded() // Saving
8176
}
8277

8378
override fun close() {

app/src/main/java/app/revanced/manager/patcher/worker/PatcherProgressManager.kt

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,6 @@ import kotlinx.collections.immutable.persistentListOf
99
import kotlinx.collections.immutable.toImmutableList
1010
import kotlinx.coroutines.flow.StateFlow
1111

12-
sealed class Progress {
13-
object Downloading : Progress()
14-
object Unpacking : Progress()
15-
object Merging : Progress()
16-
object PatchingStart : Progress()
17-
18-
data class PatchSuccess(val patchName: String) : Progress()
19-
20-
object Saving : Progress()
21-
}
22-
2312
enum class State {
2413
WAITING, COMPLETED, FAILED
2514
}
@@ -76,20 +65,13 @@ class PatcherProgressManager(context: Context, selectedPatches: List<String>, se
7665
}
7766

7867
fun replacePatchesList(newList: List<String>) {
79-
steps[stepKeyMap[Progress.PatchingStart]!!.step] = generatePatchesStep(newList)
68+
steps[1] = generatePatchesStep(newList)
8069
}
8170

8271
private fun updateCurrent(newState: State, message: String? = null) {
8372
currentStep?.let { update(it, newState, message) }
8473
}
8574

86-
87-
fun handle(progress: Progress) = when (val step = stepKeyMap[progress]) {
88-
null -> success()
89-
currentStep -> {}
90-
else -> success().also { currentStep = step }
91-
}
92-
9375
fun failure(error: Throwable) = updateCurrent(
9476
State.FAILED,
9577
error.stackTraceToString()
@@ -100,17 +82,6 @@ class PatcherProgressManager(context: Context, selectedPatches: List<String>, se
10082
fun getProgress(): List<Step> = steps
10183

10284
companion object {
103-
/**
104-
* A map of [Progress] to the corresponding position in [steps]
105-
*/
106-
private val stepKeyMap = mapOf(
107-
//Progress.Downloading to StepKey(0, 1),
108-
//Progress.Unpacking to StepKey(0, 2),
109-
//Progress.Merging to StepKey(0, 3),
110-
Progress.PatchingStart to StepKey(1, 0),
111-
//Progress.Saving to StepKey(2, 0),
112-
)
113-
11485
private fun generatePatchesStep(selectedPatches: List<String>) = Step(
11586
R.string.patcher_step_group_patching,
11687
selectedPatches.map { SubStep(it) }.toImmutableList()

app/src/main/java/app/revanced/manager/patcher/worker/PatcherWorker.kt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,19 @@ class PatcherWorker(
130130
val downloadProgress = MutableStateFlow<Pair<Float, Float>?>(null)
131131

132132
val progressManager =
133-
PatcherProgressManager(applicationContext, args.selectedPatches.flatMap { it.value }, args.input, downloadProgress)
133+
PatcherProgressManager(
134+
applicationContext,
135+
args.selectedPatches.flatMap { it.value },
136+
args.input,
137+
downloadProgress
138+
)
134139

135140
val progressFlow = args.progress
136141

137-
fun updateProgress(progress: Progress?) {
138-
progress?.let { progressManager.handle(it) }
142+
fun updateProgress(advanceCounter: Boolean = true) {
143+
if (advanceCounter) {
144+
progressManager.success()
145+
}
139146
progressFlow.value = progressManager.getProgress().toImmutableList()
140147
}
141148

@@ -165,12 +172,12 @@ class PatcherWorker(
165172

166173
// Ensure they are in the correct order so we can track progress properly.
167174
progressManager.replacePatchesList(patches.map { it.patchName })
175+
updateProgress() // Loading patches
168176

169177
val inputFile = when (val selectedApp = args.input) {
170178
is SelectedApp.Download -> {
171-
updateProgress(Progress.Downloading)
172-
173-
val savePath = applicationContext.filesDir.resolve("downloaded-apps").resolve(args.input.packageName).also { it.mkdirs() }
179+
val savePath = applicationContext.filesDir.resolve("downloaded-apps")
180+
.resolve(args.input.packageName).also { it.mkdirs() }
174181

175182
selectedApp.app.download(
176183
savePath,
@@ -182,21 +189,21 @@ class PatcherWorker(
182189
args.input.version,
183190
it
184191
)
192+
updateProgress() // Downloading
185193
}
186194
}
195+
187196
is SelectedApp.Local -> selectedApp.file
188197
is SelectedApp.Installed -> File(pm.getPackageInfo(selectedApp.packageName)!!.applicationInfo.sourceDir)
189198
}
190199

191-
updateProgress(Progress.Unpacking)
192-
193200
Session(
194201
applicationContext.cacheDir.absolutePath,
195202
frameworkPath,
196203
aaptPath,
197204
args.logger,
198205
inputFile,
199-
onProgress = { updateProgress(it) }
206+
onStepSucceeded = ::updateProgress
200207
).use { session ->
201208
session.run(File(args.output), patches, integrations)
202209
}
@@ -209,7 +216,7 @@ class PatcherWorker(
209216
progressManager.failure(e)
210217
Result.failure()
211218
} finally {
212-
updateProgress(null)
219+
updateProgress(false)
213220
}
214221
}
215222
}

0 commit comments

Comments
 (0)