Modern Apps git

← Modern-Apps.git

calendar: add Wear Tile for next event

Commit
e3f2428b41438a106865d4ee1acfc7aa6c2af9a2
Author
Vayun Mathur <[email protected]>
When
2026-12-06 05:38 UTC (7 hours ago)
Parents
ac81fcb
CalendarTileService (service/, on the shared
:library:weartiles MaTileService seam) renders the next
upcoming event (title + start time, 'All day' for all-day
events) from WearCalendarStore's conduit snapshot, or an
honest 'No events' when nothing is upcoming. Tap opens
WearMainActivity. WearCalendarStore requests a tile update
on each new snapshot; 15-minute freshness otherwise.

The next-event mapping is pure (now and zone injected) and
covered by CalendarTileServiceTest with a ProtoLayout proto
assertion. Verified: :calendar:lint and :lintVitalRelease
green; CalendarTileServiceTest green. Full
:calendar:testDevUnitTest has 2 pre-existing failures in
ImportGlanceHolidayTest on origin/main (it searches for the
'Search countries' hint that b0aa4ceb6 renamed to 'Search');
unrelated to this change and left alone.

Diff

calendar/AGENTS.md
diff --git a/calendar/AGENTS.md b/calendar/AGENTS.mdindex 129a9b10a7..30921ee740 100644--- a/calendar/AGENTS.md+++ b/calendar/AGENTS.md@@ -127,3 +127,4 @@ any phone state loads; phones get the month/agenda UI. The phone ./gradlew :calendar:lint ./gradlew :calendar:checkMetadata ```+- Wear Tile: `service/CalendarTileService` (a `MaTileService` in `:library:weartiles`) renders the next upcoming event from `WearCalendarStore`; the store requests a tile update on every new snapshot, plus 15-minute freshness. Tap opens `WearMainActivity`.
calendar/build.gradle.kts
diff --git a/calendar/build.gradle.kts b/calendar/build.gradle.ktsindex f57926204e..07ddefd2b2 100644--- a/calendar/build.gradle.kts+++ b/calendar/build.gradle.kts@@ -27,6 +27,7 @@ dependencies {     // sync/WearCalendarSync 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 for the round agenda UI.     implementation(libs.androidx.wear.compose.material)     implementation(libs.androidx.wear.compose.foundation)
calendar/src/main/AndroidManifest.xml
diff --git a/calendar/src/main/AndroidManifest.xml b/calendar/src/main/AndroidManifest.xmlindex 4687b4ce10..eec594f6a9 100644--- a/calendar/src/main/AndroidManifest.xml+++ b/calendar/src/main/AndroidManifest.xml@@ -212,6 +212,20 @@                 <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.CalendarTileService"+            android:exported="true"+            android:icon="@mipmap/ic_launcher"+            android:label="Calendar">+            <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
calendar/src/main/java/com/vayunmathur/calendar/data/WearCalendarStore.kt
diff --git a/calendar/src/main/java/com/vayunmathur/calendar/data/WearCalendarStore.kt b/calendar/src/main/java/com/vayunmathur/calendar/data/WearCalendarStore.ktindex d39f916aee..93c03aa57a 100644--- a/calendar/src/main/java/com/vayunmathur/calendar/data/WearCalendarStore.kt+++ b/calendar/src/main/java/com/vayunmathur/calendar/data/WearCalendarStore.kt@@ -5,6 +5,8 @@ import com.vayunmathur.library.wearlink.calendar.WearCalendarProtocol import com.vayunmathur.library.wearlink.calendar.WearCalendarSnapshot import com.vayunmathur.library.wearlink.calendar.WearWatchState import com.vayunmathur.calendar.sync.WearLinkBridge+import com.vayunmathur.calendar.service.CalendarTileService+import com.vayunmathur.library.weartiles.MaTileService import com.vayunmathur.library.log.Log import java.io.File import kotlinx.coroutines.flow.MutableStateFlow@@ -42,6 +44,7 @@ class WearCalendarStore private constructor(private val context: Context) {     /** Replace the stored snapshot (WearLink data item from the phone) and tell the phone it landed. */     fun setSnapshot(snapshot: WearCalendarSnapshot) {         _snapshot.value = snapshot+        MaTileService.requestUpdate(context, CalendarTileService::class.java)         runCatching { snapshotFile.writeText(WearCalendarProtocol.json.encodeToString(snapshot)) }             .onFailure { Log.status(TAG, "persist snapshot failed: ${it.message}") }         publishWatchState()
calendar/src/main/java/com/vayunmathur/calendar/service/CalendarTileService.kt
diff --git a/calendar/src/main/java/com/vayunmathur/calendar/service/CalendarTileService.kt b/calendar/src/main/java/com/vayunmathur/calendar/service/CalendarTileService.ktnew file mode 100644index 0000000000..44c10d5e2c--- /dev/null+++ b/calendar/src/main/java/com/vayunmathur/calendar/service/CalendarTileService.kt@@ -0,0 +1,33 @@+package com.vayunmathur.calendar.service++import android.content.Context+import com.vayunmathur.calendar.data.WearCalendarStore+import com.vayunmathur.library.weartiles.MaTileService+import com.vayunmathur.library.weartiles.TileSnapshot+import com.vayunmathur.library.wearlink.calendar.WearCalendarEvent+import java.time.Instant+import java.time.LocalDateTime+import java.time.ZoneId+import java.time.format.DateTimeFormatter++/** Wear Tile for Calendar: the next upcoming event from the wear snapshot. */+class CalendarTileService : MaTileService() {+    override val launchActivityName: String = "com.vayunmathur.calendar.ui.WearMainActivity"++    override fun loadSnapshot(context: Context): TileSnapshot {+        val events = WearCalendarStore.get(context).snapshot.value.events+        return calendarTileSnapshot(events, System.currentTimeMillis(), ZoneId.systemDefault())+    }++    override fun previewSnapshot(): TileSnapshot =+        TileSnapshot(appName = "Calendar", primary = "Team sync", secondary = "10:00 AM")+}++/** Pure next-event mapping; [nowMillis] is injected so tests stay deterministic. */+fun calendarTileSnapshot(events: List<WearCalendarEvent>, nowMillis: Long, zone: ZoneId): TileSnapshot {+    val next = events.filter { it.endMillis >= nowMillis }.minByOrNull { it.startMillis }+        ?: return TileSnapshot(appName = "Calendar", primary = "No events", secondary = "Nothing coming up")+    val start = LocalDateTime.ofInstant(Instant.ofEpochMilli(next.startMillis), zone)+    val time = if (next.allDay) "All day" else start.format(DateTimeFormatter.ofPattern("EEE h:mm a"))+    return TileSnapshot(appName = "Calendar", primary = next.title, secondary = time)+}
calendar/src/test/java/com/vayunmathur/calendar/service/CalendarTileServiceTest.kt
diff --git a/calendar/src/test/java/com/vayunmathur/calendar/service/CalendarTileServiceTest.kt b/calendar/src/test/java/com/vayunmathur/calendar/service/CalendarTileServiceTest.ktnew file mode 100644index 0000000000..d0cd094715--- /dev/null+++ b/calendar/src/test/java/com/vayunmathur/calendar/service/CalendarTileServiceTest.kt@@ -0,0 +1,30 @@+package com.vayunmathur.calendar.service++import com.vayunmathur.library.weartiles.TileLayout+import com.vayunmathur.library.wearlink.calendar.WearCalendarEvent+import java.time.ZoneId+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++class CalendarTileServiceTest {+    private val zone: ZoneId = ZoneId.of("UTC")+    private val now: Long = java.time.LocalDateTime.of(2026, 1, 5, 6, 0).atZone(zone).toInstant().toEpochMilli() // 2026-01-05T06:00:00Z++    @Test+    fun nextUpcomingEventRenders() {+        val events = listOf(+            WearCalendarEvent(eventId = 1, title = "Past", startMillis = now - 7_200_000L, endMillis = now - 3_600_000L),+            WearCalendarEvent(eventId = 2, title = "Team sync", startMillis = now + 3_600_000L, endMillis = now + 7_200_000L),+        )+        val tile = calendarTileSnapshot(events, now, zone)+        assertEquals("Team sync", tile.primary)+        assertTrue(tile.secondary.contains("7:00 AM"), tile.secondary)+        assertTrue(TileLayout.build(tile, "com.vayunmathur.calendar", "X").toString().contains("Team sync"))+    }++    @Test+    fun noUpcomingEventsIsHonest() {+        assertEquals("No events", calendarTileSnapshot(emptyList(), now, zone).primary)+    }+}