Modern Apps git

← Modern-Apps.git

auto: route the car launcher as LauncherHome/App (#nav)

Commit
5b42802a49494dd1503376abccd0b222e49ec24e
Author
Vayun Mathur <[email protected]>
When
2026-11-16 20:38 UTC (19 days ago)
Parents
bc40eab
The car display swapped its home grid for a hosted app on
CarLauncherState._selectedApp, a MutableStateFlow<String?> whose
null meant home: selection decided the screen, exactly the implicit
state machine the owner directive bans, and the car display had no
destinations at all.

Add the car-side destinations to Route: LauncherHome (the grid,
root of the car stack) and App(appId). carRouteFor names the
destination a selection denotes, and syncCarRoute projects the
selection onto a NavBackStack<Route>: selecting pushes App over
home, switching apps replaces it (a swap, not a descent — no hosted
app is ever buried under another), and clearing the selection pops
back home. LauncherRoot renders the stack's top and derives the
dock selection from it; taps still write CarLauncherState.selectApp,
which stays the selection engine the session focus reads.

The car Presentation has no phone chrome, activity or insets, and
its bottom bar must persist across the home/app swap, so LauncherRoot
renders the stack's top directly instead of hosting MainNavigation.
Hosted apps keep their own template stacks inside App exactly as
before, and driving-distraction behaviour is unchanged: same grid,
same dock (including the transient unpinned-open-app icon and the
re-tap home-reset action), same hosted templates.

Tests: new CarRouteTest pins the projection (push over home,
replace on switch, pop home on clear, idempotence) and JSON
round-trips for the car destinations; Y3AutoCoreTest's distinctness
and serialization sweeps now include them; the existing
LauncherRoot/CarLauncherState suites pass unchanged in behaviour.

Verified: ./gradlew :auto:compileDevKotlin :auto:testDevUnitTest
green (374 tests).

Diff

auto/AGENTS.md
diff --git a/auto/AGENTS.md b/auto/AGENTS.mdindex 89228bc5a1..3914ce2fd3 100644--- a/auto/AGENTS.md+++ b/auto/AGENTS.md@@ -28,7 +28,7 @@ _Install: ./install auto (dev by default)._ ### Root — entry + navigation - com/vayunmathur/auto/MainActivity.kt - com/vayunmathur/auto/Navigation.kt-- com/vayunmathur/auto/Route.kt+- com/vayunmathur/auto/Route.kt — phone Home/Pairing/PinnedApps + car LauncherHome/App(appId) (car stack synced from CarLauncherState.selectedApp) ### network/ — head-unit transports - com/vayunmathur/auto/network/HeadUnitServer.kt - com/vayunmathur/auto/network/TransportIntake.kt
auto/src/main/java/com/vayunmathur/auto/Route.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/Route.kt b/auto/src/main/java/com/vayunmathur/auto/Route.ktindex 0f751d0df7..4f3657c4d3 100644--- a/auto/src/main/java/com/vayunmathur/auto/Route.kt+++ b/auto/src/main/java/com/vayunmathur/auto/Route.kt@@ -1,11 +1,20 @@ package com.vayunmathur.auto +import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.library.util.NavKey import kotlinx.serialization.Serializable  /**- * Auto has two phone-side screens. The interesting interface lives on the car's display, not- * here; these are only somewhere to see whether a car is attached and how it is attached.+ * Auto's destinations, phone-side and car-side.+ *+ * The phone side ([Home]/[Pairing]/[PinnedApps]) is only somewhere to see+ * whether a car is attached and how it is attached. The interesting+ * interface lives on the car's display: its launcher is a stack of+ * [LauncherHome] under at most one [App], synced from+ * `CarLauncherState.selectedApp` (which stays the session's selection+ * engine — the projection session focus reads). Hosted apps keep their+ * own template stacks inside [App]; the car stack only decides home grid+ * versus one hosted app, never what that app shows.  */ @Serializable sealed interface Route : NavKey {@@ -20,4 +29,37 @@ sealed interface Route : NavKey {     /** The car dock pin picker: which car apps sit in the bottom bar. */     @Serializable     data object PinnedApps : Route++    /** The car launcher's home grid. Root of the car-side stack. */+    @Serializable+    data object LauncherHome : Route++    /** One hosted car app, by `DiscoveredApp.id`. */+    @Serializable+    data class App(val appId: String) : Route+}++/** The car destination a selected app id names; null is the home grid. */+fun carRouteFor(selectedAppId: String?): Route =+    selectedAppId?.let { Route.App(it) } ?: Route.LauncherHome++/**+ * Projects a selected app id onto the car-side stack, kept free of+ * composition so unit tests can drive it with a plain [NavBackStack].+ *+ * The stack is [Route.LauncherHome] plus at most one [Route.App]:+ * selecting an app pushes it (or replaces the open one — switching apps+ * is a swap, not a descent), and clearing the selection pops back home.+ * Idempotent while the selection is unchanged.+ */+fun syncCarRoute(backStack: NavBackStack<Route>, selectedAppId: String?) {+    val target = carRouteFor(selectedAppId)+    val top = backStack.backStack.lastOrNull()+    if (top == target) return+    when {+        target is Route.LauncherHome -> backStack.reset(Route.LauncherHome)+        top is Route.LauncherHome -> backStack.add(target)+        top is Route.App -> backStack.setLast(target)+        else -> backStack.reset(Route.LauncherHome, target)+    } }
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 75936145bb..6e94ab4c41 100644--- a/auto/src/main/java/com/vayunmathur/auto/platform/CarLauncherState.kt+++ b/auto/src/main/java/com/vayunmathur/auto/platform/CarLauncherState.kt@@ -64,7 +64,10 @@ object CarLauncherState {      /**      * Selected app id (`DiscoveredApp.id`); null means the home grid.-     * Written by launcher taps, read by the body + the session focus.+     * Written by launcher taps, read by the session focus, and projected+     * onto the car-side `Route` stack by the launcher (Route.kt's+     * `syncCarRoute`): this flow stays the selection engine, the stack+     * is only its screen.      */     val selectedApp: StateFlow<String?> = _selectedApp.asStateFlow() 
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 09d55df974..e26470dd20 100644--- a/auto/src/main/java/com/vayunmathur/auto/ui/LauncherRoot.kt+++ b/auto/src/main/java/com/vayunmathur/auto/ui/LauncherRoot.kt@@ -3,27 +3,42 @@ package com.vayunmathur.auto.ui import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.lifecycle.compose.collectAsStateWithLifecycle+import com.vayunmathur.auto.Route+import com.vayunmathur.auto.carRouteFor import com.vayunmathur.auto.platform.CarLauncherState+import com.vayunmathur.auto.syncCarRoute import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.Surface+import com.vayunmathur.library.util.rememberNavBackStack  /**  * The car launcher's Compose entry: body + bottom bar.  *- * Body is the home grid (null selection) or one hosted app's screen; the- * bottom bar holds home | pins | status. The dock shows the pinned apps in- * stored order (stale ids filtered), falling back to the first apps until- * the user picks — plus the open app as a transient trailing icon when it- * isn't pinned, so an unpinned app still reads selected in the bar.- * Selection writes into the shared state so the session focus follows the- * tap. One public `@Composable` per file throughout `ui/`.+ * The body is the car-side `Route` stack: [Route.LauncherHome] (the home+ * grid) with at most one [Route.App] over it. Which destination is showing+ * is decided by that stack, never by reading the selection directly:+ * `CarLauncherState.selectedApp` stays the selection engine the session+ * focus follows, and [syncCarRoute] projects it onto the stack (selecting+ * pushes/replaces the app, clearing pops home). Hosted apps keep their+ * own template stacks inside their destination. The bottom bar holds+ * home | pins | status and stays under either destination, as before.+ * The dock shows the pinned apps in stored order (stale ids filtered),+ * falling back to the first apps until the user picks — plus the open app+ * as a transient trailing icon when it isn't pinned, so an unpinned app+ * still reads selected in the bar. Selection writes into the shared state+ * so the session focus follows the tap. One public `@Composable` per file+ * throughout `ui/`.  *- * Plain `Surface + Column`: the car `Presentation` has no phone chrome or- * insets to scaffold around.+ * Plain `Surface + Column`, rendering the stack's top directly rather+ * than through `MainNavigation`: the car `Presentation` has no phone+ * chrome, activity or insets to scaffold around, and the bottom bar must+ * persist across the home/app swap. Driving-distraction behaviour is+ * unchanged — same grid, same dock, same hosted templates.  */ @Composable fun LauncherRoot(modifier: Modifier = Modifier) {@@ -31,14 +46,18 @@ fun LauncherRoot(modifier: Modifier = Modifier) {     val discovered by CarLauncherState.discovered.collectAsStateWithLifecycle()     val pinnedIds by CarLauncherState.pinnedOrder.collectAsStateWithLifecycle()     val actions by CarLauncherState.actions.collectAsStateWithLifecycle()+    val backStack = rememberNavBackStack<Route>(carRouteFor(selected))+    LaunchedEffect(selected) { syncCarRoute(backStack, selected) }+    val top = backStack.backStack.lastOrNull()+    val openAppId = (top as? Route.App)?.appId     val pinned = remember(pinnedIds, discovered) {         val byId = discovered.associateBy { it.id }         val ordered = pinnedIds.mapNotNull { byId[it] }         if (ordered.isNotEmpty()) ordered else discovered     }-    val dock = remember(pinned, selected, discovered) {-        if (selected != null && pinned.none { it.id == selected }) {-            val open = discovered.firstOrNull { it.id == selected }+    val dock = remember(pinned, openAppId, discovered) {+        if (openAppId != null && pinned.none { it.id == openAppId }) {+            val open = discovered.firstOrNull { it.id == openAppId }             if (open != null) pinned + open else pinned         } else {             pinned@@ -46,27 +65,27 @@ fun LauncherRoot(modifier: Modifier = Modifier) {     }     Surface(modifier = modifier.fillMaxSize(), color = MaterialTheme.colorScheme.surface) {         Column(Modifier.fillMaxSize()) {-            if (selected == null) {-                LauncherHomeGrid(-                    apps = discovered,-                    onOpen = { CarLauncherState.selectApp(it) },+            if (top is Route.App) {+                AppScreen(+                    appId = top.appId,                     modifier = Modifier.weight(1f),                 )             } else {-                AppScreen(-                    appId = selected!!,+                LauncherHomeGrid(+                    apps = discovered,+                    onOpen = { CarLauncherState.selectApp(it) },                     modifier = Modifier.weight(1f),                 )             }             LauncherBottomBar(                 pinned = dock,-                selectedId = selected,+                selectedId = openAppId,                 onHome = { CarLauncherState.selectApp(null) },                 // Tap the open app: home-reset it in place. Tap anything                 // else: open it where it was (each host session keeps its                 // own screen stack, so re-select resumes).                 onPin = { id ->-                    if (id == selected) {+                    if (id == openAppId) {                         discovered.firstOrNull { it.id == id }?.let {                             actions.onAppHomeTap(it.component)                         }
auto/src/test/java/com/vayunmathur/auto/CarRouteTest.kt
diff --git a/auto/src/test/java/com/vayunmathur/auto/CarRouteTest.kt b/auto/src/test/java/com/vayunmathur/auto/CarRouteTest.ktnew file mode 100644index 0000000000..a4471c99bc--- /dev/null+++ b/auto/src/test/java/com/vayunmathur/auto/CarRouteTest.kt@@ -0,0 +1,68 @@+package com.vayunmathur.auto++import com.vayunmathur.library.util.NavBackStack+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull++/**+ * The car-side stack: [Route.LauncherHome] under at most one [Route.App],+ * projected from the selected app id by [syncCarRoute]. Hosted apps keep+ * their own template stacks; this stack only decides home versus hosted.+ */+@RunWith(RobolectricTestRunner::class)+class CarRouteTest {++    private fun stackOf(vararg routes: Route) = NavBackStack(routes)++    @Test+    fun `null selection is the home grid`() {+        assertEquals(Route.LauncherHome, carRouteFor(null))+        assertEquals(Route.App("maps"), carRouteFor("maps"))+    }++    @Test+    fun `selecting an app pushes it over home`() {+        val stack = stackOf(Route.LauncherHome)+        syncCarRoute(stack, "maps")+        assertEquals(listOf(Route.LauncherHome, Route.App("maps")), stack.backStack)+    }++    @Test+    fun `switching apps replaces instead of stacking`() {+        val stack = stackOf(Route.LauncherHome)+        syncCarRoute(stack, "maps")+        syncCarRoute(stack, "music")+        assertEquals(listOf(Route.LauncherHome, Route.App("music")), stack.backStack)+    }++    @Test+    fun `clearing the selection pops back home`() {+        val stack = stackOf(Route.LauncherHome)+        syncCarRoute(stack, "maps")+        syncCarRoute(stack, null)+        assertEquals(listOf(Route.LauncherHome), stack.backStack)+        assertNull((stack.backStack.lastOrNull() as? Route.App)?.appId)+    }++    @Test+    fun `sync is idempotent while the selection is unchanged`() {+        val stack = stackOf(Route.LauncherHome)+        syncCarRoute(stack, "maps")+        syncCarRoute(stack, "maps")+        syncCarRoute(stack, null)+        syncCarRoute(stack, null)+        assertEquals(listOf(Route.LauncherHome), stack.backStack)+    }++    @Test+    fun `car routes round trip through serialization`() {+        val json = kotlinx.serialization.json.Json+        for (route in listOf<Route>(Route.LauncherHome, Route.App("com.example.maps/.Main"))) {+            val text = json.encodeToString(Route.serializer(), route)+            assertEquals(route, json.decodeFromString(Route.serializer(), text))+        }+    }+}
auto/src/test/java/com/vayunmathur/auto/Y3AutoCoreTest.kt
diff --git a/auto/src/test/java/com/vayunmathur/auto/Y3AutoCoreTest.kt b/auto/src/test/java/com/vayunmathur/auto/Y3AutoCoreTest.ktindex 5b7e6564b9..4c415e1e1b 100644--- a/auto/src/test/java/com/vayunmathur/auto/Y3AutoCoreTest.kt+++ b/auto/src/test/java/com/vayunmathur/auto/Y3AutoCoreTest.kt@@ -123,13 +123,13 @@ class Y3AutoCoreTest {     }      @Test fun routesAreDistinct() {-        val routes = listOf(Route.Home, Route.Pairing, Route.PinnedApps)-        assertEquals(3, routes.toSet().size)+        val routes = listOf(Route.Home, Route.Pairing, Route.PinnedApps, Route.LauncherHome, Route.App("maps"))+        assertEquals(5, routes.toSet().size)     }      @Test fun routeSerializationRoundTrip() {         val json = kotlinx.serialization.json.Json-        for (route in listOf<Route>(Route.Home, Route.Pairing, Route.PinnedApps)) {+        for (route in listOf<Route>(Route.Home, Route.Pairing, Route.PinnedApps, Route.LauncherHome, Route.App("maps"))) {             val text = json.encodeToString(Route.serializer(), route)             assertEquals(route, json.decodeFromString(Route.serializer(), text))         }