app: show in-place progress during image patching

Wrap PatchFileClassifier.openStream() in ProgressInputStream via
openProgressStream() in MagiskInstallImpl to emit real-time transfer
progress and EMA-smoothed speed during Tar and RecoveryGpt processing.
Also update OtaPayloadExtractor to emit operation download progress
in-place.

Render console lines starting with '\r' in-place in TerminalEmulator
using ANSI erase-in-line ("\r\u001b[2K") and exclude them from saved
logItems in FlashViewModel.

Also fix single-byte read recursion and track skipped bytes in
ProgressInputStream.

Assisted-by: Gemini 3.8 Flash
This commit is contained in:
topjohnwu
2026-10-09 06:22:04 +00:00
parent b268b361f0
commit 79bf48d768
5 changed files with 56 additions and 8 deletions
@@ -20,7 +20,12 @@ fun TerminalEmulator.appendOnMain(bytes: ByteArray, len: Int) {
}
fun TerminalEmulator.appendLineOnMain(line: String) {
val bytes = "$line\r\n".toByteArray(Charsets.UTF_8)
val text = if (line.startsWith('\r')) {
"\r\u001b[2K${line.substring(1)}"
} else {
"\r\u001b[2K$line\r\n"
}
val bytes = text.toByteArray(Charsets.UTF_8)
appendOnMain(bytes, bytes.size)
}
@@ -62,7 +62,9 @@ class FlashViewModel : BaseViewModel() {
override fun onAddElement(e: String?) {
e ?: return
emulator?.appendLineOnMain(e)
logItems.add(e)
if (!e.startsWith('\r')) {
logItems.add(e)
}
}
}
@@ -21,6 +21,7 @@ import com.topjohnwu.magisk.core.utils.DummyList
import com.topjohnwu.magisk.core.utils.MediaStoreUtils
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.openFd
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream
import com.topjohnwu.magisk.core.utils.ProgressInputStream
import com.topjohnwu.magisk.core.utils.RootUtils
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.ShellUtils
@@ -219,6 +220,38 @@ abstract class MagiskInstallImpl protected constructor(
return processInput(input)
}
private fun PatchFileClassifier.openProgressStream(): InputStream {
val max = size
val total = max.toFloat() / 1048576
var prevBytes = 0L
var prevTime = 0L
var smoothedSpeed = -1f
return ProgressInputStream(openStream()) { bytes ->
val now = System.currentTimeMillis()
val dt = now - prevTime
if (prevTime == 0L || dt <= 0) {
prevBytes = bytes
prevTime = now
return@ProgressInputStream
}
val instantSpeed = (bytes - prevBytes).toFloat() * 1000 / dt / 1048576
smoothedSpeed = if (smoothedSpeed < 0f) {
instantSpeed
} else {
0.3f * instantSpeed + 0.7f * smoothedSpeed
}
prevBytes = bytes
prevTime = now
val progress = bytes.toFloat() / 1048576
if (max > 0) {
val pct = bytes * 100 / max
console.add("\r%.2f / %.2f MB (%d%%) - %.2f MB/s".format(progress, total, pct, smoothedSpeed))
} else {
console.add("\r%.2f MB / ?? - %.2f MB/s".format(progress, smoothedSpeed))
}
}
}
private suspend fun processInput(input: DataChannel): Boolean {
try {
input.use {
@@ -226,13 +259,13 @@ abstract class MagiskInstallImpl protected constructor(
logs.add("Input type: ${file.type}, payload: ${file.entryPath ?: "<input>"}")
return when (file.type) {
PatchFileClassifier.Type.Tar -> {
processWholeFile(file.openStream(), "tar") { out ->
processWholeFile(file.openProgressStream(), "tar") { out ->
TarProcessor(installDir, out, console, logs)
}
}
PatchFileClassifier.Type.RecoveryGpt -> {
file.entryPath?.let { console.add("- Processing $it") }
processWholeFile(file.openStream(), "bin") { out ->
processWholeFile(file.openProgressStream(), "bin") { out ->
RecoveryGptProcessor(installDir, out, console, logs)
}
}
@@ -297,6 +330,7 @@ abstract class MagiskInstallImpl protected constructor(
patcher.finish(newBoot)
newBoot.delete()
} catch (e: IOException) {
patcher.close()
console.add("! Failed to output to $outFile")
outFile.delete()
Timber.e(e)
@@ -124,9 +124,7 @@ class OtaPayloadExtractor(
val count = partition.operations.size
partition.operations.forEachIndexed { index, operation ->
if (index % 5 == 0 || index == count - 1) {
console.add("- Downloading ${index + 1}/$count")
}
console.add("\r- Downloading ${index + 1}/$count")
processOperation(outChannel, operation, channel, dataBase, manifest.block_size ?: 4096)
}
@@ -20,7 +20,7 @@ class ProgressInputStream(
}
override fun read(): Int {
val b = read()
val b = super.read()
if (b >= 0) {
bytesRead++
emitProgress()
@@ -41,6 +41,15 @@ class ProgressInputStream(
return sz
}
override fun skip(n: Long): Long {
val sz = super.skip(n)
if (sz > 0) {
bytesRead += sz
emitProgress()
}
return sz
}
override fun close() {
super.close()
progressEmitter(bytesRead)