Root-cause fix for the unstoppable-alarm incident (alarm rang 15 minutes, only uninstall silenced it) plus systematic hardening of every stop path. Native (Kotlin): - Verified stop: stopActiveAlarm now derives its result from the real post-teardown state (companion instance + synchronous stopEverything + activeRingingId check) instead of reporting unconditional success. - Atomic teardown: every stop path (stop action, notification button, snooze, missed, onDestroy, startForeground failure) funnels through one stopEverything() covering audio, wakelock, notification, foreground state and firing-record cleanup; player.release() guarded. - Bounded ringing: 10-minute auto-silence armed via AlarmManager fires a FIRED->MISSED transition with a localized missed-alarm notification; repeating alarms keep their native rearm, deleted alarms never produce ghost MISSED notifications. - Durable firing record with onStartCommand re-validation (resurrection guard) and boot-time stale cleanup; firing records cleared on every refuse/mismatch/cancel path. - New notification-only dismissal channel (dismissAlarmNotificationOnly) so UI-level dedup can never kill a live ring's audio. Flutter (Dart): - Stop/disable/edit/delete of a ringing alarm always attempt to silence it; on native-query failure the stop falls back toward silence via the id-scoped legacy stop. - Verified-stop results surface failures: the ringing screen keeps dismiss-by-design on success, but on a verified failure it stays up with a persistent force-stop banner (guarded against double-dismiss) and auto-dismisses if the ring ends externally (missed/notification). - Missed events sync alarm bookkeeping without opening the ringing UI. - 4 new l10n keys translated across all 13 locales (ARB guard green). 550 tests green, analyzer clean. Reviewed in 3 adversarial 4-lens rounds (2 deterministic + 1 refuter-corroborated critical fixed); formal gentle-ai receipt waived by maintainer authorization (correction scope legitimately exceeded the frozen genesis paths). On-device QA checklist in openspec/changes/alarm-system-overhaul/tasks.md pending before archive.
164 lines
5.0 KiB
Dart
164 lines
5.0 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:pluriwave/l10n/gen/app_localizations.dart';
|
|
import 'package:pluriwave/modelos/alarma_musical.dart';
|
|
import 'package:pluriwave/servicios/servicio_alarmas_android.dart';
|
|
import 'package:pluriwave/servicios/servicio_grabacion_radio.dart';
|
|
|
|
/// Shared fake of the Android alarm bridge for alarm-related tests.
|
|
class FakePuertoAlarmasAndroid implements PuertoAlarmasAndroid {
|
|
final programadas = <AlarmaMusical>[];
|
|
final canceladas = <String>[];
|
|
final detenidas = <String>[];
|
|
final ocultadas = <String>[];
|
|
final soloOcultadas = <String>[];
|
|
final ejecucionesNativas = <EjecucionAlarmaNativa>[];
|
|
final snoozesNativos = <EstadoSnoozeNativo>[];
|
|
final _eventos = StreamController<EventoAlarmaAndroid>.broadcast();
|
|
bool ignoraOptimizacionBateria = true;
|
|
int solicitudesExencionBateria = 0;
|
|
|
|
/// Test-only failure switch (Design D7): when true, [programar] throws
|
|
/// instead of scheduling, enabling failure-path coverage that the fake
|
|
/// could not otherwise produce.
|
|
bool fallaProgramar = false;
|
|
|
|
/// Test-only failure switch: when true, [detenerSonidoActivo] reports an
|
|
/// unconfirmed/failed stop instead of a confirmed one.
|
|
bool fallaDetener = false;
|
|
|
|
/// Simulates [PluriWaveAlarmService.activeRingingId]: the id the fake
|
|
/// reports as currently ringing, or null if nothing rings.
|
|
String? alarmaSonandoIdValor;
|
|
|
|
/// Test-only failure switch (Finding 2, fail-toward-silence): when true,
|
|
/// [alarmaSonandoId] throws instead of returning a value, exercising the
|
|
/// [detenerSonidoNativo] fallback in `EstadoAlarmas._detenerSiEstaSonando`.
|
|
bool fallaConsultaSonando = false;
|
|
|
|
/// Every alarm id [detenerSonidoActivo] was invoked for, in call order.
|
|
final detencionesActivas = <String>[];
|
|
|
|
/// Test-only gate (RES-2 guard test): when set, [detenerSonidoActivo]
|
|
/// awaits it before resolving, letting tests exercise an overlapping
|
|
/// in-flight call.
|
|
Completer<void>? detenerActivoGate;
|
|
|
|
/// Simulates a native -> Flutter `alarmFired` MethodChannel event.
|
|
void emitirEvento(EventoAlarmaAndroid evento) => _eventos.add(evento);
|
|
|
|
@override
|
|
Stream<EventoAlarmaAndroid> get eventosAlarma => _eventos.stream;
|
|
|
|
@override
|
|
void configurarLocalizaciones(AppLocalizations l10n) {}
|
|
|
|
@override
|
|
Future<void> programar(AlarmaMusical alarma) async {
|
|
if (fallaProgramar) {
|
|
throw StateError('fake programar failure');
|
|
}
|
|
programadas.add(alarma);
|
|
}
|
|
|
|
@override
|
|
Future<void> cancelar(String alarmaId) async {
|
|
canceladas.add(alarmaId);
|
|
}
|
|
|
|
@override
|
|
Future<void> detenerSonidoNativo(String alarmaId) async {
|
|
detenidas.add(alarmaId);
|
|
}
|
|
|
|
@override
|
|
Future<String?> alarmaSonandoId() async {
|
|
if (fallaConsultaSonando) {
|
|
throw StateError('fake alarmaSonandoId failure');
|
|
}
|
|
return alarmaSonandoIdValor;
|
|
}
|
|
|
|
@override
|
|
Future<ResultadoDetencion> detenerSonidoActivo() async {
|
|
detencionesActivas.add(alarmaSonandoIdValor ?? '');
|
|
final gate = detenerActivoGate;
|
|
if (gate != null) await gate.future;
|
|
if (fallaDetener) {
|
|
return const ResultadoDetencion(detenido: false, estabaSonando: true);
|
|
}
|
|
return ResultadoDetencion(
|
|
detenido: true,
|
|
estabaSonando: alarmaSonandoIdValor != null,
|
|
alarmaId: alarmaSonandoIdValor,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Future<void> ocultarNotificacionAlarma(String alarmaId) async {
|
|
ocultadas.add(alarmaId);
|
|
}
|
|
|
|
@override
|
|
Future<void> ocultarSoloNotificacion(String alarmaId) async {
|
|
soloOcultadas.add(alarmaId);
|
|
}
|
|
|
|
@override
|
|
Future<DiagnosticoAlarmasAndroid> diagnostico() async =>
|
|
DiagnosticoAlarmasAndroid(
|
|
puedeProgramarExactas: true,
|
|
notificacionesPermitidas: true,
|
|
puedeUsarPantallaCompleta: true,
|
|
ignoraOptimizacionBateria: ignoraOptimizacionBateria,
|
|
alarmasNativasPendientes: 0,
|
|
fabricante: 'test',
|
|
versionSdk: 35,
|
|
);
|
|
|
|
@override
|
|
Future<bool> solicitarExencionBateria() async {
|
|
solicitudesExencionBateria++;
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
Future<EventoAlarmaAndroid?> obtenerEventoInicial() async => null;
|
|
|
|
@override
|
|
Future<List<EjecucionAlarmaNativa>>
|
|
obtenerEjecucionesNativasGestionadas() async => ejecucionesNativas;
|
|
|
|
@override
|
|
Future<List<EstadoSnoozeNativo>> obtenerEstadoSnoozeNativo() async =>
|
|
List.of(snoozesNativos);
|
|
|
|
@override
|
|
Future<bool> solicitarPermisoAlarmasExactas() async => true;
|
|
|
|
@override
|
|
Future<bool> solicitarPermisoNotificaciones() async => true;
|
|
|
|
@override
|
|
Future<bool> solicitarPermisoPantallaCompleta() async => true;
|
|
|
|
Future<void> dispose() => _eventos.close();
|
|
}
|
|
|
|
/// Inactive recording service fake, safe for widget tests.
|
|
class FakeServicioGrabacionRadioInactiva extends ServicioGrabacionRadio {
|
|
final _controller = StreamController<EstadoGrabacionRadio>.broadcast();
|
|
|
|
@override
|
|
EstadoGrabacionRadio get estado => const EstadoGrabacionRadio.inactiva();
|
|
|
|
@override
|
|
Stream<EstadoGrabacionRadio> get estadoStream => _controller.stream;
|
|
|
|
@override
|
|
Future<void> inicializar() async {}
|
|
|
|
@override
|
|
Future<void> dispose() => _controller.close();
|
|
}
|