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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user