From 2762a0ebf20103a79370f768f45412306f9fb44d Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 15 Oct 2026 00:33:42 +0200 Subject: [PATCH] app: track both read and write progress Add ProgressOutputStream and wrap both the raw input stream and output stream in runWholeFilePatcher so read and write progress and speeds are tracked independently. Render read and write progress on two in-place lines in the terminal to fit narrow screens, and support multi-line in-place updates in appendLineOnMain using ANSI cursor movement and erase-in-display. Assisted-by: Gemini 4 Argon --- .../magisk/terminal/TerminalEmulator.kt | 2 + .../magisk/terminal/TerminalProcess.kt | 23 +++- .../magisk/core/tasks/MagiskInstaller.kt | 117 ++++++++++++------ .../magisk/core/utils/ProgressInputStream.kt | 42 +++++++ 4 files changed, 141 insertions(+), 43 deletions(-) diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/terminal/TerminalEmulator.kt b/app/apk/src/main/java/com/topjohnwu/magisk/terminal/TerminalEmulator.kt index d43984db4..60a91f7cf 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/terminal/TerminalEmulator.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/terminal/TerminalEmulator.kt @@ -133,6 +133,7 @@ class TerminalEmulator( var onCopyToClipboard: ((String) -> Unit)? = null var onScreenUpdate: (() -> Unit)? = null + internal var inPlaceNewlines = 0 private var mArgIndex = 0 private val mArgs = IntArray(MAX_ESCAPE_PARAMETERS) @@ -1559,6 +1560,7 @@ class TerminalEmulator( mUtf8Index = 0 mUtf8ToFollow = 0 + inPlaceNewlines = 0 mColors.reset() } diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/terminal/TerminalProcess.kt b/app/apk/src/main/java/com/topjohnwu/magisk/terminal/TerminalProcess.kt index 1f82b7be3..8f93481c5 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/terminal/TerminalProcess.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/terminal/TerminalProcess.kt @@ -20,13 +20,24 @@ fun TerminalEmulator.appendOnMain(bytes: ByteArray, len: Int) { } fun TerminalEmulator.appendLineOnMain(line: String) { - val text = if (line.startsWith('\r')) { - "\r\u001b[2K${line.substring(1)}" - } else { - "\r\u001b[2K$line\r\n" + mainHandler.post { + val clear = if (inPlaceNewlines > 0) { + "\r\u001b[${inPlaceNewlines}A\u001b[J" + } else { + "\r\u001b[2K" + } + val text = if (line.startsWith('\r')) { + val content = line.substring(1) + inPlaceNewlines = content.count { it == '\n' } + "$clear${content.replace("\n", "\r\n")}" + } else { + inPlaceNewlines = 0 + "$clear$line\r\n" + } + val bytes = text.toByteArray(Charsets.UTF_8) + append(bytes, bytes.size) + onScreenUpdate?.invoke() } - val bytes = text.toByteArray(Charsets.UTF_8) - appendOnMain(bytes, bytes.size) } /** diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt index a55e8a107..272efb49c 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt @@ -22,6 +22,7 @@ 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.ProgressOutputStream import com.topjohnwu.magisk.core.utils.RootUtils import com.topjohnwu.magisk.core.utils.UsbBlockDevice import com.topjohnwu.superuser.Shell @@ -214,37 +215,47 @@ abstract class MagiskInstallImpl protected constructor( return processInput(input, usbDeviceName) } - private fun PatchFileClassifier.openProgressStream(): InputStream { - val max = rawSize - val total = max.toFloat() / 1048576 - var prevBytes = 0L - var prevTime = 0L - var smoothedSpeed = -1f - return openStream { raw -> - ProgressInputStream(raw) { 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 class SpeedTracker { + var bytes = 0L + private set + private var prevBytes = 0L + private var prevTime = 0L + private var smoothedSpeed = -1f + + val mb get() = bytes.toFloat() / 1048576 + + fun speed(now: Long): Float { + if (prevTime == 0L || now - prevTime > 2000) { + return 0f } + return maxOf(0f, smoothedSpeed) + } + + fun update(newBytes: Long): Boolean { + if (newBytes == bytes) return false + bytes = newBytes + val now = System.currentTimeMillis() + val dt = now - prevTime + if (prevTime == 0L || dt <= 0) { + prevBytes = newBytes + prevTime = now + return false + } + val instantSpeed = (newBytes - prevBytes).toFloat() * 1000 / dt / 1048576 + smoothedSpeed = if (smoothedSpeed < 0f) { + instantSpeed + } else { + 0.3f * instantSpeed + 0.7f * smoothedSpeed + } + prevBytes = newBytes + prevTime = now + return true + } + + fun reset() { + prevBytes = bytes + prevTime = 0L + smoothedSpeed = -1f } } @@ -259,7 +270,7 @@ abstract class MagiskInstallImpl protected constructor( } return when (file.type) { PatchFileClassifier.Type.Tar -> { - processWholeFile(file.openProgressStream(), "tar") { out -> + processWholeFile(file, "tar") { out -> TarProcessor(installDir, out, console, logs) } } @@ -268,7 +279,7 @@ abstract class MagiskInstallImpl protected constructor( if (usbDeviceName != null) { processUsbRecovery(file, usbDeviceName) } else { - processWholeFile(file.openProgressStream(), "bin") { out -> + processWholeFile(file, "bin") { out -> RecoveryGptProcessor(installDir, out, console, logs) } } @@ -296,15 +307,42 @@ abstract class MagiskInstallImpl protected constructor( } private suspend fun runWholeFilePatcher( - input: InputStream, - outStream: OutputStream, + file: PatchFileClassifier, + rawOut: OutputStream, outputLabel: String, createPatcher: (OutputStream) -> WholeFilePatcher ): Boolean { + val max = file.rawSize + val total = max.toFloat() / 1048576 + val readTracker = SpeedTracker() + val writeTracker = SpeedTracker() + + fun emitProgress() { + val now = System.currentTimeMillis() + val readSpeed = readTracker.speed(now) + val writeSpeed = writeTracker.speed(now) + val readInfo = if (max > 0) { + val pct = readTracker.bytes * 100 / max + "%.2f / %.2f MB (%d%%) - %.2f MB/s".format(readTracker.mb, total, pct, readSpeed) + } else { + "%.2f MB / ?? - %.2f MB/s".format(readTracker.mb, readSpeed) + } + val writeInfo = "%.2f MB - %.2f MB/s".format(writeTracker.mb, writeSpeed) + console.add("\rRead : $readInfo\nWrite: $writeInfo") + } + + val outStream = ProgressOutputStream(rawOut) { bytes -> + if (writeTracker.update(bytes)) emitProgress() + } val patcher = createPatcher(outStream) // Process input file try { + val input = file.openStream { raw -> + ProgressInputStream(raw) { bytes -> + if (readTracker.update(bytes)) emitProgress() + } + } targetImage = patcher.start(input) } catch (e: IOException) { patcher.close() @@ -323,13 +361,18 @@ abstract class MagiskInstallImpl protected constructor( return false } + readTracker.reset() + writeTracker.reset() + // Output file try { val newBoot = installDir.getChildFile("new-boot.img") patcher.finish(newBoot) newBoot.delete() + outStream.close() } catch (e: IOException) { patcher.close() + runCatching { outStream.close() } console.add("! Failed to output to $outputLabel") Timber.e(e) return false @@ -344,13 +387,13 @@ abstract class MagiskInstallImpl protected constructor( // Patch the boot image in a whole file (e.g. tar archive), and output a new file // with all the original contents and the patched boot image private suspend fun processWholeFile( - input: InputStream, + file: PatchFileClassifier, ext: String, createPatcher: (OutputStream) -> WholeFilePatcher ): Boolean { val outFile = MediaStoreUtils.getFile("$destName.$ext") val outStream = outFile.uri.outputStream().buffered(1024 * 1024) - if (!runWholeFilePatcher(input, outStream, outFile.toString(), createPatcher)) { + if (!runWholeFilePatcher(file, outStream, outFile.toString(), createPatcher)) { outFile.delete() return false } @@ -365,7 +408,7 @@ abstract class MagiskInstallImpl protected constructor( Timber.e(e) return false } - if (!runWholeFilePatcher(file.openProgressStream(), outStream, usbDeviceName) { out -> + if (!runWholeFilePatcher(file, outStream, usbDeviceName) { out -> RecoveryGptProcessor(installDir, out, console, logs) }) { return false diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/utils/ProgressInputStream.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/utils/ProgressInputStream.kt index 0076ae61d..00fe5c891 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/utils/ProgressInputStream.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/utils/ProgressInputStream.kt @@ -1,7 +1,9 @@ package com.topjohnwu.magisk.core.utils import java.io.FilterInputStream +import java.io.FilterOutputStream import java.io.InputStream +import java.io.OutputStream class ProgressInputStream( base: InputStream, @@ -55,3 +57,43 @@ class ProgressInputStream( progressEmitter(bytesRead) } } + +class ProgressOutputStream( + base: OutputStream, + val progressEmitter: (Long) -> Unit +) : FilterOutputStream(base) { + + private var bytesWritten = 0L + private var lastUpdate = 0L + + private fun emitProgress() { + val cur = System.currentTimeMillis() + if (cur - lastUpdate > 1000) { + lastUpdate = cur + progressEmitter(bytesWritten) + } + } + + override fun write(b: Int) { + out.write(b) + bytesWritten++ + emitProgress() + } + + override fun write(b: ByteArray) { + write(b, 0, b.size) + } + + override fun write(b: ByteArray, off: Int, len: Int) { + out.write(b, off, len) + if (len > 0) { + bytesWritten += len + emitProgress() + } + } + + override fun close() { + super.close() + progressEmitter(bytesWritten) + } +}