zhangzengfei
2020-07-30 512cae0fd797fac428f07d9d955f880e9ee353c1
app/src/main/java/com/basic/security/fragment/SettingFragment.java
@@ -1,19 +1,30 @@
package com.basic.security.fragment;
import android.content.Intent;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.basic.security.R;
import com.basic.security.base.BaseFragment;
import com.basic.security.fragment.helper.HomePlayAlarmSound;
import com.basic.security.manager.SettingManager;
import com.basic.security.model.Setting;
import com.basic.security.utils.AudioPlayer;
import com.basic.security.utils.Constants;
import com.basic.security.utils.CrashHandler;
import com.basic.security.utils.SocketClient;
import com.basic.security.utils.ToastUtil;
import com.basic.security.widget.CustomSpinner;
import com.basic.security.widget.SuccessDialog;
import org.androidannotations.annotations.AfterViews;
@@ -23,37 +34,88 @@
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import java.util.Map;
@EFragment(R.layout.fragment_setting)
public class SettingFragment extends BaseFragment {
    @ViewById
    public EditText password;
    @ViewById
    public EditText ip;
    @ViewById
    public EditText title;
    @ViewById
    public EditText port;
    @ViewById
    public Spinner second;
    @ViewById
    public Spinner alarmAudio;
    public CustomSpinner alarmAudio;
    @ViewById
    public Spinner keepTime;
    @ViewById
    public Spinner keepTimeType;
    @ViewById
    public EditText oldPassword;
    @ViewById
    public EditText newPassword;
    @ViewById
    public EditText newPassword2;
    private com.lztek.toolkit.Lztek mLztek;
    @ViewById
    public CheckBox switch_ethernet;
    @ViewById
    public RadioGroup ip_mode;
    @ViewById
    public RadioButton dhcp;
    @ViewById
    public RadioButton staticip;
    @ViewById
    public EditText ip_address;
    @ViewById
    public EditText net_mask;
    @ViewById
    public EditText gate_way;
    @ViewById
    public EditText dns_server;
    @ViewById
    public TextView error;
    public String[] secondArray = new String[]{"5", "10", "20", "30", "40", "50", "60"};
    public static String[] alarmAudioArray = new String[]{"1", "2", "3", "4", "5"};
    public static String[] keepTimeArray = new String[]{"1", "2", "3","4","5","6","7","8","9","10"};
    public static String[] keepTimeTypeArray = new String[]{"周", "月"};
    public MediaPlayer mp;
    boolean notSkipPlay = false;
    boolean saved = false;
    @Background
    public void show() {
        initData(SettingManager.getIpStr(), SettingManager.getSecondStr(), SettingManager.getAlarmAudioStr(), SettingManager.getPortStr());
        saved = false;
        initData(SettingManager.getIpStr(), SettingManager.getSecondStr(),
                SettingManager.getAlarmAudioStr(), SettingManager.getPortStr(), SettingManager.getKeepTime(), SettingManager.getKeepTimeType(), SettingManager.getAppTitle());
    }
    @UiThread
    public void initData(String ipStr, String secondStr, String alarmAudioStr, String portStr) {
    public void initData(String ipStr, String secondStr, String alarmAudioStr, String portStr, String keepTimeStr, String keepTimeTypeStr, String appTitle) {
        if (second != null) {
            second.setSelection(getSelectedIndex(secondArray, secondStr));
        }
        if (alarmAudio != null) {
            notSkipPlay = false;
            alarmAudio.setSelection(getSelectedIndex(alarmAudioArray, alarmAudioStr));
        }
        if (keepTime != null) {
            keepTime.setSelection(getSelectedIndex(keepTimeArray, keepTimeStr));
        }
        if (keepTimeType != null) {
            keepTimeType.setSelection(getSelectedIndex(keepTimeTypeArray, keepTimeTypeStr));
        }
        if (ipStr !=  null) {
            ip.setText(ipStr);
@@ -61,7 +123,19 @@
        if (portStr != null) {
            port.setText(portStr);
        }
        password.setText("");
        if (appTitle != null) {
            title.setText(appTitle);
        }
        oldPassword.setText("");
        newPassword.setText("");
        newPassword2.setText("");
        try {
            if (Constants.NativeLib) {
                refreshAddrInfo(mLztek.getEthEnable());
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    public int getSelectedIndex(String[] array, String value) {
@@ -102,46 +176,247 @@
    @AfterViews
    public void afterViews() {
        second.setAdapter(new ArrayAdapter<>(getContext(), R.layout.spinner_item_small_text, secondArray));
        second.setPopupBackgroundResource(R.drawable.round_border);
        try {
            second.setAdapter(new ArrayAdapter<>(getContext(), R.layout.spinner_item_small_text, secondArray));
            second.setPopupBackgroundResource(R.drawable.round_border);
        alarmAudio.setAdapter(new ArrayAdapter<>(getContext(), R.layout.spinner_item_small_text, alarmAudioArray));
        alarmAudio.setPopupBackgroundResource(R.drawable.round_border);
        alarmAudio.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                final int position1 = position;
                new Thread(){
            keepTime.setAdapter(new ArrayAdapter<>(getContext(), R.layout.spinner_item_small_text, keepTimeArray));
            keepTime.setPopupBackgroundResource(R.drawable.round_border);
            keepTimeType.setAdapter(new ArrayAdapter<>(getContext(), R.layout.spinner_item_small_text, keepTimeTypeArray));
            keepTimeType.setPopupBackgroundResource(R.drawable.round_border);
            alarmAudio.setAdapter(new ArrayAdapter<>(getContext(), R.layout.spinner_item_small_text, alarmAudioArray));
            alarmAudio.setPopupBackgroundResource(R.drawable.round_border);
            alarmAudio.selection = new CustomSpinner.Selection() {
                @Override
                public void selected(int position) {
                    final int position1 = position;
                    new Thread(){
                        @Override
                        public void run() {
                            if (notSkipPlay) {
                                mp = AudioPlayer.play(mp, alarmAudioArray[position1], false);
                            }
                            if (!notSkipPlay) {
                                notSkipPlay = true;
                            }
                        }
                    }.start();
                }
            };
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (Constants.NativeLib) {
                String errorMsg = CrashHandler.getErrorContent();
                System.out.println("errorMsg="+errorMsg);
                mLztek = com.lztek.toolkit.Lztek.create(mainActivity());
                switch_ethernet.setText(switch_ethernet.getText() + " [" + mLztek.getEthMac().toUpperCase() + "]");
                boolean ethEnalbe = mLztek.getEthEnable();
                switch_ethernet.setChecked(ethEnalbe);
                switch_ethernet.setEnabled(ethEnalbe);
                //refreshAddrInfo(ethEnalbe);
                switch_ethernet.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void run() {
                        //AudioPlayer.stop(mp);
                        mp = AudioPlayer.play(mp, alarmAudioArray[position1], false);
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        mLztek.setEthEnable(isChecked);
                        refreshAddrInfo(isChecked);
                    }
                }.start();
                });
                ip_mode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        if (group == ip_mode) {
                            setAddrInput(staticip.isChecked());
                        }
                    }
                });
                ip_address.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (!hasFocus) {
                            String ip = ip_address.getText().toString().trim();
                            if (!ipCheck(ip)) {
                                return;
                            }
                            if (net_mask.getText().toString().trim().length() == 0) {
                                int type = Integer.parseInt(ip.substring(0, ip.indexOf('.')));
                                String mask = 1 <= type && type <= 126? "255.0.0.0" :
                                        (128 <= type && type <= 191? "255.255.0.0" : "255.255.255.0");
                                net_mask.setText(mask);
                            }
                            if (gate_way.getText().toString().trim().length() == 0) {
                                gate_way.setText(ip.substring(0, ip.lastIndexOf('.')) + ".1");
                            }
                            if (dns_server.getText().toString().trim().length() == 0) {
                                dns_server.setText(ip.substring(0, ip.lastIndexOf('.')) + ".1");
                            }
                        }
                    }
                });
                gate_way.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (!hasFocus) {
                            String gateway = gate_way.getText().toString().trim();
                            if (!ipCheck(gateway)) {
                                return;
                            }
                            if (dns_server.getText().toString().trim().length() == 0) {
                                dns_server.setText(gateway);
                            }
                        }
                    }
                });
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
        } catch (Exception e) {
            e.printStackTrace();
        }
            }
        });
    }
    private void refreshAddrInfo(boolean ethEnalbe) {
        if (ethEnalbe) {
            setRadioButton(true);
            com.lztek.toolkit.AddrInfo addrInfo = mLztek.getEthAddrInfo();
            if (null != addrInfo) {
                boolean dhcpv = addrInfo.getIpMode() == com.lztek.toolkit.AddrInfo.DHCP;
                dhcp.setChecked(dhcpv);
                staticip.setChecked(!dhcpv);
                ip_address.setText(addrInfo.getIpAddress());
                net_mask.setText(addrInfo.getNetmask());
                gate_way.setText(addrInfo.getGateway());
                dns_server.setText(addrInfo.getDns());
                setAddrInput(!dhcpv);
            } else {
                setAddrInput(false);
                Toast.makeText(mainActivity(), "Cannot get ethernet info", Toast.LENGTH_LONG).show();
            }
        } else {
            ip_address.setText("");
            net_mask.setText("");
            gate_way.setText("");
            dns_server.setText("");
            setRadioButton(false);
            setAddrInput(false);
        }
        setButton(ethEnalbe);
    }
    private void setRadioButton(boolean enable) {
        ip_mode.setEnabled(enable);
        dhcp.setEnabled(enable);
        staticip.setEnabled(enable);
    }
    private void setButton(boolean enable) {
//        mBtOk.setEnabled(enable);
//        mBtRefresh.setEnabled(enable);
    }
    private void setAddrInput(boolean enable) {
        ip_address.setEnabled(enable);
        net_mask.setEnabled(enable);
        gate_way.setEnabled(enable);
        dns_server.setEnabled(enable);
        ip_address.setFocusable(enable);
        net_mask.setFocusable(enable);
        gate_way.setFocusable(enable);
        dns_server.setFocusable(enable);
        ip_address.setFocusableInTouchMode(enable);
        net_mask.setFocusableInTouchMode(enable);
        gate_way.setFocusableInTouchMode(enable);
        dns_server.setFocusableInTouchMode(enable);
    }
    public static boolean ipCheck(String text) {
        if (text != null && !text.isEmpty()) {
            String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
            if (text.matches(regex)) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }
    private boolean validateDate() {
        String ip = ip_address.getText().toString();
        if (ip.equals("") || !ipCheck(ip)) {
            ip_address.setFocusable(true);
            ip_address.requestFocus();
            return false;
        }
        String net = net_mask.getText().toString();
        if (net.equals("") || !ipCheck(net)) {
            net_mask.setFocusable(true);
            net_mask.requestFocus();
            return false;
        }
        return true;
    }
    private void onBtnConfirm() {
        if (staticip.isChecked()) {
            if (!validateDate()) {
                return;
            }
            String ip = ip_address.getText().toString();
            String mask = net_mask.getText().toString();
            String gate = gate_way.getText().toString();
            String dns = dns_server.getText().toString();
            mLztek.setEthIpAddress(ip, mask, gate, dns);
        } else {
            mLztek.setEthDhcpMode();
        }
    }
    long lastClick = System.currentTimeMillis();
    @Click
    public void save() {
        String secondStr = second.getSelectedItem().toString();
        String alarmAudioStr = alarmAudio.getSelectedItem().toString();
        String keepTimeStr = keepTime.getSelectedItem().toString();
        String keepTimeTypeStr = keepTimeType.getSelectedItem().toString();
        String ipStr = ip.getText().toString().trim();
        String portStr = port.getText().toString().trim();
        String passwordStr = password.getText().toString().trim();
        if (passwordStr.length() == 0) {
            ToastUtil.show("密码不能为空");
            return;
        String appTitle = title.getText().toString().trim();
        String oldPasswordStr = oldPassword.getText().toString().trim();
        String newPasswordStr = newPassword.getText().toString().trim();
        String newPassword2Str = newPassword2.getText().toString().trim();
        String password = SettingManager.oldPassword();
        if (!TextUtils.isEmpty(newPasswordStr) || !TextUtils.isEmpty(newPassword2Str) ) {
            if (oldPasswordStr.length() == 0) {
                ToastUtil.show("密码不能为空");
                return;
            }
            if(!password.equals(oldPasswordStr)) {
                ToastUtil.show("旧密码不正确");
                return;
            }
            if(!newPasswordStr.equals(newPassword2Str)) {
                ToastUtil.show("两次输入的密码不一致");
                return;
            }
            password = newPasswordStr;
        }
        if(!"basic".equals(passwordStr)) {
            ToastUtil.show("密码不正确");
            return;
        }
        if (ipStr.length() == 0) {
            ToastUtil.show("IP不能为空");
            return;
@@ -150,7 +425,27 @@
            ToastUtil.show("端口不能为空");
            return;
        }
        if (SettingManager.saveSetting(secondStr, ipStr, alarmAudioStr,portStr)) {
        if (saved) {
            return;
        }
        saved = true;
        if (Constants.NativeLib) {
            boolean ethEnalbe = mLztek.getEthEnable();
            if (ethEnalbe) {
                onBtnConfirm();
            }
        }
        Map<String, String> setting = SettingManager.findSetting();
        setting.put(Setting.alarmAudio, alarmAudioStr);
        setting.put(Setting.ip, ipStr);
        setting.put(Setting.second, secondStr);
        setting.put(Setting.port, portStr);
        setting.put(Setting.appTitle, appTitle);
        setting.put(Setting.password, password);
        setting.put(Setting.keepTime, keepTimeStr);
        setting.put(Setting.keepTimeType, keepTimeTypeStr);
        if (SettingManager.saveSetting(setting)) {
            mainActivity().fragment_home.setRootUrl();
            SuccessDialog successDialog = new SuccessDialog(mainActivity());
            successDialog.show();
@@ -159,12 +454,30 @@
            HomePlayAlarmSound.shouldRestartSound = true;
            SocketClient.needRestart = true;
        }
//        try {
//            if (Constants.NativeLib) {
//                refreshAddrInfo(mLztek.getEthEnable());
//            }
//        } catch (Exception e){
//            e.printStackTrace();
//        }
    }
    @Click
    public void goto_home() {
        mainActivity().fragment_home.stopAllVideoPlays(null);
        AudioPlayer.stopInNewThread(mp);
        mp = null;
        mainActivity().goto_home();
    }
    @Click
    public void exit() {
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }
    @Click
    public void open_system_setting() {
        startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
    }
}