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
This commit is contained in:
topjohnwu
2026-10-24 13:01:45 -07:00
committed by John Wu
parent 8acbd6305e
commit 9ee6815a54
2 changed files with 16 additions and 14 deletions
@@ -301,9 +301,11 @@ fun PatchComposableDialog(
showUsbDestination: Boolean = false, showUsbDestination: Boolean = false,
) { ) {
val context = LocalContext.current val context = LocalContext.current
var source by rememberSaveable(candidate) { // The user's choice, defaulting to the downloadable image when there is one. Not keyed on the
mutableStateOf(if (candidate != null) PatchSource.CANDIDATE else PatchSource.FILE) // 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<PatchSource?>(null) }
val source = chosenSource ?: if (candidate != null) PatchSource.CANDIDATE else PatchSource.FILE
var localFileUri by rememberSaveable { mutableStateOf<Uri?>(null) } var localFileUri by rememberSaveable { mutableStateOf<Uri?>(null) }
var pendingSubmitAfterPick by rememberSaveable { mutableStateOf(false) } var pendingSubmitAfterPick by rememberSaveable { mutableStateOf(false) }
var url by rememberSaveable { mutableStateOf("") } var url by rememberSaveable { mutableStateOf("") }
@@ -358,7 +360,7 @@ fun PatchComposableDialog(
pendingSubmitAfterPick = false pendingSubmitAfterPick = false
if (uri != null) { if (uri != null) {
localFileUri = uri localFileUri = uri
source = PatchSource.FILE chosenSource = PatchSource.FILE
if (shouldSubmit) { if (shouldSubmit) {
confirmWithUri(uri) confirmWithUri(uri)
} }
@@ -460,7 +462,7 @@ fun PatchComposableDialog(
.clip(RoundedCornerShape(12.dp)) .clip(RoundedCornerShape(12.dp))
.selectable( .selectable(
selected = source == PatchSource.CANDIDATE, selected = source == PatchSource.CANDIDATE,
onClick = { source = PatchSource.CANDIDATE }, onClick = { chosenSource = PatchSource.CANDIDATE },
role = Role.RadioButton role = Role.RadioButton
) )
.padding(vertical = 10.dp), .padding(vertical = 10.dp),
@@ -495,7 +497,7 @@ fun PatchComposableDialog(
.selectable( .selectable(
selected = source == PatchSource.FILE, selected = source == PatchSource.FILE,
onClick = { onClick = {
source = PatchSource.FILE chosenSource = PatchSource.FILE
pendingSubmitAfterPick = false pendingSubmitAfterPick = false
localFilePicker.launch("*/*") localFilePicker.launch("*/*")
}, },
@@ -534,7 +536,7 @@ fun PatchComposableDialog(
.clip(RoundedCornerShape(12.dp)) .clip(RoundedCornerShape(12.dp))
.selectable( .selectable(
selected = source == PatchSource.URL, selected = source == PatchSource.URL,
onClick = { source = PatchSource.URL }, onClick = { chosenSource = PatchSource.URL },
role = Role.RadioButton role = Role.RadioButton
) )
.padding(vertical = 10.dp), .padding(vertical = 10.dp),
@@ -102,21 +102,21 @@ class InstallViewModel(
_uiState.update { it.copy(showPatchDialog = false) } _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) { fun onPatchSelected(uri: Uri, usbDeviceName: String? = null) {
if (usbDeviceName != null) { if (usbDeviceName != null) {
viewModelScope.launch { viewModelScope.launch {
if (UsbBlockDevice.requestPermission(AppContext, usbDeviceName)) { if (UsbBlockDevice.requestPermission(AppContext, usbDeviceName)) {
_uiState.update { it.copy(patchUri = uri, usbDevice = usbDeviceName) } _uiState.update {
if (_uiState.value.method == Method.PATCH) { it.copy(method = Method.PATCH, patchUri = uri, usbDevice = usbDeviceName)
install()
} }
install()
} }
} }
} else { } else {
_uiState.update { it.copy(patchUri = uri, usbDevice = null) } _uiState.update { it.copy(method = Method.PATCH, patchUri = uri, usbDevice = null) }
if (_uiState.value.method == Method.PATCH) { install()
install()
}
} }
} }