OSAddAndConfigWiFiActivity.java 源代码


package liteos.addCamera;

import activity.addCamera.TakeNameToCamActivity;
import android.app.ActionBar;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.ColorDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import base.HiActivity;
import bean.EventReceive4179;
import bean.MyCamera;
import com.hichip.base.HiLog;
import com.hichip.callback.ICameraIOSessionCallback;
import com.hichip.campro.R;
import com.hichip.content.HiChipDefines;
import com.hichip.control.HiCamera;
import common.Constant;
import common.ConstantCommand;
import common.HiDataValue;
import common.TitleView;
import java.util.Iterator;
import java.util.Objects;
import main.MainActivity;
import main.NVR.adapter.NVRListBean;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import service.WakeUpDevService;
import utils.EmojiFilter;
import utils.FullCharUnionFilter;
import utils.HiLogcatUtil;
import utils.HiTools;
import utils.MyToast;
import utils.NotCopyAndPaste;
import utils.SharePreUtils;
import utils.SystemUtils;
import utils.UninstallDevTypeKeep;
import utils.WifiAdmin;
import utils.WifiUtils;

public class OSAddAndConfigWiFiActivity extends HiActivity implements View.OnClickListener, ICameraIOSessionCallback {
    Button but_complete;
    private MyCamera camera;
    private CountDownTimer countDownTimer;
    EditText et_add_camera_password;
    EditText et_add_camera_uid;
    EditText et_add_camera_username;
    private String mCurrentPhoneWiFi;
    private String mMac;
    private ConnectionChangeReceiver mReceiver;
    private String mSsid;
    private String mUid;
    private String mode;
    private String str_nick;
    private String str_password;
    private String str_uid;
    private String str_username;
    private CountDownTimer timer;
    TitleView titleview;
    private WifiManager wifiManager;
    private int mType = 0;
    private boolean isAP = false;
    private boolean isSupportFullChar = false;
    private boolean mconnectwifi_Q_fail = false;
    private boolean misADD_Q = false;
    private boolean miswificonnecttimeout = false;
    private Handler mLoadHandler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            super.handleMessage(message);
        }
    };
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            super.handleMessage(message);
            int i = message.what;
            if (i == -1879048191) {
                OSAddAndConfigWiFiActivity.this.handSessionState(message);
                return;
            }
            if (i == -1879048189) {
                if (message.arg2 == 0) {
                    OSAddAndConfigWiFiActivity.this.handIOCTRLSuccess(message);
                    return;
                } else {
                    OSAddAndConfigWiFiActivity.this.handIOCTRLFail(message);
                    return;
                }
            }
            if (i == 100) {
                if (OSAddAndConfigWiFiActivity.this.camera != null) {
                    OSAddAndConfigWiFiActivity oSAddAndConfigWiFiActivity = OSAddAndConfigWiFiActivity.this;
                    oSAddAndConfigWiFiActivity.goToAdd(oSAddAndConfigWiFiActivity.str_nick, OSAddAndConfigWiFiActivity.this.str_uid, OSAddAndConfigWiFiActivity.this.str_username, OSAddAndConfigWiFiActivity.this.str_password);
                    return;
                }
                return;
            }
            switch (i) {
                case 118:
                    HiLog.e("" + OSAddAndConfigWiFiActivity.this.mconnectwifi_Q_fail + "::" + OSAddAndConfigWiFiActivity.this.mHandler);
                    OSAddAndConfigWiFiActivity.this.mconnectwifi_Q_fail = true;
                    if (OSAddAndConfigWiFiActivity.this.mHandler != null) {
                        OSAddAndConfigWiFiActivity.this.mHandler.removeCallbacksAndMessages(null);
                    }
                    if (HiDataValue.wifiAdmin == null) {
                        HiDataValue.wifiAdmin = new WifiAdmin(OSAddAndConfigWiFiActivity.this.getApplicationContext());
                    }
                    HiDataValue.wifiAdmin.removeNetwork_Q();
                    if (OSAddAndConfigWiFiActivity.this.camera != null) {
                        OSAddAndConfigWiFiActivity.this.camera.disconnect(1);
                    }
                    OSAddAndConfigWiFiActivity.this.dismissLoadDialog();
                    OSAddAndConfigWiFiActivity.this.dealWithTimeOut();
                    return;
                case 119:
                    HiLog.e("" + OSAddAndConfigWiFiActivity.this.mconnectwifi_Q_fail + "::" + OSAddAndConfigWiFiActivity.this.mHandler);
                    OSAddAndConfigWiFiActivity.this.mconnectwifi_Q_fail = true;
                    HiDataValue.mwifiConnect_QLiteError = true;
                    if (OSAddAndConfigWiFiActivity.this.mHandler != null) {
                        OSAddAndConfigWiFiActivity.this.mHandler.removeCallbacksAndMessages(null);
                    }
                    if (HiDataValue.wifiAdmin == null) {
                        HiDataValue.wifiAdmin = new WifiAdmin(OSAddAndConfigWiFiActivity.this.getApplicationContext());
                    }
                    HiDataValue.wifiAdmin.removeNetwork_Q();
                    if (OSAddAndConfigWiFiActivity.this.camera != null) {
                        OSAddAndConfigWiFiActivity.this.camera.disconnect(1);
                    }
                    OSAddAndConfigWiFiActivity.this.dismissLoadDialog();
                    OSAddAndConfigWiFiActivity.this.dealWithTimeOut();
                    return;
                case 120:
                    HiLog.e("" + OSAddAndConfigWiFiActivity.this.mconnectwifi_Q_fail + "::" + OSAddAndConfigWiFiActivity.this.mHandler);
                    if (OSAddAndConfigWiFiActivity.this.mHandler != null) {
                        OSAddAndConfigWiFiActivity.this.mHandler.removeCallbacksAndMessages(null);
                    }
                    OSAddAndConfigWiFiActivity.this.mconnectwifi_Q_fail = true;
                    return;
                case 121:
                    HiLog.e("" + OSAddAndConfigWiFiActivity.this.mconnectwifi_Q_fail + "::" + OSAddAndConfigWiFiActivity.this.mHandler);
                    OSAddAndConfigWiFiActivity.this.mconnectwifi_Q_fail = false;
                    if (OSAddAndConfigWiFiActivity.this.mHandler != null) {
                        OSAddAndConfigWiFiActivity.this.mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (!OSAddAndConfigWiFiActivity.this.mconnectwifi_Q_fail) {
                                    HiLog.e("");
                                    if (OSAddAndConfigWiFiActivity.this.mHandler != null) {
                                        OSAddAndConfigWiFiActivity.this.mHandler.sendEmptyMessage(100);
                                        return;
                                    }
                                    return;
                                }
                                HiLog.e("" + OSAddAndConfigWiFiActivity.this.mconnectwifi_Q_fail);
                                if (OSAddAndConfigWiFiActivity.this.mHandler != null) {
                                    OSAddAndConfigWiFiActivity.this.mHandler.removeCallbacksAndMessages(null);
                                }
                                OSAddAndConfigWiFiActivity.this.mconnectwifi_Q_fail = true;
                                OSAddAndConfigWiFiActivity.this.dismissLoadDialog();
                                OSAddAndConfigWiFiActivity.this.dealWithTimeOut();
                            }
                        }, 1000L);
                        return;
                    }
                    return;
                default:
                    return;
            }
        }
    };

    @Override
    protected int setLayoutId() {
        return R.layout.activity_os_config_wifi_to_device;
    }

    @Override
    protected void init(Bundle bundle) {
        EventBus.getDefault().register(this);
        getIntentData();
        initViewAndData();
        setListeners();
    }

    private void registerRece() {
        IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
        ConnectionChangeReceiver connectionChangeReceiver = new ConnectionChangeReceiver();
        this.mReceiver = connectionChangeReceiver;
        registerReceiver(connectionChangeReceiver, intentFilter);
    }

    @Override
    public void onResume() {
        super.onResume();
        HiDataValue.isAPChooseWifi = false;
    }

    @Override
    public void onPause() {
        super.onPause();
        Handler handler = this.mHandler;
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mHandler = null;
        MyCamera myCamera = this.camera;
        if (myCamera != null) {
            myCamera.unregisterIOSessionListener(this);
        }
        EventBus.getDefault().unregister(this);
        ConnectionChangeReceiver connectionChangeReceiver = this.mReceiver;
        if (connectionChangeReceiver != null) {
            unregisterReceiver(connectionChangeReceiver);
        }
        CountDownTimer countDownTimer = this.countDownTimer;
        if (countDownTimer != null) {
            countDownTimer.cancel();
            this.countDownTimer = null;
        }
    }

    private void setListeners() {
        this.but_complete.setOnClickListener(this);
    }

    private void getIntentData() {
        this.mUid = getIntent().getStringExtra(HiDataValue.EXTRAS_KEY_UID);
        this.mSsid = getIntent().getStringExtra("ssid");
        this.mode = getIntent().getStringExtra(Constant.MODE);
        this.mMac = getIntent().getStringExtra(Constant.MAC);
        this.mType = getIntent().getIntExtra("type", 0);
        this.mCurrentPhoneWiFi = getIntent().getStringExtra("mCurrentPhoneWiFi");
        this.isAP = getIntent().getBooleanExtra("isAP", false);
        if (TextUtils.isEmpty(this.mCurrentPhoneWiFi)) {
            this.mCurrentPhoneWiFi = WifiUtils.getCurrentWifiSSID(this, true);
        }
    }

    private void initViewAndData() {
        this.titleview.setButton(0);
        this.titleview.setTitle(getString(R.string.title_equipment_information));
        this.titleview.setRightTxtBack(R.mipmap.x_bule);
        this.titleview.setNavigationBarButtonListener(new TitleView.NavigationBarButtonListener() {
            @Override
            public void OnNavigationButtonClick(int i) {
                if (i == 0) {
                    OSAddAndConfigWiFiActivity.this.mHandler = null;
                    OSAddAndConfigWiFiActivity.this.finish();
                } else {
                    if (i != 2) {
                        return;
                    }
                    OSAddAndConfigWiFiActivity.this.Hi_closeAPQ();
                    OSAddAndConfigWiFiActivity.this.startActivity(new Intent(OSAddAndConfigWiFiActivity.this, (Class<?>) MainActivity.class), true);
                }
            }
        });
        this.et_add_camera_uid.setText(this.mUid);
        if (!TextUtils.isEmpty(this.mUid)) {
            this.et_add_camera_uid.setEnabled(false);
        }
        this.wifiManager = (WifiManager) getApplicationContext().getSystemService("wifi");
        this.et_add_camera_password.setCustomSelectionActionModeCallback(new NotCopyAndPaste());
        this.et_add_camera_password.setFilters(new InputFilter[]{new InputFilter.LengthFilter(63), new FullCharUnionFilter(this), new EmojiFilter()});
        this.et_add_camera_username.setCustomSelectionActionModeCallback(new NotCopyAndPaste());
        this.et_add_camera_username.setFilters(new InputFilter[]{new InputFilter.LengthFilter(63), new FullCharUnionFilter(this), new EmojiFilter()});
    }

    public void Hi_closeAPQ() {
        if (HiDataValue.mHi_PhoneSys_Q) {
            if (HiDataValue.wifiAdmin == null) {
                HiDataValue.wifiAdmin = new WifiAdmin(getApplicationContext());
            }
            if (HiDataValue.wifiAdmin != null) {
                HiDataValue.wifiAdmin.removeNetwork_Q();
            }
            MyCamera myCamera = this.camera;
            if (myCamera != null) {
                myCamera.disconnect(1);
            }
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.but_complete) {
            if (this.mReceiver == null) {
                registerRece();
            }
            handButComPlete();
        } else {
            if (id != R.id.tv_hint) {
                return;
            }
            View inflate = View.inflate(this, R.layout.pup_wifi_require, null);
            final PopupWindow popupWindow = new PopupWindow(inflate);
            popupWindow.setBackgroundDrawable(new ColorDrawable(0));
            popupWindow.setWidth(-1);
            popupWindow.setHeight(-1);
            popupWindow.setFocusable(true);
            popupWindow.showAtLocation(inflate, 17, 0, 0);
            ((TextView) inflate.findViewById(R.id.tv_know)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view2) {
                    popupWindow.dismiss();
                }
            });
        }
    }

    private void handButComPlete() {
        this.str_uid = this.et_add_camera_uid.getText().toString().trim().toUpperCase();
        this.str_username = this.et_add_camera_username.getText().toString();
        this.str_password = this.et_add_camera_password.getText().toString();
        this.str_nick = getString(R.string.title_camera_fragment);
        if (this.str_username.length() == 0) {
            showAlert(getText(R.string.tips_null_username));
            return;
        }
        if (HiDataValue.CameraList != null && HiDataValue.CameraList.size() >= 64) {
            MyToast.showToast(this, getString(R.string.tips_limit_add_camera));
            return;
        }
        if (TextUtils.isEmpty(this.str_uid)) {
            showAlert(getText(R.string.tips_null_uid));
            return;
        }
        String handUid = HiTools.handUid(this.str_uid);
        this.str_uid = handUid;
        if (handUid == null) {
            MyToast.showToast(this, getString(R.string.tips_invalid_uid));
            return;
        }
        if (this.str_username.getBytes().length > 63) {
            MyToast.showToast(this, getString(R.string.tips_username_tolong));
            return;
        }
        if (this.str_password.getBytes().length > 63) {
            MyToast.showToast(this, getString(R.string.tips_password_tolong));
        } else {
            if (this.isAP) {
                this.mLoadHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (OSAddAndConfigWiFiActivity.this.mLoadDialoging == null || !OSAddAndConfigWiFiActivity.this.mLoadDialoging.isShowing() || OSAddAndConfigWiFiActivity.this.isFinishing()) {
                            return;
                        }
                        OSAddAndConfigWiFiActivity.this.dismissLoadDialog();
                        OSAddAndConfigWiFiActivity.this.dealWithTimeOut();
                    }
                }, 80000L);
                showLoadDialog(getString(R.string.adding_please_later), false, false);
                goToAdd(this.str_nick, this.str_uid, this.str_username, this.str_password);
                return;
            }
            handComplete();
        }
    }

    private void handComplete() {
        if (!this.wifiManager.isWifiEnabled()) {
            MyToast.showToast(this, getString(R.string.please_open_wifi));
            return;
        }
        this.mLoadHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (OSAddAndConfigWiFiActivity.this.mLoadDialoging == null || !OSAddAndConfigWiFiActivity.this.mLoadDialoging.isShowing() || OSAddAndConfigWiFiActivity.this.isFinishing()) {
                    return;
                }
                OSAddAndConfigWiFiActivity.this.dismissLoadDialog();
                OSAddAndConfigWiFiActivity.this.DoneGoToMain();
            }
        }, 80000L);
        showLoadDialog(getString(R.string.adding_please_later), false, false);
        goToAdd(this.str_nick, this.str_uid, this.str_username, this.str_password);
    }

    private void chickDone() {
        goToAdd(this.str_nick, this.str_uid, this.str_username, this.str_password);
        CountDownTimer countDownTimer = this.timer;
        if (countDownTimer != null) {
            countDownTimer.cancel();
            this.timer = null;
        }
        CountDownTimer countDownTimer2 = new CountDownTimer(40000L, 1000L) {
            @Override
            public void onTick(long j) {
            }

            @Override
            public void onFinish() {
                OSAddAndConfigWiFiActivity.this.wifiManager.startScan();
                Iterator<ScanResult> it = OSAddAndConfigWiFiActivity.this.wifiManager.getScanResults().iterator();
                while (it.hasNext()) {
                    String str = it.next().SSID;
                    if (!TextUtils.isEmpty(str) && (str.equals(OSAddAndConfigWiFiActivity.this.mSsid) || OSAddAndConfigWiFiActivity.this.mSsid.contains(str))) {
                        OSAddAndConfigWiFiActivity oSAddAndConfigWiFiActivity = OSAddAndConfigWiFiActivity.this;
                        MyToast.showToast(oSAddAndConfigWiFiActivity, oSAddAndConfigWiFiActivity.getString(R.string.config_fail_please_check_wifi));
                        OSAddAndConfigWiFiActivity.this.dismissLoadDialog();
                        return;
                    }
                }
                OSAddAndConfigWiFiActivity.this.DoneGoToMain();
            }
        };
        this.timer = countDownTimer2;
        countDownTimer2.start();
    }

    public void DoneGoToMain() {
        dismissLoadDialog();
        MyToast.showToast(this, getString(R.string.add_success));
        this.camera.setCameraLevel(1);
        this.camera.setNeedUpServer(true);
        this.camera.saveInDatabase(this);
        this.camera.saveInCameraList();
        MyCamera myCamera = this.camera;
        new NVRListBean(myCamera, 0, HiTools.isNVRDev(myCamera.getUid())).saveInCameraList();
        Intent intent = new Intent();
        intent.setAction(HiDataValue.ACTION_CAMERA_INIT_END);
        sendBroadcast(intent);
        Intent intent2 = new Intent(this, (Class<?>) TakeNameToCamActivity.class);
        intent2.putExtra(HiDataValue.EXTRAS_KEY_UID, this.camera.getUid());
        startActivity(intent2);
    }

    public void goToAdd(String str, String str2, String str3, String str4) {
        dismissLoadDialog();
        if (this.mode != null) {
            Handler handler = this.mLoadHandler;
            if (handler != null) {
                handler.removeCallbacksAndMessages(null);
            }
            this.mHandler = null;
            Intent intent = new Intent(this, (Class<?>) OSConfigWiFiToDeviceTwoActivity.class);
            intent.putExtra("isAP", this.isAP);
            intent.putExtra(Constant.MODE, this.mode);
            intent.putExtra(Constant.MAC, this.mMac);
            intent.putExtra("nick", str);
            intent.putExtra("username", str3);
            intent.putExtra("password", str4);
            intent.putExtra(HiDataValue.EXTRAS_KEY_UID, str2);
            intent.putExtra("mCurrentPhoneWiFi", this.mCurrentPhoneWiFi);
            intent.putExtra("isSupportFullChar", this.isSupportFullChar);
            intent.putExtra("ssid", this.mSsid);
            intent.putExtra("type", 21);
            startActivity(intent);
            EventBus.getDefault().post("disconnectCamera");
            finish();
            return;
        }
        this.camera = new MyCamera(getApplicationContext(), str, str2, str3, str4);
        boolean z = SharePreUtils.getBoolean(HiDataValue.CACHE, this, Constant.DEV_IsVivoMedia, false);
        if (SystemUtils.isVIVOMobile(this) && Build.VERSION.SDK_INT > 29 && !z) {
            this.camera.SetDecodeViVoVideoType(0);
        } else {
            this.camera.SetDecodeViVoVideoType(1);
        }
        this.camera.isFirstAdd = true;
        this.camera.registerIOSessionListener(this);
        if (this.isAP) {
            String currentWifiSSID = WifiUtils.getCurrentWifiSSID(this, true);
            HiLogcatUtil.i("connectSsid: " + currentWifiSSID + "---mSsid: " + this.mSsid);
            if ((!HiTools.isWifiConnected(this) || TextUtils.isEmpty(currentWifiSSID) || (!currentWifiSSID.startsWith(HiDataValue.START_WITH_IPCAM) && !HiDataValue.mHi_wifiConnect_Q.startsWith(HiDataValue.START_WITH_IPCAM))) && (TextUtils.isEmpty(HiDataValue.mHi_wifiConnect_Q) || !HiDataValue.mHi_wifiConnect_Q.equals(this.mSsid))) {
                if (!HiDataValue.mHi_PhoneSys_Q) {
                    if (!currentWifiSSID.equals(this.mSsid) && !TextUtils.isEmpty(this.mSsid)) {
                        changePhoneWifiToAP();
                    }
                } else {
                    HiLog.e("" + HiDataValue.mHi_wifiConnect_Q);
                    HiDataValue.mwifiConnect_QError = SharePreUtils.getBoolean(HiDataValue.CACHE, this, "wifiConnect_QError");
                    if (!HiDataValue.mwifiConnect_QLiteError && !HiDataValue.mwifiConnect_QError && (TextUtils.isEmpty(HiDataValue.mHi_wifiConnect_Q) || (!HiDataValue.mHi_wifiConnect_Q.equals(this.mSsid) && !TextUtils.isEmpty(this.mSsid)))) {
                        if (HiDataValue.wifiAdmin == null) {
                            HiDataValue.wifiAdmin = new WifiAdmin(getApplicationContext());
                        }
                        if (HiDataValue.wifiAdmin != null) {
                            this.mconnectwifi_Q_fail = false;
                            this.misADD_Q = true;
                            HiDataValue.wifiAdmin.removeNetwork_Q();
                            HiDataValue.wifiAdmin.addNetwork1_Q(this.mSsid, "01234567", this.mHandler);
                        }
                    }
                }
            }
        }
        wakeUpAndConnect(this.camera);
    }

    private void changePhoneWifiToAP() {
        this.miswificonnecttimeout = false;
        this.mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (OSAddAndConfigWiFiActivity.this.mLoadDialoging == null || !OSAddAndConfigWiFiActivity.this.mLoadDialoging.isShowing() || OSAddAndConfigWiFiActivity.this.isFinishing()) {
                    return;
                }
                OSAddAndConfigWiFiActivity.this.miswificonnecttimeout = true;
                HiLog.e("dismissLoadDialog");
                OSAddAndConfigWiFiActivity.this.dismissLoadDialog();
                OSAddAndConfigWiFiActivity.this.dealWithTimeOut();
            }
        }, 40000L);
        CountDownTimer countDownTimer = new CountDownTimer(16000L, 4000L) {
            @Override
            public void onFinish() {
            }

            @Override
            public void onTick(long j) {
                OSAddAndConfigWiFiActivity.this.handConnect();
            }
        };
        this.countDownTimer = countDownTimer;
        countDownTimer.start();
    }

    public void handConnect() {
        if (HiDataValue.wifiAdmin == null) {
            HiDataValue.wifiAdmin = new WifiAdmin(getApplicationContext());
        }
        WifiConfiguration IsExsits = HiDataValue.wifiAdmin.IsExsits(this.mSsid);
        WifiConnect(HiDataValue.wifiAdmin);
        if (IsExsits == null) {
            HiDataValue.wifiAdmin.addNetwork(HiDataValue.wifiAdmin.CreateWifiInfo(this.mSsid, "01234567", 3));
        }
    }

    private void WifiConnect(WifiAdmin wifiAdmin) {
        WifiManager wifiManager = wifiAdmin.mWifiManager;
        String str = this.mSsid;
        WifiConfiguration IsExsits = wifiAdmin.IsExsits(str);
        if (IsExsits == null) {
            wifiManager.enableNetwork(wifiManager.addNetwork(wifiAdmin.CreateWifiInfo(str, "01234567", 3)), true);
        } else {
            wifiManager.enableNetwork(IsExsits.networkId, true);
        }
    }

    public class ConnectionChangeReceiver extends BroadcastReceiver {
        private ConnectionChangeReceiver() {
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            NetworkInfo networkInfo = ((ConnectivityManager) OSAddAndConfigWiFiActivity.this.getSystemService("connectivity")).getNetworkInfo(1);
            if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) {
                WifiInfo connectionInfo = ((WifiManager) OSAddAndConfigWiFiActivity.this.getApplicationContext().getSystemService("wifi")).getConnectionInfo();
                Log.e("test", "AddAndConfigWiFiActivity: onReceive  " + connectionInfo.getSSID());
                if (TextUtils.isEmpty(OSAddAndConfigWiFiActivity.this.mSsid) || !connectionInfo.getSSID().contains(OSAddAndConfigWiFiActivity.this.mSsid) || OSAddAndConfigWiFiActivity.this.miswificonnecttimeout || OSAddAndConfigWiFiActivity.this.camera == null) {
                    return;
                }
                OSAddAndConfigWiFiActivity oSAddAndConfigWiFiActivity = OSAddAndConfigWiFiActivity.this;
                oSAddAndConfigWiFiActivity.wakeUpAndConnect(oSAddAndConfigWiFiActivity.camera);
            }
        }
    }

    public void wakeUpAndConnect(MyCamera myCamera) {
        Intent intent = new Intent(this, (Class<?>) WakeUpDevService.class);
        intent.putExtra(HiDataValue.EXTRAS_KEY_UID, myCamera.getUid());
        startService(intent);
    }

    @Override
    public void receiveSessionState(HiCamera hiCamera, int i) {
        if (hiCamera != this.camera || this.mHandler == null) {
            return;
        }
        Message obtain = Message.obtain();
        obtain.what = HiDataValue.HANDLE_MESSAGE_SESSION_STATE;
        obtain.arg1 = i;
        this.mHandler.sendMessage(obtain);
    }

    @Override
    public void receiveIOCtrlData(HiCamera hiCamera, int i, byte[] bArr, int i2) {
        if (hiCamera != this.camera || this.mHandler == null) {
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putByteArray("data", bArr);
        Message obtainMessage = this.mHandler.obtainMessage();
        obtainMessage.what = HiDataValue.HANDLE_MESSAGE_RECEIVE_IOCTRL;
        obtainMessage.obj = hiCamera;
        obtainMessage.arg1 = i;
        obtainMessage.arg2 = i2;
        obtainMessage.setData(bundle);
        this.mHandler.sendMessage(obtainMessage);
    }

    public void handIOCTRLFail(Message message) {
        int i = message.arg1;
        if (i == 16761) {
            this.camera.mIsReceived_4179 = true;
            EventBus.getDefault().postSticky(new EventReceive4179(true));
            this.camera.isWallMounted = false;
            SharePreUtils.putBoolean(HiDataValue.CACHE, this, this.camera.getUid() + Constant.ISWALLMOUNTED, false);
            UninstallDevTypeKeep.putValue(this, this.camera.getUid(), Constant.ISFISHEYE, this.camera.getmold() + "");
            return;
        }
        if (i == 16762) {
            dismissLoadDialog();
            DoneGoToMain();
            return;
        }
        if (i == 16764) {
            DoneGoToMain();
            return;
        }
        switch (i) {
            case HiChipDefines.HI_P2P_GET_WIFI_PARAM:
                dismissLoadDialog();
                DoneGoToMain();
                return;
            case HiChipDefines.HI_P2P_SET_WIFI_PARAM:
                dismissLoadDialog();
                return;
            case HiChipDefines.HI_P2P_GET_WIFI_LIST:
                DoneGoToMain();
                return;
            default:
                return;
        }
    }

    public void handIOCTRLSuccess(Message message) {
        byte[] byteArray = message.getData().getByteArray("data");
        int i = message.arg1;
        if (i != 16761) {
            if (i == 16762) {
                if (TextUtils.isEmpty(new String(new HiChipDefines.HI_P2P_S_WIFI_PARAM_NEWPWD255_EXT1(byteArray).strSSID).trim())) {
                    if (this.camera.appGetCommandFunction(HiChipDefines.HI_P2P_GET_WIFI_LIST_EXT256)) {
                        this.camera.sendIOCtrl(HiChipDefines.HI_P2P_GET_WIFI_LIST_EXT256, new byte[0]);
                        return;
                    } else {
                        this.camera.sendIOCtrl(HiChipDefines.HI_P2P_GET_WIFI_LIST, new byte[0]);
                        return;
                    }
                }
                DoneGoToMain();
                return;
            }
            if (i == 16764) {
                dismissLoadDialog();
                if (byteArray.length >= 72) {
                    Intent intent = new Intent(this, (Class<?>) OSConfigWiFiToDeviceTwoActivity.class);
                    intent.putExtra(HiDataValue.EXTRAS_KEY_UID, this.camera.getUid());
                    intent.putExtra("isAP", this.isAP);
                    intent.putExtra("nick", this.camera.getNikeName());
                    intent.putExtra("username", this.camera.getUsername());
                    intent.putExtra("password", this.camera.getPassword());
                    intent.putExtra("mCurrentPhoneWiFi", this.mCurrentPhoneWiFi);
                    intent.putExtra("isSupportFullChar", this.isSupportFullChar);
                    intent.putExtra("ssid", this.mSsid);
                    intent.putExtra("type", 21);
                    startActivity(intent);
                    EventBus.getDefault().post("disconnectCamera");
                    finish();
                    return;
                }
                DoneGoToMain();
                return;
            }
            if (i == 61445) {
                this.camera.appSetCommandFunction(new ConstantCommand.HI_P2P_FUNCTION_LITE(byteArray));
                return;
            }
            switch (i) {
                case HiChipDefines.HI_P2P_GET_WIFI_PARAM:
                    if (TextUtils.isEmpty(new String(new HiChipDefines.HI_P2P_S_WIFI_PARAM(byteArray).strSSID).trim())) {
                        if (this.camera.appGetCommandFunction(HiChipDefines.HI_P2P_GET_WIFI_LIST_EXT256)) {
                            this.camera.sendIOCtrl(HiChipDefines.HI_P2P_GET_WIFI_LIST_EXT256, new byte[0]);
                            return;
                        } else {
                            this.camera.sendIOCtrl(HiChipDefines.HI_P2P_GET_WIFI_LIST, new byte[0]);
                            return;
                        }
                    }
                    DoneGoToMain();
                    return;
                case HiChipDefines.HI_P2P_SET_WIFI_PARAM:
                    dismissLoadDialog();
                    return;
                case HiChipDefines.HI_P2P_GET_WIFI_LIST:
                    dismissLoadDialog();
                    if (byteArray.length >= 40) {
                        Intent intent2 = new Intent(this, (Class<?>) OSConfigWiFiToDeviceTwoActivity.class);
                        intent2.putExtra(HiDataValue.EXTRAS_KEY_UID, this.camera.getUid());
                        intent2.putExtra("isAP", this.isAP);
                        intent2.putExtra("nick", this.camera.getNikeName());
                        intent2.putExtra("username", this.camera.getUsername());
                        intent2.putExtra("password", this.camera.getPassword());
                        intent2.putExtra("mCurrentPhoneWiFi", this.mCurrentPhoneWiFi);
                        intent2.putExtra("isSupportFullChar", this.isSupportFullChar);
                        intent2.putExtra("ssid", this.mSsid);
                        intent2.putExtra("type", 21);
                        startActivity(intent2);
                        EventBus.getDefault().post("disconnectCamera");
                        finish();
                        return;
                    }
                    DoneGoToMain();
                    return;
                default:
                    return;
            }
        }
        this.camera.mIsReceived_4179 = true;
        EventBus.getDefault().postSticky(new EventReceive4179(true));
        HiChipDefines.HI_P2P_DEV_FISH hi_p2p_dev_fish = new HiChipDefines.HI_P2P_DEV_FISH(byteArray);
        float f = hi_p2p_dev_fish.xcircle;
        float f2 = hi_p2p_dev_fish.ycircle;
        float f3 = hi_p2p_dev_fish.rcircle;
        SharePreUtils.putFloat(HiDataValue.CACHE, this, this.camera.getUid() + "xcircle", f);
        SharePreUtils.putFloat(HiDataValue.CACHE, this, this.camera.getUid() + "ycircle", f2);
        SharePreUtils.putFloat(HiDataValue.CACHE, this, this.camera.getUid() + "rcircle", f3);
        SharePreUtils.putInt(Constant.INSTALLMODE, this, this.camera.getUid(), hi_p2p_dev_fish.mold);
        UninstallDevTypeKeep.putValue(this, this.camera.getUid(), Constant.X, f + "");
        UninstallDevTypeKeep.putValue(this, this.camera.getUid(), Constant.Y, f2 + "");
        UninstallDevTypeKeep.putValue(this, this.camera.getUid(), Constant.R, f3 + "");
        UninstallDevTypeKeep.putValue(this, this.camera.getUid(), Constant.INSTALLMODE, hi_p2p_dev_fish.mold + "");
        if (hi_p2p_dev_fish.fish == 1 && (hi_p2p_dev_fish.type == 2 || hi_p2p_dev_fish.type == 4)) {
            this.camera.isWallMounted = true;
            SharePreUtils.putBoolean(HiDataValue.CACHE, this, this.camera.getUid() + Constant.ISWALLMOUNTED, true);
            UninstallDevTypeKeep.putValue(this, this.camera.getUid(), Constant.ISWALLMOUNTED, this.camera.isWallMounted + "");
        } else {
            this.camera.isWallMounted = false;
            SharePreUtils.putBoolean(HiDataValue.CACHE, this, this.camera.getUid() + Constant.ISWALLMOUNTED, false);
            UninstallDevTypeKeep.putValue(this, this.camera.getUid(), Constant.ISFISHEYE, this.camera.getmold() + "");
        }
        this.camera.putFishModType(hi_p2p_dev_fish.type);
    }

    public void handSessionState(Message message) {
        int i = message.arg1;
        if (i == 3) {
            Handler handler = this.mLoadHandler;
            if (handler != null) {
                handler.removeCallbacksAndMessages(null);
            }
            MyToast.showToast(this, getString(R.string.device_info_error));
            dismissLoadDialog();
            return;
        }
        if (i != 4) {
            return;
        }
        dismissLoadDialog();
        this.isSupportFullChar = this.camera.appGetCommandFunction(HiChipDefines.HI_P2P_GET_CHAR);
        if (this.isAP) {
            dismissLoadDialog();
            Handler handler2 = this.mLoadHandler;
            if (handler2 != null) {
                handler2.removeCallbacksAndMessages(null);
            }
            this.mHandler = null;
            Intent intent = new Intent(this, (Class<?>) OSConfigWiFiToDeviceTwoActivity.class);
            intent.putExtra(HiDataValue.EXTRAS_KEY_UID, this.str_uid);
            intent.putExtra("nick", this.str_nick);
            intent.putExtra("username", this.str_username);
            intent.putExtra("password", this.str_password);
            intent.putExtra("mCurrentPhoneWiFi", this.mCurrentPhoneWiFi);
            intent.putExtra("isAP", this.isAP);
            intent.putExtra(Constant.MODE, this.mode);
            intent.putExtra(Constant.MAC, this.mMac);
            intent.putExtra("isSupportFullChar", this.isSupportFullChar);
            intent.putExtra("ssid", this.mSsid);
            intent.putExtra("type", 21);
            startActivity(intent);
            EventBus.getDefault().post("disconnectCamera");
            finish();
            return;
        }
        if (this.camera.appGetCommandFunction(HiChipDefines.HI_P2P_GET_WIFI_PARAM_NEWPWD255_EXT1)) {
            this.camera.sendIOCtrl(HiChipDefines.HI_P2P_GET_WIFI_PARAM_NEWPWD255_EXT1, new byte[0]);
        } else {
            this.camera.sendIOCtrl(HiChipDefines.HI_P2P_GET_WIFI_PARAM, null);
        }
    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void disconnectCamera(String str) {
        if (TextUtils.isEmpty(str) || this.camera == null || !str.equals("disconnectCamera")) {
            return;
        }
        this.camera.disconnect(1);
        Log.i("tedu", this.camera.getUid() + ": disconnect....");
    }

    public void dealWithTimeOut() {
        if (isFinishing()) {
            return;
        }
        final Dialog dialog = new Dialog(this, R.style.CustomDialog);
        View inflate = View.inflate(this, R.layout.dialog_deal_with_ap_connect_fail, null);
        Window window = dialog.getWindow();
        Objects.requireNonNull(window);
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = (int) (getResources().getDisplayMetrics().widthPixels * 0.85d);
        ((TextView) inflate.findViewById(R.id.tv_content)).setText(getString(R.string.ap_connect_hint) + "\n" + getString(R.string.wireless_name) + "  " + this.mSsid + "\n" + getString(R.string.wifi_password) + " 01234567");
        inflate.findViewById(R.id.tv_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View view) {
                OSAddAndConfigWiFiActivity.this.lambda$dealWithTimeOut$0$OSAddAndConfigWiFiActivity(dialog, view);
            }
        });
        inflate.findViewById(R.id.tv_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View view) {
                OSAddAndConfigWiFiActivity.this.lambda$dealWithTimeOut$1$OSAddAndConfigWiFiActivity(dialog, view);
            }
        });
        dialog.setCancelable(false);
        dialog.addContentView(inflate, new ActionBar.LayoutParams(-1, -2));
        dialog.getWindow().setAttributes(attributes);
        dialog.show();
    }

    public void lambda$dealWithTimeOut$0$OSAddAndConfigWiFiActivity(Dialog dialog, View view) {
        dialog.dismiss();
        this.mHandler = null;
        if (HiDataValue.mHi_PhoneSys_Q) {
            if (HiDataValue.wifiAdmin == null) {
                HiDataValue.wifiAdmin = new WifiAdmin(getApplicationContext());
            }
            if (HiDataValue.wifiAdmin != null) {
                HiDataValue.wifiAdmin.removeNetwork_Q();
            }
            MyCamera myCamera = this.camera;
            if (myCamera != null) {
                myCamera.disconnect(1);
            }
        }
        finish();
    }

    public void lambda$dealWithTimeOut$1$OSAddAndConfigWiFiActivity(Dialog dialog, View view) {
        dialog.dismiss();
        HiDataValue.isAPChooseWifi = true;
        if (HiDataValue.mHi_PhoneSys_Q) {
            if (HiDataValue.wifiAdmin == null) {
                HiDataValue.wifiAdmin = new WifiAdmin(getApplicationContext());
            }
            if (HiDataValue.wifiAdmin != null) {
                HiDataValue.wifiAdmin.removeNetwork_Q();
            }
            MyCamera myCamera = this.camera;
            if (myCamera != null) {
                myCamera.disconnect(1);
            }
        }
        Intent intent = new Intent("android.settings.WIFI_SETTINGS");
        intent.setFlags(268435456);
        startActivity(intent);
    }
}