Modern Apps git

← Modern-Apps.git

auto: selecting a car app moves host surface/touch focus to it

Commit
2585be5fdf944107f3dc2829540cb3a3ee7dc426
Author
Vayun Mathur <[email protected]>
When
2026-11-30 22:02 UTC (5 days ago)
Parents
624529a
Tapping another app in the car launcher selected it on screen
but never moved host focus: CarAppHostSession set focus once at
start (Maps/first app) and nothing called setFocused again,
despite the docs describing selectedApp as the projection the
session focus reads. The UI showed app B's template while the
Maps session received B's map surface and touches; B never got
onSurfaceAvailable. A surviving selection was likewise ignored
on reconnect.

The launcher->session bridge gains an onAppSelected action
(the existing late-wired pattern), wired to host.setFocused;
LauncherRoot's selection effect resolves the id to a component
and fires it, and start() prefers a surviving selection before
falling back to Maps/first. Two regression tests cover tap-focus
and reconnect-focus. GAL lifecycle, template quotas and media
browse were audited and cleared. Verified: :auto:testDevUnitTest
376 tests pass; lint errors are pre-existing on untouched
Route.kt.

Diff

auto/src/main/java/com/vayunmathur/auto/platform/CarLauncherState.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/platform/CarLauncherState.kt b/auto/src/main/java/com/vayunmathur/auto/platform/CarLauncherState.ktindex 6e94ab4c41..b2e53e3c28 100644--- a/auto/src/main/java/com/vayunmathur/auto/platform/CarLauncherState.kt+++ b/auto/src/main/java/com/vayunmathur/auto/platform/CarLauncherState.kt@@ -202,4 +202,10 @@ data class CarLauncherActions(      * starts.      */     val onAppHomeTap: (android.content.ComponentName) -> Unit = {},+    /**+     * Selecting an app in the launcher: moves the host's surface/touch+     * focus to that app's session. Late-wired by [CarAppHostSession]+     * like [onAppHomeTap]; no-op until the session starts.+     */+    val onAppSelected: (android.content.ComponentName) -> Unit = {}, )
auto/src/main/java/com/vayunmathur/auto/service/CarAppHostSession.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/service/CarAppHostSession.kt b/auto/src/main/java/com/vayunmathur/auto/service/CarAppHostSession.ktindex 3175847e4f..aaef601bf0 100644--- a/auto/src/main/java/com/vayunmathur/auto/service/CarAppHostSession.kt+++ b/auto/src/main/java/com/vayunmathur/auto/service/CarAppHostSession.kt@@ -42,12 +42,18 @@ class CarAppHostSession(         CarLauncherState.setDiscovered(apps)         host.bindAll(apps)         CarLauncherState.updateActions {-            it.copy(onAppHomeTap = { component -> resetApp(component) })+            it.copy(+                onAppHomeTap = { component -> resetApp(component) },+                onAppSelected = { component -> host.setFocused(component) },+            )         }-        // Default focus: Maps when present, else the first discovered app, so-        // the map surface attaches without waiting for a launcher tap.+        // Focus follows the launcher selection when one survives from a+        // previous session; otherwise Maps when present, else the first+        // discovered app, so the map surface attaches without waiting for+        // a launcher tap.+        val selected = apps.firstOrNull { it.id == CarLauncherState.selectedApp.value }         val maps = apps.firstOrNull { it.component.packageName == MAPS_PACKAGE }-        host.setFocused(maps?.component ?: apps.firstOrNull()?.component)+        host.setFocused(selected?.component ?: maps?.component ?: apps.firstOrNull()?.component)     }      /**
auto/src/main/java/com/vayunmathur/auto/ui/LauncherRoot.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/ui/LauncherRoot.kt b/auto/src/main/java/com/vayunmathur/auto/ui/LauncherRoot.ktindex c395eee414..1240a438bc 100644--- a/auto/src/main/java/com/vayunmathur/auto/ui/LauncherRoot.kt+++ b/auto/src/main/java/com/vayunmathur/auto/ui/LauncherRoot.kt@@ -49,7 +49,14 @@ fun LauncherRoot(modifier: Modifier = Modifier) {     val pinnedIds by CarLauncherState.pinnedOrder.collectAsStateWithLifecycle()     val actions by CarLauncherState.actions.collectAsStateWithLifecycle()     val backStack = rememberNavBackStack<Route>(carRouteFor(selected))-    LaunchedEffect(selected) { syncCarRoute(backStack, selected) }+    LaunchedEffect(selected, discovered, actions) {+        syncCarRoute(backStack, selected)+        // The stack above is only the screen: the host session's surface+        // and touch focus must follow the same selection, or the open+        // app's map surface is handed to the previously focused app.+        discovered.firstOrNull { it.id == selected }+            ?.let { actions.onAppSelected(it.component) }+    }     val top = backStack.backStack.lastOrNull()     val openAppId = (top as? Route.App)?.appId     val pinned = remember(pinnedIds, discovered) {
auto/src/test/java/com/vayunmathur/auto/ui/LauncherRootTest.kt
diff --git a/auto/src/test/java/com/vayunmathur/auto/ui/LauncherRootTest.kt b/auto/src/test/java/com/vayunmathur/auto/ui/LauncherRootTest.ktindex 75bad8ade7..386de54da8 100644--- a/auto/src/test/java/com/vayunmathur/auto/ui/LauncherRootTest.kt+++ b/auto/src/test/java/com/vayunmathur/auto/ui/LauncherRootTest.kt@@ -66,6 +66,28 @@ class LauncherRootTest {         rule.onNodeWithText("Maps").assertExists()     } +    @Test fun `tapping an app moves host focus to it`() {+        val focused = mutableListOf<ComponentName>()+        CarLauncherState.setDiscovered(listOf(maps, music))+        CarLauncherState.updateActions { it.copy(onAppSelected = { c -> focused += c }) }+        rule.setContent { LauncherRoot() }+        rule.onAllNodes(androidx.compose.ui.test.hasText("Music"))[0].performClick()+        rule.waitForIdle()+        assertEquals(music.id, CarLauncherState.selectedApp.value)+        assertEquals(listOf(music.component), focused)+    }++    @Test fun `pre-selected app moves host focus on composition`() {+        // Reconnect shape: a selection survives the old session, so the+        // rebuilt launcher must focus that app, not the session default.+        val focused = mutableListOf<ComponentName>()+        CarLauncherState.setDiscovered(listOf(maps, music))+        CarLauncherState.selectApp(music.id)+        CarLauncherState.updateActions { it.copy(onAppSelected = { c -> focused += c }) }+        rule.setContent { LauncherRoot() }+        assertEquals(listOf(music.component), focused)+    }+     @Test fun `tapping open app dock icon fires home tap action`() {         var homeTaps = 0         CarLauncherState.setDiscovered(listOf(maps, music))