diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt index a168eaf..7a3a35b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt @@ -73,6 +73,7 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface { private fun openEditAlarm(alarm: Alarm) { EditAlarmDialog(activity as SimpleActivity, alarm) { setupAlarms() + checkAlarmState(alarm) } } @@ -80,14 +81,20 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface { if (context!!.dbHelper.updateAlarmEnabledState(id, isEnabled)) { val alarm = alarms.firstOrNull { it.id == id } ?: return alarm.isEnabled = isEnabled - if (isEnabled) { - getClosestTriggerTimestamp(alarm) - } + checkAlarmState(alarm) } else { activity!!.toast(R.string.unknown_error_occurred) } } + private fun checkAlarmState(alarm: Alarm) { + if (alarm.isEnabled) { + getClosestTriggerTimestamp(alarm) + } else { + cancelAlarmClock(alarm) + } + } + private fun getClosestTriggerTimestamp(alarm: Alarm) { val calendar = Calendar.getInstance() calendar.firstDayOfWeek = Calendar.MONDAY @@ -147,4 +154,9 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface { intent.putExtra(ALARM_ID, alarm.id) return PendingIntent.getBroadcast(context, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT) } + + private fun cancelAlarmClock(alarm: Alarm) { + val alarmManager = context!!.getSystemService(Context.ALARM_SERVICE) as AlarmManager + alarmManager.cancel(getPendingIntent(alarm)) + } }