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
This commit is contained in:
topjohnwu
2026-10-24 13:01:45 -07:00
committed by John Wu
parent cbd8f32d90
commit 1d662cb09d
4 changed files with 50 additions and 4 deletions
@@ -1,6 +1,7 @@
package com.topjohnwu.magisk.arch package com.topjohnwu.magisk.arch
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.createSavedStateHandle
import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory import androidx.lifecycle.viewmodel.viewModelFactory
import com.topjohnwu.magisk.core.di.ServiceLocator import com.topjohnwu.magisk.core.di.ServiceLocator
@@ -22,8 +23,8 @@ val VMFactory: ViewModelProvider.Factory = viewModelFactory {
initializer { SuperuserGrantViewModel(ServiceLocator.policyDB) } initializer { SuperuserGrantViewModel(ServiceLocator.policyDB) }
initializer { InstallViewModel(ServiceLocator.networkService, ServiceLocator.firmwareCrawler) } initializer { InstallViewModel(ServiceLocator.networkService, ServiceLocator.firmwareCrawler) }
initializer { DenyListViewModel() } initializer { DenyListViewModel() }
initializer { FlashViewModel() } initializer { FlashViewModel(createSavedStateHandle()) }
initializer { ActionViewModel() } initializer { ActionViewModel(createSavedStateHandle()) }
initializer { ModuleViewModel() } initializer { ModuleViewModel() }
initializer { SettingsViewModel() } initializer { SettingsViewModel() }
} }
@@ -162,6 +162,11 @@ class MainActivity : ComponentActivity(), SplashScreenHost {
} }
entry<Route.Flash> { key -> entry<Route.Flash> { key ->
val vm: FlashViewModel = viewModel(factory = VMFactory) 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) { LaunchedEffect(key) {
if (vm.flashAction.isEmpty()) { if (vm.flashAction.isEmpty()) {
vm.flashAction = key.action vm.flashAction = key.action
@@ -194,6 +199,11 @@ class MainActivity : ComponentActivity(), SplashScreenHost {
} }
entry<Route.Action> { key -> entry<Route.Action> { key ->
val vm: ActionViewModel = viewModel(factory = VMFactory) 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) { LaunchedEffect(key) {
if (vm.actionId.isEmpty()) { if (vm.actionId.isEmpty()) {
vm.actionId = key.id 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 @Composable
private fun MainActivityDialogs( private fun MainActivityDialogs(
unsupportedMessages: List<Pair<Int, Int>>, unsupportedMessages: List<Pair<Int, Int>>,
@@ -2,6 +2,7 @@ package com.topjohnwu.magisk.ui.flash
import android.net.Uri import android.net.Uri
import androidx.core.net.toFile import androidx.core.net.toFile
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.topjohnwu.magisk.arch.BaseViewModel import com.topjohnwu.magisk.arch.BaseViewModel
import com.topjohnwu.magisk.core.AppContext import com.topjohnwu.magisk.core.AppContext
@@ -34,12 +35,18 @@ import java.io.File
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.io.IOException import java.io.IOException
class FlashViewModel : BaseViewModel() { class FlashViewModel(private val savedState: SavedStateHandle) : BaseViewModel() {
enum class State { enum class State {
FLASHING, SUCCESS, FAILED 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<Boolean>(KEY_STARTED) == true
private val _flashState = MutableStateFlow(State.FLASHING) private val _flashState = MutableStateFlow(State.FLASHING)
val flashState: StateFlow<State> = _flashState.asStateFlow() val flashState: StateFlow<State> = _flashState.asStateFlow()
@@ -79,6 +86,7 @@ class FlashViewModel : BaseViewModel() {
// --- Actions --- // --- Actions ---
fun startFlashing() { fun startFlashing() {
savedState[KEY_STARTED] = true
val action = flashAction val action = flashAction
val uri = flashUri val uri = flashUri
val usb = usbDevice val usb = usbDevice
@@ -207,4 +215,8 @@ class FlashViewModel : BaseViewModel() {
} }
fun restartPressed() = reboot() fun restartPressed() = reboot()
companion object {
private const val KEY_STARTED = "started"
}
} }
@@ -1,5 +1,6 @@
package com.topjohnwu.magisk.ui.module package com.topjohnwu.magisk.ui.module
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.topjohnwu.magisk.arch.BaseViewModel import com.topjohnwu.magisk.arch.BaseViewModel
import com.topjohnwu.magisk.core.ktx.timeFormatStandard import com.topjohnwu.magisk.core.ktx.timeFormatStandard
@@ -16,12 +17,18 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class ActionViewModel : BaseViewModel() { class ActionViewModel(private val savedState: SavedStateHandle) : BaseViewModel() {
enum class State { enum class State {
RUNNING, SUCCESS, FAILED 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<Boolean>(KEY_STARTED) == true
private val _actionState = MutableStateFlow(State.RUNNING) private val _actionState = MutableStateFlow(State.RUNNING)
val actionState: StateFlow<State> = _actionState.asStateFlow() val actionState: StateFlow<State> = _actionState.asStateFlow()
@@ -44,6 +51,7 @@ class ActionViewModel : BaseViewModel() {
} }
fun startRunAction() { fun startRunAction() {
savedState[KEY_STARTED] = true
viewModelScope.launch { viewModelScope.launch {
// Start the command with the real terminal size // Start the command with the real terminal size
terminalReady.await() terminalReady.await()
@@ -74,4 +82,8 @@ class ActionViewModel : BaseViewModel() {
showToast(file.toString()) showToast(file.toString())
} }
} }
companion object {
private const val KEY_STARTED = "started"
}
} }