111
hxb
2022-11-24 0a3e07f10937484145f33c7560607b4b2353cb81
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
package com.mm.android.deviceaddmodule.helper;
 
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.support.v4.app.Fragment;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.view.View.OnClickListener;
import android.widget.TextView;
 
import com.company.NetSDK.DEVICE_NET_INFO_EX;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.lechange.opensdk.media.DeviceInitInfo;
import com.mm.android.deviceaddmodule.R;
import com.mm.android.deviceaddmodule.mobilecommon.common.LCConfiguration;
import com.mm.android.deviceaddmodule.mobilecommon.dialog.LCAlertDialog;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
 
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * 设备添加相关工具类
 *
 */
public class Utils4AddDevice {
    public static final int NETWORK_NONE = -1; //  无网络
    public static final int NETWORK_WIFI = 0;  //  wifi
    public static final int NETWORK_MOBILE = 1;  //  数据网络
 
    public static String strRegCodeFilter(String str) {
 
        if (TextUtils.isEmpty(str)) {
            return str;
        }
        String strEx = "[0-9A-Za-z]";
        for (int i = 0; i < str.length(); i++) {
            String temp = str.substring(i, i + 1);
            if (!temp.matches(strEx)) {
                str = str.replace(temp, "");
                return strRegCodeFilter(str);
            }
        }
        return str;
    }
 
