wake-alarm/phone_app/lib/services/room_proof_settings.dart
Krzysztof kuhy Rudnicki cb50e58a56 Phase 4/3: NFC room proof, runtime permissions, non-blocking step reads
Three defects found by reviewing the emulator run, all on the dismiss path:

1. ACTIVITY_RECOGNITION was declared but never requested at runtime (API 29+).
   Denied, TYPE_STEP_COUNTER delivers nothing, so checkSteps() reports "sensor
   unavailable" forever. With steps the only wired proof, that made the alarm
   *undismissable*, not merely degraded. MainActivity now requests it
   alongside POST_NOTIFICATIONS.

2. checkNfc/checkQr were dead code — no reader, no channel method, no
   registration — so steps was the only reachable proof. That inverts the
   decision ("NFC or QR qualifies; steps alone does NOT") and removes exactly
   the redundancy whose purpose is that one failing mechanism at 06:00 must
   never block dismissal. Added NfcReader (hand-written on the existing
   channel, NfcAdapter.enableReaderMode so the tap does not restart the
   activity and reset the challenge) plus RoomProofSettings for the registered
   tag/QR. mobile_scanner stays the droppable extra.

3. StepCounter blocked the platform main thread up to 3s per read, called from
   the MethodChannel handler — jank-to-ANR on the ring screen. Now registers a
   listener and serves the last sample; reads never block.

Also: raiseAlarmVolume() is guarded. setStreamVolume can throw under a zen
rule, and by then startForeground() has already posted — a failed volume bump
must not take the ring down with it.

Verified on the API 36 AVD with the real app widget and route (the earlier
probe rendered its own UI, so it proved the Kotlin path but nothing about
/ring): screen Asleep -> Awake, AlarmActivity top resumed over the lockscreen,
RingScreen showing the room-proof gate. New widget tests cover the /ring route
resolving and the challenge staying gated behind the room proof.

flutter test: 57 pass. dart analyze: clean. Zero Python files touched.

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

44 lines
1.5 KiB
Dart

import 'package:shared_preferences/shared_preferences.dart';
/// Persisted room-proof configuration: the registered NFC tag and QR payload.
///
/// Registering nothing means that mechanism cannot satisfy the proof — it does
/// not mean everything passes. See `RoomProof` for why redundancy matters.
class RoomProofSettings {
/// Creates settings with the registered values, if any.
const RoomProofSettings({this.nfcTagId, this.qrPayload});
/// SharedPreferences key for the registered NFC tag id.
static const String kNfcTagKey = 'roomProof.nfcTagId';
/// SharedPreferences key for the expected QR payload.
static const String kQrKey = 'roomProof.qrPayload';
/// Lower-case hex id of the tag taped up in the other room.
final String? nfcTagId;
/// Text encoded in the printed QR code in the other room.
final String? qrPayload;
/// Loads the stored configuration.
static Future<RoomProofSettings> load() async {
final prefs = await SharedPreferences.getInstance();
return RoomProofSettings(
nfcTagId: prefs.getString(kNfcTagKey),
qrPayload: prefs.getString(kQrKey),
);
}
/// Stores [tagId] as the registered NFC tag.
static Future<void> saveNfcTag(String tagId) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(kNfcTagKey, tagId);
}
/// Stores [payload] as the expected QR code text.
static Future<void> saveQrPayload(String payload) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(kQrKey, payload);
}
}