mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-27 10:29:04 +01:00
Compare commits
4 Commits
82d0eb91f1
...
34783a5022
| Author | SHA1 | Date | |
|---|---|---|---|
| 34783a5022 | |||
| 2ce86a5176 | |||
| 3c2c2baba0 | |||
| d9982e53dd |
78
docs/app-split-recipes.md
Normal file
78
docs/app-split-recipes.md
Normal file
@ -0,0 +1,78 @@
|
||||
# App split recipes (Dart, Kotlin, TypeScript)
|
||||
|
||||
Settled while clearing `focus_owner`, `kcd2_dice_solver` and
|
||||
`reverse_survivors`. Referenced from `refactor_claude_todo_resume.md`.
|
||||
|
||||
## Baselines — measure before editing, re-check after
|
||||
|
||||
| Project | Command | Expect |
|
||||
| ---------------------- | ------------------- | -------------- |
|
||||
| `focus_owner` (Dart) | `flutter test` | 94 |
|
||||
| `focus_owner` (Kotlin) | see below | 40, 0 failures |
|
||||
| `kcd2_dice_solver` | `npm test -- --run` | 288 |
|
||||
| `reverse_survivors` | `npm test -- --run` | 160 |
|
||||
|
||||
Gradle prints `UP-TO-DATE` rather than a count, so read the JUnit XML, and
|
||||
**pin the JDK** — the default JDK 26 breaks the AGP jlink transform:
|
||||
|
||||
```bash
|
||||
cd focus_owner/android
|
||||
JAVA_HOME=/usr/lib/jvm/java-21-openjdk ./gradlew testDebugUnitTest --offline
|
||||
grep -ho 'tests="[0-9]*"' ../build/app/test-results/testDebugUnitTest/*.xml \
|
||||
| grep -oP '\d+' | paste -sd+ | bc
|
||||
```
|
||||
|
||||
## Dart: privates are library-scoped, so use `part`
|
||||
|
||||
A separate file is a separate **library**, so `_Card` and friends are invisible
|
||||
to it. Splitting `main.dart` with `import` + `export` produced 30 errors of the
|
||||
form "The name `_Card` isn't a class". `part 'x.dart';` + `part of 'main.dart';`
|
||||
keeps both files in one library and the analyzer goes straight to zero issues.
|
||||
|
||||
The part file carries **no imports of its own** — it inherits the library's.
|
||||
|
||||
**A `part` cannot rescue a State class.** `status_page_state.dart` stayed over
|
||||
the cap: its methods call `setState` and read seven private fields, and a
|
||||
`mixin on State<...>` cannot see them without declaring the state twice.
|
||||
|
||||
**Splitting a widget test off its `testWidgets` group loses the binding.**
|
||||
Four tests failed with "Binding has not yet been initialized" because the
|
||||
group that had been initialising it implicitly moved away. Add
|
||||
`TestWidgetsFlutterBinding.ensureInitialized()` to **both** halves.
|
||||
|
||||
## Kotlin: no `part`, so use an internal fixture object
|
||||
|
||||
Shared `private val`/`private fun` test fixtures go into an
|
||||
`internal object XFixtures` in the same package, and each test class delegates
|
||||
to it. Two compile failures came from missing one (`installed`) — list every
|
||||
`private` member of the original class before cutting.
|
||||
|
||||
`EnforcementRunner.kt` is **not splittable** as it stands: its location block
|
||||
reads the private constructor property `context` and `prefs()` eleven times,
|
||||
which no extension function can reach.
|
||||
|
||||
## TypeScript: `tsc` does not tell you a split worked
|
||||
|
||||
Three separate projects had a seam that compiled clean and failed at runtime:
|
||||
|
||||
- an import cycle leaves a constant `undefined` at module-init
|
||||
(`HEADSTART_POINTS`, twice, in `kcd2`);
|
||||
- a moved function is simply not there (`survivorStep`, 63 failures);
|
||||
- a type name that collides with a DOM global resolves to the DOM one — the
|
||||
error talks about `anchorNode` rather than saying the name is missing.
|
||||
|
||||
**Always run the suite.** Prune the imports the split leaves behind by
|
||||
iterating on `tsc`'s own TS6133/TS6192/TS6196 output rather than guessing, but
|
||||
note it cannot see a helper another file still needs.
|
||||
|
||||
## Coverage: new files must land inside the include glob
|
||||
|
||||
`kcd2` and `reverse_survivors` both gate at 100%. New source modules are picked
|
||||
up automatically; **shared test fixtures must go in `src/test/`**, which the
|
||||
coverage `exclude` names by path. Beside the source they count as production
|
||||
code and break the gate — the same trap recorded for `poker-stakes`.
|
||||
|
||||
## A split that empties a file must delete it
|
||||
|
||||
`App.test.tsx` lost its last `describe` and vitest failed the whole run with
|
||||
"No test suite found in file". Remove the shell.
|
||||
@ -0,0 +1,18 @@
|
||||
{
|
||||
"title": "Split the focus_owner Dart files under the 250-line cap",
|
||||
"objective": "Bring main.dart (538), policy.dart (272), enforcement_status_card.dart (268), widget_test.dart (351) and policy_test.dart (253) under the cap, using Dart's part/part of where private declarations cross the seam.",
|
||||
"acceptance_criteria": [
|
||||
"flutter test reports exactly 94 passing tests, the same as before the split.",
|
||||
"flutter analyze reports no issues at all: no errors, no warnings, no infos.",
|
||||
"Library-private declarations that cross a seam use part/part of rather than import. Dart privates are library-scoped, so a plain import cannot see them and the analyzer rejects it outright.",
|
||||
"No test is deleted, skipped or weakened; whole groups move verbatim.",
|
||||
"A split that separates a group from the testWidgets() calls that implicitly initialised the binding must add TestWidgetsFlutterBinding.ensureInitialized() explicitly to BOTH halves.",
|
||||
"focus_owner/analysis_options.yaml carries a pre-existing uncommitted change made by someone else and must NOT be staged."
|
||||
],
|
||||
"out_of_scope": [
|
||||
"The Kotlin sources under android/, which are a separate commit.",
|
||||
"Changing what any test asserts.",
|
||||
"Committing the unrelated analysis_options.yaml modification."
|
||||
],
|
||||
"verifier": "flutter test (expects 94 passed), flutter analyze (expects 'No issues found!'), and check_file_length.sh over focus_owner."
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
{
|
||||
"intent": "Bring the focus_owner Dart files under the cap, learning that Dart's library-scoped privates force part/part of rather than a plain import.",
|
||||
"scope": [
|
||||
"lib/main.dart (538 -> 130) + status_body.dart, status_page_state.dart",
|
||||
"lib/policy.dart (272 -> 85) + focus_policy.dart",
|
||||
"lib/enforcement_status_card.dart (268 -> 145) + status_card_parts.dart",
|
||||
"test/widget_test.dart (351 -> 128) + status_page_test.dart",
|
||||
"test/policy_test.dart (253 -> 110) + policy_decisions_test.dart",
|
||||
"Non-goal: no assertion changed; the unrelated analysis_options.yaml edit left unstaged"
|
||||
],
|
||||
"changes": [
|
||||
"Every lib split uses part/part of, not import. The first attempt used import plus export and the analyzer produced 30 errors like \"The name '_Card' isn't a class\": Dart privates are visible to the whole LIBRARY, and a separate file is a separate library unless it is a part. part/part of keeps them in one library and the analyzer went straight to zero issues.",
|
||||
"The test splits separated groups from the testWidgets() calls that had been initialising the binding implicitly, so four DevicePolicy tests failed with 'Binding has not yet been initialized'. Both halves now call TestWidgetsFlutterBinding.ensureInitialized() explicitly, with a comment saying why.",
|
||||
"An earlier automated import-pruning pass broke three tests, so it was reverted wholesale and the seven unused imports and two orphaned helpers were removed from the analyzer's exact list instead."
|
||||
],
|
||||
"verification": [
|
||||
{
|
||||
"command": "flutter test",
|
||||
"result": "fail three times, then pass",
|
||||
"evidence": "94 passed, matching the baseline measured before any edit. The three failures were all real: the private-scope errors, the missing binding initialisation, and the over-eager automated pruning."
|
||||
},
|
||||
{
|
||||
"command": "flutter analyze",
|
||||
"result": "pass",
|
||||
"evidence": "'No issues found!' - no errors, warnings or infos, and no analyzer suppression added."
|
||||
},
|
||||
{
|
||||
"command": "git diff --cached focus_owner/analysis_options.yaml, then git restore --staged",
|
||||
"result": "pass",
|
||||
"evidence": "The file had an uncommitted analyzer-exclude block from an earlier session's work, last committed at f636d2b. It was unstaged and left in the working tree rather than swept into this commit."
|
||||
},
|
||||
{
|
||||
"command": "bash ~/utils/scripts/check_file_length.sh --all | grep focus_owner",
|
||||
"result": "partial",
|
||||
"evidence": "Both test files and three of the four lib files are now under the cap. status_page_state.dart is 307 and status_page_test.dart is 309 - still over, and left for a follow-up rather than forced."
|
||||
}
|
||||
],
|
||||
"risks": [
|
||||
"part/part of makes the split files members of one library, so they share a namespace: two parts cannot declare the same private name. That is invisible until it collides.",
|
||||
"status_page_state.dart and status_page_test.dart remain over the cap. The Kotlin sources under android/ are untouched and are the bigger remaining group."
|
||||
],
|
||||
"rollback": [
|
||||
"git revert the commit; the five originals return and the six new files disappear.",
|
||||
"After rollback, re-run flutter test and expect 94 passed."
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
{
|
||||
"intent": "Split the largest focus_owner Kotlin test file under the cap, and record why the Kotlin sources and the second test file cannot follow.",
|
||||
"scope": [
|
||||
"android/app/src/test/.../EnforcementDecisionTest.kt (418 -> 168) + EnforcementDecisionCurfewTest.kt, EnforcementFixtures.kt",
|
||||
"Not split: EnforcementRunner.kt (564), DevicePolicyBridge.kt (415), FocusPolicy.kt (277), EnforcementDecision.kt (274), EnforcementLogTest.kt (251)",
|
||||
"Non-goal: no assertion changed"
|
||||
],
|
||||
"changes": [
|
||||
"The shared policy builders, the home coordinates and the installed-package set moved to an internal EnforcementFixtures object, and both test classes delegate to it. Kotlin has no part/part of, so an object in the same package is the equivalent - a plain second class could not see the originals' private members.",
|
||||
"EnforcementRunner.kt was examined and left alone: its location block reads the private constructor property `context` and the private prefs() six and five times respectively, so extension functions cannot reach them.",
|
||||
"EnforcementLogTest.kt is one line over the cap and was reverted after a split attempt: it defines helpers (`candidate`, `window`) between its test methods rather than in a fixture block, so any cut leaves unresolved references. One line is not worth restructuring a test file for."
|
||||
],
|
||||
"verification": [
|
||||
{
|
||||
"command": "JAVA_HOME=/usr/lib/jvm/java-21-openjdk ./gradlew testDebugUnitTest --offline",
|
||||
"result": "fail twice, then pass",
|
||||
"evidence": "BUILD SUCCESSFUL. Two compile failures first, both real: 'Unresolved reference installed' when a fixture was missed, then the same for the log test's inline helpers."
|
||||
},
|
||||
{
|
||||
"command": "sum the tests= and failures= attributes across build/app/test-results/testDebugUnitTest/*.xml",
|
||||
"result": "pass",
|
||||
"evidence": "40 tests, 0 failures, now across 3 XML result files instead of 2 - matching the baseline of 40 measured before any edit. Gradle reports UP-TO-DATE rather than a count, so the XML is the only way to see it."
|
||||
},
|
||||
{
|
||||
"command": "bash ~/utils/scripts/check_file_length.sh --all | grep '\\.kt:'",
|
||||
"result": "partial",
|
||||
"evidence": "EnforcementDecisionTest.kt is gone from the list. Five Kotlin files remain, four of them sources with the shared-state problem and one a test that is one line over."
|
||||
}
|
||||
],
|
||||
"risks": [
|
||||
"JAVA_HOME must be pinned to JDK 21 for this build; the default JDK 26 breaks the AGP jlink transform. Any future session running these tests needs the same prefix.",
|
||||
"Four Kotlin sources stay over the cap. Splitting EnforcementRunner means passing context and the prefs accessor explicitly, which changes its constructor contract."
|
||||
],
|
||||
"rollback": [
|
||||
"git revert the commit; EnforcementDecisionTest.kt returns to 418 lines and the two new files disappear.",
|
||||
"After rollback, re-run the gradle unit tests and expect 40 tests, 0 failures."
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
{
|
||||
"intent": "Bring the last over-cap focus_owner Dart test file under the 250-line cap, and record why status_page_state.dart cannot follow.",
|
||||
"scope": [
|
||||
"test/status_page_test.dart (309 -> 175) + status_page_actions_test.dart",
|
||||
"Not split: lib/status_page_state.dart (307)",
|
||||
"Non-goal: no assertion changed"
|
||||
],
|
||||
"changes": [
|
||||
"The release, enforcement and polling widget tests moved to status_page_actions_test.dart, taking the _logLine helper with them since nothing in the original half still used it.",
|
||||
"status_page_state.dart was attempted and reverted. It is one State class whose methods call setState and read seven private fields; extracting them as a mixin gives 'Undefined name _busy' because a mixin cannot see the concrete class's fields, and making it work would mean declaring those fields twice."
|
||||
],
|
||||
"verification": [
|
||||
{
|
||||
"command": "flutter test",
|
||||
"result": "pass",
|
||||
"evidence": "94 passed, matching the baseline. An intermediate run showed 97, which was the tell that a git checkout had restored the original file while the split copy still held the same three tests - caught by comparing the count rather than trusting a green run."
|
||||
},
|
||||
{
|
||||
"command": "grep -c 'testWidgets(' on both halves",
|
||||
"result": "pass",
|
||||
"evidence": "5 and 3, summing to the 8 the original had. Counting the cases directly is what confirmed the duplicates were gone."
|
||||
},
|
||||
{
|
||||
"command": "flutter analyze",
|
||||
"result": "pass",
|
||||
"evidence": "'No issues found!' after removing the now-unused _logLine and a dart:convert import."
|
||||
}
|
||||
],
|
||||
"risks": [
|
||||
"status_page_state.dart stays at 307 lines. Splitting it needs the State's fields moved into the mixin as well, which changes where the widget's state actually lives - a design change, not a line-count split.",
|
||||
"An automated unused-declaration pass removed _logLine while it was still in use and broke two tests; it was reverted and redone by hand. Analyzer 'unused' output is per-file and cannot see a sibling test file."
|
||||
],
|
||||
"rollback": [
|
||||
"git revert the commit; status_page_test.dart returns to 309 lines and the new file disappears.",
|
||||
"After rollback, re-run flutter test and expect 94 passed."
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,223 @@
|
||||
// Curfew and always-blocked decision cases.
|
||||
//
|
||||
// Split out of EnforcementDecisionTest.kt to keep each file under the cap.
|
||||
|
||||
package com.kuhy.focus_owner
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class EnforcementDecisionCurfewTest {
|
||||
|
||||
private val homeLat = EnforcementFixtures.homeLat
|
||||
private val homeLon = EnforcementFixtures.homeLon
|
||||
private val metresPerDegreeLat = EnforcementFixtures.metresPerDegreeLat
|
||||
|
||||
private fun policy(withCoordinates: Boolean = true, curfew: String? = """{"start":"23:00","end":"05:00"}""") =
|
||||
EnforcementFixtures.policy(withCoordinates, curfew)
|
||||
|
||||
private fun policyWithAlwaysBlocked() = EnforcementFixtures.policyWithAlwaysBlocked()
|
||||
|
||||
private val installed = EnforcementFixtures.installed
|
||||
|
||||
private fun latOffset(metres: Double) = EnforcementFixtures.latOffset(metres)
|
||||
|
||||
|
||||
@Test
|
||||
fun `workout releases youtube and nothing else`() {
|
||||
val decision = EnforcementDecision.evaluate(
|
||||
policy(),
|
||||
EnforcementInputs(
|
||||
installed,
|
||||
23 * 60 + 30,
|
||||
homeLat,
|
||||
homeLon,
|
||||
workoutActive = true,
|
||||
),
|
||||
)
|
||||
assertEquals(EnforcementReason.WORKOUT, decision.reason)
|
||||
assertTrue("com.google.android.youtube" in decision.packagesToShow)
|
||||
assertTrue("com.discord" in decision.packagesToHide)
|
||||
assertTrue("com.evil" in decision.packagesToHide)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hide and show partition the installed set`() {
|
||||
// packagesToShow is the complete allowed set, never a delta, so a
|
||||
// missed run self-repairs on the next one.
|
||||
val decision = EnforcementDecision.evaluate(
|
||||
policy(),
|
||||
EnforcementInputs(installed, 12 * 60, homeLat, homeLon),
|
||||
)
|
||||
assertEquals(installed, decision.packagesToHide + decision.packagesToShow)
|
||||
assertTrue(decision.packagesToHide.intersect(decision.packagesToShow).isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an unknown schema version is rejected`() {
|
||||
val thrown = try {
|
||||
FocusPolicy.parse("""{"schema_version": 999, "home": {}}""")
|
||||
false
|
||||
} catch (_: PolicyFormatException) {
|
||||
true
|
||||
}
|
||||
assertTrue("expected PolicyFormatException", thrown)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `prefix protection matches whole labels only`() {
|
||||
val p = policy()
|
||||
assertTrue(p.isProtected("com.android.settings"))
|
||||
assertTrue(p.isProtected("com.android.settings.sub"))
|
||||
assertFalse(p.isProtected("com.android.settingsomething"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `distance and threshold are reported so the phone can explain itself`() {
|
||||
val decision = EnforcementDecision.evaluate(
|
||||
policy(),
|
||||
EnforcementInputs(installed, 12 * 60, latOffset(5000.0), homeLon),
|
||||
)
|
||||
// ~5 km away, and the value it was actually compared against.
|
||||
assertEquals(5000.0, decision.distanceM!!, 50.0)
|
||||
assertEquals(150.0, decision.thresholdM!!, 0.001)
|
||||
assertEquals(false, decision.insideFence)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hysteresis widens the threshold only for a phone already inside`() {
|
||||
// 160 m out: outside the bare 150 m radius, inside 150 + 30. This is
|
||||
// what stops a phone parked on the boundary from flapping.
|
||||
val outside = EnforcementDecision.evaluate(
|
||||
policy(),
|
||||
EnforcementInputs(
|
||||
installed,
|
||||
12 * 60,
|
||||
latOffset(160.0),
|
||||
homeLon,
|
||||
currentlyEnforcing = false,
|
||||
),
|
||||
)
|
||||
assertEquals(150.0, outside.thresholdM!!, 0.001)
|
||||
assertEquals(EnforcementReason.AWAY, outside.reason)
|
||||
|
||||
val inside = EnforcementDecision.evaluate(
|
||||
policy(),
|
||||
EnforcementInputs(
|
||||
installed,
|
||||
12 * 60,
|
||||
latOffset(160.0),
|
||||
homeLon,
|
||||
currentlyEnforcing = true,
|
||||
),
|
||||
)
|
||||
assertEquals(180.0, inside.thresholdM!!, 0.001)
|
||||
assertEquals(EnforcementReason.AT_HOME, inside.reason)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an unanswerable fence reports null rather than outside`() {
|
||||
// No fix at all. Recording "outside" here would drop hysteresis on the
|
||||
// next pass at exactly the moment the input is least reliable.
|
||||
val decision = EnforcementDecision.evaluate(
|
||||
policy(),
|
||||
EnforcementInputs(installed, 12 * 60, latitude = null, longitude = null),
|
||||
)
|
||||
assertEquals(EnforcementReason.LOCATION_UNKNOWN, decision.reason)
|
||||
assertEquals(null, decision.insideFence)
|
||||
assertEquals(null, decision.distanceM)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `no home coordinates reports an unknown distance, not zero`() {
|
||||
// Fabricating 0 m would read on screen as "you are exactly at home".
|
||||
val decision = EnforcementDecision.evaluate(
|
||||
policy(withCoordinates = false),
|
||||
EnforcementInputs(installed, 12 * 60, homeLat, homeLon),
|
||||
)
|
||||
assertEquals(null, decision.distanceM)
|
||||
assertEquals(true, decision.insideFence)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `every hidden package carries a reason`() {
|
||||
val decision = EnforcementDecision.evaluate(
|
||||
policy(),
|
||||
EnforcementInputs(installed, 12 * 60, homeLat, homeLon),
|
||||
)
|
||||
// The UI lists these per package, so a gap would render as a blank
|
||||
// explanation next to a hidden app.
|
||||
assertEquals(decision.packagesToHide, decision.hideReasons.keys)
|
||||
assertTrue(
|
||||
decision.hideReasons.values.all { it == HideReason.NOT_IN_ALLOWLIST },
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `curfew hiding is attributed to the night list`() {
|
||||
val decision = EnforcementDecision.evaluate(
|
||||
policy(),
|
||||
EnforcementInputs(installed, 23 * 60 + 30, homeLat, homeLon),
|
||||
)
|
||||
assertEquals(EnforcementReason.CURFEW, decision.reason)
|
||||
// com.discord is day-allowed but not night-allowed, so the distinction
|
||||
// matters: "not in allowlist" would be misleading.
|
||||
assertEquals(
|
||||
HideReason.NOT_IN_NIGHT_ALLOWLIST,
|
||||
decision.hideReasons["com.discord"],
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `always-blocked is attributed as such even when away`() {
|
||||
val decision = EnforcementDecision.evaluate(
|
||||
policyWithAlwaysBlocked(),
|
||||
EnforcementInputs(installed, 12 * 60, latOffset(5000.0), homeLon),
|
||||
)
|
||||
assertEquals(EnforcementReason.AWAY, decision.reason)
|
||||
assertEquals(
|
||||
HideReason.ALWAYS_BLOCKED,
|
||||
decision.hideReasons["com.google.android.youtube"],
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `allowed prefixes cover a family of packages`() {
|
||||
val p = FocusPolicy.parse(
|
||||
"""
|
||||
{
|
||||
"schema_version": 1,
|
||||
"home": {
|
||||
"latitude": $homeLat, "longitude": $homeLon,
|
||||
"radius_m": 150.0, "hysteresis_m": 30.0
|
||||
},
|
||||
"curfew": {"start":"23:00","end":"05:00"},
|
||||
"launcher_package": "com.launcher",
|
||||
"allowed_packages": ["com.launcher"],
|
||||
"night_allowed_packages": ["com.launcher"],
|
||||
"never_disable_prefixes": [],
|
||||
"workout_unblock_domains": [],
|
||||
"allowed_prefixes": ["eu.kanade.tachiyomi"],
|
||||
"night_allowed_prefixes": ["eu.kanade.tachiyomi"],
|
||||
"browser_packages": []
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
assertTrue(p.isAllowed("eu.kanade.tachiyomi.sy", false))
|
||||
assertTrue(p.isAllowed("eu.kanade.tachiyomi.extension.all.mangadex", false))
|
||||
// Chosen 2026-08-14: manga stays available during the curfew.
|
||||
assertTrue(p.isAllowed("eu.kanade.tachiyomi.sy", true))
|
||||
// Whole labels only.
|
||||
assertFalse(p.isAllowed("eu.kanade.tachiyomisomething", false))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an asset without prefix keys still parses`() {
|
||||
// Back-compat: a policy rendered before prefixes existed must degrade
|
||||
// to exact matching rather than failing to parse at all, since an
|
||||
// unparsable policy skips the pass entirely.
|
||||
assertTrue(policy().allowedPrefixes.isEmpty())
|
||||
assertTrue(policy().nightAllowedPrefixes.isEmpty())
|
||||
}
|
||||
}
|
||||
@ -16,70 +16,18 @@ import org.junit.Test
|
||||
*/
|
||||
class EnforcementDecisionTest {
|
||||
|
||||
private val homeLat = 52.2297
|
||||
private val homeLon = 21.0122
|
||||
private val metresPerDegreeLat = 111_320.0
|
||||
private val homeLat = EnforcementFixtures.homeLat
|
||||
private val homeLon = EnforcementFixtures.homeLon
|
||||
private val metresPerDegreeLat = EnforcementFixtures.metresPerDegreeLat
|
||||
|
||||
private fun policy(withCoordinates: Boolean = true, curfew: String? = """{"start":"23:00","end":"05:00"}""") =
|
||||
FocusPolicy.parse(
|
||||
"""
|
||||
{
|
||||
"schema_version": 1,
|
||||
"home": {
|
||||
"latitude": ${if (withCoordinates) homeLat else "null"},
|
||||
"longitude": ${if (withCoordinates) homeLon else "null"},
|
||||
"radius_m": 150.0,
|
||||
"hysteresis_m": 30.0
|
||||
},
|
||||
"curfew": ${curfew ?: "null"},
|
||||
"launcher_package": "com.launcher",
|
||||
"allowed_packages": ["com.launcher","pl.mbank","com.discord","com.google.android.youtube"],
|
||||
"night_allowed_packages": ["com.launcher","pl.mbank"],
|
||||
"never_disable_prefixes": ["com.android.settings"],
|
||||
"workout_unblock_domains": ["youtube.com","googlevideo.com"],
|
||||
"browser_packages": []
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
EnforcementFixtures.policy(withCoordinates, curfew)
|
||||
|
||||
/**
|
||||
* A policy that names YouTube as always-blocked and drops it from the
|
||||
* allowlist, since the exporter guarantees those two never overlap.
|
||||
*/
|
||||
private fun policyWithAlwaysBlocked() =
|
||||
FocusPolicy.parse(
|
||||
"""
|
||||
{
|
||||
"schema_version": 1,
|
||||
"home": {
|
||||
"latitude": $homeLat,
|
||||
"longitude": $homeLon,
|
||||
"radius_m": 150.0,
|
||||
"hysteresis_m": 30.0
|
||||
},
|
||||
"curfew": {"start":"23:00","end":"05:00"},
|
||||
"launcher_package": "com.launcher",
|
||||
"allowed_packages": ["com.launcher","pl.mbank","com.discord"],
|
||||
"night_allowed_packages": ["com.launcher","pl.mbank"],
|
||||
"never_disable_prefixes": ["com.android.settings"],
|
||||
"workout_unblock_domains": ["youtube.com"],
|
||||
"browser_packages": [],
|
||||
"blockable_system_packages": ["com.google.android.youtube"],
|
||||
"always_blocked_packages": ["com.google.android.youtube"]
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
private fun policyWithAlwaysBlocked() = EnforcementFixtures.policyWithAlwaysBlocked()
|
||||
|
||||
private val installed = setOf(
|
||||
"com.launcher",
|
||||
"pl.mbank",
|
||||
"com.discord",
|
||||
"com.google.android.youtube",
|
||||
"com.evil",
|
||||
"com.android.settings",
|
||||
)
|
||||
private val installed = EnforcementFixtures.installed
|
||||
|
||||
private fun latOffset(metres: Double) = homeLat + metres / metresPerDegreeLat
|
||||
private fun latOffset(metres: Double) = EnforcementFixtures.latOffset(metres)
|
||||
|
||||
@Test
|
||||
fun `away from home restores everything except the always-blocked set`() {
|
||||
@ -218,201 +166,4 @@ class EnforcementDecisionTest {
|
||||
)
|
||||
assertTrue("com.evil" in decision.packagesToHide)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `workout releases youtube and nothing else`() {
|
||||
val decision = EnforcementDecision.evaluate(
|
||||
policy(),
|
||||
EnforcementInputs(
|
||||
installed,
|
||||
23 * 60 + 30,
|
||||
homeLat,
|
||||
homeLon,
|
||||
workoutActive = true,
|
||||
),
|
||||
)
|
||||
assertEquals(EnforcementReason.WORKOUT, decision.reason)
|
||||
assertTrue("com.google.android.youtube" in decision.packagesToShow)
|
||||
assertTrue("com.discord" in decision.packagesToHide)
|
||||
assertTrue("com.evil" in decision.packagesToHide)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hide and show partition the installed set`() {
|
||||
// packagesToShow is the complete allowed set, never a delta, so a
|
||||
// missed run self-repairs on the next one.
|
||||
val decision = EnforcementDecision.evaluate(
|
||||
policy(),
|
||||
EnforcementInputs(installed, 12 * 60, homeLat, homeLon),
|
||||
)
|
||||
assertEquals(installed, decision.packagesToHide + decision.packagesToShow)
|
||||
assertTrue(decision.packagesToHide.intersect(decision.packagesToShow).isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an unknown schema version is rejected`() {
|
||||
val thrown = try {
|
||||
FocusPolicy.parse("""{"schema_version": 999, "home": {}}""")
|
||||
false
|
||||
} catch (_: PolicyFormatException) {
|
||||
true
|
||||
}
|
||||
assertTrue("expected PolicyFormatException", thrown)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `prefix protection matches whole labels only`() {
|
||||
val p = policy()
|
||||
assertTrue(p.isProtected("com.android.settings"))
|
||||
assertTrue(p.isProtected("com.android.settings.sub"))
|
||||
assertFalse(p.isProtected("com.android.settingsomething"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `distance and threshold are reported so the phone can explain itself`() {
|
||||
val decision = EnforcementDecision.evaluate(
|
||||
policy(),
|
||||
EnforcementInputs(installed, 12 * 60, latOffset(5000.0), homeLon),
|
||||
)
|
||||
// ~5 km away, and the value it was actually compared against.
|
||||
assertEquals(5000.0, decision.distanceM!!, 50.0)
|
||||
assertEquals(150.0, decision.thresholdM!!, 0.001)
|
||||
assertEquals(false, decision.insideFence)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hysteresis widens the threshold only for a phone already inside`() {
|
||||
// 160 m out: outside the bare 150 m radius, inside 150 + 30. This is
|
||||
// what stops a phone parked on the boundary from flapping.
|
||||
val outside = EnforcementDecision.evaluate(
|
||||
policy(),
|
||||
EnforcementInputs(
|
||||
installed,
|
||||
12 * 60,
|
||||
latOffset(160.0),
|
||||
homeLon,
|
||||
currentlyEnforcing = false,
|
||||
),
|
||||
)
|
||||
assertEquals(150.0, outside.thresholdM!!, 0.001)
|
||||
assertEquals(EnforcementReason.AWAY, outside.reason)
|
||||
|
||||
val inside = EnforcementDecision.evaluate(
|
||||
policy(),
|
||||
EnforcementInputs(
|
||||
installed,
|
||||
12 * 60,
|
||||
latOffset(160.0),
|
||||
homeLon,
|
||||
currentlyEnforcing = true,
|
||||
),
|
||||
)
|
||||
assertEquals(180.0, inside.thresholdM!!, 0.001)
|
||||
assertEquals(EnforcementReason.AT_HOME, inside.reason)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an unanswerable fence reports null rather than outside`() {
|
||||
// No fix at all. Recording "outside" here would drop hysteresis on the
|
||||
// next pass at exactly the moment the input is least reliable.
|
||||
val decision = EnforcementDecision.evaluate(
|
||||
policy(),
|
||||
EnforcementInputs(installed, 12 * 60, latitude = null, longitude = null),
|
||||
)
|
||||
assertEquals(EnforcementReason.LOCATION_UNKNOWN, decision.reason)
|
||||
assertEquals(null, decision.insideFence)
|
||||
assertEquals(null, decision.distanceM)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `no home coordinates reports an unknown distance, not zero`() {
|
||||
// Fabricating 0 m would read on screen as "you are exactly at home".
|
||||
val decision = EnforcementDecision.evaluate(
|
||||
policy(withCoordinates = false),
|
||||
EnforcementInputs(installed, 12 * 60, homeLat, homeLon),
|
||||
)
|
||||
assertEquals(null, decision.distanceM)
|
||||
assertEquals(true, decision.insideFence)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `every hidden package carries a reason`() {
|
||||
val decision = EnforcementDecision.evaluate(
|
||||
policy(),
|
||||
EnforcementInputs(installed, 12 * 60, homeLat, homeLon),
|
||||
)
|
||||
// The UI lists these per package, so a gap would render as a blank
|
||||
// explanation next to a hidden app.
|
||||
assertEquals(decision.packagesToHide, decision.hideReasons.keys)
|
||||
assertTrue(
|
||||
decision.hideReasons.values.all { it == HideReason.NOT_IN_ALLOWLIST },
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `curfew hiding is attributed to the night list`() {
|
||||
val decision = EnforcementDecision.evaluate(
|
||||
policy(),
|
||||
EnforcementInputs(installed, 23 * 60 + 30, homeLat, homeLon),
|
||||
)
|
||||
assertEquals(EnforcementReason.CURFEW, decision.reason)
|
||||
// com.discord is day-allowed but not night-allowed, so the distinction
|
||||
// matters: "not in allowlist" would be misleading.
|
||||
assertEquals(
|
||||
HideReason.NOT_IN_NIGHT_ALLOWLIST,
|
||||
decision.hideReasons["com.discord"],
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `always-blocked is attributed as such even when away`() {
|
||||
val decision = EnforcementDecision.evaluate(
|
||||
policyWithAlwaysBlocked(),
|
||||
EnforcementInputs(installed, 12 * 60, latOffset(5000.0), homeLon),
|
||||
)
|
||||
assertEquals(EnforcementReason.AWAY, decision.reason)
|
||||
assertEquals(
|
||||
HideReason.ALWAYS_BLOCKED,
|
||||
decision.hideReasons["com.google.android.youtube"],
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `allowed prefixes cover a family of packages`() {
|
||||
val p = FocusPolicy.parse(
|
||||
"""
|
||||
{
|
||||
"schema_version": 1,
|
||||
"home": {
|
||||
"latitude": $homeLat, "longitude": $homeLon,
|
||||
"radius_m": 150.0, "hysteresis_m": 30.0
|
||||
},
|
||||
"curfew": {"start":"23:00","end":"05:00"},
|
||||
"launcher_package": "com.launcher",
|
||||
"allowed_packages": ["com.launcher"],
|
||||
"night_allowed_packages": ["com.launcher"],
|
||||
"never_disable_prefixes": [],
|
||||
"workout_unblock_domains": [],
|
||||
"allowed_prefixes": ["eu.kanade.tachiyomi"],
|
||||
"night_allowed_prefixes": ["eu.kanade.tachiyomi"],
|
||||
"browser_packages": []
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
assertTrue(p.isAllowed("eu.kanade.tachiyomi.sy", false))
|
||||
assertTrue(p.isAllowed("eu.kanade.tachiyomi.extension.all.mangadex", false))
|
||||
// Chosen 2026-08-14: manga stays available during the curfew.
|
||||
assertTrue(p.isAllowed("eu.kanade.tachiyomi.sy", true))
|
||||
// Whole labels only.
|
||||
assertFalse(p.isAllowed("eu.kanade.tachiyomisomething", false))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an asset without prefix keys still parses`() {
|
||||
// Back-compat: a policy rendered before prefixes existed must degrade
|
||||
// to exact matching rather than failing to parse at all, since an
|
||||
// unparsable policy skips the pass entirely.
|
||||
assertTrue(policy().allowedPrefixes.isEmpty())
|
||||
assertTrue(policy().nightAllowedPrefixes.isEmpty())
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,77 @@
|
||||
// Shared fixtures for the enforcement decision tests.
|
||||
//
|
||||
// Split out of EnforcementDecisionTest.kt to keep each file under the
|
||||
// 250-line cap; both test classes build policies from these.
|
||||
|
||||
package com.kuhy.focus_owner
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
internal object EnforcementFixtures {
|
||||
val homeLat = 52.2297
|
||||
val homeLon = 21.0122
|
||||
val metresPerDegreeLat = 111_320.0
|
||||
|
||||
fun policy(withCoordinates: Boolean = true, curfew: String? = """{"start":"23:00","end":"05:00"}""") =
|
||||
FocusPolicy.parse(
|
||||
"""
|
||||
{
|
||||
"schema_version": 1,
|
||||
"home": {
|
||||
"latitude": ${if (withCoordinates) homeLat else "null"},
|
||||
"longitude": ${if (withCoordinates) homeLon else "null"},
|
||||
"radius_m": 150.0,
|
||||
"hysteresis_m": 30.0
|
||||
},
|
||||
"curfew": ${curfew ?: "null"},
|
||||
"launcher_package": "com.launcher",
|
||||
"allowed_packages": ["com.launcher","pl.mbank","com.discord","com.google.android.youtube"],
|
||||
"night_allowed_packages": ["com.launcher","pl.mbank"],
|
||||
"never_disable_prefixes": ["com.android.settings"],
|
||||
"workout_unblock_domains": ["youtube.com","googlevideo.com"],
|
||||
"browser_packages": []
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
|
||||
/**
|
||||
* A policy that names YouTube as always-blocked and drops it from the
|
||||
* allowlist, since the exporter guarantees those two never overlap.
|
||||
*/
|
||||
fun policyWithAlwaysBlocked() =
|
||||
FocusPolicy.parse(
|
||||
"""
|
||||
{
|
||||
"schema_version": 1,
|
||||
"home": {
|
||||
"latitude": $homeLat,
|
||||
"longitude": $homeLon,
|
||||
"radius_m": 150.0,
|
||||
"hysteresis_m": 30.0
|
||||
},
|
||||
"curfew": {"start":"23:00","end":"05:00"},
|
||||
"launcher_package": "com.launcher",
|
||||
"allowed_packages": ["com.launcher","pl.mbank","com.discord"],
|
||||
"night_allowed_packages": ["com.launcher","pl.mbank"],
|
||||
"never_disable_prefixes": ["com.android.settings"],
|
||||
"workout_unblock_domains": ["youtube.com"],
|
||||
"browser_packages": [],
|
||||
"blockable_system_packages": ["com.google.android.youtube"],
|
||||
"always_blocked_packages": ["com.google.android.youtube"]
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
|
||||
val installed = setOf(
|
||||
"com.launcher",
|
||||
"pl.mbank",
|
||||
"com.discord",
|
||||
"com.google.android.youtube",
|
||||
"com.evil",
|
||||
"com.android.settings",
|
||||
)
|
||||
|
||||
fun latOffset(metres: Double) = homeLat + metres / metresPerDegreeLat
|
||||
}
|
||||
@ -3,6 +3,8 @@ import 'package:flutter/material.dart';
|
||||
import 'package:focus_owner/enforcement_record.dart';
|
||||
import 'package:focus_owner/theme.dart';
|
||||
|
||||
part 'status_card_parts.dart';
|
||||
|
||||
/// Renders the most recent enforcement pass, and why it decided what it did.
|
||||
///
|
||||
/// Exists because the three states were indistinguishable from the phone:
|
||||
@ -144,125 +146,3 @@ class EnforcementStatusCard extends StatelessWidget {
|
||||
return '${delta.inDays} d ago';
|
||||
}
|
||||
}
|
||||
|
||||
/// The hidden apps, each with the rule that hid it.
|
||||
class _HiddenList extends StatelessWidget {
|
||||
const _HiddenList({required this.hidden});
|
||||
|
||||
final List<HiddenPackage> hidden;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Theme(
|
||||
data: Theme.of(context).copyWith(dividerColor: Colors.transparent),
|
||||
child: ExpansionTile(
|
||||
tilePadding: EdgeInsets.zero,
|
||||
title: Text(
|
||||
'Why these ${hidden.length} apps are hidden',
|
||||
style: const TextStyle(color: kAccent, fontSize: 14),
|
||||
),
|
||||
children: [
|
||||
for (final entry in hidden)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 6),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
entry.package,
|
||||
style: const TextStyle(color: kText, fontSize: 12),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: kGap / 2),
|
||||
Text(
|
||||
entry.reason.explanation,
|
||||
style: TextStyle(
|
||||
color: entry.reason == HideReason.alwaysBlocked
|
||||
? kDanger
|
||||
: kMuted,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _Card extends StatelessWidget {
|
||||
const _Card({required this.child});
|
||||
|
||||
final Widget child;
|
||||
|
||||
// A Material rather than a plain decorated box: the hidden-apps
|
||||
// ExpansionTile paints its ink on the nearest Material ancestor, and a
|
||||
// DecoratedBox in between swallows the ripple.
|
||||
@override
|
||||
Widget build(BuildContext context) => Material(
|
||||
color: kSurface,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(kGap),
|
||||
child: child,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class _Field extends StatelessWidget {
|
||||
const _Field({required this.label, required this.value, this.danger = false});
|
||||
|
||||
final String label;
|
||||
final String value;
|
||||
final bool danger;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Padding(
|
||||
padding: const EdgeInsets.only(bottom: 6),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 150,
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(color: kMuted, fontSize: 13),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Text(
|
||||
value,
|
||||
style: TextStyle(
|
||||
color: danger ? kDanger : kText,
|
||||
fontSize: 13,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class _Note extends StatelessWidget {
|
||||
const _Note({required this.text, this.danger = false});
|
||||
|
||||
final String text;
|
||||
final bool danger;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Padding(
|
||||
padding: const EdgeInsets.only(top: 4, bottom: 6),
|
||||
child: Text(
|
||||
text,
|
||||
style: TextStyle(
|
||||
color: danger ? kDanger : kWarn,
|
||||
fontSize: 12,
|
||||
height: 1.3,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
197
focus_owner/lib/focus_policy.dart
Normal file
197
focus_owner/lib/focus_policy.dart
Normal file
@ -0,0 +1,197 @@
|
||||
/// The FocusPolicy aggregate and its JSON parsing.
|
||||
///
|
||||
/// Split out of policy.dart to keep it under the 250-line cap. A `part`
|
||||
/// rather than a separate library: Dart privates are library-scoped, and
|
||||
/// this code uses policy.dart's _requireNum and _parseHhMm.
|
||||
|
||||
part of 'policy.dart';
|
||||
|
||||
|
||||
/// The device-independent focus policy, as generated from `config.sh`.
|
||||
class FocusPolicy {
|
||||
const FocusPolicy({
|
||||
required this.home,
|
||||
required this.allowedPackages,
|
||||
required this.nightAllowedPackages,
|
||||
required this.neverDisablePrefixes,
|
||||
required this.workoutUnblockDomains,
|
||||
required this.curfew,
|
||||
required this.launcherPackage,
|
||||
this.allowedPrefixes = const {},
|
||||
this.nightAllowedPrefixes = const {},
|
||||
});
|
||||
|
||||
/// Parses a rendered policy document.
|
||||
factory FocusPolicy.fromJson(Map<String, Object?> json) {
|
||||
final version = json['schema_version'];
|
||||
if (version != kSupportedSchemaVersion) {
|
||||
throw PolicyFormatException(
|
||||
'unsupported schema_version $version '
|
||||
'(this build understands $kSupportedSchemaVersion)',
|
||||
);
|
||||
}
|
||||
final home = json['home'];
|
||||
if (home is! Map<String, Object?>) {
|
||||
throw const PolicyFormatException('missing "home" object');
|
||||
}
|
||||
final curfew = json['curfew'];
|
||||
return FocusPolicy(
|
||||
home: HomeLocation.fromJson(home),
|
||||
allowedPackages: _stringSet(json['allowed_packages'], 'allowed_packages'),
|
||||
nightAllowedPackages:
|
||||
_stringSet(json['night_allowed_packages'], 'night_allowed_packages'),
|
||||
neverDisablePrefixes: _stringSet(
|
||||
json['never_disable_prefixes'],
|
||||
'never_disable_prefixes',
|
||||
),
|
||||
workoutUnblockDomains: _stringSet(
|
||||
json['workout_unblock_domains'],
|
||||
'workout_unblock_domains',
|
||||
),
|
||||
curfew: curfew == null
|
||||
? null
|
||||
: CurfewWindow.fromJson(curfew as Map<String, Object?>),
|
||||
launcherPackage: json['launcher_package'] as String?,
|
||||
// Optional: absent from an asset rendered before prefixes existed, and
|
||||
// reading a missing key as empty restores exactly the old exact-match
|
||||
// behaviour rather than failing to parse the whole policy.
|
||||
allowedPrefixes: _optionalStringSet(
|
||||
json,
|
||||
'allowed_prefixes',
|
||||
),
|
||||
nightAllowedPrefixes: _optionalStringSet(
|
||||
json,
|
||||
'night_allowed_prefixes',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Loads the policy bundled with the app.
|
||||
static Future<FocusPolicy> load({
|
||||
AssetBundle? bundle,
|
||||
String assetKey = 'assets/policy.json',
|
||||
}) async {
|
||||
final raw = await (bundle ?? rootBundle).loadString(assetKey);
|
||||
final decoded = jsonDecode(raw);
|
||||
if (decoded is! Map<String, Object?>) {
|
||||
throw const PolicyFormatException('policy asset is not a JSON object');
|
||||
}
|
||||
return FocusPolicy.fromJson(decoded);
|
||||
}
|
||||
|
||||
final HomeLocation home;
|
||||
final Set<String> allowedPackages;
|
||||
final Set<String> nightAllowedPackages;
|
||||
final Set<String> neverDisablePrefixes;
|
||||
final Set<String> workoutUnblockDomains;
|
||||
final CurfewWindow? curfew;
|
||||
final String? launcherPackage;
|
||||
|
||||
/// Prefix-matched day allowlist, for apps shipping as a package family.
|
||||
///
|
||||
/// Tachiyomi installs every source as its own apk, so an exact list goes
|
||||
/// stale the moment a new extension is installed.
|
||||
final Set<String> allowedPrefixes;
|
||||
|
||||
/// Prefixes that survive the curfew. Subset of [allowedPrefixes].
|
||||
final Set<String> nightAllowedPrefixes;
|
||||
|
||||
/// Whether [package] is covered by any entry of [prefixes].
|
||||
///
|
||||
/// Matched on whole labels, so `com.android.providers` covers
|
||||
/// `com.android.providers.telephony` but not `com.android.providersomething`.
|
||||
/// Shared by every prefix list so the boundary rule cannot drift.
|
||||
static bool _matchesPrefix(String package, Set<String> prefixes) =>
|
||||
prefixes.any(
|
||||
(prefix) => package == prefix || package.startsWith('$prefix.'),
|
||||
);
|
||||
|
||||
/// Whether a package must never be disabled.
|
||||
bool isProtected(String package) =>
|
||||
_matchesPrefix(package, neverDisablePrefixes);
|
||||
|
||||
/// Whether a package may run under the given conditions.
|
||||
bool isAllowed(String package, {bool duringCurfew = false}) {
|
||||
if (isProtected(package)) return true;
|
||||
// Hiding the launcher leaves no home screen, so it outranks the lists --
|
||||
// matching FocusPolicy.kt, which has always had this check.
|
||||
if (package == launcherPackage) return true;
|
||||
final allowed = duringCurfew ? nightAllowedPackages : allowedPackages;
|
||||
if (allowed.contains(package)) return true;
|
||||
final prefixes = duringCurfew ? nightAllowedPrefixes : allowedPrefixes;
|
||||
return _matchesPrefix(package, prefixes);
|
||||
}
|
||||
|
||||
/// Whether the curfew is in force at [minutesSinceMidnight].
|
||||
bool isCurfewActive(int minutesSinceMidnight) =>
|
||||
curfew?.contains(minutesSinceMidnight) ?? false;
|
||||
|
||||
/// Packages released while a workout is in progress.
|
||||
///
|
||||
/// The rooted system expressed this exception as *domains*, because it
|
||||
/// enforced through a hosts file. Hiding apps is a coarser instrument: there
|
||||
/// is no way to unblock youtube.com without unhiding the YouTube app, so the
|
||||
/// exception is expressed as packages here.
|
||||
///
|
||||
/// Derived from [workoutUnblockDomains] rather than configured separately,
|
||||
/// so that the shell config stays the single source of truth for *intent*
|
||||
/// even though the mechanism differs.
|
||||
Set<String> get workoutExemptPackages {
|
||||
const domainToPackages = <String, List<String>>{
|
||||
'youtube.com': [
|
||||
'com.google.android.youtube',
|
||||
'com.google.android.apps.youtube.music',
|
||||
],
|
||||
};
|
||||
final packages = <String>{};
|
||||
for (final entry in domainToPackages.entries) {
|
||||
final covered = workoutUnblockDomains.any(
|
||||
(domain) => domain == entry.key || domain.endsWith('.${entry.key}'),
|
||||
);
|
||||
if (covered) packages.addAll(entry.value);
|
||||
}
|
||||
return packages;
|
||||
}
|
||||
}
|
||||
|
||||
Set<String> _stringSet(Object? value, String field) {
|
||||
if (value is! List) {
|
||||
throw PolicyFormatException('"$field" must be a list');
|
||||
}
|
||||
return value.map((e) => e! as String).toSet();
|
||||
}
|
||||
|
||||
/// A list that may be absent, read as empty.
|
||||
///
|
||||
/// Used for fields added after assets were already shipped. The strict
|
||||
/// [_stringSet] would throw, and an unparsable policy is worse than a missing
|
||||
/// field: it disables the feature entirely rather than degrading to the
|
||||
/// previous behaviour. A present-but-wrong type is still an error, since that
|
||||
/// is a real mistake. Mirrors `optionalStringSet` in `FocusPolicy.kt`.
|
||||
Set<String> _optionalStringSet(Map<String, Object?> json, String field) {
|
||||
if (!json.containsKey(field)) return const {};
|
||||
return _stringSet(json[field], field);
|
||||
}
|
||||
|
||||
double _requireNum(Object? value, String field) {
|
||||
if (value is! num) {
|
||||
throw PolicyFormatException('"$field" must be a number');
|
||||
}
|
||||
return value.toDouble();
|
||||
}
|
||||
|
||||
int _parseHhMm(Object? value, String field) {
|
||||
if (value is! String) {
|
||||
throw PolicyFormatException('"$field" must be a "HH:MM" string');
|
||||
}
|
||||
final parts = value.split(':');
|
||||
if (parts.length != 2) {
|
||||
throw PolicyFormatException('"$field" must be "HH:MM", got "$value"');
|
||||
}
|
||||
final hour = int.tryParse(parts[0]);
|
||||
final minute = int.tryParse(parts[1]);
|
||||
if (hour == null || minute == null || hour > 23 || minute > 59) {
|
||||
throw PolicyFormatException('"$field" is not a valid time: "$value"');
|
||||
}
|
||||
return hour * 60 + minute;
|
||||
}
|
||||
@ -10,6 +10,10 @@ import 'package:focus_owner/enforcement_status_card.dart';
|
||||
import 'package:focus_owner/policy.dart';
|
||||
import 'package:focus_owner/theme.dart';
|
||||
|
||||
part 'status_page_state.dart';
|
||||
|
||||
part 'status_body.dart';
|
||||
|
||||
void main() => runApp(const FocusOwnerApp());
|
||||
|
||||
// The palette used to be duplicated here as `kField`…`kDanger`, byte for
|
||||
@ -62,477 +66,3 @@ class StatusPage extends StatefulWidget {
|
||||
@override
|
||||
State<StatusPage> createState() => _StatusPageState();
|
||||
}
|
||||
|
||||
class _StatusPageState extends State<StatusPage> {
|
||||
DevicePolicyStatus? _status;
|
||||
FocusPolicy? _focusPolicy;
|
||||
String? _policyError;
|
||||
String? _error;
|
||||
bool _busy = false;
|
||||
bool _hasHome = false;
|
||||
List<EnforcementRecord> _log = const [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
unawaited(_refresh());
|
||||
}
|
||||
|
||||
Future<void> _refresh() async {
|
||||
setState(() => _busy = true);
|
||||
try {
|
||||
await Future.wait([_loadDeviceStatus(), _loadPolicy(), _loadLog()]);
|
||||
} finally {
|
||||
// In a finally because _busy gates the release-device-owner button. If
|
||||
// any loader throws -- a malformed log record used to be enough -- the
|
||||
// flag would stay true for the life of the process, and since _refresh
|
||||
// runs from initState that means the escape hatch is dead on every
|
||||
// launch. Diagnostics failing must never disable the way out.
|
||||
if (mounted) setState(() => _busy = false);
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads the durable record of past passes.
|
||||
///
|
||||
/// Failing to read it must not blank the rest of the screen: the log is
|
||||
/// diagnostics, while the provisioning state above it is what the user needs
|
||||
/// to reach the escape hatch.
|
||||
Future<void> _loadLog() async {
|
||||
try {
|
||||
final lines = await widget.policy.readEnforcementLog();
|
||||
if (!mounted) return;
|
||||
setState(() => _log = EnforcementRecord.parseLines(lines));
|
||||
} on PlatformException {
|
||||
if (!mounted) return;
|
||||
setState(() => _log = const []);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadDeviceStatus() async {
|
||||
try {
|
||||
final status = await widget.policy.status();
|
||||
final hasHome = await widget.policy.hasHomeLocation();
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_status = status;
|
||||
_hasHome = hasHome;
|
||||
_error = null;
|
||||
});
|
||||
} on PlatformException catch (e) {
|
||||
if (!mounted) return;
|
||||
setState(
|
||||
() => _error = e.message ?? 'Could not read device policy state',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadPolicy() async {
|
||||
try {
|
||||
final policy = await widget.loadFocusPolicy();
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_focusPolicy = policy;
|
||||
_policyError = null;
|
||||
});
|
||||
} on PolicyFormatException catch (e) {
|
||||
// A policy that cannot be parsed must be reported, never silently
|
||||
// treated as an empty allowlist - that would block everything.
|
||||
if (!mounted) return;
|
||||
setState(() => _policyError = e.message);
|
||||
} on Exception catch (e) {
|
||||
if (!mounted) return;
|
||||
setState(() => _policyError = '$e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _runNow() async {
|
||||
setState(() => _busy = true);
|
||||
final started = await widget.policy.runEnforcementNow();
|
||||
if (started) await _awaitNewRecord();
|
||||
if (!mounted) return;
|
||||
setState(() => _busy = false);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
started ? 'Enforcement run finished' : 'Could not start enforcement',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Waits for the pass to append a record, then shows it.
|
||||
///
|
||||
/// `runEnforcementNow` only *starts* the service, and a pass now waits up to
|
||||
/// [EnforcementRunner.ACQUIRE_TIMEOUT_MS] for a location fix, so re-reading
|
||||
/// immediately returns the record from the PREVIOUS pass. That is not a
|
||||
/// cosmetic lag: after re-anchoring home the screen kept showing the old
|
||||
/// distance, which reads as "the button did nothing" at exactly the moment
|
||||
/// the user is checking whether it worked.
|
||||
///
|
||||
/// Polls rather than waits for a callback because the pass runs in a
|
||||
/// separate service process with no channel back to the UI.
|
||||
Future<void> _awaitNewRecord() async {
|
||||
final before = _log.isEmpty ? null : _log.first.timestamp;
|
||||
for (var attempt = 0; attempt < _kRefreshAttempts; attempt++) {
|
||||
await Future<void>.delayed(_kRefreshInterval);
|
||||
if (!mounted) return;
|
||||
await _loadLog();
|
||||
if (!mounted) return;
|
||||
final latest = _log.isEmpty ? null : _log.first.timestamp;
|
||||
if (latest != null && latest != before) break;
|
||||
}
|
||||
// Provisioning rows (home set / hidden counts) move with the pass too.
|
||||
await _loadDeviceStatus();
|
||||
}
|
||||
|
||||
Future<void> _setHome() async {
|
||||
setState(() => _busy = true);
|
||||
final failure = await widget.policy.setHomeToCurrentLocation();
|
||||
if (!mounted) return;
|
||||
setState(() => _busy = false);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
failure ?? 'Home set. Enforcement now applies here, not everywhere.',
|
||||
),
|
||||
),
|
||||
);
|
||||
if (failure != null) {
|
||||
await _refresh();
|
||||
return;
|
||||
}
|
||||
// Re-evaluate rather than only re-reading. The stored record still
|
||||
// describes the OLD home, so refreshing alone leaves the previous
|
||||
// distance on screen -- measured: after re-anchoring home the card still
|
||||
// read "AWAY, 766 m" until a pass ran, which looks like the button
|
||||
// failed. A new home is exactly when the geofence answer changes.
|
||||
setState(() => _busy = true);
|
||||
if (await widget.policy.runEnforcementNow()) await _awaitNewRecord();
|
||||
if (!mounted) return;
|
||||
setState(() => _busy = false);
|
||||
await _refresh();
|
||||
}
|
||||
|
||||
Future<void> _lockVpn() async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
backgroundColor: kSurface,
|
||||
title: const Text('Lock VPN configuration?'),
|
||||
content: const Text(
|
||||
'The always-on VPN can no longer be turned off from Settings. '
|
||||
'Releasing device owner lifts this.',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Lock'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (confirmed != true) return;
|
||||
|
||||
setState(() => _busy = true);
|
||||
final ok = await widget.policy.setVpnConfigBlocked(blocked: true);
|
||||
if (!mounted) return;
|
||||
setState(() => _busy = false);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
ok ? 'VPN configuration locked' : 'Could not lock - see logcat',
|
||||
),
|
||||
),
|
||||
);
|
||||
await _refresh();
|
||||
}
|
||||
|
||||
Future<void> _release() async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
backgroundColor: kSurface,
|
||||
title: const Text('Release device owner?'),
|
||||
// Deliberately concrete, and honest in both directions. "Restored only
|
||||
// by a factory reset" is true but abstract, and this dialog is read in
|
||||
// the moment the block is most inconvenient -- the price has to be
|
||||
// legible right then, not inferred. While no account exists the price
|
||||
// really is low, and overstating it there would teach the user to
|
||||
// ignore the warning that matters later.
|
||||
content: Text(
|
||||
_status?.hasAccounts ?? true
|
||||
? 'Enforcement stops. YouTube comes back.\n\n'
|
||||
'Accounts exist on this device, so device owner can NEVER '
|
||||
'be set again without a factory reset. That means '
|
||||
're-pairing mBank, Revolut and inFakt over SMS, '
|
||||
're-activating mObywatel, signing in to every account '
|
||||
'again, and losing Signal history.\n\n'
|
||||
'This is the way out if something is broken. It is a bad '
|
||||
'trade for wanting to watch a video.'
|
||||
: 'Enforcement stops and YouTube comes back.\n\n'
|
||||
'No account exists yet, so device owner can be set again '
|
||||
'straight afterwards without a wipe. This is the moment to '
|
||||
'test the release path, before signing in.',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
style: TextButton.styleFrom(foregroundColor: kDanger),
|
||||
child: const Text('Release'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (confirmed != true) return;
|
||||
|
||||
setState(() => _busy = true);
|
||||
final released = await widget.policy.releaseDeviceOwner();
|
||||
if (!mounted) return;
|
||||
setState(() => _busy = false);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
released ? 'Device owner released' : 'Release failed - see logcat',
|
||||
),
|
||||
),
|
||||
);
|
||||
await _refresh();
|
||||
}
|
||||
|
||||
void _openLog() {
|
||||
unawaited(
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute<void>(builder: (_) => DebugLogPage(records: _log)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final status = _status;
|
||||
final error = _error;
|
||||
final Widget body;
|
||||
if (error != null) {
|
||||
body = const _Message(text: 'Could not read state', color: kDanger);
|
||||
} else if (status != null) {
|
||||
body = _StatusBody(
|
||||
status: status,
|
||||
focusPolicy: _focusPolicy,
|
||||
policyError: _policyError,
|
||||
busy: _busy,
|
||||
onRelease: _release,
|
||||
onRunNow: _runNow,
|
||||
onSetHome: _setHome,
|
||||
onLockVpn: _lockVpn,
|
||||
hasHome: _hasHome,
|
||||
log: _log,
|
||||
onOpenLog: _openLog,
|
||||
);
|
||||
} else {
|
||||
body = const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: kField,
|
||||
foregroundColor: kText,
|
||||
title: const Text('Focus Owner'),
|
||||
actions: [
|
||||
IconButton(
|
||||
onPressed: _busy ? null : _refresh,
|
||||
icon: const Icon(Icons.refresh),
|
||||
tooltip: 'Refresh',
|
||||
),
|
||||
],
|
||||
),
|
||||
// Scrollable because the action list grew past a short screen: an
|
||||
// unreachable "Release device owner" button is the one control that
|
||||
// must never be clipped off the bottom.
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(kGap),
|
||||
child: body,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _StatusBody extends StatelessWidget {
|
||||
const _StatusBody({
|
||||
required this.status,
|
||||
required this.focusPolicy,
|
||||
required this.policyError,
|
||||
required this.busy,
|
||||
required this.onRelease,
|
||||
required this.onRunNow,
|
||||
required this.onSetHome,
|
||||
required this.onLockVpn,
|
||||
required this.hasHome,
|
||||
this.log = const [],
|
||||
this.onOpenLog,
|
||||
});
|
||||
|
||||
final DevicePolicyStatus status;
|
||||
final FocusPolicy? focusPolicy;
|
||||
final String? policyError;
|
||||
final bool busy;
|
||||
final Future<void> Function() onRelease;
|
||||
final Future<void> Function() onRunNow;
|
||||
final Future<void> Function() onSetHome;
|
||||
final Future<void> Function() onLockVpn;
|
||||
|
||||
/// Whether home coordinates exist; without them enforcement is permanent.
|
||||
final bool hasHome;
|
||||
|
||||
/// Recorded passes, newest first.
|
||||
final List<EnforcementRecord> log;
|
||||
final VoidCallback? onOpenLog;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final policy = focusPolicy;
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// First, and largest: what the enforcer is doing right now and why.
|
||||
// Everything below is provisioning detail that only matters once this
|
||||
// question is answered.
|
||||
EnforcementStatusCard(
|
||||
record: log.isEmpty ? null : log.first,
|
||||
onOpenLog: onOpenLog ?? () {},
|
||||
),
|
||||
const SizedBox(height: kGap),
|
||||
_Row(label: 'Package', value: status.packageName),
|
||||
_Row(label: 'Device owner', value: status.isDeviceOwner ? 'yes' : 'no'),
|
||||
_Row(label: 'Admin active', value: status.isAdminActive ? 'yes' : 'no'),
|
||||
_Row(label: 'Android SDK', value: '${status.sdkInt}'),
|
||||
// "none (inert build)" was a leftover from when this app really did
|
||||
// apply nothing. It has been enforcing for weeks, so the honest value
|
||||
// is whether the last recorded pass hid anything.
|
||||
_Row(
|
||||
label: 'Enforcing',
|
||||
value: switch (log.isEmpty ? null : log.first.hideCount) {
|
||||
null => 'no pass recorded yet',
|
||||
0 => 'nothing hidden',
|
||||
final int n => '$n apps hidden',
|
||||
},
|
||||
),
|
||||
const SizedBox(height: kGap),
|
||||
const Divider(color: kMuted),
|
||||
const SizedBox(height: kGap / 2),
|
||||
if (policyError != null)
|
||||
_Message(text: 'Policy: $policyError', color: kDanger)
|
||||
else if (policy == null)
|
||||
const _Message(text: 'Policy: loading...', color: kMuted)
|
||||
else ...[
|
||||
_Row(
|
||||
label: 'Allowed apps',
|
||||
value:
|
||||
'${policy.allowedPackages.length}'
|
||||
' (${policy.nightAllowedPackages.length} at night)',
|
||||
),
|
||||
_Row(
|
||||
label: 'Curfew',
|
||||
value: policy.curfew == null
|
||||
? 'disabled'
|
||||
: '${_hhmm(policy.curfew!.startMinutes)}'
|
||||
'-${_hhmm(policy.curfew!.endMinutes)}',
|
||||
),
|
||||
_Row(
|
||||
label: 'Home radius',
|
||||
value: policy.home.hasCoordinates
|
||||
? '${policy.home.radiusM.round()} m'
|
||||
: '${policy.home.radiusM.round()} m (coords redacted)',
|
||||
),
|
||||
_Row(
|
||||
label: 'Workout domains',
|
||||
value: '${policy.workoutUnblockDomains.length}',
|
||||
),
|
||||
// The distinction that decides whether enforcement is geofenced or
|
||||
// permanent, so it belongs on screen rather than only in logcat.
|
||||
_Row(
|
||||
label: 'Geofence',
|
||||
value: hasHome ? 'active' : 'no home set - enforcing everywhere',
|
||||
),
|
||||
],
|
||||
const SizedBox(height: kGap * 2),
|
||||
OutlinedButton(
|
||||
onPressed: busy ? null : onRunNow,
|
||||
child: const Text('Run enforcement now'),
|
||||
),
|
||||
const SizedBox(height: kGap),
|
||||
if (status.isDeviceOwner) ...[
|
||||
OutlinedButton(
|
||||
onPressed: busy ? null : onSetHome,
|
||||
child: Text(
|
||||
hasHome ? 'Update home to here' : 'Set home to current location',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: kGap),
|
||||
OutlinedButton(
|
||||
onPressed: busy ? null : onLockVpn,
|
||||
child: const Text('Lock VPN configuration'),
|
||||
),
|
||||
const SizedBox(height: kGap),
|
||||
],
|
||||
if (status.isDeviceOwner)
|
||||
FilledButton(
|
||||
onPressed: busy ? null : onRelease,
|
||||
style: FilledButton.styleFrom(backgroundColor: kDanger),
|
||||
child: const Text('Release device owner'),
|
||||
)
|
||||
else
|
||||
const _Message(
|
||||
text: 'Not provisioned as device owner. Nothing is enforced.',
|
||||
color: kMuted,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Formats minutes-since-midnight as HH:MM.
|
||||
String _hhmm(int minutes) {
|
||||
final h = (minutes ~/ 60).toString().padLeft(2, '0');
|
||||
final m = (minutes % 60).toString().padLeft(2, '0');
|
||||
return '$h:$m';
|
||||
}
|
||||
|
||||
class _Row extends StatelessWidget {
|
||||
const _Row({required this.label, required this.value});
|
||||
|
||||
final String label;
|
||||
final String value;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: kGap / 2),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(label, style: const TextStyle(color: kMuted)),
|
||||
Text(value, style: const TextStyle(color: kText)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _Message extends StatelessWidget {
|
||||
const _Message({required this.text, required this.color});
|
||||
|
||||
final String text;
|
||||
final Color color;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) =>
|
||||
Text(text, style: TextStyle(color: color));
|
||||
}
|
||||
|
||||
@ -2,6 +2,8 @@ import 'dart:convert';
|
||||
|
||||
import 'package:flutter/services.dart' show AssetBundle, rootBundle;
|
||||
|
||||
part 'focus_policy.dart';
|
||||
|
||||
/// Schema version this build understands.
|
||||
///
|
||||
/// The asset is generated by `python3 -m python_pkg.focus_policy`, which is the
|
||||
@ -81,192 +83,3 @@ class HomeLocation {
|
||||
/// as (0, 0), which would put "home" in the Atlantic.
|
||||
bool get hasCoordinates => latitude != null && longitude != null;
|
||||
}
|
||||
|
||||
/// The device-independent focus policy, as generated from `config.sh`.
|
||||
class FocusPolicy {
|
||||
const FocusPolicy({
|
||||
required this.home,
|
||||
required this.allowedPackages,
|
||||
required this.nightAllowedPackages,
|
||||
required this.neverDisablePrefixes,
|
||||
required this.workoutUnblockDomains,
|
||||
required this.curfew,
|
||||
required this.launcherPackage,
|
||||
this.allowedPrefixes = const {},
|
||||
this.nightAllowedPrefixes = const {},
|
||||
});
|
||||
|
||||
/// Parses a rendered policy document.
|
||||
factory FocusPolicy.fromJson(Map<String, Object?> json) {
|
||||
final version = json['schema_version'];
|
||||
if (version != kSupportedSchemaVersion) {
|
||||
throw PolicyFormatException(
|
||||
'unsupported schema_version $version '
|
||||
'(this build understands $kSupportedSchemaVersion)',
|
||||
);
|
||||
}
|
||||
final home = json['home'];
|
||||
if (home is! Map<String, Object?>) {
|
||||
throw const PolicyFormatException('missing "home" object');
|
||||
}
|
||||
final curfew = json['curfew'];
|
||||
return FocusPolicy(
|
||||
home: HomeLocation.fromJson(home),
|
||||
allowedPackages: _stringSet(json['allowed_packages'], 'allowed_packages'),
|
||||
nightAllowedPackages:
|
||||
_stringSet(json['night_allowed_packages'], 'night_allowed_packages'),
|
||||
neverDisablePrefixes: _stringSet(
|
||||
json['never_disable_prefixes'],
|
||||
'never_disable_prefixes',
|
||||
),
|
||||
workoutUnblockDomains: _stringSet(
|
||||
json['workout_unblock_domains'],
|
||||
'workout_unblock_domains',
|
||||
),
|
||||
curfew: curfew == null
|
||||
? null
|
||||
: CurfewWindow.fromJson(curfew as Map<String, Object?>),
|
||||
launcherPackage: json['launcher_package'] as String?,
|
||||
// Optional: absent from an asset rendered before prefixes existed, and
|
||||
// reading a missing key as empty restores exactly the old exact-match
|
||||
// behaviour rather than failing to parse the whole policy.
|
||||
allowedPrefixes: _optionalStringSet(
|
||||
json,
|
||||
'allowed_prefixes',
|
||||
),
|
||||
nightAllowedPrefixes: _optionalStringSet(
|
||||
json,
|
||||
'night_allowed_prefixes',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Loads the policy bundled with the app.
|
||||
static Future<FocusPolicy> load({
|
||||
AssetBundle? bundle,
|
||||
String assetKey = 'assets/policy.json',
|
||||
}) async {
|
||||
final raw = await (bundle ?? rootBundle).loadString(assetKey);
|
||||
final decoded = jsonDecode(raw);
|
||||
if (decoded is! Map<String, Object?>) {
|
||||
throw const PolicyFormatException('policy asset is not a JSON object');
|
||||
}
|
||||
return FocusPolicy.fromJson(decoded);
|
||||
}
|
||||
|
||||
final HomeLocation home;
|
||||
final Set<String> allowedPackages;
|
||||
final Set<String> nightAllowedPackages;
|
||||
final Set<String> neverDisablePrefixes;
|
||||
final Set<String> workoutUnblockDomains;
|
||||
final CurfewWindow? curfew;
|
||||
final String? launcherPackage;
|
||||
|
||||
/// Prefix-matched day allowlist, for apps shipping as a package family.
|
||||
///
|
||||
/// Tachiyomi installs every source as its own apk, so an exact list goes
|
||||
/// stale the moment a new extension is installed.
|
||||
final Set<String> allowedPrefixes;
|
||||
|
||||
/// Prefixes that survive the curfew. Subset of [allowedPrefixes].
|
||||
final Set<String> nightAllowedPrefixes;
|
||||
|
||||
/// Whether [package] is covered by any entry of [prefixes].
|
||||
///
|
||||
/// Matched on whole labels, so `com.android.providers` covers
|
||||
/// `com.android.providers.telephony` but not `com.android.providersomething`.
|
||||
/// Shared by every prefix list so the boundary rule cannot drift.
|
||||
static bool _matchesPrefix(String package, Set<String> prefixes) =>
|
||||
prefixes.any(
|
||||
(prefix) => package == prefix || package.startsWith('$prefix.'),
|
||||
);
|
||||
|
||||
/// Whether a package must never be disabled.
|
||||
bool isProtected(String package) =>
|
||||
_matchesPrefix(package, neverDisablePrefixes);
|
||||
|
||||
/// Whether a package may run under the given conditions.
|
||||
bool isAllowed(String package, {bool duringCurfew = false}) {
|
||||
if (isProtected(package)) return true;
|
||||
// Hiding the launcher leaves no home screen, so it outranks the lists --
|
||||
// matching FocusPolicy.kt, which has always had this check.
|
||||
if (package == launcherPackage) return true;
|
||||
final allowed = duringCurfew ? nightAllowedPackages : allowedPackages;
|
||||
if (allowed.contains(package)) return true;
|
||||
final prefixes = duringCurfew ? nightAllowedPrefixes : allowedPrefixes;
|
||||
return _matchesPrefix(package, prefixes);
|
||||
}
|
||||
|
||||
/// Whether the curfew is in force at [minutesSinceMidnight].
|
||||
bool isCurfewActive(int minutesSinceMidnight) =>
|
||||
curfew?.contains(minutesSinceMidnight) ?? false;
|
||||
|
||||
/// Packages released while a workout is in progress.
|
||||
///
|
||||
/// The rooted system expressed this exception as *domains*, because it
|
||||
/// enforced through a hosts file. Hiding apps is a coarser instrument: there
|
||||
/// is no way to unblock youtube.com without unhiding the YouTube app, so the
|
||||
/// exception is expressed as packages here.
|
||||
///
|
||||
/// Derived from [workoutUnblockDomains] rather than configured separately,
|
||||
/// so that the shell config stays the single source of truth for *intent*
|
||||
/// even though the mechanism differs.
|
||||
Set<String> get workoutExemptPackages {
|
||||
const domainToPackages = <String, List<String>>{
|
||||
'youtube.com': [
|
||||
'com.google.android.youtube',
|
||||
'com.google.android.apps.youtube.music',
|
||||
],
|
||||
};
|
||||
final packages = <String>{};
|
||||
for (final entry in domainToPackages.entries) {
|
||||
final covered = workoutUnblockDomains.any(
|
||||
(domain) => domain == entry.key || domain.endsWith('.${entry.key}'),
|
||||
);
|
||||
if (covered) packages.addAll(entry.value);
|
||||
}
|
||||
return packages;
|
||||
}
|
||||
}
|
||||
|
||||
Set<String> _stringSet(Object? value, String field) {
|
||||
if (value is! List) {
|
||||
throw PolicyFormatException('"$field" must be a list');
|
||||
}
|
||||
return value.map((e) => e! as String).toSet();
|
||||
}
|
||||
|
||||
/// A list that may be absent, read as empty.
|
||||
///
|
||||
/// Used for fields added after assets were already shipped. The strict
|
||||
/// [_stringSet] would throw, and an unparsable policy is worse than a missing
|
||||
/// field: it disables the feature entirely rather than degrading to the
|
||||
/// previous behaviour. A present-but-wrong type is still an error, since that
|
||||
/// is a real mistake. Mirrors `optionalStringSet` in `FocusPolicy.kt`.
|
||||
Set<String> _optionalStringSet(Map<String, Object?> json, String field) {
|
||||
if (!json.containsKey(field)) return const {};
|
||||
return _stringSet(json[field], field);
|
||||
}
|
||||
|
||||
double _requireNum(Object? value, String field) {
|
||||
if (value is! num) {
|
||||
throw PolicyFormatException('"$field" must be a number');
|
||||
}
|
||||
return value.toDouble();
|
||||
}
|
||||
|
||||
int _parseHhMm(Object? value, String field) {
|
||||
if (value is! String) {
|
||||
throw PolicyFormatException('"$field" must be a "HH:MM" string');
|
||||
}
|
||||
final parts = value.split(':');
|
||||
if (parts.length != 2) {
|
||||
throw PolicyFormatException('"$field" must be "HH:MM", got "$value"');
|
||||
}
|
||||
final hour = int.tryParse(parts[0]);
|
||||
final minute = int.tryParse(parts[1]);
|
||||
if (hour == null || minute == null || hour > 23 || minute > 59) {
|
||||
throw PolicyFormatException('"$field" is not a valid time: "$value"');
|
||||
}
|
||||
return hour * 60 + minute;
|
||||
}
|
||||
|
||||
179
focus_owner/lib/status_body.dart
Normal file
179
focus_owner/lib/status_body.dart
Normal file
@ -0,0 +1,179 @@
|
||||
/// The status page body and its sub-widgets.
|
||||
///
|
||||
/// Split out of main.dart to keep it under the 250-line cap. A `part`
|
||||
/// because Dart privates are library-scoped and these widgets are private.
|
||||
|
||||
part of 'main.dart';
|
||||
|
||||
class _StatusBody extends StatelessWidget {
|
||||
const _StatusBody({
|
||||
required this.status,
|
||||
required this.focusPolicy,
|
||||
required this.policyError,
|
||||
required this.busy,
|
||||
required this.onRelease,
|
||||
required this.onRunNow,
|
||||
required this.onSetHome,
|
||||
required this.onLockVpn,
|
||||
required this.hasHome,
|
||||
this.log = const [],
|
||||
this.onOpenLog,
|
||||
});
|
||||
|
||||
final DevicePolicyStatus status;
|
||||
final FocusPolicy? focusPolicy;
|
||||
final String? policyError;
|
||||
final bool busy;
|
||||
final Future<void> Function() onRelease;
|
||||
final Future<void> Function() onRunNow;
|
||||
final Future<void> Function() onSetHome;
|
||||
final Future<void> Function() onLockVpn;
|
||||
|
||||
/// Whether home coordinates exist; without them enforcement is permanent.
|
||||
final bool hasHome;
|
||||
|
||||
/// Recorded passes, newest first.
|
||||
final List<EnforcementRecord> log;
|
||||
final VoidCallback? onOpenLog;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final policy = focusPolicy;
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// First, and largest: what the enforcer is doing right now and why.
|
||||
// Everything below is provisioning detail that only matters once this
|
||||
// question is answered.
|
||||
EnforcementStatusCard(
|
||||
record: log.isEmpty ? null : log.first,
|
||||
onOpenLog: onOpenLog ?? () {},
|
||||
),
|
||||
const SizedBox(height: kGap),
|
||||
_Row(label: 'Package', value: status.packageName),
|
||||
_Row(label: 'Device owner', value: status.isDeviceOwner ? 'yes' : 'no'),
|
||||
_Row(label: 'Admin active', value: status.isAdminActive ? 'yes' : 'no'),
|
||||
_Row(label: 'Android SDK', value: '${status.sdkInt}'),
|
||||
// "none (inert build)" was a leftover from when this app really did
|
||||
// apply nothing. It has been enforcing for weeks, so the honest value
|
||||
// is whether the last recorded pass hid anything.
|
||||
_Row(
|
||||
label: 'Enforcing',
|
||||
value: switch (log.isEmpty ? null : log.first.hideCount) {
|
||||
null => 'no pass recorded yet',
|
||||
0 => 'nothing hidden',
|
||||
final int n => '$n apps hidden',
|
||||
},
|
||||
),
|
||||
const SizedBox(height: kGap),
|
||||
const Divider(color: kMuted),
|
||||
const SizedBox(height: kGap / 2),
|
||||
if (policyError != null)
|
||||
_Message(text: 'Policy: $policyError', color: kDanger)
|
||||
else if (policy == null)
|
||||
const _Message(text: 'Policy: loading...', color: kMuted)
|
||||
else ...[
|
||||
_Row(
|
||||
label: 'Allowed apps',
|
||||
value:
|
||||
'${policy.allowedPackages.length}'
|
||||
' (${policy.nightAllowedPackages.length} at night)',
|
||||
),
|
||||
_Row(
|
||||
label: 'Curfew',
|
||||
value: policy.curfew == null
|
||||
? 'disabled'
|
||||
: '${_hhmm(policy.curfew!.startMinutes)}'
|
||||
'-${_hhmm(policy.curfew!.endMinutes)}',
|
||||
),
|
||||
_Row(
|
||||
label: 'Home radius',
|
||||
value: policy.home.hasCoordinates
|
||||
? '${policy.home.radiusM.round()} m'
|
||||
: '${policy.home.radiusM.round()} m (coords redacted)',
|
||||
),
|
||||
_Row(
|
||||
label: 'Workout domains',
|
||||
value: '${policy.workoutUnblockDomains.length}',
|
||||
),
|
||||
// The distinction that decides whether enforcement is geofenced or
|
||||
// permanent, so it belongs on screen rather than only in logcat.
|
||||
_Row(
|
||||
label: 'Geofence',
|
||||
value: hasHome ? 'active' : 'no home set - enforcing everywhere',
|
||||
),
|
||||
],
|
||||
const SizedBox(height: kGap * 2),
|
||||
OutlinedButton(
|
||||
onPressed: busy ? null : onRunNow,
|
||||
child: const Text('Run enforcement now'),
|
||||
),
|
||||
const SizedBox(height: kGap),
|
||||
if (status.isDeviceOwner) ...[
|
||||
OutlinedButton(
|
||||
onPressed: busy ? null : onSetHome,
|
||||
child: Text(
|
||||
hasHome ? 'Update home to here' : 'Set home to current location',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: kGap),
|
||||
OutlinedButton(
|
||||
onPressed: busy ? null : onLockVpn,
|
||||
child: const Text('Lock VPN configuration'),
|
||||
),
|
||||
const SizedBox(height: kGap),
|
||||
],
|
||||
if (status.isDeviceOwner)
|
||||
FilledButton(
|
||||
onPressed: busy ? null : onRelease,
|
||||
style: FilledButton.styleFrom(backgroundColor: kDanger),
|
||||
child: const Text('Release device owner'),
|
||||
)
|
||||
else
|
||||
const _Message(
|
||||
text: 'Not provisioned as device owner. Nothing is enforced.',
|
||||
color: kMuted,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Formats minutes-since-midnight as HH:MM.
|
||||
String _hhmm(int minutes) {
|
||||
final h = (minutes ~/ 60).toString().padLeft(2, '0');
|
||||
final m = (minutes % 60).toString().padLeft(2, '0');
|
||||
return '$h:$m';
|
||||
}
|
||||
|
||||
class _Row extends StatelessWidget {
|
||||
const _Row({required this.label, required this.value});
|
||||
|
||||
final String label;
|
||||
final String value;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: kGap / 2),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(label, style: const TextStyle(color: kMuted)),
|
||||
Text(value, style: const TextStyle(color: kText)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _Message extends StatelessWidget {
|
||||
const _Message({required this.text, required this.color});
|
||||
|
||||
final String text;
|
||||
final Color color;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) =>
|
||||
Text(text, style: TextStyle(color: color));
|
||||
}
|
||||
128
focus_owner/lib/status_card_parts.dart
Normal file
128
focus_owner/lib/status_card_parts.dart
Normal file
@ -0,0 +1,128 @@
|
||||
/// The card's list, field and note sub-widgets.
|
||||
///
|
||||
/// Split out of enforcement_status_card.dart to keep it under the 250-line cap. A `part`
|
||||
/// because Dart privates are library-scoped and these widgets are private.
|
||||
|
||||
part of 'enforcement_status_card.dart';
|
||||
|
||||
/// The hidden apps, each with the rule that hid it.
|
||||
class _HiddenList extends StatelessWidget {
|
||||
const _HiddenList({required this.hidden});
|
||||
|
||||
final List<HiddenPackage> hidden;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Theme(
|
||||
data: Theme.of(context).copyWith(dividerColor: Colors.transparent),
|
||||
child: ExpansionTile(
|
||||
tilePadding: EdgeInsets.zero,
|
||||
title: Text(
|
||||
'Why these ${hidden.length} apps are hidden',
|
||||
style: const TextStyle(color: kAccent, fontSize: 14),
|
||||
),
|
||||
children: [
|
||||
for (final entry in hidden)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 6),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
entry.package,
|
||||
style: const TextStyle(color: kText, fontSize: 12),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: kGap / 2),
|
||||
Text(
|
||||
entry.reason.explanation,
|
||||
style: TextStyle(
|
||||
color: entry.reason == HideReason.alwaysBlocked
|
||||
? kDanger
|
||||
: kMuted,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _Card extends StatelessWidget {
|
||||
const _Card({required this.child});
|
||||
|
||||
final Widget child;
|
||||
|
||||
// A Material rather than a plain decorated box: the hidden-apps
|
||||
// ExpansionTile paints its ink on the nearest Material ancestor, and a
|
||||
// DecoratedBox in between swallows the ripple.
|
||||
@override
|
||||
Widget build(BuildContext context) => Material(
|
||||
color: kSurface,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(kGap),
|
||||
child: child,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class _Field extends StatelessWidget {
|
||||
const _Field({required this.label, required this.value, this.danger = false});
|
||||
|
||||
final String label;
|
||||
final String value;
|
||||
final bool danger;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Padding(
|
||||
padding: const EdgeInsets.only(bottom: 6),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 150,
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(color: kMuted, fontSize: 13),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Text(
|
||||
value,
|
||||
style: TextStyle(
|
||||
color: danger ? kDanger : kText,
|
||||
fontSize: 13,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class _Note extends StatelessWidget {
|
||||
const _Note({required this.text, this.danger = false});
|
||||
|
||||
final String text;
|
||||
final bool danger;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Padding(
|
||||
padding: const EdgeInsets.only(top: 4, bottom: 6),
|
||||
child: Text(
|
||||
text,
|
||||
style: TextStyle(
|
||||
color: danger ? kDanger : kWarn,
|
||||
fontSize: 12,
|
||||
height: 1.3,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
307
focus_owner/lib/status_page_state.dart
Normal file
307
focus_owner/lib/status_page_state.dart
Normal file
@ -0,0 +1,307 @@
|
||||
/// The StatusPage state: polling, refresh and the device-status loads.
|
||||
///
|
||||
/// Split out of main.dart to keep it under the 250-line cap. A `part`
|
||||
/// because the class and its fields are library-private.
|
||||
|
||||
part of 'main.dart';
|
||||
|
||||
class _StatusPageState extends State<StatusPage> {
|
||||
DevicePolicyStatus? _status;
|
||||
FocusPolicy? _focusPolicy;
|
||||
String? _policyError;
|
||||
String? _error;
|
||||
bool _busy = false;
|
||||
bool _hasHome = false;
|
||||
List<EnforcementRecord> _log = const [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
unawaited(_refresh());
|
||||
}
|
||||
|
||||
Future<void> _refresh() async {
|
||||
setState(() => _busy = true);
|
||||
try {
|
||||
await Future.wait([_loadDeviceStatus(), _loadPolicy(), _loadLog()]);
|
||||
} finally {
|
||||
// In a finally because _busy gates the release-device-owner button. If
|
||||
// any loader throws -- a malformed log record used to be enough -- the
|
||||
// flag would stay true for the life of the process, and since _refresh
|
||||
// runs from initState that means the escape hatch is dead on every
|
||||
// launch. Diagnostics failing must never disable the way out.
|
||||
if (mounted) setState(() => _busy = false);
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads the durable record of past passes.
|
||||
///
|
||||
/// Failing to read it must not blank the rest of the screen: the log is
|
||||
/// diagnostics, while the provisioning state above it is what the user needs
|
||||
/// to reach the escape hatch.
|
||||
Future<void> _loadLog() async {
|
||||
try {
|
||||
final lines = await widget.policy.readEnforcementLog();
|
||||
if (!mounted) return;
|
||||
setState(() => _log = EnforcementRecord.parseLines(lines));
|
||||
} on PlatformException {
|
||||
if (!mounted) return;
|
||||
setState(() => _log = const []);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadDeviceStatus() async {
|
||||
try {
|
||||
final status = await widget.policy.status();
|
||||
final hasHome = await widget.policy.hasHomeLocation();
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_status = status;
|
||||
_hasHome = hasHome;
|
||||
_error = null;
|
||||
});
|
||||
} on PlatformException catch (e) {
|
||||
if (!mounted) return;
|
||||
setState(
|
||||
() => _error = e.message ?? 'Could not read device policy state',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadPolicy() async {
|
||||
try {
|
||||
final policy = await widget.loadFocusPolicy();
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_focusPolicy = policy;
|
||||
_policyError = null;
|
||||
});
|
||||
} on PolicyFormatException catch (e) {
|
||||
// A policy that cannot be parsed must be reported, never silently
|
||||
// treated as an empty allowlist - that would block everything.
|
||||
if (!mounted) return;
|
||||
setState(() => _policyError = e.message);
|
||||
} on Exception catch (e) {
|
||||
if (!mounted) return;
|
||||
setState(() => _policyError = '$e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _runNow() async {
|
||||
setState(() => _busy = true);
|
||||
final started = await widget.policy.runEnforcementNow();
|
||||
if (started) await _awaitNewRecord();
|
||||
if (!mounted) return;
|
||||
setState(() => _busy = false);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
started ? 'Enforcement run finished' : 'Could not start enforcement',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Waits for the pass to append a record, then shows it.
|
||||
///
|
||||
/// `runEnforcementNow` only *starts* the service, and a pass now waits up to
|
||||
/// [EnforcementRunner.ACQUIRE_TIMEOUT_MS] for a location fix, so re-reading
|
||||
/// immediately returns the record from the PREVIOUS pass. That is not a
|
||||
/// cosmetic lag: after re-anchoring home the screen kept showing the old
|
||||
/// distance, which reads as "the button did nothing" at exactly the moment
|
||||
/// the user is checking whether it worked.
|
||||
///
|
||||
/// Polls rather than waits for a callback because the pass runs in a
|
||||
/// separate service process with no channel back to the UI.
|
||||
Future<void> _awaitNewRecord() async {
|
||||
final before = _log.isEmpty ? null : _log.first.timestamp;
|
||||
for (var attempt = 0; attempt < _kRefreshAttempts; attempt++) {
|
||||
await Future<void>.delayed(_kRefreshInterval);
|
||||
if (!mounted) return;
|
||||
await _loadLog();
|
||||
if (!mounted) return;
|
||||
final latest = _log.isEmpty ? null : _log.first.timestamp;
|
||||
if (latest != null && latest != before) break;
|
||||
}
|
||||
// Provisioning rows (home set / hidden counts) move with the pass too.
|
||||
await _loadDeviceStatus();
|
||||
}
|
||||
|
||||
Future<void> _setHome() async {
|
||||
setState(() => _busy = true);
|
||||
final failure = await widget.policy.setHomeToCurrentLocation();
|
||||
if (!mounted) return;
|
||||
setState(() => _busy = false);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
failure ?? 'Home set. Enforcement now applies here, not everywhere.',
|
||||
),
|
||||
),
|
||||
);
|
||||
if (failure != null) {
|
||||
await _refresh();
|
||||
return;
|
||||
}
|
||||
// Re-evaluate rather than only re-reading. The stored record still
|
||||
// describes the OLD home, so refreshing alone leaves the previous
|
||||
// distance on screen -- measured: after re-anchoring home the card still
|
||||
// read "AWAY, 766 m" until a pass ran, which looks like the button
|
||||
// failed. A new home is exactly when the geofence answer changes.
|
||||
setState(() => _busy = true);
|
||||
if (await widget.policy.runEnforcementNow()) await _awaitNewRecord();
|
||||
if (!mounted) return;
|
||||
setState(() => _busy = false);
|
||||
await _refresh();
|
||||
}
|
||||
|
||||
Future<void> _lockVpn() async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
backgroundColor: kSurface,
|
||||
title: const Text('Lock VPN configuration?'),
|
||||
content: const Text(
|
||||
'The always-on VPN can no longer be turned off from Settings. '
|
||||
'Releasing device owner lifts this.',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Lock'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (confirmed != true) return;
|
||||
|
||||
setState(() => _busy = true);
|
||||
final ok = await widget.policy.setVpnConfigBlocked(blocked: true);
|
||||
if (!mounted) return;
|
||||
setState(() => _busy = false);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
ok ? 'VPN configuration locked' : 'Could not lock - see logcat',
|
||||
),
|
||||
),
|
||||
);
|
||||
await _refresh();
|
||||
}
|
||||
|
||||
Future<void> _release() async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
backgroundColor: kSurface,
|
||||
title: const Text('Release device owner?'),
|
||||
// Deliberately concrete, and honest in both directions. "Restored only
|
||||
// by a factory reset" is true but abstract, and this dialog is read in
|
||||
// the moment the block is most inconvenient -- the price has to be
|
||||
// legible right then, not inferred. While no account exists the price
|
||||
// really is low, and overstating it there would teach the user to
|
||||
// ignore the warning that matters later.
|
||||
content: Text(
|
||||
_status?.hasAccounts ?? true
|
||||
? 'Enforcement stops. YouTube comes back.\n\n'
|
||||
'Accounts exist on this device, so device owner can NEVER '
|
||||
'be set again without a factory reset. That means '
|
||||
're-pairing mBank, Revolut and inFakt over SMS, '
|
||||
're-activating mObywatel, signing in to every account '
|
||||
'again, and losing Signal history.\n\n'
|
||||
'This is the way out if something is broken. It is a bad '
|
||||
'trade for wanting to watch a video.'
|
||||
: 'Enforcement stops and YouTube comes back.\n\n'
|
||||
'No account exists yet, so device owner can be set again '
|
||||
'straight afterwards without a wipe. This is the moment to '
|
||||
'test the release path, before signing in.',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
style: TextButton.styleFrom(foregroundColor: kDanger),
|
||||
child: const Text('Release'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (confirmed != true) return;
|
||||
|
||||
setState(() => _busy = true);
|
||||
final released = await widget.policy.releaseDeviceOwner();
|
||||
if (!mounted) return;
|
||||
setState(() => _busy = false);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
released ? 'Device owner released' : 'Release failed - see logcat',
|
||||
),
|
||||
),
|
||||
);
|
||||
await _refresh();
|
||||
}
|
||||
|
||||
void _openLog() {
|
||||
unawaited(
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute<void>(builder: (_) => DebugLogPage(records: _log)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final status = _status;
|
||||
final error = _error;
|
||||
final Widget body;
|
||||
if (error != null) {
|
||||
body = const _Message(text: 'Could not read state', color: kDanger);
|
||||
} else if (status != null) {
|
||||
body = _StatusBody(
|
||||
status: status,
|
||||
focusPolicy: _focusPolicy,
|
||||
policyError: _policyError,
|
||||
busy: _busy,
|
||||
onRelease: _release,
|
||||
onRunNow: _runNow,
|
||||
onSetHome: _setHome,
|
||||
onLockVpn: _lockVpn,
|
||||
hasHome: _hasHome,
|
||||
log: _log,
|
||||
onOpenLog: _openLog,
|
||||
);
|
||||
} else {
|
||||
body = const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: kField,
|
||||
foregroundColor: kText,
|
||||
title: const Text('Focus Owner'),
|
||||
actions: [
|
||||
IconButton(
|
||||
onPressed: _busy ? null : _refresh,
|
||||
icon: const Icon(Icons.refresh),
|
||||
tooltip: 'Refresh',
|
||||
),
|
||||
],
|
||||
),
|
||||
// Scrollable because the action list grew past a short screen: an
|
||||
// unreachable "Release device owner" button is the one control that
|
||||
// must never be clipped off the bottom.
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(kGap),
|
||||
child: body,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
184
focus_owner/test/policy_decisions_test.dart
Normal file
184
focus_owner/test/policy_decisions_test.dart
Normal file
@ -0,0 +1,184 @@
|
||||
// Allow/deny decision cases.
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:focus_owner/policy.dart';
|
||||
|
||||
Map<String, Object?> _doc({
|
||||
Object? schemaVersion = kSupportedSchemaVersion,
|
||||
Object? curfew = const {'start': '23:00', 'end': '05:00'},
|
||||
double? latitude,
|
||||
double? longitude,
|
||||
}) =>
|
||||
{
|
||||
'schema_version': schemaVersion,
|
||||
'home': {
|
||||
'latitude': latitude,
|
||||
'longitude': longitude,
|
||||
'radius_m': 150.0,
|
||||
'hysteresis_m': 30.0,
|
||||
},
|
||||
'curfew': curfew,
|
||||
'launcher_package': 'com.qqlabs.minimalistlauncher',
|
||||
'allowed_packages': ['com.good', 'pl.mbank'],
|
||||
'night_allowed_packages': ['pl.mbank'],
|
||||
'never_disable_prefixes': ['com.android.providers'],
|
||||
'workout_unblock_domains': ['youtube.com'],
|
||||
'browser_packages': <String>[],
|
||||
};
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('allow decisions', () {
|
||||
final policy = FocusPolicy.fromJson(_doc());
|
||||
|
||||
test('protects system prefixes on whole labels only', () {
|
||||
expect(policy.isProtected('com.android.providers'), isTrue);
|
||||
expect(policy.isProtected('com.android.providers.telephony'), isTrue);
|
||||
expect(policy.isProtected('com.android.providersomething'), isFalse);
|
||||
});
|
||||
|
||||
test('protected packages are allowed even during curfew', () {
|
||||
expect(
|
||||
policy.isAllowed('com.android.providers', duringCurfew: true),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
test('curfew narrows the allowlist', () {
|
||||
expect(policy.isAllowed('com.good'), isTrue);
|
||||
expect(policy.isAllowed('com.good', duringCurfew: true), isFalse);
|
||||
expect(policy.isAllowed('pl.mbank', duringCurfew: true), isTrue);
|
||||
});
|
||||
|
||||
test('unknown packages are blocked', () {
|
||||
expect(policy.isAllowed('com.evil'), isFalse);
|
||||
});
|
||||
|
||||
test('the launcher is allowed even when absent from the lists', () {
|
||||
// Hiding it leaves no home screen. FocusPolicy.kt has always had this
|
||||
// check; Dart had drifted without it.
|
||||
expect(policy.allowedPackages, isNot(contains(policy.launcherPackage)));
|
||||
expect(policy.isAllowed('com.qqlabs.minimalistlauncher'), isTrue);
|
||||
expect(
|
||||
policy.isAllowed('com.qqlabs.minimalistlauncher', duringCurfew: true),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
test('an asset without prefix keys parses as empty', () {
|
||||
// Back-compat: a policy rendered before prefixes existed must still
|
||||
// parse, degrading to exact matching rather than failing entirely.
|
||||
expect(policy.allowedPrefixes, isEmpty);
|
||||
expect(policy.nightAllowedPrefixes, isEmpty);
|
||||
});
|
||||
|
||||
test('allowed prefixes cover a family of packages', () {
|
||||
final prefixed = FocusPolicy.fromJson(
|
||||
_doc()
|
||||
..['allowed_prefixes'] = ['eu.kanade.tachiyomi']
|
||||
..['night_allowed_prefixes'] = ['eu.kanade.tachiyomi'],
|
||||
);
|
||||
expect(prefixed.isAllowed('eu.kanade.tachiyomi.sy'), isTrue);
|
||||
expect(
|
||||
prefixed.isAllowed('eu.kanade.tachiyomi.extension.all.mangadex'),
|
||||
isTrue,
|
||||
);
|
||||
// Whole labels only, so an unrelated package is not swept in.
|
||||
expect(prefixed.isAllowed('eu.kanade.tachiyomisomething'), isFalse);
|
||||
// Chosen 2026-08-14: manga stays available during the curfew.
|
||||
expect(
|
||||
prefixed.isAllowed('eu.kanade.tachiyomi.sy', duringCurfew: true),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
test('a day-only prefix is blocked during curfew', () {
|
||||
final dayOnly = FocusPolicy.fromJson(
|
||||
_doc()..['allowed_prefixes'] = ['eu.kanade.tachiyomi'],
|
||||
);
|
||||
expect(dayOnly.isAllowed('eu.kanade.tachiyomi.sy'), isTrue);
|
||||
expect(
|
||||
dayOnly.isAllowed('eu.kanade.tachiyomi.sy', duringCurfew: true),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
|
||||
test('a prefix list of the wrong type is still an error', () {
|
||||
// Absent is fine; present-but-wrong is a real mistake.
|
||||
expect(
|
||||
() => FocusPolicy.fromJson(_doc()..['allowed_prefixes'] = 'nope'),
|
||||
throwsA(isA<PolicyFormatException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('isCurfewActive follows the window', () {
|
||||
expect(policy.isCurfewActive(23 * 60 + 30), isTrue);
|
||||
expect(policy.isCurfewActive(12 * 60), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('FocusPolicy.load', () {
|
||||
test('reads and parses the bundled asset', () async {
|
||||
final bundle = _StubBundle(jsonEncode(_doc()));
|
||||
final policy = await FocusPolicy.load(bundle: bundle);
|
||||
expect(policy.allowedPackages, contains('pl.mbank'));
|
||||
});
|
||||
|
||||
test('rejects an asset that is not a JSON object', () async {
|
||||
final bundle = _StubBundle('[]');
|
||||
await expectLater(
|
||||
FocusPolicy.load(bundle: bundle),
|
||||
throwsA(isA<PolicyFormatException>()),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
test('the real committed asset parses and is redacted', () async {
|
||||
// Guards the generator: a policy.json that this app cannot read, or that
|
||||
// leaks the home coordinates into git, is a build error.
|
||||
final policy = await FocusPolicy.load(bundle: _RealAssetBundle());
|
||||
// Guards against an empty or truncated render, not against a specific
|
||||
// size. The threshold was 50 when the allowlist still described the rooted
|
||||
// Blackview; the 2026-08-11 rewrite to an explicit list for this phone cut
|
||||
// it to ~26, so a high bound would only pin the old policy in place.
|
||||
expect(policy.allowedPackages.length, greaterThan(10));
|
||||
// The launcher must survive enforcement or the device has no home screen.
|
||||
expect(policy.launcherPackage, isNotNull);
|
||||
expect(policy.allowedPackages, contains(policy.launcherPackage));
|
||||
expect(policy.home.hasCoordinates, isFalse,
|
||||
reason: 'committed policy.json must not carry home coordinates');
|
||||
expect(policy.curfew, isNotNull);
|
||||
});
|
||||
}
|
||||
|
||||
class _StubBundle extends CachingAssetBundle {
|
||||
_StubBundle(this.contents);
|
||||
|
||||
final String contents;
|
||||
|
||||
@override
|
||||
Future<ByteData> load(String key) async =>
|
||||
ByteData.sublistView(utf8.encode(contents));
|
||||
|
||||
@override
|
||||
Future<String> loadString(String key, {bool cache = true}) async => contents;
|
||||
}
|
||||
|
||||
/// Reads the asset straight off disk, bypassing the (empty) test rootBundle.
|
||||
class _RealAssetBundle extends CachingAssetBundle {
|
||||
@override
|
||||
Future<ByteData> load(String key) async {
|
||||
throw UnimplementedError(key);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> loadString(String key, {bool cache = true}) async {
|
||||
final file = File('assets/policy.json');
|
||||
return file.readAsString();
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,4 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:focus_owner/policy.dart';
|
||||
|
||||
@ -101,153 +98,4 @@ void main() {
|
||||
expect(day.contains(17 * 60), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('allow decisions', () {
|
||||
final policy = FocusPolicy.fromJson(_doc());
|
||||
|
||||
test('protects system prefixes on whole labels only', () {
|
||||
expect(policy.isProtected('com.android.providers'), isTrue);
|
||||
expect(policy.isProtected('com.android.providers.telephony'), isTrue);
|
||||
expect(policy.isProtected('com.android.providersomething'), isFalse);
|
||||
});
|
||||
|
||||
test('protected packages are allowed even during curfew', () {
|
||||
expect(
|
||||
policy.isAllowed('com.android.providers', duringCurfew: true),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
test('curfew narrows the allowlist', () {
|
||||
expect(policy.isAllowed('com.good'), isTrue);
|
||||
expect(policy.isAllowed('com.good', duringCurfew: true), isFalse);
|
||||
expect(policy.isAllowed('pl.mbank', duringCurfew: true), isTrue);
|
||||
});
|
||||
|
||||
test('unknown packages are blocked', () {
|
||||
expect(policy.isAllowed('com.evil'), isFalse);
|
||||
});
|
||||
|
||||
test('the launcher is allowed even when absent from the lists', () {
|
||||
// Hiding it leaves no home screen. FocusPolicy.kt has always had this
|
||||
// check; Dart had drifted without it.
|
||||
expect(policy.allowedPackages, isNot(contains(policy.launcherPackage)));
|
||||
expect(policy.isAllowed('com.qqlabs.minimalistlauncher'), isTrue);
|
||||
expect(
|
||||
policy.isAllowed('com.qqlabs.minimalistlauncher', duringCurfew: true),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
test('an asset without prefix keys parses as empty', () {
|
||||
// Back-compat: a policy rendered before prefixes existed must still
|
||||
// parse, degrading to exact matching rather than failing entirely.
|
||||
expect(policy.allowedPrefixes, isEmpty);
|
||||
expect(policy.nightAllowedPrefixes, isEmpty);
|
||||
});
|
||||
|
||||
test('allowed prefixes cover a family of packages', () {
|
||||
final prefixed = FocusPolicy.fromJson(
|
||||
_doc()
|
||||
..['allowed_prefixes'] = ['eu.kanade.tachiyomi']
|
||||
..['night_allowed_prefixes'] = ['eu.kanade.tachiyomi'],
|
||||
);
|
||||
expect(prefixed.isAllowed('eu.kanade.tachiyomi.sy'), isTrue);
|
||||
expect(
|
||||
prefixed.isAllowed('eu.kanade.tachiyomi.extension.all.mangadex'),
|
||||
isTrue,
|
||||
);
|
||||
// Whole labels only, so an unrelated package is not swept in.
|
||||
expect(prefixed.isAllowed('eu.kanade.tachiyomisomething'), isFalse);
|
||||
// Chosen 2026-08-14: manga stays available during the curfew.
|
||||
expect(
|
||||
prefixed.isAllowed('eu.kanade.tachiyomi.sy', duringCurfew: true),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
test('a day-only prefix is blocked during curfew', () {
|
||||
final dayOnly = FocusPolicy.fromJson(
|
||||
_doc()..['allowed_prefixes'] = ['eu.kanade.tachiyomi'],
|
||||
);
|
||||
expect(dayOnly.isAllowed('eu.kanade.tachiyomi.sy'), isTrue);
|
||||
expect(
|
||||
dayOnly.isAllowed('eu.kanade.tachiyomi.sy', duringCurfew: true),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
|
||||
test('a prefix list of the wrong type is still an error', () {
|
||||
// Absent is fine; present-but-wrong is a real mistake.
|
||||
expect(
|
||||
() => FocusPolicy.fromJson(_doc()..['allowed_prefixes'] = 'nope'),
|
||||
throwsA(isA<PolicyFormatException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('isCurfewActive follows the window', () {
|
||||
expect(policy.isCurfewActive(23 * 60 + 30), isTrue);
|
||||
expect(policy.isCurfewActive(12 * 60), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('FocusPolicy.load', () {
|
||||
test('reads and parses the bundled asset', () async {
|
||||
final bundle = _StubBundle(jsonEncode(_doc()));
|
||||
final policy = await FocusPolicy.load(bundle: bundle);
|
||||
expect(policy.allowedPackages, contains('pl.mbank'));
|
||||
});
|
||||
|
||||
test('rejects an asset that is not a JSON object', () async {
|
||||
final bundle = _StubBundle('[]');
|
||||
await expectLater(
|
||||
FocusPolicy.load(bundle: bundle),
|
||||
throwsA(isA<PolicyFormatException>()),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
test('the real committed asset parses and is redacted', () async {
|
||||
// Guards the generator: a policy.json that this app cannot read, or that
|
||||
// leaks the home coordinates into git, is a build error.
|
||||
final policy = await FocusPolicy.load(bundle: _RealAssetBundle());
|
||||
// Guards against an empty or truncated render, not against a specific
|
||||
// size. The threshold was 50 when the allowlist still described the rooted
|
||||
// Blackview; the 2026-08-11 rewrite to an explicit list for this phone cut
|
||||
// it to ~26, so a high bound would only pin the old policy in place.
|
||||
expect(policy.allowedPackages.length, greaterThan(10));
|
||||
// The launcher must survive enforcement or the device has no home screen.
|
||||
expect(policy.launcherPackage, isNotNull);
|
||||
expect(policy.allowedPackages, contains(policy.launcherPackage));
|
||||
expect(policy.home.hasCoordinates, isFalse,
|
||||
reason: 'committed policy.json must not carry home coordinates');
|
||||
expect(policy.curfew, isNotNull);
|
||||
});
|
||||
}
|
||||
|
||||
class _StubBundle extends CachingAssetBundle {
|
||||
_StubBundle(this.contents);
|
||||
|
||||
final String contents;
|
||||
|
||||
@override
|
||||
Future<ByteData> load(String key) async =>
|
||||
ByteData.sublistView(utf8.encode(contents));
|
||||
|
||||
@override
|
||||
Future<String> loadString(String key, {bool cache = true}) async => contents;
|
||||
}
|
||||
|
||||
/// Reads the asset straight off disk, bypassing the (empty) test rootBundle.
|
||||
class _RealAssetBundle extends CachingAssetBundle {
|
||||
@override
|
||||
Future<ByteData> load(String key) async {
|
||||
throw UnimplementedError(key);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> loadString(String key, {bool cache = true}) async {
|
||||
final file = File('assets/policy.json');
|
||||
return file.readAsString();
|
||||
}
|
||||
}
|
||||
|
||||
204
focus_owner/test/status_page_actions_test.dart
Normal file
204
focus_owner/test/status_page_actions_test.dart
Normal file
@ -0,0 +1,204 @@
|
||||
// StatusPage release, enforcement and polling behaviour.
|
||||
|
||||
// StatusPage widget behaviour.
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:focus_owner/device_policy.dart';
|
||||
import 'package:focus_owner/main.dart';
|
||||
import 'package:focus_owner/policy.dart';
|
||||
|
||||
/// Builds a channel whose handler answers from [responses], recording calls.
|
||||
({MethodChannel channel, List<MethodCall> calls}) _fakeChannel(
|
||||
Map<String, Object?> responses,
|
||||
) {
|
||||
const channel = MethodChannel('test/device_policy');
|
||||
final calls = <MethodCall>[];
|
||||
// hasHomeLocation is read on every status refresh, so it is defaulted here
|
||||
// rather than restated in each widget test. An explicit stub still wins,
|
||||
// which is what the geofence-state tests rely on.
|
||||
final stubs = {'hasHomeLocation': false, ...responses};
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(channel, (call) async {
|
||||
calls.add(call);
|
||||
if (!stubs.containsKey(call.method)) {
|
||||
throw PlatformException(code: 'unimplemented', message: 'no stub');
|
||||
}
|
||||
return stubs[call.method];
|
||||
});
|
||||
return (channel: channel, calls: calls);
|
||||
}
|
||||
|
||||
Map<String, Object?> _statusMap({bool isDeviceOwner = false}) => {
|
||||
'packageName': 'com.kuhy.focus_owner',
|
||||
'isDeviceOwner': isDeviceOwner,
|
||||
'isAdminActive': isDeviceOwner,
|
||||
'sdkInt': 36,
|
||||
'restrictionsApplied': false,
|
||||
};
|
||||
|
||||
/// Minimal policy so widget tests never touch the (empty) test rootBundle.
|
||||
Future<FocusPolicy> _stubPolicy() async => FocusPolicy.fromJson({
|
||||
'schema_version': kSupportedSchemaVersion,
|
||||
'home': {
|
||||
'latitude': null,
|
||||
'longitude': null,
|
||||
'radius_m': 150.0,
|
||||
'hysteresis_m': 30.0,
|
||||
},
|
||||
'curfew': {'start': '23:00', 'end': '05:00'},
|
||||
'launcher_package': 'com.launcher',
|
||||
'allowed_packages': ['com.good'],
|
||||
'night_allowed_packages': <String>[],
|
||||
'never_disable_prefixes': <String>[],
|
||||
'workout_unblock_domains': ['youtube.com'],
|
||||
'browser_packages': <String>[],
|
||||
});
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('StatusPage actions', () {
|
||||
testWidgets('confirming release calls the platform', (tester) async {
|
||||
final fake = _fakeChannel({
|
||||
'status': _statusMap(isDeviceOwner: true),
|
||||
'releaseDeviceOwner': true,
|
||||
});
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatusPage(
|
||||
policy: DevicePolicy(fake.channel),
|
||||
loadFocusPolicy: _stubPolicy,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.scrollUntilVisible(find.text('Release device owner'), 100);
|
||||
await tester.tap(find.text('Release device owner'));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Release'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(
|
||||
fake.calls.where((c) => c.method == 'releaseDeviceOwner'),
|
||||
hasLength(1),
|
||||
);
|
||||
expect(find.text('Device owner released'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('running enforcement waits for the new record', (tester) async {
|
||||
// The pass runs in a separate service process and now waits for a
|
||||
// location fix, so re-reading immediately returns the PREVIOUS record.
|
||||
// Measured: after re-anchoring home the card still showed the old
|
||||
// distance, which reads as "the button did nothing".
|
||||
var passRan = false;
|
||||
const channel = MethodChannel('test/refresh');
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(channel, (call) async {
|
||||
switch (call.method) {
|
||||
case 'status':
|
||||
return _statusMap(isDeviceOwner: true);
|
||||
case 'hasHomeLocation':
|
||||
return true;
|
||||
case 'runEnforcementNow':
|
||||
passRan = true;
|
||||
return true;
|
||||
case 'readEnforcementLog':
|
||||
return [_logLine(passRan ? 'AT_HOME' : 'AWAY')];
|
||||
default:
|
||||
throw PlatformException(code: 'unimplemented');
|
||||
}
|
||||
});
|
||||
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatusPage(
|
||||
policy: DevicePolicy(channel),
|
||||
loadFocusPolicy: _stubPolicy,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('AWAY'), findsOneWidget);
|
||||
|
||||
await tester.scrollUntilVisible(find.text('Run enforcement now'), 100);
|
||||
await tester.tap(find.text('Run enforcement now'));
|
||||
// Long enough for at least one poll interval to elapse.
|
||||
await tester.pump(const Duration(seconds: 3));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('AT HOME'), findsOneWidget);
|
||||
expect(find.text('AWAY'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('a stuck pass stops polling instead of hanging',
|
||||
(tester) async {
|
||||
// If no new record ever lands the UI must still settle, not spin.
|
||||
const channel = MethodChannel('test/stuck');
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(channel, (call) async {
|
||||
switch (call.method) {
|
||||
case 'status':
|
||||
return _statusMap(isDeviceOwner: true);
|
||||
case 'hasHomeLocation':
|
||||
return true;
|
||||
case 'runEnforcementNow':
|
||||
return true;
|
||||
case 'readEnforcementLog':
|
||||
return [_logLine('AWAY')];
|
||||
default:
|
||||
throw PlatformException(code: 'unimplemented');
|
||||
}
|
||||
});
|
||||
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatusPage(
|
||||
policy: DevicePolicy(channel),
|
||||
loadFocusPolicy: _stubPolicy,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.scrollUntilVisible(find.text('Run enforcement now'), 100);
|
||||
await tester.tap(find.text('Run enforcement now'));
|
||||
// Past the polling budget (~30 s).
|
||||
await tester.pump(const Duration(seconds: 40));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('AWAY'), findsOneWidget);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// One JSON-lines record, as the platform channel returns them.
|
||||
String _logLine(String reason) => jsonEncode({
|
||||
'ts': DateTime.now().millisecondsSinceEpoch,
|
||||
'reason': reason,
|
||||
'distance_m': reason == 'AWAY' ? 766.0 : 0.0,
|
||||
'threshold_m': 150.0,
|
||||
'inside_fence': reason != 'AWAY',
|
||||
'home_configured': true,
|
||||
'fix': {
|
||||
'age_ms': 5000,
|
||||
'provider': 'gps',
|
||||
'accuracy_m': 11.0,
|
||||
'outcome': 'ACTIVE_OK',
|
||||
},
|
||||
'curfew_active': false,
|
||||
'curfew_window': '23:00-05:00',
|
||||
'counts': {
|
||||
'to_hide': 3,
|
||||
'to_show': 58,
|
||||
'hid_delta': 0,
|
||||
'restored_delta': 0,
|
||||
},
|
||||
'hidden': const <Object?>[],
|
||||
'failure': null,
|
||||
});
|
||||
174
focus_owner/test/status_page_test.dart
Normal file
174
focus_owner/test/status_page_test.dart
Normal file
@ -0,0 +1,174 @@
|
||||
// StatusPage widget behaviour.
|
||||
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:focus_owner/device_policy.dart';
|
||||
import 'package:focus_owner/main.dart';
|
||||
import 'package:focus_owner/policy.dart';
|
||||
|
||||
/// Builds a channel whose handler answers from [responses], recording calls.
|
||||
({MethodChannel channel, List<MethodCall> calls}) _fakeChannel(
|
||||
Map<String, Object?> responses,
|
||||
) {
|
||||
const channel = MethodChannel('test/device_policy');
|
||||
final calls = <MethodCall>[];
|
||||
// hasHomeLocation is read on every status refresh, so it is defaulted here
|
||||
// rather than restated in each widget test. An explicit stub still wins,
|
||||
// which is what the geofence-state tests rely on.
|
||||
final stubs = {'hasHomeLocation': false, ...responses};
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(channel, (call) async {
|
||||
calls.add(call);
|
||||
if (!stubs.containsKey(call.method)) {
|
||||
throw PlatformException(code: 'unimplemented', message: 'no stub');
|
||||
}
|
||||
return stubs[call.method];
|
||||
});
|
||||
return (channel: channel, calls: calls);
|
||||
}
|
||||
|
||||
Map<String, Object?> _statusMap({bool isDeviceOwner = false}) => {
|
||||
'packageName': 'com.kuhy.focus_owner',
|
||||
'isDeviceOwner': isDeviceOwner,
|
||||
'isAdminActive': isDeviceOwner,
|
||||
'sdkInt': 36,
|
||||
'restrictionsApplied': false,
|
||||
};
|
||||
|
||||
/// Minimal policy so widget tests never touch the (empty) test rootBundle.
|
||||
Future<FocusPolicy> _stubPolicy() async => FocusPolicy.fromJson({
|
||||
'schema_version': kSupportedSchemaVersion,
|
||||
'home': {
|
||||
'latitude': null,
|
||||
'longitude': null,
|
||||
'radius_m': 150.0,
|
||||
'hysteresis_m': 30.0,
|
||||
},
|
||||
'curfew': {'start': '23:00', 'end': '05:00'},
|
||||
'launcher_package': 'com.launcher',
|
||||
'allowed_packages': ['com.good'],
|
||||
'night_allowed_packages': <String>[],
|
||||
'never_disable_prefixes': <String>[],
|
||||
'workout_unblock_domains': ['youtube.com'],
|
||||
'browser_packages': <String>[],
|
||||
});
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('StatusPage', () {
|
||||
testWidgets('hides the release button when not device owner',
|
||||
(tester) async {
|
||||
final fake = _fakeChannel({'status': _statusMap()});
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatusPage(
|
||||
policy: DevicePolicy(fake.channel),
|
||||
loadFocusPolicy: _stubPolicy,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Release device owner'), findsNothing);
|
||||
expect(find.textContaining('Nothing is enforced'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('shows the release button when device owner', (tester) async {
|
||||
final fake = _fakeChannel({'status': _statusMap(isDeviceOwner: true)});
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatusPage(
|
||||
policy: DevicePolicy(fake.channel),
|
||||
loadFocusPolicy: _stubPolicy,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Release device owner'), findsOneWidget);
|
||||
// The old "none (inert build)" label was a leftover from before this app
|
||||
// enforced anything, and it stayed on screen for weeks while YouTube was
|
||||
// in fact being hidden. With no pass recorded the honest answer is that
|
||||
// nothing is known yet, not that nothing is applied.
|
||||
expect(find.text('no pass recorded yet'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('reports when no home is set, since that means everywhere',
|
||||
(tester) async {
|
||||
// Without coordinates every pass decides LOCATION_UNKNOWN and fails
|
||||
// closed, so enforcement is permanent rather than geofenced. That is a
|
||||
// large behavioural difference and must be visible on screen.
|
||||
final fake = _fakeChannel({
|
||||
'status': _statusMap(isDeviceOwner: true),
|
||||
'hasHomeLocation': false,
|
||||
});
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatusPage(
|
||||
policy: DevicePolicy(fake.channel),
|
||||
loadFocusPolicy: _stubPolicy,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.textContaining('enforcing everywhere'), findsOneWidget);
|
||||
expect(find.text('Set home to current location'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('offers to update home once one is set', (tester) async {
|
||||
final fake = _fakeChannel({
|
||||
'status': _statusMap(isDeviceOwner: true),
|
||||
'hasHomeLocation': true,
|
||||
});
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatusPage(
|
||||
policy: DevicePolicy(fake.channel),
|
||||
loadFocusPolicy: _stubPolicy,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('active'), findsOneWidget);
|
||||
expect(find.text('Update home to here'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('release is confirmed before it runs', (tester) async {
|
||||
final fake = _fakeChannel({
|
||||
'status': _statusMap(isDeviceOwner: true),
|
||||
'releaseDeviceOwner': true,
|
||||
});
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatusPage(
|
||||
policy: DevicePolicy(fake.channel),
|
||||
loadFocusPolicy: _stubPolicy,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.scrollUntilVisible(find.text('Release device owner'), 100);
|
||||
await tester.tap(find.text('Release device owner'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Release device owner?'), findsOneWidget);
|
||||
|
||||
// Cancelling must not call through to the platform.
|
||||
await tester.tap(find.text('Cancel'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
fake.calls.where((c) => c.method == 'releaseDeviceOwner'),
|
||||
isEmpty,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// One JSON-lines record, as the platform channel returns them.
|
||||
@ -1,11 +1,7 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:focus_owner/device_policy.dart';
|
||||
import 'package:focus_owner/main.dart';
|
||||
import 'package:focus_owner/policy.dart';
|
||||
|
||||
/// Builds a channel whose handler answers from [responses], recording calls.
|
||||
({MethodChannel channel, List<MethodCall> calls}) _fakeChannel(
|
||||
@ -36,25 +32,13 @@ Map<String, Object?> _statusMap({bool isDeviceOwner = false}) => {
|
||||
'restrictionsApplied': false,
|
||||
};
|
||||
|
||||
/// Minimal policy so widget tests never touch the (empty) test rootBundle.
|
||||
Future<FocusPolicy> _stubPolicy() async => FocusPolicy.fromJson({
|
||||
'schema_version': kSupportedSchemaVersion,
|
||||
'home': {
|
||||
'latitude': null,
|
||||
'longitude': null,
|
||||
'radius_m': 150.0,
|
||||
'hysteresis_m': 30.0,
|
||||
},
|
||||
'curfew': {'start': '23:00', 'end': '05:00'},
|
||||
'launcher_package': 'com.launcher',
|
||||
'allowed_packages': ['com.good'],
|
||||
'night_allowed_packages': <String>[],
|
||||
'never_disable_prefixes': <String>[],
|
||||
'workout_unblock_domains': ['youtube.com'],
|
||||
'browser_packages': <String>[],
|
||||
});
|
||||
|
||||
|
||||
void main() {
|
||||
// Explicit since the split moved the testWidgets() group that used to
|
||||
// initialise the binding implicitly.
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('DevicePolicyStatus', () {
|
||||
test('parses the native status map', () {
|
||||
final status = DevicePolicyStatus.fromMap(_statusMap(isDeviceOwner: true));
|
||||
@ -101,251 +85,4 @@ void main() {
|
||||
expect(await DevicePolicy(fake.channel).releaseDeviceOwner(), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('StatusPage', () {
|
||||
testWidgets('hides the release button when not device owner',
|
||||
(tester) async {
|
||||
final fake = _fakeChannel({'status': _statusMap()});
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatusPage(
|
||||
policy: DevicePolicy(fake.channel),
|
||||
loadFocusPolicy: _stubPolicy,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Release device owner'), findsNothing);
|
||||
expect(find.textContaining('Nothing is enforced'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('shows the release button when device owner', (tester) async {
|
||||
final fake = _fakeChannel({'status': _statusMap(isDeviceOwner: true)});
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatusPage(
|
||||
policy: DevicePolicy(fake.channel),
|
||||
loadFocusPolicy: _stubPolicy,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Release device owner'), findsOneWidget);
|
||||
// The old "none (inert build)" label was a leftover from before this app
|
||||
// enforced anything, and it stayed on screen for weeks while YouTube was
|
||||
// in fact being hidden. With no pass recorded the honest answer is that
|
||||
// nothing is known yet, not that nothing is applied.
|
||||
expect(find.text('no pass recorded yet'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('reports when no home is set, since that means everywhere',
|
||||
(tester) async {
|
||||
// Without coordinates every pass decides LOCATION_UNKNOWN and fails
|
||||
// closed, so enforcement is permanent rather than geofenced. That is a
|
||||
// large behavioural difference and must be visible on screen.
|
||||
final fake = _fakeChannel({
|
||||
'status': _statusMap(isDeviceOwner: true),
|
||||
'hasHomeLocation': false,
|
||||
});
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatusPage(
|
||||
policy: DevicePolicy(fake.channel),
|
||||
loadFocusPolicy: _stubPolicy,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.textContaining('enforcing everywhere'), findsOneWidget);
|
||||
expect(find.text('Set home to current location'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('offers to update home once one is set', (tester) async {
|
||||
final fake = _fakeChannel({
|
||||
'status': _statusMap(isDeviceOwner: true),
|
||||
'hasHomeLocation': true,
|
||||
});
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatusPage(
|
||||
policy: DevicePolicy(fake.channel),
|
||||
loadFocusPolicy: _stubPolicy,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('active'), findsOneWidget);
|
||||
expect(find.text('Update home to here'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('release is confirmed before it runs', (tester) async {
|
||||
final fake = _fakeChannel({
|
||||
'status': _statusMap(isDeviceOwner: true),
|
||||
'releaseDeviceOwner': true,
|
||||
});
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatusPage(
|
||||
policy: DevicePolicy(fake.channel),
|
||||
loadFocusPolicy: _stubPolicy,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.scrollUntilVisible(find.text('Release device owner'), 100);
|
||||
await tester.tap(find.text('Release device owner'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Release device owner?'), findsOneWidget);
|
||||
|
||||
// Cancelling must not call through to the platform.
|
||||
await tester.tap(find.text('Cancel'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
fake.calls.where((c) => c.method == 'releaseDeviceOwner'),
|
||||
isEmpty,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('confirming release calls the platform', (tester) async {
|
||||
final fake = _fakeChannel({
|
||||
'status': _statusMap(isDeviceOwner: true),
|
||||
'releaseDeviceOwner': true,
|
||||
});
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatusPage(
|
||||
policy: DevicePolicy(fake.channel),
|
||||
loadFocusPolicy: _stubPolicy,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.scrollUntilVisible(find.text('Release device owner'), 100);
|
||||
await tester.tap(find.text('Release device owner'));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Release'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(
|
||||
fake.calls.where((c) => c.method == 'releaseDeviceOwner'),
|
||||
hasLength(1),
|
||||
);
|
||||
expect(find.text('Device owner released'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('running enforcement waits for the new record', (tester) async {
|
||||
// The pass runs in a separate service process and now waits for a
|
||||
// location fix, so re-reading immediately returns the PREVIOUS record.
|
||||
// Measured: after re-anchoring home the card still showed the old
|
||||
// distance, which reads as "the button did nothing".
|
||||
var passRan = false;
|
||||
const channel = MethodChannel('test/refresh');
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(channel, (call) async {
|
||||
switch (call.method) {
|
||||
case 'status':
|
||||
return _statusMap(isDeviceOwner: true);
|
||||
case 'hasHomeLocation':
|
||||
return true;
|
||||
case 'runEnforcementNow':
|
||||
passRan = true;
|
||||
return true;
|
||||
case 'readEnforcementLog':
|
||||
return [_logLine(passRan ? 'AT_HOME' : 'AWAY')];
|
||||
default:
|
||||
throw PlatformException(code: 'unimplemented');
|
||||
}
|
||||
});
|
||||
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatusPage(
|
||||
policy: DevicePolicy(channel),
|
||||
loadFocusPolicy: _stubPolicy,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('AWAY'), findsOneWidget);
|
||||
|
||||
await tester.scrollUntilVisible(find.text('Run enforcement now'), 100);
|
||||
await tester.tap(find.text('Run enforcement now'));
|
||||
// Long enough for at least one poll interval to elapse.
|
||||
await tester.pump(const Duration(seconds: 3));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('AT HOME'), findsOneWidget);
|
||||
expect(find.text('AWAY'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('a stuck pass stops polling instead of hanging',
|
||||
(tester) async {
|
||||
// If no new record ever lands the UI must still settle, not spin.
|
||||
const channel = MethodChannel('test/stuck');
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(channel, (call) async {
|
||||
switch (call.method) {
|
||||
case 'status':
|
||||
return _statusMap(isDeviceOwner: true);
|
||||
case 'hasHomeLocation':
|
||||
return true;
|
||||
case 'runEnforcementNow':
|
||||
return true;
|
||||
case 'readEnforcementLog':
|
||||
return [_logLine('AWAY')];
|
||||
default:
|
||||
throw PlatformException(code: 'unimplemented');
|
||||
}
|
||||
});
|
||||
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatusPage(
|
||||
policy: DevicePolicy(channel),
|
||||
loadFocusPolicy: _stubPolicy,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.scrollUntilVisible(find.text('Run enforcement now'), 100);
|
||||
await tester.tap(find.text('Run enforcement now'));
|
||||
// Past the polling budget (~30 s).
|
||||
await tester.pump(const Duration(seconds: 40));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('AWAY'), findsOneWidget);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// One JSON-lines record, as the platform channel returns them.
|
||||
String _logLine(String reason) => jsonEncode({
|
||||
'ts': DateTime.now().millisecondsSinceEpoch,
|
||||
'reason': reason,
|
||||
'distance_m': reason == 'AWAY' ? 766.0 : 0.0,
|
||||
'threshold_m': 150.0,
|
||||
'inside_fence': reason != 'AWAY',
|
||||
'home_configured': true,
|
||||
'fix': {
|
||||
'age_ms': 5000,
|
||||
'provider': 'gps',
|
||||
'accuracy_m': 11.0,
|
||||
'outcome': 'ACTIVE_OK',
|
||||
},
|
||||
'curfew_active': false,
|
||||
'curfew_window': '23:00-05:00',
|
||||
'counts': {
|
||||
'to_hide': 3,
|
||||
'to_show': 58,
|
||||
'hid_delta': 0,
|
||||
'restored_delta': 0,
|
||||
},
|
||||
'hidden': const <Object?>[],
|
||||
'failure': null,
|
||||
});
|
||||
|
||||
@ -6,8 +6,9 @@
|
||||
|
||||
## Where things stand
|
||||
|
||||
**83 files** over 250 lines (was 183). Everything is committed and pushed to
|
||||
`main` at `093e2f6`; the tree is clean.
|
||||
**78 files** over 250 lines (was 183). Everything is committed and pushed to
|
||||
`main` at `2ce86a5`. `focus_owner/analysis_options.yaml` has an uncommitted
|
||||
change from an earlier session — **leave it unstaged**, it is not ours.
|
||||
|
||||
**`python_pkg/` is DONE — 50 violations → 0.** Do not reopen it. Cleared one
|
||||
package per tranche, pushing after each: `brother_printer` (15→0, suite pinned
|
||||
@ -62,8 +63,12 @@ Same in TypeScript: `kcd2`'s `search.ts` and `badgeValue.ts`, and
|
||||
caller's globals — stricter than `shellcheck -x` on the entry, and the check
|
||||
that decides whether a seam is real. Run it before believing a split.
|
||||
|
||||
Next: the remaining `main()`-shaped shell scripts, then `focus_owner` and
|
||||
`billsplit` (Kotlin+Dart, untouched so far — check for a test command first).
|
||||
Next: `billsplit` (Dart, untouched), then the remaining `main()`-shaped shell
|
||||
scripts. `focus_owner`'s six leftovers are four Kotlin sources that read a
|
||||
private `context`, one State class, and one test one line over.
|
||||
|
||||
Baselines and the per-language traps (Dart `part`, Kotlin fixture objects, the
|
||||
TS import cycle) are in `docs/app-split-recipes.md`.
|
||||
|
||||
## The live worklist
|
||||
|
||||
@ -136,10 +141,12 @@ Plus `.github/workflows/file-length.yml` modelled on
|
||||
|
||||
## Language recipes
|
||||
|
||||
- `docs/python-split-recipes.md` — the seam-selection rule, the `__all__`
|
||||
re-export form, the `TC001` trap, the mixin shape, and the coverage
|
||||
behaviour of new sibling modules. `python_pkg` is done; read it if a stray
|
||||
`.py` turns up, or for the seam rule, which generalises to any language.
|
||||
- `docs/python-split-recipes.md` — the seam-selection rule (generalises to
|
||||
every language), the `__all__` re-export form, the `TC001` trap and the
|
||||
mixin shape. `python_pkg` is done.
|
||||
- `docs/app-split-recipes.md` — Dart `part`, Kotlin fixture objects, the
|
||||
TypeScript import cycle that compiles clean, and the per-project test
|
||||
baselines. **Read before touching `billsplit` or `focus_owner`.**
|
||||
- `docs/shell-split-recipes.md` — **read this before the next
|
||||
`linux_configuration` split.** What replaces the pass count when there is no
|
||||
test suite, which scripts escape the repo, the lib shape that satisfies both
|
||||
@ -154,23 +161,10 @@ Each rule in both cost a failed gate run or a revert to establish.
|
||||
- **Shell** — extract into `lib/*.sh` sourced by a thin entry script. See
|
||||
`docs/shell-split-recipes.md` for the lib shape; note it corrects the old
|
||||
advice to repeat `set -euo pipefail` in every lib.
|
||||
- **Python** — extract cohesive helpers into sibling modules; keep the public
|
||||
API stable by re-exporting.
|
||||
- **TS/Dart** — extract components; re-export from the original path so no
|
||||
importer changes.
|
||||
- **Tests** — split by describe/test-group. Coverage must not drop, and
|
||||
**verify by the pass _count_, not a green run**: a split that loses a whole
|
||||
module still reports "passed".
|
||||
In `python_pkg`, shared fixture builders go in the package's
|
||||
**`tests/conftest.py`** — the `name-tests-test` hook rejects any other
|
||||
non-`test_*.py` file under `tests/`, so a `_helpers.py` will fail the gate.
|
||||
`python_pkg/syncyomi_guard/tests/conftest.py` is the precedent, and
|
||||
`meta/pyproject.toml` omits `*/tests/*` from coverage by path so nothing
|
||||
there is counted as production code (the opposite of the `poker-stakes`
|
||||
trap below). Prefer one shared conftest over another `partN` file: the repo
|
||||
already carries four, and each new one that redeclares the same fixture
|
||||
scaffolding pushes jscpd toward its 2% duplication threshold — which only
|
||||
fires on the real `git commit`.
|
||||
- **Python / TS / Dart / Kotlin** — see the two recipe docs above.
|
||||
- **Tests** — split by describe/test-group and **verify by the pass _count_,
|
||||
not a green run**: a split that loses a whole module still reports "passed".
|
||||
Shared fixtures go where the coverage config already excludes them.
|
||||
|
||||
Never game the cap: no one-lining, no deleting tests, no moving code into an
|
||||
exempt extension, no suppressions.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user