|
| 1 | +package com.blankj.subutil.util; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.os.Build; |
| 5 | +import android.os.Environment; |
| 6 | +import android.text.TextUtils; |
| 7 | + |
| 8 | +import java.io.BufferedReader; |
| 9 | +import java.io.File; |
| 10 | +import java.io.FileInputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStreamReader; |
| 13 | +import java.lang.reflect.Method; |
| 14 | +import java.util.Properties; |
| 15 | + |
| 16 | +/** |
| 17 | + * <pre> |
| 18 | + * author: Blankj |
| 19 | + * blog : http://blankj.com |
| 20 | + * time : 2018/07/04 |
| 21 | + * desc : utils about rom |
| 22 | + * </pre> |
| 23 | + */ |
| 24 | +public final class RomUtils { |
| 25 | + |
| 26 | + public static final String SYS_EMUI = "emui"; |
| 27 | + public static final String SYS_MIUI = "miui"; |
| 28 | + public static final String SYS_FLYME = "flyme"; |
| 29 | + public static final String SYS_COLOROS = "colorOs"; |
| 30 | + public static final String SYS_FUNTOUCH = "Funtouch"; |
| 31 | + public static final String SYS_SAMSUNG = "samsung"; |
| 32 | + |
| 33 | + /////////////////////////////////////////////////////////////////////////// |
| 34 | + // MIUI |
| 35 | + /////////////////////////////////////////////////////////////////////////// |
| 36 | + private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code"; |
| 37 | + private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name"; |
| 38 | + private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage"; |
| 39 | + private static final String KEY_MIUI_VERSION_INCREMENTAL = "ro.build.version.incremental"; |
| 40 | + |
| 41 | + /////////////////////////////////////////////////////////////////////////// |
| 42 | + // EMUI |
| 43 | + /////////////////////////////////////////////////////////////////////////// |
| 44 | + private static final String KEY_EMUI_API_LEVEL = "ro.build.hw_emui_api_level"; |
| 45 | + private static final String KEY_EMUI_VERSION = "ro.build.version.emui"; |
| 46 | + private static final String KEY_EMUI_CONFIG_HW_SYS_VERSION = "ro.confg.hw_systemversion"; |
| 47 | + |
| 48 | + /////////////////////////////////////////////////////////////////////////// |
| 49 | + // OPPO |
| 50 | + /////////////////////////////////////////////////////////////////////////// |
| 51 | + private static final String KEY_OPPO_NAME = "ro.rom.different.version"; |
| 52 | + private static final String KEY_OPPO_VERSION = "ro.build.version.opporom"; |
| 53 | + |
| 54 | + /////////////////////////////////////////////////////////////////////////// |
| 55 | + // VIVO |
| 56 | + /////////////////////////////////////////////////////////////////////////// |
| 57 | + private static final String KEY_VIVO_NAME = "ro.vivo.os.name"; |
| 58 | + private static final String KEY_VIVO_VERSION = "ro.vivo.os.version"; |
| 59 | + |
| 60 | + private static RomBean bean = null; |
| 61 | + |
| 62 | + /** |
| 63 | + * Return the name of rom. |
| 64 | + * |
| 65 | + * @return the name of rom |
| 66 | + */ |
| 67 | + public static RomBean getRom() { |
| 68 | + if (bean != null) return bean; |
| 69 | + bean = new RomBean(); |
| 70 | + // 小米 |
| 71 | + if (!TextUtils.isEmpty(getSystemProperty(KEY_MIUI_VERSION_CODE)) |
| 72 | + || !TextUtils.isEmpty(getSystemProperty(KEY_MIUI_VERSION_NAME)) |
| 73 | + || !TextUtils.isEmpty(getSystemProperty(KEY_MIUI_INTERNAL_STORAGE))) { |
| 74 | + bean.setRomName(SYS_MIUI); |
| 75 | + bean.setRomVersion(getSystemProperty(KEY_MIUI_VERSION_INCREMENTAL)); |
| 76 | + } |
| 77 | + // 华为 |
| 78 | + else if (!TextUtils.isEmpty(getSystemProperty(KEY_EMUI_API_LEVEL)) |
| 79 | + || !TextUtils.isEmpty(getSystemProperty(KEY_EMUI_VERSION)) |
| 80 | + || !TextUtils.isEmpty(getSystemProperty(KEY_EMUI_CONFIG_HW_SYS_VERSION))) { |
| 81 | + bean.setRomName(SYS_EMUI); |
| 82 | + String version = getSystemProperty(KEY_EMUI_VERSION);// EmotionUI_2.0 |
| 83 | + String[] temp = version.split("_"); |
| 84 | + if (temp.length > 1) { |
| 85 | + bean.setRomVersion(temp[1]); |
| 86 | + } else { |
| 87 | + bean.setRomVersion(version); |
| 88 | + } |
| 89 | + } |
| 90 | + // 魅族 |
| 91 | + else if (Build.DISPLAY.toLowerCase().contains("flyme")) { |
| 92 | + bean.setRomName(SYS_FLYME); |
| 93 | + bean.setRomVersion(Build.DISPLAY); |
| 94 | + return bean; |
| 95 | + } |
| 96 | + // OPPO |
| 97 | + else if (!TextUtils.isEmpty(getSystemProperty(KEY_OPPO_NAME)) && |
| 98 | + getSystemProperty(KEY_OPPO_NAME).toLowerCase().contains("coloros")) { |
| 99 | + bean.setRomName(SYS_COLOROS); |
| 100 | + bean.setRomVersion(getSystemProperty(KEY_OPPO_VERSION)); |
| 101 | + } |
| 102 | + // VIVO |
| 103 | + else if (!TextUtils.isEmpty(getSystemProperty(KEY_VIVO_NAME))) { |
| 104 | + bean.setRomName(SYS_FUNTOUCH); |
| 105 | + bean.setRomVersion(getSystemProperty(KEY_VIVO_VERSION)); |
| 106 | + } |
| 107 | + // 其他手机 |
| 108 | + else { |
| 109 | + String brand = Build.BRAND; |
| 110 | + bean.setRomName(Build.BRAND); |
| 111 | + if (SYS_SAMSUNG.equalsIgnoreCase(brand)) { |
| 112 | + bean.setRomVersion(getSystemProperty("ro.build.changelist")); |
| 113 | + } |
| 114 | + } |
| 115 | + return bean; |
| 116 | + } |
| 117 | + |
| 118 | + private static String getSystemProperty(final String name) { |
| 119 | + String prop = getSystemPropertyByShell(name); |
| 120 | + if (!TextUtils.isEmpty(prop)) return prop; |
| 121 | + prop = getSystemPropertyByStream(name); |
| 122 | + if (!TextUtils.isEmpty(prop)) return prop; |
| 123 | + if (Build.VERSION.SDK_INT < 28) { |
| 124 | + return getSystemPropertyByReflect(name); |
| 125 | + } |
| 126 | + return prop; |
| 127 | + } |
| 128 | + |
| 129 | + private static String getSystemPropertyByShell(final String propName) { |
| 130 | + String line; |
| 131 | + BufferedReader input = null; |
| 132 | + try { |
| 133 | + Process p = Runtime.getRuntime().exec("getprop " + propName); |
| 134 | + input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024); |
| 135 | + return input.readLine(); |
| 136 | + } catch (IOException e) { |
| 137 | + return ""; |
| 138 | + } finally { |
| 139 | + if (input != null) { |
| 140 | + try { |
| 141 | + input.close(); |
| 142 | + } catch (IOException ignore) { |
| 143 | + |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private static String getSystemPropertyByStream(final String key) { |
| 150 | + try { |
| 151 | + Properties prop = new Properties(); |
| 152 | + FileInputStream is = new FileInputStream( |
| 153 | + new File(Environment.getRootDirectory(), "build.prop") |
| 154 | + ); |
| 155 | + prop.load(is); |
| 156 | + return prop.getProperty(key, ""); |
| 157 | + } catch (Exception e) { |
| 158 | + e.printStackTrace(); |
| 159 | + return ""; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private static String getSystemPropertyByReflect(String key) { |
| 164 | + try { |
| 165 | + @SuppressLint("PrivateApi") |
| 166 | + Class<?> clz = Class.forName("android.os.SystemProperties"); |
| 167 | + Method get = clz.getMethod("get", String.class, String.class); |
| 168 | + return (String) get.invoke(clz, key, ""); |
| 169 | + } catch (Exception e) { |
| 170 | + return ""; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + public static class RomBean { |
| 175 | + private String romName; |
| 176 | + private String romVersion; |
| 177 | + |
| 178 | + public String getRomName() { |
| 179 | + if (romName == null) return ""; |
| 180 | + return romName; |
| 181 | + } |
| 182 | + |
| 183 | + private void setRomName(String romName) { |
| 184 | + this.romName = romName; |
| 185 | + } |
| 186 | + |
| 187 | + public String getRomVersion() { |
| 188 | + if (romVersion == null) return ""; |
| 189 | + return romVersion; |
| 190 | + } |
| 191 | + |
| 192 | + private void setRomVersion(String romVersion) { |
| 193 | + this.romVersion = romVersion; |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public String toString() { |
| 198 | + return "romName: " + romName + |
| 199 | + "\nromVersion: " + romVersion; |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments