package g.i.a.b.e;

import android.annotation.TargetApi;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import g.h.a.i;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;

/* JADX INFO: loaded from: classes.dex */
public final class b {
    public static b a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public Context f5327b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public List<g.i.a.b.e.a> f5328c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public BluetoothAdapter f5329d;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public a f5335j;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public BluetoothHeadset f5330e = null;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public BluetoothA2dp f5331f = null;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public BluetoothProfile f5332g = null;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public BluetoothProfile f5333h = null;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public BluetoothProfile f5334i = null;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public BluetoothProfile.ServiceListener f5336k = new C0073b();

    public class a extends BroadcastReceiver {
        public a() {
        }

        @Override // android.content.BroadcastReceiver
        @TargetApi(19)
        public void onReceive(Context context, Intent intent) {
            String action;
            String str;
            action = intent.getAction();
            action.hashCode();
            switch (action) {
                case "android.bluetooth.headset.profile.action.AUDIO_STATE_CHANGED":
                    BluetoothDevice bluetoothDevice = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
                    if (bluetoothDevice != null) {
                        int intExtra = intent.getIntExtra("android.bluetooth.profile.extra.STATE", -1);
                        i.w0(String.format(Locale.US, "%s: action=%s, state: %d->%d", i.S0(bluetoothDevice.getAddress(), true), action, Integer.valueOf(intent.getIntExtra("android.bluetooth.profile.extra.PREVIOUS_STATE", -1)), Integer.valueOf(intExtra)));
                        List<g.i.a.b.e.a> list = b.this.f5328c;
                        if (list != null && list.size() > 0) {
                            Iterator<g.i.a.b.e.a> it = b.this.f5328c.iterator();
                            while (it.hasNext()) {
                                Objects.requireNonNull(it.next());
                            }
                            break;
                        }
                        Objects.requireNonNull(b.this);
                        i.V3(false, "no callback registed");
                        break;
                    }
                    break;
                case "android.bluetooth.input.profile.action.CONNECTION_STATE_CHANGED":
                    BluetoothDevice bluetoothDevice2 = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
                    int intExtra2 = intent.getIntExtra("android.bluetooth.profile.extra.STATE", -1);
                    int intExtra3 = intent.getIntExtra("android.bluetooth.profile.extra.PREVIOUS_STATE", -1);
                    if (bluetoothDevice2 != null) {
                        i.w0(String.format(Locale.US, "%s: action=%s, state: %d->%d", i.S0(bluetoothDevice2.getAddress(), true), action, Integer.valueOf(intExtra3), Integer.valueOf(intExtra2)));
                        List<g.i.a.b.e.a> list2 = b.this.f5328c;
                        if (list2 != null && list2.size() > 0) {
                            Iterator<g.i.a.b.e.a> it2 = b.this.f5328c.iterator();
                            while (it2.hasNext()) {
                                it2.next().b(bluetoothDevice2, intExtra2);
                            }
                            break;
                        }
                        Objects.requireNonNull(b.this);
                        i.V3(false, "no callback registed");
                        break;
                    }
                    break;
                case "android.bluetooth.a2dp.profile.action.PLAYING_STATE_CHANGED":
                    BluetoothDevice bluetoothDevice3 = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
                    int intExtra4 = intent.getIntExtra("android.bluetooth.profile.extra.STATE", -1);
                    int intExtra5 = intent.getIntExtra("android.bluetooth.profile.extra.PREVIOUS_STATE", -1);
                    if (bluetoothDevice3 != null) {
                        i.w0(String.format(Locale.US, "%s: action=%s, state: %d->%d", i.S0(bluetoothDevice3.getAddress(), true), action, Integer.valueOf(intExtra5), Integer.valueOf(intExtra4)));
                        List<g.i.a.b.e.a> list3 = b.this.f5328c;
                        if (list3 != null && list3.size() > 0) {
                            Iterator<g.i.a.b.e.a> it3 = b.this.f5328c.iterator();
                            while (it3.hasNext()) {
                                Objects.requireNonNull(it3.next());
                            }
                            break;
                        }
                        Objects.requireNonNull(b.this);
                        i.V3(false, "no callback registed");
                        break;
                    }
                    break;
                case "android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED":
                    BluetoothDevice bluetoothDevice4 = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
                    int intExtra6 = intent.getIntExtra("android.bluetooth.profile.extra.STATE", -1);
                    int intExtra7 = intent.getIntExtra("android.bluetooth.profile.extra.PREVIOUS_STATE", -1);
                    if (bluetoothDevice4 != null) {
                        i.w0(String.format(Locale.US, "action=%s, device:%s, state: %d->%d", action, i.S0(bluetoothDevice4.getAddress(), true), Integer.valueOf(intExtra7), Integer.valueOf(intExtra6)));
                        List<g.i.a.b.e.a> list4 = b.this.f5328c;
                        if (list4 != null && list4.size() > 0) {
                            Iterator<g.i.a.b.e.a> it4 = b.this.f5328c.iterator();
                            while (it4.hasNext()) {
                                it4.next().a(bluetoothDevice4, intExtra6);
                            }
                            break;
                        }
                        Objects.requireNonNull(b.this);
                        i.V3(false, "no callback registed");
                        break;
                    }
                    break;
                case "android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED":
                    BluetoothDevice bluetoothDevice5 = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
                    int intExtra8 = intent.getIntExtra("android.bluetooth.profile.extra.STATE", -1);
                    int intExtra9 = intent.getIntExtra("android.bluetooth.profile.extra.PREVIOUS_STATE", -1);
                    if (bluetoothDevice5 != null) {
                        i.w0(String.format(Locale.US, "%s: action=%s, state: %d->%d", i.S0(bluetoothDevice5.getAddress(), true), action, Integer.valueOf(intExtra9), Integer.valueOf(intExtra8)));
                        List<g.i.a.b.e.a> list5 = b.this.f5328c;
                        if (list5 != null && list5.size() > 0) {
                            Iterator<g.i.a.b.e.a> it5 = b.this.f5328c.iterator();
                            while (it5.hasNext()) {
                                Objects.requireNonNull(it5.next());
                            }
                            break;
                        }
                        Objects.requireNonNull(b.this);
                        i.V3(false, "no callback registed");
                        break;
                    }
                    break;
                case "android.bluetooth.avrcp-controller.profile.action.CONNECTION_STATE_CHANGED":
                    BluetoothDevice bluetoothDevice6 = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
                    int intExtra10 = intent.getIntExtra("android.bluetooth.profile.extra.STATE", -1);
                    int intExtra11 = intent.getIntExtra("android.bluetooth.profile.extra.PREVIOUS_STATE", -1);
                    if (bluetoothDevice6 != null) {
                        i.w0(String.format(Locale.US, "%s: action=%s, state: %d->%d", i.S0(bluetoothDevice6.getAddress(), true), action, Integer.valueOf(intExtra11), Integer.valueOf(intExtra10)));
                        break;
                    }
                    break;
                case "android.bluetooth.headset.action.VENDOR_SPECIFIC_HEADSET_EVENT":
                    b bVar = b.this;
                    bVar.getClass();
                    if (((BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE")) != null) {
                        if (intent.getStringExtra("android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_CMD") != null) {
                            int intExtra12 = intent.getIntExtra("android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE", -1);
                            if (intExtra12 == 0 && intExtra12 == 1 && intExtra12 == 2 && intExtra12 == 3 && intExtra12 == 4) {
                                List<g.i.a.b.e.a> list6 = bVar.f5328c;
                                if (list6 != null && list6.size() > 0) {
                                    Iterator<g.i.a.b.e.a> it6 = bVar.f5328c.iterator();
                                    while (it6.hasNext()) {
                                        Objects.requireNonNull(it6.next());
                                    }
                                }
                                i.V3(false, "no callback registed");
                            } else {
                                str = "onVendorSpecificHeadsetEvent() unknown command";
                            }
                        } else {
                            str = "onVendorSpecificHeadsetEvent() command is null";
                        }
                        break;
                    } else {
                        str = "onVendorSpecificHeadsetEvent() remote device is null";
                    }
                    i.U3(str);
                    break;
            }
        }
    }

    /* JADX INFO: renamed from: g.i.a.b.e.b$b, reason: collision with other inner class name */
    public class C0073b implements BluetoothProfile.ServiceListener {
        public C0073b() {
        }

        @Override // android.bluetooth.BluetoothProfile.ServiceListener
        public void onServiceConnected(int i2, BluetoothProfile bluetoothProfile) {
            i.w0(i.U2(i2) + " profile connected");
            if (i2 == 11) {
                b.this.f5333h = bluetoothProfile;
                return;
            }
            if (i2 == 12) {
                b.this.f5334i = bluetoothProfile;
                return;
            }
            if (i2 == 1) {
                b.this.f5330e = (BluetoothHeadset) bluetoothProfile;
                return;
            }
            if (i2 == 2) {
                b.this.f5331f = (BluetoothA2dp) bluetoothProfile;
            } else if (i2 == 3) {
                Objects.requireNonNull(b.this);
            } else {
                if (i2 != 4) {
                    return;
                }
                b.this.f5332g = bluetoothProfile;
            }
        }

        @Override // android.bluetooth.BluetoothProfile.ServiceListener
        public void onServiceDisconnected(int i2) {
            i.w0(i.U2(i2) + " profile disconnected");
            if (i2 == 1) {
                b.this.f5330e = null;
                return;
            }
            if (i2 == 2) {
                b.this.f5331f = null;
                return;
            }
            if (i2 == 3) {
                Objects.requireNonNull(b.this);
                return;
            }
            if (i2 == 4) {
                b.this.f5332g = null;
            } else if (i2 == 11) {
                b.this.f5333h = null;
            } else if (i2 == 12) {
                b.this.f5334i = null;
            }
        }
    }

    public b(Context context) {
        String str;
        this.f5335j = null;
        Context applicationContext = context.getApplicationContext();
        this.f5327b = applicationContext;
        if (applicationContext != null) {
            if (this.f5329d == null) {
                BluetoothManager bluetoothManager = (BluetoothManager) applicationContext.getSystemService("bluetooth");
                if (bluetoothManager == null) {
                    str = "Unable to initialize BluetoothManager.";
                } else {
                    BluetoothAdapter adapter = bluetoothManager.getAdapter();
                    this.f5329d = adapter;
                    if (adapter == null) {
                        str = "Unable to obtain a BluetoothAdapter.";
                    }
                }
            }
            if (this.f5329d == null) {
                i.W3("mBluetoothAdapter == null");
                return;
            }
            this.f5335j = new a();
            IntentFilter intentFilter = new IntentFilter();
            f(2);
            intentFilter.addAction("android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED");
            intentFilter.addAction("android.bluetooth.a2dp.profile.action.PLAYING_STATE_CHANGED");
            f(1);
            intentFilter.addAction("android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED");
            intentFilter.addAction("android.bluetooth.headset.profile.action.AUDIO_STATE_CHANGED");
            intentFilter.addAction("android.bluetooth.headset.action.VENDOR_SPECIFIC_HEADSET_EVENT");
            f(4);
            intentFilter.addAction("android.bluetooth.input.profile.action.CONNECTION_STATE_CHANGED");
            f(11);
            intentFilter.addAction("android.bluetooth.a2dp-sink.profile.action.CONNECTION_STATE_CHANGED");
            intentFilter.addAction("android.bluetooth.a2dp-sink.profile.action.PLAYING_STATE_CHANGED");
            f(12);
            intentFilter.addAction("android.bluetooth.avrcp-controller.profile.action.CONNECTION_STATE_CHANGED");
            this.f5327b.registerReceiver(this.f5335j, intentFilter);
            return;
        }
        str = "not intialized";
        i.W3(str);
    }

    public static void g(Context context) {
        if (a == null) {
            synchronized (b.class) {
                if (a == null) {
                    a = new b(context);
                }
            }
        }
    }

    public void a(g.i.a.b.e.a aVar) {
        if (aVar == null) {
            return;
        }
        if (this.f5328c == null) {
            this.f5328c = new CopyOnWriteArrayList();
        }
        if (!this.f5328c.contains(aVar)) {
            this.f5328c.add(aVar);
        }
        StringBuilder sbF = g.a.a.a.a.F("mManagerCallbacks.size=");
        sbF.append(this.f5328c.size());
        i.x0(false, sbF.toString());
    }

    public boolean b(BluetoothDevice bluetoothDevice) {
        BluetoothProfile bluetoothProfile;
        String str;
        if (Build.VERSION.SDK_INT >= 28) {
            bluetoothProfile = this.f5332g;
            str = "android.bluetooth.BluetoothHidHost";
        } else {
            bluetoothProfile = this.f5332g;
            str = "android.bluetooth.BluetoothInputDevice";
        }
        return i.Q(bluetoothProfile, str, bluetoothDevice);
    }

    public boolean c(BluetoothDevice bluetoothDevice) {
        String str;
        if (bluetoothDevice == null) {
            str = "device is null";
        } else {
            BluetoothA2dp bluetoothA2dp = this.f5331f;
            if (bluetoothA2dp == null) {
                str = "A2DP not initialized";
            } else {
                if (bluetoothA2dp.getConnectionState(bluetoothDevice) == 2) {
                    BluetoothA2dp bluetoothA2dp2 = this.f5331f;
                    if (bluetoothA2dp2 != null) {
                        try {
                            bluetoothA2dp2.getClass().getMethod("setPriority", BluetoothDevice.class, Integer.TYPE).invoke(bluetoothA2dp2, bluetoothDevice, 100);
                        } catch (Exception e2) {
                            e2.printStackTrace();
                        }
                    }
                    return i.B0(this.f5331f, bluetoothDevice);
                }
                str = "A2DP already disconnected";
            }
        }
        i.W3(str);
        return false;
    }

    public boolean d(String str) {
        String str2;
        BluetoothDevice remoteDevice = this.f5329d.getRemoteDevice(str);
        if (remoteDevice == null) {
            return false;
        }
        BluetoothHeadset bluetoothHeadset = this.f5330e;
        if (bluetoothHeadset == null) {
            str2 = "BluetoothHeadset service is not connected";
        } else {
            if (bluetoothHeadset.getConnectionState(remoteDevice) == 2) {
                return i.B0(this.f5330e, remoteDevice);
            }
            str2 = "BluetoothHeadset profile is not connected";
        }
        i.W3(str2);
        return false;
    }

    /* JADX WARN: Removed duplicated region for block: B:30:0x003d  */
    /* JADX WARN: Removed duplicated region for block: B:36:0x0051  */
    /* JADX WARN: Removed duplicated region for block: B:39:0x005a  */
    /* JADX WARN: Removed duplicated region for block: B:41:0x0061  */
    /* JADX WARN: Removed duplicated region for block: B:44:0x006a  */
    /* JADX WARN: Removed duplicated region for block: B:46:0x006f  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public int e(int r3, android.bluetooth.BluetoothDevice r4) {
        /*
            r2 = this;
            android.bluetooth.BluetoothAdapter r0 = r2.f5329d
            if (r0 == 0) goto L76
            boolean r0 = r0.isEnabled()
            if (r0 != 0) goto Lb
            goto L76
        Lb:
            r0 = 1
            if (r3 == r0) goto L1d
            r0 = 2
            if (r3 == r0) goto L2b
            r0 = 4
            if (r3 == r0) goto L39
            r0 = 11
            if (r3 == r0) goto L56
            r0 = 12
            if (r3 == r0) goto L66
            goto L74
        L1d:
            android.bluetooth.BluetoothHeadset r3 = r2.f5330e
            if (r3 == 0) goto L26
            int r3 = r3.getConnectionState(r4)
            return r3
        L26:
            java.lang.String r3 = "HEADSET profile not connected"
            g.h.a.i.w0(r3)
        L2b:
            android.bluetooth.BluetoothA2dp r3 = r2.f5331f
            if (r3 == 0) goto L34
            int r3 = r3.getConnectionState(r4)
            return r3
        L34:
            java.lang.String r3 = "A2DP profile not connected"
            g.h.a.i.w0(r3)
        L39:
            android.bluetooth.BluetoothProfile r3 = r2.f5332g
            if (r3 == 0) goto L51
            int r0 = android.os.Build.VERSION.SDK_INT
            r1 = 28
            if (r0 < r1) goto L4a
            java.lang.String r0 = "android.bluetooth.BluetoothHidHost"
            int r3 = g.h.a.i.c1(r3, r0, r4)
            return r3
        L4a:
            java.lang.String r0 = "android.bluetooth.BluetoothInputDevice"
            int r3 = g.h.a.i.c1(r3, r0, r4)
            return r3
        L51:
            java.lang.String r3 = "HID_HOST profile not connected"
            g.h.a.i.w0(r3)
        L56:
            android.bluetooth.BluetoothProfile r3 = r2.f5333h
            if (r3 == 0) goto L61
            java.lang.String r0 = "android.bluetooth.BluetoothA2dpSink"
            int r3 = g.h.a.i.c1(r3, r0, r4)
            return r3
        L61:
            java.lang.String r3 = "A2DP_SINK profile not connected"
            g.h.a.i.w0(r3)
        L66:
            android.bluetooth.BluetoothProfile r3 = r2.f5334i
            if (r3 == 0) goto L6f
            int r3 = r3.getConnectionState(r4)
            return r3
        L6f:
            java.lang.String r3 = "AVRCP profile not connected"
            g.h.a.i.w0(r3)
        L74:
            r3 = 0
            return r3
        L76:
            java.lang.String r3 = "BT not enabled"
            g.h.a.i.w0(r3)
            r3 = -1
            return r3
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i.a.b.e.b.e(int, android.bluetooth.BluetoothDevice):int");
    }

    public boolean f(int i2) {
        try {
            if (this.f5329d.getProfileProxy(this.f5327b, this.f5336k, i2)) {
                return true;
            }
            i.W3(String.format(Locale.US, "getProfileProxy %d failed", Integer.valueOf(i2)));
            return false;
        } catch (Exception e2) {
            i.W3(String.format(Locale.US, "getProfileProxy %d exception: %s", Integer.valueOf(i2), e2.toString()));
            return false;
        }
    }
}
