clock: editing an alarm now disarms its pending snooze
- Commit
996836c49d0369340e6640a926d949f68f87d8d7- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 19:54 UTC (5 days ago)
- Parents
4694277
Snooze an alarm to 07:05, then edit it before it fires: the UI says the edit 'drops any pending snooze', but it only cleared the database field and re-armed the ring intent — the armed snooze PendingIntent still fired at 07:05 with the pre-edit label and ringtone. Rebooting first changed the outcome (BootReceiver arms snoozes from the now-null field), proving the AlarmManager entry was the stray state. The invariant now lives in AlarmScheduler, owner of both intents: scheduling an alarm with snoozedUntil == null cancels any armed snooze for it first (callers that keep a snooze pass it set), and edit paths that never reschedule — a disabled alarm being edited, the ringtone picker — cancel explicitly via cancelPendingSnooze. Three Robolectric regression tests (edited alarm's snooze cancelled while the ring stays; retained snooze survives; explicit cancel disarms), verified failing against the unguarded scheduler. DST stepping, exact-alarm fallbacks, boot/timezone re-arm, timers and world clock were audited and cleared. Verified: :clock:testDevUnitTest 265 tests pass; lint errors are pre-existing on untouched files.
Diff
clock/src/main/java/com/vayunmathur/clock/platform/AlarmScheduler.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/platform/AlarmScheduler.kt b/clock/src/main/java/com/vayunmathur/clock/platform/AlarmScheduler.ktindex c00ddbfc96..3b1591c9a3 100644--- a/clock/src/main/java/com/vayunmathur/clock/platform/AlarmScheduler.kt+++ b/clock/src/main/java/com/vayunmathur/clock/platform/AlarmScheduler.kt@@ -62,6 +62,20 @@ object AlarmScheduler { // Convert LocalTime to actual milliseconds for today or tomorrow val triggerTime = calculateNextTriggerMillis(alarm) + // A snooze PendingIntent armed earlier carries the ring settings from+ // when it was created and is independent of this ring schedule (it+ // lives on its own request code, and cancel() is the only other place+ // that drops it). Callers that edit an alarm clear snoozedUntil on the+ // copy they pass in precisely so the pending snooze dies with the old+ // schedule, so enforce that here: an alarm that says it has no snooze+ // pending must not have one armed in AlarmManager either. Callers+ // that keep snoozedUntil set (BootReceiver's restore, which re-arms+ // the snooze right after this, and AlarmReceiver's repeat re-arm)+ // retain their pending snooze untouched.+ if (alarm.snoozedUntil == null) {+ cancelSnooze(context, alarmManager, alarm.id)+ }+ // Use setAlarmClock so the user sees the alarm icon in their status bar setAlarmClockOrFallback(alarmManager, triggerTime, pendingIntent, alarm.id) }@@ -97,6 +111,18 @@ object AlarmScheduler { cancelSnooze(context, alarmManager, alarm.id) } + /**+ * Drops only the armed snooze PendingIntent for [alarmId], leaving the+ * ring schedule untouched. For edit paths that clear `snoozedUntil` in+ * the database but do not (re)schedule the ring — a disabled alarm being+ * edited has no ring to re-arm, so [schedule]'s matching guard cannot+ * cover it.+ */+ fun cancelPendingSnooze(context: Context, alarmId: Long) {+ val alarmManager = context.getSystemService(AlarmManager::class.java)+ cancelSnooze(context, alarmManager, alarmId)+ }+ /** * Arms the snoozed ring as its own AlarmManager entry, on a distinct request * code so it cannot clobber the alarm's recurring schedule, and persists it
clock/src/main/java/com/vayunmathur/clock/ui/AlarmPage.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/ui/AlarmPage.kt b/clock/src/main/java/com/vayunmathur/clock/ui/AlarmPage.ktindex cdb81cd5d2..c22a2bfe04 100644--- a/clock/src/main/java/com/vayunmathur/clock/ui/AlarmPage.kt+++ b/clock/src/main/java/com/vayunmathur/clock/ui/AlarmPage.kt@@ -40,8 +40,15 @@ fun AlarmPage(backStack: NavBackStack<Route>, clockViewModel: ClockViewModel) { val picked = ringtonePickerResult(result.data) clockViewModel.alarms.value.firstOrNull { it.id == pickingAlarmId }?.let { target -> // Same reschedule requirement as setGradualVolumeSeconds below:- // the fire intent carries the ringtone URI.- val updated = target.copy(ringtoneUri = picked)+ // the fire intent carries the ringtone URI. Clearing+ // snoozedUntil follows save()'s rule for edited alarms (the+ // pending snooze carries the old ringtone); cancel the armed+ // snooze as save() does, since a disabled alarm is not+ // rescheduled below and so never reaches schedule()'s guard.+ if (target.snoozedUntil != null) {+ alarmScheduler.cancelPendingSnooze(context, target.id)+ }+ val updated = target.copy(ringtoneUri = picked, snoozedUntil = null) if (updated.enabled) alarmScheduler.schedule(context, updated) clockViewModel.upsert(updated) }@@ -53,8 +60,17 @@ fun AlarmPage(backStack: NavBackStack<Route>, clockViewModel: ClockViewModel) { object : AlarmActions { private fun save(alarm: Alarm) { // Snooze state belongs to the old schedule: re-arming the alarm- // (new time, new days) drops any pending snooze with it.- val cleared = if (alarm.snoozedUntil != null) alarm.copy(snoozedUntil = null) else alarm+ // (new time, new days) drops any pending snooze with it. The+ // armed snooze lives in AlarmManager on its own request code,+ // so clearing the field alone leaves it armed — cancel it too+ // (schedule() also guards this, but a disabled alarm is not+ // rescheduled at all).+ val cleared = if (alarm.snoozedUntil != null) {+ alarmScheduler.cancelPendingSnooze(context, alarm.id)+ alarm.copy(snoozedUntil = null)+ } else {+ alarm+ } if (cleared.enabled) alarmScheduler.schedule(context, cleared) clockViewModel.upsert(cleared) }
clock/src/test/java/com/vayunmathur/clock/platform/AlarmSchedulerSnoozeEditTest.kt
diff --git a/clock/src/test/java/com/vayunmathur/clock/platform/AlarmSchedulerSnoozeEditTest.kt b/clock/src/test/java/com/vayunmathur/clock/platform/AlarmSchedulerSnoozeEditTest.ktnew file mode 100644index 0000000000..9fbea3dfd0--- /dev/null+++ b/clock/src/test/java/com/vayunmathur/clock/platform/AlarmSchedulerSnoozeEditTest.kt@@ -0,0 +1,112 @@+package com.vayunmathur.clock.platform++import android.app.AlarmManager+import android.content.Context+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.clock.FakeAndroidKeyStore+import com.vayunmathur.clock.data.Alarm+import com.vayunmathur.clock.data.ClockRepository+import kotlinx.coroutines.runBlocking+import kotlinx.datetime.LocalTime+import org.junit.Before+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.Shadows.shadowOf+import kotlin.test.Test+import kotlin.test.assertFalse+import kotlin.test.assertTrue+import kotlin.time.Instant++/**+ * Regression tests for the stale-snooze bug: editing an alarm cleared+ * `snoozedUntil` in the database and re-armed the ring, but the snooze+ * PendingIntent armed in AlarmManager (on its own request code) was never+ * cancelled, so the snoozed ring still fired at the old time with the ring+ * settings captured when the user snoozed. A reboot masked it only because+ * BootReceiver re-arms snoozes from the (now cleared) database field.+ */+@RunWith(RobolectricTestRunner::class)+class AlarmSchedulerSnoozeEditTest {++ @Before fun setUp() = FakeAndroidKeyStore.install()++ private val ctx: Context get() = ApplicationProvider.getApplicationContext()+ private val alarmManager get() = ctx.getSystemService(AlarmManager::class.java)++ private fun alarm(snoozedUntil: Instant?) = Alarm(+ time = LocalTime(23, 59),+ name = "wake",+ enabled = true,+ days = 0,+ snoozedUntil = snoozedUntil,+ id = 42,+ )++ private val snoozeTarget = Instant.parse("2026-11-16T12:00:00Z")++ /**+ * Arms the snooze PendingIntent the way the ringing screen does. The+ * persist step after the arm dies on SQLCipher on the host JVM (the+ * documented ceiling in SchedulerAndReceiverDeepTest); the arm itself+ * has already landed in AlarmManager by then.+ */+ private fun armSnooze() {+ runCatching {+ runBlocking {+ AlarmScheduler.scheduleSnoozeAt(+ ctx,+ ClockRepository.get(ctx),+ alarm(snoozeTarget),+ snoozeTarget.toEpochMilliseconds(),+ )+ }+ }+ assertTrue(+ shadowOf(alarmManager).scheduledAlarms.any {+ it.triggerAtMs == snoozeTarget.toEpochMilliseconds()+ },+ "precondition: snooze should be armed in AlarmManager",+ )+ }++ private fun snoozeArmed(): Boolean =+ shadowOf(alarmManager).scheduledAlarms.any {+ it.triggerAtMs == snoozeTarget.toEpochMilliseconds()+ }++ @Test+ fun reschedulingEditedAlarmWithClearedSnoozeCancelsArmedSnooze() {+ armSnooze()++ // AlarmPage.save(): the edited copy carries snoozedUntil = null and+ // is rescheduled. The armed snooze must not survive it.+ AlarmScheduler.schedule(ctx, alarm(snoozedUntil = null))++ assertFalse(snoozeArmed(), "edited alarm's pending snooze must be cancelled")+ // The ring itself is still armed.+ assertTrue(shadowOf(alarmManager).scheduledAlarms.isNotEmpty())+ }++ @Test+ fun reschedulingAlarmWithPendingSnoozeKeepsArmedSnooze() {+ armSnooze()++ // BootReceiver / AlarmReceiver path: the alarm still records its+ // snooze, so re-arming the ring must not drop it (BootReceiver+ // re-arms the snooze from snoozedUntil right after scheduling).+ AlarmScheduler.schedule(ctx, alarm(snoozedUntil = snoozeTarget))++ assertTrue(snoozeArmed(), "pending snooze must survive a ring reschedule that keeps it")+ }++ @Test+ fun cancelPendingSnoozeDropsArmedSnoozeForDisabledAlarmEdit() {+ armSnooze()++ // AlarmPage.save() on a disabled alarm never calls schedule(), so+ // it cancels the armed snooze explicitly.+ AlarmScheduler.cancelPendingSnooze(ctx, 42)++ assertFalse(snoozeArmed(), "explicit snooze cancel must disarm the snooze")+ }+}