-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHardwarex.java
237 lines (216 loc) · 9.04 KB
/
Hardwarex.java
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
/*
* Copyright (C) 2018 Peng fei Pan <panpfpanpf@outlook.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.panpf.androidx.hardware;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresPermission;
import android.telephony.TelephonyManager;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import me.panpf.androidx.content.Contextx;
import me.panpf.javax.lang.Stringx;
import me.panpf.javax.util.Collectionx;
import me.panpf.javax.util.Predicate;
import me.panpf.javax.util.Transformer;
@SuppressWarnings("WeakerAccess")
public class Hardwarex {
private Hardwarex() {
}
@NonNull
public static String getProduct() {
return Stringx.orEmpty(Build.PRODUCT);
}
@NonNull
public static String getBrand() {
return Stringx.orEmpty(Build.BRAND);
}
@NonNull
public static String getModel() {
return Stringx.orEmpty(Build.MODEL);
}
@NonNull
public static String getDevice() {
return Stringx.orEmpty(Build.DEVICE);
}
@NonNull
public static String getHardware() {
return Stringx.orEmpty(Build.HARDWARE);
}
@NonNull
public static String[] getSupportedAbis() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return Build.SUPPORTED_ABIS;
} else {
//noinspection deprecation
return Collectionx.filter(Collectionx.listOf(Build.CPU_ABI, Build.CPU_ABI2), new Predicate<String>() {
@Override
public boolean accept(@NonNull String s) {
return Stringx.isNotEmpty(s) && !"unknown".equals(s);
}
}).toArray(new String[0]);
}
} catch (Throwable e) {
e.printStackTrace();
return new String[0];
}
}
@NonNull
@SuppressLint({"HardwareIds", "InlinedApi"})
@RequiresPermission(anyOf = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_SMS, Manifest.permission.READ_PHONE_NUMBERS})
public static String getPhoneNumber(@NonNull Context context) {
try {
TelephonyManager manager = Contextx.telephonyManagerOrNull(context);
return Stringx.isNotSafeOr(manager != null ? manager.getLine1Number() : null, "unknown");
} catch (Throwable e) {
e.printStackTrace();
return Stringx.isNotSafeOr(e instanceof SecurityException ? "PermissionDenied" : null, "unknown");
}
}
@NonNull
@SuppressLint({"HardwareIds", "MissingPermission"})
@RequiresPermission(Manifest.permission.READ_PHONE_STATE)
public static String getDeviceId(@NonNull Context context) {
try {
TelephonyManager manager = Contextx.telephonyManagerOrNull(context);
return Stringx.isNotSafeOr(manager != null ? (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? manager.getImei() : manager.getDeviceId()) : null, "unknown");
} catch (Throwable e) {
e.printStackTrace();
return Stringx.isNotSafeOr(e instanceof SecurityException ? "PermissionDenied" : null, "unknown");
}
}
@NonNull
@SuppressLint("HardwareIds")
public static String getAndroidId(@NonNull Context context) {
return Stringx.isNotSafeOr(Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID), "unknown");
}
/**
* 获取国际移动用户识别码
*/
@NonNull
@SuppressLint({"HardwareIds", "MissingPermission"})
@RequiresPermission(Manifest.permission.READ_PHONE_STATE)
public static String getSubscriberId(@NonNull Context context) {
try {
TelephonyManager manager = Contextx.telephonyManagerOrNull(context);
return Stringx.isNotSafeOr(manager != null ? manager.getSubscriberId() : null, "unknown");
} catch (Throwable e) {
e.printStackTrace();
return Stringx.isNotSafeOr(e instanceof SecurityException ? "PermissionDenied" : null, "unknown");
}
}
/**
* 获取 SIM 卡序列号
*/
@NonNull
@SuppressLint({"HardwareIds", "MissingPermission"})
@RequiresPermission(Manifest.permission.READ_PHONE_STATE)
public static String getSimSerialNumber(@NonNull Context context) {
try {
TelephonyManager manager = Contextx.telephonyManagerOrNull(context);
return Stringx.isNotSafeOr(manager != null ? manager.getSimSerialNumber() : null, "unknown");
} catch (Throwable e) {
e.printStackTrace();
return Stringx.isNotSafeOr(e instanceof SecurityException ? "PermissionDenied" : null, "unknown");
}
}
@NonNull
@SuppressLint({"MissingPermission", "HardwareIds"})
@RequiresPermission(Manifest.permission.READ_PHONE_STATE)
public static String getSerial() {
try {
return Stringx.isNotSafeOr(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? Build.getSerial() : Build.SERIAL, "unknown");
} catch (Throwable e) {
e.printStackTrace();
return Stringx.isNotSafeOr(e instanceof SecurityException ? "PermissionDenied" : null, "unknown");
}
}
/**
* 获取国际移动设备身份码
*/
@NonNull
@SuppressLint({"HardwareIds", "MissingPermission"})
@RequiresPermission(Manifest.permission.READ_PHONE_STATE)
public static String getIMEI(@NonNull Context context) {
return getDeviceId(context);
}
/**
* 获取国际移动用户识别码
*/
@NonNull
@SuppressLint({"HardwareIds", "MissingPermission"})
@RequiresPermission(Manifest.permission.READ_PHONE_STATE)
public static String getIMSI(@NonNull Context context) {
return getSubscriberId(context);
}
/**
* 获取 MAC 地址
*/
@NonNull
@SuppressLint({"HardwareIds", "MissingPermission"})
@RequiresPermission(Manifest.permission.ACCESS_WIFI_STATE)
public static String getMacAddress(@NonNull Context context) {
String macAddress = null;
try {
WifiManager wifiManager = Contextx.wifiManagerOrNull(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Enumeration<NetworkInterface> interfaceEnumeration = NetworkInterface.getNetworkInterfaces();
List<NetworkInterface> nis = interfaceEnumeration != null ? Collections.list(interfaceEnumeration) : null;
if (nis != null && nis.size() > 0) {
NetworkInterface networkInterface = Collectionx.find(nis, new Predicate<NetworkInterface>() {
@Override
public boolean accept(@NonNull NetworkInterface networkInterface) {
return networkInterface.getName().equalsIgnoreCase("wlan0");
}
});
byte[] address = networkInterface != null ? networkInterface.getHardwareAddress() : null;
if (address != null) {
List<Byte> byteList = new LinkedList<>();
for (byte by : address) {
byteList.add(by);
}
macAddress = Collectionx.joinToString(byteList, ":", null, null, -1, null, new Transformer<Byte, CharSequence>() {
@NonNull
@Override
public CharSequence transform(@NonNull Byte by) {
return Integer.toHexString(by & 0xFF);
}
});
}
} else {
WifiInfo wifiInfo = wifiManager != null ? wifiManager.getConnectionInfo() : null;
macAddress = wifiInfo != null ? wifiInfo.getMacAddress() : null;
}
} else {
WifiInfo wifiInfo = wifiManager != null ? wifiManager.getConnectionInfo() : null;
macAddress = wifiInfo != null ? wifiInfo.getMacAddress() : null;
}
} catch (Exception e) {
e.printStackTrace();
macAddress = e instanceof SecurityException ? "PermissionDenied" : null;
}
return macAddress != null ? macAddress : "02:00:00:00:00:00";
}
}