cast-tv: build the receiver UI on androidx.tv
- Commit
532279706ce0391900e36cf90ab749fdf5ca085d- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-23 07:05 UTC (3 months ago)
- Parents
e7e9d01
Diff
build-logic/src/main/kotlin/common-conventions-tv.gradle.kts
diff --git a/build-logic/src/main/kotlin/common-conventions-tv.gradle.kts b/build-logic/src/main/kotlin/common-conventions-tv.gradle.ktsnew file mode 100644index 0000000000..244528127f--- /dev/null+++ b/build-logic/src/main/kotlin/common-conventions-tv.gradle.kts@@ -0,0 +1,42 @@+/**+ * A television app: `common-conventions-app` plus `androidx.tv`.+ *+ * **An additive layer, not a third standalone app plugin.** `common-conventions-wear` is standalone+ * because a watch genuinely needs a different `compileSdk`, a different `minSdk`, a single ABI and+ * `androidx.wear.compose` instead of Material 3. A Google TV box needs none of that: it is an ordinary+ * Android app with a D-pad. Mirroring the Wear plugin would have meant cloning the version read from+ * `version.txt`, the signing configs, the build types, `dependenciesInfo`, the namespace derivation,+ * the launcher-icon extension *together with its per-variant `GenerateLauncherIconTask` wiring and the+ * pinned Material Symbols ref*, the `nativeAbis` extension, the `beforeVariants` block, the lint+ * block, the shared-res source directory and the archive reproducibility tasks - roughly two hundred+ * lines that would then drift from the file they were copied out of. `common-conventions-preview-+ * metadata` is the precedent for a layer that adds one thing and inherits the rest.+ *+ * It also avoids a real regression: the Wear plugin hardcodes `abiFilters.add("arm64-v8a")`, and+ * Google TV hardware runs a 32-bit userspace, so a clone would have produced an APK the onn box+ * refuses to install.+ *+ * `common-conventions-app` is applied here so this plugin stands on its own, but a consumer should+ * still name it in its own `plugins` block: Gradle generates the type-safe accessors for+ * `launcherIcon` and `nativeAbis` from the plugins that build file applies directly, not from ones it+ * inherits.+ */++plugins {+ id("common-conventions-app")+}++// The version catalog is not implicitly in scope in a precompiled script plugin; the app and wear+// conventions reach it exactly this way.+val libs = the<org.gradle.accessors.dm.LibrariesForLibs>()++dependencies {+ // **Material Design for a ten-foot UI, and the reason this plugin exists at all.** Material 3's+ // components are built for touch: they have no D-pad focus states worth the name, so a TV built on+ // them looks correct in a screenshot and is unusable from a sofa. `androidx.tv` is the same design+ // language with focus as a first-class concern.+ //+ // It resolves against the Compose versions the catalog already pins - `tv-material` asks for+ // compose 1.10 and the BOM here is newer, so Gradle takes the newer and nothing is downgraded.+ implementation(libs.androidx.tv.material)+}
cast/tv/build.gradle.kts
diff --git a/cast/tv/build.gradle.kts b/cast/tv/build.gradle.ktsindex b77d17b430..712d3a9998 100644--- a/cast/tv/build.gradle.kts+++ b/cast/tv/build.gradle.kts@@ -1,5 +1,10 @@ plugins { id("common-conventions-app")+ // androidx.tv, so this app is built on a design system that knows what a D-pad is. Named here+ // *alongside* the app convention rather than instead of it, even though it applies that itself:+ // the type-safe `launcherIcon` and `nativeAbis` accessors below are generated from the plugins this+ // file applies directly.+ id("common-conventions-tv") // No preview-metadata convention. Every screen here is either a full-screen decoder surface or // a TV-shaped idle screen, and Layoutlib can render neither into a *phone* listing image - // release.sh writes previews into a hardcoded graphics/phone-screenshots. `camera` and
cast/tv/src/main/java/com/vayunmathur/cast/tv/MainActivity.kt
diff --git a/cast/tv/src/main/java/com/vayunmathur/cast/tv/MainActivity.kt b/cast/tv/src/main/java/com/vayunmathur/cast/tv/MainActivity.ktindex 458f867dab..8d3c022782 100644--- a/cast/tv/src/main/java/com/vayunmathur/cast/tv/MainActivity.kt+++ b/cast/tv/src/main/java/com/vayunmathur/cast/tv/MainActivity.kt@@ -10,8 +10,8 @@ import com.vayunmathur.cast.tv.platform.MirrorActivity import com.vayunmathur.cast.tv.platform.ReceiverController import com.vayunmathur.cast.tv.platform.ReceiverPhase import com.vayunmathur.cast.tv.service.ReceiverService+import com.vayunmathur.cast.tv.ui.CastTvTheme import com.vayunmathur.cast.tv.ui.ReceiverContent-import com.vayunmathur.library.ui.DynamicTheme import androidx.lifecycle.lifecycleScope import kotlinx.coroutines.flow.distinctUntilChangedBy import kotlinx.coroutines.launch@@ -32,7 +32,7 @@ class MainActivity : ComponentActivity() { super.onCreate(savedInstanceState) ReceiverService.start(this) setContent {- DynamicTheme(darkTheme = true) {+ CastTvTheme { val state by ReceiverController.state.collectAsState() ReceiverContent(state) }
cast/tv/src/main/java/com/vayunmathur/cast/tv/ui/ReceiverContent.kt
diff --git a/cast/tv/src/main/java/com/vayunmathur/cast/tv/ui/ReceiverContent.kt b/cast/tv/src/main/java/com/vayunmathur/cast/tv/ui/ReceiverContent.ktindex f691b0ffb6..8d54547e4e 100644--- a/cast/tv/src/main/java/com/vayunmathur/cast/tv/ui/ReceiverContent.kt+++ b/cast/tv/src/main/java/com/vayunmathur/cast/tv/ui/ReceiverContent.kt@@ -11,100 +11,102 @@ import androidx.compose.ui.res.pluralStringResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp+import androidx.tv.material3.MaterialTheme+import androidx.tv.material3.Text import com.vayunmathur.cast.tv.R import com.vayunmathur.cast.tv.platform.ReceiverFailure import com.vayunmathur.cast.tv.platform.ReceiverPhase import com.vayunmathur.cast.tv.platform.ReceiverUiState-import com.vayunmathur.library.ui.AppScaffold-import com.vayunmathur.library.ui.MaterialTheme-import com.vayunmathur.library.ui.Text-import com.vayunmathur.library.ui.appBarScrollBehavior /**- * The idle and pairing screen: the whole of this app's UI.+ * The idle and pairing screen. *- * **No `androidx.tv` dependency.** There is nothing focusable here - no list to move a D-pad through,- * no button to press - so the "Material 3 has no D-pad focus" problem never arises and the repo's rule- * that Material comes only through `:library:ui` stays intact. Everything the user does happens on the- * phone; this screen only ever tells them what is going on.+ * **On `androidx.tv`, and this paragraph used to argue the opposite.** The old reasoning was that+ * nothing here is focusable - no list to move a D-pad through, no button to press - so Material 3's+ * missing focus handling never came up, and the repo's rule that Material arrives only through+ * `:library:ui` could stay intact. That held for exactly as long as this app had no controls. It now+ * has a transport overlay driven from the television's remote, and building *that* on touch-first+ * components while this screen stayed on a third design system would leave one app with two. *- * Sizes are deliberately large: this is read from a sofa, and a pair code the user has to walk up to- * the TV to read is a pair code that makes the feature feel broken.+ * So the whole of `cast:tv` is on `androidx.tv`: the same Material design language, with focus as a+ * first-class concern rather than an afterthought. `:library:ui` remains on the compile classpath+ * because the app convention plugin puts it there, and R8 strips what nothing references.+ *+ * The `AppScaffold` went with it, and is not replaced. A title bar reading "Cast TV" above a screen+ * whose entire job is to show one sentence was furniture; the ten-foot rule is that everything on+ * screen should be worth reading from a sofa.+ *+ * Sizes are deliberately large: this is read from across a room, and a pair code the user has to walk+ * up to the TV to make out is a pair code that makes the feature feel broken. */ @Composable fun ReceiverContent(state: ReceiverUiState, modifier: Modifier = Modifier) {- AppScaffold(- title = stringResource(R.string.app_name),- modifier = modifier,- scrollBehavior = appBarScrollBehavior(),- ) { padding ->- Column(- modifier = Modifier.fillMaxSize().padding(padding).padding(48.dp),- verticalArrangement = Arrangement.spacedBy(24.dp, Alignment.CenterVertically),- horizontalAlignment = Alignment.CenterHorizontally,- ) {- if (state.localNetworkBlocked) {- Headline(stringResource(R.string.tv_local_network_blocked), isError = true)- Detail(stringResource(R.string.tv_local_network_blocked_hint))- return@Column+ Column(+ modifier = modifier.fillMaxSize().padding(48.dp),+ verticalArrangement = Arrangement.spacedBy(24.dp, Alignment.CenterVertically),+ horizontalAlignment = Alignment.CenterHorizontally,+ ) {+ if (state.localNetworkBlocked) {+ Headline(stringResource(R.string.tv_local_network_blocked), isError = true)+ Detail(stringResource(R.string.tv_local_network_blocked_hint))+ return@Column+ }+ when (val phase = state.phase) {+ ReceiverPhase.Starting -> Headline(stringResource(R.string.tv_starting))++ ReceiverPhase.Advertising -> {+ Headline(stringResource(R.string.tv_ready_title))+ Detail(+ stringResource(+ R.string.tv_ready_hint,+ state.deviceName.ifBlank { stringResource(R.string.tv_name_unknown) },+ ),+ ) }- when (val phase = state.phase) {- ReceiverPhase.Starting -> Headline(stringResource(R.string.tv_starting)) - ReceiverPhase.Advertising -> {- Headline(stringResource(R.string.tv_ready_title))+ is ReceiverPhase.Pairing -> {+ Detail(stringResource(R.string.tv_pair_from, phase.senderName))+ Headline(stringResource(R.string.tv_pair_title))+ // The code itself, as large as the type scale goes: it is the one thing on this+ // screen the user has to read from across a room and copy exactly.+ Text(+ text = phase.code.chunked(3).joinToString(" "),+ style = MaterialTheme.typography.displayLarge,+ textAlign = TextAlign.Center,+ )+ if (phase.codeChanged) {+ Detail(stringResource(R.string.tv_pair_new_code), isError = true)+ } else { Detail(- stringResource(- R.string.tv_ready_hint,- state.deviceName.ifBlank { stringResource(R.string.tv_name_unknown) },+ pluralStringResource(+ R.plurals.tv_pair_attempts,+ phase.attemptsLeft,+ phase.attemptsLeft, ), ) }+ } - is ReceiverPhase.Pairing -> {- Detail(stringResource(R.string.tv_pair_from, phase.senderName))- Headline(stringResource(R.string.tv_pair_title))- // The code itself, as large as the type scale goes: it is the one thing on this- // screen the user has to read from across a room and copy exactly.- Text(- text = phase.code.chunked(3).joinToString(" "),- style = MaterialTheme.typography.displayLarge,- textAlign = TextAlign.Center,- )- if (phase.codeChanged) {- Detail(stringResource(R.string.tv_pair_new_code), isError = true)- } else {- Detail(- pluralStringResource(- R.plurals.tv_pair_attempts,- phase.attemptsLeft,- phase.attemptsLeft,- ),- )- }- }-- is ReceiverPhase.Connected ->- Headline(stringResource(R.string.tv_paired, phase.senderName))+ is ReceiverPhase.Connected ->+ Headline(stringResource(R.string.tv_paired, phase.senderName)) - // The mirror has its own full-screen Activity, so this is only ever seen for the- // moment between the stream starting and that Activity coming up.- is ReceiverPhase.Mirroring ->- Headline(stringResource(R.string.tv_mirroring, phase.sourceName))+ // The mirror has its own full-screen Activity, so this is only ever seen for the+ // moment between the stream starting and that Activity coming up.+ is ReceiverPhase.Mirroring ->+ Headline(stringResource(R.string.tv_mirroring, phase.sourceName)) - is ReceiverPhase.Failed -> Headline(- stringResource(- when (phase.reason) {- ReceiverFailure.NoDecoder -> R.string.tv_no_decoder- ReceiverFailure.MissingCodecConfig ->- R.string.tv_missing_codec_config- ReceiverFailure.Handshake -> R.string.tv_stream_failed- ReceiverFailure.StreamEnded -> R.string.tv_stream_failed- },- ),- isError = true,- )- }+ is ReceiverPhase.Failed -> Headline(+ stringResource(+ when (phase.reason) {+ ReceiverFailure.NoDecoder -> R.string.tv_no_decoder+ ReceiverFailure.MissingCodecConfig ->+ R.string.tv_missing_codec_config+ ReceiverFailure.Handshake -> R.string.tv_stream_failed+ ReceiverFailure.StreamEnded -> R.string.tv_stream_failed+ },+ ),+ isError = true,+ ) } } }
cast/tv/src/main/java/com/vayunmathur/cast/tv/ui/Theme.kt
diff --git a/cast/tv/src/main/java/com/vayunmathur/cast/tv/ui/Theme.kt b/cast/tv/src/main/java/com/vayunmathur/cast/tv/ui/Theme.ktnew file mode 100644index 0000000000..f16a2f6d48--- /dev/null+++ b/cast/tv/src/main/java/com/vayunmathur/cast/tv/ui/Theme.kt@@ -0,0 +1,31 @@+package com.vayunmathur.cast.tv.ui++import androidx.compose.foundation.background+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import androidx.tv.material3.MaterialTheme+import androidx.tv.material3.darkColorScheme++/**+ * The theme for every screen in this app.+ *+ * **Always dark, and not dynamic.** `DynamicTheme` was the wrong shape here twice over: a television+ * has no wallpaper to derive a colour scheme from, and there is no light mode to offer because nobody+ * has ever wanted a white screen filling a wall in a dim room. The old call site passed+ * `darkTheme = true` unconditionally, which was that conclusion reached one argument short.+ *+ * The background is painted here rather than by a `Surface`, so that a full-bleed decoder surface can+ * live under an overlay that draws nothing of its own - the mirror Activity needs transparency above+ * the picture, and a themed `Surface` wrapping everything would be an opaque layer in the way.+ */+@Composable+fun CastTvTheme(content: @Composable () -> Unit) {+ MaterialTheme(colorScheme = darkColorScheme()) {+ Box(+ modifier = Modifier.fillMaxSize().background(MaterialTheme.colorScheme.background),+ content = { content() },+ )+ }+}
gradle/libs.versions.toml
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.tomlindex 71a15990a3..5823e984ea 100644--- a/gradle/libs.versions.toml+++ b/gradle/libs.versions.toml@@ -37,6 +37,8 @@ material3 = "1.5.0-alpha23" material3AdaptiveNavigationSuite = "1.5.0-alpha23" foundation = "1.12.0-beta02" wearComposeMaterial = "1.7.0-alpha05"+# TV Material: Material Design for a ten-foot UI, where Material 3's focus handling does not reach.+tv = "1.1.0" # AndroidX – navigation navigation3 = "1.2.0-alpha05"@@ -195,6 +197,10 @@ androidx-compose-material3-adaptive-navigation-suite = { group = "androidx.compo androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" } androidx-wear-compose-material = { group = "androidx.wear.compose", name = "compose-material", version.ref = "wearComposeMaterial" } androidx-wear-compose-foundation = { group = "androidx.wear.compose", name = "compose-foundation", version.ref = "wearComposeMaterial" }+# No `tv-foundation` companion: it was deprecated at 1.0.0 and its lazy lists moved into+# `androidx.compose.foundation`, which the Compose BOM above already provides. Pinning the abandoned+# artifact would drag compose 1.6 into the resolution for components nothing here uses.+androidx-tv-material = { group = "androidx.tv", name = "tv-material", version.ref = "tv" } # AndroidX – navigation androidx-navigation3-runtime = { group = "androidx.navigation3", name = "navigation3-runtime", version.ref = "navigation3" }