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

fix: make RomUtils compatible with HarmonyOS #1735

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 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 @@ -12,6 +12,8 @@
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* <pre>
Expand All @@ -24,6 +26,7 @@
public final class RomUtils {

private static final String[] ROM_HUAWEI = {"huawei"};
private static final String[] ROM_HARMONY = {"harmony"};
private static final String[] ROM_VIVO = {"vivo"};
private static final String[] ROM_XIAOMI = {"xiaomi"};
private static final String[] ROM_OPPO = {"oppo"};
Expand Down Expand Up @@ -70,6 +73,15 @@ public static boolean isHuawei() {
return ROM_HUAWEI[0].equals(getRomInfo().name);
}

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

/**
* Return whether the rom is made by vivo.
*
Expand Down Expand Up @@ -251,6 +263,11 @@ public static RomInfo getRomInfo() {
bean = new RomInfo();
final String brand = getBrand();
final String manufacturer = getManufacturer();
if (isHarmonyOS()) {
sInfo.name = ROM_HARMONY[0];
sInfo.version = getHarmonyOSVersion();
return sInfo;
}
if (isRightRom(brand, manufacturer, ROM_HUAWEI)) {
bean.name = ROM_HUAWEI[0];
String version = getRomVersion(VERSION_PROPERTY_HUAWEI);
Expand Down Expand Up @@ -435,6 +452,26 @@ private static String getSystemPropertyByReflect(String key) {
return "";
}

private static boolean isHarmonyOS() {
try {
Class<?> clz = Class.forName("com.huawei.system.BuildEx");
Method method = clz.getMethod("getOsBrand");
return "harmony".equals(method.invoke(clz));
} catch (Throwable unused) {
return false;
}
}

private static String getHarmonyOSVersion() {
String version = getRomVersion("");
Matcher matcher = Pattern.compile("\\d+\\.\\d+\\.\\d+[\\d.]*").matcher(version);
if (matcher.find()) {
return matcher.group();
} else {
return version;
}
}

public static class RomInfo {
private String name;
private String version;
Expand Down