From 12f5e530e66bdb72fc46813591bfc84c75f76cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Thu, 31 Aug 2023 12:14:47 +0200 Subject: [PATCH] Reuse existing alarms on `ACTION_SET_ALARM` --- .../clock/activities/IntentHandlerActivity.kt | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/activities/IntentHandlerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/clock/activities/IntentHandlerActivity.kt index 29c08dd..e393182 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/activities/IntentHandlerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/activities/IntentHandlerActivity.kt @@ -72,8 +72,6 @@ class IntentHandlerActivity : SimpleActivity() { val skipUi = getBooleanExtra(AlarmClock.EXTRA_SKIP_UI, false) val defaultAlarmSound = getDefaultAlarmSound(RingtoneManager.TYPE_ALARM) - // TODO: Find existing alarm and reuse - var weekDays = 0 days?.forEach { weekDays += getBitForCalendarDay(it) @@ -98,6 +96,33 @@ class IntentHandlerActivity : SimpleActivity() { } } ?: defaultAlarmSound + // We don't want to accidentally edit existing alarm, so allow reuse only when skipping UI + if (hasExtra(AlarmClock.EXTRA_HOUR) && skipUi) { + var daysToCompare = weekDays + val timeInMinutes = hour * 60 + minute + if (weekDays <= 0) { + daysToCompare = if (timeInMinutes > getCurrentDayMinutes()) { + TODAY_BIT + } else { + TOMORROW_BIT + } + } + val existingAlarm = dbHelper.getAlarms().firstOrNull { + it.days == daysToCompare + && it.vibrate == vibrate + && it.soundTitle == soundToUse.title + && it.soundUri == soundToUse.uri + && it.label == (message ?: "") + && it.timeInMinutes == timeInMinutes + } + + if (existingAlarm != null && !existingAlarm.isEnabled) { + existingAlarm.isEnabled = true + startAlarm(existingAlarm) + finish() + } + } + val newAlarm = createNewAlarm(DEFAULT_ALARM_MINUTES, 0) newAlarm.isEnabled = true newAlarm.days = weekDays @@ -163,7 +188,9 @@ class IntentHandlerActivity : SimpleActivity() { val existingTimer = it.firstOrNull { it.state is TimerState.Idle } // We don't want to accidentally edit existing timer, so allow reuse only when skipping UI - if (existingTimer != null && skipUi) { + if (existingTimer != null + && skipUi + && (existingTimer.state is TimerState.Idle || (existingTimer.state is TimerState.Finished && !existingTimer.oneShot))) { startTimer(existingTimer) } else { createAndStartNewTimer()