mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-24 13:06:25 +01:00
The alarm was opt-in: it only armed when the user tapped "Set Alarm & Sync to PC", so it could be defeated by simply not tapping it, or by cancelling it the night before. It is now automatic and there is no lever a half-asleep user can pull that buys them sleep. - AlarmPolicy bakes the schedule in at 08:00 daily. The only edit the UI offers is moving the NEXT alarm earlier; setEarlier() refuses anything at or after the baseline, and refuses to relax an active override. Earlier-only is the whole design: every available change costs the user sleep. - The ratchet resets after each fire, so an early start is one-shot. A one-way ratchet with no reset is a trap, not a defence — the first 05:00 would otherwise be permanent with no way back short of reinstalling. - load() re-validates the stored value and ignores anything at or after the baseline. SharedPreferences is not a trust boundary. - No cancel. The Kotlin handler is deleted, not just the Dart wrapper, so the capability genuinely does not exist rather than being one invokeMethod away. - Arming happens in main()'s init seam and via AlarmWatchdog, a daily self-rescheduling check that re-arms if nothing is scheduled — closing the gap where a force-stop or a failed re-arm goes unnoticed until the morning it does not ring. BootReceiver re-establishes it, since setInexactRepeating does not survive a reboot. - Publishing to the PC is automatic too; it used to need the same tap. PC half, so the two agree with the network down: - FALLBACK_ALARM_HOUR 13 -> 8, matching the phone's baseline. At 13:00 a Firebase outage silently moved the alarm five hours LATER — the one direction the design refuses to allow. - _trigger._clamp_to_baseline() refuses any resolved alarm later than the baseline, whatever its source. Without it the PC would faithfully reproduce a stale or tampered remote value and the forbidden direction stays reachable through sync. Tests: 465 Python at 100% branch coverage, 79 Dart. Both ratchet directions are covered, per the standing rule about monotonic counters. test_sync.py and test_trigger.py were split (payload, token, CLI, clamp) to stay under the 250-line gate; the split lost indirect coverage of extract_hlc/newest_alarm, which now have direct tests instead. `python -m wake_alarm._sync --show` and `_trigger --dry-run` both re-run and still print real output. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EckWmyfetYq3Mi8MAnUudh
116 lines
4.7 KiB
Dart
116 lines
4.7 KiB
Dart
import 'package:flutter/services.dart';
|
|
|
|
/// Dart side of the single MethodChannel the Kotlin ring path exposes.
|
|
///
|
|
/// One channel for the whole feature (see `AlarmChannel.kt`): the alarm must
|
|
/// never depend on a pub plugin compiling against AGP 9.
|
|
class AlarmScheduler {
|
|
/// Creates a scheduler; [channel] is injectable so tests need no platform.
|
|
AlarmScheduler({MethodChannel? channel})
|
|
: _channel = channel ?? const MethodChannel(channelName);
|
|
|
|
/// Channel name, matching `AlarmActivity.CHANNEL` on the Kotlin side.
|
|
static const String channelName = 'com.kuhy.wake_alarm_sync/alarm';
|
|
|
|
final MethodChannel _channel;
|
|
|
|
/// Arms the alarm for the next occurrence of [hour]:[minute] in local time.
|
|
///
|
|
/// The phone rings **daily**, matching the PC's `ALARM_DAYS` (all seven
|
|
/// days) and its baked-in fallback hour. Callers pass a time resolved by
|
|
/// `AlarmPolicy`, never a user-chosen one: the schedule is fixed at 08:00
|
|
/// and only ever ratchets earlier.
|
|
Future<void> schedule(int hour, int minute) =>
|
|
_channel.invokeMethod<void>('schedule', <String, int>{
|
|
'hour': hour,
|
|
'minute': minute,
|
|
});
|
|
|
|
// There is deliberately NO cancel(). The alarm cannot be switched off from
|
|
// the app: the only way past it is the full dismiss ritual at ring time.
|
|
// The Kotlin `cancel` handler is gone too, so this is not merely a missing
|
|
// Dart wrapper around a live capability.
|
|
|
|
/// Arms the fixed schedule and starts the daily watchdog.
|
|
///
|
|
/// Called from the app's init seam, so simply launching the app arms the
|
|
/// alarm — there is no button to forget to press.
|
|
Future<void> armSchedule(int hour, int minute) =>
|
|
_channel.invokeMethod<void>('armSchedule', <String, int>{
|
|
'hour': hour,
|
|
'minute': minute,
|
|
});
|
|
|
|
/// Re-arms from the stored time; false when no alarm was ever set.
|
|
///
|
|
/// Called from the app's init seam because the store has no change stream:
|
|
/// without this, an app restart that does not touch the picker would leave
|
|
/// nothing armed.
|
|
Future<bool> rearm() async =>
|
|
await _channel.invokeMethod<bool>('rearm') ?? false;
|
|
|
|
/// Stops the ring and immediately re-arms for tomorrow.
|
|
Future<void> dismiss() => _channel.invokeMethod<void>('dismiss');
|
|
|
|
/// Whether the system will honour the full-screen intent.
|
|
///
|
|
/// False from API 34 unless granted. The caller must treat this as a hard
|
|
/// failure: without it the alarm posts a notification the sleeper never sees.
|
|
Future<bool> canUseFullScreenIntent() async =>
|
|
await _channel.invokeMethod<bool>('canUseFullScreenIntent') ?? false;
|
|
|
|
/// Whether the device can read NFC and has it switched on.
|
|
Future<bool> hasNfc() async =>
|
|
await _channel.invokeMethod<bool>('hasNfc') ?? false;
|
|
|
|
/// Calls [onTag] with each NFC tag id scanned while the ring screen is up.
|
|
///
|
|
/// Reader mode is driven from Kotlin, so the tap does not restart the
|
|
/// activity — a restart would reset the challenge.
|
|
void onNfcTag(void Function(String tagId) onTag) {
|
|
_channel.setMethodCallHandler((call) async {
|
|
if (call.method == 'onNfcTag') {
|
|
final id = call.arguments;
|
|
if (id is String) onTag(id);
|
|
}
|
|
return null;
|
|
});
|
|
}
|
|
|
|
/// Starts reader mode so the next tag tapped can be registered.
|
|
///
|
|
/// Returns false when the device has no NFC or it is switched off, so the
|
|
/// UI can say so rather than waiting on a tap that cannot come.
|
|
Future<bool> startTagRegistration() async =>
|
|
await _channel.invokeMethod<bool>('startTagRegistration') ?? false;
|
|
|
|
/// Stops reader mode after registration.
|
|
Future<void> stopTagRegistration() =>
|
|
_channel.invokeMethod<void>('stopTagRegistration');
|
|
|
|
/// Stops listening for NFC taps.
|
|
void clearNfcListener() => _channel.setMethodCallHandler(null);
|
|
|
|
/// Closes the ring screen after a successful dismiss.
|
|
///
|
|
/// Without this the dismiss screen stays up over the lockscreen forever —
|
|
/// back is disabled by design — and the live instance would carry a
|
|
/// satisfied room proof and the solved answer into the next ring.
|
|
Future<void> closeRingScreen() =>
|
|
_channel.invokeMethod<void>('closeRingScreen');
|
|
|
|
/// Starts the ring immediately, without waiting for the alarm instant.
|
|
///
|
|
/// Exists so the foreground-service and full-screen-intent path can be
|
|
/// exercised on an emulator; it arms nothing and changes no stored time.
|
|
Future<void> ringNow() => _channel.invokeMethod<void>('ringNow');
|
|
|
|
/// Captures the step baseline for this ring.
|
|
Future<void> beginStepCount() =>
|
|
_channel.invokeMethod<void>('beginStepCount');
|
|
|
|
/// Steps since [beginStepCount], or -1 when unavailable.
|
|
Future<int> stepsSinceRingStart() async =>
|
|
await _channel.invokeMethod<int>('stepCount') ?? -1;
|
|
}
|