package com.basic.security.fragment;
|
|
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.count.R;
|
import com.basic.security.base.BaseFragment;
|
import com.basic.security.manager.SettingManager;
|
import com.basic.security.model.Setting;
|
import com.basic.security.utils.Constants;
|
import com.basic.security.utils.CrashHandler;
|
import com.basic.security.utils.IpUtils;
|
import com.basic.security.utils.ToastUtil;
|
import com.basic.security.widget.SuccessDialog;
|
|
import org.androidannotations.annotations.AfterViews;
|
import org.androidannotations.annotations.Background;
|
import org.androidannotations.annotations.CheckedChange;
|
import org.androidannotations.annotations.Click;
|
import org.androidannotations.annotations.EFragment;
|
import org.androidannotations.annotations.UiThread;
|
import org.androidannotations.annotations.ViewById;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
@EFragment(R.layout.fragment_setting)
|
public class SettingFragment extends BaseFragment {
|
|
@ViewById
|
public Spinner hour;
|
@ViewById
|
public Spinner minute;
|
@ViewById
|
public CheckBox initValid;
|
@ViewById
|
public CheckBox adjustValid;
|
@ViewById
|
public EditText initPerson;
|
@ViewById
|
public EditText adjustPerson;
|
@ViewById
|
public TextView ip;
|
|
@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;
|
|
public static List<String> hours = new ArrayList<>();
|
public static List<String> minutes = new ArrayList<>();
|
|
boolean saved = false;
|
|
static {
|
for ( int i = 0; i <= 23; i++) {
|
String item = i+"";
|
if (i <= 9) {
|
item = "0"+i;
|
}
|
hours.add(item);
|
}
|
|
for ( int i = 0; i <= 59; i=i+5) {
|
String item = i+"";
|
if (i <= 9) {
|
item = "0"+i;
|
}
|
minutes.add(item);
|
}
|
}
|
|
@Background
|
public void show() {
|
saved = false;
|
initData(SettingManager.getSetting());
|
}
|
|
@UiThread
|
public void initData(Map<String, String> setting) {
|
ip.setText(IpUtils.getIpAddress(mainActivity()));
|
if (Constants.TRUE.equals(setting.get(Setting.initValid))) {
|
initValid.setChecked(true);
|
}
|
initValid(null, initValid.isChecked());
|
if (Constants.TRUE.equals(setting.get(Setting.adjustValid))) {
|
adjustValid.setChecked(true);
|
}
|
adjustValid(null, adjustValid.isChecked());
|
if (!TextUtils.isEmpty(setting.get(Setting.hour))) {
|
hour.setSelection(getSelectedIndex(hours, setting.get(Setting.hour)));
|
}
|
if (!TextUtils.isEmpty(setting.get(Setting.minute))) {
|
minute.setSelection(getSelectedIndex(hours, setting.get(Setting.minute)));
|
}
|
if (!TextUtils.isEmpty(setting.get(Setting.initPerson))) {
|
initPerson.setText(setting.get(Setting.initPerson));
|
}
|
if (!TextUtils.isEmpty(setting.get(Setting.adjustPerson))) {
|
adjustPerson.setText(setting.get(Setting.adjustPerson));
|
}
|
oldPassword.setText("");
|
newPassword.setText("");
|
newPassword2.setText("");
|
}
|
|
public int getSelectedIndex(List<String> array, String value) {
|
int index = 0;
|
for (int i = 0; i < array.size(); i++) {
|
String arrayValue = array.get(i);
|
if (arrayValue.equals(value)) {
|
index = i;
|
}
|
}
|
return index;
|
}
|
|
@AfterViews
|
public void afterViews() {
|
hour.setAdapter(new ArrayAdapter<>(getContext(), R.layout.spinner_item_small_text, hours.toArray()));
|
hour.setPopupBackgroundResource(R.drawable.round_border);
|
minute.setAdapter(new ArrayAdapter<>(getContext(), R.layout.spinner_item_small_text, minutes.toArray()));
|
minute.setPopupBackgroundResource(R.drawable.round_border);
|
|
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 onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
mLztek.setEthEnable(isChecked);
|
refreshAddrInfo(isChecked);
|
}
|
});
|
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);
|
}
|
}
|
}
|
});
|
}
|
|
} 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);
|
// if ("0.0.0.0".equals(addrInfo.getDns())) {
|
// ip_address.setText(IpUtils.getIpAddress(mainActivity()));
|
// net_mask.setText("255.255.255.0");
|
// gate_way.setText("192.168.1.1");
|
// dns_server.setText("8.8.8.8");
|
// } else {
|
//
|
// }
|
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);
|
|
// Toast.makeText(mainActivity(), "Ethernet set static ip ok", Toast.LENGTH_LONG).show();
|
} else {
|
mLztek.setEthDhcpMode();
|
// Toast.makeText(mainActivity(), "Ethernet set dhcp mode ok", Toast.LENGTH_LONG).show();
|
}
|
}
|
|
@Click
|
public void save() {
|
|
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 (saved) {
|
return;
|
}
|
saved = true;
|
if (Constants.NativeLib) {
|
boolean ethEnalbe = mLztek.getEthEnable();
|
if (ethEnalbe) {
|
onBtnConfirm();
|
}
|
}
|
Map<String, String> newSetting = new HashMap<>();
|
newSetting.put(Setting.initValid, initValid.isChecked()?Constants.TRUE:Constants.FALSE);
|
newSetting.put(Setting.adjustValid, adjustValid.isChecked()?Constants.TRUE:Constants.FALSE);
|
newSetting.put(Setting.hour, hour.getSelectedItem().toString());
|
newSetting.put(Setting.minute, minute.getSelectedItem().toString());
|
newSetting.put(Setting.password, password);
|
newSetting.put(Setting.initPerson, initPerson.getText().toString());
|
newSetting.put(Setting.adjustPerson, adjustPerson.getText().toString());
|
|
saveSetting(newSetting);
|
}
|
|
@Background
|
public void saveSetting(Map<String, String> newSetting) {
|
SettingManager.saveSetting(newSetting);
|
saveSuccess();
|
}
|
|
@UiThread
|
public void saveSuccess() {
|
SuccessDialog successDialog = new SuccessDialog(mainActivity());
|
successDialog.show();
|
}
|
|
@Click
|
public void goto_home() {
|
mainActivity().goto_home();
|
}
|
|
@CheckedChange
|
public void initValid(CompoundButton buttonView, boolean isChecked) {
|
hour.setEnabled(isChecked);
|
minute.setEnabled(isChecked);
|
initPerson.setEnabled(isChecked);
|
}
|
|
@CheckedChange
|
public void adjustValid(CompoundButton buttonView, boolean isChecked) {
|
adjustPerson.setEnabled(isChecked);
|
}
|
|
}
|