feat(eq): android auto custom equalizer and robust device detection
- MainActivity: onListen re-emits the current active device and registers the audio device callback idempotently, so recreated activities resync instead of freezing the active-device id on a disconnected device. - servicio_dispositivo_audio: resubscribir() re-opens the event channel; estado_ecualizador exposes refrescarDispositivoActual() with an in-flight guard, invoked on app resume and when opening advanced EQ options, clearing stale green-dot device selections. - navegacion_auto/servicio_audio: new 'Personalizado' browse tree in Android Auto (5 band folders, 13 gain steps each) applied live via setBanda; preset and gain taps persist at device level when multi-device EQ is active and respect station/matrix overrides, with apply-before-persist ordering and children-changed notifications. - l10n: regenerate stale generated localizations; add rxdart as direct dependency for the subscribeToChildren override.
This commit is contained in:
@@ -7,12 +7,15 @@ import 'package:flutter/foundation.dart' show visibleForTesting;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
import '../estado/orden_emisoras.dart';
|
||||
import '../modelos/dispositivo_audio.dart';
|
||||
import '../modelos/emisora.dart';
|
||||
import '../modelos/grupo_favoritos.dart';
|
||||
import '../modelos/pista_local.dart';
|
||||
import '../modelos/preset_ecualizador.dart';
|
||||
import 'musica_local_auto.dart';
|
||||
import 'persistencia_tolerante.dart';
|
||||
import 'servicio_dispositivo_audio.dart';
|
||||
import 'servicio_ecualizador.dart';
|
||||
import 'servicio_favoritos.dart';
|
||||
|
||||
/// Generic page slice over [items] (Design ADR-6): returns at most [tamano]
|
||||
@@ -43,6 +46,38 @@ const _prefijoPresetEq = 'eq_preset:';
|
||||
/// predicate.
|
||||
bool esPresetMediaId(String id) => id.startsWith(_prefijoPresetEq);
|
||||
|
||||
/// Custom-EQ band folder media-id prefix (feature auto-custom-eq):
|
||||
/// `eq_banda:<indice>`. Collision-free against [_prefijoPresetEq] and
|
||||
/// [_prefijoGananciaEq] — the three `eq_` prefixes diverge at index 3
|
||||
/// (`b` vs `p` vs `g`), so no `startsWith` check ever matches a sibling's id.
|
||||
const _prefijoBandaEq = 'eq_banda:';
|
||||
|
||||
/// Whether [id] identifies a custom-EQ band folder (feature auto-custom-eq).
|
||||
bool esBandaEqMediaId(String id) => id.startsWith(_prefijoBandaEq);
|
||||
|
||||
/// Builds the `eq_banda:<indice>` media id for band [indice] (feature
|
||||
/// auto-custom-eq) — single authority for the id shape, shared by the tree
|
||||
/// builder and the handler's children-changed notification.
|
||||
String idBandaEq(int indice) => '$_prefijoBandaEq$indice';
|
||||
|
||||
/// Custom-EQ gain leaf media-id prefix (feature auto-custom-eq):
|
||||
/// `eq_gain:<indice>:<db>`. Collision-free against [_prefijoBandaEq] and
|
||||
/// [_prefijoPresetEq] (see [_prefijoBandaEq]'s divergence note).
|
||||
const _prefijoGananciaEq = 'eq_gain:';
|
||||
|
||||
/// Whether [id] identifies a custom-EQ gain leaf item (feature
|
||||
/// auto-custom-eq).
|
||||
bool esGananciaEqMediaId(String id) => id.startsWith(_prefijoGananciaEq);
|
||||
|
||||
/// Number of fixed EQ bands (`PresetEcualizador` asserts exactly 5).
|
||||
const _numBandasEq = 5;
|
||||
|
||||
/// Frequency labels for the 5 EQ bands, in the same order as
|
||||
/// `PresetEcualizador.bandas` (60Hz, 250Hz, 1kHz, 4kHz, 16kHz — the model's
|
||||
/// documented band layout, same frequencies the phone's
|
||||
/// `EcualizadorWidget._etiquetas` renders).
|
||||
const etiquetasBandasEq = ['60 Hz', '250 Hz', '1 kHz', '4 kHz', '16 kHz'];
|
||||
|
||||
/// Local-track media-id prefix (Design "media-id scheme"), collision-free
|
||||
/// against [_prefijoEmisora], [_prefijoPresetEq], `grupo:` and the bare
|
||||
/// folder id constants. Top-level (not a [ConstructorArbolAuto] member),
|
||||
@@ -208,6 +243,11 @@ class ConstructorArbolAuto {
|
||||
/// generic station-list `hijos()` path.
|
||||
static const idEcualizador = 'ecualizador';
|
||||
|
||||
/// Browsable "Personalizado" folder id under [idEcualizador] (feature
|
||||
/// auto-custom-eq). Deliberately NOT added to [_idsCarpetas] — routed by
|
||||
/// its own dedicated branch in `getChildren`, like [idEcualizador] itself.
|
||||
static const idEqPersonalizado = 'eq_custom';
|
||||
|
||||
/// Root folder id for the local-music browsable root (Design "media-id
|
||||
/// scheme"). Deliberately NOT added to [_idsCarpetas] — it has its own
|
||||
/// dedicated branch (`hijosMusicaLocal`), not the generic station-list
|
||||
@@ -736,6 +776,52 @@ class ConstructorArbolAuto {
|
||||
List<MediaItem> presetsEq(List<PresetEcualizador> presets) =>
|
||||
presets.map(itemPresetEq).toList();
|
||||
|
||||
/// The browsable "Personalizado" folder appended after the fixed preset
|
||||
/// leaves under `Ecualizador` (feature auto-custom-eq). Hardcoded Spanish
|
||||
/// label, matching every other car-tree label in this file — never routed
|
||||
/// through `AppLocalizations` (see [_tituloMasLocal]'s precedent).
|
||||
MediaItem itemEqPersonalizado() =>
|
||||
_carpeta(idEqPersonalizado, 'Personalizado');
|
||||
|
||||
/// The 5 per-band browsable folders under `Personalizado` (feature
|
||||
/// auto-custom-eq): id `eq_banda:<indice>`, title
|
||||
/// `<frecuencia> · <ganancia actual>` (e.g. `'60 Hz · +3 dB'`) so the
|
||||
/// driver sees the effective custom gains at a glance. [actual] is the
|
||||
/// persisted/effective custom preset resolved by the caller
|
||||
/// ([presetPersonalizadoEfectivo]).
|
||||
List<MediaItem> bandasEq(PresetEcualizador actual) => [
|
||||
for (
|
||||
var i = 0;
|
||||
i < actual.bandas.length && i < etiquetasBandasEq.length;
|
||||
i++
|
||||
)
|
||||
_carpeta(
|
||||
idBandaEq(i),
|
||||
'${etiquetasBandasEq[i]} · ${formatearGananciaEq(actual.bandas[i])}',
|
||||
),
|
||||
];
|
||||
|
||||
/// The 13 playable gain leaves for band [indice] (feature auto-custom-eq):
|
||||
/// -12..+12 dB in steps of 2, id `eq_gain:<indice>:<db>`. The currently
|
||||
/// selected gain — when it falls on the 2 dB grid — is marked with a
|
||||
/// leading `● ` so the active value is visible while browsing. An
|
||||
/// out-of-range [indice] returns `[]`, never throws.
|
||||
List<MediaItem> gananciasBandaEq(int indice, PresetEcualizador actual) {
|
||||
if (indice < 0 || indice >= actual.bandas.length) return const [];
|
||||
final gananciaActual = actual.bandas[indice];
|
||||
return [
|
||||
for (var db = -12; db <= 12; db += 2)
|
||||
MediaItem(
|
||||
id: '$_prefijoGananciaEq$indice:$db',
|
||||
title:
|
||||
'${db.toDouble() == gananciaActual ? '● ' : ''}'
|
||||
'${formatearGananciaEq(db.toDouble())}',
|
||||
playable: true,
|
||||
extras: _contentStyleGrid,
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
/// Children of the `Favoritos` folder (Design "Ungrouped favorites stay as
|
||||
/// direct leaves at the Favoritos root"): non-empty custom-group folders
|
||||
/// (phone order, capped at [_maxGruposPorFavoritos]), followed by
|
||||
@@ -1221,6 +1307,79 @@ PresetEcualizador? resolverPresetEq(String id, List<PresetEcualizador> presets)
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Formats a gain in dB for the Auto tree (feature auto-custom-eq):
|
||||
/// explicit `+` for boosts, plain `0 dB` for neutral, ASCII `-` for cuts.
|
||||
/// Non-integer factory-preset gains keep one decimal (`'+1.5 dB'`) so band
|
||||
/// titles never lie about the effective value.
|
||||
String formatearGananciaEq(double db) {
|
||||
final esEntera = db == db.roundToDouble();
|
||||
final valor = esEntera ? db.round().toString() : db.toStringAsFixed(1);
|
||||
return db > 0 ? '+$valor dB' : '$valor dB';
|
||||
}
|
||||
|
||||
/// Parses an `eq_banda:<indice>` [id] into its band index (feature
|
||||
/// auto-custom-eq). Any other shape (no prefix, non-integer, outside the
|
||||
/// fixed [_numBandasEq]-band range) returns `null` instead of throwing.
|
||||
int? indiceBandaEqDesde(String id) {
|
||||
if (!esBandaEqMediaId(id)) return null;
|
||||
final indice = int.tryParse(id.substring(_prefijoBandaEq.length));
|
||||
if (indice == null || indice < 0 || indice >= _numBandasEq) return null;
|
||||
return indice;
|
||||
}
|
||||
|
||||
/// Parses an `eq_gain:<indice>:<db>` [id] into its `(indice, db)` pair
|
||||
/// (feature auto-custom-eq): the prefix is stripped by length, the remainder
|
||||
/// split on its single `:`. Any other shape (no prefix, missing/non-integer
|
||||
/// fields, index outside the band range, gain outside -12..+12) returns
|
||||
/// `null` instead of throwing.
|
||||
(int indice, double db)? gananciaEqDesde(String id) {
|
||||
if (!esGananciaEqMediaId(id)) return null;
|
||||
final resto = id.substring(_prefijoGananciaEq.length);
|
||||
final separador = resto.indexOf(':');
|
||||
if (separador <= 0) return null;
|
||||
final indice = int.tryParse(resto.substring(0, separador));
|
||||
final db = int.tryParse(resto.substring(separador + 1));
|
||||
if (indice == null || db == null) return null;
|
||||
if (indice < 0 || indice >= _numBandasEq) return null;
|
||||
if (db < -12 || db > 12) return null;
|
||||
return (indice, db.toDouble());
|
||||
}
|
||||
|
||||
/// Persistence-targeting decision for a car EQ action — preset tap or band
|
||||
/// change (feature auto-custom-eq): returns the deviceId to persist a
|
||||
/// DEVICE-level entry for, or `null` to fall back to the global principal.
|
||||
/// `null` cases mirror the phone hierarchy's own exclusions: multi-device
|
||||
/// toggle off, unknown device (query failed/timed out — [dispositivo] is
|
||||
/// `null`), built-in speaker (must keep falling through to L4 global), and
|
||||
/// composite-placeholder BT ids (ADR-6). Pure — testable without platform
|
||||
/// channels.
|
||||
String? dispositivoDestinoEq({
|
||||
required bool multiDeviceEnabled,
|
||||
required DispositivoAudio? dispositivo,
|
||||
}) {
|
||||
if (!multiDeviceEnabled || dispositivo == null) return null;
|
||||
if (dispositivo.tipo == TipoDispositivo.altavozInterno) return null;
|
||||
if (dispositivo.id.startsWith(prefijoPlaceholderBtName)) return null;
|
||||
return dispositivo.id;
|
||||
}
|
||||
|
||||
/// Resolves the custom-EQ preset the Auto tree displays and edits (feature
|
||||
/// auto-custom-eq): the device-level entry for [deviceId] when multi-device
|
||||
/// is enabled and one exists, the global principal otherwise. Station/matrix
|
||||
/// overrides are deliberately NOT consulted — the `Personalizado` tree
|
||||
/// displays and edits exactly the level a gain tap persists to
|
||||
/// ([dispositivoDestinoEq]), so what the driver sees is what a tap changes.
|
||||
PresetEcualizador presetPersonalizadoEfectivo({
|
||||
required ConfiguracionEcualizador config,
|
||||
required String? deviceId,
|
||||
}) {
|
||||
if (config.eqMultiDeviceEnabled && deviceId != null) {
|
||||
final porDispositivo = config.presetsDispositivo[deviceId];
|
||||
if (porDispositivo != null) return porDispositivo;
|
||||
}
|
||||
return config.principal;
|
||||
}
|
||||
|
||||
/// Pure per-station apply gate (Design ADR-5), mirroring
|
||||
/// `EstadoEcualizador.cambiarPresetPrincipal`'s exact logic
|
||||
/// (`estado_ecualizador.dart:302-304`): the new principal preset is applied
|
||||
@@ -1232,16 +1391,46 @@ bool debeAplicarPrincipalAhora({
|
||||
required Set<String> clavesPorEmisora,
|
||||
}) => uuidActual == null || !clavesPorEmisora.contains(uuidActual);
|
||||
|
||||
/// Apply-live gate for a DEVICE-targeted car selection (feature
|
||||
/// auto-custom-eq), extending [debeAplicarPrincipalAhora] with the matrix
|
||||
/// level of the phone hierarchy: the freshly persisted device-level preset
|
||||
/// is audible now unless the current station carries a per-station override
|
||||
/// or a `estación:dispositivoDestino` matrix entry shadows it.
|
||||
bool debeAplicarSeleccionAhora({
|
||||
required String? uuidActual,
|
||||
required Set<String> clavesPorEmisora,
|
||||
required Set<String> clavesMatriz,
|
||||
required String? deviceIdDestino,
|
||||
}) {
|
||||
if (uuidActual == null) return true;
|
||||
if (clavesPorEmisora.contains(uuidActual)) return false;
|
||||
if (deviceIdDestino != null &&
|
||||
clavesMatriz.contains('$uuidActual:$deviceIdDestino')) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Orchestrates an `eq_preset:<nombre>` selection from the car (Design
|
||||
/// "Data flow — a preset tap", ADR-3): resolves [id] via [resolverPresetEq],
|
||||
/// persists it as principal via [persistirPrincipal], and conditionally
|
||||
/// applies it live via [aplicar] when [debeAplicarPrincipalAhora] allows it.
|
||||
/// persists it, and conditionally applies it live via [aplicar].
|
||||
///
|
||||
/// Persistence targeting (feature auto-custom-eq): when the optional
|
||||
/// [dispositivoDestino]/[persistirDispositivo] seams are provided and the
|
||||
/// destination resolves to a deviceId ([dispositivoDestinoEq]'s decision),
|
||||
/// the preset is persisted as a DEVICE-level entry — so the car selection
|
||||
/// sticks for the car's output device instead of being shadowed by the
|
||||
/// hierarchy's L3 lookup — and the live application is gated by
|
||||
/// [debeAplicarSeleccionAhora]. Otherwise (seams omitted, or destination
|
||||
/// `null`: toggle off, built-in, placeholder, query error/timeout) the
|
||||
/// original global path runs unchanged: [persistirPrincipal] +
|
||||
/// [debeAplicarPrincipalAhora].
|
||||
///
|
||||
/// This function's signature exposes ONLY the EQ persist/apply seams — it
|
||||
/// has NO parameter for `playMediaItem`, `mediaItem`, or `playbackState`, so
|
||||
/// there is no code path from a preset tap to playback (Design ADR-3,
|
||||
/// non-playback invariant enforced structurally, not by discipline). An
|
||||
/// unknown/stale [id] is a no-op: neither seam is invoked and no exception
|
||||
/// unknown/stale [id] is a no-op: no seam is invoked and no exception
|
||||
/// propagates (Spec "Unknown or stale preset id").
|
||||
Future<void> aplicarPresetPorMediaId(
|
||||
String id, {
|
||||
@@ -1250,9 +1439,34 @@ Future<void> aplicarPresetPorMediaId(
|
||||
required Future<Set<String>> Function() clavesPorEmisora,
|
||||
required Future<void> Function(PresetEcualizador) persistirPrincipal,
|
||||
required Future<void> Function(PresetEcualizador) aplicar,
|
||||
Future<String?> Function()? dispositivoDestino,
|
||||
Future<void> Function(String deviceId, PresetEcualizador preset)?
|
||||
persistirDispositivo,
|
||||
Future<Set<String>> Function()? clavesMatriz,
|
||||
}) async {
|
||||
final preset = resolverPresetEq(id, presets);
|
||||
if (preset == null) return;
|
||||
|
||||
final destino =
|
||||
(dispositivoDestino == null || persistirDispositivo == null)
|
||||
? null
|
||||
: await dispositivoDestino();
|
||||
if (destino != null) {
|
||||
// Apply-first ordering: if the live application throws, nothing has
|
||||
// been persisted yet, so audible and persisted state cannot diverge.
|
||||
if (debeAplicarSeleccionAhora(
|
||||
uuidActual: uuidActual,
|
||||
clavesPorEmisora: await clavesPorEmisora(),
|
||||
clavesMatriz:
|
||||
clavesMatriz == null ? const <String>{} : await clavesMatriz(),
|
||||
deviceIdDestino: destino,
|
||||
)) {
|
||||
await aplicar(preset);
|
||||
}
|
||||
await persistirDispositivo!(destino, preset);
|
||||
return;
|
||||
}
|
||||
|
||||
await persistirPrincipal(preset);
|
||||
if (debeAplicarPrincipalAhora(
|
||||
uuidActual: uuidActual,
|
||||
@@ -1262,6 +1476,75 @@ Future<void> aplicarPresetPorMediaId(
|
||||
}
|
||||
}
|
||||
|
||||
/// Orchestrates an `eq_gain:<indice>:<db>` selection from the car (feature
|
||||
/// auto-custom-eq): parses [id] via [gananciaEqDesde], resolves the custom
|
||||
/// base preset for the persistence target ([presetPersonalizadoEfectivo]
|
||||
/// over [cargarConfig]'s snapshot), replaces the single band (the result is
|
||||
/// always named `Personalizado` via `copyWithBandas`), persists it at DEVICE
|
||||
/// level when [dispositivoDestino] yields a deviceId — global principal
|
||||
/// otherwise, including the headless error/timeout fallback — and applies
|
||||
/// the band live via [aplicarBanda] (the handler's `setBanda`, itself a
|
||||
/// no-op while the EQ engine is unavailable).
|
||||
///
|
||||
/// Live application is gated by [debeAplicarSeleccionAhora] over the same
|
||||
/// optional [uuidActual]/[clavesPorEmisora]/[clavesMatriz] seams as
|
||||
/// [aplicarPresetPorMediaId] (omitted seams keep the legacy always-apply
|
||||
/// behavior), and runs BEFORE persistence so a throwing apply cannot leave
|
||||
/// persisted and audible state divergent.
|
||||
///
|
||||
/// Same non-playback structural invariant as [aplicarPresetPorMediaId]: no
|
||||
/// playback seam exists in this signature. A malformed/out-of-range [id] or
|
||||
/// a failing [cargarConfig] degrades to a no-op — no seam is invoked and no
|
||||
/// exception propagates.
|
||||
Future<void> aplicarGananciaPorMediaId(
|
||||
String id, {
|
||||
required Future<ConfiguracionEcualizador> Function() cargarConfig,
|
||||
required Future<String?> Function() dispositivoDestino,
|
||||
required Future<void> Function(String deviceId, PresetEcualizador preset)
|
||||
persistirDispositivo,
|
||||
required Future<void> Function(PresetEcualizador preset) persistirPrincipal,
|
||||
required Future<void> Function(int indice, double db) aplicarBanda,
|
||||
String? uuidActual,
|
||||
Future<Set<String>> Function()? clavesPorEmisora,
|
||||
Future<Set<String>> Function()? clavesMatriz,
|
||||
}) async {
|
||||
final ganancia = gananciaEqDesde(id);
|
||||
if (ganancia == null) return;
|
||||
final (indice, db) = ganancia;
|
||||
|
||||
final ConfiguracionEcualizador config;
|
||||
try {
|
||||
config = await cargarConfig();
|
||||
} catch (_) {
|
||||
return;
|
||||
}
|
||||
|
||||
final destino = await dispositivoDestino();
|
||||
final base = presetPersonalizadoEfectivo(config: config, deviceId: destino);
|
||||
final bandas = List<double>.from(base.bandas);
|
||||
bandas[indice] = db;
|
||||
final modificado = base.copyWithBandas(bandas);
|
||||
|
||||
// Apply-first ordering (same rationale as [aplicarPresetPorMediaId]'s
|
||||
// device branch): a throwing apply must not leave persisted state ahead
|
||||
// of the audible one.
|
||||
if (debeAplicarSeleccionAhora(
|
||||
uuidActual: uuidActual,
|
||||
clavesPorEmisora:
|
||||
clavesPorEmisora == null ? const <String>{} : await clavesPorEmisora(),
|
||||
clavesMatriz:
|
||||
clavesMatriz == null ? const <String>{} : await clavesMatriz(),
|
||||
deviceIdDestino: destino,
|
||||
)) {
|
||||
await aplicarBanda(indice, db);
|
||||
}
|
||||
if (destino != null) {
|
||||
await persistirDispositivo(destino, modificado);
|
||||
} else {
|
||||
await persistirPrincipal(modificado);
|
||||
}
|
||||
}
|
||||
|
||||
/// Local, cold-start-safe implementation of [FuenteEmisorasAuto] (Design
|
||||
/// "getChildren data source"). Reads favourites from SQLite and custom
|
||||
/// stations from the tolerant JSON file directly — both loadable without
|
||||
|
||||
@@ -4,10 +4,13 @@ import 'dart:ui' show Locale;
|
||||
|
||||
import 'package:audio_service/audio_service.dart';
|
||||
import 'package:flutter/foundation.dart' show visibleForTesting;
|
||||
import 'package:flutter/services.dart' show MethodChannel;
|
||||
import 'package:just_audio/just_audio.dart';
|
||||
import 'package:rxdart/rxdart.dart' show BehaviorSubject, ValueStream;
|
||||
|
||||
import '../l10n/display_names.dart';
|
||||
import '../l10n/gen/app_localizations.dart';
|
||||
import '../modelos/dispositivo_audio.dart';
|
||||
import '../modelos/emisora.dart';
|
||||
import '../modelos/pista_local.dart';
|
||||
import '../modelos/preset_ecualizador.dart';
|
||||
@@ -16,6 +19,7 @@ import 'controlador_reconexion.dart';
|
||||
import 'musica_local_auto.dart';
|
||||
import 'navegacion_auto.dart';
|
||||
import 'servicio_audio_session.dart';
|
||||
import 'servicio_dispositivo_audio.dart';
|
||||
import 'servicio_ecualizador.dart';
|
||||
|
||||
/// Estado de reproducción expuesto al UI.
|
||||
@@ -909,6 +913,10 @@ class PluriWaveAudioHandler extends BaseAudioHandler
|
||||
await _androidAudioSessionIdSub?.cancel();
|
||||
await _player.dispose();
|
||||
await _androidAudioSessionIdController.close();
|
||||
for (final subject in _hijosSubjects.values) {
|
||||
await subject.close();
|
||||
}
|
||||
_hijosSubjects.clear();
|
||||
}
|
||||
|
||||
Emisora _emisoraDesdeMediaItem(MediaItem mediaItem) {
|
||||
@@ -925,6 +933,92 @@ class PluriWaveAudioHandler extends BaseAudioHandler
|
||||
// ── Android Auto browsing (thin delegation to navegacion_auto.dart's
|
||||
// already-tested pure logic — Design "getChildren data source") ─────────
|
||||
|
||||
/// One-shot device-query channel (feature auto-custom-eq): the SAME
|
||||
/// method channel `ServicioDispositivoAudioReal` talks to, but method
|
||||
/// calls only — opening a second EventChannel subscription here would
|
||||
/// steal the phone-side service's Dart stream handler.
|
||||
static const _canalDispositivos = MethodChannel('pluriwave/audio_devices');
|
||||
|
||||
/// Short timeout for the device query: on a headless Auto bind no
|
||||
/// Activity (and thus no channel handler) exists, and a car tap must fall
|
||||
/// back to global persistence instead of hanging.
|
||||
static const _timeoutConsultaDispositivo = Duration(seconds: 2);
|
||||
|
||||
/// Fresh active-output-device query for the car EQ paths. Returns `null`
|
||||
/// on ANY failure (missing handler while headless, timeout, malformed
|
||||
/// map) so callers degrade to global persistence — never a crash.
|
||||
Future<DispositivoAudio?> _dispositivoActivoAuto() async {
|
||||
try {
|
||||
final raw = await _canalDispositivos
|
||||
.invokeMethod<Map<dynamic, dynamic>>('getActiveDevice')
|
||||
.timeout(_timeoutConsultaDispositivo);
|
||||
if (raw == null) return null;
|
||||
return ServicioDispositivoAudioReal.dispositivoDesdeMapa(
|
||||
Map<String, dynamic>.from(raw),
|
||||
);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolves the persistence target for a car EQ action (feature
|
||||
/// auto-custom-eq): a deviceId for a DEVICE-level write, `null` for the
|
||||
/// global principal (toggle off, built-in speaker, placeholder id, or the
|
||||
/// headless error/timeout fallback).
|
||||
Future<String?> _dispositivoDestinoEqAuto(
|
||||
ServicioEcualizador servicio,
|
||||
) async {
|
||||
try {
|
||||
final config = await servicio.cargar();
|
||||
if (!config.eqMultiDeviceEnabled) return null;
|
||||
return dispositivoDestinoEq(
|
||||
multiDeviceEnabled: config.eqMultiDeviceEnabled,
|
||||
dispositivo: await _dispositivoActivoAuto(),
|
||||
);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// The custom preset the Auto tree shows and edits right now (feature
|
||||
/// auto-custom-eq): the device-level entry for the current output device
|
||||
/// when multi-device is on, the global principal otherwise — resolved
|
||||
/// from persistence so a headless bind (no `EstadoEcualizador`) still
|
||||
/// reports honest gains.
|
||||
Future<PresetEcualizador> _presetPersonalizadoAuto() async {
|
||||
final servicio = ServicioEcualizador();
|
||||
final config = await servicio.cargar();
|
||||
final destino =
|
||||
config.eqMultiDeviceEnabled
|
||||
? dispositivoDestinoEq(
|
||||
multiDeviceEnabled: true,
|
||||
dispositivo: await _dispositivoActivoAuto(),
|
||||
)
|
||||
: null;
|
||||
return presetPersonalizadoEfectivo(config: config, deviceId: destino);
|
||||
}
|
||||
|
||||
/// Per-parent children-changed subjects (feature auto-custom-eq):
|
||||
/// audio_service subscribes to [subscribeToChildren]'s stream the first
|
||||
/// time the platform loads a parent's children and translates every later
|
||||
/// emission into a native `notifyChildrenChanged`, making the car
|
||||
/// re-request `getChildren` so band titles and the selection mark refresh
|
||||
/// right after a gain tap.
|
||||
final Map<String, BehaviorSubject<Map<String, dynamic>>> _hijosSubjects = {};
|
||||
|
||||
@override
|
||||
ValueStream<Map<String, dynamic>> subscribeToChildren(String parentMediaId) =>
|
||||
_hijosSubjects.putIfAbsent(
|
||||
parentMediaId,
|
||||
() => BehaviorSubject.seeded(<String, dynamic>{}),
|
||||
);
|
||||
|
||||
/// Emits a children-changed notification for [parentMediaId] — a no-op
|
||||
/// until the platform has browsed that parent at least once.
|
||||
void _notificarHijosCambiados(String parentMediaId) {
|
||||
_hijosSubjects[parentMediaId]?.add(<String, dynamic>{});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<MediaItem>> getChildren(
|
||||
String parentMediaId, [
|
||||
@@ -939,7 +1033,21 @@ class PluriWaveAudioHandler extends BaseAudioHandler
|
||||
return constructor.raiz(incluirMusicaLocal: incluirMusicaLocal);
|
||||
}
|
||||
if (parentMediaId == ConstructorArbolAuto.idEcualizador) {
|
||||
return constructor.presetsEq(PresetEcualizador.presets);
|
||||
return [
|
||||
...constructor.presetsEq(PresetEcualizador.presets),
|
||||
constructor.itemEqPersonalizado(),
|
||||
];
|
||||
}
|
||||
if (parentMediaId == ConstructorArbolAuto.idEqPersonalizado) {
|
||||
return constructor.bandasEq(await _presetPersonalizadoAuto());
|
||||
}
|
||||
if (esBandaEqMediaId(parentMediaId)) {
|
||||
final indice = indiceBandaEqDesde(parentMediaId);
|
||||
if (indice == null) return const [];
|
||||
return constructor.gananciasBandaEq(
|
||||
indice,
|
||||
await _presetPersonalizadoAuto(),
|
||||
);
|
||||
}
|
||||
final musicaLocal = await hijosMusicaLocal(
|
||||
parentMediaId,
|
||||
@@ -996,17 +1104,54 @@ class PluriWaveAudioHandler extends BaseAudioHandler
|
||||
// apply) — there is no playback parameter to inject here.
|
||||
if (esPresetMediaId(mediaId)) {
|
||||
final servicio = ServicioEcualizador();
|
||||
// Persistence targeting (feature auto-custom-eq): with multi-device
|
||||
// EQ on and a non-builtin output device active, the tap persists a
|
||||
// DEVICE-level entry so the selection sticks for the car's device
|
||||
// instead of being shadowed by the hierarchy; otherwise (or on any
|
||||
// headless query failure) it persists the global principal as
|
||||
// before.
|
||||
await aplicarPresetPorMediaId(
|
||||
mediaId,
|
||||
presets: PresetEcualizador.presets,
|
||||
uuidActual: emisoraActual?.uuid,
|
||||
clavesPorEmisora: () async =>
|
||||
(await servicio.cargar()).porEmisora.keys.toSet(),
|
||||
clavesMatriz: () async =>
|
||||
(await servicio.cargar()).presetsMatriz.keys.toSet(),
|
||||
dispositivoDestino: () => _dispositivoDestinoEqAuto(servicio),
|
||||
persistirDispositivo: servicio.guardarPresetDispositivo,
|
||||
persistirPrincipal: servicio.guardarPrincipal,
|
||||
aplicar: aplicarPreset,
|
||||
);
|
||||
return;
|
||||
}
|
||||
// Custom-EQ gain selection (feature auto-custom-eq): same
|
||||
// unconditional-return shape as the eq_preset branch above — an
|
||||
// `eq_gain:` id can never fall through to playback routing.
|
||||
if (esGananciaEqMediaId(mediaId)) {
|
||||
final servicio = ServicioEcualizador();
|
||||
await aplicarGananciaPorMediaId(
|
||||
mediaId,
|
||||
cargarConfig: servicio.cargar,
|
||||
dispositivoDestino: () => _dispositivoDestinoEqAuto(servicio),
|
||||
persistirDispositivo: servicio.guardarPresetDispositivo,
|
||||
persistirPrincipal: servicio.guardarPrincipal,
|
||||
aplicarBanda: setBanda,
|
||||
uuidActual: emisoraActual?.uuid,
|
||||
clavesPorEmisora: () async =>
|
||||
(await servicio.cargar()).porEmisora.keys.toSet(),
|
||||
clavesMatriz: () async =>
|
||||
(await servicio.cargar()).presetsMatriz.keys.toSet(),
|
||||
);
|
||||
// Refresh the affected browse nodes so the band title under
|
||||
// `Personalizado` and the `● ` selection mark reflect the new gain.
|
||||
final ganancia = gananciaEqDesde(mediaId);
|
||||
if (ganancia != null) {
|
||||
_notificarHijosCambiados(ConstructorArbolAuto.idEqPersonalizado);
|
||||
_notificarHijosCambiados(idBandaEq(ganancia.$1));
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Local-track playback (Design "Local Track Playback Reuses Existing
|
||||
// Pipeline", Spec "User selects a local track"): SECOND branch,
|
||||
// unconditional `return`, mirroring the eq_preset branch above — a
|
||||
|
||||
@@ -4,6 +4,14 @@ import 'package:flutter/services.dart';
|
||||
|
||||
import '../modelos/dispositivo_audio.dart';
|
||||
|
||||
/// Composite-placeholder id prefix (bt-device-identity ADR-6): marks a BT
|
||||
/// device whose real MAC is not yet known (BLUETOOTH_CONNECT denied or
|
||||
/// unresolved). Single source of truth for the marker shared by
|
||||
/// `EstadoEcualizador` and the Auto EQ persistence-targeting logic: ids with
|
||||
/// this prefix are transient and must never receive a device-level preset
|
||||
/// entry.
|
||||
const prefijoPlaceholderBtName = 'bt_a2dp:name:';
|
||||
|
||||
/// Abstract service for audio device detection.
|
||||
///
|
||||
/// Implementations:
|
||||
@@ -21,6 +29,16 @@ abstract class ServicioDispositivoAudio {
|
||||
/// (method channel round-trip). Returns the cached value if already known.
|
||||
Future<DispositivoAudio> obtenerDispositivoActual();
|
||||
|
||||
/// Cancels the current device-change subscription and subscribes again.
|
||||
///
|
||||
/// Sends the platform `cancel`+`listen` control messages, which re-triggers
|
||||
/// `onListen` on the CURRENT activity's stream handler and re-registers the
|
||||
/// native `AudioDeviceCallback`. Needed because the Flutter engine outlives
|
||||
/// the Activity (`AudioServiceActivity`): after an activity recreation the
|
||||
/// new handler never saw a `listen`, so its event sink stays null and
|
||||
/// device events stop reaching Dart until this resync runs.
|
||||
Future<void> resubscribir();
|
||||
|
||||
/// Requests the `BLUETOOTH_CONNECT` runtime permission (API 31+) at the
|
||||
/// point the device-management UI is opened (bt-device-identity ADR-1).
|
||||
/// Returns true when granted or not required (SDK < 31, iOS); false when
|
||||
@@ -88,6 +106,12 @@ class ServicioDispositivoAudioReal extends ServicioDispositivoAudio {
|
||||
return device;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> resubscribir() async {
|
||||
await _eventSub?.cancel();
|
||||
_subscribeToEvents();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> solicitarPermisoBluetooth() async {
|
||||
final granted = await _methodChannel.invokeMethod<bool>(
|
||||
@@ -102,6 +126,15 @@ class ServicioDispositivoAudioReal extends ServicioDispositivoAudio {
|
||||
await _controller.close();
|
||||
}
|
||||
|
||||
/// Builds a [DispositivoAudio] from the raw platform-channel map shape.
|
||||
///
|
||||
/// Public so headless consumers (the Android Auto handler's one-shot
|
||||
/// `getActiveDevice` query) reuse the exact same mapping without opening a
|
||||
/// second event-channel subscription, which would steal this service's
|
||||
/// stream handler on the Dart side.
|
||||
static DispositivoAudio dispositivoDesdeMapa(Map<String, dynamic> map) =>
|
||||
_mapToDispositivo(map);
|
||||
|
||||
static DispositivoAudio _mapToDispositivo(Map<String, dynamic> map) {
|
||||
final id = map['id'] as String? ?? 'builtin_speaker';
|
||||
final type = map['type'] as int? ?? 2;
|
||||
|
||||
Reference in New Issue
Block a user