From 6b0f812835a0f3363c77abd0ce7de8234ebe3caa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B6rkert?= Date: Sun, 22 Nov 2026 13:46:32 +0100 Subject: [PATCH] Fire day-only alarms when night starts at midnight The alarm condition check and the sound check now share AlarmConfiguration.isNight, where night runs from Night starts up to the next Day starts. With Night starts at 00:00 a Day alarm fires during the day and is held back from midnight until Day starts. Version 8.0.11. --- app/build.gradle.kts | 2 +- .../loopandlearn/loopfollow/alarm/Alarm.kt | 12 ++- .../loopfollow/alarm/AlarmConditions.kt | 5 +- .../loopfollow/alarm/AlarmManager.kt | 12 +-- .../loopfollow/ui/alarms/SnoozerScreen.kt | 2 +- .../loopfollow/alarm/DayNightScheduleTest.kt | 98 +++++++++++++++++++ 6 files changed, 113 insertions(+), 18 deletions(-) create mode 100644 app/src/test/java/com/loopandlearn/loopfollow/alarm/DayNightScheduleTest.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 1f99a40..af0f08a 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -8,7 +8,7 @@ plugins { } // The release version. versionCode grows with it (1.2.3 -> 10203), so every release installs over the one before. -val lfVersion = "8.0.9" +val lfVersion = "8.0.11" val lfVersionCode = lfVersion.split(".").map { it.toInt() }.let { (major, minor, patch) -> major * 10000 + minor * 100 + patch } // A second or third LoopFollow next to the first one, each with its own settings: -PlfAppSuffix=Second gives the diff --git a/app/src/main/java/com/loopandlearn/loopfollow/alarm/Alarm.kt b/app/src/main/java/com/loopandlearn/loopfollow/alarm/Alarm.kt index 4385960..fb88f9b 100644 --- a/app/src/main/java/com/loopandlearn/loopfollow/alarm/Alarm.kt +++ b/app/src/main/java/com/loopandlearn/loopfollow/alarm/Alarm.kt @@ -14,6 +14,7 @@ import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put +import java.util.Calendar import java.util.UUID /** Categorizes alarms, prioritized in the order they appear here. */ @@ -328,7 +329,16 @@ data class AlarmConfiguration( val ignoreZeroBG: Boolean = true, val autoSnoozeCGMStart: Boolean = false, val enableVolumeButtonSnooze: Boolean = false, -) +) { + /** Night runs from [nightStart] up to the next [dayStart], wrapping past midnight when [nightStart] is later in the day. */ + fun isNight(now: Double): Boolean { + val cal = Calendar.getInstance().apply { timeInMillis = (now * 1000).toLong() } + val nowMin = cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE) + val day = dayStart.minutesSinceMidnight + val night = nightStart.minutesSinceMidnight + return if (night >= day) nowMin >= night || nowMin < day else nowMin >= night && nowMin < day + } +} /** A future-dated carb entry whose scheduled time has not yet arrived. */ @Serializable diff --git a/app/src/main/java/com/loopandlearn/loopfollow/alarm/AlarmConditions.kt b/app/src/main/java/com/loopandlearn/loopfollow/alarm/AlarmConditions.kt index 77c3bcc..1e64a47 100644 --- a/app/src/main/java/com/loopandlearn/loopfollow/alarm/AlarmConditions.kt +++ b/app/src/main/java/com/loopandlearn/loopfollow/alarm/AlarmConditions.kt @@ -2,7 +2,6 @@ package com.loopandlearn.loopfollow.alarm import com.loopandlearn.loopfollow.storage.Storage import com.loopandlearn.loopfollow.storage.StorageValue -import java.util.Calendar import kotlin.math.abs import kotlin.math.ceil @@ -44,9 +43,7 @@ abstract class AlarmCondition(val type: AlarmType) { if (snooze != null && snooze > now) return false if (!passesBGLimits(alarm, data)) return false - val cal = Calendar.getInstance().apply { timeInMillis = (now * 1000).toLong() } - val nowMin = cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE) - val isNight = nowMin < config.dayStart.minutesSinceMidnight || nowMin >= config.nightStart.minutesSinceMidnight + val isNight = config.isNight(now) when (alarm.activeOption) { DayNightOption.DAY -> if (isNight) return false DayNightOption.NIGHT -> if (!isNight) return false diff --git a/app/src/main/java/com/loopandlearn/loopfollow/alarm/AlarmManager.kt b/app/src/main/java/com/loopandlearn/loopfollow/alarm/AlarmManager.kt index 529e787..f40d506 100644 --- a/app/src/main/java/com/loopandlearn/loopfollow/alarm/AlarmManager.kt +++ b/app/src/main/java/com/loopandlearn/loopfollow/alarm/AlarmManager.kt @@ -7,7 +7,6 @@ import com.loopandlearn.loopfollow.helpers.LogManager import com.loopandlearn.loopfollow.helpers.nowSeconds import com.loopandlearn.loopfollow.storage.Observable import com.loopandlearn.loopfollow.storage.Storage -import java.util.Calendar object AlarmManager { private val evaluators: Map = listOf( @@ -116,7 +115,7 @@ object AlarmManager { if (mute != null && mute > now) playSound = false if (!config.audioDuringCalls && isOnPhoneCall()) playSound = false - val isNight = isNight(config, now) + val isNight = config.isNight(now) val isDay = !isNight when (alarm.playSoundOption) { DayNightOption.NEVER -> playSound = false @@ -144,15 +143,6 @@ object AlarmManager { } } - /** Night runs from nightStart to dayStart, across midnight when nightStart is later. */ - fun isNight(config: AlarmConfiguration, now: Double): Boolean { - val cal = Calendar.getInstance().apply { timeInMillis = (now * 1000).toLong() } - val nowMin = cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE) - val day = config.dayStart.minutesSinceMidnight - val night = config.nightStart.minutesSinceMidnight - return if (night >= day) nowMin >= night || nowMin < day else nowMin >= night && nowMin < day - } - private fun isOnPhoneCall(): Boolean { val am = LoopFollowApplication.instance.getSystemService(Context.AUDIO_SERVICE) as AudioManager return am.mode == AudioManager.MODE_IN_CALL || am.mode == AudioManager.MODE_IN_COMMUNICATION diff --git a/app/src/main/java/com/loopandlearn/loopfollow/ui/alarms/SnoozerScreen.kt b/app/src/main/java/com/loopandlearn/loopfollow/ui/alarms/SnoozerScreen.kt index d3f585a..5eb2f07 100644 --- a/app/src/main/java/com/loopandlearn/loopfollow/ui/alarms/SnoozerScreen.kt +++ b/app/src/main/java/com/loopandlearn/loopfollow/ui/alarms/SnoozerScreen.kt @@ -239,7 +239,7 @@ fun SnoozerScreen() { } } -private fun isNightNow(): Boolean = AlarmManager.isNight(Storage.alarmConfiguration.value, nowSeconds()) +private fun isNightNow(): Boolean = Storage.alarmConfiguration.value.isNight(nowSeconds()) @Composable private fun SnoozerBar( diff --git a/app/src/test/java/com/loopandlearn/loopfollow/alarm/DayNightScheduleTest.kt b/app/src/test/java/com/loopandlearn/loopfollow/alarm/DayNightScheduleTest.kt new file mode 100644 index 0000000..e9771b9 --- /dev/null +++ b/app/src/test/java/com/loopandlearn/loopfollow/alarm/DayNightScheduleTest.kt @@ -0,0 +1,98 @@ +package com.loopandlearn.loopfollow.alarm + +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test +import java.util.Calendar + +class DayNightScheduleTest { + private fun config(day: Pair, night: Pair) = + AlarmConfiguration(dayStart = TimeOfDay(day.first, day.second), nightStart = TimeOfDay(night.first, night.second)) + + private fun at(hour: Int, minute: Int): Double { + val cal = Calendar.getInstance().apply { + set(Calendar.HOUR_OF_DAY, hour) + set(Calendar.MINUTE, minute) + set(Calendar.SECOND, 0) + set(Calendar.MILLISECOND, 0) + } + return cal.timeInMillis / 1000.0 + } + + private fun lowAlarm(active: DayNightOption) = + Alarm(type = AlarmType.LOW, belowBG = 80.0, persistentMinutes = 0, activeOption = active) + + private fun lowData(now: Double) = AlarmData( + bgReadings = listOf(60, 60, 60).mapIndexed { i, v -> GlucoseValue(v, now - (2 - i) * 300.0) }, + predictionData = emptyList(), + expireDate = null, + lastLoopTime = null, + latestOverrideStart = null, + latestOverrideEnd = null, + latestTempTargetStart = null, + latestTempTargetEnd = null, + activeOverrideEnd = null, + activeTempTargetEnd = null, + recBolus = null, + cob = null, + sageInsertTime = null, + pumpInsertTime = null, + latestPumpVolume = null, + iob = null, + recentBoluses = emptyList(), + latestBattery = null, + latestBatteryIsCharging = null, + latestPumpBattery = null, + batteryHistory = emptyList(), + recentCarbs = emptyList(), + dbSizePercentage = null, + ) + + @Test fun eveningNightStartWrapsPastMidnight() { + val c = config(7 to 0, 22 to 0) + assertFalse(c.isNight(at(7, 0))) + assertFalse(c.isNight(at(12, 0))) + assertFalse(c.isNight(at(21, 59))) + assertTrue(c.isNight(at(22, 0))) + assertTrue(c.isNight(at(0, 0))) + assertTrue(c.isNight(at(6, 59))) + } + + @Test fun midnightNightStartIsNightOnlyUntilDayStart() { + val c = config(7 to 0, 0 to 0) + assertTrue(c.isNight(at(0, 0))) + assertTrue(c.isNight(at(3, 30))) + assertTrue(c.isNight(at(6, 59))) + assertFalse(c.isNight(at(7, 0))) + assertFalse(c.isNight(at(12, 0))) + assertFalse(c.isNight(at(23, 59))) + } + + @Test fun nightStartAfterMidnightBeforeDayStart() { + val c = config(7 to 0, 1 to 30) + assertFalse(c.isNight(at(0, 45))) + assertTrue(c.isNight(at(1, 30))) + assertTrue(c.isNight(at(6, 59))) + assertFalse(c.isNight(at(7, 0))) + } + + @Test fun dayAlarmFiresDuringTheDayWithMidnightNightStart() { + val c = config(7 to 0, 0 to 0) + val now = at(12, 0) + assertTrue(LowBGCondition().shouldFire(lowAlarm(DayNightOption.DAY), lowData(now), now, c)) + } + + @Test fun dayAlarmIsHeldBackAtNightWithMidnightNightStart() { + val c = config(7 to 0, 0 to 0) + val now = at(3, 0) + assertFalse(LowBGCondition().shouldFire(lowAlarm(DayNightOption.DAY), lowData(now), now, c)) + } + + @Test fun nightAlarmFollowsTheSameSchedule() { + val c = config(7 to 0, 0 to 0) + val night = at(3, 0) + val day = at(12, 0) + assertTrue(LowBGCondition().shouldFire(lowAlarm(DayNightOption.NIGHT), lowData(night), night, c)) + assertFalse(LowBGCondition().shouldFire(lowAlarm(DayNightOption.NIGHT), lowData(day), day, c)) + } +}