package g.i.a.c.a0;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import g.h.a.i;
import g.i.a.c.a0.c;
import g.i.a.c.p.a;
import g.i.a.c.p.f;
import io.netty.handler.traffic.AbstractTrafficShapingHandler;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
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 */
public class e extends g.i.a.c.a0.a implements f {
    public static volatile e E;
    public g.i.a.b.e.c F;
    public BluetoothGatt G;
    public BluetoothGattService H;
    public BluetoothGattService I;
    public BluetoothGattCharacteristic J;
    public g.i.a.c.p.a K;
    public a.c L = new a();
    public Runnable M = new b();
    public Runnable N = new c();
    public Runnable O = new d();
    public Handler P = new Handler(Looper.getMainLooper());
    public BluetoothGattCallback Q = new C0078e();

    public class a implements a.c {
        public a() {
        }
    }

    public class b implements Runnable {
        public b() {
        }

        @Override // java.lang.Runnable
        public void run() {
            e eVar = e.this;
            synchronized (eVar.f5465s) {
                try {
                    eVar.f5465s.wait(AbstractTrafficShapingHandler.DEFAULT_MAX_TIME);
                } catch (InterruptedException e2) {
                    i.w0("wait sync data interrupted: " + e2.toString());
                }
            }
            try {
                Thread.sleep(800L);
            } catch (InterruptedException e3) {
                e3.printStackTrace();
            }
            e.k(e.this);
        }
    }

    public class c implements Runnable {
        public c() {
        }

        @Override // java.lang.Runnable
        public void run() {
            e.k(e.this);
        }
    }

    public class d implements Runnable {
        public d() {
        }

        @Override // java.lang.Runnable
        public void run() {
            Thread thread;
            BluetoothDevice bluetoothDeviceH;
            e eVar = e.this;
            if (eVar.f5466t != 536) {
                g.a.a.a.a.m0(g.a.a.a.a.F("ignore state:"), e.this.f5466t);
                return;
            }
            String str = eVar.B;
            int bondState = 10;
            if (eVar.f5447w != null && (bluetoothDeviceH = eVar.h(str)) != null) {
                bondState = bluetoothDeviceH.getBondState();
            }
            eVar.A = bondState;
            if (e.this.A == 11) {
                i.U3("BOND_BONDING: wait to discover service");
                thread = new Thread(e.this.M);
            } else {
                StringBuilder sbF = g.a.a.a.a.F(">> mBondState: ");
                sbF.append(e.this.A);
                i.U3(sbF.toString());
                thread = new Thread(e.this.N);
            }
            thread.start();
        }
    }

    /* JADX INFO: renamed from: g.i.a.c.a0.e$e, reason: collision with other inner class name */
    public class C0078e extends BluetoothGattCallback {
        public C0078e() {
        }

