app: always use TerminalScreen in FlashScreen

Unify FlashScreen to render TerminalScreen unconditionally for all
installation and flashing actions instead of branching on FLASH_ZIP
with a LazyColumn fallback:

- FlashScreen: Remove LazyColumn, scrollbar, and the unused action
  parameter, displaying TerminalScreen inside the scaffold body.
- FlashViewModel: Forward MagiskInstaller output directly to the
  terminal emulator via appendLineOnMain and remove consoleItems.
  Await emulatorReady before starting flashing operations.
- FlashViewModel: Preserve transcriptText saving for FLASH_ZIP while
  saving logItems (stdout and stderr) for MagiskInstaller actions.
- MainActivity: Drop action parameter from FlashScreen invocation.

Assisted-by: Gemini 3.8 Flash
This commit is contained in:
topjohnwu
2026-09-04 09:44:49 -07:00
committed by John Wu
parent b36c60abc1
commit cc08282b6d
3 changed files with 13 additions and 68 deletions
@@ -151,7 +151,7 @@ class MainActivity : ComponentActivity(), SplashScreenHost {
vm.startFlashing()
}
}
FlashScreen(vm, action = key.action, onBack = { navigator.pop() })
FlashScreen(vm, onBack = { navigator.pop() })
}
entry<Route.SuperuserDetail> { key ->
val vm: SuperuserViewModel = viewModel(
@@ -1,38 +1,24 @@
package com.topjohnwu.magisk.ui.flash
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.ui.component.rememberExternalStoragePermissionLauncher
import com.topjohnwu.magisk.ui.component.verticalScrollbar
import com.topjohnwu.magisk.ui.terminal.TerminalScreen
import com.topjohnwu.magisk.core.R as CoreR
@@ -40,14 +26,12 @@ import com.topjohnwu.magisk.core.R as CoreR
@Composable
fun FlashScreen(
viewModel: FlashViewModel,
action: String,
onBack: () -> Unit,
modifier: Modifier = Modifier
) {
val flashState by viewModel.flashState.collectAsStateWithLifecycle()
val showReboot by viewModel.showReboot.collectAsStateWithLifecycle()
val finished = flashState != FlashViewModel.State.FLASHING
val useTerminal = action == Const.Value.FLASH_ZIP
val saveLog = rememberExternalStoragePermissionLauncher {
viewModel.saveLog()
}
@@ -100,43 +84,11 @@ fun FlashScreen(
)
}
) { padding ->
if (useTerminal) {
TerminalScreen(
modifier = Modifier
.fillMaxSize()
.padding(padding),
onEmulatorCreated = { viewModel.onEmulatorCreated(it) },
)
} else {
val items = viewModel.consoleItems
val listState = rememberLazyListState()
LaunchedEffect(items.size) {
if (items.isNotEmpty()) {
listState.animateScrollToItem(items.size - 1)
}
}
LazyColumn(
state = listState,
modifier = Modifier
.fillMaxSize()
.padding(padding)
.verticalScrollbar(listState, contentPadding = PaddingValues(vertical = 4.dp))
.horizontalScroll(rememberScrollState())
.padding(horizontal = 8.dp, vertical = 4.dp)
) {
itemsIndexed(items) { _, line ->
Text(
text = line,
fontFamily = FontFamily.Monospace,
fontSize = 12.sp,
lineHeight = 16.sp,
color = MaterialTheme.colorScheme.onSurface,
modifier = Modifier.fillMaxWidth()
)
}
}
}
TerminalScreen(
modifier = Modifier
.fillMaxSize()
.padding(padding),
onEmulatorCreated = { viewModel.onEmulatorCreated(it) },
)
}
}
@@ -1,7 +1,6 @@
package com.topjohnwu.magisk.ui.flash
import android.net.Uri
import androidx.compose.runtime.mutableStateListOf
import androidx.core.net.toFile
import androidx.lifecycle.viewModelScope
import com.topjohnwu.magisk.arch.BaseViewModel
@@ -50,8 +49,6 @@ class FlashViewModel : BaseViewModel() {
var flashAction: String = ""
var flashUri: Uri? = null
// --- TerminalScreen mode (FLASH_ZIP) ---
private var emulator: TerminalEmulator? = null
private val emulatorReady = CompletableDeferred<TerminalEmulator>()
@@ -60,29 +57,27 @@ class FlashViewModel : BaseViewModel() {
emulatorReady.complete(emu)
}
// --- LazyColumn mode (MagiskInstaller) ---
val consoleItems = mutableStateListOf<String>()
private val logItems = mutableListOf<String>().synchronized()
private val outItems = object : CallbackList<String>() {
override fun onAddElement(e: String?) {
e ?: return
consoleItems.add(e)
emulator?.appendLineOnMain(e)
logItems.add(e)
}
}
// --- Shared ---
// --- Actions ---
fun startFlashing() {
val action = flashAction
val uri = flashUri
viewModelScope.launch {
val emu = emulatorReady.await()
when (action) {
Const.Value.FLASH_ZIP -> {
uri ?: return@launch
flashZip(uri)
flashZip(emu, uri)
}
Const.Value.UNINSTALL -> {
_showReboot.value = false
@@ -126,9 +121,7 @@ class FlashViewModel : BaseViewModel() {
_flashState.value = if (success) State.SUCCESS else State.FAILED
}
private suspend fun flashZip(uri: Uri) {
val emu = emulatorReady.await()
private suspend fun flashZip(emu: TerminalEmulator, uri: Uri) {
val installDir = File(AppContext.cacheDir, "flash")
val result = withContext(Dispatchers.IO) {
try {
@@ -191,7 +184,7 @@ class FlashViewModel : BaseViewModel() {
val file = MediaStoreUtils.getFile(name)
file.uri.outputStream().bufferedWriter().use { writer ->
val transcript = emulator?.screen?.transcriptText
if (transcript != null) {
if (flashAction == Const.Value.FLASH_ZIP && transcript != null) {
writer.write(transcript)
} else {
synchronized(logItems) {