wake-alarm/phone_app/lib/services/alarm_scheduler.dart
Krzysztof kuhy Rudnicki af2a9b416b Phase 4/3: the phone's Dart half — ring screen, challenges, room proofs
Dart side of the alarm (items 1, 4, 3). main.dart had no init seam at all;
it now has ensureInitialized(), a navigatorKey and a /ring route.

- challenge.dart: all five kinds ported from _challenges.py so both halves
  behave identically. kDismissCodeLength/kDismissFlashSeconds are duplicated
  from _constants.py, not synced — noted in the file, since Dart cannot read
  Python.
- alarm_scheduler.dart: the Dart side of the single MethodChannel.
- ring_screen.dart: room proof first, then a challenge, then dismiss. Back is
  a no-op (PopScope) so the alarm cannot be escaped.
- room_proof.dart: NFC or QR satisfies on its own; steps NEVER do alone —
  the phone is stationary while you walk to it, so steps only accrue after
  pickup and can be faked by shaking it in bed. Steps count only together
  with elapsed time, as the fallback when both others fail.
- home_screen: arms the app's own alarm instead of asking the user to confirm
  in the Clock app (item 1's "no user interaction"). SET_ALARM stays as
  redundancy with SKIP_UI=true.

Re-arm has three hooks and needs all three: startup, BOOT_COMPLETED, and
dismiss. The plan assumed LogStore.changes would cover this; that stream does
not exist in crdt_sync_dart (verified — the library has no Stream at all), so
the picker's save is only one hook and a restart would otherwise arm nothing.

Fixed a real gap the API 36 AVD exposed: POST_NOTIFICATIONS was never
requested at runtime, so a fresh install sat at importance=NONE and the
full-screen intent was rejected outright — the alarm would ring with no
dismiss screen. MainActivity now requests it, and canUseFullScreenIntent()
also checks areNotificationsEnabled() so the check gates rather than warns.

Verified on the API 36 AVD: screen Asleep -> Awake, AlarmActivity became the
top resumed activity over the lockscreen, service isForeground=true
types=0x2 (mediaPlayback) category=alarm, no crash. flutter test: 54 pass.
dart analyze: clean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EckWmyfetYq3Mi8MAnUudh
2026-08-27 21:22:07 +02:00

64 lines
2.6 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` which is
/// currently all seven days. The two are deliberately not synced — a shared
/// day set is item 8's shape, and diverging defaults would have the halves
/// firing on different days.
Future<void> schedule(int hour, int minute) =>
_channel.invokeMethod<void>('schedule', <String, int>{
'hour': hour,
'minute': minute,
});
/// Cancels the armed alarm and forgets the stored time.
Future<void> cancel() => _channel.invokeMethod<void>('cancel');
/// 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;
/// 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;
}