From 954e9013e5bb90137622eb5af4267df4835819d9 Mon Sep 17 00:00:00 2001
From: manman <manman@micous.com>
Date: Tue, 26 Mar 2024 11:50:20 +0800
Subject: [PATCH] =?UTF-8?q?[M]=20RomUtils=20=E5=A2=9E=E5=8A=A0=E9=B8=BF?=
 =?UTF-8?q?=E8=92=99=E5=92=8C=E8=8D=A3=E8=80=80=E5=88=A4=E6=96=AD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../utilcode/pkg/feature/rom/RomActivity.kt   | 12 ++---
 lib/utilcode/README-CN.md                     |  2 +
 lib/utilcode/README.md                        |  2 +
 .../com/blankj/utilcode/util/RomUtils.java    | 45 +++++++++++++++++++
 4 files changed, 56 insertions(+), 5 deletions(-)

diff --git a/feature/utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/rom/RomActivity.kt b/feature/utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/rom/RomActivity.kt
index e2b4a3c7bc..e7b89a760e 100644
--- a/feature/utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/rom/RomActivity.kt
+++ b/feature/utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/rom/RomActivity.kt
@@ -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
 
 /**
@@ -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))
+        }
     }
 }
diff --git a/lib/utilcode/README-CN.md b/lib/utilcode/README-CN.md
index 6610b3be99..b69673dd13 100644
--- a/lib/utilcode/README-CN.md
+++ b/lib/utilcode/README-CN.md
@@ -915,6 +915,8 @@ readRaw2List       : 从 raw 中按行读取字符串
 * ### Rom 相关 -> [RomUtils.java][rom.java] -> [Demo][rom.demo]
 ```
 isHuawei   : 是否华为
+isHarmonyOS: 是否鸿蒙系统
+isHonor    : 是否荣耀
 isVivo     : 是否 VIVO
 isXiaomi   : 是否小米
 isOppo     : 是否 OPPO
diff --git a/lib/utilcode/README.md b/lib/utilcode/README.md
index d8f5c13bcb..ae69f23721 100644
--- a/lib/utilcode/README.md
+++ b/lib/utilcode/README.md
@@ -914,6 +914,8 @@ readRaw2List
 * ### About Rom -> [RomUtils.java][rom.java] -> [Demo][rom.demo]
 ```
 isHuawei
+isHarmonyOS
+isHonor
 isVivo
 isXiaomi
 isOppo
diff --git a/lib/utilcode/src/main/java/com/blankj/utilcode/util/RomUtils.java b/lib/utilcode/src/main/java/com/blankj/utilcode/util/RomUtils.java
index 49e1cf6fcf..13323bc1a1 100644
--- a/lib/utilcode/src/main/java/com/blankj/utilcode/util/RomUtils.java
+++ b/lib/utilcode/src/main/java/com/blankj/utilcode/util/RomUtils.java
@@ -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"};
@@ -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";
@@ -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.
      *
@@ -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);
@@ -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);
@@ -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;