wjc
2023-03-30 c5451371d006652b8c7a5da8c3ca7525d7b39fef
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
package com.videogo.devicemgt;
 
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputFilter.LengthFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
 
import com.videogo.EzvizApplication;
import ezviz.ezopensdkcommon.common.RootActivity;
import com.videogo.constant.IntentConsts;
import com.videogo.device.PeripheralInfo;
import com.videogo.errorlayer.ErrorInfo;
import com.videogo.exception.BaseException;
import com.videogo.exception.ErrorCode;
import com.videogo.ui.util.ActivityUtils;
import com.videogo.util.ConnectionDetector;
import com.videogo.util.LogUtil;
import com.videogo.widget.TitleBar;
import com.videogo.widget.WaitDialog;
 
import ezviz.ezopensdk.R;
 
//import com.videogo.restful.exception.VideoGoNetSDKException;
//import com.videogo.util.ActivityUtils;
//import com.videogo.widget.inputfilter.BytesLengthFilter;
//import com.videogo.widget.inputfilter.IllegalWordFilter;
 
public class ModifyDeviceNameActivity extends RootActivity implements OnClickListener {
 
    protected static final int MSG_UPDATA_DEVICE_NAME_FAIL = 1001;
 
    protected static final int MSG_UPDATA_DEVICE_NAME_SUCCESS = 1002;
 
    private final static int TYPE_DEVICE = 0x01;
    private final static int TYPE_CAMERA = 0x02;
    private final static int TYPE_DETECTOR = 0x04;
 
    private TitleBar mTitleBar;
    private EditText mNameText;
    private TextView mDetectorTypeView;
    private ImageButton mNameDelButton;
    private TextView mInputHintView;
 
    private ViewGroup mCommonNameLayout;
    private GridView mCommonNameGridView;
 
    private PeripheralInfo mDetector;
 
    private WaitDialog mWaitDialog;
    
    private String mDeviceNameString;
 
    private Handler mHandler;
    private int mType;
    private Button mSaveButton = null;
 
    private String mDeviceName;
    private String mDeviceSerial;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modify_device_name_page);
 
        findViews();
        initData();
        initTitleBar();
        initViews();
    }
 
    private void findViews() {
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.setBackButton(R.drawable.common_title_cancel_selector);
        mNameText = (EditText) findViewById(R.id.name_text);
        mNameDelButton = (ImageButton) findViewById(R.id.name_del);
        mDetectorTypeView = (TextView) findViewById(R.id.detector_type);
        mInputHintView = (TextView) findViewById(R.id.input_hint);
        mCommonNameLayout = (ViewGroup) findViewById(R.id.common_name_layout);
        mSaveButton = (Button) findViewById(R.id.btn_id_save_name);
    }
 
    private void initData() {
        mHandler = new MyHandler();
 
        if (getIntent().hasExtra(IntentConsts.EXTRA_NAME)) {
            mDeviceName = getIntent().getStringExtra(IntentConsts.EXTRA_NAME);
 
            mType = TYPE_DEVICE;
 
            mInputHintView.setText(getString(R.string.detail_modify_device_name_limit_tip, 50));
            mNameText.setFilters(new InputFilter[] {
                    new LengthFilter(50)});
 
        }
        if (getIntent().hasExtra(IntentConsts.EXTRA_DEVICE_ID)){
            mDeviceSerial = getIntent().getStringExtra(IntentConsts.EXTRA_DEVICE_ID);
        }
 
        mWaitDialog = new WaitDialog(ModifyDeviceNameActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
        mWaitDialog.setCancelable(false);
    }
 
    private void initTitleBar() {
        mTitleBar.setTitle(R.string.ez_modify_name);
        mTitleBar.addBackButton(new OnClickListener() {
 
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
//        mTitleBar.addRightButton(R.drawable.common_title_confirm_selector, new OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                modifyDeviceName();
//            }
//        });
    }
 
    private void initViews() {
        mNameText.setText(TextUtils.isEmpty(mDeviceName)?"":mDeviceName);
        mNameText.setSelection(mNameText.getText().length());
        mNameText.addTextChangedListener(new TextWatcher() {
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
 
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
 
            @Override
            public void afterTextChanged(Editable s) {
                String str = s.toString();
                setSelectLabel(str);
            }
        });
        mNameText.setOnEditorActionListener(new OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    modifyDeviceName();
                    return true;
                }
                return false;
            }
        });
 
        mNameDelButton.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View v) {
                mNameText.setText(null);
            }
        });
 
        if (mType == TYPE_DETECTOR) {} else {
            mCommonNameLayout.setVisibility(View.GONE);
        }
        mSaveButton.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.btn_id_save_name) {
            modifyDeviceName();
        }
    }
 
    private void setSelectLabel(String input) {}
 
    private void modifyDeviceName() {
        if(TextUtils.isEmpty(mDeviceName)) {
            return;
        }
        mDeviceNameString = mNameText.getText().toString().trim();
 
        if (TextUtils.isEmpty(mDeviceNameString)) {
            showToast(R.string.company_addr_is_empty);
            return;
        }
 
        // 本地网络检测
        if (!ConnectionDetector.isNetworkAvailable(ModifyDeviceNameActivity.this)) {
            showToast(R.string.offline_warn_text);
            return;
        }
 
        mWaitDialog.show();
 
        new Thread() {
            @Override
            public void run() {
                int errorCode = 0;
 
                try {
                    EzvizApplication.getOpenSDK().setDeviceName(mDeviceSerial, mDeviceNameString);
                } catch (BaseException e) {
                    e.printStackTrace();
 
                    ErrorInfo errorInfo = (ErrorInfo) e.getObject();
                    LogUtil.d("TAG", errorInfo.toString());
                }
 
                if (errorCode != 0) {
                    mHandler.obtainMessage(MSG_UPDATA_DEVICE_NAME_FAIL, errorCode, 0).sendToTarget();
                } else {
                    mHandler.obtainMessage(MSG_UPDATA_DEVICE_NAME_SUCCESS).sendToTarget();
                }
            }
        }.start();
    }
 
    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what==MSG_UPDATA_DEVICE_NAME_FAIL) {
                handleUpdateFail(msg.arg1);
            }else  if (msg.what==MSG_UPDATA_DEVICE_NAME_SUCCESS) {
                handleUpdateSuccess();
            }else{}
        }
    }
 
    private void handleUpdateFail(int errorCode) {
        mWaitDialog.dismiss();
        if (errorCode == ErrorCode.ERROR_TRANSF_DEVICE_OFFLINE) {
            showToast(R.string.camera_not_online);
        } else if (errorCode == ErrorCode.ERROR_WEB_SESSION_ERROR) {
            ActivityUtils.handleSessionException(ModifyDeviceNameActivity.this);
        } else if (errorCode == ErrorCode.ERROR_WEB_HARDWARE_SIGNATURE_ERROR) {
            ActivityUtils.handleSessionException(ModifyDeviceNameActivity.this);
        } else {
            // 修改失败,提示失败的消息
            showToast(R.string.detail_modify_fail, errorCode);
        }
    }
 
    private void handleUpdateSuccess() {
        mWaitDialog.dismiss();
        showToast(R.string.detail_modify_success);
        Intent intent = new Intent();
        intent.putExtra(IntentConsts.EXTRA_NAME, mDeviceNameString);
        setResult(RESULT_OK, intent);
        finish();
    }
 
    private void hideSoftInput() {
        if (getCurrentFocus() != null) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
 
    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.fade_down);
    }
}