package g.i.a.c.p;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import g.i.a.c.a0.e;
import g.i.a.c.p.f;
import g.i.a.c.q.i;
import g.i.a.c.q.j;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.UUID;

/* JADX INFO: loaded from: classes.dex */
public abstract class a implements f {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public int f5611f;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public BluetoothGatt f5612m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public BluetoothGattService f5613n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public BluetoothGattService f5614o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public BluetoothGattCharacteristic f5615p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public BluetoothGattCharacteristic f5616q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public List<BluetoothGattCharacteristic> f5617r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public i f5618s;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public String f5620u;

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

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public g.i.a.c.a0.b f5622w;

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

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public List<j> f5619t = new ArrayList();

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public int f5623x = 1;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public final BluetoothGattCallback f5625z = new C0084a();
    public Object A = new Object();
    public boolean B = true;
    public int C = 0;
    public Object D = new Object();

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

        @Override // android.bluetooth.BluetoothGattCallback
        public void onCharacteristicChanged(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
            a.this.b(bluetoothGatt, bluetoothGattCharacteristic);
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onCharacteristicRead(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i2) {
            a.this.c(bluetoothGatt, bluetoothGattCharacteristic, i2);
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onConnectionStateChange(BluetoothGatt bluetoothGatt, int i2, int i3) {
            if (i2 == 0 && i3 == 0) {
                a aVar = a.this;
                if ((aVar.C & 256) == 256) {
                    aVar.j(2);
                    a.this.n();
                }
            }
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onDescriptorWrite(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, int i2) {
            super.onDescriptorWrite(bluetoothGatt, bluetoothGattDescriptor, i2);
            try {
                g.h.a.i.U3("onDescriptorWrite: " + i2);
                synchronized (a.this.A) {
                    a aVar = a.this;
                    aVar.B = true;
                    aVar.A.notifyAll();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
                g.h.a.i.D0(e2.toString());
            }
        }
    }

    public class b extends Thread {
        public b(C0084a c0084a) {
        }

        @Override // java.lang.Thread, java.lang.Runnable
        public void run() {
            super.run();
            setName("AdapterXGBase-DeviceInfoThread");
            a.this.m();
        }
    }

    public interface c {
    }

    public void a() {
        b bVar = this.f5624y;
        if (bVar != null) {
            bVar.interrupt();
            this.f5624y = null;
        }
        this.C = 0;
        g.i.a.b.e.c.f5337b.k(this.f5620u, this.f5625z);
    }

    public void b(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
    }

    public abstract void c(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i2);

    public void d(g.i.a.c.a0.b bVar, String str, BluetoothGatt bluetoothGatt, BluetoothGattService bluetoothGattService, BluetoothGattService bluetoothGattService2, c cVar) {
        this.f5622w = bVar;
        this.f5620u = str;
        this.f5612m = g.i.a.b.e.c.f5337b.f5342g.get(str);
        this.f5613n = bluetoothGattService;
        this.f5614o = bluetoothGattService2;
        this.f5621v = cVar;
        this.f5619t = new ArrayList();
        this.f5617r = new ArrayList();
        g.i.a.c.a0.b bVar2 = this.f5622w;
        this.f5623x = bVar2 != null ? bVar2.f5457h : 1;
        k();
        l();
        g.i.a.b.e.c.f5337b.j(this.f5620u, this.f5625z);
    }

    public void e(String str, BluetoothGatt bluetoothGatt, BluetoothGattService bluetoothGattService, BluetoothGattService bluetoothGattService2) {
        g.h.a.i.U3("sync data ...");
        this.f5620u = str;
        this.f5612m = bluetoothGatt;
        this.f5613n = bluetoothGattService;
        this.f5614o = bluetoothGattService2;
        k();
        l();
        b bVar = new b(null);
        this.f5624y = bVar;
        bVar.start();
    }

    /* JADX WARN: Removed duplicated region for block: B:13:0x0024  */
    /* JADX WARN: Removed duplicated region for block: B:6:0x0012  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void f(byte[] r6) {
        /*
            r5 = this;
            java.nio.ByteBuffer r0 = java.nio.ByteBuffer.wrap(r6)
            java.nio.ByteOrder r1 = java.nio.ByteOrder.LITTLE_ENDIAN
            r0.order(r1)
            int r1 = r5.f5623x
            r2 = 0
            r3 = 1
            if (r1 != r3) goto L17
            int r6 = r6.length
            if (r6 <= 0) goto L24
        L12:
            byte r6 = r0.get()
            goto L25
        L17:
            int r1 = r6.length
            r4 = 2
            if (r1 < r4) goto L20
            short r6 = r0.getShort()
            goto L25
        L20:
            int r6 = r6.length
            if (r6 <= 0) goto L24
            goto L12
        L24:
            r6 = r2
        L25:
            java.lang.StringBuilder r0 = new java.lang.StringBuilder
            r0.<init>()
            java.lang.String r1 = "current battery: "
            r0.append(r1)
            r0.append(r6)
            java.lang.String r0 = r0.toString()
            g.h.a.i.V3(r3, r0)
            g.i.a.c.q.i r0 = r5.i()
            r0.f5671g = r3
            r0.f5672h = r6
            r0.f5673i = r2
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i.a.c.p.a.f(byte[]):void");
    }

    public boolean g(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, boolean z2) {
        int properties = bluetoothGattCharacteristic.getProperties();
        if ((properties & 16) == 0) {
            g.h.a.i.W3("check properties failed: " + properties);
            this.B = false;
            return false;
        }
        StringBuilder sbF = g.a.a.a.a.F("setCharacteristicNotification() - uuid: ");
        sbF.append(bluetoothGattCharacteristic.getUuid());
        sbF.append(" enabled: ");
        sbF.append(z2);
        g.h.a.i.U3(sbF.toString());
        bluetoothGatt.setCharacteristicNotification(bluetoothGattCharacteristic, z2);
        BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(f.f5633b);
        if (descriptor != null) {
            boolean z3 = descriptor.getValue() != null && descriptor.getValue().length == 2 && descriptor.getValue()[0] > 0 && descriptor.getValue()[1] == 0;
            g.a.a.a.a.l0("current cccd state: ", z3, true);
            if (z2 && z3) {
                this.B = true;
                g.h.a.i.W3("cccd already enabled");
                return true;
            }
            if (!z2 && !z3) {
                g.h.a.i.W3("cccd already disable");
                this.B = true;
                return true;
            }
            descriptor.setValue(z2 ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
            if (bluetoothGatt.writeDescriptor(descriptor)) {
                synchronized (this.A) {
                    g.h.a.i.w0("wait write Characteristic Notification 15000ms");
                    try {
                        this.B = false;
                        this.A.wait(30000L);
                    } catch (InterruptedException e2) {
                        g.h.a.i.D0("wait writeDescriptor interrupted: " + e2.toString());
                    }
                }
                return this.B;
            }
        }
        return false;
    }

    public boolean h(BluetoothGattCharacteristic bluetoothGattCharacteristic) {
        String str;
        if (this.f5612m == null) {
            str = "mBluetoothGatt is null maybe disconnected just now";
        } else if (bluetoothGattCharacteristic == null) {
            str = "characteristic can not be null";
        } else {
            g.h.a.i.U3(String.format(Locale.US, "readCharacteristic:(%d) %s", Integer.valueOf(bluetoothGattCharacteristic.getInstanceId()), bluetoothGattCharacteristic.getUuid().toString()));
            if (this.f5612m.readCharacteristic(bluetoothGattCharacteristic)) {
                o();
                return this.C != 2;
            }
            str = "readCharacteristic failed";
        }
        g.h.a.i.W3(str);
        return false;
    }

    public i i() {
        if (this.f5618s == null) {
            this.f5618s = new i(this.f5611f, 2);
        }
        return this.f5618s;
    }

    public void j(int i2) {
        g.h.a.i.w0(String.format("syndata: 0x%04X >> 0x%04X", Integer.valueOf(this.C), Integer.valueOf(i2)));
        this.C = i2;
        c cVar = this.f5621v;
        if (cVar == null) {
            g.h.a.i.V3(false, "no callback registered");
            return;
        }
        e.a aVar = (e.a) cVar;
        if (!g.i.a.c.a0.e.this.b()) {
            g.i.a.c.a0.e eVar = g.i.a.c.a0.e.this;
            Objects.requireNonNull(eVar);
            g.h.a.i.V3(false, String.format("ignore, is not in preparing state: 0x%04X", Integer.valueOf(eVar.f5466t)));
        } else if (i2 == 1) {
            g.i.a.c.a0.e eVar2 = g.i.a.c.a0.e.this;
            eVar2.e((eVar2.f5466t & 2048) == 2048 ? 2074 : 527);
        } else if (i2 == 2) {
            g.i.a.c.a0.e.this.f(new g.i.a.c.g.a(5));
        }
    }

    public final void k() {
        BluetoothGattService service = this.f5612m.getService(f.a.a);
        if (service == null) {
            g.h.a.i.V3(true, "BATTERY_SERVICE not found");
            return;
        }
        UUID uuid = f.a.f5639b;
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(uuid);
        this.f5615p = characteristic;
        if (characteristic == null) {
            g.h.a.i.V3(true, "BAS_READ_CHARACTERITIC not found");
            return;
        }
        StringBuilder sbF = g.a.a.a.a.F("find BAS_READ_CHARACTERITIC: ");
        sbF.append(uuid.toString());
        g.h.a.i.U3(sbF.toString());
    }

    public final void l() {
        String string;
        BluetoothGatt bluetoothGatt = this.f5612m;
        UUID uuid = f.b.a;
        BluetoothGattService service = bluetoothGatt.getService(uuid);
        if (service == null) {
            g.h.a.i.U3("DEVICE_INFORMATION_SERVICE not found");
            return;
        }
        g.a.a.a.a.t0(uuid, g.a.a.a.a.F("find DEVICE_INFORMATION_SERVICE: "));
        UUID uuid2 = f.b.f5640b;
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(uuid2);
        this.f5616q = characteristic;
        if (characteristic == null) {
            string = "DIS_PNP_ID_CHARACTERISTIC not found";
        } else {
            StringBuilder sbF = g.a.a.a.a.F("find DIS_PNP_ID_CHARACTERISTIC: ");
            sbF.append(uuid2.toString());
            string = sbF.toString();
        }
        g.h.a.i.w0(string);
    }

    public abstract void m();

    public void n() {
        synchronized (this.D) {
            this.D.notifyAll();
        }
    }

    public void o() {
        synchronized (this.D) {
            try {
                this.D.wait(6000L);
            } catch (InterruptedException e2) {
                g.h.a.i.W3("wait sync data interrupted: " + e2.toString());
            }
        }
    }
}
