zhangzengfei
2020-07-30 512cae0fd797fac428f07d9d955f880e9ee353c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package com.basic.security.fragment;
 
import android.content.Intent;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.view.View;
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;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
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 ip;
    @ViewById
    public EditText title;
    @ViewById
    public EditText port;
    @ViewById
    public Spinner second;
    @ViewById
    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() {
        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, 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);
        }
        if (portStr != null) {
            port.setText(portStr);
        }
        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) {
        int index = 0;
        for (int i = 0; i < array.length; i++) {
            String arrayValue = array[i];
            if (arrayValue.equals(value)) {
                index = i;
            }
        }
        return index;
    }
 
    public static int getAudioRawIndex(String audioAlarmStr) {
        if (audioAlarmStr != null) {
            for (int i = 0; i < SettingFragment.alarmAudioArray.length; i++) {
                if (SettingFragment.alarmAudioArray[i].equals(audioAlarmStr)) {
                    if (i == 0) {
                        return R.raw.a1;
                    }
                    if (i == 1) {
                        return R.raw.a2;
                    }
                    if (i == 2) {
                        return R.raw.a3;
                    }
                    if (i == 3) {
                        return R.raw.a4;
                    }
                    if (i == 4) {
                        return R.raw.a5;
                    }
                }
            }
        }
        return R.raw.a1;
    }
 
    @AfterViews
    public void afterViews() {
        try {
            second.setAdapter(new ArrayAdapter<>(getContext(), R.layout.spinner_item_small_text, secondArray));
            second.setPopupBackgroundResource(R.drawable.round_border);
 
            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 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);
                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 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 (ipStr.length() == 0) {
            ToastUtil.show("IP不能为空");
            return;
        }
        if (portStr.length() == 0) {
            ToastUtil.show("端口不能为空");
            return;
        }
        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();
            AudioPlayer.stopInNewThread(mp);
            mp = null;
            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);
    }
}