From 9ee6815a54800fd1bbaa1cb16e0a12813e66d24c Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 24 Oct 2026 06:06:18 +0000 Subject: [PATCH] app: keep patch method across process death The patch dialog restores its state after process death, including a pending file pick, but the selected install method only lived in InstallViewModel. If the app was killed while the file picker was open, the new ViewModel had no method, so picking the image closed the dialog and silently did nothing. On Pixel and Googlebook devices, the dialog also keyed its source choice on the downloadable firmware image. That is only found after the firmware search, which runs again after process death, so a file chosen before was reset to the downloadable image. Set the patch method when an image is selected, and save the user's source choice without keying it on the firmware image. Assisted-by: Claude Opus 5.5 --- .../topjohnwu/magisk/ui/install/InstallDialog.kt | 16 +++++++++------- .../magisk/ui/install/InstallViewModel.kt | 14 +++++++------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/install/InstallDialog.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/install/InstallDialog.kt index 52252aaf4..dafb09c3c 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/install/InstallDialog.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/install/InstallDialog.kt @@ -301,9 +301,11 @@ fun PatchComposableDialog( showUsbDestination: Boolean = false, ) { val context = LocalContext.current - var source by rememberSaveable(candidate) { - mutableStateOf(if (candidate != null) PatchSource.CANDIDATE else PatchSource.FILE) - } + // The user's choice, defaulting to the downloadable image when there is one. Not keyed on the + // candidate: it is only found after the firmware search, which runs again after the process + // is recreated, and must not reset a choice made before that + var chosenSource by rememberSaveable { mutableStateOf(null) } + val source = chosenSource ?: if (candidate != null) PatchSource.CANDIDATE else PatchSource.FILE var localFileUri by rememberSaveable { mutableStateOf(null) } var pendingSubmitAfterPick by rememberSaveable { mutableStateOf(false) } var url by rememberSaveable { mutableStateOf("") } @@ -358,7 +360,7 @@ fun PatchComposableDialog( pendingSubmitAfterPick = false if (uri != null) { localFileUri = uri - source = PatchSource.FILE + chosenSource = PatchSource.FILE if (shouldSubmit) { confirmWithUri(uri) } @@ -460,7 +462,7 @@ fun PatchComposableDialog( .clip(RoundedCornerShape(12.dp)) .selectable( selected = source == PatchSource.CANDIDATE, - onClick = { source = PatchSource.CANDIDATE }, + onClick = { chosenSource = PatchSource.CANDIDATE }, role = Role.RadioButton ) .padding(vertical = 10.dp), @@ -495,7 +497,7 @@ fun PatchComposableDialog( .selectable( selected = source == PatchSource.FILE, onClick = { - source = PatchSource.FILE + chosenSource = PatchSource.FILE pendingSubmitAfterPick = false localFilePicker.launch("*/*") }, @@ -534,7 +536,7 @@ fun PatchComposableDialog( .clip(RoundedCornerShape(12.dp)) .selectable( selected = source == PatchSource.URL, - onClick = { source = PatchSource.URL }, + onClick = { chosenSource = PatchSource.URL }, role = Role.RadioButton ) .padding(vertical = 10.dp), diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/install/InstallViewModel.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/install/InstallViewModel.kt index ea3abd31b..348913e0f 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/install/InstallViewModel.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/install/InstallViewModel.kt @@ -102,21 +102,21 @@ class InstallViewModel( _uiState.update { it.copy(showPatchDialog = false) } } + // Also sets the method: it is only kept in memory, and the image may be picked after the + // process was recreated, e.g. if it was killed while the file picker was open fun onPatchSelected(uri: Uri, usbDeviceName: String? = null) { if (usbDeviceName != null) { viewModelScope.launch { if (UsbBlockDevice.requestPermission(AppContext, usbDeviceName)) { - _uiState.update { it.copy(patchUri = uri, usbDevice = usbDeviceName) } - if (_uiState.value.method == Method.PATCH) { - install() + _uiState.update { + it.copy(method = Method.PATCH, patchUri = uri, usbDevice = usbDeviceName) } + install() } } } else { - _uiState.update { it.copy(patchUri = uri, usbDevice = null) } - if (_uiState.value.method == Method.PATCH) { - install() - } + _uiState.update { it.copy(method = Method.PATCH, patchUri = uri, usbDevice = null) } + install() } }