mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-21 14:42:20 +01:00
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
121 lines
4.0 KiB
Dart
121 lines
4.0 KiB
Dart
import 'package:wake_alarm_sync/services/alarm_scheduler.dart';
|
|
|
|
/// Which mechanism satisfied the separate-room requirement.
|
|
enum RoomProofKind {
|
|
/// Tapped the registered NFC tag in the other room.
|
|
nfc,
|
|
|
|
/// Scanned the printed QR code in the other room.
|
|
qr,
|
|
|
|
/// Walked far enough, for long enough, since the alarm started.
|
|
steps,
|
|
}
|
|
|
|
/// Result of a room-proof attempt.
|
|
class RoomProofResult {
|
|
/// Creates a result; [kind] is null when nothing has satisfied yet.
|
|
const RoomProofResult({required this.satisfied, this.kind, this.detail = ''});
|
|
|
|
/// Whether the user has proven they are in the other room.
|
|
final bool satisfied;
|
|
|
|
/// Which mechanism satisfied it, or null if none did.
|
|
final RoomProofKind? kind;
|
|
|
|
/// Human-readable reason, shown on the ring screen.
|
|
final String detail;
|
|
}
|
|
|
|
/// Proof that the user physically walked to another room before dismissing.
|
|
///
|
|
/// **Any one of NFC or QR satisfies.** Steps alone deliberately does NOT: the
|
|
/// phone is stationary in the other room while you walk *to* it, so steps only
|
|
/// accrue after pickup and can be faked by shaking it in bed. Steps are only
|
|
/// accepted together with elapsed time, as the fallback for when both other
|
|
/// mechanisms fail — a single failing mechanism at 06:00 must never make the
|
|
/// alarm undismissable.
|
|
class RoomProof {
|
|
/// Creates a room-proof checker over [scheduler]'s platform channel.
|
|
RoomProof({required this.scheduler, this._registeredTagId});
|
|
|
|
/// Platform bridge used to read the step counter.
|
|
final AlarmScheduler scheduler;
|
|
final String? _registeredTagId;
|
|
|
|
/// Steps required by the fallback, counted since the alarm started ringing.
|
|
static const int requiredSteps = 25;
|
|
|
|
/// Elapsed time the fallback also requires, so shaking the phone cannot pass.
|
|
static const Duration requiredElapsed = Duration(minutes: 2);
|
|
|
|
/// Whether [tagId] is the tag registered in Settings.
|
|
///
|
|
/// Registering no tag means NFC cannot satisfy the proof — it does not mean
|
|
/// every tag passes.
|
|
RoomProofResult checkNfc(String tagId) {
|
|
final registered = _registeredTagId;
|
|
if (registered == null || registered.isEmpty) {
|
|
return const RoomProofResult(
|
|
satisfied: false,
|
|
detail: 'No NFC tag registered in Settings',
|
|
);
|
|
}
|
|
if (tagId != registered) {
|
|
return const RoomProofResult(satisfied: false, detail: 'Wrong NFC tag');
|
|
}
|
|
return const RoomProofResult(
|
|
satisfied: true,
|
|
kind: RoomProofKind.nfc,
|
|
detail: 'NFC tag matched',
|
|
);
|
|
}
|
|
|
|
/// Whether [payload] is the expected printed QR code.
|
|
///
|
|
/// A printed code is photographable, which is exactly why it is one of two
|
|
/// accepted proofs rather than the only one.
|
|
RoomProofResult checkQr(String payload, {required String expected}) {
|
|
if (expected.isEmpty || payload != expected) {
|
|
return const RoomProofResult(satisfied: false, detail: 'Wrong QR code');
|
|
}
|
|
return const RoomProofResult(
|
|
satisfied: true,
|
|
kind: RoomProofKind.qr,
|
|
detail: 'QR code matched',
|
|
);
|
|
}
|
|
|
|
/// The fallback: enough steps AND enough elapsed time since the ring started.
|
|
///
|
|
/// Both are required together. [elapsed] is passed in rather than measured
|
|
/// here so the ring screen owns the single clock.
|
|
Future<RoomProofResult> checkSteps(Duration elapsed) async {
|
|
final steps = await scheduler.stepsSinceRingStart();
|
|
if (steps < 0) {
|
|
return const RoomProofResult(
|
|
satisfied: false,
|
|
detail: 'Step sensor unavailable',
|
|
);
|
|
}
|
|
if (steps < requiredSteps) {
|
|
return RoomProofResult(
|
|
satisfied: false,
|
|
detail: '$steps/$requiredSteps steps',
|
|
);
|
|
}
|
|
if (elapsed < requiredElapsed) {
|
|
final left = requiredElapsed - elapsed;
|
|
return RoomProofResult(
|
|
satisfied: false,
|
|
detail: 'Keep moving for ${left.inSeconds}s more',
|
|
);
|
|
}
|
|
return const RoomProofResult(
|
|
satisfied: true,
|
|
kind: RoomProofKind.steps,
|
|
detail: 'Walked far enough, long enough',
|
|
);
|
|
}
|
|
}
|