diff --git a/phone_app/android/app/src/main/kotlin/com/kuhy/wake_alarm_sync/MainActivity.kt b/phone_app/android/app/src/main/kotlin/com/kuhy/wake_alarm_sync/MainActivity.kt index 291fb8a..603b30f 100644 --- a/phone_app/android/app/src/main/kotlin/com/kuhy/wake_alarm_sync/MainActivity.kt +++ b/phone_app/android/app/src/main/kotlin/com/kuhy/wake_alarm_sync/MainActivity.kt @@ -1,6 +1,7 @@ package com.kuhy.wake_alarm_sync import android.Manifest +import android.content.Intent import android.content.pm.PackageManager import android.os.Bundle import androidx.core.app.ActivityCompat @@ -71,6 +72,24 @@ class MainActivity : FlutterActivity() { registeringTag = NfcReader.start(this, channel) result.success(registeringTag) } + // Finishes the picker after starting a test ring, so it + // cannot sit on top of AlarmActivity — that pause used to + // tear NFC reader mode down for the whole ring. + "ringNow" -> { + AlarmChannel.handleCall(applicationContext, call, result) + // An unlocked, in-use device downgrades the full-screen + // intent to a heads-up notification (appops logs a + // rejectTime), so the ring screen would never appear on + // its own during a hardware test. Launch it directly and + // step out of the way: leaving the picker in front pauses + // AlarmActivity, which used to kill NFC reader mode. + startActivity( + Intent(this, AlarmActivity::class.java).apply { + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + }, + ) + finishAndRemoveTask() + } "stopTagRegistration" -> { registeringTag = false NfcReader.stop(this) diff --git a/phone_app/lib/main_ring_probe.dart b/phone_app/lib/main_ring_probe.dart index f7573e5..edf0db4 100644 --- a/phone_app/lib/main_ring_probe.dart +++ b/phone_app/lib/main_ring_probe.dart @@ -3,20 +3,20 @@ import 'package:flutter/material.dart'; import 'package:wake_alarm_sync/main.dart'; import 'package:wake_alarm_sync/services/alarm_scheduler.dart'; -/// Debug entrypoint that starts the ring and opens the real dismiss route. +/// Debug entrypoint that starts the ring exactly as a real alarm would. /// -/// Used to exercise the Kotlin foreground-service and full-screen-intent path -/// on an emulator, where the normal flow needs a sync backend first. It runs -/// the **real** app widget and pushes [kRingRoute], so what appears is exactly -/// what AlarmActivity's initial route produces — a probe with its own UI would -/// prove nothing about the route. +/// Calls `ringNow`, which starts AlarmService — the service then posts its +/// full-screen intent and the system launches AlarmActivity on top. This +/// process then finishes itself, so the picker never sits in front of the +/// ring screen: it used to, and AlarmActivity's pause tore NFC reader mode +/// down with it, which is precisely the bug this probe now has to avoid +/// reintroducing. /// /// Not part of the shipped app: `flutter build apk` uses `lib/main.dart`. Future main() async { WidgetsFlutterBinding.ensureInitialized(); await AlarmScheduler().ringNow(); + // Nothing to show: the ring screen is a separate activity launched by the + // service. Rendering the picker here would cover it. runApp(const WakeAlarmSyncApp()); - WidgetsBinding.instance.addPostFrameCallback((_) { - navigatorKey.currentState?.pushNamed(kRingRoute); - }); } diff --git a/phone_app/lib/screens/ring_screen.dart b/phone_app/lib/screens/ring_screen.dart index 8421ac3..b787679 100644 --- a/phone_app/lib/screens/ring_screen.dart +++ b/phone_app/lib/screens/ring_screen.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:developer' as developer; import 'package:flutter/material.dart'; import 'package:wake_alarm_sync/services/alarm_scheduler.dart'; @@ -40,9 +41,6 @@ class _RingScreenState extends State { bool _inRoom = false; String _status = ''; - /// Hidden after [kDismissFlashSeconds] for the flash challenge only. - bool _codeVisible = true; - Timer? _flashTimer; @override void initState() { @@ -50,7 +48,6 @@ class _RingScreenState extends State { _proof = RoomProof(scheduler: _scheduler); unawaited(_scheduler.beginStepCount()); unawaited(_loadRoomProof()); - _startFlashTimerIfNeeded(); } /// Loads the registered tag/QR and starts listening for NFC taps. @@ -71,11 +68,28 @@ class _RingScreenState extends State { void _onNfcTag(String tagId) { final result = _proof.checkNfc(tagId); + developer.log( + 'nfc tag=$tagId satisfied=${result.satisfied} detail=${result.detail}', + name: 'WakeAlarmRing', + ); if (!mounted) return; setState(() { _inRoom = result.satisfied; _status = result.detail; }); + if (result.satisfied) _logChallenge(); + } + + /// Logs the current challenge so a hardware test can be scripted. + /// + /// Debug aid only: the answer is already on screen for whoever is holding + /// the phone, so this leaks nothing the user cannot see. + void _logChallenge() { + developer.log( + 'challenge kind=${_challenge.kind.name} display="${_challenge.display}" ' + 'answer=${_challenge.answer}', + name: 'WakeAlarmRing', + ); } /// Called by the QR scanner when a code is decoded. @@ -91,27 +105,23 @@ class _RingScreenState extends State { @override void dispose() { _scheduler.clearNfcListener(); - _flashTimer?.cancel(); _input.dispose(); _elapsed.stop(); super.dispose(); } - void _startFlashTimerIfNeeded() { - if (_challenge.kind != ChallengeKind.flash) return; - _flashTimer?.cancel(); - _flashTimer = Timer(const Duration(seconds: kDismissFlashSeconds), () { - if (mounted) setState(() => _codeVisible = false); - }); - } - Future _trySteps() async { final result = await _proof.checkSteps(_elapsed.elapsed); + developer.log( + 'steps satisfied=${result.satisfied} detail=${result.detail}', + name: 'WakeAlarmRing', + ); if (!mounted) return; setState(() { _inRoom = result.satisfied; _status = result.detail; }); + if (result.satisfied) _logChallenge(); } void _submit() { @@ -119,10 +129,8 @@ class _RingScreenState extends State { setState(() { _status = 'Wrong — here is another one'; _challenge = _generator.next(); - _codeVisible = true; _input.clear(); }); - _startFlashTimerIfNeeded(); return; } setState(() => _status = 'Dismissed'); @@ -133,7 +141,9 @@ class _RingScreenState extends State { /// /// Ordered: the sound must stop even if closing the window fails. Future _finishDismiss() async { + developer.log('dismiss: stopping ring', name: 'WakeAlarmRing'); await _scheduler.dismiss(); + developer.log('dismiss: closing screen', name: 'WakeAlarmRing'); await _scheduler.closeRingScreen(); } @@ -183,7 +193,9 @@ class _RingScreenState extends State { Text(_challenge.hint, style: theme.textTheme.bodyLarge), const SizedBox(height: 12), Text( - _codeVisible ? _challenge.display : '••••••', + // Always visible: a challenge that hides itself is unsolvable when the + // user reaches the screen seconds later, having walked to another room. + _challenge.display, textAlign: TextAlign.center, style: theme.textTheme.displaySmall, ), diff --git a/phone_app/lib/services/challenge.dart b/phone_app/lib/services/challenge.dart index 1b0875c..79bb957 100644 --- a/phone_app/lib/services/challenge.dart +++ b/phone_app/lib/services/challenge.dart @@ -91,15 +91,18 @@ class ChallengeGenerator { final Random _random; - /// Picks one of the five kinds at random and generates an instance. + /// Picks a challenge kind at random and generates an instance. + /// + /// [flash] is deliberately **excluded**. It hides the code after + /// [kDismissFlashSeconds], and on the phone that is unsolvable in practice: + /// the ring screen is reached by walking to another room and tapping a tag, + /// so the code has usually already vanished by the time the user is looking + /// at the screen — reported from the phone on 2026-08-27 as "impossible to + /// actually do". Every challenge the alarm shows must stay on screen until + /// it is answered. [flash] is kept as a constructor for the PC half's + /// parity tests, but nothing routes to it here. Challenge next() { - final builders = [ - math, - flash, - sort, - sequence, - reverse, - ]; + final builders = [math, sort, sequence, reverse]; return builders[_random.nextInt(builders.length)](); } diff --git a/phone_app/test/services/challenge_test.dart b/phone_app/test/services/challenge_test.dart index c6bad37..837b31c 100644 --- a/phone_app/test/services/challenge_test.dart +++ b/phone_app/test/services/challenge_test.dart @@ -142,12 +142,28 @@ void main() { expect(c.answer, word.split('').reversed.join()); }); - test('next() can produce every kind', () { + test('next() never returns a challenge that hides itself', () { + // flash disappears after kDismissFlashSeconds, which is unsolvable on + // the phone: the user reaches the ring screen only after walking to + // another room and tapping a tag. final kinds = {}; for (var i = 0; i < 400; i++) { kinds.add(ChallengeGenerator().next().kind); } - expect(kinds, hasLength(ChallengeKind.values.length)); + expect(kinds, isNot(contains(ChallengeKind.flash))); + }); + + test('next() produces every kind that is safe to show', () { + final kinds = {}; + for (var i = 0; i < 400; i++) { + kinds.add(ChallengeGenerator().next().kind); + } + expect(kinds, { + ChallengeKind.math, + ChallengeKind.sort, + ChallengeKind.sequence, + ChallengeKind.reverse, + }); }); }); }