        public final void a() {
            if (!e.this.b()) {
                e.this.e(4097);
            } else {
                e.this.d();
                e.this.f(new g.i.a.c.g.a(0));
            }
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onCharacteristicRead(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i2) {
            super.onCharacteristicRead(bluetoothGatt, bluetoothGattCharacteristic, i2);
            UUID uuid = bluetoothGattCharacteristic.getUuid();
            bluetoothGattCharacteristic.getValue();
            if (i2 != 0) {
                i.x0(e.this.f5458f, String.format("Characteristic read error:0x%04X ", Integer.valueOf(i2)));
                if (!f.f5635d.equals(uuid)) {
                    i.U3("ignore exctption when read other info");
                    return;
                } else {
                    if (e.this.b()) {
                        e.this.f(new g.i.a.c.g.a(5));
                        return;
                    }
                    return;
                }
            }
            byte[] value = bluetoothGattCharacteristic.getValue();
            if (f.f5635d.equals(uuid)) {
                ByteBuffer byteBufferWrap = ByteBuffer.wrap(value);
                byteBufferWrap.order(ByteOrder.LITTLE_ENDIAN);
                e.this.m(byteBufferWrap.getShort(0));
                e eVar = e.this;
                g.i.a.c.p.a aVar = eVar.K;
                if (aVar != null) {
                    aVar.e(eVar.B, eVar.G, eVar.H, eVar.I);
                }
            }
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onConnectionStateChange(BluetoothGatt bluetoothGatt, int i2, int i3) {
            if (i2 == 0) {
                if (i3 == 2) {
                    e eVar = e.this;
                    eVar.G = eVar.F.f(eVar.B);
                    g.i.a.c.a0.b bVar = e.this.f5462p;
                    if (bVar != null && bVar.f5456g) {
                        g.i.a.b.e.g.a.b(bluetoothGatt);
                    }
                    if (bluetoothGatt != null) {
                        e eVar2 = e.this;
                        if (eVar2.f5466t != 536) {
                            eVar2.e(536);
                            e eVar3 = e.this;
                            if (eVar3.P == null) {
                                i.V3(eVar3.f5458f, "mHandler == null");
                                return;
                            }
                            i.U3("delay to discover service for : 1600");
                            e eVar4 = e.this;
                            eVar4.P.removeCallbacks(eVar4.O);
                            e eVar5 = e.this;
                            boolean zPostDelayed = eVar5.P.postDelayed(eVar5.O, 1600L);
                            if (zPostDelayed) {
                                return;
                            }
                            g.a.a.a.a.l0("postDelayed:", zPostDelayed, e.this.f5458f);
                            return;
                        }
                        return;
                    }
                } else if (i3 != 0) {
                    return;
                } else {
                    e.this.a();
                }
            }
            a();
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onServicesDiscovered(BluetoothGatt bluetoothGatt, int i2) {
            e eVar = e.this;
            int i3 = eVar.f5466t;
            if (i3 == 1025) {
                i.w0("ignore, when it is ota processing");
                return;
            }
            if (i2 == 0) {
                eVar.e(539);
                if (i3 == 537) {
                    e.this.d();
                    return;
                }
                return;
            }
            i.W3("service discovery failed !!!");
            if (e.this.b()) {
                e.this.f(new g.i.a.c.g.a(1));
            }
        }
    }

    public e(Context context) {
        this.f5460n = context;
        o();
    }

    public static boolean k(e eVar) {
        boolean zDiscoverServices;
        BluetoothGattService service;
        BluetoothGatt bluetoothGatt;
        UUID uuid;
        BluetoothGattCharacteristic characteristic;
        if (eVar.f5466t == 537) {
            i.W3("discoverServices already started");
            return false;
        }
        eVar.e(537);
        i.V3(false, "discoverServices...");
        BluetoothGatt bluetoothGatt2 = eVar.G;
        if (bluetoothGatt2 != null) {
            zDiscoverServices = bluetoothGatt2.discoverServices();
        } else {
            i.w0("mBtGatt is null");
            zDiscoverServices = false;
        }
        if (!zDiscoverServices) {
            i.x0(eVar.f5459m, "discoverServices failed");
            if (!eVar.b()) {
                return false;
            }
            eVar.f(new g.i.a.c.g.a(1));
            return false;
        }
        synchronized (eVar.f5465s) {
            try {
                i.U3("wait discover service complete");
                eVar.f5465s.wait(30000L);
            } catch (InterruptedException e2) {
                i.x0(eVar.f5459m, e2.toString());
            }
        }
        if (eVar.f5466t == 537) {
            i.W3("discoverServices timeout");
            eVar.a();
            return false;
        }
        BluetoothGatt bluetoothGatt3 = eVar.G;
        if (bluetoothGatt3 != null) {
            g.i.a.c.a0.b bVar = eVar.f5462p;
            if (bVar != null) {
                service = bluetoothGatt3.getService(bVar.f5454e);
                bluetoothGatt = eVar.G;
                uuid = eVar.f5462p.f5455f;
            } else {
                service = bluetoothGatt3.getService(f.f5634c);
                bluetoothGatt = eVar.G;
                uuid = f.f5636e;
            }
            BluetoothGattService service2 = bluetoothGatt.getService(uuid);
            eVar.H = service;
            eVar.I = service2;
            eVar.e(540);
            if (service == null) {
                boolean z2 = eVar.f5458f;
                StringBuilder sbF = g.a.a.a.a.F("not find OTA_SERVICE = ");
                sbF.append(f.f5634c);
                i.x0(z2, sbF.toString());
                characteristic = null;
            } else {
                boolean z3 = eVar.f5458f;
                StringBuilder sbF2 = g.a.a.a.a.F("find OTA_SERVICE = ");
                sbF2.append(f.f5634c);
                i.V3(z3, sbF2.toString());
                characteristic = service.getCharacteristic(f.f5635d);
            }
            eVar.J = characteristic;
            if (characteristic != null) {
                boolean z4 = eVar.f5458f;
                StringBuilder sbF3 = g.a.a.a.a.F("find CHARACTERISTIC_PROTOCOL_TYPE = ");
                sbF3.append(f.f5635d);
                i.V3(z4, sbF3.toString());
                BluetoothGattCharacteristic bluetoothGattCharacteristic = eVar.J;
                if (eVar.G == null || bluetoothGattCharacteristic == null) {
                    i.W3("mBtGatt is null maybe disconnected just now");
                } else {
                    if (eVar.f5458f) {
                        i.U3(String.format(Locale.US, "readCharacteristic:(%d) %s", Integer.valueOf(bluetoothGattCharacteristic.getInstanceId()), bluetoothGattCharacteristic.getUuid().toString()));
                    }
                    eVar.G.readCharacteristic(bluetoothGattCharacteristic);
                }
            } else {
                eVar.m(0);
                g.i.a.c.p.a aVar = eVar.K;
                if (aVar != null) {
                    aVar.e(eVar.B, eVar.G, eVar.H, eVar.I);
                }
            }
        }
        return true;
    }

    public static e p(Context context) {
        if (E == null) {
            synchronized (e.class) {
                if (E == null) {
                    E = new e(context.getApplicationContext());
                }
            }
        }
        return E;
    }

    @Override // g.i.a.c.a0.c
    public void a() {
        boolean z2 = false;
        this.f5463q = 0;
        Handler handler = this.f5467u;
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
        String str = this.B;
        if (str != null) {
            g.i.a.b.e.c cVar = this.F;
            if (cVar != null) {
                if (cVar.i(str)) {
                    g.i.a.b.e.c cVar2 = this.F;
                    String str2 = this.B;
                    BluetoothGattCallback bluetoothGattCallback = this.Q;
                    List<BluetoothGattCallback> listG = cVar2.g(str2);
                    if (listG != null && listG.contains(bluetoothGattCallback)) {
                        z2 = true;
                    }
                    if (z2) {
                        e(4096);
                        this.F.a(this.B);
                    } else {
                        i.V3(this.f5459m, "no gatt callback registered");
                    }
                } else {
                    i.U3("already disconnected");
                }
            }
            this.G = null;
        }
        i.w0("no device registered");
        e(4097);
        this.G = null;
    }

    public final boolean l(String str) {
        e(535);
        return this.F.d(str, this.Q);
    }

    public final void m(int i2) {
        g.i.a.c.p.a aVar;
        i.U3(String.format("protocolType=0x%04X", Integer.valueOf(i2)));
        g.i.a.c.p.a aVar2 = this.K;
        if (aVar2 != null) {
            aVar2.a();
        }
        if (i2 == 16) {
            aVar = new g.i.a.c.w.a();
        } else {
            if (i2 != 20) {
                if (i2 == 18) {
                    g.i.a.c.a0.b bVar = this.f5462p;
                    if (bVar != null) {
                        "BeeTgt02".equals(bVar.f5451b);
                    }
                    this.K = new g.i.a.c.r.a(i2);
                } else if (i2 == 19) {
                    g.i.a.c.a0.b bVar2 = this.f5462p;
                    this.K = new g.i.a.c.s.a(i2, bVar2 != null && "BeeTgt02".equals(bVar2.f5451b));
                } else {
                    g.i.a.c.a0.b bVar3 = this.f5462p;
                    aVar = new g.i.a.c.v.a(0, bVar3 != null && "BeeTgt02".equals(bVar3.f5451b));
                }
                this.K.d(this.f5462p, this.B, this.G, this.H, this.I, this.L);
            }
            aVar = new g.i.a.c.t.a();
        }
        this.K = aVar;
        this.K.d(this.f5462p, this.B, this.G, this.H, this.I, this.L);
    }

    public final boolean n(String str) {
        boolean zBooleanValue;
        Method method;
        BluetoothDevice bluetoothDeviceH = h(str);
        if (bluetoothDeviceH == null) {
            return false;
        }
        g.i.a.b.e.b bVar = g.i.a.b.e.b.a;
        Objects.requireNonNull(bVar);
        if (!(bVar.f5332g != null)) {
            i.W3("HID_HOST not supported");
            return false;
        }
        int bondState = bluetoothDeviceH.getBondState();
        if (bondState != 12) {
            g.a.a.a.a.f0("connect with not bond device, bond first, current state: ", bondState, this.f5458f);
            i.U3(String.format("notifyStateChanged 0x%04X >> 0x%04X", Integer.valueOf(this.f5466t), 532));
            this.f5466t = 532;
            c.b bVar2 = this.f5464r;
            if (bVar2 != null) {
                bVar2.onStateChanged(532);
            } else {
                i.V3(false, "no callback registered");
            }
            return bluetoothDeviceH.createBond();
        }
        if (r(bluetoothDeviceH.getAddress())) {
            i.w0("hogp already connected");
            return l(bluetoothDeviceH.getAddress());
        }
        try {
            method = bluetoothDeviceH.getClass().getMethod("removeBond", null);
        } catch (Exception e2) {
            StringBuilder sbF = g.a.a.a.a.F("An exception occurred while removing bond information: ");
            sbF.append(e2.getMessage());
            i.W3(sbF.toString());
        }
        if (method != null) {
            method.setAccessible(true);
            zBooleanValue = ((Boolean) method.invoke(bluetoothDeviceH, null)).booleanValue();
        } else {
            zBooleanValue = false;
        }
        if (zBooleanValue) {
            i.w0("remove bond first");
            e(533);
            return false;
        }
        i.w0("remove bond failed");
        e(529);
        return g.i.a.b.e.b.a.b(bluetoothDeviceH);
    }

    public void o() {
        boolean z2 = g.i.a.c.b.a;
        this.f5458f = z2;
        this.f5459m = z2;
        this.f5447w = BluetoothAdapter.getDefaultAdapter();
        q().x(2);
        g.i.a.b.e.b bVar = g.i.a.b.e.b.a;
        this.f5448x = bVar;
        if (bVar == null) {
            g.i.a.b.e.b.g(this.f5460n);
            this.f5448x = g.i.a.b.e.b.a;
        }
        g.i.a.b.e.b bVar2 = this.f5448x;
        if (bVar2 != null) {
            bVar2.a(null);
        } else {
            i.V3(this.f5458f, "BluetoothProfileManager not initialized");
        }
        g.i.a.b.e.d dVar = g.i.a.b.e.d.a;
        this.f5449y = dVar;
        if (dVar == null) {
            g.i.a.b.e.d.a(this.f5460n);
            this.f5449y = g.i.a.b.e.d.a;
        }
        g.i.a.b.e.d dVar2 = this.f5449y;
        if (dVar2 != null) {
            g.i.a.b.e.e eVar = this.C;
            if (dVar2.f5349d == null) {
                dVar2.f5349d = new CopyOnWriteArrayList();
            }
            if (!dVar2.f5349d.contains(eVar)) {
                dVar2.f5349d.add(eVar);
            }
        } else {
            i.W3("BluetoothProfileManager not initialized");
        }
        g.i.a.b.e.c cVar = g.i.a.b.e.c.f5337b;
        this.F = cVar;
        if (cVar == null) {
            g.i.a.b.e.c.h(this.f5460n);
            this.F = g.i.a.b.e.c.f5337b;
        }
    }

    public g.i.a.c.q.i q() {
        g.i.a.c.p.a aVar = this.K;
        return aVar != null ? aVar.i() : new g.i.a.c.q.i(0, 2);
    }

    public boolean r(String str) {
        BluetoothDevice bluetoothDeviceH = h(str);
        return bluetoothDeviceH != null && g.i.a.b.e.b.a.e(4, bluetoothDeviceH) == 2;
    }
}
