package a.b.a.f;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import androidx.collection.ArrayMap;
import java.util.List;

public class g {
    private static final String g = "CompatLeScanner";

    /* renamed from: a  reason: collision with root package name */
    private BluetoothLeScanner f16a;
    private c b;
    private ArrayMap<BluetoothDevice, ScanResult> c = new ArrayMap<>();
    private boolean d = false;
    private ScanCallback e = new a();
    private ScanCallback f = new b();

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

        public void onScanFailed(int i) {
            a.a.a.b.b(g.g, "MatchCallback: onScanFailed with reason: " + i);
            g.this.a((g) i);
        }

        public void onScanResult(int i, ScanResult scanResult) {
            g.this.b(scanResult);
        }
    }

    public class b extends ScanCallback {
        public b() {
        }

        public void onScanFailed(int i) {
            a.a.a.b.b(g.g, "LostCallback: onScanFailed with reason: " + i);
            g.this.a((g) i);
        }

        public void onScanResult(int i, ScanResult scanResult) {
            BluetoothDevice device = scanResult.getDevice();
            if (i == 4 && g.this.c.containsKey(device)) {
                g.this.a((g) scanResult);
            }
        }
    }

    public interface c {

        /* renamed from: a  reason: collision with root package name */
        public static final int f19a = 1;
        public static final int b = 0;

        void a(int i);

        void a(int i, ScanResult scanResult);
    }

    public static g a(BluetoothLeScanner bluetoothLeScanner) {
        if (bluetoothLeScanner == null) {
            return null;
        }
        g gVar = new g();
        gVar.f16a = bluetoothLeScanner;
        return gVar;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private synchronized void a(int i) {
        if (!this.d) {
            c cVar = this.b;
            if (cVar != null) {
                cVar.a(i);
            }
            this.d = true;
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private synchronized void a(ScanResult scanResult) {
        ScanResult remove = this.c.remove(scanResult.getDevice());
        if (remove == null) {
            a.a.a.b.a(g, "CompatLeScanner doLost can not find Map key, result is null");
            return;
        }
        c cVar = this.b;
        if (cVar != null) {
            cVar.a(0, remove);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private synchronized void b(ScanResult scanResult) {
        this.c.put(scanResult.getDevice(), scanResult);
        c cVar = this.b;
        if (cVar != null) {
            cVar.a(1, scanResult);
        }
    }

    public void a() {
        a.a.a.b.a(g, "stopScan...");
        try {
            this.f16a.stopScan(this.e);
            this.f16a.stopScan(this.f);
        } catch (IllegalStateException e2) {
            a.a.a.b.e(g, "Scan stop failed because of illegal state, not a problem.", e2);
        }
        synchronized (this) {
            this.b = null;
        }
        this.c.clear();
    }

    public void a(List<ScanFilter> list, ScanSettings scanSettings, c cVar) {
        a.a.a.b.a(g, "startScan...");
        synchronized (this) {
            this.b = cVar;
            this.d = false;
        }
        ScanSettings build = new ScanSettings.Builder().setScanMode(scanSettings.getScanMode()).setCallbackType(1).build();
        ScanSettings build2 = new ScanSettings.Builder().setScanMode(scanSettings.getScanMode()).setCallbackType(4).build();
        try {
            a.a.a.b.a(g, "Start match scanner...");
            this.f16a.startScan(list, build, this.e);
            a.a.a.b.a(g, "Start lost scanner...");
            this.f16a.startScan(list, build2, this.f);
        } catch (IllegalStateException e2) {
            a.a.a.b.e(g, "Scan start failed because of illegal state.", e2);
        }
    }
}
