From b4ff90ce5d3a33e4299c9fb96a5afc0607012994 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/14] 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 9250a9a3cd9c1610ae0cae3b720d09ad00db4bbd Mon Sep 17 00:00:00 2001 From: Joshix Date: Mon, 28 Oct 2024 12:00:00 +0000 Subject: [PATCH 02/14] 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 91474a42f52fd343adbb41f6088f90144a3cb35c Mon Sep 17 00:00:00 2001 From: Joshix Date: Mon, 28 Oct 2024 12:00:00 +0000 Subject: [PATCH 03/14] 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 ff6f15872a509ac9525764d1a2e6ea1209d14668 Mon Sep 17 00:00:00 2001 From: Joshix Date: Mon, 28 Oct 2024 12:00:00 +0000 Subject: [PATCH 04/14] 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 2f05baf50c921cbb83d9c58e0dff946496fcea71 Mon Sep 17 00:00:00 2001 From: Joshix Date: Mon, 28 Oct 2024 12:00:00 +0000 Subject: [PATCH 05/14] 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 3819229ca64d36af0d24cfa12608fba885fd5036 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/14] 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 47371ed8f7ecdba292d4ac496cf4d89c272d4fbe 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/14] 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 20f73758570cf1fb1fbf0677315b52b55ea71958 Mon Sep 17 00:00:00 2001 From: Joshix Date: Tue, 29 Oct 2024 15:00:00 +0000 Subject: [PATCH 08/14] 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 e9083cebf3dbba41c6d1161a986368f45a45d221 Mon Sep 17 00:00:00 2001 From: Joshix Date: Tue, 29 Oct 2024 15:00:00 +0000 Subject: [PATCH 09/14] 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 29a06913a2e3addfeb6a6144886488e1171ec15e Mon Sep 17 00:00:00 2001 From: Joshix Date: Tue, 29 Oct 2024 15:00:00 +0000 Subject: [PATCH 10/14] 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 0b90ef17950c9d3792b2c7e699abda9796bcc1c7 Mon Sep 17 00:00:00 2001 From: Joshix Date: Tue, 29 Oct 2024 15:00:00 +0000 Subject: [PATCH 11/14] 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 dabeb743a8c647ac221d38cebfc6ba893ca52fae Mon Sep 17 00:00:00 2001 From: Joshix Date: Tue, 29 Oct 2024 15:00:00 +0000 Subject: [PATCH 12/14] 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 7aaf9db04ce0934397c16e897139cb5742790ab7 Mon Sep 17 00:00:00 2001 From: Joshix Date: Sat, 9 Nov 2024 21:00:00 +0000 Subject: [PATCH 13/14] 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 } From 7005396c0bd2a39f34253b4892691ca830bf27a8 Mon Sep 17 00:00:00 2001 From: Joshix Date: Wed, 29 Jan 2025 18:00:00 +0000 Subject: [PATCH 14/14] small changes --- .../kotlin/org/fossify/clock/extensions/Context.kt | 10 ++++------ .../main/kotlin/org/fossify/clock/helpers/Constants.kt | 5 +++++ 2 files changed, 9 insertions(+), 6 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 ca1ad4ff..c597e6ad 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) ?: return + val triggerTime = getTimeOfNextAlarm(alarm) ?: return setupAlarmClock(alarm, triggerTime) if (showToast) { @@ -253,7 +253,7 @@ fun Context.getClosestEnabledAlarmString(callback: (result: String) -> Unit) { val now = Calendar.getInstance() val nextAlarmList = enabledAlarms - .mapNotNull { getTimeOfNextAlarm(it.timeInMinutes, it.days) } + .mapNotNull(::getTimeOfNextAlarm) .filter { it > now } val closestAlarmTime = nextAlarmList.minOrNull() @@ -325,7 +325,7 @@ fun Context.getTimerNotification(timer: Timer, pendingIntent: PendingIntent, add if (isOreoPlus()) { try { notificationManager.deleteNotificationChannel(channelId) - } catch (e: Exception) { + } catch (_: Exception) { } val audioAttributes = AudioAttributes.Builder() @@ -351,10 +351,8 @@ fun Context.getTimerNotification(timer: Timer, pendingIntent: PendingIntent, add } } - val title = if (timer.label.isEmpty()) { + val title = timer.label.ifEmpty { getString(R.string.timer) - } else { - timer.label } val reminderActivityIntent = getReminderActivityIntent() 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 5cd47c57..8012f546 100644 --- a/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt +++ b/app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt @@ -1,6 +1,7 @@ package org.fossify.clock.helpers import org.fossify.clock.extensions.isBitSet +import org.fossify.clock.models.Alarm import org.fossify.clock.models.MyTimeZone import org.fossify.commons.helpers.* import java.util.Calendar @@ -222,6 +223,10 @@ fun getAllTimeZones() = arrayListOf( MyTimeZone(89, "GMT+13:00 Tongatapu", "Pacific/Tongatapu") ) +fun getTimeOfNextAlarm(alarm: Alarm): Calendar? { + return getTimeOfNextAlarm(alarm.timeInMinutes, alarm.days) +} + fun getTimeOfNextAlarm(alarmTimeInMinutes: Int, days: Int): Calendar? { val nextAlarmTime = Calendar.getInstance() nextAlarmTime.firstDayOfWeek = Calendar.MONDAY