wxr
2022-11-24 2af932533ef851bf983385244e9912976dbd4daa
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
package com.mm.android.deviceaddmodule.p_inputsn;
 
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
 
import com.google.zxing.Result;
import com.lechange.opensdk.softap.LCOpenSDK_SoftAPConfig;
import com.lechange.opensdk.utils.LogUtils;
import com.mm.android.deviceaddmodule.R;
import com.mm.android.deviceaddmodule.SearchDeviceManager;
import com.mm.android.deviceaddmodule.base.BaseDevAddFragment;
import com.mm.android.deviceaddmodule.contract.ScanContract;
import com.mm.android.deviceaddmodule.helper.DeviceAddHelper;
import com.mm.android.deviceaddmodule.helper.PageNavigationHelper;
import com.mm.android.deviceaddmodule.mobilecommon.AppConsume.ScanResult;
import com.mm.android.deviceaddmodule.mobilecommon.common.LCConfiguration;
import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil;
import com.mm.android.deviceaddmodule.mobilecommon.utils.PreferencesHelper;
import com.mm.android.deviceaddmodule.p_inputsn.scanresult.DecodeImgCallback;
import com.mm.android.deviceaddmodule.p_inputsn.scanresult.DecodeImgThread;
import com.mm.android.deviceaddmodule.p_inputsn.scanresult.ImageUtil;
import com.mm.android.deviceaddmodule.presenter.ScanPresenter;
import com.mm.android.deviceaddmodule.views.AddBoxTipDialog;
import com.mm.android.dhqrscanner.BaseScannerView;
import com.mm.android.dhqrscanner.DHScannerView;
 
import static android.app.Activity.RESULT_OK;
 
/**
 * 二维码扫描页
 */
