Skip to content

Commit 7aa8dc1

Browse files
authored
opt: 优化设备名称获取,代码来自 StatusBarLyric (#258)
Block-Network/StatusBarLyric@1696d67
1 parent 802983e commit 7aa8dc1

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

app/src/main/kotlin/cn/lyric/getter/tool/Tools.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.lyric.getter.tool
22

3+
import android.annotation.SuppressLint
34
import android.content.Context
45
import android.content.pm.PackageInfo
56
import android.os.Build
@@ -10,6 +11,18 @@ import java.io.DataOutputStream
1011
object Tools {
1112
var xpActivation: Boolean = false
1213

14+
val getPhoneName by lazy {
15+
val marketName = getSystemProperties("ro.product.marketname")
16+
if (marketName.isNotEmpty()) bigtextone(marketName) else bigtextone(Build.BRAND) + " " + Build.MODEL
17+
}
18+
19+
fun bigtextone(st:String): String {
20+
val formattedBrand = st.replaceFirstChar {
21+
if (it.isLowerCase()) it.titlecase() else it.toString()
22+
}
23+
return formattedBrand
24+
}
25+
1326
fun restartTheScopedSoftware(context: Context) {
1427
var s = ""
1528
context.resources.getStringArray(R.array.need_module).forEach {
@@ -44,4 +57,16 @@ object Tools {
4457
@Suppress("DEPRECATION")
4558
versionCode
4659
}
60+
61+
@SuppressLint("PrivateApi")
62+
fun getSystemProperties(key: String): String {
63+
val ret: String = try {
64+
Class.forName("android.os.SystemProperties").getDeclaredMethod("get", String::class.java).invoke(null, key) as String
65+
} catch (iAE: IllegalArgumentException) {
66+
throw iAE
67+
} catch (e: Exception) {
68+
""
69+
}
70+
return ret
71+
}
4772
}

app/src/main/kotlin/cn/lyric/getter/ui/fragment/HomeFragment.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import cn.lyric.getter.databinding.FragmentHomeBinding
1919
import cn.lyric.getter.tool.ActivityTools.getAppRules
2020
import cn.lyric.getter.tool.BackupTools
2121
import cn.lyric.getter.tool.ConfigTools.config
22+
import cn.lyric.getter.tool.Tools.getPhoneName
2223
import cn.lyric.getter.tool.Tools.restartTheScopedSoftware
2324
import cn.lyric.getter.ui.adapter.NoticeAdapter
2425
import cn.lyric.getter.ui.viewmodel.HomeViewModel
@@ -149,7 +150,7 @@ class HomeFragment : Fragment() {
149150
}
150151
}
151152
}
152-
deviceValue.text = "${Build.BRAND} ${Build.MODEL} Android${Build.VERSION.RELEASE}"
153+
deviceValue.text = "$getPhoneName (${Build.DEVICE}) Android ${Build.VERSION.RELEASE}"
153154
versionLabelValue.text = BuildConfig.VERSION_NAME
154155
versionCodeValue.text = BuildConfig.VERSION_CODE.toString()
155156
versionTypeValue.text = BuildConfig.BUILD_TYPE.toUpperFirstCaseAndLowerOthers()

0 commit comments

Comments
 (0)