git 3 weeks ago
parent
commit
7aeca20691

+ 99 - 0
src/main/kotlin/cn/qqck/kotlin/tools/Android.kt

@@ -0,0 +1,99 @@
+package cn.qqck.kotlin.tools
+
+object Android {
+    data class Info(
+        /**
+         * 手机正式名称、手机品牌
+         * - ro.product.name
+         * - cmi
+         */
+        val name: String,
+        /**
+         * 手机品牌
+         * - ro.product.brand
+         * - Xiaomi
+         */
+        val brand: String,
+        /**
+         * 手机型号
+         * - ro.product.model
+         * - M2001J1E
+         */
+        val model: String,
+        /**
+         * 设备名
+         * - ro.product.device
+         * - cmi
+         */
+        val device: String,
+        /**
+         * 制造商
+         * - ro.product.manufacturer
+         * - Xiaomi
+         */
+        val manufacturer: String,
+        /**
+         * 系统版本
+         * - Build.VERSION.RELEASE
+         * - 10、11
+         */
+        val version: String,
+        /**
+         * 设备首次使用的 API 级别(Android 版本)
+         * - ro.product.firstApiLevel
+         * - Android(29、32、33)
+         */
+        val firstApiLevel: String,
+        /**
+         * GPU 供应商
+         * - ro.gpu.vendor
+         * - Qualcomm
+         */
+        val gpu_vendor: String,
+        /**
+         * GPU 型号
+         * - ro.gpu.renderer
+         * - Adreno (TM) 650
+         */
+        val gpu_renderer: String,
+        /**
+         * 运行内存
+         * - 8、12
+         */
+        val ram: Int,
+        /**
+         * 手机平台信息
+         * - Linux aarch64、Linux armv8l
+         */
+        val platfrom: String,
+    )
+
+    private val array = arrayOf(
+        Info(
+            "cmi", "Xiaomi", "M2001J1E", "cmi", "Xiaomi", "10", "29",
+            "Qualcomm", "Adreno (TM) 650", 8, "Linux aarch64",
+        ),
+        Info(
+            "cmi", "Xiaomi", "M2001J1E", "cmi", "Xiaomi", "10", "29",
+            "Qualcomm", "Adreno (TM) 650", 12, "Linux aarch64",
+        ),
+        Info(
+            "nuwa", "Xiaomi", "2210132C", "nuwa", "Xiaomi", "13", "33",
+            "Qualcomm", "Adreno (TM) 740", 8, "Linux aarch64",
+        ),
+        Info(
+            "nuwa", "Xiaomi", "2210132C", "nuwa", "Xiaomi", "13", "33",
+            "Qualcomm", "Adreno (TM) 740", 12, "Linux aarch64",
+        ),
+        Info(
+            "shennong", "Xiaomi", "23116PN5BC", "shennong", "Xiaomi", "13", "33",
+            "Qualcomm", "Adreno (TM) 750", 12, "Linux aarch64",
+        ),
+        Info(
+            "shennong", "Xiaomi", "23116PN5BC", "shennong", "Xiaomi", "13", "33",
+            "Qualcomm", "Adreno (TM) 750", 16, "Linux aarch64",
+        ),
+    )
+
+    fun random(): Info = array.random()
+}

+ 12 - 7
src/main/kotlin/cn/qqck/kotlin/tools/String.kt

@@ -175,20 +175,25 @@ fun String?.isIpv6(): Boolean {
 }
 
 /**
- * 隐藏字符串中间一部分字符,用于部分信息的脱敏显示
+ * 隐藏字符串的中间部分字符,保持前后指定长度字符的可见性
  *
- * @param hs 替代被隐藏字符的字符,默认为 '*'。
- * @param pre_len 保留的前缀字符个数,默认为 2。
- * @param suf_len 保留的后缀字符个数,默认为 2。
- * @return 隐藏中间部分字符后的字符串。当字符串长度小于等于前缀长度和后缀长度之和时,原样返回字符串。
+ * @param hs 用于替代隐藏字符部分的字符,默认为 '*'。
+ * @param pre_len 前缀保留的可见字符数,默认为2。
+ * @param suf_len 后缀保留的可见字符数,默认为2。
+ * @param hs_max 最大隐藏字符数目,若为null,则默认隐藏中间所有指定部分字符。
+ * @return 返回处理后的字符串,包含可见的前后缀字符与隐藏字符部分。
  */
-fun String.hide(hs: Char = '*', pre_len: Int = 2, suf_len: Int = 2): String {
+fun String.hide(hs: Char = '*', pre_len: Int = 2, suf_len: Int = 2, hs_max: Int?): String {
     val j_slen = this.length
     if (j_slen <= pre_len + suf_len) return this
     val j_midlen = j_slen - pre_len - suf_len
     val j_shide = CharArray(j_slen)
     for (j_i in 0 until pre_len) j_shide[j_i] = this[j_i]
-    for (j_i in pre_len until pre_len + j_midlen) j_shide[j_i] = hs
+    if (hs_max == null || j_midlen < hs_max) {
+        for (j_i in pre_len until pre_len + j_midlen) j_shide[j_i] = hs
+    } else {
+        for (j_i in pre_len until pre_len + hs_max) j_shide[j_i] = hs
+    }
     for (j_i in j_slen - suf_len until j_slen) j_shide[j_i] = this[j_i]
     return String(j_shide)
 }