Prevent firing notifications one minute early

pull/2912/head
Alex Baker 2 years ago
parent df8f637239
commit b918e87e05

@ -144,6 +144,41 @@ class AlarmJobServiceTest : InjectingTestCase() {
} }
} }
@Test
fun alarmsOneMinuteApart() = runBlocking {
freezeAt(DateTime(2024, 5, 17, 23, 20)) {
taskDao.insert(
Task(
dueDate = createDueDate(
Task.URGENCY_SPECIFIC_DAY_TIME,
DateTime(2024, 5, 17, 23, 20).millis
)
)
)
alarmService.synchronizeAlarms(1, mutableSetOf(Alarm(type = Alarm.TYPE_REL_END)))
taskDao.insert(Task())
alarmService.synchronizeAlarms(
taskId = 2,
alarms = mutableSetOf(
Alarm(
type = Alarm.TYPE_SNOOZE,
time = DateTime(2024, 5, 17, 23, 21).millis)
)
)
testResults(
listOf(
Notification(
taskId = 1L,
timestamp = DateTimeUtils2.currentTimeMillis(),
type = Alarm.TYPE_REL_END
)
),
DateTime(2024, 5, 17, 23, 21).millis
)
}
}
@Test @Test
fun futureSnoozeOverrideOverdue() = runBlocking { fun futureSnoozeOverrideOverdue() = runBlocking {
freezeAt(DateTime(2024, 5, 17, 23, 20)) { freezeAt(DateTime(2024, 5, 17, 23, 20)) {

@ -73,15 +73,15 @@ class AlarmCalculator(
* We take the last reminder time and add approximately the reminder period. If it's still in * We take the last reminder time and add approximately the reminder period. If it's still in
* the past, we set it to some time in the near future. * the past, we set it to some time in the near future.
*/ */
private fun calculateNextRandomReminder(random: Random, task: Task, reminderPeriod: Long): Long { private fun calculateNextRandomReminder(random: Random, task: Task, reminderPeriod: Long) =
if (reminderPeriod > 0) { if (reminderPeriod > 0) {
var `when` = task.reminderLast maxOf(
if (`when` == 0L) { task.reminderLast
`when` = task.creationDate .coerceAtLeast(task.creationDate)
} .plus((reminderPeriod * (0.85f + 0.3f * random.nextFloat())).toLong()),
`when` += (reminderPeriod * (0.85f + 0.3f * random.nextFloat())).toLong() task.hideUntil
return Math.max(`when`, task.hideUntil) )
} else {
AlarmService.NO_ALARM
} }
return AlarmService.NO_ALARM
}
} }

@ -104,7 +104,9 @@ class AlarmService @Inject constructor(
val alarmEntries = alarms.mapNotNull { val alarmEntries = alarms.mapNotNull {
alarmCalculator.toAlarmEntry(task, it) alarmCalculator.toAlarmEntry(task, it)
} }
val (now, later) = alarmEntries.partition { it.timestamp <= DateTime().startOfMinute().plusMinutes(1).millis } val (now, later) = alarmEntries.partition {
it.timestamp < DateTime().startOfMinute().plusMinutes(1).millis
}
later later
.filter { it.type == TYPE_SNOOZE } .filter { it.type == TYPE_SNOOZE }
.maxByOrNull { it.timestamp } .maxByOrNull { it.timestamp }

Loading…
Cancel
Save