package com.ifpdos.factorytest;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Handler;
import android.provider.Settings;
import android.util.AttributeSet;
import android.util.Log;
import com.ifpdos.factorytest.util.VolumeUtil;
import com.seewo.sdk.SDKAudioHelper;
import com.seewo.vcommons.constants.SeewoIntent;

/* JADX INFO: loaded from: classes.dex */
public class BluetoothTest extends TestItem {
    private static final String BLUEBOOTH_DETECTED = "bluebooth_detected";
    private static final String BLUETOOTH_DETECTED = "bluetooth_detected";
    private static final String TAG = "BlutoothTest";
    private final int TIMEOUT_MS;
    private AskDialog askDialog;
    private BluetoothAdapter blueadapter;
    private OnAskDialogResultListener mAskRetListener;
    private Handler mHandler;
    private IntentFilter mIntentFilter;
    private LocalBluetoothAdapter mLocalManager;
    private MediaPlayer mMediaPlay;
    private BroadcastReceiver mReceiver;
    private boolean orgBluetoothStatus;
    private Runnable runnable;
    private AskDialog tipsDialog;
    private int volume;

    public BluetoothTest(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.mMediaPlay = null;
        this.volume = 0;
        this.mLocalManager = null;
        this.mIntentFilter = null;
        this.mReceiver = null;
        this.mAskRetListener = null;
        this.TIMEOUT_MS = 10000;
        this.mHandler = new Handler();
        this.orgBluetoothStatus = false;
        try {
            this.blueadapter = BluetoothAdapter.getDefaultAdapter();
            this.mLocalManager = LocalBluetoothAdapter.getInstance();
        } catch (Exception e) {
            e.printStackTrace();
            testDone(false, this.mTagDone);
            this.mLocalManager = null;
        }
        this.orgBluetoothStatus = this.blueadapter.isEnabled();
        Log.d(TAG, "BluetoothTest: orgBluetoothStatus = " + this.orgBluetoothStatus);
        this.runnable = new Runnable() { // from class: com.ifpdos.factorytest.BluetoothTest.1
            @Override // java.lang.Runnable
            public void run() {
                if (BluetoothTest.this.tipsDialog != null) {
                    Log.d(BluetoothTest.TAG, "Dialog not dismissed within 10 seconds");
                    BluetoothTest.this.tipsDialog.dismiss();
                    BluetoothTest bluetoothTest = BluetoothTest.this;
                    bluetoothTest.testDone(false, bluetoothTest.mContext.getResources().getString(R.string.open_bluetooth_fail));
                }
            }
        };
    }

    @Override // com.ifpdos.factorytest.TestItem
    boolean doTest() {
        if (!getBlueToothChipState() && !ConfigParser.isSmartCustomer()) {
            testDone(false, this.mContext.getResources().getString(R.string.bluetooth_not_enable));
            return false;
        }
        registerBlueToothBroadcast();
        this.mAskRetListener = new AnonymousClass2();
        this.askDialog = new AskDialog(this.mContext, getTitle(), this.mContext.getString(R.string.test_yes_or_no_pass), this.mAskRetListener);
        playBlueToothMusic();
        Log.d(TAG, "Status = " + this.mLocalManager.getBluetoothState());
        if (this.mLocalManager.getBluetoothState() == 12 || this.mLocalManager.getBluetoothState() == 11) {
            startBlueToothSetting();
        } else {
            this.mLocalManager.setBluetoothEnabled(true);
            this.tipsDialog = new AskDialog(this.mContext, false, this.mTagYes, false, null, getTitle(), this.mContext.getResources().getString(R.string.bluetooth_enabling), null);
            this.mHandler.postDelayed(this.runnable, 10000L);
        }
        return true;
    }

    /* JADX INFO: renamed from: com.ifpdos.factorytest.BluetoothTest$2, reason: invalid class name */
    class AnonymousClass2 extends OnAskDialogResultListener {
        AnonymousClass2() {
        }

        @Override // com.ifpdos.factorytest.OnAskDialogResultListener
        public void onAskDialogResult(Context context, boolean z) {
            BluetoothTest.this.mHandler.postDelayed(new Runnable() { // from class: com.ifpdos.factorytest.BluetoothTest$2$$ExternalSyntheticLambda0
                @Override // java.lang.Runnable
                public final void run() {
                    this.f$0.lambda$onAskDialogResult$0();
                }
            }, 100L);
            BluetoothTest.this.askDialog = null;
            BluetoothTest bluetoothTest = BluetoothTest.this;
            bluetoothTest.testDone(z, bluetoothTest.mTagDone);
        }

        /* JADX INFO: Access modifiers changed from: private */
        public /* synthetic */ void lambda$onAskDialogResult$0() {
            if (BluetoothTest.this.mLocalManager == null || !BluetoothTest.this.blueadapter.isEnabled()) {
                return;
            }
            BluetoothTest.this.mLocalManager.setBluetoothEnabled(false);
        }
    }

