Modern Apps git

← Modern-Apps.git

screentime: fix wind-down overnight tail day-bit check

Commit
b138f20de5999c2e34b99968969c20b081d53dd5
Author
Vayun Mathur <[email protected]>
When
2026-11-08 02:19 UTC (28 days ago)
Parents
05b6fca
Fix WindDownSchedule.activeAt for overnight windows.

Root cause: the overnight branch returned false whenever the current day's bit was unset, so the morning tail was keyed off the wrong day. A schedule such as Mon 22:00-06:00 therefore stopped at midnight and did not cover Tue 00:00-06:00 unless Tuesday was also selected.

Change the predicate so the evening half belongs to the current day's bit and the morning tail belongs to yesterday's bit, matching parentalcontrols TimeWindow.contains. Add WindDownScheduleTest coverage for overnight evenings, tails, boundaries, same-day windows, and disabled schedules.

User-visible effect: wind-down now stays active through the overnight morning tail on the correct days.

Verified with: ~/code/ma-cov/gslot ./gradlew :screentime:testDevUnitTest; BUILD SUCCESSFUL.

Diff

screentime/src/main/java/com/vayunmathur/screentime/platform/WindowScheduler.kt
diff --git a/screentime/src/main/java/com/vayunmathur/screentime/platform/WindowScheduler.kt b/screentime/src/main/java/com/vayunmathur/screentime/platform/WindowScheduler.ktindex a41a927459..ee09133bc2 100644--- a/screentime/src/main/java/com/vayunmathur/screentime/platform/WindowScheduler.kt+++ b/screentime/src/main/java/com/vayunmathur/screentime/platform/WindowScheduler.kt@@ -85,11 +85,13 @@ fun WindDownSchedule.activeAt(at: LocalDateTime): Boolean {     val minute = at.hour * MINUTES_PER_HOUR + at.minute     val day = at.dayOfWeek.value - 1     val set = (daysMask shr day) and 1 == 1-    if (!set) return false     return if (startMinute <= endMinute) {-        minute >= startMinute && minute < endMinute+        set && minute >= startMinute && minute < endMinute     } else {-        minute >= startMinute || (minute < endMinute && isYesterdaySet(day))+        // Overnight: the evening half belongs to today's bit, the morning tail to+        // yesterday's - so "Mon 22:00-06:00" still applies at 02:00 on Tuesday.+        // Same predicate as parental controls' TimeWindow.contains.+        (set && minute >= startMinute) || (minute < endMinute && isYesterdaySet(day))     } } 
screentime/src/test/java/com/vayunmathur/screentime/platform/WindDownScheduleTest.kt
diff --git a/screentime/src/test/java/com/vayunmathur/screentime/platform/WindDownScheduleTest.kt b/screentime/src/test/java/com/vayunmathur/screentime/platform/WindDownScheduleTest.ktnew file mode 100644index 0000000000..2181826705--- /dev/null+++ b/screentime/src/test/java/com/vayunmathur/screentime/platform/WindDownScheduleTest.kt@@ -0,0 +1,74 @@+package com.vayunmathur.screentime.platform++import com.vayunmathur.screentime.data.WindDownSchedule+import java.time.LocalDateTime+import kotlin.test.Test+import kotlin.test.assertFalse+import kotlin.test.assertTrue++class WindDownScheduleTest {++    // 2026-11-09 is a Monday.+    private fun at(day: Int, month: Int, hour: Int, minute: Int = 0): LocalDateTime =+        LocalDateTime.of(2026, month, day, hour, minute)++    private fun overnight(daysMask: Int) = WindDownSchedule(+        enabled = true,+        startMinute = 22 * 60,+        endMinute = 6 * 60,+        daysMask = daysMask,+    )++    @Test+    fun overnightEveningBelongsToTodaysBit() {+        val schedule = overnight(daysMask = 1 shl 0)+        assertTrue(schedule.activeAt(at(day = 9, month = 11, hour = 23)))+    }++    @Test+    fun overnightTailBelongsToYesterdaysBit() {+        // "Mon 22:00-06:00" must still apply at 02:00 on Tuesday.+        val schedule = overnight(daysMask = 1 shl 0)+        assertTrue(schedule.activeAt(at(day = 10, month = 11, hour = 2)))+    }++    @Test+    fun overnightTailEndsAtEndMinute() {+        val schedule = overnight(daysMask = 1 shl 0)+        assertFalse(schedule.activeAt(at(day = 10, month = 11, hour = 7)))+    }++    @Test+    fun overnightTailAbsentWhenYesterdayUnset() {+        // Only Tuesday set: Monday 02:00 is not covered because Sunday is unset.+        val schedule = overnight(daysMask = 1 shl 1)+        assertFalse(schedule.activeAt(at(day = 9, month = 11, hour = 2)))+        assertTrue(schedule.activeAt(at(day = 10, month = 11, hour = 23)))+    }++    @Test+    fun middayOutsideWindowIsInactive() {+        val schedule = overnight(daysMask = WindDownSchedule.ALL_DAYS)+        assertFalse(schedule.activeAt(at(day = 9, month = 11, hour = 12)))+    }++    @Test+    fun sameDayWindowUsesTodaysBitOnly() {+        val schedule = WindDownSchedule(+            enabled = true,+            startMinute = 9 * 60,+            endMinute = 17 * 60,+            daysMask = 1 shl 0,+        )+        assertTrue(schedule.activeAt(at(day = 9, month = 11, hour = 12)))+        assertFalse(schedule.activeAt(at(day = 9, month = 11, hour = 8)))+        assertFalse(schedule.activeAt(at(day = 10, month = 11, hour = 12)))+    }++    @Test+    fun disabledScheduleIsNeverActive() {+        val schedule = overnight(daysMask = WindDownSchedule.ALL_DAYS).copy(enabled = false)+        assertFalse(schedule.activeAt(at(day = 9, month = 11, hour = 23)))+        assertFalse(schedule.activeAt(at(day = 10, month = 11, hour = 2)))+    }+}