Modern Apps git

← Modern-Apps.git

calendar: deleting a calendar cancels its reminders and drops its cached events

Commit
4460173146793dc226342b3d4aa3f9580b880447
Author
Vayun Mathur <[email protected]>
When
2026-12-01 06:49 UTC (5 days ago)
Parents
c6d3d41
deleteCalendar only refreshed calendars and widgets. The
provider cascade deleted the calendar's events and
reminders, but every sibling mutation (upsertEvent,
deleteEventSeries, importIcsEvents, setDefaultReminders)
also reloads _events and runs ReminderScheduler.
reconcileAll โ€” the cancel-all-then-reschedule pass โ€”
and deleteCalendar skipped both. The tracked exact alarm and
the locked-boot mirror entry survived, and at trigger time
ReminderReceiver posted the notification unconditionally from
the alarm extras (stale title), resolving the (now-gone)
instance only for the deep link; a locked-boot pass could
likewise re-arm it from the stale mirror. The deleted
calendar's events also lingered in the cached list.

deleteCalendar now reloads _events and reconciles alarms
after the provider delete, exactly as deleteEventSeries
does, before refreshing calendars and widgets. New
regression test arms alarms for events in two calendars,
simulates the cascade, deletes one calendar and asserts the
events drop, one alarm remains, and the mirror holds only
the surviving event (failed pre-fix). Scoped edit/split, ICS
round-trips, wear snapshots and DST positioning were audited
and cleared. Verified: :calendar:testDevUnitTest โ€” the one
failure is pre-existing HolidayCalendarsScreen WIP, proven
identical without this change; :calendar:lint passes.

Diff

