parentalcontrols: removing a cap unblocks the app immediately
- Commit
79f266f6f6bf868974add4f4f7ddce889b241d9a- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 21:24 UTC (5 days ago)
- Parents
15891ae
Once an app tripped its daily cap, the spent mark in LimitState was treated as authoritative until midnight — even after the parent removed the cap or deleted the rule. The observer was unregistered correctly, but decideEnforcement blocked any package in reachedPackages without checking that a cap still existed; only a bonus grant could free the app. AppLimit now requires an actual dailyLimitMinutes on the rule: a spent mark governs only while its cap exists. Other restrictions on the same row (bedtime) still apply, and a still-capped spent app stays blocked. New regression test covers rule-deleted / cap-removed / cap-kept; a precedence test that had encoded the buggy behaviour was corrected. Time windows, bonus accounting, PIN gate and the master switch were audited and cleared. Verified: :parentalcontrols:testDevUnitTest 96/96 pass; lint error is pre-existing on untouched DailyLimitScreen.kt.
Diff
parentalcontrols/src/main/java/com/vayunmathur/parentalcontrols/domain/Enforcement.kt
diff --git a/parentalcontrols/src/main/java/com/vayunmathur/parentalcontrols/domain/Enforcement.kt b/parentalcontrols/src/main/java/com/vayunmathur/parentalcontrols/domain/Enforcement.ktindex c3bd2fa561..85da71160a 100644--- a/parentalcontrols/src/main/java/com/vayunmathur/parentalcontrols/domain/Enforcement.kt+++ b/parentalcontrols/src/main/java/com/vayunmathur/parentalcontrols/domain/Enforcement.kt@@ -26,7 +26,12 @@ data class EnforcementInput( val downtimeActive: Boolean = false, val schoolTimeActive: Boolean = false, val deviceOverBudget: Boolean = false,- /** Packages whose per-app daily budget has already been observed as spent today. */+ /**+ * Packages whose per-app daily budget has already been observed as spent today. A mark+ * governs only while the package still carries a cap: removing the cap (or deleting the+ * rule) retires the mark with it, so a stale mark left in the per-day scratch state can+ * never keep an uncapped app blocked until midnight.+ */ val reachedPackages: Set<String> = emptySet(), /** * Packages that must never be blocked (see [ExemptApps.kt]): the home/launcher and@@ -59,7 +64,8 @@ fun decideEnforcement(input: EnforcementInput): Map<String, EnforcementReason> { input.downtimeActive && !allowedInWindow -> EnforcementReason.Downtime input.schoolTimeActive && !allowedInWindow -> EnforcementReason.SchoolTime input.deviceOverBudget -> EnforcementReason.DailyLimit- packageName in input.reachedPackages -> EnforcementReason.AppLimit+ packageName in input.reachedPackages && rule?.dailyLimitMinutes != null ->+ EnforcementReason.AppLimit else -> null } if (reason != null) result[packageName] = reason
parentalcontrols/src/test/java/com/vayunmathur/parentalcontrols/domain/EnforcementTest.kt
diff --git a/parentalcontrols/src/test/java/com/vayunmathur/parentalcontrols/domain/EnforcementTest.kt b/parentalcontrols/src/test/java/com/vayunmathur/parentalcontrols/domain/EnforcementTest.ktindex db1dddce75..7c242f4d35 100644--- a/parentalcontrols/src/test/java/com/vayunmathur/parentalcontrols/domain/EnforcementTest.kt+++ b/parentalcontrols/src/test/java/com/vayunmathur/parentalcontrols/domain/EnforcementTest.kt@@ -62,7 +62,7 @@ class EnforcementTest { fun precedenceIsBedtimeThenDowntimeThenSchoolThenDeviceThenApp() { val rules = listOf( AppRestriction("com.example.game", blockedAtBedtime = true),- AppRestriction("com.example.reader", allowedInDowntime = true),+ AppRestriction("com.example.reader", dailyLimitMinutes = 30, allowedInDowntime = true), ) val all = EnforcementInput( launchablePackages = launchable,@@ -85,11 +85,53 @@ class EnforcementTest { schoolTimeActive = false, deviceOverBudget = false, ), )- assertEquals(EnforcementReason.AppLimit, noWindows["com.example.video"]) assertEquals(EnforcementReason.AppLimit, noWindows["com.example.reader"])+ // video carries a spent mark but no cap (its rule is gone): the mark governs nothing.+ assertNull(noWindows["com.example.video"]) assertNull(noWindows["com.example.game"]) } + @Test+ fun spentMarkWithoutACapBlocksNothing() {+ // Regression: the observer trips at 30 min, LimitState marks the app spent, and the+ // parent then removes the cap. AppLimits.sync unregisters the observer for the removed+ // cap, but the mark stays in the per-day scratch state until midnight — it must not+ // keep the now-uncapped app blocked for the rest of the day.+ val reached = setOf("com.example.game")+ // Rule row deleted entirely (the editor deletes a row with no restriction left).+ assertTrue(+ decideEnforcement(+ EnforcementInput(launchable, restrictions = emptyList(), reachedPackages = reached),+ ).isEmpty(),+ )+ // Rule row kept for another restriction, cap removed: that restriction still applies,+ // the retired cap does not.+ val bedtimeOnly = listOf(+ AppRestriction("com.example.game", dailyLimitMinutes = null, blockedAtBedtime = true),+ )+ assertTrue(+ decideEnforcement(+ EnforcementInput(launchable, bedtimeOnly, reachedPackages = reached),+ ).isEmpty(),+ )+ assertEquals(+ EnforcementReason.Bedtime,+ decideEnforcement(+ EnforcementInput(+ launchable, bedtimeOnly, bedtimeActive = true, reachedPackages = reached,+ ),+ )["com.example.game"],+ )+ // And a package that still carries its cap stays blocked by its spent mark.+ val capped = listOf(AppRestriction("com.example.game", dailyLimitMinutes = 30))+ assertEquals(+ EnforcementReason.AppLimit,+ decideEnforcement(+ EnforcementInput(launchable, capped, reachedPackages = reached),+ )["com.example.game"],+ )+ }+ @Test fun restrictedAppOutsideLaunchableListStaysGoverned() { val rules = listOf(AppRestriction("com.example.hidden", blockedAtBedtime = true))