    public static String wifiPwdFilter(String str) {
 
        if (TextUtils.isEmpty(str)) {
            return str;
        }
        // TD:20780
        String chinese1 = "[\u2E80-\uA4CF]";
        String chinese2 = "[\uF900-\uFAFF]";
        String chinese3 = "[\uFE30-\uFE4F]";
 
        for (int i = 0; i < str.length(); i++) {
            String temp = str.substring(i, i + 1);
            if (temp.matches(chinese1) || temp.matches(chinese2) || temp.matches(chinese3)) {
                str = str.replace(temp, "");
                return wifiPwdFilter(str);
            }
        }
        return str;
    }
 
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE); // 获取系统网络连接管理�?
        if (connectivity == null) { // 如果网络管理器为null
            return false; // 返回false表明网络无法连接
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo(); // 获取�?��的网络连接对�?
            if (info != null) { // 网络信息不为null
                for (NetworkInfo anInfo : info) { // 遍历网路连接对象
                    if (anInfo.isConnected()) { // 当有�?��网络连接对象连接上网络时
                        return true; // 返回true表明网络连接正常
                    }
                }
            }
        }
        return false;
    }
 
    public static boolean isWifi(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        } else {
            NetworkInfo curNetwork = connectivity.getActiveNetworkInfo();
            if (curNetwork != null && curNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                return true;
            }
        }
        return false;
    }
 
    public static int getNetWorkState(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if(connectivity != null) {
            NetworkInfo curNetwork = connectivity.getActiveNetworkInfo();
            if (curNetwork != null && curNetwork.isConnected()) {
                if (curNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                    return NETWORK_WIFI;
                } else if (curNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                    return NETWORK_MOBILE;
                }
            }
        }
        return NETWORK_NONE;
    }
 
    public static boolean isWifiEnabled(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        boolean wifi= (wifiInfo.getSSID()==null) || wifiInfo.getSSID().equalsIgnoreCase("");
        return !wifi;
    }
 
    public static boolean isWifiConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (mConnectivityManager == null){
                return false;
            }
            NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (mWiFiNetworkInfo != null) {
                return mWiFiNetworkInfo.isAvailable();
            }
        }
        return false;
    }
    /**
     * 判断是否只包含数字或大小写字母
     *
     * @param str
     * @return
     */
    public static boolean checkString(String str) {
        String regEx = "[0-9A-Za-z]*"; // 只能是数字以及个别字符
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        if (m.matches()) {
            return true;
        } else {
            return false;
        }
    }
 
    public static String filterInvalidString(String str) {
        if (TextUtils.isEmpty(str)) {
            return str;
        }
        String numberAndAbc = "[a-zA-Z0-9]";
        StringBuilder buffer = new StringBuilder();
        int len = str.length();
        for (int i = 0; i < len; ++i) {
            String temp = str.substring(i, i + 1);
            if (temp.matches(numberAndAbc)) {
                buffer.append(temp);
            }
        }
        return buffer.toString();
    }
 
    public static String filterInvalidString4Type(String str) {
        if (TextUtils.isEmpty(str)) {
            return str;
        }
        String numberAndAbc = "[a-zA-Z0-9-/\\\\]";
        StringBuilder buffer = new StringBuilder();
        int len = str.length();
        for (int i = 0; i < len; ++i) {
            String temp = str.substring(i, i + 1);
            if (temp.matches(numberAndAbc)) {
                buffer.append(temp);
            }
        }
        return buffer.toString();
    }
 
    /**
     * 是否是乐盒设备
     *
     * @param deviceMode
     * @return
     */
    public static boolean isDeviceTypeBox(String deviceMode) {
        if (deviceMode == null || TextUtils.isEmpty(deviceMode)) {
            return false;
        }
        if (deviceMode.equalsIgnoreCase("G10")) {
            return true;
        } else {
            return false;
        }
    }
 
    /**
     * 设置下划线字体
     */
    public static SpannableString setSpannableString(Context context, int showResId, int showUnderLineResId) {
 
        String showResourceTip = context.getResources().getString(showResId);
        String showUnderLineResTip = context.getResources().getString(showUnderLineResId);
        String tip = showResourceTip + showUnderLineResTip;
        SpannableString ss = new SpannableString(tip);
        int start = showResourceTip.length();
        int end = showUnderLineResTip.length() + start;
        int flag = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
        MyURLSpan mus = new MyURLSpan(context.getResources().getString(R.string.assetfont_html));// 字体
        ForegroundColorSpan fcs = new ForegroundColorSpan(context.getResources()
                .getColor(R.color.lc_color_4ea7f2));
        ss.setSpan(mus, start, end, flag);
        ss.setSpan(fcs, start, end, flag);
        return ss;
    }
 
    /**
     * <p>
     * 设置下划线字体及点击事件
     * </p>
     */
    public static void setSpannableString(int showResId, int showUnderLineResId, OnClickListener listener, TextView view) {
        String showResourceTip = "";
        if (showResId != 0) {
            showResourceTip = view.getContext().getResources().getString(showResId);
        }
        String showUnderLineResTip = view.getContext().getResources().getString(showUnderLineResId);
        String tip = showResourceTip + showUnderLineResTip;
        SpannableString ss = new SpannableString(tip);
        int start = showResourceTip.length();
        int end = showUnderLineResTip.length() + start;
        int flag = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
        MyURLSpan mus = new MyURLSpan(view.getContext().getResources().getString(R.string.assetfont_html));// 字体
        mus.setOnClickListener(listener);
        ForegroundColorSpan fcs = new ForegroundColorSpan(view.getContext().getResources()
                .getColor(R.color.lc_color_4ea7f2));
        ss.setSpan(mus, start, end, flag);
        ss.setSpan(fcs, start, end, flag);
        view.setText(ss);
        view.setMovementMethod(LinkMovementMethod.getInstance());
    }
 
    /**
     * <p>
     * 设置下划线字体及点击事件
     * </p>
     */
    public static void setSpannableString(int showResId, String showUnderLineResTip, OnClickListener listener, TextView view) {
        String showResourceTip = "";
        if (showResId != 0) {
            showResourceTip = view.getContext().getResources().getString(showResId);
        }
        if (showUnderLineResTip == null) {
            showUnderLineResTip = "";
        }
        String tip = showResourceTip + showUnderLineResTip;
        SpannableString ss = new SpannableString(tip);
        int start = showResourceTip.length();
        int end = showUnderLineResTip.length() + start;
        int flag = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
        MyURLSpan mus = new MyURLSpan(view.getContext().getResources().getString(R.string.assetfont_html));// 字体
        mus.setOnClickListener(listener);
        ForegroundColorSpan fcs = new ForegroundColorSpan(view.getContext().getResources()
                .getColor(R.color.lc_color_4ea7f2));
        ss.setSpan(mus, start, end, flag);
        ss.setSpan(fcs, start, end, flag);
        view.setText(ss);
        view.setMovementMethod(LinkMovementMethod.getInstance());
    }
 
    private static DisplayImageOptions mDeviceModeImageOptions;
 
 
    /**
     * 是否为TP1等设备,包括TP1 TC1 TK1 TC3 TK3 TC4 TC5 TC5S TP6、TP6C、TC6、TC6C、TP7
     */
    public static boolean isTp1And(String deviceModelName) {
        return LCConfiguration.TYPE_TC1.equals(deviceModelName) || LCConfiguration.TYPE_TK1.equals(deviceModelName)
                || LCConfiguration.TYPE_TC3.equals(deviceModelName) || LCConfiguration.TYPE_TK3.equals(deviceModelName)
                || LCConfiguration.TYPE_TC4.equals(deviceModelName) || LCConfiguration.TYPE_TC5.equals(deviceModelName)
                || LCConfiguration.TYPE_TC5S.equals(deviceModelName) || LCConfiguration.TYPE_TP1.equals(deviceModelName)
                || LCConfiguration.TYPE_TP6.equals(deviceModelName) || LCConfiguration.TYPE_TP6C.equals(deviceModelName)
                || LCConfiguration.TYPE_TC6.equals(deviceModelName) || LCConfiguration.TYPE_TC6C.equals(deviceModelName)
                || LCConfiguration.TYPE_TP7.equals(deviceModelName);
    }
 
 
    //根据域名获取
    public static String getAddDeviceHelpUrl(String host) {
        if (host.contains(":443")) {
            host = host.split(":")[0];
        }
        return "http://" + host + "/bindhelp.html";
    }
 
 
    /**
     *  * 检测设备新老版本, ture 表示新版本并且DHCP打开走单播流程, false 老版本/DHCP关闭走组播加单播流程。
     *  * @return
     *  
     */
    public static boolean checkDeviceVersion(DeviceInitInfo deviceInfo) {
        if(deviceInfo==null)
            return false;
        int flag = (deviceInfo.mSpecialAbility >> 2) & 0x03;
        return 0x00 != flag && 0x03 != flag;
    }
 
 
    /**
     *  * 检测设备获取的IP是否有效, ture 有效
     *  * @param deviceInfo
     *  * @return
     *  
     */
    public static boolean checkEffectiveIP(DeviceInitInfo deviceInfo) {
        if(deviceInfo==null)
            return false;
        int flag = (deviceInfo.mSpecialAbility >> 2) & 0x03;
        return 0x02 == flag;
    }
 
    /**
     * 生成二维码
     * @param url
     * @param width
     * @param height
     * @return
     */
    public static Bitmap creatQRImage(String url, final int width, final int height) {
        try {
            if(TextUtils.isEmpty(url)) {
                return null;
            }
            Hashtable<EncodeHintType, String> hints = new Hashtable<>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            hints.put(EncodeHintType.MARGIN, "0");  // 不要边距
            // 图像数据变换,使用矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints);
            int[] pixels = new int[width * height];
            // 下面这里按照二维码的算法,逐个生成二维码的图片
            // 两个for循环是图片横列扫描的结果
            for(int y = 0; y < height; y++) {
                for(int x = 0; x <width; x++) {
                    if(bitMatrix.get(x, y)) {
                        pixels[y * width + x] = 0xff000000;
                    } else {
                        pixels[y * width + x] = 0xffffffff;
                    }
                }
            }
            // 生成二维码图片格式,使用ARGB_8888
            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            return bitmap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
}