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
package ezviz.ezopensdkcommon.configwifi;
 
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
 
import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.ezviz.sdk.configwifi.EZConfigWifiErrorEnum;
 
import ezviz.ezopensdkcommon.R;
import ezviz.ezopensdkcommon.common.IntentConstants;
import ezviz.ezopensdkcommon.common.LogUtil;
import ezviz.ezopensdkcommon.common.RootActivity;
import ezviz.ezopensdkcommon.common.RouteNavigator;
 
public class ConfigWifiExecutingActivity extends RootActivity implements ConfigWifiExecutingActivityPresenter.Callback{
 
    private final static String TAG = ConfigWifiExecutingActivity.class.getSimpleName();
 
    private ConfigWifiExecutingActivityPresenter mPresenter;
    private View mConfigResultView;
    private View mConfigSuccessView;
    private View mConfigFailView;
    private TextView mConfigErrorInfoTv;
    private String mAllErrorInfo;
 
    public static void launch(Context context, Intent intent){
        Intent newIntent = new Intent(context, ConfigWifiExecutingActivity.class);
        newIntent.putExtras(intent);
        newIntent.putExtra(IntentConstants.USE_MANUAL_AP_CONFIG, true);
        context.startActivity(newIntent);
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config_wifi_executing);
 
        mPresenter = ConfigWifiExecutingActivityPresenter.getPresenter(getIntent()
                .getStringExtra(IntentConstants.SELECTED_PRESENTER_TYPE));
        if (mPresenter == null){
            LogUtil.e(TAG, "failed to init!");
            return;
        }
        initUI();
        getWindow().getDecorView().post(new Runnable() {
            @Override
            public void run() {
                showExecutingUi();
                startConfig();
            }
        });
    }
 
    @Override
    protected void onStop() {
        super.onStop();
        showToast(getString(R.string.app_common_stop_config_wifi_while_switched_to_background));
        exitPage();
    }
 
    /**
     * 重试
     */
    public void onClickRetryConfigWifi(View view) {
        showExecutingUi();
        startConfig();
    }
 
    /**
     * 配网成功,根据使用sdk不同,跳转到对应页面
     */
    public void onClickToConfigAnother(View view) {
        // 如果是在使用配网sdk,则跳转到配网开始页
        if (getIntent().getBooleanExtra(IntentConstants.USING_CONFIG_WIFI_SDK, false)){
            ARouter.getInstance().build(RouteNavigator.CONFIG_WIFI_MAIN_PAGE)
                    .navigation(this, new NavCallback() {
                        @Override
                        public void onArrival(Postcard postcard) {
                            exitPage();
                        }
 
                        @Override
                        public void onLost(Postcard postcard) {
                            // do nothing
                        }
                    });
        }
    }
 
    @Override
    public void onBackPressed() {
        exitPage();
    }
 
    public void onClickExit(View view) {
        exitPage();
    }
 
    private void exitPage(){
        stopConfig();
        finish();
    }
 
    @Override
    public void onConnectedToWifi() {
        LogUtil.i(TAG, "onConnectedToWifi");
        // 仅使用配网sdk时,才展示配网成功的ui
        if (getIntent().getBooleanExtra(IntentConstants.USING_CONFIG_WIFI_SDK, false)){
            showConfigSuccessUi();
        }
    }
 
    @Override
    public void onConnectedToPlatform() {
        LogUtil.i(TAG, "onConnectedToPlatform");
        ARouter.getInstance().build(RouteNavigator.ADD_DEVICE_PAGE)
                .withString(IntentConstants.DEVICE_SERIAL, getIntent().getStringExtra(IntentConstants.DEVICE_SERIAL))
                .withString(IntentConstants.DEVICE_VERIFY_CODE, getIntent().getStringExtra(IntentConstants.DEVICE_VERIFY_CODE))
                .withFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK)
                .navigation(this, new NavCallback() {
                    @Override
                    public void onArrival(Postcard postcard) {
                        exitPage();
                    }
 
                    @Override
                    public void onLost(Postcard postcard) {
                        // do nothing
                    }
                });
    }
 
    @Override
    public void onConfigInfo(int info) {
 
    }
 
    @Override
    public void onConfigError(int code, String msg) {
        String errorInfo = "***"  + "error code is: " + code +  ", error msg is: " + msg + "\n";
        LogUtil.e(TAG, errorInfo);
        // 仅展示新定义的错误码
        for (EZConfigWifiErrorEnum error : EZConfigWifiErrorEnum.values()){
            if (code == error.code){
                if (mAllErrorInfo == null){
                    mAllErrorInfo = "" + errorInfo;
                }else{
                    mAllErrorInfo += errorInfo;
                }
                break;
            }
        }
        // 用户拒绝连接设备热点则认为配网失败
        if (code == EZConfigWifiErrorEnum.USER_REFUSED_CONNECTION_REQUEST.code){
            failedToConfig();
        }
    }
 
    @Override
    public void onTimeout() {
        failedToConfig();
    }
 
    private void failedToConfig(){
        stopConfig();
        switch (mPresenter.getType()){
            case ConfigWifiTypeConstants.CONFIG_WIFI_SDK_AP:
            case ConfigWifiTypeConstants.FULL_SDK_AP:
                ManualConnectDeviceHotspotActivity.Companion.launch(this, getIntent());
                break;
            default:
                showConfigFailUi();
                break;
        }
    }
 
    private void startConfig(){
        if (mPresenter != null){
            mPresenter.setCallback(this);
            mPresenter.startConfigWifi(getApplication(), getIntent());
        }
    }
 
    private void stopConfig(){
        if (mPresenter != null){
            mPresenter.setCallback(null);
            mPresenter.stopConfigWifi();
        }
    }
 
    private void initUI() {
        mConfigResultView = findViewById(R.id.app_common_vg_config_result);
        mConfigSuccessView = findViewById(R.id.app_common_config_result_success);
        mConfigFailView = findViewById(R.id.app_common_config_result_fail);
        mConfigErrorInfoTv = findViewById(R.id.app_common_all_config_error_info);
    }
 
    private void showExecutingUi(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mConfigResultView.setVisibility(View.GONE);
                mConfigSuccessView.setVisibility(View.GONE);
                mConfigFailView.setVisibility(View.GONE);
                mAllErrorInfo = null;
            }
        });
    }
 
    private void showConfigSuccessUi(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mConfigResultView.setVisibility(View.VISIBLE);
                mConfigSuccessView.setVisibility(View.VISIBLE);
                mConfigFailView.setVisibility(View.GONE);
                mAllErrorInfo = null;
            }
        });
    }
 
    private void showConfigFailUi(){
       runOnUiThread(new Runnable() {
           @Override
           public void run() {
               mConfigErrorInfoTv.setText(mAllErrorInfo);
               mConfigResultView.setVisibility(View.VISIBLE);
               mConfigSuccessView.setVisibility(View.GONE);
               mConfigFailView.setVisibility(View.VISIBLE);
               mAllErrorInfo = null;
           }
       });
    }
 
}