-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathRomUtils.java
449 lines (406 loc) · 14.2 KB
/
RomUtils.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package com.dds.common.utils;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.Properties;
public final class RomUtils {
private static final String[] ROM_HUAWEI = {"huawei"};
private static final String[] ROM_VIVO = {"vivo"};
private static final String[] ROM_XIAOMI = {"xiaomi"};
private static final String[] ROM_OPPO = {"oppo"};
private static final String[] ROM_LEECO = {"leeco", "letv"};
private static final String[] ROM_360 = {"360", "qiku"};
private static final String[] ROM_ZTE = {"zte"};
private static final String[] ROM_ONEPLUS = {"oneplus"};
private static final String[] ROM_NUBIA = {"nubia"};
private static final String[] ROM_COOLPAD = {"coolpad", "yulong"};
private static final String[] ROM_LG = {"lg", "lge"};
private static final String[] ROM_GOOGLE = {"google"};
private static final String[] ROM_SAMSUNG = {"samsung"};
private static final String[] ROM_MEIZU = {"meizu"};
private static final String[] ROM_LENOVO = {"lenovo"};
private static final String[] ROM_SMARTISAN = {"smartisan", "deltainno"};
private static final String[] ROM_HTC = {"htc"};
private static final String[] ROM_SONY = {"sony"};
private static final String[] ROM_GIONEE = {"gionee", "amigo"};
private static final String[] ROM_MOTOROLA = {"motorola"};
private static final String VERSION_PROPERTY_HUAWEI = "ro.build.version.emui";
private static final String VERSION_PROPERTY_VIVO = "ro.vivo.os.build.display.id";
private static final String VERSION_PROPERTY_XIAOMI = "ro.build.version.incremental";
private static final String VERSION_PROPERTY_OPPO = "ro.build.version.opporom";
private static final String VERSION_PROPERTY_LEECO = "ro.letv.release.version";
private static final String VERSION_PROPERTY_360 = "ro.build.uiversion";
private static final String VERSION_PROPERTY_ZTE = "ro.build.MiFavor_version";
private static final String VERSION_PROPERTY_ONEPLUS = "ro.rom.version";
private static final String VERSION_PROPERTY_NUBIA = "ro.build.rom.id";
private final static String UNKNOWN = "unknown";
private static RomInfo bean = null;
private RomUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Return whether the rom is made by huawei.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isHuawei() {
return ROM_HUAWEI[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by vivo.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isVivo() {
return ROM_VIVO[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by xiaomi.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isXiaomi() {
return ROM_XIAOMI[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by oppo.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isOppo() {
return ROM_OPPO[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by leeco.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isLeeco() {
return ROM_LEECO[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by 360.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean is360() {
return ROM_360[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by zte.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isZte() {
return ROM_ZTE[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by oneplus.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isOneplus() {
return ROM_ONEPLUS[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by nubia.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isNubia() {
return ROM_NUBIA[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by coolpad.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isCoolpad() {
return ROM_COOLPAD[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by lg.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isLg() {
return ROM_LG[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by google.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isGoogle() {
return ROM_GOOGLE[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by samsung.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isSamsung() {
return ROM_SAMSUNG[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by meizu.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isMeizu() {
return ROM_MEIZU[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by lenovo.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isLenovo() {
return ROM_LENOVO[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by smartisan.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isSmartisan() {
return ROM_SMARTISAN[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by htc.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isHtc() {
return ROM_HTC[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by sony.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isSony() {
return ROM_SONY[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by gionee.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isGionee() {
return ROM_GIONEE[0].equals(getRomInfo().name);
}
/**
* Return whether the rom is made by motorola.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isMotorola() {
return ROM_MOTOROLA[0].equals(getRomInfo().name);
}
/**
* Return the rom's information.
*
* @return the rom's information
*/
public static RomInfo getRomInfo() {
if (bean != null) return bean;
bean = new RomInfo();
final String brand = getBrand();
final String manufacturer = getManufacturer();
if (isRightRom(brand, manufacturer, ROM_HUAWEI)) {
bean.name = ROM_HUAWEI[0];
String version = getRomVersion(VERSION_PROPERTY_HUAWEI);
String[] temp = version.split("_");
if (temp.length > 1) {
bean.version = temp[1];
} else {
bean.version = version;
}
return bean;
}
if (isRightRom(brand, manufacturer, ROM_VIVO)) {
bean.name = ROM_VIVO[0];
bean.version = getRomVersion(VERSION_PROPERTY_VIVO);
return bean;
}
if (isRightRom(brand, manufacturer, ROM_XIAOMI)) {
bean.name = ROM_XIAOMI[0];
bean.version = getRomVersion(VERSION_PROPERTY_XIAOMI);
return bean;
}
if (isRightRom(brand, manufacturer, ROM_OPPO)) {
bean.name = ROM_OPPO[0];
bean.version = getRomVersion(VERSION_PROPERTY_OPPO);
return bean;
}
if (isRightRom(brand, manufacturer, ROM_LEECO)) {
bean.name = ROM_LEECO[0];
bean.version = getRomVersion(VERSION_PROPERTY_LEECO);
return bean;
}
if (isRightRom(brand, manufacturer, ROM_360)) {
bean.name = ROM_360[0];
bean.version = getRomVersion(VERSION_PROPERTY_360);
return bean;
}
if (isRightRom(brand, manufacturer, ROM_ZTE)) {
bean.name = ROM_ZTE[0];
bean.version = getRomVersion(VERSION_PROPERTY_ZTE);
return bean;
}
if (isRightRom(brand, manufacturer, ROM_ONEPLUS)) {
bean.name = ROM_ONEPLUS[0];
bean.version = getRomVersion(VERSION_PROPERTY_ONEPLUS);
return bean;
}
if (isRightRom(brand, manufacturer, ROM_NUBIA)) {
bean.name = ROM_NUBIA[0];
bean.version = getRomVersion(VERSION_PROPERTY_NUBIA);
return bean;
}
if (isRightRom(brand, manufacturer, ROM_COOLPAD)) {
bean.name = ROM_COOLPAD[0];
} else if (isRightRom(brand, manufacturer, ROM_LG)) {
bean.name = ROM_LG[0];
} else if (isRightRom(brand, manufacturer, ROM_GOOGLE)) {
bean.name = ROM_GOOGLE[0];
} else if (isRightRom(brand, manufacturer, ROM_SAMSUNG)) {
bean.name = ROM_SAMSUNG[0];
} else if (isRightRom(brand, manufacturer, ROM_MEIZU)) {
bean.name = ROM_MEIZU[0];
} else if (isRightRom(brand, manufacturer, ROM_LENOVO)) {
bean.name = ROM_LENOVO[0];
} else if (isRightRom(brand, manufacturer, ROM_SMARTISAN)) {
bean.name = ROM_SMARTISAN[0];
} else if (isRightRom(brand, manufacturer, ROM_HTC)) {
bean.name = ROM_HTC[0];
} else if (isRightRom(brand, manufacturer, ROM_SONY)) {
bean.name = ROM_SONY[0];
} else if (isRightRom(brand, manufacturer, ROM_GIONEE)) {
bean.name = ROM_GIONEE[0];
} else if (isRightRom(brand, manufacturer, ROM_MOTOROLA)) {
bean.name = ROM_MOTOROLA[0];
} else {
bean.name = manufacturer;
}
bean.version = getRomVersion("");
return bean;
}
private static boolean isRightRom(final String brand, final String manufacturer, final String... names) {
for (String name : names) {
if (brand.contains(name) || manufacturer.contains(name)) {
return true;
}
}
return false;
}
private static String getManufacturer() {
try {
String manufacturer = Build.MANUFACTURER;
if (!TextUtils.isEmpty(manufacturer)) {
return manufacturer.toLowerCase();
}
} catch (Throwable ignore) {/**/}
return UNKNOWN;
}
private static String getBrand() {
try {
String brand = Build.BRAND;
if (!TextUtils.isEmpty(brand)) {
return brand.toLowerCase();
}
} catch (Throwable ignore) {/**/}
return UNKNOWN;
}
private static String getRomVersion(final String propertyName) {
String ret = "";
if (!TextUtils.isEmpty(propertyName)) {
ret = getSystemProperty(propertyName);
}
if (TextUtils.isEmpty(ret) || ret.equals(UNKNOWN)) {
try {
String display = Build.DISPLAY;
if (!TextUtils.isEmpty(display)) {
ret = display.toLowerCase();
}
} catch (Throwable ignore) {/**/}
}
if (TextUtils.isEmpty(ret)) {
return UNKNOWN;
}
return ret;
}
private static String getSystemProperty(final String name) {
String prop = getSystemPropertyByShell(name);
if (!TextUtils.isEmpty(prop)) return prop;
prop = getSystemPropertyByStream(name);
if (!TextUtils.isEmpty(prop)) return prop;
if (Build.VERSION.SDK_INT < 28) {
return getSystemPropertyByReflect(name);
}
return prop;
}
private static String getSystemPropertyByShell(final String propName) {
String line;
BufferedReader input = null;
try {
Process p = Runtime.getRuntime().exec("getprop " + propName);
input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
String ret = input.readLine();
if (ret != null) {
return ret;
}
} catch (IOException ignore) {
} finally {
if (input != null) {
try {
input.close();
} catch (IOException ignore) {/**/}
}
}
return "";
}
private static String getSystemPropertyByStream(final String key) {
try {
Properties prop = new Properties();
FileInputStream is = new FileInputStream(
new File(Environment.getRootDirectory(), "build.prop")
);
prop.load(is);
return prop.getProperty(key, "");
} catch (Exception ignore) {/**/}
return "";
}
private static String getSystemPropertyByReflect(String key) {
try {
@SuppressLint("PrivateApi")
Class<?> clz = Class.forName("android.os.SystemProperties");
Method getMethod = clz.getMethod("get", String.class, String.class);
return (String) getMethod.invoke(clz, key, "");
} catch (Exception e) {/**/}
return "";
}
public static class RomInfo {
private String name;
private String version;
public String getName() {
return name;
}
public String getVersion() {
return version;
}
@Override
public String toString() {
return "RomInfo{name=" + name +
", version=" + version + "}";
}
}
}