clock-wear: alarms re-arm at the right local time after a zone change
- Commit
86eda7424c98c01786fc46b1b3dff7af719695eb- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 07:58 UTC (5 days ago)
- Parents
2956872
The wear BootReceiver handled only BOOT_COMPLETED and the manifest registered nothing else, while alarms arm as absolute epoch instants computed in the current zone. Move the watch from Denver to New York and a 07:00 alarm armed as 14:00Z rings at 09:00 EST — two hours late, indefinitely, until a snapshot re-arrives or the watch reboots. The phone receiver already re-arms on TIMEZONE_CHANGED/TIME_SET with exactly this rationale; the wear side it mirrors never subscribed. The receiver now reconciles the persisted snapshot's alarms in the now-current zone on those broadcasts (and the manifest registers them), persisting the new arming proof. Timers are deliberately untouched: they are durations on the monotonic base (or an epoch end a zone change does not move), so the boot timer-restore must not run on a wall-clock jump. Two Robolectric tests fire each broadcast against a seeded America/New_York alarm and assert the re-armed instant (failed pre-fix); clock-wear gains the Robolectric test deps for them. AGENTS.md documents the arming triggers. Phone alarm DST, timer bases, stopwatch and world clock were audited and cleared. Verified: :clock:wear:testDebugUnitTest 19 tests pass; :clock:wear:lint 0 errors.
Diff
clock-wear/AGENTS.md
diff --git a/clock-wear/AGENTS.md b/clock-wear/AGENTS.mdindex 94d7b87634..bde32649ee 100644--- a/clock-wear/AGENTS.md+++ b/clock-wear/AGENTS.md@@ -29,8 +29,10 @@ depend on another app module). Change both copies together; each side's - **Synced from the phone (snapshot):** world clocks (display only by nature) and the alarm list. A synced alarm rings on the watch only because `platform/WatchAlarmScheduler` arms it with the watch's own- AlarmManager on snapshot arrival / boot. The UI calls it armed only- from persisted `ArmedAlarmRecord` proof (`domain/WearAlarmState.kt`);+ AlarmManager on snapshot arrival / boot / timezone or wall-clock change+ (the armed instant is recomputed in the zone then in force). The UI+ calls it armed only from persisted `ArmedAlarmRecord` proof+ (`domain/WearAlarmState.kt`); enabled-but-unarmed alarms are labelled display-only with the reason. - **Watch-native (never synced):** timers (`platform/WatchTimerScheduler` + `WatchTimerReceiver`, presets in `domain/WatchTimerState.kt`) and the
clock-wear/build.gradle.kts
diff --git a/clock-wear/build.gradle.kts b/clock-wear/build.gradle.ktsindex 6fb770e2f6..d2def3fbb9 100644--- a/clock-wear/build.gradle.kts+++ b/clock-wear/build.gradle.kts@@ -23,6 +23,9 @@ android { // convention) requires 31, so the watch floor is 31. minSdk = 31 }+ testOptions {+ unitTests.isIncludeAndroidResources = true+ } } dependencies {@@ -32,4 +35,6 @@ dependencies { implementation(project(":library:wearlink")) testImplementation(libs.kotlin.test) testImplementation(libs.kotlin.test.junit)+ testImplementation(libs.robolectric)+ testImplementation(libs.androidx.test.core) }
clock-wear/src/main/AndroidManifest.xml
diff --git a/clock-wear/src/main/AndroidManifest.xml b/clock-wear/src/main/AndroidManifest.xmlindex ae66c0bc7a..01fb0e93a0 100644--- a/clock-wear/src/main/AndroidManifest.xml+++ b/clock-wear/src/main/AndroidManifest.xml@@ -66,6 +66,8 @@ android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" />+ <action android:name="android.intent.action.TIMEZONE_CHANGED" />+ <action android:name="android.intent.action.TIME_SET" /> </intent-filter> </receiver>
clock-wear/src/main/java/com/vayunmathur/clock/wear/platform/BootReceiver.kt
diff --git a/clock-wear/src/main/java/com/vayunmathur/clock/wear/platform/BootReceiver.kt b/clock-wear/src/main/java/com/vayunmathur/clock/wear/platform/BootReceiver.ktindex 47db23d42e..41251ff6aa 100644--- a/clock-wear/src/main/java/com/vayunmathur/clock/wear/platform/BootReceiver.kt+++ b/clock-wear/src/main/java/com/vayunmathur/clock/wear/platform/BootReceiver.kt@@ -12,9 +12,24 @@ import com.vayunmathur.clock.wear.domain.watchTimerRemainingMillis * from the persisted snapshot and the watch-native timers from their * persisted anchors. A timer that expired while the watch was off is * marked finished and rung immediately rather than silently lost.+ *+ * A timezone or wall-clock change leaves the schedules in place but makes+ * them mean a different local time: each synced alarm is armed as an+ * absolute epoch instant computed in the zone in force when it was armed,+ * so a 07:00 alarm armed in one zone would otherwise ring at 09:00 local+ * after moving two zones east. Re-arm the alarms (only) from the persisted+ * snapshot in the new zone, exactly as the phone's receiver does. Timers+ * are durations armed on the monotonic base (or, after a reboot, for an+ * epoch end the zone does not move), so they are left untouched. */ class BootReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) {+ if (intent.action == Intent.ACTION_TIMEZONE_CHANGED ||+ intent.action == Intent.ACTION_TIME_CHANGED+ ) {+ rearmAlarms(context)+ return+ } if (intent.action != Intent.ACTION_BOOT_COMPLETED) return val store = WearClockStore.get(context) val persisted = store.state.value@@ -38,4 +53,17 @@ class BootReceiver : BroadcastReceiver() { } } }++ /** Recompute every synced alarm's trigger in the now-current zone and re-arm it. */+ private fun rearmAlarms(context: Context) {+ val store = WearClockStore.get(context)+ val persisted = store.state.value+ val (armed, failures) = WatchAlarmScheduler.reconcile(+ context,+ persisted.snapshot,+ persisted.armedRecords.map { it.alarmId },+ System.currentTimeMillis(),+ )+ store.setArming(armed, failures)+ } }
clock-wear/src/test/java/com/vayunmathur/clock/wear/platform/BootReceiverTimezoneTest.kt
diff --git a/clock-wear/src/test/java/com/vayunmathur/clock/wear/platform/BootReceiverTimezoneTest.kt b/clock-wear/src/test/java/com/vayunmathur/clock/wear/platform/BootReceiverTimezoneTest.ktnew file mode 100644index 0000000000..66f451d81f--- /dev/null+++ b/clock-wear/src/test/java/com/vayunmathur/clock/wear/platform/BootReceiverTimezoneTest.kt@@ -0,0 +1,104 @@+package com.vayunmathur.clock.wear.platform++import android.app.AlarmManager+import android.content.Context+import android.content.Intent+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.clock.wear.data.WearClockStore+import com.vayunmathur.clock.wear.domain.WearClockAlarm+import com.vayunmathur.clock.wear.domain.WearClockSnapshot+import com.vayunmathur.clock.wear.domain.nextWearAlarmTriggerMillis+import kotlinx.datetime.TimeZone+import kotlinx.datetime.toLocalDateTime+import org.junit.After+import org.junit.Before+import org.junit.Test+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.Shadows.shadowOf+import org.robolectric.annotation.Config+import kotlin.test.assertEquals+import kotlin.test.assertNotNull+import kotlin.time.Instant++/**+ * Regression tests for the wear timezone bug: a synced alarm is armed as an+ * absolute epoch instant computed in the watch's then-current zone, and+ * nothing re-armed it when that zone (or the wall clock) changed, so a+ * 07:00 alarm armed in America/Denver (14:00Z) rang at 09:00 local after+ * the watch moved to America/New_York instead of 07:00 (12:00Z). The+ * phone's BootReceiver re-arms on these broadcasts; the watch's only+ * listened for boot.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class BootReceiverTimezoneTest {++ private val ctx: Context get() = ApplicationProvider.getApplicationContext()+ private var previousDefaultZone: java.util.TimeZone? = null++ @Before+ fun setUp() {+ previousDefaultZone = java.util.TimeZone.getDefault()+ }++ @After+ fun tearDown() {+ previousDefaultZone?.let { java.util.TimeZone.setDefault(it) }+ }++ /**+ * Seeds the store with one enabled alarm two hours from now in New York,+ * moves the watch's default zone there, and returns the trigger a+ * correct re-arm must arm (07:00-style wall time in the new zone).+ */+ private fun seedAlarmInNewYork(alarmId: Long): Long {+ val newYork = TimeZone.of("America/New_York")+ java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("America/New_York"))+ val nowMillis = System.currentTimeMillis()+ val target = Instant.fromEpochMilliseconds(nowMillis + 2 * 3_600_000L)+ .toLocalDateTime(newYork)+ val alarm = WearClockAlarm(+ alarmId = alarmId,+ label = "Wake",+ hour = target.hour,+ minute = target.minute,+ daysMask = 0,+ enabled = true,+ )+ val store = WearClockStore.get(ctx)+ store.setSnapshot(+ WearClockSnapshot(generatedAtMillis = 1_000L, alarms = listOf(alarm)),+ )+ store.setArming(emptyList(), emptyList())+ return nextWearAlarmTriggerMillis(+ target.hour, target.minute, 0, nowMillis, newYork,+ )+ }++ private fun scheduledTriggerMillis(): Long? {+ val shadow = shadowOf(ctx.getSystemService(AlarmManager::class.java))+ return shadow.scheduledAlarms.firstOrNull()?.triggerAtMs+ }++ @Test+ fun timezoneChangeRearmsSyncedAlarmInNewZone() {+ val expected = seedAlarmInNewYork(alarmId = 7_301L)+ BootReceiver().onReceive(ctx, Intent(Intent.ACTION_TIMEZONE_CHANGED))+ val trigger = scheduledTriggerMillis()+ assertNotNull(trigger, "timezone change should re-arm the synced alarm")+ assertEquals(expected, trigger)+ // The arming proof the UI renders from must carry the new trigger.+ val armed = WearClockStore.get(ctx).state.value.armedRecords+ assertEquals(listOf(expected), armed.map { it.nextTriggerMillis })+ }++ @Test+ fun timeSetRearmsSyncedAlarmInNewZone() {+ val expected = seedAlarmInNewYork(alarmId = 7_302L)+ BootReceiver().onReceive(ctx, Intent(Intent.ACTION_TIME_CHANGED))+ val trigger = scheduledTriggerMillis()+ assertNotNull(trigger, "wall-clock change should re-arm the synced alarm")+ assertEquals(expected, trigger)+ }+}