package com.ifpdos.factorytest;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.Log;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import kotlin.jvm.internal.ShortCompanionObject;

/* JADX INFO: loaded from: classes.dex */
public class BluetoothAutoTest extends TestItem {
    private static final String BLE_TEST_NAME = "CVTE_BLE_TEST";
    private static final String TAG = "BluetoothAutoTest";
    private static final int TIME_OUT_SECONDS = 25;
    private final BluetoothAdapter bluetoothAdapter;
    private final Handler mHandler;
    private final IntentFilter mIntentFilter;
    private LocalBluetoothAdapter mLocalManager;
    private final BroadcastReceiver mReceiver;
    private final ScheduledExecutorService mTaskExecutor;
    private final boolean orgBlueToothStatus;
    private ScheduledFuture<?> scheduledFuture;

    public BluetoothAutoTest(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.mHandler = new Handler(Looper.getMainLooper());
        this.mTaskExecutor = Executors.newSingleThreadScheduledExecutor();
        IntentFilter intentFilter = new IntentFilter();
        this.mIntentFilter = intentFilter;
        intentFilter.addAction("android.bluetooth.adapter.action.STATE_CHANGED");
        intentFilter.addAction("android.bluetooth.device.action.FOUND");
        intentFilter.addAction("android.bluetooth.adapter.action.DISCOVERY_FINISHED");
        BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
        this.bluetoothAdapter = defaultAdapter;
        this.mLocalManager = LocalBluetoothAdapter.getInstance();
        this.orgBlueToothStatus = defaultAdapter.isEnabled();
        this.mReceiver = new BroadcastReceiver() { // from class: com.ifpdos.factorytest.BluetoothAutoTest.1
            @Override // android.content.BroadcastReceiver
            public void onReceive(Context context2, Intent intent) {
                String action = intent.getAction();
                if (action != null && action.equals("android.bluetooth.adapter.action.STATE_CHANGED")) {
                    Log.d(BluetoothAutoTest.TAG, "onReceive:ACTION_STATE_CHANGED state = " + intent.getIntExtra("android.bluetooth.adapter.extra.STATE", Integer.MIN_VALUE));
                }
                if ("android.bluetooth.device.action.FOUND".equals(action)) {
                    BluetoothDevice bluetoothDevice = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
                    BluetoothAutoTest.this.handleScannedDevice(bluetoothDevice.getName(), bluetoothDevice.getAddress(), Short.valueOf(intent.getShortExtra("android.bluetooth.device.extra.RSSI", ShortCompanionObject.MIN_VALUE)));
                }
                if ("android.bluetooth.adapter.action.DISCOVERY_FINISHED".equals(action)) {
                    Log.d(BluetoothAutoTest.TAG, "onReceive:ACTION_DISCOVERY_FINISHED");
                    BluetoothAutoTest.this.bluetoothAdapter.startDiscovery();
                }
            }
        };
    }

    public void bluetoothTestDone(final boolean z, final String str) {
        this.mHandler.post(new Runnable() { // from class: com.ifpdos.factorytest.BluetoothAutoTest.2
            @Override // java.lang.Runnable
            public void run() {
                try {
                    if (BluetoothAutoTest.this.mContext != null) {
                        BluetoothAutoTest.this.mContext.unregisterReceiver(BluetoothAutoTest.this.mReceiver);
                    }
                    BluetoothAutoTest.this.testDone(z, str);
                    BluetoothAutoTest.this.stopBluetoothScan();
                    BluetoothAutoTest.this.restoreBluetooth();
                } catch (Exception e) {
                    Log.e(BluetoothAutoTest.TAG, "bluetoothTestDone: error", e);
                }
            }
        });
    }

