wjc
2023-07-12 e604e1797744977f599dad9f543db3e7477fe115
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
package com.hdl.photovoltaic.ui;
 
 
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
 
 
import androidx.annotation.NonNull;
import androidx.appcompat.content.res.AppCompatResources;
 
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.hdl.linkpm.sdk.core.exception.HDLException;
import com.hdl.linkpm.sdk.user.HDLLinkPMUser;
import com.hdl.linkpm.sdk.user.bean.HDLLoginBean;
import com.hdl.linkpm.sdk.user.bean.HDLUserRegionBean;
import com.hdl.linkpm.sdk.user.callback.ILoginCallBack;
import com.hdl.photovoltaic.HDLApp;
import com.hdl.photovoltaic.R;
import com.hdl.photovoltaic.base.CustomBaseActivity;
import com.hdl.photovoltaic.enums.ShowErrorMode;
import com.hdl.photovoltaic.other.HdlFileLogic;
import com.hdl.photovoltaic.other.HdlResidenceLogic;
import com.hdl.photovoltaic.ui.bean.HouseIdBean;
import com.hdl.photovoltaic.ui.bean.LoginUserBean;
import com.hdl.photovoltaic.ui.bean.LoginUserRegionBean;
import com.hdl.photovoltaic.config.AppConfigManage;
import com.hdl.photovoltaic.databinding.ActivityHomeLoginBinding;
import com.hdl.photovoltaic.listener.CloudCallBeak;
import com.hdl.photovoltaic.other.HdlAccountLogic;
import com.hdl.photovoltaic.other.HdlThreadLogic;
import com.hdl.photovoltaic.ui.device.FastScanActivity;
import com.hdl.photovoltaic.uni.HDLUniMP;
import com.hdl.photovoltaic.utils.PermissionUtils;
import com.hdl.photovoltaic.utils.WifiUtils;
 
import org.json.JSONException;
import org.json.JSONObject;
 
import java.util.List;
 
 
public class HomeLoginActivity extends CustomBaseActivity {
 
    private ActivityHomeLoginBinding viewBinding;
 
    @Override
    public Object getContentView() {
        viewBinding = ActivityHomeLoginBinding.inflate(getLayoutInflater());
        return viewBinding.getRoot();
    }
 
    @Override
    public void onBindView(Bundle savedInstanceState) {
        //初始化
        initView();
        //初始化界面监听器
        initEvent();
        //在界面中使用
//        KeyboardStateObserverUtils.getKeyboardStateObserver(_mActivity).setKeyboardVisibilityListener(new KeyboardStateObserverUtils.OnKeyboardVisibilityListener() {
//            @Override
//            public void onKeyboardShow(int h) {
//                //Toast.makeText(MainActivity.this,"键盘弹出",Toast.LENGTH_SHORT).show();
//                ConstraintSet cs = new ConstraintSet();
//                cs.clone(viewBinding.homeLoginParentCl);
////                (viewBinding.loadingConfirmationRl.getTop()-dip2px(h))
//                cs.connect(R.id.home_login_tv, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, h);//
//                //应用约束
//                cs.applyTo(viewBinding.homeLoginParentCl);
//            }
//
//            @Override
//
//            public void onKeyboardHide(int h) {
//                ConstraintSet cs = new ConstraintSet();
//                cs.clone(viewBinding.homeLoginParentCl);
//                cs.connect(R.id.home_login_tv, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, h);//
//                //应用约束
//                cs.applyTo(viewBinding.homeLoginParentCl);
//            }
//
//        });
 
 
    }
 
    private void initView() {
        setStringDifferentColors(viewBinding.homeLoginPrivacyTv);
        //测试用
        viewBinding.homeLoginAccountEt.setText("13375012441");
        viewBinding.homeLoginPasswordEt.setText("123456");
        isLoginTextViewEnabled();
    }
 
    private void initEvent() {
 
        viewBinding.homeLoginAccountEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 
            }
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d("HomeLoginActivity2", String.format("CharSequence=%s,start=%s,before=%s,count=%s", s, start, before, count));
 
            }
 
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    if (viewBinding.homeLoginClearContentsIv.getVisibility() == View.GONE) {
                        viewBinding.homeLoginClearContentsIv.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (viewBinding.homeLoginClearContentsIv.getVisibility() == View.VISIBLE) {
                        viewBinding.homeLoginClearContentsIv.setVisibility(View.GONE);
                    }
                }
                isLoginTextViewEnabled();
 
            }
        });
        viewBinding.homeLoginClearContentsIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewBinding.homeLoginAccountEt.setText("");
            }
        });
 
        viewBinding.homeLoginPasswordEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 
            }
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
 
            }
 
            @Override
            public void afterTextChanged(Editable s) {
                isLoginTextViewEnabled();
            }
        });
        viewBinding.homeLoginHideIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.isSelected()) {
                    v.setSelected(false);
                    viewBinding.homeLoginHideIv.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.hide));
                    viewBinding.homeLoginPasswordEt.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    v.setSelected(true);
                    viewBinding.homeLoginHideIv.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.show));
                    viewBinding.homeLoginPasswordEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
 
 
                }
                if (!TextUtils.isEmpty(viewBinding.homeLoginPasswordEt.getText())) {
                    viewBinding.homeLoginPasswordEt.setSelection(viewBinding.homeLoginPasswordEt.getText().length());
                }
            }
        });
 
 
        viewBinding.homeLoginCheckIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!v.isSelected());
            }
        });
        viewBinding.homeLoginPrivacyCheckIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!v.isSelected());
            }
        });
        //登录
        viewBinding.homeLoginTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
 
                String account = viewBinding.homeLoginAccountEt.getText().toString().replace(" ", "");
                String password = viewBinding.homeLoginPasswordEt.getText().toString().replace(" ", "");
                boolean isLogin = isLocalCheckAccountAndPassword(account, password);
                if (!isLogin) {
                    return;
                }
