library:carhost: AA-style renderer with artwork, grids, now-playing
- Commit
64f26c2047747b0ecb9cfed01c8799e234f22171- Author
- vayun-mathur <[email protected]>
- When
- 2026-09-22 00:30 UTC (2 months ago)
- Parents
d468a81
Diff
library/carhost/src/main/java/com/vayunmathur/library/carhost/CarTemplateView.kt
diff --git a/library/carhost/src/main/java/com/vayunmathur/library/carhost/CarTemplateView.kt b/library/carhost/src/main/java/com/vayunmathur/library/carhost/CarTemplateView.ktindex 2d57f49117..8eccdd3d08 100644--- a/library/carhost/src/main/java/com/vayunmathur/library/carhost/CarTemplateView.kt+++ b/library/carhost/src/main/java/com/vayunmathur/library/carhost/CarTemplateView.kt@@ -1,14 +1,24 @@ package com.vayunmathur.library.carhost +import androidx.compose.foundation.Image+import androidx.compose.foundation.background+import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.ExperimentalLayoutApi import androidx.compose.foundation.layout.FlowRow import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.Spacer+import androidx.compose.foundation.layout.aspectRatio import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.size import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items+import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.runtime.Composable@@ -18,10 +28,19 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier+import androidx.compose.ui.draw.clip+import androidx.compose.ui.graphics.painter.BitmapPainter+import androidx.compose.ui.graphics.painter.Painter+import androidx.compose.ui.graphics.asImageBitmap+import androidx.compose.ui.layout.ContentScale+import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.input.ImeAction+import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.car.app.model.Template import androidx.car.app.model.TemplateWrapper+import androidx.core.graphics.drawable.IconCompat+import androidx.core.graphics.drawable.toBitmap import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.Card import com.vayunmathur.library.ui.CircularProgressIndicator@@ -33,17 +52,14 @@ import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.TextButton /**- * Renders a parsed [HostTemplate] the way MA Auto's head unit draws it.+ * Renders a parsed [HostTemplate] the way MA Auto's head unit draws it, styled to+ * match Android Auto's list/grid/now-playing conventions. * * Extracted from the `:auto` app so any module (the head unit, or an app's * screenshot test) can render a car view without depending on `:auto`. Two- * couplings the app used to own are now parameters:- * - the template is passed in directly (the app reads it off its session flow);- * - the map surface is a [mapContent] slot — `:auto` supplies its real- * `TextureView` island, headless previews get the default flat placeholder.- *- * Each template kind has its own renderer below; unhandled kinds already fell- * back to [HostTemplate.Pane] text in the parsers, so the view never blanks.+ * couplings the app used to own are parameters: the [template] is passed in+ * directly, and the map surface is a [mapContent] slot (`:auto` supplies its real+ * `TextureView` island; headless previews get the flat placeholder). */ @Composable fun CarTemplateView(@@ -51,22 +67,26 @@ fun CarTemplateView( modifier: Modifier = Modifier, mapContent: @Composable (Modifier) -> Unit = { CarMapPlaceholder(it) }, ) {- when (template) {- is HostTemplate.Navigation -> NavigationRenderer(template, mapContent, modifier)- is HostTemplate.PlaceList -> PlaceListRenderer(template, modifier)- is HostTemplate.TemplateList -> ListRenderer(template, modifier)- is HostTemplate.Grid -> GridRenderer(template, modifier)- is HostTemplate.Pane -> PaneRenderer(template, modifier)- is HostTemplate.Search -> SearchRenderer(template, modifier)- is HostTemplate.Message -> MessageRenderer(template, modifier)- is HostTemplate.Tabs -> TabsRenderer(template, mapContent, modifier)- is HostTemplate.MediaPlayback -> MediaPlaybackRenderer(template, modifier)- is HostTemplate.SignIn -> SignInRenderer(template, modifier)- is HostTemplate.InCall -> InCallRenderer(template, modifier)- is HostTemplate.Keypad -> KeypadRenderer(template, modifier)- is HostTemplate.MapWithContent -> MapWithContentRenderer(template, mapContent, modifier)- is HostTemplate.LegacyNav -> LegacyNavRenderer(template, mapContent, modifier)- null -> LoadingRenderer(modifier)+ Surface(modifier.fillMaxSize(), color = MaterialTheme.colorScheme.surface) {+ Box(Modifier.padding(CAR_PADDING)) {+ when (template) {+ is HostTemplate.Navigation -> NavigationRenderer(template, mapContent)+ is HostTemplate.PlaceList -> PlaceListRenderer(template)+ is HostTemplate.TemplateList -> ListRenderer(template)+ is HostTemplate.Grid -> GridRenderer(template)+ is HostTemplate.Pane -> PaneRenderer(template)+ is HostTemplate.Search -> SearchRenderer(template)+ is HostTemplate.Message -> MessageRenderer(template)+ is HostTemplate.Tabs -> TabsRenderer(template, mapContent)+ is HostTemplate.MediaPlayback -> MediaPlaybackRenderer(template)+ is HostTemplate.SignIn -> SignInRenderer(template)+ is HostTemplate.InCall -> InCallRenderer(template)+ is HostTemplate.Keypad -> KeypadRenderer(template)+ is HostTemplate.MapWithContent -> MapWithContentRenderer(template, mapContent)+ is HostTemplate.LegacyNav -> LegacyNavRenderer(template, mapContent)+ null -> LoadingRenderer()+ }+ } } } @@ -93,116 +113,103 @@ fun CarMapPlaceholder(modifier: Modifier = Modifier) { Surface(modifier, color = MaterialTheme.colorScheme.surfaceVariant) {} } +// ── Renderers ──────────────────────────────────────────────────────────────+ @Composable private fun NavigationRenderer( template: HostTemplate.Navigation, mapContent: @Composable (Modifier) -> Unit,- modifier: Modifier = Modifier, ) {- Column(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(8.dp)) {- val banner = listOfNotNull(template.cue, template.road).joinToString(" · ").takeIf { it.isNotBlank() }- if (banner != null) {- Text(text = banner, style = MaterialTheme.typography.headlineSmall)- }- Row(horizontalArrangement = Arrangement.spacedBy(12.dp)) {- template.distanceText?.let { Text(text = it, style = MaterialTheme.typography.bodyLarge) }- template.etaText?.let { Text(text = it, style = MaterialTheme.typography.bodyLarge) }+ Column(Modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(CAR_GAP)) {+ ManeuverCard(template)+ mapContent(Modifier.fillMaxWidth().weight(1f).clip(CAR_SHAPE))+ if (template.loading) LoadingRow()+ ActionRow(template.actions)+ }+}++@Composable+private fun ManeuverCard(template: HostTemplate.Navigation) {+ val cue = template.cue ?: template.road ?: "Navigating"+ Card(Modifier.fillMaxWidth()) {+ Row(+ Modifier.padding(CAR_GAP),+ horizontalArrangement = Arrangement.spacedBy(CAR_GAP),+ verticalAlignment = Alignment.CenterVertically,+ ) {+ template.distanceText?.let {+ Text(it, style = MaterialTheme.typography.headlineMedium)+ }+ Column(Modifier.weight(1f)) {+ Text(cue, style = MaterialTheme.typography.titleLarge, maxLines = 2, overflow = TextOverflow.Ellipsis)+ template.road?.takeIf { it != cue }?.let {+ Text(it, style = MaterialTheme.typography.bodyMedium)+ }+ template.lanesText?.let {+ Text(it, style = MaterialTheme.typography.titleMedium)+ }+ }+ template.etaText?.let {+ InfoChip(it)+ } }- template.lanesText?.let { Text(text = it, style = MaterialTheme.typography.bodyMedium) }- mapContent(Modifier.fillMaxWidth().weight(1f))- if (template.loading) CircularProgressIndicator()- ActionRow(actions = template.actions) } } @Composable-private fun PlaceListRenderer(template: HostTemplate.PlaceList, modifier: Modifier = Modifier) {- TemplateListBody(- title = template.title,- sections = listOf(HostUiSection(rows = template.rows)),- loading = template.loading,- actions = template.actions,- modifier = modifier,- )+private fun PlaceListRenderer(template: HostTemplate.PlaceList) {+ TemplateListBody(template.title, listOf(HostUiSection(rows = template.rows)), template.loading, template.actions) } @Composable private fun ListRenderer(template: HostTemplate.TemplateList, modifier: Modifier = Modifier) {- TemplateListBody(- title = template.title,- sections = template.sections,- loading = template.loading,- actions = template.actions,- modifier = modifier,- )+ TemplateListBody(template.title, template.sections, template.loading, template.actions, modifier) } @Composable private fun GridRenderer(template: HostTemplate.Grid, modifier: Modifier = Modifier) {- LazyColumn(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(4.dp)) {- template.title?.let {- item { Text(text = it, style = MaterialTheme.typography.headlineSmall) }- }- items(template.items, key = { it.title }) { item ->- GridItemRow(item = item)- }- if (template.loading) {- item { CircularProgressIndicator() }- }- if (template.actions.isNotEmpty()) {- item { ActionRow(actions = template.actions) }- }+ val tiles = template.items.map { Tile(it.image, it.title, it.text, it.loading, it.onClick) }+ LazyColumn(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(CAR_GAP)) {+ template.title?.let { item { ScreenTitle(it) } }+ gridRows(tiles)+ if (template.loading) item { LoadingRow() }+ if (template.actions.isNotEmpty()) item { ActionRow(template.actions) } } } @Composable-private fun PaneRenderer(template: HostTemplate.Pane, modifier: Modifier = Modifier) {- TemplateListBody(- title = template.title,- sections = listOf(HostUiSection(rows = template.rows)),- loading = template.loading,- actions = template.actions,- modifier = modifier,- )+private fun PaneRenderer(template: HostTemplate.Pane) {+ TemplateListBody(template.title, listOf(HostUiSection(rows = template.rows)), template.loading, template.actions) } @Composable private fun SearchRenderer(template: HostTemplate.Search, modifier: Modifier = Modifier) { var text by remember(template.initialText) { mutableStateOf(template.initialText) }- Column(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(8.dp)) {+ Column(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(CAR_GAP)) { OutlinedTextField( value = text,- onValueChange = {- text = it- template.onSearchTextChanged?.invoke(it)- },+ onValueChange = { text = it; template.onSearchTextChanged?.invoke(it) }, label = { Text(template.hint ?: "Search") }, singleLine = true, keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search), keyboardActions = KeyboardActions(onSearch = { template.onSearchSubmitted?.invoke(text) }), modifier = Modifier.fillMaxWidth(), )- TemplateListBody(- title = null,- sections = listOf(HostUiSection(rows = template.rows)),- loading = template.loading,- actions = template.actions,- modifier = Modifier.weight(1f),- )+ TemplateListBody(null, listOf(HostUiSection(rows = template.rows)), template.loading, template.actions, Modifier.weight(1f)) } } @Composable private fun MessageRenderer(template: HostTemplate.Message, modifier: Modifier = Modifier) {- Column(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(8.dp)) {+ Column(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(CAR_GAP)) { Card(Modifier.fillMaxWidth()) {- Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {- template.title?.let { Text(text = it, style = MaterialTheme.typography.headlineSmall) }- template.message?.let { Text(text = it, style = MaterialTheme.typography.bodyLarge) }- if (template.loading) CircularProgressIndicator()+ Column(Modifier.padding(CAR_GAP), verticalArrangement = Arrangement.spacedBy(CAR_GAP)) {+ template.title?.let { Text(it, style = MaterialTheme.typography.headlineSmall) }+ template.message?.let { Text(it, style = MaterialTheme.typography.bodyLarge) }+ if (template.loading) LoadingRow() } }- ActionRow(actions = template.actions)+ ActionRow(template.actions) } } @@ -210,101 +217,91 @@ private fun MessageRenderer(template: HostTemplate.Message, modifier: Modifier = private fun TabsRenderer( template: HostTemplate.Tabs, mapContent: @Composable (Modifier) -> Unit,- modifier: Modifier = Modifier, ) {- Column(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(8.dp)) {- Row(horizontalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.fillMaxWidth()) {- for (tab in template.tabs) {- val selected = tab.contentId == template.activeContentId- if (selected) {- Button(onClick = {}) { Text(tab.title ?: tab.contentId) }- } else {- TextButton(onClick = { template.onTabSelected?.invoke(tab.contentId) }) {- Text(tab.title ?: tab.contentId)- }- }- }- }- val content = template.content- if (content != null) {- when (content) {- is HostTemplate.TemplateList -> ListRenderer(content, Modifier.weight(1f))- is HostTemplate.Grid -> GridRenderer(content, Modifier.weight(1f))- is HostTemplate.Pane -> PaneRenderer(content, Modifier.weight(1f))- is HostTemplate.Message -> MessageRenderer(content, Modifier.weight(1f))- is HostTemplate.Search -> SearchRenderer(content, Modifier.weight(1f))- is HostTemplate.Navigation -> NavigationRenderer(content, mapContent, Modifier.weight(1f))- else -> TemplateListBody(null, emptyList(), false, emptyList(), Modifier.weight(1f))+ Column(Modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(CAR_GAP)) {+ TabBar(template)+ RenderInner(template.content, mapContent, template.loading, Modifier.weight(1f))+ }+}++@OptIn(ExperimentalLayoutApi::class)+@Composable+private fun TabBar(template: HostTemplate.Tabs) {+ FlowRow(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(CAR_GAP)) {+ for (tab in template.tabs) {+ val selected = tab.contentId == template.activeContentId+ val label = tab.title ?: tab.contentId+ if (selected) {+ Button(onClick = {}) { Text(label) }+ } else {+ TextButton(onClick = { template.onTabSelected?.invoke(tab.contentId) }) { Text(label) } }- } else if (template.loading) {- CircularProgressIndicator() } } } @Composable private fun MediaPlaybackRenderer(template: HostTemplate.MediaPlayback, modifier: Modifier = Modifier) {- Column(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(8.dp)) {- Card(Modifier.fillMaxWidth()) {- Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {- Text(text = template.title ?: "Now playing", style = MaterialTheme.typography.headlineSmall)- Text(- text = "Transport renders from the media session token.",- style = MaterialTheme.typography.bodyMedium,- )- }+ Column(+ modifier.fillMaxSize(),+ horizontalAlignment = Alignment.CenterHorizontally,+ verticalArrangement = Arrangement.spacedBy(CAR_GAP, Alignment.CenterVertically),+ ) {+ ArtworkTile(template.image, Modifier.size(220.dp), CAR_SHAPE)+ Text(template.title ?: "Now playing", style = MaterialTheme.typography.headlineSmall, maxLines = 1, overflow = TextOverflow.Ellipsis)+ Row(horizontalArrangement = Arrangement.spacedBy(CAR_GAP), verticalAlignment = Alignment.CenterVertically) {+ TextButton(onClick = {}) { Text("⏮") }+ Button(onClick = {}) { Text("▶ Play") }+ TextButton(onClick = {}) { Text("⏭") } }+ if (template.loading) LoadingRow() } } @Composable private fun SignInRenderer(template: HostTemplate.SignIn, modifier: Modifier = Modifier) {- Column(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(8.dp)) {+ Column(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(CAR_GAP)) { Card(Modifier.fillMaxWidth()) {- Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {- template.title?.let { Text(text = it, style = MaterialTheme.typography.headlineSmall) }- template.message?.let { Text(text = it, style = MaterialTheme.typography.bodyLarge) }- if (template.loading) CircularProgressIndicator()+ Column(Modifier.padding(CAR_GAP), verticalArrangement = Arrangement.spacedBy(CAR_GAP)) {+ template.title?.let { Text(it, style = MaterialTheme.typography.headlineSmall) }+ template.message?.let { Text(it, style = MaterialTheme.typography.bodyLarge) }+ if (template.loading) LoadingRow() } }- ActionRow(actions = template.actions)+ ActionRow(template.actions) } } @Composable private fun InCallRenderer(template: HostTemplate.InCall, modifier: Modifier = Modifier) {- Column(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(8.dp)) {- Card(Modifier.fillMaxWidth()) {- Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {- template.title?.let { Text(text = it, style = MaterialTheme.typography.headlineSmall) }- for (line in template.texts) {- Text(text = line, style = MaterialTheme.typography.bodyLarge)- }- if (template.loading) CircularProgressIndicator()- }- }- ActionRow(actions = template.actions)+ Column(+ modifier.fillMaxSize(),+ horizontalAlignment = Alignment.CenterHorizontally,+ verticalArrangement = Arrangement.spacedBy(CAR_GAP, Alignment.CenterVertically),+ ) {+ ArtworkTile(null, Modifier.size(120.dp), CircleShapeLike)+ template.title?.let { Text(it, style = MaterialTheme.typography.headlineMedium, maxLines = 1) }+ for (line in template.texts) Text(line, style = MaterialTheme.typography.bodyLarge)+ if (template.loading) LoadingRow()+ ActionRow(template.actions) } } @Composable private fun KeypadRenderer(template: HostTemplate.Keypad, modifier: Modifier = Modifier) { var text by remember(template.phoneNumber) { mutableStateOf(template.phoneNumber) }- Column(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(8.dp)) {- template.title?.let { Text(text = it, style = MaterialTheme.typography.headlineSmall) }+ Column(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(CAR_GAP)) {+ template.title?.let { ScreenTitle(it) } OutlinedTextField( value = text,- onValueChange = {- text = it- template.onNumberChanged?.invoke(it)- },+ onValueChange = { text = it; template.onNumberChanged?.invoke(it) }, label = { Text("Number") }, singleLine = true, keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), modifier = Modifier.fillMaxWidth(), ) template.onPrimaryAction?.let { primary ->- Button(onClick = primary) { Text(template.primaryTitle ?: "Call") }+ Button(onClick = primary, modifier = Modifier.fillMaxWidth()) { Text(template.primaryTitle ?: "Call") } } } }@@ -313,22 +310,11 @@ private fun KeypadRenderer(template: HostTemplate.Keypad, modifier: Modifier = M private fun MapWithContentRenderer( template: HostTemplate.MapWithContent, mapContent: @Composable (Modifier) -> Unit,- modifier: Modifier = Modifier, ) {- Column(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(8.dp)) {- mapContent(Modifier.fillMaxWidth().weight(1f))- val content = template.content- if (content != null) {- when (content) {- is HostTemplate.TemplateList -> ListRenderer(content, Modifier.weight(1f))- is HostTemplate.Grid -> GridRenderer(content, Modifier.weight(1f))- is HostTemplate.Pane -> PaneRenderer(content, Modifier.weight(1f))- is HostTemplate.Message -> MessageRenderer(content, Modifier.weight(1f))- is HostTemplate.Search -> SearchRenderer(content, Modifier.weight(1f))- else -> Unit- }- }- ActionRow(actions = template.actions)+ Column(Modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(CAR_GAP)) {+ mapContent(Modifier.fillMaxWidth().weight(1f).clip(CAR_SHAPE))+ RenderInner(template.content, mapContent, loading = false, Modifier.weight(1f))+ ActionRow(template.actions) } } @@ -336,32 +322,45 @@ private fun MapWithContentRenderer( private fun LegacyNavRenderer( template: HostTemplate.LegacyNav, mapContent: @Composable (Modifier) -> Unit,- modifier: Modifier = Modifier, ) {- val state = template.state- Column(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(8.dp)) {- val banner = listOfNotNull(state.cue, state.road).joinToString(" · ").takeIf { it.isNotBlank() }- banner?.let { Text(text = it, style = MaterialTheme.typography.headlineSmall) }- state.distanceText?.let { Text(text = it, style = MaterialTheme.typography.bodyLarge) }- state.lanesText?.let { Text(text = it, style = MaterialTheme.typography.bodyMedium) }- state.etaText?.let { Text(text = it, style = MaterialTheme.typography.bodyMedium) }- mapContent(Modifier.fillMaxWidth().weight(1f))- Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {- for (action in state.actions) {- TextButton(onClick = action.onClick) { Text(action.title) }- }+ val s = template.state+ Column(Modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(CAR_GAP)) {+ ManeuverCard(+ HostTemplate.Navigation(+ cue = s.cue, road = s.road, distanceText = s.distanceText,+ etaText = s.etaText, lanesText = s.lanesText,+ )+ )+ mapContent(Modifier.fillMaxWidth().weight(1f).clip(CAR_SHAPE))+ Row(horizontalArrangement = Arrangement.spacedBy(CAR_GAP)) {+ for (a in s.actions) TextButton(onClick = a.onClick) { Text(a.title) } } } } @Composable-private fun LoadingRenderer(modifier: Modifier = Modifier) {- Column(- modifier.fillMaxSize(),- horizontalAlignment = Alignment.CenterHorizontally,- verticalArrangement = Arrangement.Center,- ) {- CircularProgressIndicator()+private fun LoadingRenderer() {+ Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { CircularProgressIndicator() }+}++// ── Shared building blocks ───────────────────────────────────────────────────++@Composable+private fun RenderInner(+ content: HostTemplate?,+ mapContent: @Composable (Modifier) -> Unit,+ loading: Boolean,+ modifier: Modifier,+) {+ when (content) {+ is HostTemplate.TemplateList -> ListRenderer(content, modifier)+ is HostTemplate.Grid -> GridRenderer(content, modifier)+ is HostTemplate.Pane -> PaneRenderer(content)+ is HostTemplate.Message -> MessageRenderer(content, modifier)+ is HostTemplate.Search -> SearchRenderer(content, modifier)+ is HostTemplate.Navigation -> NavigationRenderer(content, mapContent)+ null -> if (loading) Box(modifier.fillMaxSize(), contentAlignment = Alignment.Center) { CircularProgressIndicator() }+ else -> Unit } } @@ -373,40 +372,61 @@ private fun TemplateListBody( actions: List<HostUiAction>, modifier: Modifier = Modifier, ) {- LazyColumn(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(4.dp)) {- title?.let {- item { Text(text = it, style = MaterialTheme.typography.headlineSmall) }- }+ LazyColumn(modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(CAR_ROW_GAP)) {+ title?.let { item { ScreenTitle(it) } } for (section in sections) {- section.header?.let { header ->- item { Text(text = header, style = MaterialTheme.typography.titleMedium) }+ section.header?.let { header -> item { SectionHeader(header) } }+ when {+ section.chips -> item { ChipStrip(section.rows) }+ section.grid -> gridRows(section.rows.map { Tile(it.image, it.title, it.texts.firstOrNull(), false, it.onClick) })+ else -> items(section.rows, key = { it.title }) { TemplateRow(it) } }- if (section.chips) {- // ChipSection: a horizontal strip of compact pills, not list rows.- item { ChipStrip(rows = section.rows) }- } else {- items(section.rows, key = { it.title }) { row ->- TemplateRow(row = row)- }+ }+ if (loading) item { LoadingRow() }+ if (actions.isNotEmpty()) item { ActionRow(actions) }+ }+}++/** Emits chunked rows of grid tiles as plain [LazyColumn] items (no nested scroll). */+private fun androidx.compose.foundation.lazy.LazyListScope.gridRows(tiles: List<Tile>) {+ tiles.chunked(GRID_COLUMNS).forEach { chunk ->+ item {+ Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(CAR_GAP)) {+ for (tile in chunk) TileCell(tile, Modifier.weight(1f))+ repeat(GRID_COLUMNS - chunk.size) { Spacer(Modifier.weight(1f)) } } }- if (loading) {- item { CircularProgressIndicator() }+ }+}++/** A grid tile: square artwork over a title + optional subtitle. */+@Composable+private fun TileCell(tile: Tile, modifier: Modifier = Modifier) {+ val click = tile.onClick+ Column(+ modifier+ .let { if (click != null) it.clickable(onClick = click) else it }+ .padding(bottom = CAR_GAP),+ verticalArrangement = Arrangement.spacedBy(4.dp),+ ) {+ Box(Modifier.fillMaxWidth().aspectRatio(1f)) {+ ArtworkTile(tile.image, Modifier.fillMaxSize(), CAR_SHAPE)+ if (tile.loading) CircularProgressIndicator(Modifier.align(Alignment.Center)) }- if (actions.isNotEmpty()) {- item { ActionRow(actions = actions) }+ Text(tile.title, style = MaterialTheme.typography.titleSmall, maxLines = 1, overflow = TextOverflow.Ellipsis)+ tile.subtitle?.let {+ Text(it, style = MaterialTheme.typography.bodySmall, maxLines = 1, overflow = TextOverflow.Ellipsis) } } } -/** A wrapping row of compact, tappable chips (filters / quick actions). */-@OptIn(androidx.compose.foundation.layout.ExperimentalLayoutApi::class)+@OptIn(ExperimentalLayoutApi::class) @Composable private fun ChipStrip(rows: List<HostUiRow>) { FlowRow(- modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp),- horizontalArrangement = Arrangement.spacedBy(8.dp),- verticalArrangement = Arrangement.spacedBy(8.dp),+ Modifier.fillMaxWidth().padding(vertical = 4.dp),+ horizontalArrangement = Arrangement.spacedBy(CAR_GAP),+ verticalArrangement = Arrangement.spacedBy(CAR_GAP), ) { for (row in rows) { val click = row.onClick@@ -417,7 +437,7 @@ private fun ChipStrip(rows: List<HostUiRow>) { color = MaterialTheme.colorScheme.secondaryContainer, ) { Text(- text = row.title,+ row.title, style = MaterialTheme.typography.labelLarge, modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp), )@@ -430,41 +450,112 @@ private fun ChipStrip(rows: List<HostUiRow>) { private fun TemplateRow(row: HostUiRow) { val click = row.onClick ListItem(- headlineContent = { Text(row.title) },+ headlineContent = { Text(row.title, maxLines = 1, overflow = TextOverflow.Ellipsis) }, supportingContent = {- if (row.texts.isNotEmpty()) Text(row.texts.joinToString("\n"))+ if (row.texts.isNotEmpty()) {+ Text(row.texts.joinToString(" · "), maxLines = 2, overflow = TextOverflow.Ellipsis)+ } },+ leadingContent = row.image?.let { { ArtworkTile(it, Modifier.size(56.dp), CAR_SHAPE) } }, trailingContent = {- if (click != null) {- TextButton(onClick = click) { Text(if (row.browse) "›" else "Open") }- }+ if (click != null) TextButton(onClick = click) { Text(if (row.browse) "›" else "Open") } }, ) } +/** Resolves an [IconCompat] to a painter, or a themed placeholder box when absent. */ @Composable-private fun GridItemRow(item: HostUiGridItem) {- val click = item.onClick- ListItem(- headlineContent = { Text(item.title) },- supportingContent = {- item.text?.let { Text(it) }- },- trailingContent = {- if (item.loading) CircularProgressIndicator()- else if (click != null) {- TextButton(onClick = click) { Text("Open") }- }- },+private fun ArtworkTile(icon: IconCompat?, modifier: Modifier, shape: androidx.compose.ui.graphics.Shape) {+ val painter: Painter? = when {+ icon == null -> null+ // Resource icons render natively (vectors included) even under Layoutlib.+ icon.type == IconCompat.TYPE_RESOURCE ->+ runCatching { icon.resId }.getOrNull()?.takeIf { it != 0 }+ ?.let { androidx.compose.ui.res.painterResource(it) }+ // Content-URI / bitmap art (device) resolves via loadDrawable.+ else -> rememberIconPainter(icon)+ }+ if (painter != null) {+ Image(painter, contentDescription = null, contentScale = ContentScale.Crop, modifier = modifier.clip(shape))+ } else {+ Box(modifier.clip(shape).background(MaterialTheme.colorScheme.surfaceVariant), contentAlignment = Alignment.Center) {+ Text("♪", style = MaterialTheme.typography.headlineMedium)+ }+ }+}++@Composable+private fun rememberIconPainter(icon: IconCompat?): Painter? {+ val context = LocalContext.current+ return remember(icon) {+ if (icon == null) return@remember null+ runCatching {+ // loadDrawable resolves content-URI/bitmap art on-device; under Layoutlib+ // it returns null for resources, so fall back to the resource directly.+ val d = icon.loadDrawable(context)+ ?: if (icon.type == IconCompat.TYPE_RESOURCE) {+ androidx.core.content.res.ResourcesCompat.getDrawable(+ context.resources, icon.resId, context.theme,+ )+ } else {+ null+ }+ ?: return@runCatching null+ val bmp = if (d.intrinsicWidth > 0 && d.intrinsicHeight > 0) d.toBitmap() else d.toBitmap(256, 256)+ BitmapPainter(bmp.asImageBitmap())+ }.getOrNull()+ }+}++@Composable+private fun ScreenTitle(text: String) {+ Text(text, style = MaterialTheme.typography.headlineSmall, modifier = Modifier.padding(bottom = 4.dp))+}++@Composable+private fun SectionHeader(text: String) {+ Text(+ text.uppercase(),+ style = MaterialTheme.typography.labelLarge,+ color = MaterialTheme.colorScheme.primary,+ modifier = Modifier.padding(top = CAR_GAP, bottom = 2.dp), ) } +@Composable+private fun InfoChip(text: String) {+ Surface(shape = MaterialTheme.shapes.large, color = MaterialTheme.colorScheme.tertiaryContainer) {+ Text(text, style = MaterialTheme.typography.titleMedium, modifier = Modifier.padding(horizontal = 12.dp, vertical = 6.dp))+ }+}++@Composable+private fun LoadingRow() {+ Row(Modifier.fillMaxWidth().padding(CAR_GAP), horizontalArrangement = Arrangement.Center) {+ CircularProgressIndicator()+ }+}+ @Composable private fun ActionRow(actions: List<HostUiAction>) { if (actions.isEmpty()) return- Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {- for (action in actions) {- Button(onClick = action.onClick) { Text(action.title) }- }+ Row(Modifier.padding(top = 4.dp), horizontalArrangement = Arrangement.spacedBy(CAR_GAP)) {+ for (action in actions) Button(onClick = action.onClick) { Text(action.title) } } }++/** Flattened tile descriptor shared by the Grid template and grid sections. */+private data class Tile(+ val image: IconCompat?,+ val title: String,+ val subtitle: String?,+ val loading: Boolean,+ val onClick: (() -> Unit)?,+)++private val CAR_PADDING = 16.dp+private val CAR_GAP = 12.dp+private val CAR_ROW_GAP = 4.dp+private val CAR_SHAPE = RoundedCornerShape(12.dp)+private val CircleShapeLike = RoundedCornerShape(percent = 50)+private const val GRID_COLUMNS = 3
library/carhost/src/main/java/com/vayunmathur/library/carhost/HostTemplateState.kt
diff --git a/library/carhost/src/main/java/com/vayunmathur/library/carhost/HostTemplateState.kt b/library/carhost/src/main/java/com/vayunmathur/library/carhost/HostTemplateState.ktindex c200e7a753..e55958259d 100644--- a/library/carhost/src/main/java/com/vayunmathur/library/carhost/HostTemplateState.kt+++ b/library/carhost/src/main/java/com/vayunmathur/library/carhost/HostTemplateState.kt@@ -37,19 +37,21 @@ data class HostUiAction( val onClick: () -> Unit, ) -/** One browsable row: title, subtitle lines, and the app's own click. */+/** One browsable row: title, subtitle lines, optional leading image, and click. */ data class HostUiRow( val title: String, val texts: List<String> = emptyList(), val browse: Boolean = false,+ val image: androidx.core.graphics.drawable.IconCompat? = null, val onClick: (() -> Unit)? = null, ) -/** One grid cell: title, subtitle, and the app's own click. */+/** One grid cell: title, subtitle, optional artwork, and the app's own click. */ data class HostUiGridItem( val title: String, val text: String? = null, val loading: Boolean = false,+ val image: androidx.core.graphics.drawable.IconCompat? = null, val onClick: (() -> Unit)? = null, ) @@ -155,6 +157,7 @@ sealed interface HostTemplate { data class MediaPlayback( val title: String? = null, val loading: Boolean = false,+ val image: androidx.core.graphics.drawable.IconCompat? = null, ) : HostTemplate /** Parked-only sign-in / permissions / long text. */@@ -199,13 +202,14 @@ data class HostUiTab( * One named section inside [HostTemplate.TemplateList]. * * [chips] marks a section that came from a `ChipSection`: the renderer draws its- * rows as a horizontal chip strip (compact filter/quick-action pills) instead of- * full-width list rows. Row/grid sections leave it false.+ * rows as a horizontal chip strip. [grid] marks a `GridSection`: rows render as+ * artwork tiles in a multi-column grid. Plain row sections leave both false. */ data class HostUiSection( val header: String? = null, val rows: List<HostUiRow> = emptyList(), val chips: Boolean = false,+ val grid: Boolean = false, ) /** Parses template wrappers into [HostTemplate]. Pure functions, any thread. */@@ -269,6 +273,7 @@ object HostTemplateParsers { title = carText(runCatching { item.title }.getOrNull()) ?: return@mapNotNull null, texts = listOfNotNull(carText(runCatching { item.text }.getOrNull())), browse = false,+ image = carIcon(runCatching { item.image }.getOrNull()), onClick = runCatching { item.onClickDelegate }.getOrNull()?.let { d -> { thread(name = "ma-auto-carhost-click", isDaemon = true) {@@ -313,6 +318,7 @@ object HostTemplateParsers { header = header, rows = rows, chips = section is androidx.car.app.model.ChipSection,+ grid = section is androidx.car.app.model.GridSection, ) } return HostTemplate.TemplateList(@@ -588,6 +594,7 @@ object HostTemplateParsers { title = title, texts = texts, browse = runCatching { row.isBrowsable }.getOrDefault(false),+ image = carIcon(runCatching { row.image }.getOrNull()), onClick = delegate?.let { d -> { thread(name = "ma-auto-carhost-click", isDaemon = true) {@@ -609,6 +616,7 @@ object HostTemplateParsers { title = title, text = carText(runCatching { grid.text }.getOrNull()), loading = runCatching { grid.isLoading }.getOrDefault(false),+ image = carIcon(runCatching { grid.image }.getOrNull()), onClick = delegate?.let { d -> { thread(name = "ma-auto-carhost-click", isDaemon = true) {@@ -658,6 +666,10 @@ object HostTemplateParsers { text?.toCharSequence()?.toString()?.takeIf { it.isNotBlank() } }.getOrNull() + /** Pulls the [androidx.core.graphics.drawable.IconCompat] out of a car image, if any. */+ private fun carIcon(icon: androidx.car.app.model.CarIcon?): androidx.core.graphics.drawable.IconCompat? =+ runCatching { icon?.icon }.getOrNull()+ private fun laneArrows(directions: List<LaneDirection>): String = directions.joinToString("") { dir -> when (runCatching { dir.shape }.getOrNull()) { LaneDirection.SHAPE_STRAIGHT -> STRAIGHT_ARROW