From 1d662cb09db4d43b1117e205311aca44cadd70c4 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 23 Oct 2026 23:09:09 +0000 Subject: [PATCH] app: don't rerun flash and action jobs on restore The back stack survives process death, but a flash or action screen's ViewModel doesn't. When such a screen was restored, its new ViewModel had no job yet, so the job started again without the user asking: a Magisk install, a module zip, an uninstall, or a module's action script, which may not be safe to repeat. Record in the ViewModel's SavedStateHandle that the job was started. If a restored screen finds the flag in a new ViewModel, its job and output are gone, so leave the screen instead of running the job again. Assisted-by: Claude Opus 5.5 --- .../topjohnwu/magisk/arch/ViewModelFactory.kt | 5 +++-- .../com/topjohnwu/magisk/ui/MainActivity.kt | 21 +++++++++++++++++++ .../magisk/ui/flash/FlashViewModel.kt | 14 ++++++++++++- .../magisk/ui/module/ActionViewModel.kt | 14 ++++++++++++- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/arch/ViewModelFactory.kt b/app/apk/src/main/java/com/topjohnwu/magisk/arch/ViewModelFactory.kt index f5b68f016..adc144fe9 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/arch/ViewModelFactory.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/arch/ViewModelFactory.kt @@ -1,6 +1,7 @@ package com.topjohnwu.magisk.arch import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.createSavedStateHandle import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.viewModelFactory import com.topjohnwu.magisk.core.di.ServiceLocator @@ -22,8 +23,8 @@ val VMFactory: ViewModelProvider.Factory = viewModelFactory { initializer { SuperuserGrantViewModel(ServiceLocator.policyDB) } initializer { InstallViewModel(ServiceLocator.networkService, ServiceLocator.firmwareCrawler) } initializer { DenyListViewModel() } - initializer { FlashViewModel() } - initializer { ActionViewModel() } + initializer { FlashViewModel(createSavedStateHandle()) } + initializer { ActionViewModel(createSavedStateHandle()) } initializer { ModuleViewModel() } initializer { SettingsViewModel() } } diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt index 149f5c6f9..f0bbd8fdb 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt @@ -162,6 +162,11 @@ class MainActivity : ComponentActivity(), SplashScreenHost { } entry { key -> val vm: FlashViewModel = viewModel(factory = VMFactory) + if (vm.isInterrupted) { + // Restored after process death: nothing is left to show + LeaveInterruptedScreen(navigator, key) + return@entry + } LaunchedEffect(key) { if (vm.flashAction.isEmpty()) { vm.flashAction = key.action @@ -194,6 +199,11 @@ class MainActivity : ComponentActivity(), SplashScreenHost { } entry { key -> val vm: ActionViewModel = viewModel(factory = VMFactory) + if (vm.isInterrupted) { + // Restored after process death: nothing is left to show + LeaveInterruptedScreen(navigator, key) + return@entry + } LaunchedEffect(key) { if (vm.actionId.isEmpty()) { vm.actionId = key.id @@ -369,6 +379,17 @@ class MainActivity : ComponentActivity(), SplashScreenHost { } } +/** + * Leave a flash or action screen restored after process death. Its job and output only lived + * in memory, and running the job again may not be safe, so there is nothing left to show. + */ +@Composable +private fun LeaveInterruptedScreen(navigator: Navigator, key: Route) { + LaunchedEffect(key) { + if (navigator.current() == key) navigator.pop() + } +} + @Composable private fun MainActivityDialogs( unsupportedMessages: List>, diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt index d23d525f5..7e7b38e25 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt @@ -2,6 +2,7 @@ package com.topjohnwu.magisk.ui.flash import android.net.Uri import androidx.core.net.toFile +import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.viewModelScope import com.topjohnwu.magisk.arch.BaseViewModel import com.topjohnwu.magisk.core.AppContext @@ -34,12 +35,18 @@ import java.io.File import java.io.FileNotFoundException import java.io.IOException -class FlashViewModel : BaseViewModel() { +class FlashViewModel(private val savedState: SavedStateHandle) : BaseViewModel() { enum class State { FLASHING, SUCCESS, FAILED } + /** + * Whether the job was started before the process died. The job and its output only lived + * in memory and are gone, so it must never run again: repeating it may not be safe. + */ + val isInterrupted = savedState.get(KEY_STARTED) == true + private val _flashState = MutableStateFlow(State.FLASHING) val flashState: StateFlow = _flashState.asStateFlow() @@ -79,6 +86,7 @@ class FlashViewModel : BaseViewModel() { // --- Actions --- fun startFlashing() { + savedState[KEY_STARTED] = true val action = flashAction val uri = flashUri val usb = usbDevice @@ -207,4 +215,8 @@ class FlashViewModel : BaseViewModel() { } fun restartPressed() = reboot() + + companion object { + private const val KEY_STARTED = "started" + } } diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ActionViewModel.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ActionViewModel.kt index 83fa32252..c05fc90c8 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ActionViewModel.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ActionViewModel.kt @@ -1,5 +1,6 @@ package com.topjohnwu.magisk.ui.module +import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.viewModelScope import com.topjohnwu.magisk.arch.BaseViewModel import com.topjohnwu.magisk.core.ktx.timeFormatStandard @@ -16,12 +17,18 @@ import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -class ActionViewModel : BaseViewModel() { +class ActionViewModel(private val savedState: SavedStateHandle) : BaseViewModel() { enum class State { RUNNING, SUCCESS, FAILED } + /** + * Whether the action was started before the process died. The action and its output only + * lived in memory and are gone, so it must never run again: repeating it may not be safe. + */ + val isInterrupted = savedState.get(KEY_STARTED) == true + private val _actionState = MutableStateFlow(State.RUNNING) val actionState: StateFlow = _actionState.asStateFlow() @@ -44,6 +51,7 @@ class ActionViewModel : BaseViewModel() { } fun startRunAction() { + savedState[KEY_STARTED] = true viewModelScope.launch { // Start the command with the real terminal size terminalReady.await() @@ -74,4 +82,8 @@ class ActionViewModel : BaseViewModel() { showToast(file.toString()) } } + + companion object { + private const val KEY_STARTED = "started" + } }