discord-jadx/app/src/main/java/com/discord/rtcconnection/audio/DiscordAudioManager.java

953 lines
39 KiB
Java

package com.discord.rtcconnection.audio;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.media.AudioAttributes;
import android.media.AudioFocusRequest;
import android.media.AudioManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import androidx.annotation.MainThread;
import c.a.q.k0.f;
import c.a.q.k0.h;
import c.a.q.k0.i;
import c.a.q.l0.a;
import c0.g;
import c0.t.n;
import c0.t.o;
import c0.z.d.m;
import com.discord.rtcconnection.enums.AudioManagerBroadcastAction;
import com.discord.rtcconnection.enums.BluetoothBroadcastAction;
import com.discord.rtcconnection.enums.BluetoothHeadsetAudioState;
import com.discord.rtcconnection.enums.BluetoothProfileConnectionState;
import com.discord.rtcconnection.enums.ScoAudioState;
import com.discord.utilities.lifecycle.ApplicationProvider;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;
import java.util.Set;
import kotlin.Lazy;
import kotlin.NoWhenBranchMatchedException;
import kotlin.jvm.functions.Function0;
import org.webrtc.MediaStreamTrack;
import org.webrtc.ThreadUtils;
import rx.subjects.BehaviorSubject;
import rx.subjects.SerializedSubject;
/* compiled from: DiscordAudioManager.kt */
public final class DiscordAudioManager {
public static final Lazy a = g.lazy(d.i);
public static final AudioDevice b = new AudioDevice(null, false, null, null, 15);
/* renamed from: c reason: collision with root package name */
public static final List<DeviceTypes> f2581c = n.listOf((Object[]) new DeviceTypes[]{DeviceTypes.EARPIECE, DeviceTypes.SPEAKERPHONE, DeviceTypes.BLUETOOTH_HEADSET, DeviceTypes.WIRED_HEADSET});
public static final DiscordAudioManager d = null;
public boolean A;
public boolean B;
public boolean C;
public boolean D;
public final AudioManager e;
public final BluetoothManager f;
public final Lazy g;
public final boolean h;
public final Object i = this;
public final long j;
public final ContentResolver k;
public final c.a.q.k0.c l;
public final c.a.q.k0.a m;
public BluetoothHeadset n;
public BluetoothScoState o;
public AudioManager.OnAudioFocusChangeListener p;
public AudioFocusRequest q;
/* renamed from: r reason: collision with root package name */
public List<AudioDevice> f2582r;
public final SerializedSubject<List<AudioDevice>, List<AudioDevice>> s;
public DeviceTypes t;
public final SerializedSubject<DeviceTypes, DeviceTypes> u;
public ContentObserver v;
/* renamed from: w reason: collision with root package name */
public int f2583w;
/* renamed from: x reason: collision with root package name */
public final int f2584x;
/* renamed from: y reason: collision with root package name */
public final SerializedSubject<Integer, Integer> f2585y;
/* renamed from: z reason: collision with root package name */
public DeviceTypes f2586z;
/* compiled from: DiscordAudioManager.kt */
public static final class AudioDevice {
public final DeviceTypes a;
public final boolean b;
/* renamed from: c reason: collision with root package name */
public final String f2587c;
public final String d;
public AudioDevice() {
this(null, false, null, null, 15);
}
public AudioDevice(DeviceTypes deviceTypes, boolean z2, String str, String str2) {
m.checkNotNullParameter(deviceTypes, "type");
this.a = deviceTypes;
this.b = z2;
this.f2587c = str;
this.d = str2;
}
public AudioDevice(DeviceTypes deviceTypes, boolean z2, String str, String str2, int i) {
deviceTypes = (i & 1) != 0 ? DeviceTypes.INVALID : deviceTypes;
z2 = (i & 2) != 0 ? false : z2;
int i2 = i & 4;
int i3 = i & 8;
m.checkNotNullParameter(deviceTypes, "type");
this.a = deviceTypes;
this.b = z2;
this.f2587c = null;
this.d = null;
}
public static AudioDevice a(AudioDevice audioDevice, DeviceTypes deviceTypes, boolean z2, String str, String str2, int i) {
DeviceTypes deviceTypes2 = (i & 1) != 0 ? audioDevice.a : null;
if ((i & 2) != 0) {
z2 = audioDevice.b;
}
if ((i & 4) != 0) {
str = audioDevice.f2587c;
}
if ((i & 8) != 0) {
str2 = audioDevice.d;
}
Objects.requireNonNull(audioDevice);
m.checkNotNullParameter(deviceTypes2, "type");
return new AudioDevice(deviceTypes2, z2, str, str2);
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof AudioDevice)) {
return false;
}
AudioDevice audioDevice = (AudioDevice) obj;
return m.areEqual(this.a, audioDevice.a) && this.b == audioDevice.b && m.areEqual(this.f2587c, audioDevice.f2587c) && m.areEqual(this.d, audioDevice.d);
}
public int hashCode() {
DeviceTypes deviceTypes = this.a;
int i = 0;
int hashCode = (deviceTypes != null ? deviceTypes.hashCode() : 0) * 31;
boolean z2 = this.b;
if (z2) {
z2 = true;
}
int i2 = z2 ? 1 : 0;
int i3 = z2 ? 1 : 0;
int i4 = z2 ? 1 : 0;
int i5 = (hashCode + i2) * 31;
String str = this.f2587c;
int hashCode2 = (i5 + (str != null ? str.hashCode() : 0)) * 31;
String str2 = this.d;
if (str2 != null) {
i = str2.hashCode();
}
return hashCode2 + i;
}
public String toString() {
StringBuilder O = c.d.b.a.a.O("AudioDevice(type=");
O.append(this.a);
O.append(", isAvailable=");
O.append(this.b);
O.append(", id=");
O.append(this.f2587c);
O.append(", name=");
return c.d.b.a.a.G(O, this.d, ")");
}
}
/* compiled from: DiscordAudioManager.kt */
public enum BluetoothScoState {
INVALID(-1),
OFF(0),
ON(1),
TURNING_ON(2),
TURNING_OFF(3);
private final int value;
private BluetoothScoState(int i) {
this.value = i;
}
public final int getValue() {
return this.value;
}
}
/* compiled from: DiscordAudioManager.kt */
public enum DeviceTypes {
DEFAULT(-2),
INVALID(-1),
SPEAKERPHONE(0),
WIRED_HEADSET(1),
EARPIECE(2),
BLUETOOTH_HEADSET(3);
private final int value;
private DeviceTypes(int i) {
this.value = i;
}
public final int getValue() {
return this.value;
}
}
/* compiled from: DiscordAudioManager.kt */
public static final class a implements Runnable {
public final /* synthetic */ DiscordAudioManager i;
public a(DiscordAudioManager discordAudioManager) {
this.i = discordAudioManager;
}
@Override // java.lang.Runnable
public final void run() {
DiscordAudioManager discordAudioManager = this.i;
c.a.q.k0.a aVar = discordAudioManager.m;
Objects.requireNonNull(aVar);
ThreadUtils.checkIsOnMainThread();
boolean z2 = false;
List<AudioManagerBroadcastAction> listOf = n.listOf((Object[]) new AudioManagerBroadcastAction[]{AudioManagerBroadcastAction.HeadsetPlug, AudioManagerBroadcastAction.ScoAudioStateUpdated});
c.c.a.a0.d.c1("AudioManagerBroadcastReceiver", "registering for broadcasts with actions: " + listOf);
IntentFilter intentFilter = new IntentFilter();
for (AudioManagerBroadcastAction audioManagerBroadcastAction : listOf) {
intentFilter.addAction(audioManagerBroadcastAction.getAction());
}
aVar.b.registerReceiver(aVar, intentFilter);
c.a.q.k0.c cVar = discordAudioManager.l;
Objects.requireNonNull(cVar);
ThreadUtils.checkIsOnMainThread();
if (!cVar.k) {
StringBuilder O = c.d.b.a.a.O("registering for broadcasts with actions: ");
Set<BluetoothBroadcastAction> set = c.a.q.k0.c.i;
O.append(set);
c.c.a.a0.d.c1("BluetoothBroadcastReceiver", O.toString());
IntentFilter intentFilter2 = new IntentFilter();
for (BluetoothBroadcastAction bluetoothBroadcastAction : set) {
intentFilter2.addAction(bluetoothBroadcastAction.getAction());
}
cVar.m.registerReceiver(cVar, intentFilter2);
c.a.q.k0.d dVar = c.a.q.k0.d.f215c;
Lazy lazy = c.a.q.k0.d.a;
if (((String) lazy.getValue()) != null) {
Context context = cVar.m;
m.checkNotNullParameter(context, "context");
m.checkNotNullParameter(cVar, "receiver");
try {
String str = (String) lazy.getValue();
if (str != null) {
context.registerReceiver(cVar, new IntentFilter(str));
}
} catch (Throwable unused) {
}
}
try {
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
if (defaultAdapter != null) {
z2 = defaultAdapter.getProfileProxy(cVar.m, cVar, 1);
}
} catch (SecurityException e) {
c.c.a.a0.d.g1("BluetoothBroadcastReceiver", "failed to get BluetoothHeadset profile: " + e);
}
if (z2) {
c.c.a.a0.d.c1("BluetoothBroadcastReceiver", "listening for HeadsetProfile proxy");
} else {
c.c.a.a0.d.d1("BluetoothBroadcastReceiver", "listening for HeadsetProfile proxy failed", null);
}
cVar.k = true;
}
c.a.q.k0.e eVar = new c.a.q.k0.e(discordAudioManager);
synchronized (discordAudioManager.i) {
discordAudioManager.p = eVar;
}
}
}
/* compiled from: DiscordAudioManager.kt */
public final class b implements h {
public b() {
}
@Override // c.a.q.k0.h
public void a(Context context, boolean z2) {
m.checkNotNullParameter(context, "context");
m.checkNotNullParameter(context, "context");
}
@Override // c.a.q.k0.h
@MainThread
public void b(Context context, ScoAudioState.b bVar) {
boolean z2;
m.checkNotNullParameter(context, "context");
m.checkNotNullParameter(bVar, "scoAudioStateUpdate");
int ordinal = bVar.a.ordinal();
if (ordinal == 0) {
c.c.a.a0.d.c1("DiscordAudioManager", "[onScoAudioStateUpdate] scoAudioStateUpdate = " + bVar);
DiscordAudioManager discordAudioManager = DiscordAudioManager.this;
BluetoothScoState bluetoothScoState = discordAudioManager.o;
discordAudioManager.k();
int ordinal2 = bluetoothScoState.ordinal();
if (ordinal2 == 2) {
synchronized (DiscordAudioManager.this.i) {
z2 = DiscordAudioManager.this.D;
}
if (z2) {
c.c.a.a0.d.c1("DiscordAudioManager", "SCO off detected directly from ON. Refreshing Bluetooth device");
DiscordAudioManager.this.j();
DiscordAudioManager.this.l();
}
} else if (ordinal2 == 3) {
StringBuilder O = c.d.b.a.a.O("Unable to turn on SCO. Clearing Bluetooth device. mode: ");
O.append(DiscordAudioManager.this.e.getMode());
c.c.a.a0.d.c1("DiscordAudioManager", O.toString());
synchronized (DiscordAudioManager.this.i) {
DiscordAudioManager discordAudioManager2 = DiscordAudioManager.this;
List<AudioDevice> list = discordAudioManager2.f2582r;
ArrayList arrayList = new ArrayList(o.collectionSizeOrDefault(list, 10));
for (AudioDevice audioDevice : list) {
arrayList.add(audioDevice.a.ordinal() != 5 ? AudioDevice.a(audioDevice, null, false, null, null, 15) : AudioDevice.a(audioDevice, null, false, null, null, 1));
}
discordAudioManager2.f2582r = arrayList;
discordAudioManager2.s.j.onNext(arrayList);
}
DiscordAudioManager discordAudioManager3 = DiscordAudioManager.this;
discordAudioManager3.a(discordAudioManager3.f2582r);
}
} else if (ordinal == 1) {
c.c.a.a0.d.c1("DiscordAudioManager", "[onScoAudioStateUpdate] scoAudioStateUpdate = " + bVar);
DiscordAudioManager.this.j();
}
}
@Override // c.a.q.k0.h
public void c(Context context) {
m.checkNotNullParameter(context, "context");
m.checkNotNullParameter(context, "context");
}
@Override // c.a.q.k0.h
public void d(Context context, boolean z2) {
m.checkNotNullParameter(context, "context");
m.checkNotNullParameter(context, "context");
}
@Override // c.a.q.k0.h
@MainThread
public void e(Context context, c.a.q.l0.a aVar) {
m.checkNotNullParameter(context, "context");
m.checkNotNullParameter(aVar, "wiredHeadsetState");
c.c.a.a0.d.c1("DiscordAudioManager", "[onWiredHeadsetPlug] wiredHeadsetState = " + aVar);
if (m.areEqual(aVar, a.b.a)) {
synchronized (DiscordAudioManager.this.i) {
DiscordAudioManager discordAudioManager = DiscordAudioManager.this;
List<AudioDevice> list = discordAudioManager.f2582r;
ArrayList arrayList = new ArrayList(o.collectionSizeOrDefault(list, 10));
for (AudioDevice audioDevice : list) {
int ordinal = audioDevice.a.ordinal();
arrayList.add(ordinal != 3 ? ordinal != 4 ? AudioDevice.a(audioDevice, null, false, null, null, 15) : AudioDevice.a(audioDevice, null, DiscordAudioManager.this.h, null, null, 13) : AudioDevice.a(audioDevice, null, false, null, null, 13));
}
discordAudioManager.f2582r = arrayList;
discordAudioManager.s.j.onNext(arrayList);
}
} else if (aVar instanceof a.C0036a) {
synchronized (DiscordAudioManager.this.i) {
DiscordAudioManager discordAudioManager2 = DiscordAudioManager.this;
List<AudioDevice> list2 = discordAudioManager2.f2582r;
ArrayList arrayList2 = new ArrayList(o.collectionSizeOrDefault(list2, 10));
for (AudioDevice audioDevice2 : list2) {
int ordinal2 = audioDevice2.a.ordinal();
arrayList2.add(ordinal2 != 3 ? ordinal2 != 4 ? AudioDevice.a(audioDevice2, null, false, null, null, 15) : AudioDevice.a(audioDevice2, null, false, null, null, 13) : AudioDevice.a(audioDevice2, null, true, null, null, 13));
}
discordAudioManager2.f2582r = arrayList2;
discordAudioManager2.s.j.onNext(arrayList2);
}
}
DiscordAudioManager.this.l();
}
}
/* compiled from: DiscordAudioManager.kt */
public final class c implements i {
public c() {
}
@Override // c.a.q.k0.i
@MainThread
public void a(Context context, BluetoothHeadsetAudioState.b bVar) {
m.checkNotNullParameter(context, "context");
m.checkNotNullParameter(bVar, "audioState");
BluetoothHeadsetAudioState bluetoothHeadsetAudioState = bVar.a;
if (bluetoothHeadsetAudioState == null) {
bluetoothHeadsetAudioState = BluetoothHeadsetAudioState.Disconnected;
}
BluetoothDevice bluetoothDevice = bVar.f2588c;
StringBuilder sb = new StringBuilder();
sb.append("[onHeadsetAudioStateChanged] state: ");
sb.append(bluetoothHeadsetAudioState);
sb.append(", device: ");
sb.append(bluetoothDevice != null ? bluetoothDevice.getName() : null);
c.c.a.a0.d.c1("DiscordAudioManager", sb.toString());
int ordinal = bluetoothHeadsetAudioState.ordinal();
if (ordinal == 0) {
DiscordAudioManager.this.l();
} else if (ordinal == 2) {
synchronized (DiscordAudioManager.this.i) {
DiscordAudioManager discordAudioManager = DiscordAudioManager.this;
List<AudioDevice> list = discordAudioManager.f2582r;
ArrayList arrayList = new ArrayList(o.collectionSizeOrDefault(list, 10));
for (AudioDevice audioDevice : list) {
arrayList.add(audioDevice.a.ordinal() != 5 ? AudioDevice.a(audioDevice, null, false, null, null, 15) : AudioDevice.a(audioDevice, null, true, bluetoothDevice != null ? bluetoothDevice.getAddress() : null, bluetoothDevice != null ? bluetoothDevice.getName() : null, 1));
}
discordAudioManager.f2582r = arrayList;
discordAudioManager.s.j.onNext(arrayList);
}
}
}
@Override // c.a.q.k0.i
@MainThread
public void b(BluetoothDevice bluetoothDevice) {
c.a.q.k0.d dVar = c.a.q.k0.d.f215c;
if (!(((String) c.a.q.k0.d.a.getValue()) != null)) {
throw new IllegalStateException("Check failed.".toString());
} else if (bluetoothDevice != null) {
DiscordAudioManager.this.l();
}
}
@Override // c.a.q.k0.i
@MainThread
public void c(BluetoothHeadset bluetoothHeadset) {
DiscordAudioManager.this.n = bluetoothHeadset;
}
@Override // c.a.q.k0.i
@MainThread
public void d(Context context, BluetoothProfileConnectionState.b bVar) {
ArrayList arrayList;
DiscordAudioManager discordAudioManager;
DeviceTypes deviceTypes;
m.checkNotNullParameter(context, "context");
m.checkNotNullParameter(bVar, "connectionState");
BluetoothProfileConnectionState bluetoothProfileConnectionState = bVar.b;
if (bluetoothProfileConnectionState == null) {
bluetoothProfileConnectionState = BluetoothProfileConnectionState.Disconnected;
}
BluetoothDevice bluetoothDevice = bVar.f2589c;
StringBuilder sb = new StringBuilder();
sb.append("[onHeadsetConnectionStateChanged] state: ");
sb.append(bluetoothProfileConnectionState);
sb.append(", device: ");
String str = null;
sb.append(bluetoothDevice != null ? bluetoothDevice.getName() : null);
c.c.a.a0.d.c1("DiscordAudioManager", sb.toString());
int ordinal = bluetoothProfileConnectionState.ordinal();
if (ordinal != 0) {
if (ordinal != 1) {
if (ordinal == 2) {
synchronized (DiscordAudioManager.this.i) {
discordAudioManager = DiscordAudioManager.this;
deviceTypes = discordAudioManager.t;
}
if (deviceTypes == DeviceTypes.BLUETOOTH_HEADSET) {
discordAudioManager.g();
return;
} else {
discordAudioManager.l();
return;
}
} else if (ordinal != 3) {
return;
}
}
c.c.a.a0.d.c1("DiscordAudioManager", "[onHeadsetConnectionStateChanged] " + bluetoothProfileConnectionState + "...");
return;
}
synchronized (DiscordAudioManager.this.i) {
List<AudioDevice> list = DiscordAudioManager.this.f2582r;
arrayList = new ArrayList(o.collectionSizeOrDefault(list, 10));
for (AudioDevice audioDevice : list) {
arrayList.add(AudioDevice.a(audioDevice, null, false, null, null, 15));
}
}
DeviceTypes deviceTypes2 = DeviceTypes.BLUETOOTH_HEADSET;
if (((AudioDevice) arrayList.get(deviceTypes2.getValue())).f2587c != null) {
if (bluetoothDevice != null) {
str = bluetoothDevice.getAddress();
}
if (m.areEqual(str, ((AudioDevice) arrayList.get(deviceTypes2.getValue())).f2587c)) {
DiscordAudioManager.this.l();
}
}
}
}
/* compiled from: DiscordAudioManager.kt */
public static final class d extends c0.z.d.o implements Function0<DiscordAudioManager> {
public static final d i = new d();
public d() {
super(0);
}
/* Return type fixed from 'java.lang.Object' to match base method */
@Override // kotlin.jvm.functions.Function0
/* renamed from: invoke */
public DiscordAudioManager mo17invoke() {
return new DiscordAudioManager(ApplicationProvider.INSTANCE.get());
}
}
/* compiled from: DiscordAudioManager.kt */
public static final class e extends c0.z.d.o implements Function0<c.a.q.k0.b> {
public final /* synthetic */ Context $context;
/* JADX INFO: super call moved to the top of the method (can break code semantics) */
public e(Context context) {
super(0);
this.$context = context;
}
/* Return type fixed from 'java.lang.Object' to match base method */
@Override // kotlin.jvm.functions.Function0
/* renamed from: invoke */
public c.a.q.k0.b mo17invoke() {
return new c.a.q.k0.b(this.$context);
}
}
public DiscordAudioManager(Context context) {
AudioDevice audioDevice;
m.checkNotNullParameter(context, "context");
Object systemService = context.getSystemService(MediaStreamTrack.AUDIO_TRACK_KIND);
Objects.requireNonNull(systemService, "null cannot be cast to non-null type android.media.AudioManager");
this.e = (AudioManager) systemService;
Object systemService2 = context.getSystemService("bluetooth");
Objects.requireNonNull(systemService2, "null cannot be cast to non-null type android.bluetooth.BluetoothManager");
this.f = (BluetoothManager) systemService2;
this.g = g.lazy(new e(context));
this.h = context.getPackageManager().hasSystemFeature("android.hardware.telephony");
Thread currentThread = Thread.currentThread();
m.checkNotNullExpressionValue(currentThread, "Thread.currentThread()");
this.j = currentThread.getId();
ContentResolver contentResolver = context.getContentResolver();
m.checkNotNullExpressionValue(contentResolver, "context.contentResolver");
this.k = contentResolver;
this.l = new c.a.q.k0.c(context, new c());
this.m = new c.a.q.k0.a(context, new b());
new Handler(Looper.getMainLooper()).post(new a(this));
this.o = BluetoothScoState.INVALID;
List<AudioDevice> listOf = n.listOf((Object[]) new AudioDevice[]{new AudioDevice(DeviceTypes.SPEAKERPHONE, false, null, null, 14), new AudioDevice(DeviceTypes.WIRED_HEADSET, false, null, null, 14), new AudioDevice(DeviceTypes.EARPIECE, false, null, null, 14), new AudioDevice(DeviceTypes.BLUETOOTH_HEADSET, false, null, null, 14)});
ArrayList arrayList = new ArrayList();
for (AudioDevice audioDevice2 : listOf) {
int ordinal = audioDevice2.a.ordinal();
if (ordinal == 0 || ordinal == 1) {
audioDevice = null;
} else if (ordinal == 2) {
audioDevice = AudioDevice.a(audioDevice2, null, true, null, null, 13);
} else if (ordinal == 3) {
audioDevice = AudioDevice.a(audioDevice2, null, this.e.isWiredHeadsetOn(), null, null, 13);
} else if (ordinal == 4) {
audioDevice = AudioDevice.a(audioDevice2, null, this.h, null, null, 13);
} else if (ordinal == 5) {
audioDevice = AudioDevice.a(audioDevice2, null, false, null, null, 15);
} else {
throw new NoWhenBranchMatchedException();
}
if (audioDevice != null) {
arrayList.add(audioDevice);
}
}
this.f2582r = arrayList;
this.s = new SerializedSubject<>(BehaviorSubject.l0(arrayList));
DeviceTypes deviceTypes = DeviceTypes.INVALID;
this.t = deviceTypes;
this.u = new SerializedSubject<>(BehaviorSubject.l0(deviceTypes));
this.f2584x = this.e.getStreamMaxVolume(3);
this.f2585y = new SerializedSubject<>(BehaviorSubject.l0(Integer.valueOf(this.f2583w)));
this.f2586z = DeviceTypes.DEFAULT;
}
public static final DiscordAudioManager d() {
return (DiscordAudioManager) a.getValue();
}
public static final List<AudioDevice> f() {
return n.listOf((Object[]) new AudioDevice[]{new AudioDevice(DeviceTypes.SPEAKERPHONE, false, null, null, 14), new AudioDevice(DeviceTypes.WIRED_HEADSET, false, null, null, 14), new AudioDevice(DeviceTypes.EARPIECE, false, null, null, 14), new AudioDevice(DeviceTypes.BLUETOOTH_HEADSET, false, null, null, 14)});
}
public final void a(List<AudioDevice> list) {
DeviceTypes deviceTypes;
DeviceTypes deviceTypes2;
synchronized (this.i) {
List<DeviceTypes> list2 = f2581c;
ListIterator<DeviceTypes> listIterator = list2.listIterator(list2.size());
while (true) {
if (!listIterator.hasPrevious()) {
deviceTypes = null;
break;
}
deviceTypes = listIterator.previous();
if (list.get(deviceTypes.getValue()).b) {
break;
}
}
deviceTypes2 = deviceTypes;
if (deviceTypes2 == null) {
deviceTypes2 = DeviceTypes.SPEAKERPHONE;
}
}
c.c.a.a0.d.c1("DiscordAudioManager", "Default device to activate: " + deviceTypes2);
b(deviceTypes2);
}
public final void b(DeviceTypes deviceTypes) {
boolean z2;
boolean z3;
synchronized (this.i) {
z2 = true;
z3 = !this.D;
}
if (z3) {
c.c.a.a0.d.g1("DiscordAudioManager", "Unable to activate audio output outside Discord-requested communication mode");
return;
}
if (deviceTypes == DeviceTypes.BLUETOOTH_HEADSET) {
j();
} else {
k();
}
if (deviceTypes != DeviceTypes.SPEAKERPHONE) {
z2 = false;
}
if (this.e.isSpeakerphoneOn() != z2) {
this.e.setSpeakerphoneOn(z2);
}
synchronized (this.i) {
this.t = deviceTypes;
this.u.j.onNext(deviceTypes);
}
c.c.a.a0.d.c1("DiscordAudioManager", "Activated device: " + deviceTypes);
}
public final void c() {
Thread currentThread = Thread.currentThread();
m.checkNotNullExpressionValue(currentThread, "Thread.currentThread()");
if (currentThread.getId() != this.j) {
throw new IllegalStateException("Method was not called from a valid thread");
}
}
public final c.a.q.k0.b e() {
return (c.a.q.k0.b) this.g.getValue();
}
/* JADX WARNING: Removed duplicated region for block: B:33:0x006c */
/* JADX WARNING: Removed duplicated region for block: B:36:0x0076 */
/* JADX WARNING: Removed duplicated region for block: B:37:0x007e */
/* JADX WARNING: Removed duplicated region for block: B:47:0x0099 A[SYNTHETIC] */
public final void g() {
List<BluetoothDevice> list;
Object obj;
BluetoothDevice bluetoothDevice;
boolean z2;
boolean z3;
if (!e().d) {
c.c.a.a0.d.g1("DiscordAudioManager", "refreshBluetoothHeadset() requires BLUETOOTH permission");
return;
}
BluetoothHeadset bluetoothHeadset = this.n;
if (bluetoothHeadset == null || (list = bluetoothHeadset.getConnectedDevices()) == null) {
list = n.emptyList();
}
Iterator<T> it = list.iterator();
while (true) {
if (!it.hasNext()) {
obj = null;
break;
}
obj = it.next();
BluetoothDevice bluetoothDevice2 = (BluetoothDevice) obj;
BluetoothHeadset bluetoothHeadset2 = this.n;
if (bluetoothHeadset2 != null) {
z3 = bluetoothHeadset2.isAudioConnected(bluetoothDevice2);
continue;
} else {
z3 = false;
continue;
}
if (z3) {
break;
}
}
BluetoothDevice bluetoothDevice3 = (BluetoothDevice) obj;
BluetoothHeadset bluetoothHeadset3 = this.n;
if (bluetoothHeadset3 != null) {
c.a.q.k0.d dVar = c.a.q.k0.d.f215c;
m.checkNotNullParameter(bluetoothHeadset3, "headsetProfile");
try {
Method method = (Method) c.a.q.k0.d.b.getValue();
Object invoke = method != null ? method.invoke(bluetoothHeadset3, new Object[0]) : null;
if (!(invoke instanceof BluetoothDevice)) {
invoke = null;
}
bluetoothDevice = (BluetoothDevice) invoke;
} catch (Throwable unused) {
}
if (bluetoothDevice3 == null) {
bluetoothDevice3 = bluetoothDevice;
}
if (e().d) {
c.c.a.a0.d.g1("DiscordAudioManager", "hasBluetoothHeadset() requires BLUETOOTH permission");
} else {
BluetoothAdapter adapter = this.f.getAdapter();
if (adapter != null) {
int profileConnectionState = adapter.getProfileConnectionState(1);
if (adapter.isEnabled() && profileConnectionState == 2) {
z2 = true;
synchronized (this.i) {
List<AudioDevice> list2 = this.f2582r;
ArrayList arrayList = new ArrayList(o.collectionSizeOrDefault(list2, 10));
for (AudioDevice audioDevice : list2) {
arrayList.add(audioDevice.a.ordinal() != 5 ? AudioDevice.a(audioDevice, null, false, null, null, 15) : AudioDevice.a(audioDevice, null, (!this.D || this.t != DeviceTypes.BLUETOOTH_HEADSET) ? z2 : bluetoothDevice3 != null, bluetoothDevice3 != null ? bluetoothDevice3.getAddress() : null, bluetoothDevice3 != null ? bluetoothDevice3.getName() : null, 1));
}
this.f2582r = arrayList;
this.s.j.onNext(arrayList);
}
return;
}
}
}
z2 = false;
synchronized (this.i) {
}
}
bluetoothDevice = null;
if (bluetoothDevice3 == null) {
}
if (e().d) {
}
z2 = false;
synchronized (this.i) {
}
}
/* JADX WARNING: Code restructure failed: missing block: B:18:0x0090, code lost:
if (r6.e.requestAudioFocus(r7) == 1) goto L_0x009b;
*/
/* JADX WARNING: Code restructure failed: missing block: B:20:0x0099, code lost:
if (r6.e.requestAudioFocus(r1, 0, 1) == 1) goto L_0x009b;
*/
/* JADX WARNING: Code restructure failed: missing block: B:47:0x00e4, code lost:
if (r6.e.abandonAudioFocusRequest(r0) == 1) goto L_0x00fa;
*/
/* JADX WARNING: Code restructure failed: missing block: B:58:0x00f8, code lost:
if (r6.e.abandonAudioFocus(r0) == 1) goto L_0x00fa;
*/
/* JADX WARNING: Removed duplicated region for block: B:61:0x00fd */
/* JADX WARNING: Removed duplicated region for block: B:62:0x0105 */
/* JADX WARNING: Removed duplicated region for block: B:66:0x0119 */
/* JADX WARNING: Removed duplicated region for block: B:69:0x0129 */
/* JADX WARNING: Removed duplicated region for block: B:72:0x0132 */
/* JADX WARNING: Removed duplicated region for block: B:81:? A[RETURN, SYNTHETIC] */
public final void h(boolean z2) {
boolean z3;
boolean z4;
AudioFocusRequest audioFocusRequest;
c();
if (!e().b) {
c.c.a.a0.d.g1("DiscordAudioManager", "MODIFY_AUDIO_SETTINGS is missing. Client will run with reduced functionality");
return;
}
Handler handler = null;
boolean z5 = false;
if (z2) {
Looper myLooper = Looper.myLooper();
if (myLooper != null) {
handler = new Handler(myLooper);
}
f fVar = new f(this, handler);
this.k.registerContentObserver(Settings.System.CONTENT_URI, true, fVar);
this.v = fVar;
this.A = this.e.isSpeakerphoneOn();
this.B = this.e.isMicrophoneMute();
this.C = this.e.isBluetoothScoOn();
i(true);
synchronized (this.i) {
AudioManager.OnAudioFocusChangeListener onAudioFocusChangeListener = this.p;
if (onAudioFocusChangeListener != null) {
if (Build.VERSION.SDK_INT >= 26) {
AudioFocusRequest.Builder builder = new AudioFocusRequest.Builder(1);
AudioAttributes.Builder builder2 = new AudioAttributes.Builder();
builder2.setUsage(2);
builder2.setContentType(1);
builder.setAcceptsDelayedFocusGain(true);
Looper myLooper2 = Looper.myLooper();
m.checkNotNull(myLooper2);
builder.setOnAudioFocusChangeListener(onAudioFocusChangeListener, new Handler(myLooper2));
builder.setAudioAttributes(builder2.build());
AudioFocusRequest build = builder.build();
this.q = build;
}
z5 = true;
if (z5) {
c.c.a.a0.d.c1("DiscordAudioManager", "Successful requestAudioFocus()");
} else {
c.c.a.a0.d.g1("DiscordAudioManager", "Unable to requestAudioFocus()");
}
}
}
l();
return;
}
ContentObserver contentObserver = this.v;
if (contentObserver != null) {
this.k.unregisterContentObserver(contentObserver);
}
this.v = null;
b(DeviceTypes.INVALID);
synchronized (this.i) {
this.f2586z = DeviceTypes.DEFAULT;
}
i(false);
if (Build.VERSION.SDK_INT >= 26) {
synchronized (this.i) {
audioFocusRequest = this.q;
}
if (audioFocusRequest != null) {
}
if (!z5) {
c.c.a.a0.d.c1("DiscordAudioManager", "Successful releaseAudioFocus()");
} else {
c.c.a.a0.d.g1("DiscordAudioManager", "Unable to releaseAudioFocus()");
}
z3 = this.B;
if (this.e.isMicrophoneMute() != z3) {
this.e.setMicrophoneMute(z3);
}
z4 = this.A;
if (this.e.isSpeakerphoneOn() != z4) {
this.e.setSpeakerphoneOn(z4);
}
if (this.C) {
j();
return;
}
return;
}
synchronized (this.i) {
AudioManager.OnAudioFocusChangeListener onAudioFocusChangeListener2 = this.p;
if (onAudioFocusChangeListener2 != null) {
}
}
z3 = this.B;
if (this.e.isMicrophoneMute() != z3) {
}
z4 = this.A;
if (this.e.isSpeakerphoneOn() != z4) {
}
if (this.C) {
}
z5 = true;
if (!z5) {
}
z3 = this.B;
if (this.e.isMicrophoneMute() != z3) {
}
z4 = this.A;
if (this.e.isSpeakerphoneOn() != z4) {
}
if (this.C) {
}
}
public final void i(boolean z2) {
try {
this.e.setMode(z2 ? 3 : 0);
} catch (SecurityException unused) {
}
synchronized (this.i) {
this.D = z2;
}
}
public final synchronized void j() {
BluetoothScoState bluetoothScoState = this.o;
BluetoothScoState bluetoothScoState2 = BluetoothScoState.ON;
if (bluetoothScoState != bluetoothScoState2) {
BluetoothScoState bluetoothScoState3 = BluetoothScoState.TURNING_ON;
if (bluetoothScoState != bluetoothScoState3) {
if (this.e.isBluetoothScoOn()) {
this.o = bluetoothScoState2;
return;
}
this.o = bluetoothScoState3;
this.e.startBluetoothSco();
}
}
}
public final synchronized void k() {
BluetoothScoState bluetoothScoState = this.o;
if (bluetoothScoState != BluetoothScoState.ON && bluetoothScoState != BluetoothScoState.TURNING_ON) {
return;
}
if (!this.e.isBluetoothScoOn()) {
this.o = BluetoothScoState.OFF;
return;
}
this.o = BluetoothScoState.TURNING_OFF;
this.e.stopBluetoothSco();
}
public final void l() {
DeviceTypes deviceTypes;
ArrayList arrayList;
synchronized (this.i) {
deviceTypes = this.f2586z;
}
g();
synchronized (this.i) {
List<AudioDevice> list = this.f2582r;
arrayList = new ArrayList(o.collectionSizeOrDefault(list, 10));
for (AudioDevice audioDevice : list) {
arrayList.add(AudioDevice.a(audioDevice, null, false, null, null, 15));
}
}
if (deviceTypes == DeviceTypes.DEFAULT || !arrayList.get(deviceTypes.getValue()).b) {
a(arrayList);
} else {
b(deviceTypes);
}
}
}