-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceUtil.java
More file actions
executable file
·101 lines (83 loc) · 2.97 KB
/
DeviceUtil.java
File metadata and controls
executable file
·101 lines (83 loc) · 2.97 KB
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
package example.base.util;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import java.security.MessageDigest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DeviceUtil {
public static int SCREEN_WIDTH = 0;
public static boolean isConnected(Context context) {
if (context == null) {
return false;
}
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
}
public static void hideSoftKeyboard(Activity activity) {
if (activity == null) {
return;
}
// Check if no view has focus:
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
public static boolean isXiaoMi() {
return android.os.Build.MANUFACTURER.trim().toLowerCase().equals("xiaomi");
}
public static DisplayMetrics getDisplayMetrics(Context context) {
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
return metrics;
}
public static boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
public static boolean isPhoneValid(String s)
{
String regex ="^[89]\\d{7}$";
try {
Pattern patt = Pattern.compile(regex);
Matcher matcher = patt.matcher(s);
return matcher.matches();
} catch (RuntimeException e) {
return false;
}
}
public static String generateDeviceUUID(Context context) {
String serial = android.os.Build.SERIAL;
String androidID = Settings.Secure.ANDROID_ID;
String deviceUUID = serial + androidID;
/*
* SHA-1
*/
MessageDigest digest;
byte[] result;
try {
digest = MessageDigest.getInstance("SHA-1");
result = digest.digest(deviceUUID.getBytes("UTF-8"));
} catch (Exception e) {
e.printStackTrace();
return null;
}
StringBuilder sb = new StringBuilder();
for (byte b : result) {
sb.append(String.format("%02X", b));
}
return sb.toString();
}
}