package g.i.a.b.c;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.HandlerThread;
import g.h.a.i;
import g.i.a.b.e.h.e;
import java.util.Calendar;
import java.util.Locale;
import java.util.Objects;

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

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public g.i.a.b.e.h.d f5309d;

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public BluetoothAdapter f5311f;
    public boolean a = false;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public int f5312g = 0;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public boolean f5313h = false;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public long f5314i = 0;

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public Runnable f5316k = new b();

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public Runnable f5317l = new c();

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public boolean f5318m = false;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public Runnable f5319n = new d();

    /* JADX INFO: renamed from: g.i.a.b.c.a$a, reason: collision with other inner class name */
    public class C0071a extends BroadcastReceiver {

        /* JADX INFO: renamed from: g.i.a.b.c.a$a$a, reason: collision with other inner class name */
        public class RunnableC0072a implements Runnable {
            public RunnableC0072a() {
            }

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

        public C0071a() {
        }

        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("android.bluetooth.adapter.action.STATE_CHANGED".equals(action)) {
                int intExtra = intent.getIntExtra("android.bluetooth.adapter.extra.STATE", -1);
                i.U3(String.format(Locale.US, "[%s] %d -> %d", action, Integer.valueOf(intent.getIntExtra("android.bluetooth.adapter.extra.PREVIOUS_STATE", -1)), Integer.valueOf(intExtra)));
                if (intExtra == 10) {
                    int i2 = a.this.f5312g;
                    if (i2 == 2 || i2 == 1) {
                        new Thread(new RunnableC0072a()).start();
                    }
                }
            }
        }
    }

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

        @Override // java.lang.Runnable
        public void run() {
            String str;
            long timeInMillis = Calendar.getInstance().getTimeInMillis();
            a aVar = a.this;
            if (timeInMillis < aVar.f5314i) {
                aVar.f5314i = 0L;
            }
            long j2 = timeInMillis - aVar.f5314i;
            int i2 = aVar.f5312g;
            if (i2 == 1) {
                if (j2 <= 30000) {
                    return;
                } else {
                    str = String.format(Locale.US, "no scan response received after start scan for %d ms", 30000L);
                }
            } else {
                if (i2 != 2) {
                    StringBuilder sbF = g.a.a.a.a.F("ignore state:");
                    sbF.append(a.this.f5312g);
                    i.V3(false, sbF.toString());
                    a.b(a.this);
                    return;
                }
                if (j2 <= 30000) {
                    a.b(aVar);
                    return;
                }
                str = String.format(Locale.US, "exceed %d ms , no scan response received since last time", 30000L);
            }
            i.w0(str);
            a.this.i();
        }
    }

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

        @Override // java.lang.Runnable
        public void run() {
            Objects.requireNonNull(a.this);
            i.V3(false, "scan delay time reached");
            a.this.i();
        }
    }

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

        @Override // java.lang.Runnable
        public void run() {
            if (a.this.f5309d == null) {
                i.V3(false, "no callback registed");
            }
            a.this.k();
        }
    }

    public static void b(a aVar) {
        Handler handler = aVar.f5310e;
        if (handler == null) {
            i.V3(false, "mHandler == null");
        } else {
            handler.removeCallbacksAndMessages(aVar.f5317l);
            aVar.f5310e.postDelayed(aVar.f5317l, aVar.f5308c.f5394s);
        }
    }

    public void a(int i2) {
        int i3 = this.f5312g;
        if (i3 != i2) {
            if (this.a) {
                i.w0(String.format(Locale.US, "ScanState 0x%02X >> 0x%02X", Integer.valueOf(i3), Integer.valueOf(i2)));
            }
            this.f5312g = i2;
            g.i.a.b.e.h.d dVar = this.f5309d;
            if (dVar != null) {
                dVar.b(i2);
            } else {
                i.V3(false, "no callback registed");
            }
        }
        int i4 = this.f5312g;
        if (i4 == 0 || i4 == 3) {
            Handler handler = this.f5310e;
            if (handler != null) {
                handler.removeCallbacks(this.f5317l);
                this.f5310e.removeCallbacks(this.f5316k);
                this.f5310e.removeCallbacks(this.f5319n);
            }
            if (!this.f5318m || this.f5310e == null) {
                return;
            }
            i.V3(this.a, "wait to start auto scan");
            this.f5310e.postDelayed(this.f5319n, this.f5308c.f5396u);
        }
    }

    public boolean c() {
        if (this.f5313h) {
            i.w0("please call onDestroy() method first");
            return false;
        }
        this.a = i.f5270b;
        BluetoothManager bluetoothManager = (BluetoothManager) this.f5307b.getSystemService("bluetooth");
        if (bluetoothManager != null) {
            this.f5311f = bluetoothManager.getAdapter();
        }
        if (this.f5308c == null) {
            i.V3(false, "create new ScannerParams");
            this.f5308c = new e(0);
        }
        if (this.f5310e == null) {
            HandlerThread handlerThread = new HandlerThread("ScannerPresenter");
            handlerThread.start();
            this.f5310e = new Handler(handlerThread.getLooper());
        }
        if (this.f5309d == null) {
            i.V3(false, "callback is null");
        }
        this.f5307b.registerReceiver(this.f5315j, new IntentFilter("android.bluetooth.adapter.action.STATE_CHANGED"));
        this.f5313h = true;
        i.U3("initialized");
        return true;
    }

    public abstract boolean d(BluetoothDevice bluetoothDevice);

    public boolean e(BluetoothDevice bluetoothDevice, int i2, byte[] bArr) {
        boolean zD;
        this.f5314i = Calendar.getInstance().getTimeInMillis();
        int i3 = this.f5312g;
        if (i3 == 1) {
            a(2);
        } else if (i3 != 2) {
            i.U3(String.format("stop to calibration state: 0x%04X", Integer.valueOf(i3)));
            i();
            return false;
        }
        if (bluetoothDevice == null) {
            i.w0("ignore, device is null");
            return false;
        }
        int i4 = this.f5308c.f5393r;
        if (i4 <= -1000 || i4 <= i2) {
            zD = d(bluetoothDevice);
        } else {
            i.W3("filter, low rssi:" + i2);
            zD = false;
        }
        if (!zD) {
            return false;
        }
        g.i.a.b.e.h.b bVar = new g.i.a.b.e.h.b(bluetoothDevice, bluetoothDevice.getName(), i2, bluetoothDevice.getBondState() == 12, false, bArr);
        g.i.a.b.e.h.d dVar = this.f5309d;
        if (dVar != null) {
            dVar.a(bVar);
        } else {
            i.V3(false, "no callback registed");
        }
        if (this.f5308c.f5388m == 1) {
            i.w0("SCAN_MECHANISM_FILTER_ONE > scanDevice(false)");
            i();
        }
        return true;
    }

    public boolean f() {
        if (this.f5310e == null) {
            i.V3(false, "mHandler == null");
            return false;
        }
        i.V3(false, String.format(Locale.US, "wait to check scan period(%d)", 30000L));
        this.f5310e.removeCallbacks(this.f5316k);
        return this.f5310e.postDelayed(this.f5316k, 30000L);
    }

    public boolean g() {
        int i2 = this.f5312g;
        if (i2 == 1 || i2 == 2) {
            long timeInMillis = Calendar.getInstance().getTimeInMillis();
            if (timeInMillis < this.f5314i) {
                this.f5314i = 0L;
            }
            if (timeInMillis - this.f5314i > 30000) {
                i.w0(String.format(Locale.US, "exceed %d ms , no scan response received since last time", 30000L));
                i();
            } else {
                f();
            }
            return false;
        }
        a(1);
        this.f5310e.removeCallbacks(this.f5317l);
        this.f5310e.removeCallbacks(this.f5316k);
        this.f5310e.removeCallbacks(this.f5319n);
        this.f5314i = 0L;
        e eVar = this.f5308c;
        if (eVar != null) {
            this.f5318m = eVar.f5395t;
        } else {
            this.f5318m = false;
        }
        return true;
    }

    public boolean h() {
        Handler handler = this.f5310e;
        if (handler == null) {
            return true;
        }
        handler.removeCallbacks(this.f5317l);
        this.f5310e.removeCallbacks(this.f5316k);
        this.f5310e.removeCallbacks(this.f5319n);
        return true;
    }

    public abstract boolean i();

    public void j() {
        Context context = this.f5307b;
        if (context != null) {
            try {
                context.unregisterReceiver(this.f5315j);
            } catch (Exception e2) {
                i.E0(false, e2.toString());
            }
        }
        this.f5309d = null;
        Handler handler = this.f5310e;
        if (handler != null) {
            handler.removeCallbacks(this.f5317l);
            this.f5310e.removeCallbacks(this.f5319n);
            this.f5310e.removeCallbacks(this.f5316k);
            this.f5310e = null;
        }
        l();
        this.f5313h = false;
    }

    public abstract boolean k();

    public abstract boolean l();
}
