package com.discord.utilities.media; import android.app.Application; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import d0.g; import d0.o; import d0.t.h0; import d0.z.d.m; import java.util.Map; import kotlin.Lazy; import kotlin.Unit; import kotlin.jvm.functions.Function1; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.reflect.KFunction; import rx.Observable; import rx.subjects.BehaviorSubject; import rx.subjects.SerializedSubject; /* compiled from: AudioOutputMonitor.kt */ public final class AudioOutputMonitor extends BroadcastReceiver { public static final Companion Companion = new Companion(null); private static final Lazy INSTANCE$delegate = g.lazy(AudioOutputMonitor$Companion$INSTANCE$2.INSTANCE); private final IntentFilter intentFilter; private final Map> intentHandlers; private AudioOutputState outputState; private final SerializedSubject outputStateSubject; /* compiled from: AudioOutputMonitor.kt */ public static final class Companion { private Companion() { } public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } public final AudioOutputMonitor getINSTANCE() { Lazy access$getINSTANCE$cp = AudioOutputMonitor.access$getINSTANCE$cp(); Companion companion = AudioOutputMonitor.Companion; return (AudioOutputMonitor) access$getINSTANCE$cp.getValue(); } public final void initialize(Application application) { m.checkNotNullParameter(application, "application"); getINSTANCE().bindContext(application); } } public AudioOutputMonitor() { AudioOutputState audioOutputState = new AudioOutputState(false, false, false, false, null, 31, null); this.outputState = audioOutputState; this.outputStateSubject = new SerializedSubject<>(BehaviorSubject.l0(audioOutputState)); Map> mapOf = h0.mapOf(o.to("android.intent.action.HEADSET_PLUG", new AudioOutputMonitor$intentHandlers$1(this)), o.to("android.bluetooth.adapter.action.STATE_CHANGED", new AudioOutputMonitor$intentHandlers$2(this)), o.to("android.media.ACTION_SCO_AUDIO_STATE_UPDATED", new AudioOutputMonitor$intentHandlers$3(this)), o.to("android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED", new AudioOutputMonitor$intentHandlers$4(this))); this.intentHandlers = mapOf; IntentFilter intentFilter = new IntentFilter(); for (String str : mapOf.keySet()) { intentFilter.addAction(str); } this.intentFilter = intentFilter; } public static final /* synthetic */ Lazy access$getINSTANCE$cp() { return INSTANCE$delegate; } public static final /* synthetic */ void access$handleBluetoothAdapterUpdate(AudioOutputMonitor audioOutputMonitor, Intent intent) { audioOutputMonitor.handleBluetoothAdapterUpdate(intent); } public static final /* synthetic */ void access$handleBluetoothConnectionUpdate(AudioOutputMonitor audioOutputMonitor, int i, BluetoothDevice bluetoothDevice) { audioOutputMonitor.handleBluetoothConnectionUpdate(i, bluetoothDevice); } public static final /* synthetic */ void access$handleBluetoothConnectionUpdate(AudioOutputMonitor audioOutputMonitor, Intent intent) { audioOutputMonitor.handleBluetoothConnectionUpdate(intent); } public static final /* synthetic */ void access$handleHeadsetPlugIntent(AudioOutputMonitor audioOutputMonitor, Intent intent) { audioOutputMonitor.handleHeadsetPlugIntent(intent); } public static final /* synthetic */ void access$handleScoUpdate(AudioOutputMonitor audioOutputMonitor, Intent intent) { audioOutputMonitor.handleScoUpdate(intent); } private final synchronized void handleBluetoothAdapterUpdate(int i) { AudioOutputState audioOutputState = this.outputState; setOutputState(AudioOutputState.copy$default(audioOutputState, i != 10 ? i != 12 ? audioOutputState.isBluetoothAdapterDisabled() : false : true, false, false, false, null, 30, null)); } private final synchronized void handleBluetoothAdapterUpdate(Intent intent) { handleBluetoothAdapterUpdate(intent.getIntExtra("android.bluetooth.adapter.extra.STATE", -1)); } private final synchronized void handleBluetoothConnectionUpdate(int i, BluetoothDevice bluetoothDevice) { setOutputState(AudioOutputState.copy$default(this.outputState, false, i != 2, false, false, bluetoothDevice != null ? bluetoothDevice.getName() : null, 13, null)); } private final synchronized void handleBluetoothConnectionUpdate(Intent intent) { handleBluetoothConnectionUpdate(intent.getIntExtra("android.bluetooth.profile.extra.STATE", -1), (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE")); } private final synchronized void handleHeadsetPlugIntent(Intent intent) { int intExtra = intent.getIntExtra("state", -1); AudioOutputState audioOutputState = this.outputState; boolean z2 = true; if (intExtra == 1) { z2 = false; } setOutputState(AudioOutputState.copy$default(audioOutputState, false, false, false, z2, null, 23, null)); } private final synchronized void handleInitialState(Context context) { Object systemService = context.getSystemService("bluetooth"); if (!(systemService instanceof BluetoothManager)) { systemService = null; } BluetoothManager bluetoothManager = (BluetoothManager) systemService; if (bluetoothManager != null) { BluetoothAdapter adapter = bluetoothManager.getAdapter(); if (adapter != null) { adapter.getProfileProxy(context, new AudioOutputMonitor$handleInitialState$1(this, adapter), 1); handleBluetoothAdapterUpdate(adapter.isEnabled() ? 12 : 10); } } } private final synchronized void handleScoUpdate(Intent intent) { int intExtra = intent.getIntExtra("android.media.ACTION_SCO_AUDIO_STATE_UPDATED", -1); AudioOutputState audioOutputState = this.outputState; boolean z2 = true; if (intExtra == 1) { z2 = false; } setOutputState(AudioOutputState.copy$default(audioOutputState, false, false, z2, false, null, 27, null)); } private final void setOutputState(AudioOutputState audioOutputState) { this.outputState = audioOutputState; this.outputStateSubject.j.onNext(audioOutputState); } public final void bindContext(Context context) { m.checkNotNullParameter(context, "context"); context.registerReceiver(this, this.intentFilter); handleInitialState(context); } public final Observable getOutputState() { Observable r = this.outputStateSubject.r(); m.checkNotNullExpressionValue(r, "outputStateSubject\n .distinctUntilChanged()"); return r; } @Override // android.content.BroadcastReceiver public void onReceive(Context context, Intent intent) { String action; if (intent != null && (action = intent.getAction()) != null) { m.checkNotNullExpressionValue(action, "intent?.action ?: return"); KFunction kFunction = this.intentHandlers.get(action); if (kFunction != null) { Unit unit = (Unit) ((Function1) kFunction).invoke(intent); } } } }