weartiles: shared Wear Tile seam (MaTileService)
- Commit
ad5a55e116a91b424649e08248b8bcf9d370b901- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-06 03:41 UTC (9 hours ago)
- Parents
8a160e3
Wear OS Tiles are the watch equivalent of phone Glance widgets: swipeable watch-home cards rendered from one immutable local snapshot, never the network. Phone widgets already share this shape through MaWidget in :library:widgets; tiles now share it through MaTileService in the new :library:weartiles module. MaTileService (Suspending-free TileService over tiles 1.5.0 / protolayout 1.3.0, first-party AndroidX pinned in the version catalog) owns timeline, freshness interval, resources version, centered ProtoLayout (icon, app name, primary, secondary), whole-tile tap launching the app's watch entry activity, and a dash fallback when the snapshot read fails. Apps keep only loadSnapshot/previewSnapshot/launchActivityName and call MaTileService.requestUpdate from their wear store when a fresh conduit snapshot lands, alongside the periodic freshness cadence. TileLayout.build is pure ProtoLayout construction so unit tests assert the rendered proto (snapshot text, launch target, icon resource id) without a device; TileLayoutTest covers it plus the empty-snapshot fallback. Verified: :library:weartiles:testDevUnitTest, :lint and :lintVitalRelease green.
Diff
AGENTS.md
diff --git a/AGENTS.md b/AGENTS.mdindex f29c0c591a..80e5cae574 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -37,6 +37,7 @@ Monorepo: ~100 Gradle modules + Rust workspace. Do scoped work only. | image/media | `library/image/`, `library/media/` | image loading (offline-local; network apps opt into HTTP via `Builder.remote()` with a `NetworkClient` fetch fn) / AV playback | | e2ee-p2p | `library/e2ee-p2p/` | E2EE, p2p, `Hkdf` | | widgets/work | `library/widgets/`, `library/work/` | Glance widgets / WorkManager |+| weartiles | `library/weartiles/` | Wear OS Tiles (`MaTileService`, ProtoLayout) — watch equivalent of Glance widgets | | biometric/ink | `library/biometric/`, `library/ink/` | biometric / stylus ink | | downloadservice | `library/downloadservice/` | foreground downloads (INTERNET itself) | | format | `library/format/` | relative-age bucketing + `"<amount>m ago"` wording (opt-in; calendar dates stay in `library/ui` `DateString`) |
gradle/libs.versions.toml
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.tomlindex 7c5594b726..ed887985c1 100644--- a/gradle/libs.versions.toml+++ b/gradle/libs.versions.toml@@ -56,6 +56,10 @@ material3AdaptiveNavigationSuite = "1.5.0-beta01" # (all 1.12.1 in that BOM) with no consumer using a 1.13-only API (direct consumers: # :library:tv, :library:map, :library:image, :photos use layout/lazy APIs present in 1.12.1). wearComposeMaterial = "1.7.0-alpha05"+# Wear OS Tiles (swipeable watch-home cards) + ProtoLayout rendering.+# First-party AndroidX only (see SUPPLY_CHAIN_RISKS.md note in library/weartiles).+wearTiles = "1.5.0"+protolayout = "1.3.0" # TV Material: Material Design for a ten-foot UI, where Material 3's focus handling does not reach. tv = "1.1.0" @@ -260,6 +264,9 @@ androidx-compose-material3 = { group = "androidx.compose.material3", name = "mat androidx-compose-material-icons-extended = { group = "androidx.compose.material", name = "material-icons-extended" } androidx-compose-material3-adaptive-navigation-suite = { group = "androidx.compose.material3", name = "material3-adaptive-navigation-suite", version.ref = "material3AdaptiveNavigationSuite" } androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }+androidx-concurrent-futures = { group = "androidx.concurrent", name = "concurrent-futures", version = "1.3.0" }+androidx-wear-tiles = { group = "androidx.wear.tiles", name = "tiles", version.ref = "wearTiles" }+androidx-wear-protolayout = { group = "androidx.wear.protolayout", name = "protolayout", version.ref = "protolayout" } 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
library/weartiles/AGENTS.md
diff --git a/library/weartiles/AGENTS.md b/library/weartiles/AGENTS.mdnew file mode 100644index 0000000000..2fbf420b21--- /dev/null+++ b/library/weartiles/AGENTS.md@@ -0,0 +1,35 @@+# AGENTS.md — library/weartiles/ (':library:weartiles')++> Scoped supplement. Global rules in root `AGENTS.md` apply.++- Gradle: :library:weartiles / dir: library/weartiles/+- Package: `com.vayunmathur.library.weartiles` (flat, one package)+- Entry files: `MaTileService.kt` (shared TileService shell), `TileSnapshot.kt`, `TileLayout.kt`+- Deps: `androidx.wear.tiles:tiles` + `androidx.wear.protolayout:protolayout`+ (first-party AndroidX, pinned in `gradle/libs.versions.toml`; no third-party+ supply-chain risk beyond Google Maven, same as Glance/Wear Compose)++## Purpose+Wear OS Tiles — the swipeable watch-home cards, the watch equivalent of+phone Glance widgets (`:library:widgets` / `MaWidget`). A tile renders from+one immutable local snapshot, never from the network: each watch-capable+app's wear-side store (conduit-synced JSON in app-private storage) is the+data source, exactly as phone widgets render from cache alone.++## How to use+Each app declares one `widget/<App>TileService.kt` subclass of+`MaTileService` in its own module/manifest (tiles belong to each app like+phone widgets do; the Watch conduit app owns no other app's tile):+override `loadSnapshot()` (read the wear store, map to `TileSnapshot`),+`previewSnapshot()` (deterministic sample for tests/previews), and+`launchActivityName` (the watch entry activity a tap opens). Call+`MaTileService.requestUpdate(context, ServiceClass)` from the wear store+when a fresh snapshot lands; the tile also self-refreshes on the+`freshnessIntervalMillis` timeline cadence. Layout, timeline, resources+versioning, click-to-launch and fallback rendering live here, not per app.++## Verify+```+./gradlew :library:weartiles:lint :library:weartiles:lintVitalRelease+./gradlew :library:weartiles:testDevUnitTest+```
library/weartiles/build.gradle.kts
diff --git a/library/weartiles/build.gradle.kts b/library/weartiles/build.gradle.ktsnew file mode 100644index 0000000000..5513224835--- /dev/null+++ b/library/weartiles/build.gradle.kts@@ -0,0 +1,16 @@+plugins {+ id("common-conventions-library")+}++android {+ namespace = "com.vayunmathur.library.weartiles"+}++dependencies {+ api(libs.androidx.wear.tiles)+ api(libs.androidx.wear.protolayout)+ api(libs.androidx.concurrent.futures)+ implementation(libs.kotlinx.coroutines.android)+ testImplementation(libs.kotlin.test)+ testImplementation(libs.kotlin.test.junit)+}
library/weartiles/src/main/java/com/vayunmathur/library/weartiles/MaTileService.kt
diff --git a/library/weartiles/src/main/java/com/vayunmathur/library/weartiles/MaTileService.kt b/library/weartiles/src/main/java/com/vayunmathur/library/weartiles/MaTileService.ktnew file mode 100644index 0000000000..88e17df522--- /dev/null+++ b/library/weartiles/src/main/java/com/vayunmathur/library/weartiles/MaTileService.kt@@ -0,0 +1,91 @@+package com.vayunmathur.library.weartiles++import android.content.Context+import androidx.wear.protolayout.LayoutElementBuilders+import androidx.wear.protolayout.ResourceBuilders+import androidx.wear.protolayout.TimelineBuilders+import androidx.wear.tiles.RequestBuilders+import androidx.wear.tiles.TileBuilders+import androidx.wear.tiles.TileService+import androidx.concurrent.futures.CallbackToFutureAdapter+import com.google.common.util.concurrent.ListenableFuture+import com.vayunmathur.library.log.Log++/**+ * Shared Wear Tile shell for every Modern-Apps watch tile — the watch+ * equivalent of `MaWidget` for phone Glance widgets.+ *+ * A tile keeps only what is per-app: [loadSnapshot] reads the app's+ * wear-side store (the latest conduit-synced snapshot in app-private+ * storage) and maps it to a [TileSnapshot]; [previewSnapshot] supplies a+ * deterministic sample; [launchActivityName] is the watch entry activity+ * a tap opens. Timeline, freshness, resources versioning, layout and+ * failure fallback live here. Rendering never touches the network.+ *+ * Refresh: the system re-asks on [TileSnapshot.freshnessMillis], and the+ * wear store calls [requestUpdate] the moment a fresh snapshot lands.+ */+abstract class MaTileService : TileService() {++ /** Fully-qualified watch entry activity a tile tap opens. */+ protected abstract val launchActivityName: String++ /** Read the latest local wear snapshot; cheap, offline, may throw — failures render a fallback. */+ protected abstract fun loadSnapshot(context: Context): TileSnapshot++ /** Deterministic sample snapshot for tile previews and tests. */+ abstract fun previewSnapshot(): TileSnapshot++ protected open val logTag: String get() = this::class.simpleName ?: "MaTileService"++ @Suppress("TooGenericExceptionCaught")+ final override fun onTileRequest(requestParams: RequestBuilders.TileRequest): ListenableFuture<TileBuilders.Tile> {+ val snapshot = try {+ loadSnapshot(this)+ } catch (e: Exception) {+ Log.error(logTag, "loadSnapshot failed", e)+ TileSnapshot.empty(previewSnapshot().appName)+ }+ val layout = TileLayout.build(snapshot, packageName, launchActivityName)+ val entry = TimelineBuilders.TimelineEntry.Builder().setLayout(+ LayoutElementBuilders.Layout.Builder().setRoot(layout).build(),+ ).build()+ val tile = TileBuilders.Tile.Builder()+ .setResourcesVersion(RESOURCES_VERSION)+ .setFreshnessIntervalMillis(snapshot.freshnessMillis)+ .setTileTimeline(TimelineBuilders.Timeline.Builder().addTimelineEntry(entry).build())+ .build()+ return CallbackToFutureAdapter.getFuture { it.set(tile); "MaTile" }+ }++ final override fun onTileResourcesRequest(+ requestParams: RequestBuilders.ResourcesRequest,+ ): ListenableFuture<ResourceBuilders.Resources> {+ val builder = ResourceBuilders.Resources.Builder().setVersion(RESOURCES_VERSION)+ val iconResId = try {+ loadSnapshot(this).iconResId+ } catch (_: Exception) {+ previewSnapshot().iconResId+ }+ if (iconResId != 0) {+ builder.addIdToImageMapping(+ TILE_ICON_RESOURCE_ID,+ ResourceBuilders.ImageResource.Builder()+ .setAndroidResourceByResId(+ ResourceBuilders.AndroidImageResourceByResId.Builder().setResourceId(iconResId).build(),+ )+ .build(),+ )+ }+ return CallbackToFutureAdapter.getFuture { it.set(builder.build()); "MaTileRes" }+ }++ companion object {+ const val RESOURCES_VERSION: String = "1"++ /** Ask the tile host to re-render [serviceClass] now (snapshot-change refresh). */+ fun requestUpdate(context: Context, serviceClass: Class<out TileService>) {+ runCatching { getUpdater(context).requestUpdate(serviceClass) }+ }+ }+}
library/weartiles/src/main/java/com/vayunmathur/library/weartiles/TileLayout.kt
diff --git a/library/weartiles/src/main/java/com/vayunmathur/library/weartiles/TileLayout.kt b/library/weartiles/src/main/java/com/vayunmathur/library/weartiles/TileLayout.ktnew file mode 100644index 0000000000..8a4fc10830--- /dev/null+++ b/library/weartiles/src/main/java/com/vayunmathur/library/weartiles/TileLayout.kt@@ -0,0 +1,68 @@+package com.vayunmathur.library.weartiles++import androidx.wear.protolayout.ActionBuilders+import androidx.wear.protolayout.DimensionBuilders+import androidx.wear.protolayout.LayoutElementBuilders+import androidx.wear.protolayout.ModifiersBuilders++/** Icon resource id referenced by the layout; served by `MaTileService` resources. */+const val TILE_ICON_RESOURCE_ID: String = "tile_icon"++/**+ * Pure ProtoLayout builder for a [TileSnapshot]: centered, Material-style+ * column (optional icon, app name, primary value, secondary line) whose+ * whole surface is one clickable that launches [launchClassName] in+ * [packageName]. Kept Android-free so unit tests can assert the proto.+ */+object TileLayout {++ fun build(snapshot: TileSnapshot, packageName: String, launchClassName: String): LayoutElementBuilders.LayoutElement {+ val launch = ActionBuilders.LaunchAction.Builder()+ .setAndroidActivity(+ ActionBuilders.AndroidActivity.Builder()+ .setPackageName(packageName)+ .setClassName(launchClassName)+ .build(),+ )+ .build()+ val clickable = ModifiersBuilders.Clickable.Builder()+ .setId("open_app")+ .setOnClick(launch)+ .build()+ val column = LayoutElementBuilders.Column.Builder()+ .setHorizontalAlignment(LayoutElementBuilders.HORIZONTAL_ALIGN_CENTER)+ .setModifiers(ModifiersBuilders.Modifiers.Builder().setClickable(clickable).build())+ if (snapshot.iconResId != 0) {+ column.addContent(+ LayoutElementBuilders.Image.Builder()+ .setResourceId(TILE_ICON_RESOURCE_ID)+ .setWidth(DimensionBuilders.dp(32f))+ .setHeight(DimensionBuilders.dp(32f))+ .build(),+ )+ column.addContent(spacer())+ }+ column.addContent(text(snapshot.appName, spSize = 12f, bold = false))+ column.addContent(text(snapshot.primary, spSize = 28f, bold = true))+ if (snapshot.secondary.isNotEmpty()) {+ column.addContent(text(snapshot.secondary, spSize = 14f, bold = false))+ }+ return LayoutElementBuilders.Box.Builder()+ .setHorizontalAlignment(LayoutElementBuilders.HORIZONTAL_ALIGN_CENTER)+ .setVerticalAlignment(LayoutElementBuilders.VERTICAL_ALIGN_CENTER)+ .addContent(column.build())+ .build()+ }++ private fun spacer(): LayoutElementBuilders.LayoutElement =+ LayoutElementBuilders.Spacer.Builder()+ .setWidth(DimensionBuilders.dp(4f))+ .setHeight(DimensionBuilders.dp(6f))+ .build()++ private fun text(value: String, spSize: Float, bold: Boolean): LayoutElementBuilders.LayoutElement {+ val font = LayoutElementBuilders.FontStyle.Builder().setSize(DimensionBuilders.sp(spSize))+ if (bold) font.setWeight(LayoutElementBuilders.FONT_WEIGHT_BOLD)+ return LayoutElementBuilders.Text.Builder().setText(value).setFontStyle(font.build()).build()+ }+}
library/weartiles/src/main/java/com/vayunmathur/library/weartiles/TileSnapshot.kt
diff --git a/library/weartiles/src/main/java/com/vayunmathur/library/weartiles/TileSnapshot.kt b/library/weartiles/src/main/java/com/vayunmathur/library/weartiles/TileSnapshot.ktnew file mode 100644index 0000000000..93e4ea8ada--- /dev/null+++ b/library/weartiles/src/main/java/com/vayunmathur/library/weartiles/TileSnapshot.kt@@ -0,0 +1,32 @@+package com.vayunmathur.library.weartiles++/**+ * One immutable glanceable snapshot a Wear Tile renders from.+ *+ * Tiles render from the latest locally persisted wear snapshot only+ * (conduit-synced, app-private storage) — never from the network, exactly+ * as phone Glance widgets render from cache alone. An app maps its wear+ * store to this shape; [MaTileService] owns layout/timeline/resources.+ *+ * @property appName short app label shown at the top of the tile.+ * @property primary the one big glanceable value (temperature, next alarm…).+ * @property secondary supporting line (condition, event time, task title…).+ * @property iconResId drawable shown above the text; 0 renders text-only.+ * @property freshnessMillis how long the timeline entry stays fresh before+ * the system re-asks the service (periodic freshness per Tiles conventions).+ */+data class TileSnapshot(+ val appName: String,+ val primary: String,+ val secondary: String = "",+ val iconResId: Int = 0,+ val freshnessMillis: Long = DEFAULT_FRESHNESS_MILLIS,+) {+ companion object {+ /** Default periodic freshness: tiles re-render at least every 15 min. */+ const val DEFAULT_FRESHNESS_MILLIS: Long = 15 * 60_000L++ /** Honest empty state used before the first conduit snapshot lands. */+ fun empty(appName: String): TileSnapshot = TileSnapshot(appName, "—", "Open app to sync")+ }+}
library/weartiles/src/test/java/com/vayunmathur/library/weartiles/TileLayoutTest.kt
diff --git a/library/weartiles/src/test/java/com/vayunmathur/library/weartiles/TileLayoutTest.kt b/library/weartiles/src/test/java/com/vayunmathur/library/weartiles/TileLayoutTest.ktnew file mode 100644index 0000000000..8f8f7054ae--- /dev/null+++ b/library/weartiles/src/test/java/com/vayunmathur/library/weartiles/TileLayoutTest.kt@@ -0,0 +1,28 @@+package com.vayunmathur.library.weartiles++import kotlin.test.Test+import kotlin.test.assertTrue++class TileLayoutTest {++ @Test+ fun layoutCarriesSnapshotTextAndLaunchTarget() {+ val snapshot = TileSnapshot(appName = "Weather", primary = "21°", secondary = "Clear", iconResId = 42)+ val proto = TileLayout.build(snapshot, "com.vayunmathur.weather", "com.vayunmathur.weather.ui.WearMainActivity")+ .toFingerprint()+ assertTrue(proto.contains("Weather"), proto)+ assertTrue(proto.contains("21°"), proto)+ assertTrue(proto.contains("Clear"), proto)+ assertTrue(proto.contains("com.vayunmathur.weather"), proto)+ assertTrue(proto.contains(TILE_ICON_RESOURCE_ID), proto)+ }++ @Test+ fun emptySnapshotRendersDashFallback() {+ val proto = TileLayout.build(TileSnapshot.empty("Clock"), "com.vayunmathur.clock", "X").toFingerprint()+ assertTrue(proto.contains("—"), proto)+ }+}++private fun androidx.wear.protolayout.LayoutElementBuilders.LayoutElement.toFingerprint(): String =+ toString()
settings.gradle.kts
diff --git a/settings.gradle.kts b/settings.gradle.ktsindex 63e9d1e531..c2796217ba 100644--- a/settings.gradle.kts+++ b/settings.gradle.kts@@ -83,6 +83,7 @@ include(":library:media") include(":library:log") include(":library:format") include(":library:wearlink")+include(":library:weartiles") include(":watch") include(":watch:wear") project(":watch:wear").projectDir = file("watch-wear")