    private void registerBlueToothBroadcast() {
        this.mReceiver = new BroadcastReceiver() { // from class: com.ifpdos.factorytest.BluetoothTest.3
            @Override // android.content.BroadcastReceiver
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (action == null || !action.equals("android.bluetooth.adapter.action.STATE_CHANGED")) {
                    return;
                }
                BluetoothTest.this.handleBluetoothStateChanged(intent.getIntExtra("android.bluetooth.adapter.extra.STATE", Integer.MIN_VALUE));
            }
        };
        IntentFilter intentFilter = new IntentFilter();
        this.mIntentFilter = intentFilter;
        if (this.mLocalManager != null) {
            intentFilter.addAction("android.bluetooth.adapter.action.STATE_CHANGED");
        }
        if (this.mContext != null) {
            this.mContext.registerReceiver(this.mReceiver, this.mIntentFilter);
        }
    }

    private boolean getBlueToothChipState() {
        int i = Settings.Global.getInt(this.mContext.getContentResolver(), BLUETOOTH_DETECTED, 0);
        int i2 = Settings.Global.getInt(this.mContext.getContentResolver(), BLUEBOOTH_DETECTED, 0);
        Log.d(TAG, "getBlueToothChipState = " + i);
        Log.d(TAG, "getBlueboothChipState = " + i2);
        return i == 1 || i2 == 1;
    }

    static String getMacAddr() {
        long jCurrentTimeMillis = System.currentTimeMillis();
        String address = null;
        while (address == null) {
            try {
                Thread.sleep(100L);
            } catch (Exception unused) {
            }
            BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
            if (defaultAdapter != null) {
                address = defaultAdapter.isEnabled() ? defaultAdapter.getAddress() : null;
            }
            if (2000 + jCurrentTimeMillis < System.currentTimeMillis()) {
                break;
            }
        }
        return address == null ? "unknow" : address;
    }

    static boolean macIsNotMatch(String str) {
        return (MainTestActivity.mTestMode != 0 || ConfigParameter.mBluetoothMacPrefix.isEmpty() || str.startsWith(ConfigParameter.mBluetoothMacPrefix)) ? false : true;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void handleBluetoothStateChanged(int i) {
        String string;
        Log.d(TAG, "bluetooth state=" + i);
        if (this.mContext == null) {
            return;
        }
        switch (i) {
            case 10:
                string = this.mContext.getResources().getString(R.string.disabled);
                break;
            case 11:
                string = this.mContext.getResources().getString(R.string.openning);
                break;
            case 12:
                string = this.mContext.getResources().getString(R.string.opened);
                startBlueToothSetting();
                break;
            case 13:
                string = this.mContext.getResources().getString(R.string.closing);
                break;
            default:
                string = this.mContext.getResources().getString(R.string.error);
                break;
        }
        setStatus(string);
    }

    private void startBlueToothSetting() {
        Intent intent = new Intent(SeewoIntent.ACTION_BLUETOOTH_SETTING);
        intent.setFlags(SeewoIntent.DRAWER_STATE_LEFT);
        this.mContext.startActivity(intent);
        this.mContext.startService(new Intent(this.mContext, (Class<?>) FloatNavbarService.class));
        AskDialog askDialog = this.tipsDialog;
        if (askDialog != null) {
            askDialog.dismiss();
        }
        this.mHandler.removeCallbacks(this.runnable);
    }

    private void playBlueToothMusic() {
        try {
            this.volume = SDKAudioHelper.I.getVolume();
            Log.d(TAG, "Bluetooth test origin volume:" + this.volume);
            VolumeUtil.INSTANCE.setVolume(50, true);
            MediaPlayer mediaPlayerCreate = MediaPlayer.create(this.mContext, R.raw.music);
            this.mMediaPlay = mediaPlayerCreate;
            mediaPlayerCreate.seekTo(0);
            this.mMediaPlay.start();
            this.mMediaPlay.setLooping(true);
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }

    private void releaseBlueToothMusic() {
        try {
            Log.d(TAG, "media release  begin");
            this.mMediaPlay.stop();
            this.mMediaPlay.release();
            VolumeUtil.INSTANCE.setVolume(this.volume, false);
        } catch (Exception e) {
            Log.e(TAG, "media release  error !!!", e);
        }
    }

    @Override // com.ifpdos.factorytest.TestItem
    void onStop() {
        super.onStop();
        if (this.askDialog != null) {
            this.mContext.startService(new Intent(this.mContext, (Class<?>) FloatNavbarService.class));
        }
    }

    @Override // com.ifpdos.factorytest.TestItem
    void testDone(boolean z, String str) {
        this.mTested = true;
        this.mStarted = false;
        setResult(z);
        setStatus(str);
        if (this.mOnTestDoneListener != null) {
            this.mOnTestDoneListener.onTestDone(this, this.mId, z, this.mTagDone);
        }
        try {
            AskDialog askDialog = this.askDialog;
            if (askDialog != null) {
                askDialog.dismiss();
            }
            if (this.mContext != null && this.mReceiver != null) {
                this.mContext.unregisterReceiver(this.mReceiver);
            }
        } catch (Exception e) {
            Log.e(TAG, "testDone  error !!!", e);
        }
        releaseBlueToothMusic();
    }
}