public class ScanFragment extends BaseDevAddFragment implements ScanContract.View, BaseScannerView.HandleDecodeResultListener
        , View.OnClickListener {
    ScanContract.Presenter mPresenter;
    DHScannerView mDHScannerView;           //二维码扫描控件
    TextView mNext, mFlashTv, mPhone;
    boolean isLight = false;                 //闪光灯是否开启
    public static final int REQUEST_IMAGE = 10;
 
 
    public static ScanFragment newInstance() {
        ScanFragment fragment = new ScanFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }
 
    public void changeFlashLight() {
        isLight = !isLight;
        mDHScannerView.onFlash(isLight);
        Drawable drawable = getResources().getDrawable(isLight ? R.drawable.adddevice_icon_falshlight_h : R.drawable.adddevice_icon_falshlight_n);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        mFlashTv.setCompoundDrawables(null, drawable, null, null);
        mFlashTv.setText(isLight ? R.string.add_device_falshlight_on : R.string.add_device_falshlight_off);
    }
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_scan, container, false);
    }
 
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
 
 
    protected void initData() {
        mPresenter = new ScanPresenter(this);
        mPresenter.resetCache();//进入扫描页,清空缓存
 
 
    }
 
    protected void initView(View view) {
        mDHScannerView = view.findViewById(R.id.dh_scanview);
        mDHScannerView.setHandleDecodeResuleListener(this);
        mDHScannerView.setScanInRect(true);
        mNext = view.findViewById(R.id.next_btn);
        mNext.setOnClickListener(this);
        mPhone = view.findViewById(R.id.phone_btn);
        mPhone.setOnClickListener(this);
        mFlashTv = view.findViewById(R.id.tv_flash);
        mFlashTv.setOnClickListener(this);
    }
 
    @Override
    public void onResume() {
        super.onResume();
        mDHScannerView.onScanResume();
    }
 
    @Override
    public void onPause() {
        super.onPause();
        mDHScannerView.onScanPause();
        mDHScannerView.onFlash(false);
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.recyle();
    }
 
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mDHScannerView.onScanDestory();
    }
 
 
    @Override
    public void handleDecodeResult(String result, byte[] bytes, int with, int height) {
        if(!isViewActive())return;
        if (TextUtils.isEmpty(result)) {
            mDHScannerView.onScanResume();
        } else {
            LogUtil.debugLog("ScanFragment", "result : " + result);
            ScanResult scanResult = mPresenter.parseScanStr(result, "");
            String sn = scanResult.getSn().trim();
            if(!isLetterDigit(sn)) {
                toast(R.string.add_device_qrcode_error_tip);
                PageNavigationHelper.gotoManualInputPage(this);
                return;
            }
            //扫描二维码--kaede
            mPresenter.getDeviceInfo(scanResult.getSn().trim(), scanResult.getMode());
        }
    }
 
    private boolean isLetterDigit(String str) {
        // 序列号二维码规则字母 + 数字,长度 10 - 32位
        String regex = "^[a-z0-9A-Z]{10,32}$";
        return str.matches(regex);
    }
 
    @Override
    public void openCamerError() {
 
    }
 
    @Override
    public void showToastInfo(String msg) {
        toast(msg);
        mDHScannerView.onScanResume();
    }
 
    @Override
    public void showToastInfo(int msgId) {
        toast(msgId);
        mDHScannerView.onScanResume();
    }
 
    @Override
    public void showToastInfo(int msgId, String msg) {
        if (!TextUtils.isEmpty(msg)) {
            toast(msg);
        } else {
            toast(msgId);
        }
        mDHScannerView.onScanResume();
    }
 
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.next_btn == id) {
            PageNavigationHelper.gotoManualInputPage(this);
        } else if(R.id.tv_flash == id) {
            changeFlashLight();
        } else if(R.id.phone_btn == id) {
            /*打开相册*/
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent, REQUEST_IMAGE);
        }
    }
 
    @Override
    public void goTypeChoosePage() {
        PageNavigationHelper.gotoTypeChoosePage(this);
    }
 
    @Override
    public void goNotSupportBindTipPage() {
        PageNavigationHelper.gotoErrorTipPage(this, DeviceAddHelper.ErrorCode.DEVICE_BIND_ERROR_NOT_SUPPORT_TO_BIND);
    }
 
    @Override
    public void goOtherUserBindTipPage() {
        PageNavigationHelper.gotoErrorTipPage(this, DeviceAddHelper.ErrorCode.INPUT_SN_ERROR_BIND_BY_OTHER);
    }
 
    @Override
    public void showAddBoxTip() {
        if (!PreferencesHelper.getInstance(getActivity()).getBoolean(
                LCConfiguration.SHOW_ADD_BOX_TIP)) {
            AddBoxTipDialog a = new AddBoxTipDialog();
            a.setDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    goCloudConnectPage();
                }
            });
            a.show(getActivity().getSupportFragmentManager(), a.getClass().getName());
        } else {
            goCloudConnectPage();
        }
    }
 
    @Override
    public void goCloudConnectPage() {
        PageNavigationHelper.gotoCloudConnectPage(this);
    }
 
    @Override
    public void goDeviceLoginPage() {
        PageNavigationHelper.gotoDevLoginPage(this);
    }
 
    @Override
    public void goSecCodePage() {
        PageNavigationHelper.gotoDevSecCodePage(this);
    }
 
    @Override
    public void goDeviceBindPage() {
        PageNavigationHelper.gotoDeviceBindPage(this);
    }
 
    @Override
    public void goIMEIInputPage() {
        PageNavigationHelper.gotoIMEIInputPage(this);
    }
 
    @Override
    public void goBindSuceesPage() {
        PageNavigationHelper.gotoBindSuccessPage(this);
    }
 
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE && resultCode == RESULT_OK) {
            String path = ImageUtil.getImageAbsolutePath(getActivity(), data.getData());
 
            new DecodeImgThread(path, new DecodeImgCallback() {
                @Override
                public void onImageDecodeSuccess(Result result) {
                    handleDecodeResult(result.getText(),null,0,0);
                }
 
                @Override
                public void onImageDecodeFailed() {
                    toast(R.string.device_add_scan_error);
                }
            }).run();
 
        }
    }
}