SystemSettingActivity.java 源代码


package activity.setting;

import activity.setting.SystemSettingActivity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import base.HiActivity;
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.HiDataValue;
import common.TitleView;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import main.MainActivity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.xmlbeans.XmlErrorCodes;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import utils.DialogUtilsCamHiPro;
import utils.HiTools;
import utils.MyToast;

public class SystemSettingActivity extends HiActivity implements ICameraIOSessionCallback {
    private static final int GET_UPDATE_VERSION_DATA = 65536;
    private static final int GET_UPDATE_VERSION_NUM = 39321;
    private static final int UPDATA_STATE_CHECKING = 1;
    private static final int UPDATA_STATE_NONE = 0;
    private static long updateTime;
    private MyCamera mCamera;
    private ProgressDialog progressDialog;
    private UpdateInfo updateInfo;
    private int updateStatus = 0;
    private String redirectAddr = null;
    private boolean send = false;
    private List<UpdateInfo> mListUpdataInfo = new ArrayList();
    boolean isUpdate = false;
    private Handler handler = new AnonymousClass4();

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

    @Override
    protected void init(Bundle bundle) {
        String stringExtra = getIntent().getStringExtra(HiDataValue.EXTRAS_KEY_UID);
        Iterator<MyCamera> it = HiDataValue.CameraList.iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            MyCamera next = it.next();
            if (stringExtra.equals(next.getUid())) {
                this.mCamera = next;
                break;
            }
        }
        if (HiTools.cameraWhetherNull(this.mCamera, this)) {
            return;
        }
        initView();
    }

    @Override
    public void onResume() {
        super.onResume();
        this.mCamera.registerIOSessionListener(this);
    }

    private void initView() {
        TitleView titleView = (TitleView) findViewById(R.id.title_top);
        titleView.setTitle(getResources().getString(R.string.initial_set));
        titleView.setButton(0);
        titleView.setNavigationBarButtonListener(new TitleView.NavigationBarButtonListener() {
            @Override
            public void OnNavigationButtonClick(int i) {
                if (i != 0) {
                    return;
                }
                SystemSettingActivity.this.finish();
            }
        });
        ((TextView) findViewById(R.id.tv_restart_camera)).setOnClickListener(new AnonymousClass2());
        ((TextView) findViewById(R.id.tv_restore_factory_settings)).setOnClickListener(new AnonymousClass3());
        if (this.mCamera.appGetCommandFunction(HiChipDefines.HI_P2P_GET_DEV_INFO_EXT)) {
            this.mCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_DEV_INFO_EXT, new byte[0]);
        } else {
            this.mCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_DEV_INFO, new byte[0]);
            this.mCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_VENDOR_INFO, new byte[0]);
        }
    }

    public class AnonymousClass2 implements View.OnClickListener {
        AnonymousClass2() {
        }

        @Override
        public void onClick(View view) {
            new DialogUtilsCamHiPro(SystemSettingActivity.this).title(SystemSettingActivity.this.getString(R.string.tip_reminder)).message(SystemSettingActivity.this.getString(R.string.tips_reboot_camera)).cancelText(SystemSettingActivity.this.getString(R.string.cancel)).sureText(SystemSettingActivity.this.getString(R.string.sure)).setSureOnClickListener(new View.OnClickListener() {
                @Override
                public final void onClick(View view2) {
                    SystemSettingActivity.AnonymousClass2.this.lambda$onClick$0$SystemSettingActivity$2(view2);
                }
            }).build().show();
        }

        public void lambda$onClick$0$SystemSettingActivity$2(View view) {
            SystemSettingActivity.this.showjuHuaDialog();
            SystemSettingActivity.this.mCamera.setReconnectTimes(30);
            SystemSettingActivity.this.mCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_REBOOT, new byte[0]);
            SystemSettingActivity.this.mCamera.isSystemState = 1;
        }
    }

    public class AnonymousClass3 implements View.OnClickListener {
        AnonymousClass3() {
        }

        @Override
        public void onClick(View view) {
            new DialogUtilsCamHiPro(SystemSettingActivity.this).title(SystemSettingActivity.this.getString(R.string.tip_reminder)).message(SystemSettingActivity.this.getString(R.string.tips_restore_factory_settings)).cancelText(SystemSettingActivity.this.getString(R.string.cancel)).sureText(SystemSettingActivity.this.getString(R.string.sure)).setSureOnClickListener(new View.OnClickListener() {
                @Override
                public final void onClick(View view2) {
                    SystemSettingActivity.AnonymousClass3.this.lambda$onClick$0$SystemSettingActivity$3(view2);
                }
            }).build().show();
        }

        public void lambda$onClick$0$SystemSettingActivity$3(View view) {
            SystemSettingActivity.this.showjuHuaDialog();
            SystemSettingActivity.this.mCamera.setReconnectTimes(20);
            SystemSettingActivity.this.mCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_RESET, new byte[0]);
            SystemSettingActivity.this.mCamera.isSystemState = 2;
        }
    }

    public class UpdateInfo {
        String url;
        String ver;

        public UpdateInfo(String str, String str2) {
            this.url = str;
            this.ver = str2;
        }
    }

    private class ThreadHttpResqust extends Thread {
        public ThreadHttpResqust() {
        }

        @Override
        public void run() {
            try {
                HttpResponse execute = new DefaultHttpClient().execute(new HttpGet("http://upgrade.hichip.net/goke_update.html"));
                if (execute.getStatusLine().getStatusCode() == 200) {
                    String entityUtils = EntityUtils.toString(execute.getEntity());
                    HiLog.v("result:" + entityUtils);
                    try {
                        JSONArray jSONArray = new JSONObject(entityUtils).getJSONArray(XmlErrorCodes.LIST);
                        for (int i = 0; i < jSONArray.length(); i++) {
                            JSONObject jSONObject = jSONArray.getJSONObject(i);
                            String string = jSONObject.getString("url");
                            String string2 = jSONObject.getString("ver");
                            SystemSettingActivity.this.mListUpdataInfo.add(new UpdateInfo(string, string2));
                            HiLog.v("url:" + string + "     ver:" + string2);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    if (SystemSettingActivity.this.mListUpdataInfo.size() > 0) {
                        Message message = new Message();
                        message.what = SystemSettingActivity.GET_UPDATE_VERSION_NUM;
                        message.arg1 = 1;
                        SystemSettingActivity.this.handler.sendMessage(message);
                        return;
                    }
                }
                HiLog.v("result: failed");
                Message message2 = new Message();
                message2.what = SystemSettingActivity.GET_UPDATE_VERSION_NUM;
                message2.arg1 = 0;
                SystemSettingActivity.this.handler.sendMessage(message2);
            } catch (Exception e2) {
                HiLog.v("Exception: " + e2);
                Message message3 = new Message();
                message3.what = SystemSettingActivity.GET_UPDATE_VERSION_NUM;
                message3.arg1 = 0;
                SystemSettingActivity.this.handler.sendMessage(message3);
            }
        }
    }

    public class ThreadCheckRedirect extends Thread {
        private ThreadCheckRedirect() {
        }

        @Override
        public void run() {
            Message message;
            int i;
            Socket socket;
            String readLine;
            Socket socket2 = null;
            String str = null;
            Socket socket3 = null;
            Socket socket4 = null;
            try {
                try {
                    UpdateInfo unused = SystemSettingActivity.this.updateInfo;
                    System.out.println("updateInfo:" + SystemSettingActivity.this.updateInfo);
                    System.out.println("updateInfo.url:" + SystemSettingActivity.this.updateInfo.url);
                    i = 80;
                    socket = new Socket();
                } catch (Throwable th) {
                    th = th;
                }
            } catch (UnknownHostException e) {
                e = e;
            } catch (IOException e2) {
                e = e2;
            }
            try {
                socket.setReuseAddress(true);
                Matcher matcher = Pattern.compile("(?<=//|)((\\w)+\\.)+\\w+(:\\d{0,5})?").matcher(SystemSettingActivity.this.updateInfo.url);
                if (matcher.find()) {
                    str = matcher.group();
                    System.out.println("host2:" + str);
                }
                if (str.contains(Constants.COLON_SEPARATOR)) {
                    String[] split = str.split(Constants.COLON_SEPARATOR);
                    String str2 = split[0];
                    int parseInt = Integer.parseInt(split[1]);
                    System.out.println("---00port:      " + split[0]);
                    System.out.println("---port:    " + parseInt);
                    str = str2;
                    i = parseInt;
                }
                socket.connect(new InetSocketAddress(str, i));
                DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
                String str3 = (((((("GET /" + SystemSettingActivity.this.updateInfo.ver + ".exe HTTP/1.1\r\n") + "Accept: */*\r\n") + "Accept-Language: zh-cn\r\n") + "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n") + "Host: " + str + "\r\n") + "Connection: Keep-Alive\r\n") + "\r\n";
                System.out.println("sendhead:" + str3);
                dataOutputStream.write(str3.getBytes());
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                while (true) {
                    readLine = bufferedReader.readLine();
                    System.out.println("in.readLine:" + readLine);
                    if (readLine.contains("HTTP/")) {
                        if (!readLine.contains("302") && !readLine.contains("301")) {
                            break;
                        } else {
                            System.out.println("------------ 301 302 Found----------------");
                        }
                    }
                    if (readLine.contains("Location:")) {
                        String trim = readLine.substring(9).trim();
                        System.out.println("1------------ newhttp----------------:" + trim);
                        SystemSettingActivity systemSettingActivity = SystemSettingActivity.this;
                        readLine = readLine.substring(9).trim();
                        systemSettingActivity.redirectAddr = readLine;
                        break;
                    }
                }
                dataOutputStream.close();
                bufferedReader.close();
                try {
                    socket.close();
                } catch (IOException unused2) {
                }
                message = new Message();
                socket2 = readLine;
            } catch (UnknownHostException e3) {
                e = e3;
                socket3 = socket;
                e.printStackTrace();
                if (socket3 != null) {
                    try {
                        socket3.close();
                    } catch (IOException unused3) {
                    }
                }
                message = new Message();
                socket2 = socket3;
                message.what = 65536;
                message.arg1 = 1;
                SystemSettingActivity.this.handler.sendMessage(message);
            } catch (IOException e4) {
                e = e4;
                socket4 = socket;
                e.printStackTrace();
                if (socket4 != null) {
                    try {
                        socket4.close();
                    } catch (IOException unused4) {
                    }
                }
                message = new Message();
                socket2 = socket4;
                message.what = 65536;
                message.arg1 = 1;
                SystemSettingActivity.this.handler.sendMessage(message);
            } catch (Throwable th2) {
                th = th2;
                socket2 = socket;
                if (socket2 != null) {
                    try {
                        socket2.close();
                    } catch (IOException unused5) {
                    }
                }
                Message message2 = new Message();
                message2.what = 65536;
                message2.arg1 = 1;
                SystemSettingActivity.this.handler.sendMessage(message2);
                throw th;
            }
            message.what = 65536;
            message.arg1 = 1;
            SystemSettingActivity.this.handler.sendMessage(message);
        }
    }

    private void checkUpdate() {
        new ThreadHttpResqust().start();
        this.updateStatus = 1;
    }

    public class AnonymousClass4 extends Handler {
        AnonymousClass4() {
        }

        @Override
        public void handleMessage(Message message) {
            switch (message.what) {
                case HiDataValue.HANDLE_MESSAGE_SESSION_STATE:
                    if (message.arg1 != 4) {
                        return;
                    }
                    SystemSettingActivity.this.mCamera.setReconnectTimes(10);
                    SystemSettingActivity.this.dismissjuHuaDialog();
                    SystemSettingActivity.this.mCamera.isSystemState = 0;
                    SystemSettingActivity.this.startActivity(new Intent(SystemSettingActivity.this, (Class<?>) MainActivity.class));
                    return;
                case HiChipDefines.HI_P2P_GET_DEV_INFO_EXT:
                    if (message.arg2 == 0) {
                        TextUtils.isEmpty(Packet.getString(new HiChipDefines.HI_P2P_GET_DEV_INFO_EXT(message.getData().getByteArray("data")).aszSystemSoftVersion));
                        return;
                    }
                    return;
                case HiChipDefines.HI_P2P_SET_DOWNLOAD:
                    SystemSettingActivity.this.mCamera.isSystemState = 3;
                    return;
                case HiChipDefines.HI_P2P_GET_DEV_INFO:
                    if (message.arg2 == 0) {
                        TextUtils.isEmpty(Packet.getString(new HiChipDefines.HI_P2P_S_DEV_INFO(message.getData().getByteArray("data")).strSoftVer));
                        return;
                    }
                    return;
                case HiChipDefines.HI_P2P_SET_REBOOT:
                    SystemSettingActivity.this.mCamera.isSystemState = 1;
                    SystemSettingActivity.this.dismissjuHuaDialog();
                    SystemSettingActivity.this.handler.postDelayed(new Runnable() {
                        @Override
                        public final void run() {
                            SystemSettingActivity.AnonymousClass4.this.lambda$handleMessage$0$SystemSettingActivity$4();
                        }
                    }, 800L);
                    return;
                case HiChipDefines.HI_P2P_SET_RESET:
                    SystemSettingActivity.this.mCamera.isSystemState = 2;
                    SystemSettingActivity.this.mCamera.setUsername("admin");
                    SystemSettingActivity.this.mCamera.setPassword("admin");
                    SystemSettingActivity.this.mCamera.updateInDatabase(SystemSettingActivity.this);
                    SystemSettingActivity.this.dismissjuHuaDialog();
                    SystemSettingActivity.this.handler.postDelayed(new Runnable() {
                        @Override
                        public final void run() {
                            SystemSettingActivity.AnonymousClass4.this.lambda$handleMessage$1$SystemSettingActivity$4();
                        }
                    }, 800L);
                    return;
                case SystemSettingActivity.GET_UPDATE_VERSION_NUM:
                    if (message.arg1 != 1) {
                        SystemSettingActivity.this.updateStatus = 0;
                        SystemSettingActivity systemSettingActivity = SystemSettingActivity.this;
                        MyToast.showToast(systemSettingActivity, systemSettingActivity.getString(R.string.tips_update_system_failed));
                        SystemSettingActivity.this.dismissProgressDialog();
                        return;
                    }
                    SystemSettingActivity.this.updateInfo = (UpdateInfo) message.obj;
                    SystemSettingActivity.this.send = true;
                    if (SystemSettingActivity.this.mCamera != null) {
                        SystemSettingActivity.this.mCamera.registerIOSessionListener(SystemSettingActivity.this);
                    }
                    handleDeviceInfo();
                    return;
                case 65536:
                    SystemSettingActivity.this.updateStatus = 0;
                    SystemSettingActivity.this.showDialogOld();
                    return;
                default:
                    return;
            }
        }

        public void lambda$handleMessage$0$SystemSettingActivity$4() {
            SystemSettingActivity.this.startActivity(new Intent(SystemSettingActivity.this, (Class<?>) MainActivity.class));
        }

        public void lambda$handleMessage$1$SystemSettingActivity$4() {
            SystemSettingActivity.this.startActivity(new Intent(SystemSettingActivity.this, (Class<?>) MainActivity.class));
        }

        private void handleDeviceInfo() {
            String string = SystemSettingActivity.getString(SystemSettingActivity.this.mCamera.getDeciveInfo().aszSystemSoftVersion);
            for (UpdateInfo updateInfo : SystemSettingActivity.this.mListUpdataInfo) {
                HiLog.v("version:" + string);
                String[] split = updateInfo.ver.split("\\.");
                String[] split2 = string.split("\\.");
                if (split.length == split2.length && split2.length == 5) {
                    int i = 0;
                    while (true) {
                        if (i >= split.length) {
                            break;
                        }
                        if (i == split.length - 1) {
                            String[] split3 = split[i].split(Constants.ACCEPT_TIME_SEPARATOR_SERVER);
                            String[] split4 = split2[i].split(Constants.ACCEPT_TIME_SEPARATOR_SERVER);
                            HiLog.v("last_new_array:" + split3.length + "  last_old_array:" + split4.length);
                            int parseInt = split3.length >= 1 ? Integer.parseInt(split3[0]) : 0;
                            int parseInt2 = split4.length >= 1 ? Integer.parseInt(split4[0]) : 0;
                            HiLog.v("newi:" + parseInt + "  oldi:" + parseInt2);
                            if (parseInt > parseInt2) {
                                SystemSettingActivity.this.updateInfo = updateInfo;
                                SystemSettingActivity.this.isUpdate = true;
                            }
                        } else if (!split2[i].equals(split[i])) {
                            HiLog.v(i + "b_new:" + split[i]);
                            HiLog.v(i + "b_old:" + split2[i]);
                            SystemSettingActivity.this.isUpdate = false;
                            break;
                        }
                        i++;
                    }
                }
                if (SystemSettingActivity.this.isUpdate) {
                    break;
                }
            }
            if (SystemSettingActivity.this.isUpdate) {
                if (SystemSettingActivity.this.updateInfo == null) {
                    return;
                }
                SystemSettingActivity.this.redirectAddr = SystemSettingActivity.this.updateInfo.url + SystemSettingActivity.this.updateInfo.ver + ".exe";
                new ThreadCheckRedirect().start();
                return;
            }
            SystemSettingActivity.this.showDialogNew();
            SystemSettingActivity.this.updateStatus = 0;
        }
    }

    public void dismissProgressDialog() {
        ProgressDialog progressDialog = this.progressDialog;
        if (progressDialog != null) {
            progressDialog.cancel();
        }
        this.progressDialog = null;
    }

    private void showProgressDialog() {
        ProgressDialog progressDialog = new ProgressDialog(this);
        this.progressDialog = progressDialog;
        progressDialog.setProgressStyle(0);
        this.progressDialog.setCancelable(true);
        this.progressDialog.setCanceledOnTouchOutside(false);
        this.progressDialog.setIcon(R.mipmap.ic_launcher);
        this.progressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
            }
        });
        this.progressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                return false;
            }
        });
        this.progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialogInterface) {
            }
        });
        this.progressDialog.setMessage(getText(R.string.tips_loading));
        this.progressDialog.show();
    }

    public void showDialogNew() {
        dismissProgressDialog();
        this.send = false;
        MyCamera myCamera = this.mCamera;
        if (myCamera != null) {
            myCamera.unregisterIOSessionListener(this);
        }
        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(getText(R.string.tips_warning)).setMessage(getText(R.string.tip_system_update_new)).setPositiveButton(getText(R.string.btn_ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        }).show();
    }

    public void showDialogOld() {
        dismissProgressDialog();
        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(getText(R.string.tips_warning)).setMessage(getText(R.string.tip_system_update_old)).setPositiveButton(getText(R.string.btn_ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (System.currentTimeMillis() - SystemSettingActivity.updateTime <= 180000) {
                    return;
                }
                String str = SystemSettingActivity.this.redirectAddr;
                byte[] bArr = new byte[128];
                byte[] bytes = str.getBytes();
                System.arraycopy(bytes, 0, bArr, 0, bytes.length <= 128 ? bytes.length : 128);
                HiLog.v("dl:" + str);
                long unused = SystemSettingActivity.updateTime = System.currentTimeMillis();
                SystemSettingActivity.this.send = true;
                if (SystemSettingActivity.this.mCamera != null) {
                    SystemSettingActivity.this.mCamera.registerIOSessionListener(SystemSettingActivity.this);
                }
                SystemSettingActivity.this.mCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_DOWNLOAD, HiChipDefines.HI_P2P_S_SET_DOWNLOAD.parseContent(0, bArr));
                SystemSettingActivity.this.mCamera.isSystemState = 3;
                new AlertDialog.Builder(SystemSettingActivity.this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(SystemSettingActivity.this.getText(R.string.tips_warning)).setMessage(SystemSettingActivity.this.getText(R.string.tip_system_update)).setPositiveButton(SystemSettingActivity.this.getText(R.string.btn_ok), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface2, int i2) {
                        SystemSettingActivity.this.startActivity(new Intent(SystemSettingActivity.this, (Class<?>) MainActivity.class));
                    }
                }).show();
            }
        }).setNegativeButton(getText(R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        }).show();
    }

    public static String getString(byte[] bArr) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bArr.length && bArr[i] != 0; i++) {
            sb.append((char) bArr[i]);
        }
        return sb.toString();
    }

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

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

    @Override
    public void onStop() {
        super.onStop();
        if (this.send) {
            this.send = false;
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        MyCamera myCamera = this.mCamera;
        if (myCamera != null) {
            myCamera.unregisterIOSessionListener(this);
        }
    }
}