clock: add Wear Tile for next alarm
- Commit
ac81fcbc048f2e2eab810df83ab7ac4fad4b81e1- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-06 05:38 UTC (7 hours ago)
- Parents
6a1f0e9
ClockTileService (service/, on the shared :library:weartiles MaTileService seam) renders the next enabled alarm from WearClockStore's conduit snapshot โ computed watch-locally from hour/minute/daysMask in the watch timezone, matching how the watch arms its own alarms โ or the current time with 'No alarms' when nothing is enabled. Tap opens WearMainActivity. WearClockStore requests a tile update on each new snapshot; the timeline also carries 15-minute freshness. The next-trigger mapping is pure (now and zone injected) and covered by ClockTileServiceTest, including the ProtoLayout proto assertion. Verified: :clock:testDevUnitTest (all green), :clock:lint, :clock:lintVitalRelease.
Diff
clock/AGENTS.md
diff --git a/clock/AGENTS.md b/clock/AGENTS.mdindex a33c8210af..80393861e2 100644--- a/clock/AGENTS.md+++ b/clock/AGENTS.md@@ -122,3 +122,4 @@ same way โ `platform/BootReceiver` (phone) and ./gradlew :clock:lint ./gradlew :clock:checkMetadata ```+- Wear Tile: `service/ClockTileService` (a `MaTileService` in `:library:weartiles`) renders the next enabled alarm (or the time with `No alarms`) from `WearClockStore`; the store requests a tile update on every new snapshot, plus 15-minute freshness. Tap opens `WearMainActivity`.
clock/build.gradle.kts
diff --git a/clock/build.gradle.kts b/clock/build.gradle.ktsindex 2b07c069a8..318a2b5e63 100644--- a/clock/build.gradle.kts+++ b/clock/build.gradle.kts@@ -30,6 +30,7 @@ dependencies { // sync/WearClockSync publishes the phone snapshot, sync/WearLinkBridge // receives it on a watch. FormFactor picks the side at runtime. implementation(project(":library:wearlink"))+ implementation(project(":library:weartiles")) // Watch experience (ui/Wear*): Wear Compose + the repo log facade the // watch platform/sync code writes through. implementation(libs.androidx.wear.compose.material)
clock/src/main/AndroidManifest.xml
diff --git a/clock/src/main/AndroidManifest.xml b/clock/src/main/AndroidManifest.xmlindex e7a6cacbe9..7511937835 100644--- a/clock/src/main/AndroidManifest.xml+++ b/clock/src/main/AndroidManifest.xml@@ -192,5 +192,19 @@ <action android:name="com.vayunmathur.watch.action.SYNC_TRIGGER" /> </intent-filter> </receiver>++ <!-- Wear OS Tile (watch-home card): renders from the wear snapshot store. -->+ <service+ android:name=".service.ClockTileService"+ android:exported="true"+ android:icon="@mipmap/ic_launcher"+ android:label="Clock">+ <intent-filter>+ <action android:name="androidx.wear.tiles.action.BIND_TILE_PROVIDER" />+ </intent-filter>+ <meta-data+ android:name="androidx.wear.tiles.PREVIEW"+ android:resource="@mipmap/ic_launcher" />+ </service> </application> </manifest>\ No newline at end of file
clock/src/main/java/com/vayunmathur/clock/data/WearClockStore.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/data/WearClockStore.kt b/clock/src/main/java/com/vayunmathur/clock/data/WearClockStore.ktindex 83fe661ee4..bdadce5dfd 100644--- a/clock/src/main/java/com/vayunmathur/clock/data/WearClockStore.kt+++ b/clock/src/main/java/com/vayunmathur/clock/data/WearClockStore.kt@@ -7,7 +7,9 @@ import com.vayunmathur.clock.domain.WatchStopwatch import com.vayunmathur.clock.domain.WatchTimer import com.vayunmathur.library.wearlink.clock.WearClockProtocol import com.vayunmathur.library.wearlink.clock.WearClockSnapshot+import com.vayunmathur.clock.service.ClockTileService import com.vayunmathur.library.log.Log+import com.vayunmathur.library.weartiles.MaTileService import java.io.File import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow@@ -57,6 +59,7 @@ class WearClockStore private constructor(private val context: Context) { /** Replace the phone snapshot (WearLink data item); arming is reconciled by the bridge. */ fun setSnapshot(snapshot: WearClockSnapshot) { update { it.copy(snapshot = snapshot) }+ MaTileService.requestUpdate(context, ClockTileService::class.java) } /** Record the outcome of reconciling the snapshot against AlarmManager. */
clock/src/main/java/com/vayunmathur/clock/service/ClockTileService.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/service/ClockTileService.kt b/clock/src/main/java/com/vayunmathur/clock/service/ClockTileService.ktnew file mode 100644index 0000000000..fe9f121af7--- /dev/null+++ b/clock/src/main/java/com/vayunmathur/clock/service/ClockTileService.kt@@ -0,0 +1,56 @@+package com.vayunmathur.clock.service++import android.content.Context+import com.vayunmathur.library.weartiles.MaTileService+import com.vayunmathur.library.weartiles.TileSnapshot+import com.vayunmathur.library.wearlink.clock.WearClockAlarm+import com.vayunmathur.clock.data.WearClockStore+import java.time.Instant+import java.time.LocalDateTime+import java.time.ZoneId+import java.time.format.DateTimeFormatter++/** Wear Tile for Clock: the next enabled alarm, or the time when none is set. */+class ClockTileService : MaTileService() {+ override val launchActivityName: String = "com.vayunmathur.clock.ui.WearMainActivity"++ override fun loadSnapshot(context: Context): TileSnapshot {+ val alarms = WearClockStore.get(context).state.value.snapshot.alarms+ return clockTileSnapshot(alarms, System.currentTimeMillis(), ZoneId.systemDefault())+ }++ override fun previewSnapshot(): TileSnapshot =+ TileSnapshot(appName = "Clock", primary = "7:30 AM", secondary = "Next alarm")+}++/** Pure next-alarm mapping; [nowMillis] is injected so tests stay deterministic. */+fun clockTileSnapshot(alarms: List<WearClockAlarm>, nowMillis: Long, zone: ZoneId): TileSnapshot {+ val next = alarms.filter { it.enabled }+ .mapNotNull { alarm -> nextTriggerMillis(alarm, nowMillis, zone)?.let { it to alarm } }+ .minByOrNull { it.first }+ val timeFormat = DateTimeFormatter.ofPattern("h:mm a")+ return if (next == null) {+ val now = LocalDateTime.ofInstant(Instant.ofEpochMilli(nowMillis), zone)+ TileSnapshot(appName = "Clock", primary = now.format(timeFormat), secondary = "No alarms")+ } else {+ val at = LocalDateTime.ofInstant(Instant.ofEpochMilli(next.first), zone)+ val label = next.second.label.ifEmpty { "Alarm" }+ TileSnapshot(appName = "Clock", primary = at.format(timeFormat), secondary = label)+ }+}++/** Next fire time for [alarm] after [nowMillis]; weekday bit 0 = Sunday, 0 mask = one-shot. */+fun nextTriggerMillis(alarm: WearClockAlarm, nowMillis: Long, zone: ZoneId): Long? {+ val now = LocalDateTime.ofInstant(Instant.ofEpochMilli(nowMillis), zone)+ for (dayOffset in 0..7) {+ val date = now.toLocalDate().plusDays(dayOffset.toLong())+ if (alarm.daysMask != 0) {+ val bit = date.dayOfWeek.value % 7+ if (alarm.daysMask and (1 shl bit) == 0) continue+ }+ val candidate = date.atTime(alarm.hour, alarm.minute).atZone(zone).toInstant().toEpochMilli()+ if (candidate > nowMillis) return candidate+ if (alarm.daysMask == 0 && dayOffset > 0) return null+ }+ return null+}
clock/src/test/java/com/vayunmathur/clock/service/ClockTileServiceTest.kt
diff --git a/clock/src/test/java/com/vayunmathur/clock/service/ClockTileServiceTest.kt b/clock/src/test/java/com/vayunmathur/clock/service/ClockTileServiceTest.ktnew file mode 100644index 0000000000..1d8840abd3--- /dev/null+++ b/clock/src/test/java/com/vayunmathur/clock/service/ClockTileServiceTest.kt@@ -0,0 +1,34 @@+package com.vayunmathur.clock.service++import com.vayunmathur.library.weartiles.TileLayout+import com.vayunmathur.library.wearlink.clock.WearClockAlarm+import java.time.ZoneId+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++class ClockTileServiceTest {+ private val zone: ZoneId = ZoneId.of("UTC")+ // Fixed: 2026-01-05T06:00:00Z (a Monday).+ private val now: Long = java.time.LocalDateTime.of(2026, 1, 5, 6, 0).atZone(zone).toInstant().toEpochMilli()++ @Test+ fun nextEnabledAlarmWins() {+ val alarms = listOf(+ WearClockAlarm(alarmId = 1, hour = 9, minute = 0, enabled = true, label = "Work"),+ WearClockAlarm(alarmId = 2, hour = 7, minute = 30, enabled = true, label = "Wake"),+ WearClockAlarm(alarmId = 3, hour = 6, minute = 30, enabled = false, label = "Off"),+ )+ val tile = clockTileSnapshot(alarms, now, zone)+ assertEquals("7:30 AM", tile.primary)+ assertEquals("Wake", tile.secondary)+ assertTrue(TileLayout.build(tile, "com.vayunmathur.clock", "X").toString().contains("7:30 AM"))+ }++ @Test+ fun noAlarmShowsTime() {+ val tile = clockTileSnapshot(emptyList(), now, zone)+ assertEquals("6:00 AM", tile.primary)+ assertEquals("No alarms", tile.secondary)+ }+}