//                boolean isSelected = viewBinding.homeLoginPrivacyCheckIv.isSelected();
//                if(!isSelected){
//                    //隐私协议提示
//                    return;
//                }
                showLoading(getString(R.string.home_login_登录中));
                HdlAccountLogic.getInstance().regionByAccount(account, new CloudCallBeak<HDLUserRegionBean>() {
                    @Override
                    public void onSuccess(HDLUserRegionBean obj) {
                        AppConfigManage.setUserRegionUrl("http://59.41.255.150:7777");//obj.getRegionUrl()
                        HDLLinkPMUser.getInstance().setUserRegionUrl(AppConfigManage.getUserRegionUrl());
                        HdlAccountLogic.getInstance().loginByPassword(account, password, new ILoginCallBack() {
                            @Override
                            public void onSuccess(HDLLoginBean obj) {
                                HdlResidenceLogic.getInstance().getResidenceIdList("", "", new CloudCallBeak<List<HouseIdBean>>() {
                                    @Override
                                    public void onSuccess(List<HouseIdBean> obj) {
                                        hideLoading();
                                        HdlResidenceLogic.getInstance().setHouseIdList(obj);
                                        restoreButtonStyleToInitializeState();
                                        startActivity(MyPowerStationActivity.class);
                                    }
 
                                    @Override
                                    public void onFailure(HDLException e) {
                                        hideLoading();
                                        restoreButtonStyleToInitializeState();
                                        startActivity(MyPowerStationActivity.class);
                                    }
                                });
                            }
 
                            @Override
                            public void onFailure(HDLException exception) {
                                hideLoading();
                            }
                        });
                    }
 
                    @Override
                    public void onFailure(HDLException e) {
                        hideLoading();
                    }
                });
 
 
            }
        });
        //体验电站
        viewBinding.homeLoginExperienceTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                requestPermissions();
//                startActivity(MyPowerStationActivity.class);
                HdlFileLogic.getInstance().deleteDirectory(HdlFileLogic.getInstance().getCurrentHomeRootPath());
            }
        });
 
    }
 
    /**
     * 校验登录按钮是否启用
     */
    private void isLoginTextViewEnabled() {
        String account = viewBinding.homeLoginAccountEt.getText().toString().replace(" ", "");
        String password = viewBinding.homeLoginPasswordEt.getText().toString().replace(" ", "");
        boolean isEnabled = account.length() > 0 && password.length() > 0;
        viewBinding.homeLoginTv.setEnabled(isEnabled);
    }
 
    /**
     * 本地校验输入账号或者密码是否正确
     *
     * @param account  账号
     * @param password 密码
     *                 正确返回<true>true</true>
     *                 一般方法
     */
    private boolean isLocalCheckAccountAndPassword(String account, String password) {
        if (TextUtils.isEmpty(account)) {
            viewBinding.homeLoginLine1V.setBackgroundColor(getColor(R.color.text_D34545));
            viewBinding.homeLoginAccountTextErrorTv.setText(R.string.home_login_error_账号不能为空);
            return false;
        }
        if (TextUtils.isEmpty(password)) {
            viewBinding.homeLoginLine2V.setBackgroundColor(getColor(R.color.text_D34545));
            viewBinding.homeLoginPasswordTextErrorTv.setText(R.string.home_login_error_密码不能为空);
            return false;
        }
 
 
        if (password.length() < 6 || password.length() > 16) {
            viewBinding.homeLoginLine2V.setBackgroundColor(getColor(R.color.text_D34545));
            viewBinding.homeLoginPasswordTextErrorTv.setText(R.string.home_login_error_最少6_16个字符);
            return false;
        }
        return true;
 
    }
 
    /**
     * 恢复组件样式到初始化状态
     */
    private void restoreButtonStyleToInitializeState() {
        HdlThreadLogic.runMainThread(new Runnable() {
            @Override
            public void run() {
                viewBinding.homeLoginLine1V.setBackgroundColor(getColor(R.color.text_E1E1E1));
                viewBinding.homeLoginAccountTextErrorTv.setText("");
                viewBinding.homeLoginLine2V.setBackgroundColor(getColor(R.color.text_E1E1E1));
                viewBinding.homeLoginPasswordTextErrorTv.setText("");
            }
        }, null, null);
    }
 
    /**
     * 设置一串字符多种颜色
     *
     * @param view 当前组件
     */
    private void setStringDifferentColors(TextView view) {
        String s = view.getText().toString();
        SpannableStringBuilder spannable = new SpannableStringBuilder(s);
        //设置文字的前景色
        spannable.setSpan(new ForegroundColorSpan(getColor(R.color.text_245EC3)), 2, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        spannable.setSpan(new ForegroundColorSpan(getColor(R.color.text_245EC3)), 9, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        view.setText(spannable);
    }
 
 
}