From ee4d64f8f1d0af3ecbc4becbfd24cd0e48cc6fd8 Mon Sep 17 00:00:00 2001 From: rodrig20 <101592068+rodrig20@users.noreply.github.com> Date: Tue, 1 Dec 2026 02:01:08 +0000 Subject: [PATCH] feat(usb): enable gadget when mounting the first image Closes #18 --- .../com/rodrig20/isodroid/MainActivity.kt | 53 +++++++++++++++---- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/rodrig20/isodroid/MainActivity.kt b/app/src/main/java/com/rodrig20/isodroid/MainActivity.kt index f61090f..748188e 100644 --- a/app/src/main/java/com/rodrig20/isodroid/MainActivity.kt +++ b/app/src/main/java/com/rodrig20/isodroid/MainActivity.kt @@ -240,6 +240,23 @@ fun App() { // While a turn on/off script runs, lock the toggle with a // progress bar so taps feel answered immediately. var isToggling by remember { mutableStateOf(false) } + + // Enables the gadget and surfaces failures in the UI. Shared by the + // gadget switch and the auto-enable done when mounting the first item. + suspend fun enableGadget(): Boolean { + val result = rootManager.turnOnApp() + if (result.startsWith("Error")) { + snackbarHostState.showSnackbar(result) + return false + } + if (result.contains("waiting-host")) { + snackbarHostState.showSnackbar( + "Gadget on, but the PC has not enumerated it yet (plug the cable or rescan)" + ) + } + return true + } + when (currentScreen) { is Screen.Home -> HomeScreen( isAppEnabled = isAppEnabled, @@ -253,14 +270,7 @@ fun App() { coroutineScope.launch { try { if (enabled) { - val result = rootManager.turnOnApp() - if (result.startsWith("Error")) { - snackbarHostState.showSnackbar(result) - } else if (result.contains("waiting-host")) { - snackbarHostState.showSnackbar( - "Gadget on, but the PC has not enumerated it yet (plug the cable or rescan)" - ) - } + enableGadget() } else { val result = rootManager.turnOffApp() if (result.startsWith("Error")) { @@ -289,6 +299,19 @@ fun App() { } } }, + onEnsureGadgetEnabled = { + if (isToggling) { + false + } else { + isToggling = true + try { + IsoLog.i("auto enabling gadget before first mount") + enableGadget() + } finally { + isToggling = false + } + } + }, rootManager = rootManager, onNavigateToSettings = { currentScreen = Screen.Settings } ) @@ -337,7 +360,9 @@ fun HomeScreen( rootManager: RootManager, // Manager for root operations onNavigateToSettings: () -> Unit, // Callback for navigating to settings snackbarHostState: SnackbarHostState = remember { SnackbarHostState() }, - isToggling: Boolean = false // A turn on/off script is running + isToggling: Boolean = false, // A turn on/off script is running + // Turns the gadget on when a mount needs it (false = failed). + onEnsureGadgetEnabled: suspend () -> Boolean = { false } ) { val context = LocalContext.current val diskItemRepository = remember { DiskItemRepository(context) } @@ -408,6 +433,12 @@ fun HomeScreen( coroutineScope.launch { var updatedItem: DiskItem if (newItemState) { + // Mounting needs a bound gadget, so bring it up + // first when it is still off. + if (!isAppEnabled && !onEnsureGadgetEnabled()) { + // enableGadget already reported the failure. + return@launch + } // Mount the item if the new state is active val result = rootManager.mountItem( item.path ?: "", @@ -496,7 +527,9 @@ fun HomeScreen( Switch( checked = item.isActive, onCheckedChange = { onToggle(it) }, - enabled = isAppEnabled + // Enabled even with the gadget off: mounting + // turns the gadget on automatically. + enabled = !isToggling ) } }