mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-22 17:22:18 +01:00
Resend the lockdown on an already-penalised day
The workday lockdown signal was fire-once: a day already penalised returned ALREADY_PENALISED and sent nothing, so a signal focus-owner missed (not running, not yet Device Owner) never landed (live check C6, 2026-11-22). StickCheck.run now re-sends the stored locked_through when it is today or later, without touching the streak or the date; lockdown_sent carries resend=true. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
83e1e158fd
commit
8253836612
@ -62,6 +62,10 @@ a minute is recorded once (both activities run the launch check).
|
||||
does afterwards: a lockdown through the next day, stacking. The streak
|
||||
links penalty days, stick days (Tue–Thu) plus bypass days; free and sick
|
||||
days are skipped. A plain late dismiss on Fri–Mon still changes nothing.
|
||||
A later check or bypass on the day already penalised sends the stored
|
||||
lockdown (`locked_through`, if today or later) again, same date and
|
||||
streak, so a signal focus-owner missed (not running, not yet Device
|
||||
Owner) still lands.
|
||||
- **The phone rings again** (`Bypass.ringAgain`). During a session it is a
|
||||
re-ring: only the full dismiss sequence silences it and `re_rings` goes up
|
||||
by one when it does (`SessionHandoff.afterDismiss`).
|
||||
|
||||
@ -157,7 +157,8 @@ Phone (`phone_events.kind`): `alarm_fired` (data: `reason` =
|
||||
`exact`|`boot`|`watchdog`, `date` judged, `verdict`, incl. `free_day`|`sick_day`), `bypass` (data: `cause`, `date`, `session`; `DOCS-bypass.md`),
|
||||
`bypass_ignored`, `notifications` (data: `enabled`, `trigger` =
|
||||
`evening`|`app_block`|`channel_block`; `DOCS-bypass.md`), `unsuspended`, `lockdown_sent` (data:
|
||||
`until`, `streak`), `lift_sent`, `escape_used`, `boot`.
|
||||
`until`, `streak`, `resend`: true when a day already penalised sent its
|
||||
stored lockdown again), `lift_sent`, `escape_used`, `boot`.
|
||||
|
||||
`ring_started` is published twice over: a headless Dart engine started by
|
||||
`AlarmReceiver` at the fire (so a full-screen intent downgraded to a heads-up
|
||||
|
||||
@ -67,6 +67,8 @@ data class StickRun(
|
||||
val state: StickState,
|
||||
val checkedThrough: LocalDate,
|
||||
val signalUntil: LocalDate?,
|
||||
/** [signalUntil] repeats a lockdown already bought, not a new miss's. */
|
||||
val resend: Boolean = false,
|
||||
)
|
||||
|
||||
/**
|
||||
@ -93,6 +95,9 @@ object StickCheck {
|
||||
/**
|
||||
* Judges every unchecked day, oldest first, then today.
|
||||
*
|
||||
* A day found [StickVerdict.ALREADY_PENALISED] re-sends the stored
|
||||
* lockdown when it still runs through today ([StickRun.resend]).
|
||||
*
|
||||
* Past days run from the day after [StickInputs.checkedThrough], at most
|
||||
* [BACKLOG_DAYS] back -- in order, so the streak builds exactly as if
|
||||
* each day had been checked on time. A first run (no checkedThrough)
|
||||
@ -104,6 +109,7 @@ object StickCheck {
|
||||
val verdicts = mutableListOf<DayVerdict>()
|
||||
var state = inputs.state
|
||||
var signal: LocalDate? = null
|
||||
var resend = false
|
||||
val checked = inputs.checkedThrough
|
||||
var day = if (checked == null) {
|
||||
today
|
||||
@ -117,12 +123,22 @@ object StickCheck {
|
||||
verdicts += DayVerdict(day, verdict)
|
||||
if (verdict == StickVerdict.MISS) {
|
||||
signal = next.lockedThrough?.takeIf { !it.isBefore(today) }
|
||||
resend = false
|
||||
} else if (verdict == StickVerdict.ALREADY_PENALISED) {
|
||||
// The one signal may never have landed (focus-owner not
|
||||
// running or not yet Device Owner, C6 2026-11-22), so a day
|
||||
// already penalised sends the stored lockdown again. Same
|
||||
// date, same streak: focus-owner applies it idempotently.
|
||||
state.lockedThrough?.takeIf { !it.isBefore(today) }?.let {
|
||||
signal = it
|
||||
resend = true
|
||||
}
|
||||
}
|
||||
state = next
|
||||
day = day.plusDays(1)
|
||||
}
|
||||
val settled = if (verdicts.last().verdict.final) today else today.minusDays(1)
|
||||
return StickRun(verdicts, state, maxOf(settled, checked ?: settled), signal)
|
||||
return StickRun(verdicts, state, maxOf(settled, checked ?: settled), signal, resend)
|
||||
}
|
||||
|
||||
/** One day's verdict and the state after it; [now] is when it is judged. */
|
||||
|
||||
@ -15,8 +15,8 @@ import java.time.LocalDateTime
|
||||
* alarm") and `watchdog` (the daily net). Every run also catches up on the
|
||||
* unchecked days before today ([StickCheck.run]), so a phone off for days
|
||||
* is judged day by day when it comes back. Running twice is harmless --
|
||||
* [StickVerdict.ALREADY_PENALISED] makes a second miss on one day a no-op,
|
||||
* which matters because Android replays BOOT_COMPLETED to a force-stopped
|
||||
* [StickVerdict.ALREADY_PENALISED] makes a second miss on one day a no-op
|
||||
* for the streak (it only re-sends the same lockdown), which matters because Android replays BOOT_COMPLETED to a force-stopped
|
||||
* app on its next launch.
|
||||
*/
|
||||
object StickRunner {
|
||||
@ -50,13 +50,18 @@ object StickRunner {
|
||||
if (run.state != before) StickStore.save(context, run.state)
|
||||
StickStore.setCheckedThrough(context, run.checkedThrough)
|
||||
// A missed day whose lockdown already ended (caught up days later)
|
||||
// still counts toward the streak, but nothing is sent for it.
|
||||
// still counts toward the streak, but nothing is sent for it. A day
|
||||
// already penalised sends its lockdown again ([StickRun.resend]).
|
||||
run.signalUntil?.let { until ->
|
||||
WorkdayStick.sendLockdown(context, until)
|
||||
EventLog.append(
|
||||
context,
|
||||
"lockdown_sent",
|
||||
mapOf("until" to until.toString(), "streak" to run.state.streak),
|
||||
mapOf(
|
||||
"until" to until.toString(),
|
||||
"streak" to run.state.streak,
|
||||
"resend" to run.resend,
|
||||
),
|
||||
)
|
||||
}
|
||||
return run.verdicts.last().verdict
|
||||
|
||||
@ -89,16 +89,6 @@ class StickCheckBypassTest {
|
||||
assertEquals(StickState(), sick.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aSecondBypassTheSameDayIsAlreadyPenalised() {
|
||||
val afterSunday = StickState(streak = 1, lockedThrough = mon, lastMiss = sun)
|
||||
val run = StickCheck.run(
|
||||
inputs(sun.atTime(8, 0), bypass = setOf(sun), state = afterSunday, checkedThrough = sun),
|
||||
)
|
||||
assertEquals(StickVerdict.ALREADY_PENALISED, run.verdicts.single().verdict)
|
||||
assertNull(run.signalUntil)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aBypassCountsOnlyOnARingingDayWithARingOrSessionLive() {
|
||||
assertTrue(BypassRules.counts(ringingDay = true, ringLive = true, sessionLive = false))
|
||||
|
||||
@ -0,0 +1,87 @@
|
||||
package com.kuhy.wake_alarm_sync
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
import java.time.LocalTime
|
||||
|
||||
/**
|
||||
* A day already penalised re-sends the lockdown it bought (2026-11-22, C6):
|
||||
* the one signal went out while focus-owner was neither Device Owner nor
|
||||
* running, and every later bypass that day got `already_penalised` and sent
|
||||
* nothing, so the lockdown never landed.
|
||||
*/
|
||||
class StickResendTest {
|
||||
// 2026-11-22 is a Sunday, 24 a Tuesday.
|
||||
private val sun = LocalDate.of(2026, 11, 22)
|
||||
private val mon = sun.plusDays(1)
|
||||
private val tue = sun.plusDays(2)
|
||||
|
||||
private fun inputs(
|
||||
now: LocalDateTime,
|
||||
state: StickState,
|
||||
escaped: Set<LocalDate> = emptySet(),
|
||||
) = StickInputs(
|
||||
now = now,
|
||||
alarm = LocalTime.of(7, 30),
|
||||
dismissedOn = { false },
|
||||
escapedOn = { it in escaped },
|
||||
isFree = { false },
|
||||
isSick = { false },
|
||||
state = state,
|
||||
checkedThrough = now.toLocalDate(),
|
||||
bypassOn = { it == sun },
|
||||
)
|
||||
|
||||
@Test
|
||||
fun aSecondBypassTheSameDayResendsTheSameLockdown() {
|
||||
val afterSunday = StickState(streak = 1, lockedThrough = mon, lastMiss = sun)
|
||||
val run = StickCheck.run(inputs(sun.atTime(8, 0), afterSunday))
|
||||
assertEquals(StickVerdict.ALREADY_PENALISED, run.verdicts.single().verdict)
|
||||
assertEquals(mon, run.signalUntil)
|
||||
assertTrue(run.resend)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aResendNeverExtendsTheStreakOrTheDate() {
|
||||
val stacked = StickState(streak = 3, lockedThrough = sun.plusDays(3), lastMiss = sun)
|
||||
val run = StickCheck.run(inputs(sun.atTime(9, 0), stacked))
|
||||
assertEquals(stacked, run.state)
|
||||
assertEquals(sun.plusDays(3), run.signalUntil)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aLockdownThatAlreadyEndedIsNotResent() {
|
||||
val ended = StickState(streak = 1, lockedThrough = sun.minusDays(1), lastMiss = sun)
|
||||
val run = StickCheck.run(inputs(sun.atTime(8, 0), ended))
|
||||
assertEquals(StickVerdict.ALREADY_PENALISED, run.verdicts.single().verdict)
|
||||
assertNull(run.signalUntil)
|
||||
assertFalse(run.resend)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun noStoredDateMeansNothingToResend() {
|
||||
val undated = StickState(streak = 1, lockedThrough = null, lastMiss = sun)
|
||||
assertNull(StickCheck.run(inputs(sun.atTime(8, 0), undated)).signalUntil)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun anEscapedDayIsNotResent() {
|
||||
val afterSunday = StickState(streak = 1, lockedThrough = mon, lastMiss = sun)
|
||||
val run = StickCheck.run(inputs(sun.atTime(8, 0), afterSunday, escaped = setOf(sun)))
|
||||
assertEquals(StickVerdict.ESCAPED, run.verdicts.single().verdict)
|
||||
assertNull(run.signalUntil)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aFreshMissIsSentButNotMarkedAResend() {
|
||||
val run = StickCheck.run(inputs(tue.atTime(9, 0), StickState()))
|
||||
assertEquals(StickVerdict.MISS, run.verdicts.single().verdict)
|
||||
assertEquals(tue.plusDays(1), run.signalUntil)
|
||||
assertFalse(run.resend)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user