Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion app/src/main/java/com/loopandlearn/loopfollow/alarm/Alarm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AlarmType, AlarmCondition> = listOf(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Int, Int>, night: Pair<Int, Int>) =
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))
}
}