AddAndConfigWiFiActivity.java 源代码


package activity.addCamera;

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.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 com.hichip.tools.Packet;
import com.xiaomi.mipush.sdk.Constants;
import common.Constant;
import common.ConstantCommand;
import common.HiDataValue;
import common.TitleView;
import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import main.MainActivity;
import main.NVR.adapter.NVRListBean;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
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 AddAndConfigWiFiActivity extends HiActivity implements View.OnClickListener, ICameraIOSessionCallback {
    Button but_complete;
    private CountDownTimer countDownTimer;
    EditText et_add_camera_password;
    EditText et_add_camera_uid;
    EditText et_add_camera_username;
    private MyCamera mCamera;
    private String mCurrentPhoneWiFi;
    private String mIp;
    private ConnectionChangeReceiver mReceiver;
    private String mSsid;
    private String mUid;
    private String str_nick;
    private String str_password;
    private String str_uid;
    private String str_username;
    TitleView titleview;
    private WifiManager wifiManager;
    private int mType = 0;
    private boolean isAP = false;
    private boolean isSupportFullChar = false;
    private int myEncType = -1;
    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) {
                AddAndConfigWiFiActivity.this.handSessionState(message);
                return;
            }
            if (i == -1879048189) {
                if (message.arg2 == 0) {
                    AddAndConfigWiFiActivity.this.handIOCTRLSuccess(message);
                    return;
                } else {
                    AddAndConfigWiFiActivity.this.handIOCTRLFail(message);
                    return;
                }
            }
            if (i == 100) {
                if (AddAndConfigWiFiActivity.this.mCamera != null) {
                    if (AddAndConfigWiFiActivity.this.mHandler != null) {
                        AddAndConfigWiFiActivity.this.mHandler.removeCallbacksAndMessages(null);
                    }
                    AddAndConfigWiFiActivity.this.mCamera.registerIOSessionListener(AddAndConfigWiFiActivity.this);
                    AddAndConfigWiFiActivity.this.mCamera.connect();
                    AddAndConfigWiFiActivity.this.mCamera.setReconnectTimes(10);
                    AddAndConfigWiFiActivity.this.mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (AddAndConfigWiFiActivity.this.mLoadDialoging == null || !AddAndConfigWiFiActivity.this.mLoadDialoging.isShowing() || AddAndConfigWiFiActivity.this.isFinishing()) {
                                return;
                            }
                            AddAndConfigWiFiActivity.this.miswificonnecttimeout = true;
                            HiLog.e("in dealWithTimeOut()");
                            AddAndConfigWiFiActivity.this.dismissLoadDialog();
                            AddAndConfigWiFiActivity.this.dealWithTimeOut();
                        }
                    }, 40000L);
                    return;
                }
                return;
            }
            switch (i) {
                case 118:
                    HiLog.e("" + AddAndConfigWiFiActivity.this.mconnectwifi_Q_fail + "::" + AddAndConfigWiFiActivity.this.mHandler);
                    AddAndConfigWiFiActivity.this.mconnectwifi_Q_fail = true;
                    if (AddAndConfigWiFiActivity.this.mHandler != null) {
                        AddAndConfigWiFiActivity.this.mHandler.removeCallbacksAndMessages(null);
                    }
                    if (HiDataValue.wifiAdmin == null) {
                        HiDataValue.wifiAdmin = new WifiAdmin(AddAndConfigWiFiActivity.this.getApplicationContext());
                    }
                    HiDataValue.wifiAdmin.removeNetwork_Q();
                    AddAndConfigWiFiActivity.this.dismissLoadDialog();
                    HiLog.e("in dealWithTimeOut()");
                    AddAndConfigWiFiActivity.this.dealWithConfigFail();
                    return;
                case 119:
                    HiLog.e("" + AddAndConfigWiFiActivity.this.mconnectwifi_Q_fail + "::" + AddAndConfigWiFiActivity.this.mHandler);
                    AddAndConfigWiFiActivity.this.mconnectwifi_Q_fail = true;
                    HiDataValue.mwifiConnect_QNorError = true;
                    if (AddAndConfigWiFiActivity.this.mHandler != null) {
                        AddAndConfigWiFiActivity.this.mHandler.removeCallbacksAndMessages(null);
                    }
                    if (HiDataValue.wifiAdmin == null) {
                        HiDataValue.wifiAdmin = new WifiAdmin(AddAndConfigWiFiActivity.this.getApplicationContext());
                    }
                    HiDataValue.wifiAdmin.removeNetwork_Q();
                    AddAndConfigWiFiActivity.this.dismissLoadDialog();
                    HiLog.e("in dealWithTimeOut()");
                    AddAndConfigWiFiActivity.this.dealWithTimeOut();
                    return;
                case 120:
                    HiLog.e("" + AddAndConfigWiFiActivity.this.mconnectwifi_Q_fail + "::" + AddAndConfigWiFiActivity.this.mHandler);
                    if (AddAndConfigWiFiActivity.this.mHandler != null) {
                        AddAndConfigWiFiActivity.this.mHandler.removeCallbacksAndMessages(null);
                    }
                    AddAndConfigWiFiActivity.this.mconnectwifi_Q_fail = true;
                    return;
                case 121:
                    HiLog.e("" + AddAndConfigWiFiActivity.this.mconnectwifi_Q_fail + "::" + AddAndConfigWiFiActivity.this.mHandler);
                    AddAndConfigWiFiActivity.this.mconnectwifi_Q_fail = false;
                    if (AddAndConfigWiFiActivity.this.mHandler != null) {
                        AddAndConfigWiFiActivity.this.mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (!AddAndConfigWiFiActivity.this.mconnectwifi_Q_fail) {
                                    HiLog.e("");
                                    if (AddAndConfigWiFiActivity.this.mHandler != null) {
                                        AddAndConfigWiFiActivity.this.mHandler.sendEmptyMessage(100);
                                        return;
                                    }
                                    return;
                                }
                                HiLog.e("" + AddAndConfigWiFiActivity.this.mconnectwifi_Q_fail);
                                if (AddAndConfigWiFiActivity.this.mHandler != null) {
                                    AddAndConfigWiFiActivity.this.mHandler.removeCallbacksAndMessages(null);
                                }
                                AddAndConfigWiFiActivity.this.mconnectwifi_Q_fail = true;
                                AddAndConfigWiFiActivity.this.dismissLoadDialog();
                                HiLog.e("in dealWithTimeOut()");
                                AddAndConfigWiFiActivity.this.dealWithTimeOut();
                            }
                        }, 1000L);
                        return;
                    }
                    return;
                default:
                    return;
            }
        }
    };

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

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

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

    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.mType = getIntent().getIntExtra("type", 0);
        this.mCurrentPhoneWiFi = getIntent().getStringExtra("mCurrentPhoneWiFi");
        this.mIp = getIntent().getStringExtra(Constant.IP_STR);
        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) {
                    HiLog.e("");
                    AddAndConfigWiFiActivity.this.mHandler = null;
                    AddAndConfigWiFiActivity.this.finish();
                } else {
                    if (i != 2) {
                        return;
                    }
                    AddAndConfigWiFiActivity.this.Hi_closeAPQ();
                    AddAndConfigWiFiActivity.this.mHandler = null;
                    AddAndConfigWiFiActivity.this.startActivity(new Intent(AddAndConfigWiFiActivity.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.mCamera;
            if (myCamera != null) {
                myCamera.disconnect(1);
            }
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id != R.id.but_complete) {
            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();
                }
            });
            return;
        }
        if (this.mReceiver == null) {
            registerRece();
        }
        this.myEncType = -1;
        HiLog.e("" + HiDataValue.mHi_wifiConnect_Q + "::");
        dismissjuHuaDialog();
        handButComPlete();
    }

    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 final void run() {
                        AddAndConfigWiFiActivity.this.lambda$handButComPlete$0$AddAndConfigWiFiActivity();
                    }
                }, 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();
        }
    }

    public void lambda$handButComPlete$0$AddAndConfigWiFiActivity() {
        if (this.mLoadDialoging == null || !this.mLoadDialoging.isShowing() || isFinishing()) {
            return;
        }
        dismissLoadDialog();
        HiLog.e("in dealWithTimeOut()");
        dealWithTimeOut();
    }

    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 (AddAndConfigWiFiActivity.this.mLoadDialoging == null || !AddAndConfigWiFiActivity.this.mLoadDialoging.isShowing() || AddAndConfigWiFiActivity.this.isFinishing()) {
                    return;
                }
                AddAndConfigWiFiActivity.this.dismissLoadDialog();
                AddAndConfigWiFiActivity.this.DoneGoToMain();
            }
        }, 40000L);
        showLoadDialog(getString(R.string.adding_please_later), false, false);
        goToAdd(this.str_nick, this.str_uid, this.str_username, this.str_password);
    }

    public void DoneGoToMain() {
        dismissLoadDialog();
        for (MyCamera myCamera : HiDataValue.CameraList) {
            if (myCamera.getUid().equals(this.mCamera.getUid())) {
                myCamera.setPassword(this.mCamera.getPassword());
                myCamera.updateInDatabase(this);
                myCamera.disconnect(1);
                Intent intent = new Intent();
                intent.putExtra("mIsAdd", true);
                intent.setAction(HiDataValue.ACTION_CAMERA_INIT_END);
                sendBroadcast(intent);
                this.mHandler = null;
                startActivity(new Intent(this, (Class<?>) MainActivity.class));
                MyToast.showToast(this, getString(R.string.add_success));
                return;
            }
        }
        MyToast.showToast(this, getString(R.string.add_success));
        this.mCamera.setCameraLevel(1);
        this.mCamera.setNeedUpServer(true);
        this.mCamera.saveInDatabase(this);
        this.mCamera.saveInCameraList();
        MyCamera myCamera2 = this.mCamera;
        new NVRListBean(myCamera2, 0, HiTools.isNVRDev(myCamera2.getUid())).saveInCameraList();
        EventBus.getDefault().post("disconnectCamera");
        Intent intent2 = new Intent();
        intent2.setAction(HiDataValue.ACTION_CAMERA_INIT_END);
        sendBroadcast(intent2);
        Intent intent3 = new Intent(this, (Class<?>) TakeNameToCamActivity.class);
        intent3.putExtra(HiDataValue.EXTRAS_KEY_UID, this.mCamera.getUid());
        startActivity(intent3);
    }

    private void goToAdd(String str, String str2, String str3, String str4) {
        this.mCamera = 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.mCamera.SetDecodeViVoVideoType(0);
        } else {
            this.mCamera.SetDecodeViVoVideoType(1);
        }
        this.mCamera.isFirstAdd = true;
        this.mCamera.registerIOSessionListener(this);
        if (this.isAP) {
            String currentWifiSSID = WifiUtils.getCurrentWifiSSID(this, true);
            HiLog.e("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))) {
                HiLog.e("connectSsid: " + currentWifiSSID + "---mSsid: " + this.mSsid);
            } else {
                HiLog.e("connectSsid: " + currentWifiSSID + "---mSsid: " + this.mSsid);
                if (!HiDataValue.mHi_PhoneSys_Q) {
                    if (!currentWifiSSID.equals(this.mSsid) && !TextUtils.isEmpty(this.mSsid)) {
                        this.miswificonnecttimeout = false;
                        HiLog.e("connectSsid: " + currentWifiSSID + "---mSsid: " + this.mSsid);
                        changePhoneWifiToAP();
                    }
                } else {
                    HiLog.e("" + HiDataValue.mHi_wifiConnect_Q);
                    HiDataValue.mwifiConnect_QError = SharePreUtils.getBoolean(HiDataValue.CACHE, this, "wifiConnect_QError");
                    if (HiDataValue.mwifiConnect_QNorError || HiDataValue.mwifiConnect_QError) {
                        dismissjuHuaDialog();
                        dismissLoadDialog();
                        dealWithTimeOut();
                    } else if (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);
                        }
                    }
                }
            }
        }
        this.mCamera.connect();
    }

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

            @Override
            public void onTick(long j) {
                AddAndConfigWiFiActivity.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) AddAndConfigWiFiActivity.this.getSystemService("connectivity")).getNetworkInfo(1);
            if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) {
                WifiInfo connectionInfo = ((WifiManager) AddAndConfigWiFiActivity.this.getApplicationContext().getSystemService("wifi")).getConnectionInfo();
                HiLogcatUtil.i("AddAndConfigWiFiActivity: onReceive  " + connectionInfo.getSSID());
                if (TextUtils.isEmpty(AddAndConfigWiFiActivity.this.mSsid) || !connectionInfo.getSSID().contains(AddAndConfigWiFiActivity.this.mSsid) || AddAndConfigWiFiActivity.this.miswificonnecttimeout || AddAndConfigWiFiActivity.this.mHandler == null) {
                    return;
                }
                AddAndConfigWiFiActivity.this.mHandler.sendEmptyMessage(100);
            }
        }
    }

    @Override
    public void receiveSessionState(HiCamera hiCamera, int i) {
        if (hiCamera != this.mCamera || 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.mCamera || 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);
    }

    @Override
    public void onResume() {
        super.onResume();
        this.misADD_Q = false;
        HiLog.e("");
    }

    @Override
    public void onPause() {
        super.onPause();
        HiLog.e("" + this.misADD_Q);
        HiLog.e("");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        HiLog.e("");
        MyCamera myCamera = this.mCamera;
        if (myCamera != null) {
            myCamera.unregisterIOSessionListener(this);
        }
        Handler handler = this.mHandler;
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
        Handler handler2 = this.mLoadHandler;
        if (handler2 != null) {
            handler2.removeCallbacksAndMessages(null);
        }
        this.mHandler = null;
        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;
        }
    }

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

    public void handIOCTRLSuccess(Message message) {
        int i;
        boolean z;
        byte[] byteArray = message.getData().getByteArray("data");
        int i2 = message.arg1;
        int i3 = 0;
        if (i2 == 16761) {
            this.mCamera.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.mCamera.getUid() + "xcircle", f);
            SharePreUtils.putFloat(HiDataValue.CACHE, this, this.mCamera.getUid() + "ycircle", f2);
            SharePreUtils.putFloat(HiDataValue.CACHE, this, this.mCamera.getUid() + "rcircle", f3);
            SharePreUtils.putInt(Constant.INSTALLMODE, this, this.mCamera.getUid(), hi_p2p_dev_fish.mold);
            UninstallDevTypeKeep.putValue(this, this.mCamera.getUid(), Constant.X, f + "");
            UninstallDevTypeKeep.putValue(this, this.mCamera.getUid(), Constant.Y, f2 + "");
            UninstallDevTypeKeep.putValue(this, this.mCamera.getUid(), Constant.R, f3 + "");
            UninstallDevTypeKeep.putValue(this, this.mCamera.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.mCamera.isWallMounted = true;
                SharePreUtils.putBoolean(HiDataValue.CACHE, this, this.mCamera.getUid() + Constant.ISWALLMOUNTED, true);
                UninstallDevTypeKeep.putValue(this, this.mCamera.getUid(), Constant.ISWALLMOUNTED, this.mCamera.isWallMounted + "");
            } else {
                this.mCamera.isWallMounted = false;
                SharePreUtils.putBoolean(HiDataValue.CACHE, this, this.mCamera.getUid() + Constant.ISWALLMOUNTED, false);
                UninstallDevTypeKeep.putValue(this, this.mCamera.getUid(), Constant.ISFISHEYE, this.mCamera.getmold() + "");
            }
            this.mCamera.putFishModType(hi_p2p_dev_fish.type);
            return;
        }
        if (i2 == 16762) {
            if (TextUtils.isEmpty(new String(new HiChipDefines.HI_P2P_S_WIFI_PARAM_NEWPWD255_EXT1(byteArray).strSSID).trim())) {
                if (this.mCamera.appGetCommandFunction(HiChipDefines.HI_P2P_GET_WIFI_LIST_EXT256)) {
                    this.mCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_WIFI_LIST_EXT256, new byte[0]);
                    return;
                } else {
                    this.mCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_WIFI_LIST, new byte[0]);
                    return;
                }
            }
            DoneGoToMain();
            return;
        }
        String str = "==mType==";
        if (i2 == 16764) {
            int Hi_GetUseWifi = Hi_GetUseWifi(this, "1");
            HiChipDefines.HI_P2P_S_WIFI_LIST_EXT hi_p2p_s_wifi_list_ext = new HiChipDefines.HI_P2P_S_WIFI_LIST_EXT(byteArray);
            int i4 = hi_p2p_s_wifi_list_ext.u32Num;
            int i5 = hi_p2p_s_wifi_list_ext.flag;
            boolean z2 = false;
            while (i4 > 0) {
                HiChipDefines.SWifiAp_Ext sWifiAp_Ext = new HiChipDefines.SWifiAp_Ext(byteArray, ((hi_p2p_s_wifi_list_ext.u32Num - i4) * 68) + 8);
                String encoding = HiTools.getEncoding(sWifiAp_Ext.strSSID);
                HiChipDefines.HI_P2P_S_WIFI_LIST_EXT hi_p2p_s_wifi_list_ext2 = hi_p2p_s_wifi_list_ext;
                StringBuilder sb = new StringBuilder();
                sb.append(str);
                String str2 = str;
                sb.append((int) sWifiAp_Ext.EncType);
                sb.append(Constants.COLON_SEPARATOR);
                sb.append(encoding);
                sb.append(Constants.COLON_SEPARATOR);
                sb.append(this.mCurrentPhoneWiFi);
                sb.append("::");
                sb.append(i4);
                HiLog.e(sb.toString());
                if (Hi_GetUseWifi == -2 || Hi_GetUseWifi == -3) {
                    if (encoding.trim().equals(this.mCurrentPhoneWiFi)) {
                        this.myEncType = sWifiAp_Ext.EncType;
                        HiLog.e("==mType-EXT256==" + this.myEncType + Constants.COLON_SEPARATOR + encoding + Constants.COLON_SEPARATOR + this.mCurrentPhoneWiFi);
                        i = -1;
                        i4 = -1;
                        z2 = true;
                    }
                    i = -1;
                } else {
                    if (Hi_GetUseWifi(this, encoding) == 1) {
                        this.myEncType = sWifiAp_Ext.EncType;
                        HiLog.e("==mType-EXT256==" + this.myEncType + Constants.COLON_SEPARATOR + encoding + Constants.COLON_SEPARATOR + this.mCurrentPhoneWiFi);
                        this.mCurrentPhoneWiFi = encoding;
                        i = -1;
                        i4 = -1;
                        z2 = true;
                    }
                    i = -1;
                }
                i4 += i;
                hi_p2p_s_wifi_list_ext = hi_p2p_s_wifi_list_ext2;
                str = str2;
            }
            if (i5 == 1 || z2) {
                dismissLoadDialog();
                if (byteArray.length >= 72) {
                    Handler handler = this.mLoadHandler;
                    if (handler != null) {
                        handler.removeCallbacksAndMessages(null);
                    }
                    Handler handler2 = this.mHandler;
                    if (handler2 != null) {
                        handler2.removeCallbacksAndMessages(null);
                    }
                    this.mHandler = null;
                    Intent intent = new Intent(this, (Class<?>) ConfigWiFiToDeviceTwoActivity.class);
                    HiLog.e(":" + this.mCamera.getUid() + Constants.COLON_SEPARATOR + this.mCurrentPhoneWiFi + Constants.COLON_SEPARATOR + this.mCamera.getPassword() + ":::" + z2);
                    intent.putExtra(HiDataValue.EXTRAS_KEY_UID, this.mCamera.getUid());
                    intent.putExtra("nick", this.mCamera.getNikeName());
                    intent.putExtra("username", this.mCamera.getUsername());
                    intent.putExtra("password", this.mCamera.getPassword());
                    intent.putExtra("isAP", this.isAP);
                    intent.putExtra("ssid", this.mSsid);
                    intent.putExtra("myEncType", this.myEncType);
                    intent.putExtra("type", 31);
                    intent.putExtra("mCurrentPhoneWiFi", this.mCurrentPhoneWiFi);
                    intent.putExtra("isSupportFullChar", this.isSupportFullChar);
                    startActivity(intent);
                    EventBus.getDefault().post("disconnectCamera");
                    finish();
                    return;
                }
                DoneGoToMain();
                return;
            }
            return;
        }
        if (i2 == 61445) {
            this.mCamera.appSetCommandFunction(new ConstantCommand.HI_P2P_FUNCTION_LITE(byteArray));
            return;
        }
        switch (i2) {
            case HiChipDefines.HI_P2P_GET_WIFI_PARAM:
                if (TextUtils.isEmpty(new String(new HiChipDefines.HI_P2P_S_WIFI_PARAM(byteArray).strSSID).trim())) {
                    if (this.mCamera.appGetCommandFunction(HiChipDefines.HI_P2P_GET_WIFI_LIST_EXT256)) {
                        this.mCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_WIFI_LIST_EXT256, new byte[0]);
                        return;
                    } else {
                        this.mCamera.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();
                int Hi_GetUseWifi2 = Hi_GetUseWifi(this, "1");
                if (byteArray != null && byteArray.length > 0) {
                    int byteArrayToInt_Little = Packet.byteArrayToInt_Little(byteArray, 0);
                    int totalSize = HiChipDefines.SWifiAp.getTotalSize();
                    if (byteArrayToInt_Little > 0 && byteArray.length >= 40) {
                        int i6 = 0;
                        while (i6 < byteArrayToInt_Little) {
                            byte[] bArr = new byte[32];
                            int i7 = (i6 * totalSize) + 4;
                            System.arraycopy(byteArray, i7, bArr, i3, 32);
                            byte b = byteArray[i7 + 33];
                            String encoding2 = HiTools.getEncoding(bArr);
                            HiLog.e("==mType==" + ((int) b) + Constants.COLON_SEPARATOR + encoding2 + Constants.COLON_SEPARATOR + this.mCurrentPhoneWiFi + ":::" + byteArrayToInt_Little);
                            if (Hi_GetUseWifi2 == -2 || Hi_GetUseWifi2 == -3) {
                                if (encoding2.trim().equals(this.mCurrentPhoneWiFi)) {
                                    this.myEncType = b;
                                    HiLog.e("==mType==" + this.myEncType + Constants.COLON_SEPARATOR + encoding2 + Constants.COLON_SEPARATOR + this.mCurrentPhoneWiFi);
                                    z = true;
                                } else {
                                    i6++;
                                    i3 = 0;
                                }
                            } else if (Hi_GetUseWifi(this, encoding2) == 1) {
                                this.myEncType = b;
                                HiLog.e("==mType==" + this.myEncType + Constants.COLON_SEPARATOR + encoding2 + Constants.COLON_SEPARATOR + this.mCurrentPhoneWiFi);
                                this.mCurrentPhoneWiFi = encoding2;
                                z = true;
                            } else {
                                i6++;
                                i3 = 0;
                            }
                            if (byteArray.length < 40) {
                                Handler handler3 = this.mLoadHandler;
                                if (handler3 != null) {
                                    handler3.removeCallbacksAndMessages(null);
                                }
                                Handler handler4 = this.mHandler;
                                if (handler4 != null) {
                                    handler4.removeCallbacksAndMessages(null);
                                }
                                this.mHandler = null;
                                Intent intent2 = new Intent(this, (Class<?>) ConfigWiFiToDeviceTwoActivity.class);
                                HiLog.e(":" + this.mCamera.getUid() + Constants.COLON_SEPARATOR + this.mCurrentPhoneWiFi + Constants.COLON_SEPARATOR + this.mCamera.getPassword() + ":::" + z);
                                intent2.putExtra(HiDataValue.EXTRAS_KEY_UID, this.mCamera.getUid());
                                intent2.putExtra("nick", this.mCamera.getNikeName());
                                intent2.putExtra("username", this.mCamera.getUsername());
                                intent2.putExtra("password", this.mCamera.getPassword());
                                intent2.putExtra("isAP", this.isAP);
                                intent2.putExtra("ssid", this.mSsid);
                                intent2.putExtra("myEncType", this.myEncType);
                                intent2.putExtra("type", 31);
                                intent2.putExtra("mCurrentPhoneWiFi", this.mCurrentPhoneWiFi);
                                intent2.putExtra("isSupportFullChar", this.isSupportFullChar);
                                startActivity(intent2);
                                EventBus.getDefault().post("disconnectCamera");
                                finish();
                                return;
                            }
                            DoneGoToMain();
                            return;
                        }
                    }
                }
                z = false;
                if (byteArray.length < 40) {
                }
                break;
            default:
                return;
        }
    }

    public void handSessionState(Message message) {
        int i = message.arg1;
        if (i == 0) {
            this.mCamera.connect();
            return;
        }
        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;
        }
        this.isSupportFullChar = this.mCamera.appGetCommandFunction(HiChipDefines.HI_P2P_GET_CHAR);
        if (this.mCamera.appGetCommandFunction(HiChipDefines.HI_P2P_GET_WIFI_PARAM_NEWPWD255_EXT1)) {
            this.mCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_WIFI_PARAM_NEWPWD255_EXT1, new byte[0]);
        } else {
            this.mCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_WIFI_PARAM, null);
        }
    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void disconnectCamera(String str) {
        if (TextUtils.isEmpty(str) || this.mCamera == null || !str.equals("disconnectCamera")) {
            return;
        }
        this.mCamera.disconnect(1);
        Log.i("tedu", this.mCamera.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) {
                AddAndConfigWiFiActivity.this.lambda$dealWithTimeOut$1$AddAndConfigWiFiActivity(dialog, view);
            }
        });
        inflate.findViewById(R.id.tv_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View view) {
                AddAndConfigWiFiActivity.this.lambda$dealWithTimeOut$2$AddAndConfigWiFiActivity(dialog, view);
            }
        });
        dialog.setCancelable(false);
        dialog.addContentView(inflate, new ActionBar.LayoutParams(-1, -2));
        dialog.getWindow().setAttributes(attributes);
        dialog.show();
    }

    public void lambda$dealWithTimeOut$1$AddAndConfigWiFiActivity(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.mCamera;
            if (myCamera != null) {
                myCamera.disconnect(1);
            }
        }
        finish();
    }

    public void lambda$dealWithTimeOut$2$AddAndConfigWiFiActivity(Dialog dialog, View view) {
        dialog.dismiss();
        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.mCamera;
            if (myCamera != null) {
                myCamera.disconnect(1);
            }
        }
        Intent intent = new Intent("android.settings.WIFI_SETTINGS");
        intent.setFlags(268435456);
        startActivity(intent);
    }

    public void dealWithConfigFail() {
        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 textView = (TextView) inflate.findViewById(R.id.tv_content);
        ((TextView) inflate.findViewById(R.id.tv_2)).setText(getString(R.string.try_again));
        textView.setText(getString(R.string.sendtodevfail));
        inflate.findViewById(R.id.tv_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View view) {
                AddAndConfigWiFiActivity.this.lambda$dealWithConfigFail$3$AddAndConfigWiFiActivity(dialog, view);
            }
        });
        inflate.findViewById(R.id.tv_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View view) {
                AddAndConfigWiFiActivity.this.lambda$dealWithConfigFail$4$AddAndConfigWiFiActivity(dialog, view);
            }
        });
        dialog.setCancelable(false);
        dialog.addContentView(inflate, new ActionBar.LayoutParams(-1, -2));
        dialog.getWindow().setAttributes(attributes);
        dialog.show();
    }

    public void lambda$dealWithConfigFail$3$AddAndConfigWiFiActivity(Dialog dialog, View view) {
        dialog.dismiss();
        this.mHandler = null;
        finish();
    }

    public void lambda$dealWithConfigFail$4$AddAndConfigWiFiActivity(Dialog dialog, View view) {
        dialog.dismiss();
        if (this.mReceiver == null) {
            registerRece();
        }
        this.myEncType = -1;
        HiLog.e("" + HiDataValue.mHi_wifiConnect_Q + "::");
        dismissjuHuaDialog();
        handButComPlete();
    }

    private int Hi_GetUseWifi(Context context, String str) {
        if (str == null) {
            return -1;
        }
        Set<String> stringSet = SharePreUtils.getStringSet(HiDataValue.CACHE, context, HiDataValue.use_SSIDList2);
        if (stringSet == null) {
            return -3;
        }
        if (stringSet.size() <= 0) {
            return -2;
        }
        String[] strArr = (String[]) stringSet.toArray(new String[stringSet.size()]);
        Arrays.sort(strArr);
        if (stringSet.size() <= 2) {
            return -3;
        }
        HiLog.e(str + "::" + strArr[stringSet.size() - 3].substring(3));
        return str.equals(strArr[stringSet.size() - 3].substring(3)) ? 1 : -3;
    }
}