calendar/src/main/java/com/vayunmathur/calendar/platform/CalendarViewModel.kt
diff --git a/calendar/src/main/java/com/vayunmathur/calendar/platform/CalendarViewModel.kt b/calendar/src/main/java/com/vayunmathur/calendar/platform/CalendarViewModel.ktindex fd7bf8412c..36681a678e 100644--- a/calendar/src/main/java/com/vayunmathur/calendar/platform/CalendarViewModel.kt+++ b/calendar/src/main/java/com/vayunmathur/calendar/platform/CalendarViewModel.kt@@ -556,6 +556,13 @@ class CalendarViewModel(application: Application) :             } catch (expected: Exception) {                 Log.error("CalendarViewModel", "Error deleting calendar", expected)             }+            // Deleting the calendar cascades to its events in the provider, so the+            // cached event list and the reminder alarms must be rebuilt like every+            // other event mutation: without this, an alarm armed for an event of the+            // deleted calendar stays tracked (and mirrored for locked boot) and+            // ReminderReceiver posts it even though the event no longer exists.+            _events.value = Event.getAllEvents(app)+            ReminderScheduler.reconcileAll(app, _events.value)             refreshCalendarsAndWidgets()         }     }
calendar/src/test/java/com/vayunmathur/calendar/util/DeleteCalendarReminderTest.kt
diff --git a/calendar/src/test/java/com/vayunmathur/calendar/util/DeleteCalendarReminderTest.kt b/calendar/src/test/java/com/vayunmathur/calendar/util/DeleteCalendarReminderTest.ktnew file mode 100644index 0000000000..9cc7410b0c--- /dev/null+++ b/calendar/src/test/java/com/vayunmathur/calendar/util/DeleteCalendarReminderTest.kt@@ -0,0 +1,107 @@+package com.vayunmathur.calendar.util++// FIXTURE RELATIVE-TIME EXCEPTION: the events are seeded relative to the wall clock the+// scheduler reads internally, so their reminders are in the future and actually arm; a fixed+// past instant would silently change the branch under test.++import android.app.AlarmManager+import android.app.Application+import android.content.Context+import android.os.Looper+import android.provider.CalendarContract+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.calendar.CalendarTestData as D+import com.vayunmathur.calendar.FakeCalendarData+import com.vayunmathur.calendar.data.ReminderMirror+import com.vayunmathur.calendar.platform.CalendarViewModel+import com.vayunmathur.calendar.platform.ReminderScheduler+import kotlinx.coroutines.runBlocking+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.assertEquals+import kotlin.test.assertTrue++@RunWith(RobolectricTestRunner::class)+class DeleteCalendarReminderTest {++    private val context: Context get() = ApplicationProvider.getApplicationContext()+    private val app: Application get() = ApplicationProvider.getApplicationContext()++    private fun reminderRow(eventId: Long, minutes: Int): Map<String, Any?> = mapOf(+        CalendarContract.Reminders.EVENT_ID to eventId,+        CalendarContract.Reminders.MINUTES to minutes,+    )++    @Before+    fun setUp() {+        FakeCalendarData.reset()+        FakeCalendarData.register()+        val now = System.currentTimeMillis()+        val doomed = D.event(+            id = 42, calendar = D.PERSONAL, title = "Doomed standup", reminders = listOf(10),+        ).copy(start = now + 2 * 3_600_000L, end = now + 2 * 3_600_000L + 1_800_000L)+        val keeper = D.event(+            id = 43, calendar = D.WORK, title = "Keeper review", reminders = listOf(10),+        ).copy(start = now + 3 * 3_600_000L, end = now + 3 * 3_600_000L + 1_800_000L)+        FakeCalendarData.calendars = listOf(+            FakeCalendarData.calendarRow(D.PERSONAL),+            FakeCalendarData.calendarRow(D.WORK),+        )+        FakeCalendarData.events = listOf(+            FakeCalendarData.eventRow(doomed),+            FakeCalendarData.eventRow(keeper),+        )+        FakeCalendarData.reminders = listOf(reminderRow(42, 10), reminderRow(43, 10))+        FakeCalendarData.instances = emptyList()+    }++    private fun loadedVm(): CalendarViewModel {+        val vm = CalendarViewModel(app)+        var waited = 0+        while (vm.events.value.size != 2 && waited < 200) {+            shadowOf(Looper.getMainLooper()).idle(); Thread.sleep(10); waited+++        }+        assertEquals(2, vm.events.value.size)+        return vm+    }++    @Test+    fun `deleting a calendar cancels its events reminder alarms and mirror entries`() = runBlocking {+        val vm = loadedVm()+        // Arm both events' reminders, as a boot/unlock reconcile would.+        ReminderScheduler.reconcileAll(context, vm.events.value)+        assertEquals(+            setOf(42L, 43L),+            ReminderMirror.read(context).map { it.eventId }.toSet(),+        )+        val alarmManager = context.getSystemService(AlarmManager::class.java)+        assertEquals(2, shadowOf(alarmManager).scheduledAlarms.size)++        // The provider cascades a calendar delete to its events and reminders.+        FakeCalendarData.calendars = listOf(FakeCalendarData.calendarRow(D.WORK))+        FakeCalendarData.events = FakeCalendarData.events.filter {+            it[CalendarContract.Events.CALENDAR_ID] != 1L+        }+        FakeCalendarData.reminders = listOf(reminderRow(43, 10))++        vm.deleteCalendar(1)+        var waited = 0+        while (+            (vm.events.value.any { it.calendarID == 1L } ||+                vm.calendars.value.any { it.id == 1L }) && waited < 200+        ) {+            shadowOf(Looper.getMainLooper()).idle(); Thread.sleep(10); waited+++        }++        // The deleted calendar's event is gone from the cache, its alarm is+        // cancelled, and only the surviving calendar's reminder stays armed+        // (in AlarmManager and in the locked-boot mirror).+        assertTrue(vm.events.value.none { it.calendarID == 1L }, "events: ${vm.events.value}")+        assertEquals(listOf(43L), vm.events.value.map { it.id })+        assertEquals(1, shadowOf(alarmManager).scheduledAlarms.size)+        assertEquals(listOf(43L), ReminderMirror.read(context).map { it.eventId })+    }+}