From d776b40c43ad7ac44767fd46f2a2609792f96acd Mon Sep 17 00:00:00 2001 From: MiMoHo <37556964+MiMoHo@users.noreply.github.com> Date: Fri, 3 Jul 2026 20:56:45 +0200 Subject: [PATCH] fix: allow stopping an alarm deleted while ringing AlarmService.onStartCommand looked up the alarm in the DB up front and bailed out via stopSelfIfIdle() whenever it was missing. When an alarm is deleted while ringing, that lookup returns null, so ACTION_STOP_ALARM never reached stopActiveAlarm() and the alarm kept sounding with no way to dismiss or snooze it. Only the START path needs the alarm object; the STOP path only compares the id against the active alarm. Move the DB lookup (and null guard) into the ACTION_START_ALARM branch so a deleted alarm can still be stopped by id. Fixes #406. --- CHANGELOG.md | 3 +++ .../org/fossify/clock/services/AlarmService.kt | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fbc4a861..09caa8c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Fixed alarms not stopping when dismissed or snoozed after being deleted while ringing ([#406]) ## [1.6.0] - 2026-01-30 ### Added @@ -111,6 +113,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#247]: https://github.com/FossifyOrg/Clock/issues/247 [#335]: https://github.com/FossifyOrg/Clock/issues/335 [#346]: https://github.com/FossifyOrg/Clock/issues/346 +[#406]: https://github.com/FossifyOrg/Clock/issues/406 [Unreleased]: https://github.com/FossifyOrg/Clock/compare/1.6.0...HEAD [1.6.0]: https://github.com/FossifyOrg/Clock/compare/1.5.0...1.6.0 diff --git a/app/src/main/kotlin/org/fossify/clock/services/AlarmService.kt b/app/src/main/kotlin/org/fossify/clock/services/AlarmService.kt index 3d7b627b..dcfe3d2b 100644 --- a/app/src/main/kotlin/org/fossify/clock/services/AlarmService.kt +++ b/app/src/main/kotlin/org/fossify/clock/services/AlarmService.kt @@ -60,14 +60,25 @@ class AlarmService : Service() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { val action = intent?.action ?: ACTION_START_ALARM val alarmId = intent?.getIntExtra(ALARM_ID, -1) ?: -1 - val newAlarm = applicationContext.dbHelper.getAlarmWithId(alarmId) - if (alarmId == -1 || newAlarm == null) { + if (alarmId == -1) { stopSelfIfIdle() return START_NOT_STICKY } when (action) { - ACTION_START_ALARM -> startNewAlarm(newAlarm) + ACTION_START_ALARM -> { + // Starting requires the alarm to still exist in the DB. + val newAlarm = applicationContext.dbHelper.getAlarmWithId(alarmId) + if (newAlarm == null) { + stopSelfIfIdle() + return START_NOT_STICKY + } + + startNewAlarm(newAlarm) + } + + // Stopping only needs the id, so an alarm deleted while ringing + // can still be dismissed/snoozed. See issue #406. ACTION_STOP_ALARM -> stopActiveAlarm(alarmId) else -> throw IllegalArgumentException("Unknown action: $action") }