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 /// single source of truth. Refusing an unknown version is deliberate: silently /// accepting a newer schema would mean enforcing a policy this code has /// misread, and a misread allowlist is an allowlist that blocks the dialer. const int kSupportedSchemaVersion = 1; /// Raised when the bundled policy cannot be trusted. class PolicyFormatException implements Exception { const PolicyFormatException(this.message); final String message; @override String toString() => 'PolicyFormatException: $message'; } /// A nightly window that may wrap midnight (23:00 -> 05:00). class CurfewWindow { const CurfewWindow({required this.startMinutes, required this.endMinutes}); /// Parses an `{"start": "23:00", "end": "05:00"}` object. factory CurfewWindow.fromJson(Map json) { return CurfewWindow( startMinutes: _parseHhMm(json['start'], 'curfew.start'), endMinutes: _parseHhMm(json['end'], 'curfew.end'), ); } /// Minutes since midnight. final int startMinutes; final int endMinutes; /// Whether [minutesSinceMidnight] falls inside the window. /// /// Wrapping windows are the normal case here, so "inside" means at or after /// the start *or* before the end, not both. bool contains(int minutesSinceMidnight) { if (startMinutes <= endMinutes) { return minutesSinceMidnight >= startMinutes && minutesSinceMidnight < endMinutes; } return minutesSinceMidnight >= startMinutes || minutesSinceMidnight < endMinutes; } } /// The home anchor. Coordinates are null in a redacted policy. class HomeLocation { const HomeLocation({ required this.latitude, required this.longitude, required this.radiusM, required this.hysteresisM, }); /// Parses the `home` object. factory HomeLocation.fromJson(Map json) { return HomeLocation( latitude: (json['latitude'] as num?)?.toDouble(), longitude: (json['longitude'] as num?)?.toDouble(), radiusM: _requireNum(json['radius_m'], 'home.radius_m'), hysteresisM: _requireNum(json['hysteresis_m'], 'home.hysteresis_m'), ); } final double? latitude; final double? longitude; final double radiusM; final double hysteresisM; /// Whether this policy carries real coordinates. /// /// The committed asset is redacted, so the location gate cannot run against /// it. Enforcement must check this rather than treating a missing coordinate /// as (0, 0), which would put "home" in the Atlantic. bool get hasCoordinates => latitude != null && longitude != null; }