Rom 检测

由于 Android 机型繁多,存在着各种定制的ROM,所以在开发应用的过程中,时常需要针对不同的ROM进行适配。

对于深度定制的ROM通常检查/system/build.prop文件中的某些自定义字段(比如ROM版本名)是否存在来判断ROM类型。

一些常见机型的 build.prop文件
https://github.com/czy1121/settingscompat/tree/master/roms

对于未深度定制的系统,可能没有明显的自定义字段,这时直接用 Build.MANUFACTURER 来判断。

代码如下,检查是否某类型ROM,获取 ROM名称RomUtil.getName(),版本名RomUtil.getVersion()

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
public class RomUtil {
private static final String TAG = "RomUtil";
public static final String ROM_MIUI = "MIUI";
public static final String ROM_EMUI = "EMUI";
public static final String ROM_FLYME = "FLYME";
public static final String ROM_OPPO = "OPPO";
public static final String ROM_SMARTISAN = "SMARTISAN";
public static final String ROM_VIVO = "VIVO";
public static final String ROM_QIKU = "QIKU";
public static final String ROM_LENOVO = "LENOVO";
public static final String ROM_SAMSUNG = "SAMSUNG";
private static final String KEY_VERSION_MIUI = "ro.miui.ui.version.name";
private static final String KEY_VERSION_EMUI = "ro.build.version.emui";
private static final String KEY_VERSION_OPPO = "ro.build.version.opporom";
private static final String KEY_VERSION_SMARTISAN = "ro.smartisan.version";
private static final String KEY_VERSION_VIVO = "ro.vivo.os.version";
private static final String KEY_VERSION_GIONEE = "ro.gn.sv.version";
private static final String KEY_VERSION_LENOVO = "ro.lenovo.lvp.version";
private static final String KEY_VERSION_FLYME = "ro.build.display.id";
private static final String KEY_EMUI_VERSION_CODE = "ro.build.hw_emui_api_level";
private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
private static final String KEY_MIUI_HANDY_MODE_SF = "ro.miui.has_handy_mode_sf";
private static final String KEY_MIUI_REAL_BLUR = "ro.miui.has_real_blur";
private static final String KEY_FLYME_PUBLISHED = "ro.flyme.published";
private static final String KEY_FLYME_FLYME = "ro.meizu.setupwizard.flyme";
private static final String KEY_FLYME_ICON_FALG = "persist.sys.use.flyme.icon";
private static final String KEY_FLYME_SETUP_FALG = "ro.meizu.setupwizard.flyme";
private static final String KEY_FLYME_PUBLISH_FALG = "ro.flyme.published";
private static final String KEY_VIVO_OS_NAME = "ro.vivo.os.name";
private static final String KEY_VIVO_OS_VERSION = "ro.vivo.os.version";
private static final String KEY_VIVO_ROM_VERSION = "ro.vivo.rom.version";
public static boolean isEmui() {
return check(ROM_EMUI);
}
public static boolean isMiui() {
return check(ROM_MIUI);
}
public static boolean isVivo() {
return check(ROM_VIVO);
}
public static boolean isOppo() {
return check(ROM_OPPO);
}
public static boolean isFlyme() {
return check(ROM_FLYME);
}
public static boolean isQiku() {
return check(ROM_QIKU) || check("360");
}
public static boolean isSmartisan() {
return check(ROM_SMARTISAN);
}
private static String sName;
public static String getName() {
if (sName == null) {
check("");
}
return sName;
}
private static String sVersion;
public static String getVersion() {
if (sVersion == null) {
check("");
}
return sVersion;
}
public static boolean check(String rom) {
if (sName != null) {
return sName.equals(rom);
}
if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_MIUI))) {
sName = ROM_MIUI;
} else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_EMUI))){
sName = ROM_EMUI;
} else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_OPPO))){
sName = ROM_OPPO;
} else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_VIVO))){
sName = ROM_VIVO;
} else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_SMARTISAN))){
sName = ROM_SMARTISAN;
} else {
sVersion = Build.DISPLAY;
if (sVersion.toUpperCase().contains(ROM_FLYME)) {
sName = ROM_FLYME;
} else {
sVersion = Build.UNKNOWN;
sName = Build.MANUFACTURER.toUpperCase();
}
}
return sName.equals(rom);
}
public static String getProp(String name) {
String line = null;
BufferedReader input = null;
try {
Process p = Runtime.getRuntime().exec("getprop " + name);
input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
line = input.readLine();
input.close();
} catch (IOException ex) {
Log.e(TAG, "Unable to read prop " + name, ex);
return null;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return line;
}
// private static Properties props = new Properties();
// static {
// try {
// props.load(new FileInputStream(new File("/system/build.prop")));
// } catch (IOException e) {
//
// }
// }
// public static String getProp(String name) {
// return props.getProperty(name, Build.UNKNOWN);
// }
}
欢迎打赏,谢谢支持~