From 8ac7246cecd81df06a009b107bfbb5a7efb35bfa Mon Sep 17 00:00:00 2001 From: Joshix-1 <57299889+Joshix-1@users.noreply.github.com> Date: Mon, 28 Oct 2024 11:01:32 +0100 Subject: [PATCH 01/13] fix scheduleNextAlarm --- .../org/fossify/clock/extensions/Context.kt | 40 ++++++++----------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/clock/extensions/Context.kt b/app/src/main/kotlin/org/fossify/clock/extensions/Context.kt index 95057827..d2e2e7d7 100644 --- a/app/src/main/kotlin/org/fossify/clock/extensions/Context.kt +++ b/app/src/main/kotlin/org/fossify/clock/extensions/Context.kt @@ -102,39 +102,33 @@ fun Context.createNewTimer(): Timer { fun Context.scheduleNextAlarm(alarm: Alarm, showToast: Boolean) { val calendar = Calendar.getInstance() calendar.firstDayOfWeek = Calendar.MONDAY - val currentTimeInMinutes = getCurrentDayMinutes() - if (alarm.days == TODAY_BIT) { - val triggerInMinutes = alarm.timeInMinutes - currentTimeInMinutes - setupAlarmClock(alarm, triggerInMinutes * 60 - calendar.get(Calendar.SECOND)) + val nextAlarmDay = Calendar.getInstance() - if (showToast) { - showRemainingTimeMessage(triggerInMinutes) - } + if (alarm.days == TODAY_BIT) { + } else if (alarm.days == TOMORROW_BIT) { - val triggerInMinutes = alarm.timeInMinutes - currentTimeInMinutes + DAY_MINUTES - setupAlarmClock(alarm, triggerInMinutes * 60 - calendar.get(Calendar.SECOND)) - - if (showToast) { - showRemainingTimeMessage(triggerInMinutes) - } + nextAlarmDay.add(Calendar.DAY_OF_MONTH, 1) } else { - for (i in 0..7) { - val currentDay = (calendar.get(Calendar.DAY_OF_WEEK) + 5) % 7 + for (i in 0..8) { + val currentDay = (nextAlarmDay.get(Calendar.DAY_OF_WEEK) + 5) % 7 val isCorrectDay = alarm.days and 2.0.pow(currentDay).toInt() != 0 - if (isCorrectDay && (alarm.timeInMinutes > currentTimeInMinutes || i > 0)) { - val triggerInMinutes = alarm.timeInMinutes - currentTimeInMinutes + (i * DAY_MINUTES) - setupAlarmClock(alarm, triggerInMinutes * 60 - calendar.get(Calendar.SECOND)) - - if (showToast) { - showRemainingTimeMessage(triggerInMinutes) - } + if (isCorrectDay && (i > 0 || alarm.timeInMinutes > getCurrentDayMinutes())) { break } else { - calendar.add(Calendar.DAY_OF_MONTH, 1) + nextAlarmDay.add(Calendar.DAY_OF_MONTH, 1) } } } + nextAlarmDay.set(Calendar.HOUR, alarm.timeInMinutes / 60) + nextAlarmDay.set(Calendar.MINUTE, alarm.timeInMinutes % 60) + nextAlarmDay.set(Calendar.SECOND, 0) + val triggerInSeconds = (nextAlarmDay.getTimeInMillis() - calendar.getTimeInMillis()) / 1000; + setupAlarmClock(alarm, triggerInSeconds) + + if (showToast) { + showRemainingTimeMessage(triggerInSeconds / 60) + } } fun Context.showRemainingTimeMessage(totalMinutes: Int) { From 9c241fd51a304c686097b4fbeb44c87404b44936 Mon Sep 17 00:00:00 2001 From: Joshix Date: Mon, 28 Oct 2024 12:00:00 +0000 Subject: [PATCH 02/13] improve sheduling of alarms --- .../clock/activities/ReminderActivity.kt | 11 +++- .../activities/SnoozeReminderActivity.kt | 3 +- .../org/fossify/clock/extensions/Context.kt | 59 +++++-------------- .../org/fossify/clock/helpers/Constants.kt | 43 ++++++++------ .../fossify/clock/services/SnoozeService.kt | 3 +- 5 files changed, 52 insertions(+), 67 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/clock/activities/ReminderActivity.kt b/app/src/main/kotlin/org/fossify/clock/activities/ReminderActivity.kt index b1f01ee7..3c0b6a41 100644 --- a/app/src/main/kotlin/org/fossify/clock/activities/ReminderActivity.kt +++ b/app/src/main/kotlin/org/fossify/clock/activities/ReminderActivity.kt @@ -23,6 +23,7 @@ import org.fossify.commons.helpers.MINUTE_SECONDS import org.fossify.commons.helpers.SILENT import org.fossify.commons.helpers.isOreoMr1Plus import org.fossify.commons.helpers.isOreoPlus +import java.util.Calendar class ReminderActivity : SimpleActivity() { companion object { @@ -266,18 +267,22 @@ class ReminderActivity : SimpleActivity() { private fun snoozeAlarm(overrideSnoozeDuration: Int? = null) { destroyEffects() + val now = Calendar.getInstance() if (overrideSnoozeDuration != null) { - setupAlarmClock(alarm!!, overrideSnoozeDuration * MINUTE_SECONDS) + now.add(Calendar.MINUTE, overrideSnoozeDuration) + setupAlarmClock(alarm!!, now) wasAlarmSnoozed = true finishActivity() } else if (config.useSameSnooze) { - setupAlarmClock(alarm!!, config.snoozeTime * MINUTE_SECONDS) + now.add(Calendar.MINUTE, config.snoozeTime) + setupAlarmClock(alarm!!, now) wasAlarmSnoozed = true finishActivity() } else { showPickSecondsDialog(config.snoozeTime * MINUTE_SECONDS, true, cancelCallback = { finishActivity() }) { config.snoozeTime = it / MINUTE_SECONDS - setupAlarmClock(alarm!!, it) + now.add(Calendar.SECOND, it) + setupAlarmClock(alarm!!, now) wasAlarmSnoozed = true finishActivity() } diff --git a/app/src/main/kotlin/org/fossify/clock/activities/SnoozeReminderActivity.kt b/app/src/main/kotlin/org/fossify/clock/activities/SnoozeReminderActivity.kt index 17f40d60..ab00a2f2 100644 --- a/app/src/main/kotlin/org/fossify/clock/activities/SnoozeReminderActivity.kt +++ b/app/src/main/kotlin/org/fossify/clock/activities/SnoozeReminderActivity.kt @@ -9,6 +9,7 @@ import org.fossify.clock.extensions.setupAlarmClock import org.fossify.clock.helpers.ALARM_ID import org.fossify.commons.extensions.showPickSecondsDialog import org.fossify.commons.helpers.MINUTE_SECONDS +import java.util.Calendar class SnoozeReminderActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -18,7 +19,7 @@ class SnoozeReminderActivity : AppCompatActivity() { hideNotification(id) showPickSecondsDialog(config.snoozeTime * MINUTE_SECONDS, true, cancelCallback = { dialogCancelled() }) { config.snoozeTime = it / MINUTE_SECONDS - setupAlarmClock(alarm, it) + setupAlarmClock(alarm, Calendar.getInstance().apply { add(Calendar.SECOND, it) }) finishActivity() } } diff --git a/app/src/main/kotlin/org/fossify/clock/extensions/Context.kt b/app/src/main/kotlin/org/fossify/clock/extensions/Context.kt index d2e2e7d7..fcaee97e 100644 --- a/app/src/main/kotlin/org/fossify/clock/extensions/Context.kt +++ b/app/src/main/kotlin/org/fossify/clock/extensions/Context.kt @@ -100,34 +100,13 @@ fun Context.createNewTimer(): Timer { } fun Context.scheduleNextAlarm(alarm: Alarm, showToast: Boolean) { - val calendar = Calendar.getInstance() - calendar.firstDayOfWeek = Calendar.MONDAY - - val nextAlarmDay = Calendar.getInstance() - - if (alarm.days == TODAY_BIT) { - - } else if (alarm.days == TOMORROW_BIT) { - nextAlarmDay.add(Calendar.DAY_OF_MONTH, 1) - } else { - for (i in 0..8) { - val currentDay = (nextAlarmDay.get(Calendar.DAY_OF_WEEK) + 5) % 7 - val isCorrectDay = alarm.days and 2.0.pow(currentDay).toInt() != 0 - if (isCorrectDay && (i > 0 || alarm.timeInMinutes > getCurrentDayMinutes())) { - break - } else { - nextAlarmDay.add(Calendar.DAY_OF_MONTH, 1) - } - } - } - nextAlarmDay.set(Calendar.HOUR, alarm.timeInMinutes / 60) - nextAlarmDay.set(Calendar.MINUTE, alarm.timeInMinutes % 60) - nextAlarmDay.set(Calendar.SECOND, 0) - val triggerInSeconds = (nextAlarmDay.getTimeInMillis() - calendar.getTimeInMillis()) / 1000; - setupAlarmClock(alarm, triggerInSeconds) + val triggerTime = getTimeOfNextAlarm(alarm.timeInMinutes, alarm.days) + setupAlarmClock(alarm, triggerTime) if (showToast) { - showRemainingTimeMessage(triggerInSeconds / 60) + val now = Calendar.getInstance() + val triggerInMillis = triggerTime.timeInMillis - now.timeInMillis + showRemainingTimeMessage((triggerInMillis / (1000 * 60)).toInt()) } } @@ -136,9 +115,10 @@ fun Context.showRemainingTimeMessage(totalMinutes: Int) { toast(fullString, Toast.LENGTH_LONG) } -fun Context.setupAlarmClock(alarm: Alarm, triggerInSeconds: Int) { +fun Context.setupAlarmClock(alarm: Alarm, triggerTime: Calendar) { val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager - val targetMS = System.currentTimeMillis() + triggerInSeconds * 1000 + + val targetMS = triggerTime.timeInMillis try { AlarmManagerCompat.setAlarmClock(alarmManager, targetMS, getOpenAlarmTabIntent(), getAlarmIntent(alarm)) @@ -271,27 +251,18 @@ fun Context.getClosestEnabledAlarmString(callback: (result: String) -> Unit) { return@getEnabledAlarms } + val nextAlarmList = enabledAlarms - .mapNotNull { getTimeUntilNextAlarm(it.timeInMinutes, it.days) } + .map { getTimeOfNextAlarm(it.timeInMinutes, it.days) } - if (nextAlarmList.isEmpty()) { - callback("") - } + val closestAlarmTime = nextAlarmList.minOrNull() - var closestAlarmTime = Int.MAX_VALUE - nextAlarmList.forEach { time -> - if (time < closestAlarmTime) { - closestAlarmTime = time - } - } - - if (closestAlarmTime == Int.MAX_VALUE) { + if (closestAlarmTime == null) { callback("") + return@getEnabledAlarms } - val calendar = Calendar.getInstance().apply { firstDayOfWeek = Calendar.MONDAY } - calendar.add(Calendar.MINUTE, closestAlarmTime) - val dayOfWeekIndex = (calendar.get(Calendar.DAY_OF_WEEK) + 5) % 7 + val dayOfWeekIndex = (closestAlarmTime.get(Calendar.DAY_OF_WEEK) + 5) % 7 val dayOfWeek = resources.getStringArray(org.fossify.commons.R.array.week_days_short)[dayOfWeekIndex] val pattern = if (DateFormat.is24HourFormat(this)) { "HH:mm" @@ -299,7 +270,7 @@ fun Context.getClosestEnabledAlarmString(callback: (result: String) -> Unit) { "h:mm a" } - val formattedTime = SimpleDateFormat(pattern, Locale.getDefault()).format(calendar.time) + val formattedTime = SimpleDateFormat(pattern, Locale.getDefault()).format(closestAlarmTime.time) callback("$dayOfWeek $formattedTime") } } diff --git a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt index 99fd99c4..d40cdeef 100644 --- a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt +++ b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt @@ -222,29 +222,36 @@ fun getAllTimeZones() = arrayListOf( MyTimeZone(89, "GMT+13:00 Tongatapu", "Pacific/Tongatapu") ) -fun getTimeUntilNextAlarm(alarmTimeInMinutes: Int, days: Int): Int? { - val calendar = Calendar.getInstance() - calendar.firstDayOfWeek = Calendar.MONDAY - val currentTimeInMinutes = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE) - val currentDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - Calendar.MONDAY +fun getTimeOfNextAlarm(alarmTimeInMinutes: Int, days: Int): Calendar { + val nextAlarmTime = Calendar.getInstance() + nextAlarmTime.firstDayOfWeek = Calendar.MONDAY - var minTimeDifferenceInMinutes = Int.MAX_VALUE + val hour = alarmTimeInMinutes / 60 + nextAlarmTime.set(Calendar.HOUR, hour) + nextAlarmTime.set(Calendar.AM_PM, if (hour >= 13) Calendar.PM else Calendar.AM) + nextAlarmTime.set(Calendar.MINUTE, alarmTimeInMinutes % 60) + nextAlarmTime.set(Calendar.SECOND, 0) + nextAlarmTime.set(Calendar.MILLISECOND, 0) - for (i in 0..6) { - val alarmDayOfWeek = (currentDayOfWeek + i) % 7 - if (isAlarmEnabledForDay(alarmDayOfWeek, days)) { - val timeDifferenceInMinutes = getTimeDifferenceInMinutes(currentTimeInMinutes, alarmTimeInMinutes, i) - if (timeDifferenceInMinutes < minTimeDifferenceInMinutes) { - minTimeDifferenceInMinutes = timeDifferenceInMinutes - } + if (days == TODAY_BIT) { + val now = Calendar.getInstance() + if (nextAlarmTime < now) { + nextAlarmTime.add(Calendar.DAY_OF_MONTH, 7) } - } - - return if (minTimeDifferenceInMinutes != Int.MAX_VALUE) { - minTimeDifferenceInMinutes + } else if (days == TOMORROW_BIT) { + nextAlarmTime.add(Calendar.DAY_OF_MONTH, 1) } else { - null + val now = Calendar.getInstance() + for (i in 0..8) { + val currentDay = (nextAlarmTime.get(Calendar.DAY_OF_WEEK) + 5) % 7 + if (isAlarmEnabledForDay(currentDay, days) && now < nextAlarmTime) { + break + } else { + nextAlarmTime.add(Calendar.DAY_OF_MONTH, 1) + } + } } + return nextAlarmTime } fun isAlarmEnabledForDay(day: Int, alarmDays: Int) = alarmDays.isBitSet(day) diff --git a/app/src/main/kotlin/org/fossify/clock/services/SnoozeService.kt b/app/src/main/kotlin/org/fossify/clock/services/SnoozeService.kt index 9b4024b4..dfdaa94f 100644 --- a/app/src/main/kotlin/org/fossify/clock/services/SnoozeService.kt +++ b/app/src/main/kotlin/org/fossify/clock/services/SnoozeService.kt @@ -8,12 +8,13 @@ import org.fossify.clock.extensions.hideNotification import org.fossify.clock.extensions.setupAlarmClock import org.fossify.clock.helpers.ALARM_ID import org.fossify.commons.helpers.MINUTE_SECONDS +import java.util.Calendar class SnoozeService : IntentService("Snooze") { override fun onHandleIntent(intent: Intent?) { val id = intent!!.getIntExtra(ALARM_ID, -1) val alarm = dbHelper.getAlarmWithId(id) ?: return hideNotification(id) - setupAlarmClock(alarm, config.snoozeTime * MINUTE_SECONDS) + setupAlarmClock(alarm, Calendar.getInstance().apply { add(Calendar.MINUTE, config.snoozeTime) }) } } From 983c9ec293817ad9d7d02e2b071205b75799632f Mon Sep 17 00:00:00 2001 From: Joshix Date: Mon, 28 Oct 2024 12:00:00 +0000 Subject: [PATCH 03/13] extract common snoozing logic into function --- .../clock/activities/ReminderActivity.kt | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/clock/activities/ReminderActivity.kt b/app/src/main/kotlin/org/fossify/clock/activities/ReminderActivity.kt index 3c0b6a41..b9382f46 100644 --- a/app/src/main/kotlin/org/fossify/clock/activities/ReminderActivity.kt +++ b/app/src/main/kotlin/org/fossify/clock/activities/ReminderActivity.kt @@ -267,28 +267,24 @@ class ReminderActivity : SimpleActivity() { private fun snoozeAlarm(overrideSnoozeDuration: Int? = null) { destroyEffects() - val now = Calendar.getInstance() if (overrideSnoozeDuration != null) { - now.add(Calendar.MINUTE, overrideSnoozeDuration) - setupAlarmClock(alarm!!, now) - wasAlarmSnoozed = true - finishActivity() + snoozeAlarm { add(Calendar.MINUTE, overrideSnoozeDuration) } } else if (config.useSameSnooze) { - now.add(Calendar.MINUTE, config.snoozeTime) - setupAlarmClock(alarm!!, now) - wasAlarmSnoozed = true - finishActivity() + snoozeAlarm { add(Calendar.MINUTE, config.snoozeTime) } } else { showPickSecondsDialog(config.snoozeTime * MINUTE_SECONDS, true, cancelCallback = { finishActivity() }) { config.snoozeTime = it / MINUTE_SECONDS - now.add(Calendar.SECOND, it) - setupAlarmClock(alarm!!, now) - wasAlarmSnoozed = true - finishActivity() + snoozeAlarm { add(Calendar.SECOND, it) } } } } + private fun snoozeAlarm(block: Calendar.() -> Unit) { + setupAlarmClock(alarm!!, block.run { Calendar.getInstance() }) + wasAlarmSnoozed = true + finishActivity() + } + private fun finishActivity() { if (!wasAlarmSnoozed && alarm != null) { cancelAlarmClock(alarm!!) From d7aca4c0086402222e3fdeabe0a4d3978cceae0d Mon Sep 17 00:00:00 2001 From: Joshix Date: Mon, 28 Oct 2024 12:00:00 +0000 Subject: [PATCH 04/13] fix setting the hour correctly --- app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt index d40cdeef..97f65792 100644 --- a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt +++ b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt @@ -227,9 +227,11 @@ fun getTimeOfNextAlarm(alarmTimeInMinutes: Int, days: Int): Calendar { nextAlarmTime.firstDayOfWeek = Calendar.MONDAY val hour = alarmTimeInMinutes / 60 - nextAlarmTime.set(Calendar.HOUR, hour) + val minute = alarmTimeInMinutes % 60 + nextAlarmTime.set(Calendar.AM_PM, if (hour >= 13) Calendar.PM else Calendar.AM) - nextAlarmTime.set(Calendar.MINUTE, alarmTimeInMinutes % 60) + nextAlarmTime.set(Calendar.HOUR, hour % 12) + nextAlarmTime.set(Calendar.MINUTE, minute) nextAlarmTime.set(Calendar.SECOND, 0) nextAlarmTime.set(Calendar.MILLISECOND, 0) From 6d1ccdf342d4992e774b8d60375be34c7a707431 Mon Sep 17 00:00:00 2001 From: Joshix Date: Mon, 28 Oct 2024 12:00:00 +0000 Subject: [PATCH 05/13] improve calender handling --- app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt index 97f65792..3be48517 100644 --- a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt +++ b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt @@ -229,8 +229,7 @@ fun getTimeOfNextAlarm(alarmTimeInMinutes: Int, days: Int): Calendar { val hour = alarmTimeInMinutes / 60 val minute = alarmTimeInMinutes % 60 - nextAlarmTime.set(Calendar.AM_PM, if (hour >= 13) Calendar.PM else Calendar.AM) - nextAlarmTime.set(Calendar.HOUR, hour % 12) + nextAlarmTime.set(Calendar.HOUR_OF_DAY, hour) nextAlarmTime.set(Calendar.MINUTE, minute) nextAlarmTime.set(Calendar.SECOND, 0) nextAlarmTime.set(Calendar.MILLISECOND, 0) @@ -238,7 +237,7 @@ fun getTimeOfNextAlarm(alarmTimeInMinutes: Int, days: Int): Calendar { if (days == TODAY_BIT) { val now = Calendar.getInstance() if (nextAlarmTime < now) { - nextAlarmTime.add(Calendar.DAY_OF_MONTH, 7) + nextAlarmTime.add(Calendar.WEEK_OF_YEAR, 1) } } else if (days == TOMORROW_BIT) { nextAlarmTime.add(Calendar.DAY_OF_MONTH, 1) From 5f2ec5164cc5e10114d7fdfca44298825bfcd4d7 Mon Sep 17 00:00:00 2001 From: Joshix-1 <57299889+Joshix-1@users.noreply.github.com> Date: Tue, 29 Oct 2024 10:28:18 +0100 Subject: [PATCH 06/13] remove check --- app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt index 3be48517..623dae5b 100644 --- a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt +++ b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt @@ -235,10 +235,7 @@ fun getTimeOfNextAlarm(alarmTimeInMinutes: Int, days: Int): Calendar { nextAlarmTime.set(Calendar.MILLISECOND, 0) if (days == TODAY_BIT) { - val now = Calendar.getInstance() - if (nextAlarmTime < now) { - nextAlarmTime.add(Calendar.WEEK_OF_YEAR, 1) - } + // do nothing, alarm is today } else if (days == TOMORROW_BIT) { nextAlarmTime.add(Calendar.DAY_OF_MONTH, 1) } else { From f9d7d811905fd18b744b30c0406360197bbee34d Mon Sep 17 00:00:00 2001 From: Joshix-1 <57299889+Joshix-1@users.noreply.github.com> Date: Tue, 29 Oct 2024 10:29:41 +0100 Subject: [PATCH 07/13] use getBitForCalendarDay --- app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt index 623dae5b..f6339f6a 100644 --- a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt +++ b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt @@ -241,7 +241,7 @@ fun getTimeOfNextAlarm(alarmTimeInMinutes: Int, days: Int): Calendar { } else { val now = Calendar.getInstance() for (i in 0..8) { - val currentDay = (nextAlarmTime.get(Calendar.DAY_OF_WEEK) + 5) % 7 + val currentDay = getBitForCalendarDay(Calendar.DAY_OF_WEEK) if (isAlarmEnabledForDay(currentDay, days) && now < nextAlarmTime) { break } else { From 96f72b05b355b23255dd21b089efc8cc59de9e7a Mon Sep 17 00:00:00 2001 From: Joshix Date: Tue, 29 Oct 2024 15:00:00 +0000 Subject: [PATCH 08/13] remove unused fun --- .../main/kotlin/org/fossify/clock/helpers/Constants.kt | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt index f6339f6a..2261cb70 100644 --- a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt +++ b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt @@ -253,13 +253,3 @@ fun getTimeOfNextAlarm(alarmTimeInMinutes: Int, days: Int): Calendar { } fun isAlarmEnabledForDay(day: Int, alarmDays: Int) = alarmDays.isBitSet(day) - -fun getTimeDifferenceInMinutes(currentTimeInMinutes: Int, alarmTimeInMinutes: Int, daysUntilAlarm: Int): Int { - val minutesInADay = 24 * 60 - val minutesUntilAlarm = daysUntilAlarm * minutesInADay + alarmTimeInMinutes - return if (minutesUntilAlarm > currentTimeInMinutes) { - minutesUntilAlarm - currentTimeInMinutes - } else { - minutesInADay - (currentTimeInMinutes - minutesUntilAlarm) - } -} From 66a8603935a7f48330e8afc993af32761713c3f2 Mon Sep 17 00:00:00 2001 From: Joshix Date: Tue, 29 Oct 2024 15:00:00 +0000 Subject: [PATCH 09/13] use getBitForCalendarDay --- .../main/kotlin/org/fossify/clock/helpers/Constants.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt index 2261cb70..e6695aff 100644 --- a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt +++ b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt @@ -111,14 +111,14 @@ fun formatTime(showSeconds: Boolean, use24HourFormat: Boolean, hours: Int, minut fun getTomorrowBit(): Int { val calendar = Calendar.getInstance() calendar.add(Calendar.DAY_OF_WEEK, 1) - val dayOfWeek = (calendar.get(Calendar.DAY_OF_WEEK) + 5) % 7 - return 2.0.pow(dayOfWeek).toInt() + val day = calendar.get(Calendar.DAY_OF_WEEK) + return getBitForCalendarDay(day) } fun getTodayBit(): Int { val calendar = Calendar.getInstance() - val dayOfWeek = (calendar.get(Calendar.DAY_OF_WEEK) + 5) % 7 - return 2.0.pow(dayOfWeek).toInt() + val day = calendar.get(Calendar.DAY_OF_WEEK) + return getBitForCalendarDay(day) } fun getBitForCalendarDay(day: Int): Int { From 8f76795d1dc1f5b9636f534517453dc8d18cbf13 Mon Sep 17 00:00:00 2001 From: Joshix Date: Tue, 29 Oct 2024 15:00:00 +0000 Subject: [PATCH 10/13] filter out alarms in the past --- app/src/main/kotlin/org/fossify/clock/extensions/Context.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/kotlin/org/fossify/clock/extensions/Context.kt b/app/src/main/kotlin/org/fossify/clock/extensions/Context.kt index fcaee97e..d79a08a6 100644 --- a/app/src/main/kotlin/org/fossify/clock/extensions/Context.kt +++ b/app/src/main/kotlin/org/fossify/clock/extensions/Context.kt @@ -251,9 +251,10 @@ fun Context.getClosestEnabledAlarmString(callback: (result: String) -> Unit) { return@getEnabledAlarms } - + val now = Calendar.getInstance() val nextAlarmList = enabledAlarms .map { getTimeOfNextAlarm(it.timeInMinutes, it.days) } + .filter { it > now } val closestAlarmTime = nextAlarmList.minOrNull() From 1203f093cd8e5cf8aa68119f33c5a4a748a98897 Mon Sep 17 00:00:00 2001 From: Joshix Date: Tue, 29 Oct 2024 15:00:00 +0000 Subject: [PATCH 11/13] fix getTimeOfNextAlarm --- .../org/fossify/clock/helpers/Constants.kt | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt index e6695aff..c1e62797 100644 --- a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt +++ b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt @@ -236,20 +236,19 @@ fun getTimeOfNextAlarm(alarmTimeInMinutes: Int, days: Int): Calendar { if (days == TODAY_BIT) { // do nothing, alarm is today - } else if (days == TOMORROW_BIT) { - nextAlarmTime.add(Calendar.DAY_OF_MONTH, 1) - } else { - val now = Calendar.getInstance() - for (i in 0..8) { - val currentDay = getBitForCalendarDay(Calendar.DAY_OF_WEEK) - if (isAlarmEnabledForDay(currentDay, days) && now < nextAlarmTime) { - break - } else { - nextAlarmTime.add(Calendar.DAY_OF_MONTH, 1) - } + return nextAlarmTime + } + if (days == TOMORROW_BIT) { + return nextAlarmTime.apply { add(Calendar.DAY_OF_MONTH, 1) } + } + val now = Calendar.getInstance() + for (i in 0..8) { + val currentDay = (nextAlarmTime.get(Calendar.DAY_OF_WEEK) + 5) % 7 + if (days.isBitSet(currentDay) && now < nextAlarmTime) { + return nextAlarmTime + } else { + nextAlarmTime.add(Calendar.DAY_OF_MONTH, 1) } } - return nextAlarmTime + throw RuntimeException("Failed to getTimeOfNextAlarm") } - -fun isAlarmEnabledForDay(day: Int, alarmDays: Int) = alarmDays.isBitSet(day) From 003d96b496d0a903323b57a3f869f070176d8a11 Mon Sep 17 00:00:00 2001 From: Joshix Date: Tue, 29 Oct 2024 15:00:00 +0000 Subject: [PATCH 12/13] return nullable calendar in getTimeOfNextAlarm --- app/src/main/kotlin/org/fossify/clock/extensions/Context.kt | 4 ++-- app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/clock/extensions/Context.kt b/app/src/main/kotlin/org/fossify/clock/extensions/Context.kt index d79a08a6..ca1ad4ff 100644 --- a/app/src/main/kotlin/org/fossify/clock/extensions/Context.kt +++ b/app/src/main/kotlin/org/fossify/clock/extensions/Context.kt @@ -100,7 +100,7 @@ fun Context.createNewTimer(): Timer { } fun Context.scheduleNextAlarm(alarm: Alarm, showToast: Boolean) { - val triggerTime = getTimeOfNextAlarm(alarm.timeInMinutes, alarm.days) + val triggerTime = getTimeOfNextAlarm(alarm.timeInMinutes, alarm.days) ?: return setupAlarmClock(alarm, triggerTime) if (showToast) { @@ -253,7 +253,7 @@ fun Context.getClosestEnabledAlarmString(callback: (result: String) -> Unit) { val now = Calendar.getInstance() val nextAlarmList = enabledAlarms - .map { getTimeOfNextAlarm(it.timeInMinutes, it.days) } + .mapNotNull { getTimeOfNextAlarm(it.timeInMinutes, it.days) } .filter { it > now } val closestAlarmTime = nextAlarmList.minOrNull() diff --git a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt index c1e62797..470eadd6 100644 --- a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt +++ b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt @@ -222,7 +222,7 @@ fun getAllTimeZones() = arrayListOf( MyTimeZone(89, "GMT+13:00 Tongatapu", "Pacific/Tongatapu") ) -fun getTimeOfNextAlarm(alarmTimeInMinutes: Int, days: Int): Calendar { +fun getTimeOfNextAlarm(alarmTimeInMinutes: Int, days: Int): Calendar? { val nextAlarmTime = Calendar.getInstance() nextAlarmTime.firstDayOfWeek = Calendar.MONDAY @@ -250,5 +250,5 @@ fun getTimeOfNextAlarm(alarmTimeInMinutes: Int, days: Int): Calendar { nextAlarmTime.add(Calendar.DAY_OF_MONTH, 1) } } - throw RuntimeException("Failed to getTimeOfNextAlarm") + return null } From d492066fb8a0cbf36ac296710f69b0062844d13a Mon Sep 17 00:00:00 2001 From: Joshix Date: Sat, 9 Nov 2024 21:00:00 +0000 Subject: [PATCH 13/13] fix code-style stuff --- .../org/fossify/clock/helpers/Constants.kt | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt index 470eadd6..5cd47c57 100644 --- a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt +++ b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt @@ -234,21 +234,25 @@ fun getTimeOfNextAlarm(alarmTimeInMinutes: Int, days: Int): Calendar? { nextAlarmTime.set(Calendar.SECOND, 0) nextAlarmTime.set(Calendar.MILLISECOND, 0) - if (days == TODAY_BIT) { - // do nothing, alarm is today - return nextAlarmTime - } - if (days == TOMORROW_BIT) { - return nextAlarmTime.apply { add(Calendar.DAY_OF_MONTH, 1) } - } - val now = Calendar.getInstance() - for (i in 0..8) { - val currentDay = (nextAlarmTime.get(Calendar.DAY_OF_WEEK) + 5) % 7 - if (days.isBitSet(currentDay) && now < nextAlarmTime) { - return nextAlarmTime - } else { - nextAlarmTime.add(Calendar.DAY_OF_MONTH, 1) + return when (days) { + TODAY_BIT -> { + // do nothing, alarm is today + nextAlarmTime + } + TOMORROW_BIT -> { + nextAlarmTime.apply { add(Calendar.DAY_OF_MONTH, 1) } + } + else -> { + val now = Calendar.getInstance() + repeat(8) { + val currentDay = (nextAlarmTime.get(Calendar.DAY_OF_WEEK) + 5) % 7 + if (days.isBitSet(currentDay) && now < nextAlarmTime) { + return nextAlarmTime + } else { + nextAlarmTime.add(Calendar.DAY_OF_MONTH, 1) + } + } + null } } - return null }