package g.i.a.b.e;

import android.annotation.TargetApi;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import g.h.a.i;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

/* JADX INFO: loaded from: classes.dex */
@TargetApi(18)
public class c {
    public static boolean a = true;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static c f5337b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public boolean f5338c;

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

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

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final Object f5345j = new Object();

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public HashMap<String, BluetoothGatt> f5342g = new HashMap<>();

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public HashMap<String, Integer> f5344i = new HashMap<>();

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public HashMap<String, List<BluetoothGattCallback>> f5343h = new HashMap<>();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public List<String> f5341f = new CopyOnWriteArrayList();

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

        @Override // android.bluetooth.BluetoothGattCallback
        public void onCharacteristicChanged(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
            String address = bluetoothGatt.getDevice().getAddress();
            byte[] value = bluetoothGattCharacteristic.getValue();
            if (c.this.f5338c) {
                i.w0(value != null ? String.format(Locale.US, "<< %s\n(%d)%s", bluetoothGattCharacteristic.getUuid(), Integer.valueOf(value.length), i.y(value)) : String.format(Locale.US, "<< %s", bluetoothGattCharacteristic.getUuid()));
            }
            List<BluetoothGattCallback> list = c.this.f5343h.get(address);
            if (list == null || list.size() <= 0) {
                return;
            }
            Iterator<BluetoothGattCallback> it = list.iterator();
            while (it.hasNext()) {
                it.next().onCharacteristicChanged(bluetoothGatt, bluetoothGattCharacteristic);
            }
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onCharacteristicRead(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i2) {
            byte[] value = bluetoothGattCharacteristic.getValue();
            i.w0(c.this.f5338c ? value != null ? String.format(Locale.US, "%s << %s\n:\t(%d)%s", i.S2(i2), bluetoothGattCharacteristic.getUuid(), Integer.valueOf(value.length), i.y(value)) : String.format(Locale.US, "%s << %s", i.S2(i2), bluetoothGattCharacteristic.getUuid()) : value != null ? String.format(Locale.US, "%s << (%d)", i.S2(i2), Integer.valueOf(value.length)) : String.format(Locale.US, "%s <<", i.S2(i2)));
            synchronized (c.this.f5345j) {
                Objects.requireNonNull(c.this);
                c.this.f5345j.notifyAll();
            }
            List<BluetoothGattCallback> list = c.this.f5343h.get(bluetoothGatt.getDevice().getAddress());
            if (list == null || list.size() <= 0) {
                return;
            }
            Iterator<BluetoothGattCallback> it = list.iterator();
            while (it.hasNext()) {
                it.next().onCharacteristicRead(bluetoothGatt, bluetoothGattCharacteristic, i2);
            }
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onCharacteristicWrite(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i2) {
            String address = bluetoothGatt.getDevice().getAddress();
            byte[] value = bluetoothGattCharacteristic.getValue();
            if (c.this.f5338c) {
                i.w0(value != null ? String.format(Locale.US, "%s << %s\n(%d)%s", i.S2(i2), bluetoothGattCharacteristic.getUuid(), Integer.valueOf(value.length), i.y(value)) : String.format(Locale.US, "%s << %s", i.S2(i2), bluetoothGattCharacteristic.getUuid()));
            }
            synchronized (c.this.f5345j) {
                Objects.requireNonNull(c.this);
                c.this.f5345j.notifyAll();
            }
            List<BluetoothGattCallback> list = c.this.f5343h.get(address);
            if (list == null || list.size() <= 0) {
                return;
            }
            Iterator<BluetoothGattCallback> it = list.iterator();
            while (it.hasNext()) {
                it.next().onCharacteristicWrite(bluetoothGatt, bluetoothGattCharacteristic, i2);
            }
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onConnectionStateChange(BluetoothGatt bluetoothGatt, int i2, int i3) {
            BluetoothDevice device = bluetoothGatt.getDevice();
            if (device == null) {
                return;
            }
            String address = device.getAddress();
            Locale locale = Locale.US;
            Object[] objArr = new Object[3];
            objArr[0] = i.S0(address, true);
            objArr[1] = i2 != 0 ? i2 != 1 ? i2 != 8 ? i2 != 19 ? i2 != 22 ? i2 != 34 ? i2 != 62 ? i2 != 133 ? i2 != 256 ? g.a.a.a.a.n("UNKNOWN (", i2, ")") : "0x0100-GATT_CONN_CANCEL" : "0x0085-GATT_ERROR" : "0x3E-GATT_CONN_FAIL_ESTABLISH" : "0x22-GATT_CONN_LMP_TIMEOUT" : "0x16-GATT_CONN_TERMINATE_LOCAL_HOST" : "0x13-GATT_CONN_TERMINATE_PEER_USER" : "0x08-GATT_CONN_TIMEOUT" : "0x01-GATT_CONN_L2C_FAILURE" : "0x0000 - SUCCESS";
            objArr[2] = i3 != 0 ? i3 != 1 ? i3 != 2 ? i3 != 3 ? g.a.a.a.a.n("UNKNOWN (", i3, ")") : "3-BluetoothProfile.STATE_DISCONNECTING" : "2-BluetoothProfile.STATE_CONNECTED" : "1-BluetoothProfile.STATE_CONNECTING" : "0-BluetoothProfile.STATE_DISCONNECTED";
            i.U3(String.format(locale, "%s, status: %s , newState: %s", objArr));
            if (i2 == 0 && i3 == 2) {
                c.this.f5344i.put(address, 2);
                c.this.f5342g.put(address, bluetoothGatt);
            } else {
                c.this.f5344i.put(address, 0);
            }
            List<BluetoothGattCallback> list = c.this.f5343h.get(address);
            if (list == null || list.size() <= 0) {
                return;
            }
            Iterator<BluetoothGattCallback> it = list.iterator();
            while (it.hasNext()) {
                it.next().onConnectionStateChange(bluetoothGatt, i2, i3);
            }
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onDescriptorRead(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, int i2) {
            super.onDescriptorRead(bluetoothGatt, bluetoothGattDescriptor, i2);
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onDescriptorWrite(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, int i2) {
            String address = bluetoothGatt.getDevice().getAddress();
            UUID uuid = bluetoothGattDescriptor.getCharacteristic().getUuid();
            byte[] value = bluetoothGattDescriptor.getValue();
            if (c.this.f5338c) {
                i.w0(value != null ? String.format(Locale.US, "%s>> {\nCharacteristic:%s\nDescriptor:%s\nvalue:(%d)%s\n}", i.S2(i2), uuid, bluetoothGattDescriptor.getUuid(), Integer.valueOf(value.length), i.y(value)) : String.format(Locale.US, "%s>> {\nCharacteristic:%s\nDescriptor:%s}", i.S2(i2), uuid, bluetoothGattDescriptor.getUuid()));
            }
            synchronized (c.this.f5345j) {
                Objects.requireNonNull(c.this);
                c.this.f5345j.notifyAll();
            }
            List<BluetoothGattCallback> list = c.this.f5343h.get(address);
            if (list == null || list.size() <= 0) {
                return;
            }
            Iterator<BluetoothGattCallback> it = list.iterator();
            while (it.hasNext()) {
                it.next().onDescriptorWrite(bluetoothGatt, bluetoothGattDescriptor, i2);
            }
        }

        @Override // android.bluetooth.BluetoothGattCallback
        @TargetApi(21)
        public void onMtuChanged(BluetoothGatt bluetoothGatt, int i2, int i3) {
            String address = bluetoothGatt.getDevice().getAddress();
            i.w0(String.format(Locale.US, "%s << mtu=%d, addr=%s", i.S2(i3), Integer.valueOf(i2), i.S0(address, true)));
            List<BluetoothGattCallback> list = c.this.f5343h.get(address);
            if (list == null || list.size() <= 0) {
                return;
            }
            Iterator<BluetoothGattCallback> it = list.iterator();
            while (it.hasNext()) {
                it.next().onMtuChanged(bluetoothGatt, i2, i3);
            }
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onPhyRead(BluetoothGatt bluetoothGatt, int i2, int i3, int i4) {
            super.onPhyRead(bluetoothGatt, i2, i3, i4);
            String address = bluetoothGatt.getDevice().getAddress();
            i.w0(String.format(Locale.US, "%s << %s: txPhy=%d, rxPhy=%d", i.S0(address, true), i.S2(i4), Integer.valueOf(i2), Integer.valueOf(i3)));
            List<BluetoothGattCallback> list = c.this.f5343h.get(address);
            if (list == null || list.size() <= 0) {
                return;
            }
            Iterator<BluetoothGattCallback> it = list.iterator();
            while (it.hasNext()) {
                it.next().onPhyRead(bluetoothGatt, i2, i3, i4);
            }
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onPhyUpdate(BluetoothGatt bluetoothGatt, int i2, int i3, int i4) {
            super.onPhyUpdate(bluetoothGatt, i2, i3, i4);
            String address = bluetoothGatt.getDevice().getAddress();
            i.w0(String.format(Locale.US, "%s << %s: txPhy=%d, rxPhy=%d", i.S0(address, true), i.S2(i4), Integer.valueOf(i2), Integer.valueOf(i3)));
            List<BluetoothGattCallback> list = c.this.f5343h.get(address);
            if (list == null || list.size() <= 0) {
                return;
            }
            Iterator<BluetoothGattCallback> it = list.iterator();
            while (it.hasNext()) {
                it.next().onPhyUpdate(bluetoothGatt, i2, i3, i4);
            }
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onReadRemoteRssi(BluetoothGatt bluetoothGatt, int i2, int i3) {
            super.onReadRemoteRssi(bluetoothGatt, i2, i3);
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onReliableWriteCompleted(BluetoothGatt bluetoothGatt, int i2) {
            super.onReliableWriteCompleted(bluetoothGatt, i2);
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onServicesDiscovered(BluetoothGatt bluetoothGatt, int i2) {
            String address = bluetoothGatt.getDevice().getAddress();
            i.w0(String.format(Locale.US, "%s << addr=%s", i.S2(i2), i.S0(address, true)));
            boolean z2 = c.a;
            List<BluetoothGattCallback> list = c.this.f5343h.get(address);
            if (list == null || list.size() <= 0) {
                return;
            }
            Iterator<BluetoothGattCallback> it = list.iterator();
            while (it.hasNext()) {
                it.next().onServicesDiscovered(bluetoothGatt, i2);
            }
        }
    }

    static {
        UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0044  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public c(android.content.Context r2) {
        /*
            r1 = this;
            r1.<init>()
            r0 = 0
            r1.f5338c = r0
            java.lang.Object r0 = new java.lang.Object
            r0.<init>()
            r1.f5345j = r0
            r1.f5346k = r2
            java.util.HashMap r2 = new java.util.HashMap
            r2.<init>()
            r1.f5342g = r2
            java.util.HashMap r2 = new java.util.HashMap
            r2.<init>()
            r1.f5344i = r2
            java.util.HashMap r2 = new java.util.HashMap
            r2.<init>()
            r1.f5343h = r2
            java.util.concurrent.CopyOnWriteArrayList r2 = new java.util.concurrent.CopyOnWriteArrayList
            r2.<init>()
            r1.f5341f = r2
            boolean r2 = g.h.a.i.f5270b
            r1.f5338c = r2
            android.bluetooth.BluetoothManager r2 = r1.f5339d
            if (r2 != 0) goto L44
            android.content.Context r2 = r1.f5346k
            java.lang.String r0 = "bluetooth"
            java.lang.Object r2 = r2.getSystemService(r0)
            android.bluetooth.BluetoothManager r2 = (android.bluetooth.BluetoothManager) r2
            r1.f5339d = r2
            if (r2 != 0) goto L44
            java.lang.String r2 = "BLUETOOTH_SERVICE not supported."
            goto L54
        L44:
            android.bluetooth.BluetoothAdapter r2 = r1.f5340e
            if (r2 != 0) goto L58
            android.bluetooth.BluetoothManager r2 = r1.f5339d
            android.bluetooth.BluetoothAdapter r2 = r2.getAdapter()
            r1.f5340e = r2
            if (r2 != 0) goto L58
            java.lang.String r2 = "BluetoothAdapter is not supported"
        L54:
            g.h.a.i.W3(r2)
            goto L5d
        L58:
            java.lang.String r2 = "initialize success"
            g.h.a.i.w0(r2)
        L5d:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i.a.b.e.c.<init>(android.content.Context):void");
    }

    public static synchronized void h(Context context) {
        if (f5337b == null) {
            synchronized (c.class) {
                if (f5337b == null) {
                    f5337b = new c(context.getApplicationContext());
                }
            }
        }
    }

    public void a(String str) {
        if (str == null) {
            return;
        }
        BluetoothGatt bluetoothGatt = this.f5342g.get(str);
        if (bluetoothGatt != null) {
            if (i(str)) {
                if (this.f5338c) {
                    i.U3("disconnect : " + str);
                }
                bluetoothGatt.disconnect();
                try {
                    Thread.sleep(500L);
                } catch (InterruptedException e2) {
                    e2.printStackTrace();
                }
            }
            if (a) {
                bluetoothGatt.close();
            }
            this.f5342g.remove(str);
        }
        HashMap<String, List<BluetoothGattCallback>> map = this.f5343h;
        if (map != null) {
            map.remove(str);
        }
        List<String> list = this.f5341f;
        if (list == null || !list.contains(str)) {
            return;
        }
        this.f5341f.remove(str);
    }

    public synchronized void b(String str) {
        c(str, a);
    }

    public synchronized void c(String str, boolean z2) {
        BluetoothGatt bluetoothGatt;
        if (str == null) {
            i.x0(this.f5338c, "Invalid address");
            return;
        }
        HashMap<String, BluetoothGatt> map = this.f5342g;
        if (map != null) {
            if (z2 && (bluetoothGatt = map.get(str)) != null) {
                bluetoothGatt.close();
            }
            this.f5342g.remove(str);
        }
        HashMap<String, List<BluetoothGattCallback>> map2 = this.f5343h;
        if (map2 != null) {
            map2.remove(str);
        }
        List<String> list = this.f5341f;
        if (list != null) {
            list.remove(str);
        }
    }

    public boolean d(String str, BluetoothGattCallback bluetoothGattCallback) {
        return e(str, false, 2, 1, bluetoothGattCallback);
    }

    public boolean e(String str, boolean z2, int i2, int i3, BluetoothGattCallback bluetoothGattCallback) {
        BluetoothGatt bluetoothGatt;
        BluetoothAdapter bluetoothAdapter = this.f5340e;
        if (bluetoothAdapter == null) {
            i.W3("BluetoothAdapter not initialized");
            return false;
        }
        if (str == null) {
            i.W3("unspecified address.");
            return false;
        }
        BluetoothDevice remoteDevice = bluetoothAdapter.getRemoteDevice(str);
        if (remoteDevice == null) {
            i.W3("Device not found.  Unable to connect.");
            return false;
        }
        if (this.f5341f.contains(str) && (bluetoothGatt = this.f5342g.get(str)) != null) {
            if (i(str)) {
                if (this.f5338c) {
                    StringBuilder sbF = g.a.a.a.a.F("already connected, addr=");
                    sbF.append(i.S0(str, true));
                    i.U3(sbF.toString());
                }
                j(str, bluetoothGattCallback);
                if (bluetoothGattCallback != null) {
                    bluetoothGattCallback.onConnectionStateChange(bluetoothGatt, 0, 2);
                }
                return true;
            }
            if (z2) {
                j(str, bluetoothGattCallback);
                if (this.f5338c) {
                    i.U3("re-connect previous device: " + str);
                }
                if (bluetoothGatt.connect()) {
                    this.f5344i.put(str, 1);
                    return true;
                }
                i.w0("reconnect failed.");
                b(str);
                return false;
            }
            b(str);
        }
        if (this.f5338c) {
            StringBuilder sbF2 = g.a.a.a.a.F("create connection to ");
            sbF2.append(i.S0(str, true));
            i.U3(sbF2.toString());
        }
        j(str, bluetoothGattCallback);
        this.f5344i.put(str, 1);
        BluetoothGatt bluetoothGattConnectGatt = remoteDevice.connectGatt(this.f5346k, z2, new a(), i2, i3);
        if (bluetoothGattConnectGatt == null) {
            i.w0("BluetoothGatt not exist.  Unable to connect.");
            this.f5344i.put(str, 0);
            b(str);
            return false;
        }
        this.f5342g.put(str, bluetoothGattConnectGatt);
        if (!this.f5341f.contains(str)) {
            this.f5341f.add(str);
        }
        return true;
    }

    public BluetoothGatt f(String str) {
        return this.f5342g.get(str);
    }

    public List<BluetoothGattCallback> g(String str) {
        HashMap<String, List<BluetoothGattCallback>> map = this.f5343h;
        if (map != null) {
            return map.get(str);
        }
        return null;
    }

    public boolean i(String str) {
        Integer num = this.f5344i.get(str);
        return num != null && num.intValue() == 2;
    }

    public synchronized void j(String str, BluetoothGattCallback bluetoothGattCallback) {
        List<BluetoothGattCallback> listG = g(str);
        if (listG == null) {
            CopyOnWriteArrayList copyOnWriteArrayList = new CopyOnWriteArrayList();
            copyOnWriteArrayList.add(bluetoothGattCallback);
            this.f5343h.put(str, copyOnWriteArrayList);
        } else {
            if (!listG.contains(bluetoothGattCallback)) {
                listG.add(bluetoothGattCallback);
                this.f5343h.put(str, listG);
            }
        }
    }

    public synchronized void k(String str, BluetoothGattCallback bluetoothGattCallback) {
        List<BluetoothGattCallback> listG = g(str);
        if (listG != null) {
            if (listG.contains(bluetoothGattCallback)) {
                listG.remove(bluetoothGattCallback);
                this.f5343h.put(str, listG);
            }
        } else {
            if (this.f5338c) {
                i.U3("callback not registered, addr= " + i.S0(str, true));
            }
        }
    }
}