    @Override // com.ifpdos.factorytest.TestItem
    boolean doTest() {
        startTestingUi();
        this.mTaskExecutor.execute(new Runnable() { // from class: com.ifpdos.factorytest.BluetoothAutoTest$$ExternalSyntheticLambda1
            @Override // java.lang.Runnable
            public final void run() {
                this.f$0.startBluetoothTest();
            }
        });
        return true;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void startBluetoothTest() {
        if (this.mContext != null) {
            this.mContext.registerReceiver(this.mReceiver, this.mIntentFilter);
        }
        initBluetooth();
        startBluetoothTimer();
        startBluetoothScan();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$startBluetoothTimer$0() {
        bluetoothTestDone(false, this.mContext.getString(R.string.bluetooth_test_fail));
    }

    private void startBluetoothTimer() {
        this.scheduledFuture = this.mTaskExecutor.schedule(new Runnable() { // from class: com.ifpdos.factorytest.BluetoothAutoTest$$ExternalSyntheticLambda0
            @Override // java.lang.Runnable
            public final void run() {
                this.f$0.lambda$startBluetoothTimer$0();
            }
        }, 25L, TimeUnit.SECONDS);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void handleScannedDevice(String str, String str2, Short sh) {
        Log.d(TAG, "Device Name: " + str + ", Device Address: " + str2 + getResources().getString(R.string.wifi_test_strength) + sh + " isDiscovering: " + this.bluetoothAdapter.isDiscovering());
        if (!BLE_TEST_NAME.equals(str) || sh.shortValue() <= -60) {
            return;
        }
        this.scheduledFuture.cancel(true);
        bluetoothTestDone(true, this.mContext.getString(R.string.bluetooth_test_detected) + str);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void stopBluetoothScan() {
        try {
            Log.d(TAG, "stopBluetoothScan: blueadapter.isDiscovering:" + this.bluetoothAdapter.isDiscovering());
            if (this.bluetoothAdapter.isDiscovering()) {
                Log.d(TAG, "stopBluetoothScan");
                this.bluetoothAdapter.cancelDiscovery();
            }
        } catch (Exception e) {
            bluetoothTestDone(false, this.mContext.getString(R.string.open_bluetooth_error));
            this.scheduledFuture.cancel(true);
            Log.e(TAG, "stopBluetoothScan err", e);
        }
    }

    private void startBluetoothScan() {
        int i = 10;
        while (i >= 0) {
            i--;
            try {
                if (this.bluetoothAdapter.isEnabled()) {
                    this.bluetoothAdapter.startDiscovery();
                    Log.d(TAG, "startBluetoothScan");
                    return;
                }
                Thread.sleep(1000L);
            } catch (Exception e) {
                bluetoothTestDone(false, this.mContext.getString(R.string.open_bluetooth_error));
                this.scheduledFuture.cancel(true);
                Log.e(TAG, "startBluetoothScan err", e);
                return;
            }
        }
    }

    @Override // com.ifpdos.factorytest.TestItem
    void onStop() {
        super.onStop();
    }

    @Override // com.ifpdos.factorytest.TestItem
    void onDestroy() {
        super.onDestroy();
        this.mLocalManager.setBluetoothEnabled(this.orgBlueToothStatus);
        this.mHandler.removeCallbacksAndMessages(null);
    }

    private void initBluetooth() {
        try {
            Log.d(TAG, "BluetoothTest: " + this.bluetoothAdapter.isEnabled() + " Status = " + this.mLocalManager.getBluetoothState());
            if (this.mLocalManager.getBluetoothState() != 12) {
                this.mLocalManager.setBluetoothEnabled(true);
            }
        } catch (Exception e) {
            Log.e(TAG, "initBluetooth:LocalBluetoothAdapter.getInstance error ", e);
            this.mLocalManager = null;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void restoreBluetooth() {
        try {
            if (this.orgBlueToothStatus != this.bluetoothAdapter.isEnabled()) {
                this.mLocalManager.setBluetoothEnabled(this.orgBlueToothStatus);
                Log.d(TAG, "setBluetoothEnabled: " + this.orgBlueToothStatus);
            }
        } catch (Exception e) {
            Log.e(TAG, "setBluetoothEnabled error ", e);
        }
    }
}
