Remove the flash challenge; revert the MethodChannel split that regressed NFC

Two things, one of them an undo of my own change.

The flash challenge is gone from the phone's rotation. 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
already vanished by the time the user is looking at it. Reported from the phone
as "invisible - impossible to actually do". Every challenge the alarm shows now
stays on screen until it is answered, and a test asserts next() never returns
one that hides itself. The constructor stays for the PC half's parity tests;
nothing routes to it.

The two-channel split is reverted. I introduced it to fix a tag reaching Kotlin
but not Dart, and pointed the push channel at the call channel — which broke
NFC *and* the step counter, both of which had been confirmed working on the
phone one build earlier. The user caught it: "they worked previously so your
changes regressed it". The split was a theory that was never shown to be
necessary; the single channel from 08b44b4 is restored unchanged.

Lesson worth keeping: when hardware has just confirmed a path works, the next
change should be the smallest possible delta on top of that exact state, not a
restructure bundled with a fix.

flutter test: 70 pass. dart analyze lib/ test/: clean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EckWmyfetYq3Mi8MAnUudh
This commit is contained in:
Krzysztof Rudnicki 2026-08-27 22:45:35 +02:00
parent 859e6effbb
commit a845cac883
5 changed files with 85 additions and 35 deletions

View File

@ -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)

View File

@ -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<void> 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);
});
}

View File

@ -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<RingScreen> {
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<RingScreen> {
_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<RingScreen> {
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<RingScreen> {
@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<void> _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<RingScreen> {
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<RingScreen> {
///
/// Ordered: the sound must stop even if closing the window fails.
Future<void> _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<RingScreen> {
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,
),

View File

@ -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 = <Challenge Function()>[
math,
flash,
sort,
sequence,
reverse,
];
final builders = <Challenge Function()>[math, sort, sequence, reverse];
return builders[_random.nextInt(builders.length)]();
}

View File

@ -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 = <ChallengeKind>{};
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 = <ChallengeKind>{};
for (var i = 0; i < 400; i++) {
kinds.add(ChallengeGenerator().next().kind);
}
expect(kinds, <ChallengeKind>{
ChallengeKind.math,
ChallengeKind.sort,
ChallengeKind.sequence,
ChallengeKind.reverse,
});
});
});
}