Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[M] RomUtils 增加鸿蒙和荣耀判断 #1803

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.blankj.common.activity.CommonActivity
import com.blankj.common.item.CommonItem
import com.blankj.common.item.CommonItemTitle
import com.blankj.utilcode.pkg.R
import com.blankj.utilcode.util.CollectionUtils
import com.blankj.utilcode.util.RomUtils

/**
Expand All @@ -32,9 +31,12 @@ class RomActivity : CommonActivity() {

override fun bindItems(): MutableList<CommonItem<*>> {
val romInfo = RomUtils.getRomInfo()
return CollectionUtils.newArrayList(
CommonItemTitle("Rom Name", romInfo.name),
CommonItemTitle("Rom Version", romInfo.version)
)
return mutableListOf<CommonItem<*>>().apply {
if (RomUtils.isHarmonyOS()) {
add(CommonItemTitle("Harmony OS", "YES"))
}
add(CommonItemTitle("Rom Name", romInfo.name))
add(CommonItemTitle("Rom Version", romInfo.version))
}
}
}
2 changes: 2 additions & 0 deletions lib/utilcode/README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,8 @@ readRaw2List : 从 raw 中按行读取字符串
* ### Rom 相关 -> [RomUtils.java][rom.java] -> [Demo][rom.demo]
```
isHuawei : 是否华为
isHarmonyOS: 是否鸿蒙系统
isHonor : 是否荣耀
isVivo : 是否 VIVO
isXiaomi : 是否小米
isOppo : 是否 OPPO
Expand Down
2 changes: 2 additions & 0 deletions lib/utilcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,8 @@ readRaw2List
* ### About Rom -> [RomUtils.java][rom.java] -> [Demo][rom.demo]
```
isHuawei
isHarmonyOS
isHonor
isVivo
isXiaomi
isOppo
Expand Down
45 changes: 45 additions & 0 deletions lib/utilcode/src/main/java/com/blankj/utilcode/util/RomUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
public final class RomUtils {

private static final String[] ROM_HUAWEI = {"huawei"};
private static final String[] ROM_HARMONY = {"harmony"};
private static final String[] ROM_HONOR = {"honor"};
private static final String[] ROM_VIVO = {"vivo"};
private static final String[] ROM_XIAOMI = {"xiaomi"};
private static final String[] ROM_OPPO = {"oppo"};
Expand All @@ -45,6 +47,8 @@ public final class RomUtils {
private static final String[] ROM_MOTOROLA = {"motorola"};

private static final String VERSION_PROPERTY_HUAWEI = "ro.build.version.emui";
private static final String VERSION_PROPERTY_HONOR = "ro.honor.build.display.id";
private static final String VERSION_PROPERTY_HARMONY = "hw_sc.build.platform.version";
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";
Expand All @@ -70,6 +74,23 @@ public static boolean isHuawei() {
return ROM_HUAWEI[0].equals(getRomInfo().name);
}

/**
* Return whether the rom is harmony OS.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isHarmonyOS() {
return ROM_HARMONY[0].equals(getRomInfo().name);
}

/**
* Return whether the rom is made by honor.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isHonor() {
return ROM_HONOR[0].equals(getRomInfo().name);
}

/**
* Return whether the rom is made by vivo.
*
Expand Down Expand Up @@ -251,6 +272,11 @@ public static RomInfo getRomInfo() {
bean = new RomInfo();
final String brand = getBrand();
final String manufacturer = getManufacturer();
if (checkIsHarmonyOs()) {
bean.name = ROM_HARMONY[0];
bean.version = getRomVersion(VERSION_PROPERTY_HARMONY);
return bean;
}
if (isRightRom(brand, manufacturer, ROM_HUAWEI)) {
bean.name = ROM_HUAWEI[0];
String version = getRomVersion(VERSION_PROPERTY_HUAWEI);
Expand All @@ -267,6 +293,11 @@ public static RomInfo getRomInfo() {
bean.version = getRomVersion(VERSION_PROPERTY_VIVO);
return bean;
}
if (isRightRom(brand, manufacturer, ROM_HONOR)) {
bean.name = ROM_HONOR[0];
bean.version = getRomVersion(VERSION_PROPERTY_HONOR);
return bean;
}
if (isRightRom(brand, manufacturer, ROM_XIAOMI)) {
bean.name = ROM_XIAOMI[0];
bean.version = getRomVersion(VERSION_PROPERTY_XIAOMI);
Expand Down Expand Up @@ -342,6 +373,20 @@ private static boolean isRightRom(final String brand, final String manufacturer,
return false;
}

/**
* Return whether the rom is harmony OS.
*
* @return {@code true}: yes<br>{@code false}: no
*/
private static boolean checkIsHarmonyOs() {
try {
Class<?> buildExClass = Class.forName("com.huawei.system.BuildEx");
Object osBrand = buildExClass.getMethod("getOsBrand").invoke(buildExClass);
return osBrand != null && ROM_HARMONY[0].equalsIgnoreCase(osBrand.toString());
} catch (Throwable ignore) {/**/}
return false;
}

private static String getManufacturer() {
try {
String manufacturer = Build.MANUFACTURER;
Expand Down