package com.ifpdos.factorytest;

import android.app.Activity;
import android.app.ActivityManager;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.core.internal.view.SupportMenu;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import kotlin.jvm.internal.ShortCompanionObject;

/* JADX INFO: loaded from: classes.dex */
public class BluetoothTestActivity extends Activity {
    private static final String TAG = "BluetoothTestActivity";
    private boolean mFinish;
    private List<ScanResult> mResults;
    private LinearLayout mRootView;
    private LocalBluetoothAdapter mLocalManager = null;
    private IntentFilter mIntentFilter = null;
    private BroadcastReceiver mReceiver = null;
    private int mWidth = 1024;
    private boolean deviceFound = false;

    @Override // android.app.Activity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.mResults = new ArrayList();
        LinearLayout linearLayout = new LinearLayout(this) { // from class: com.ifpdos.factorytest.BluetoothTestActivity.1
            @Override // android.view.View
            public void onSizeChanged(int i, int i2, int i3, int i4) {
                Log.d(BluetoothTestActivity.TAG, "w=" + i + ", h=" + i2);
                BluetoothTestActivity.this.mWidth = i;
            }
        };
        this.mRootView = linearLayout;
        linearLayout.setOrientation(1);
        this.mRootView.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
        this.mReceiver = new BroadcastReceiver() { // from class: com.ifpdos.factorytest.BluetoothTestActivity.2
            @Override // android.content.BroadcastReceiver
            public void onReceive(Context context, Intent intent) {
                if (BluetoothTestActivity.this.mFinish) {
                    return;
                }
                String action = intent.getAction();
                if (action.equals("android.bluetooth.device.action.FOUND")) {
                    short shortExtra = intent.getShortExtra("android.bluetooth.device.extra.RSSI", ShortCompanionObject.MIN_VALUE);
                    BluetoothTestActivity.this.addBtDevice((BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE"), shortExtra);
                } else {
                    if (action.equals("Cvte.Bluetooth.StartScan")) {
                        BluetoothTestActivity.this.mResults.removeAll(BluetoothTestActivity.this.mResults);
                        return;
                    }
                    if (action.equals("android.bluetooth.adapter.action.STATE_CHANGED") && BluetoothTestActivity.this.mLocalManager != null && BluetoothTestActivity.this.mLocalManager.getBluetoothState() == 12 && BluetoothTestActivity.this.mLocalManager.startScanning()) {
                        BluetoothTestActivity.this.mResults.removeAll(BluetoothTestActivity.this.mResults);
                        BluetoothTestActivity.this.showMac();
                    }
                }
            }
        };
        this.mIntentFilter = new IntentFilter();
        LocalBluetoothAdapter localBluetoothAdapter = LocalBluetoothAdapter.getInstance();
        this.mLocalManager = localBluetoothAdapter;
        if (localBluetoothAdapter != null) {
            this.mIntentFilter.addAction("android.bluetooth.adapter.action.STATE_CHANGED");
            this.mIntentFilter.addAction("android.bluetooth.device.action.FOUND");
        }
        LocalBluetoothAdapter localBluetoothAdapter2 = this.mLocalManager;
        if (localBluetoothAdapter2 != null) {
            if (localBluetoothAdapter2.getBluetoothState() == 12) {
                this.mLocalManager.startScanning();
            } else {
                this.mLocalManager.setBluetoothEnabled(true);
            }
        }
        setTitle(getResources().getString(R.string.test_bt));
        setContentView(this.mRootView);
        showMac();
    }

    public class ScanResult {
        String mAddr;
        String mName;
        short mRssi;

        public ScanResult(String str, String str2, short s) {
            this.mAddr = str;
            this.mName = str2;
            this.mRssi = s;
        }

        public String toString() {
            return "Name=" + this.mName + ", Address=" + this.mAddr + ", RSSI=" + ((int) this.mRssi);
        }
    }

    public class BtDeviItem extends LinearLayout {
        private Context mContext;
        private TextView mInfo;
        private TextView mLevel;

        public BtDeviItem(Context context) {
            super(context);
            this.mContext = context;
            setOrientation(0);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-1, -2);
            layoutParams.setMargins(0, BluetoothTestActivity.this.dip(10), 0, 0);
            setLayoutParams(layoutParams);
            this.mInfo = new TextView(this.mContext);
            this.mLevel = new TextView(this.mContext);
            addView(this.mInfo);
            addView(this.mLevel);
        }

        void setInfo(String str) {
            this.mInfo.setText(str);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(BluetoothTestActivity.this.mWidth / 2, -2);
            layoutParams.setMargins(BluetoothTestActivity.this.dip(20), 0, 0, 0);
            this.mInfo.setLayoutParams(layoutParams);
        }

        /* JADX WARN: Removed duplicated region for block: B:4:0x0004 A[PHI: r0
          0x0004: PHI (r0v3 int) = (r0v0 int), (r0v1 int) binds: [B:3:0x0002, B:6:0x0008] A[DONT_GENERATE, DONT_INLINE]] */
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct add '--show-bad-code' argument
        */
        void setLevel(int r5) {
            /*
                r4 = this;
                r0 = -100
                if (r5 >= r0) goto L6
            L4:
                r5 = r0
                goto Lb
            L6:
                r0 = -50
                if (r5 <= r0) goto Lb
                goto L4
            Lb:
                int r5 = r5 + 100
                android.widget.LinearLayout$LayoutParams r0 = new android.widget.LinearLayout$LayoutParams
                com.ifpdos.factorytest.BluetoothTestActivity r1 = com.ifpdos.factorytest.BluetoothTestActivity.this
                int r1 = com.ifpdos.factorytest.BluetoothTestActivity.m75$$Nest$fgetmWidth(r1)
                int r1 = r1 / 2
                int r1 = r1 * r5
                int r1 = r1 / 50
                r2 = -2
                r0.<init>(r1, r2)
                android.widget.TextView r1 = r4.mLevel
                int r5 = r5 * 255
                int r5 = r5 / 50
                int r2 = 255 - r5
                int r2 = r2 << 16
                r3 = -16777216(0xffffffffff000000, float:-1.7014118E38)
                r2 = r2 | r3
                int r5 = r5 << 8
                r5 = r5 | r2
                r1.setBackgroundColor(r5)
                android.widget.TextView r4 = r4.mLevel
                r4.setLayoutParams(r0)
                return
            */
            throw new UnsupportedOperationException("Method not decompiled: com.ifpdos.factorytest.BluetoothTestActivity.BtDeviItem.setLevel(int):void");
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void showMac() {
        TextView textView = new TextView(this);
        textView.setTextSize(40.0f);
        String macAddr = BluetoothTest.getMacAddr();
        textView.setText(" MAC=" + macAddr);
        if (macAddr.equals("unknow") || BluetoothTest.macIsNotMatch(macAddr)) {
            textView.setTextColor(SupportMenu.CATEGORY_MASK);
        } else {
            textView.setTextColor(-16711936);
        }
        this.mRootView.addView(textView);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void addBtDevice(BluetoothDevice bluetoothDevice, short s) {
        if (isRunBackground(getApplicationContext())) {
            stopScan();
            finish();
            return;
        }
        this.mRootView.removeAllViews();
        int i = 0;
        boolean z = false;
        for (ScanResult scanResult : this.mResults) {
            if (scanResult.mAddr.equals(bluetoothDevice.getAddress())) {
                scanResult.mName = bluetoothDevice.getName();
                scanResult.mRssi = s;
                z = true;
            }
        }
        if (!z) {
            this.mResults.add(new ScanResult(bluetoothDevice.getAddress(), bluetoothDevice.getName(), s));
        }
        showMac();
        List<ScanResult> list = this.mResults;
        if (list != null) {
            for (ScanResult scanResult2 : list) {
                Log.d(TAG, "Scan results=" + scanResult2.toString());
                i++;
                BtDeviItem btDeviItem = new BtDeviItem(this);
                btDeviItem.setInfo((scanResult2.mName + "(" + scanResult2.mAddr + "):") + ((int) scanResult2.mRssi) + "dBm, ");
                btDeviItem.setLevel(scanResult2.mRssi);
                this.mRootView.addView(btDeviItem);
            }
            Log.d(TAG, "count=" + i);
            if (i > 0) {
                this.deviceFound = true;
            }
        }
    }

    @Override // android.app.Activity, android.view.KeyEvent.Callback
    public boolean onKeyDown(int i, KeyEvent keyEvent) {
        if (i != 4) {
            return false;
        }
        stopScan();
        handlerBack();
        return false;
    }

    private void handlerBack() {
        if (this.deviceFound) {
            new AskDialog(this, getResources().getString(R.string.test_bt), getResources().getString(R.string.test_yes_or_no_pass), new OnAskDialogResultListener() { // from class: com.ifpdos.factorytest.BluetoothTestActivity.3
                @Override // com.ifpdos.factorytest.OnAskDialogResultListener
                public void onAskDialogResult(Context context, boolean z) {
                    Intent intent = new Intent();
                    String string = BluetoothTestActivity.this.getResources().getString(R.string.test_done);
                    if (string == null) {
                        string = "";
                    }
                    intent.putExtra("com.ifpdos.factorytest.info", string);
                    if (z) {
                        BluetoothTestActivity.this.setResult(1, intent);
                    } else {
                        BluetoothTestActivity.this.setResult(2, intent);
                    }
                    BluetoothTestActivity.this.finish();
                }
            });
            return;
        }
        Intent intent = new Intent();
        String string = getResources().getString(R.string.test_done);
        if (string == null) {
            string = "";
        }
        intent.putExtra("com.ifpdos.factorytest.info", string);
        setResult(2, intent);
        finish();
    }

    @Override // android.app.Activity
    protected void onResume() {
        super.onResume();
        this.mFinish = false;
        registerReceiver(this.mReceiver, this.mIntentFilter);
    }

    @Override // android.app.Activity
    protected void onPause() {
        super.onPause();
    }

    @Override // android.app.Activity
    protected void onDestroy() {
        super.onDestroy();
    }

    private void stopScan() {
        this.mFinish = true;
        BroadcastReceiver broadcastReceiver = this.mReceiver;
        if (broadcastReceiver != null) {
            unregisterReceiver(broadcastReceiver);
            this.mReceiver = null;
            this.mLocalManager.cancelDiscovery();
            this.mLocalManager.disable();
        }
    }

    int dip(int i) {
        return MainTestActivity.dip(i);
    }

    public boolean isRunBackground(Context context) {
        Iterator<ActivityManager.RunningAppProcessInfo> it = ((ActivityManager) context.getSystemService("activity")).getRunningAppProcesses().iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            ActivityManager.RunningAppProcessInfo next = it.next();
            if (next.processName.equals(context.getPackageName())) {
                if (next.importance == 400) {
                    return true;
                }
            }
        }
        return false;
    }
}
