wake-alarm/phone_app/lib/services/alarm_arming.dart
Krzysztof kuhy Rudnicki 38fb4f3763 Add motion tokens and haptics to the phone app
Motion tokens hand-copied from DOCS-motion.md into theme.dart; every
page route goes through appRoute() (slow, decelerate in, accelerate
out, instant under Remove animations). mediumImpact on the picker
confirm, heavyImpact on a refused time. The Clock app line shows only
when the SET_ALARM handoff finished before its 3 s timeout. Sound is
skipped on purpose.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 08:09:48 +01:00

60 lines
2.3 KiB
Dart

import 'package:android_intent_plus/android_intent.dart';
import 'package:wake_alarm_sync/services/alarm_scheduler.dart';
/// Arms the phone's own alarm for a chosen time (item 1: no user interaction).
///
/// Split out of `home_screen.dart` to keep that file under the 250-line gate,
/// and because "what it takes to arm an alarm" is its own concern from "the
/// picker UI".
class AlarmArming {
/// Creates an armer over [scheduler].
const AlarmArming(this.scheduler);
/// Platform bridge to the Kotlin scheduler.
final AlarmScheduler scheduler;
/// Arms the alarm for [hour]:[minute].
///
/// Returns an error string when it could not be armed, or null on success.
/// A revoked full-screen-intent grant is a hard failure, not a warning:
/// without it the alarm posts a notification the sleeping user never sees,
/// so reporting success would be a lie the user only discovers at 06:00.
Future<String?> arm(int hour, int minute) async {
if (!await scheduler.canUseFullScreenIntent()) {
return 'Allow notifications and "full screen notifications" for this '
'app in Android settings — without them the alarm cannot show '
'over the lockscreen.';
}
await scheduler.schedule(hour, minute);
return null;
}
/// Also registers the time in the system Clock app, as pure redundancy.
///
/// This used to be the mechanism, and required confirming in another app
/// every single time. `SKIP_UI` is true because the app has already armed
/// its own alarm by now, so this must not demand a tap.
///
/// android_intent_plus v5 uses startActivityForResult and the Clock app
/// never sends a result, so this times out after 3s rather than hang.
///
/// Returns true only if the launch completed before the timeout.
Future<bool> registerWithClockApp(int hour, int minute) async {
final intent = AndroidIntent(
action: 'android.intent.action.SET_ALARM',
arguments: <String, Object>{
'android.intent.extra.alarm.HOUR': hour,
'android.intent.extra.alarm.MINUTES': minute,
'android.intent.extra.alarm.SKIP_UI': true,
'android.intent.extra.alarm.MESSAGE': 'Wake Alarm',
},
);
var completed = true;
await intent.launch().timeout(
const Duration(seconds: 3),
onTimeout: () => completed = false,
);
return completed;
}
}