Skip to content

Commit

Permalink
opt: 优化设备名称获取,代码来自 StatusBarLyric (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghhccghk authored Dec 30, 2024
1 parent 802983e commit 7aa8dc1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
25 changes: 25 additions & 0 deletions app/src/main/kotlin/cn/lyric/getter/tool/Tools.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.lyric.getter.tool

import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageInfo
import android.os.Build
Expand All @@ -10,6 +11,18 @@ import java.io.DataOutputStream
object Tools {
var xpActivation: Boolean = false

val getPhoneName by lazy {
val marketName = getSystemProperties("ro.product.marketname")
if (marketName.isNotEmpty()) bigtextone(marketName) else bigtextone(Build.BRAND) + " " + Build.MODEL
}

fun bigtextone(st:String): String {
val formattedBrand = st.replaceFirstChar {
if (it.isLowerCase()) it.titlecase() else it.toString()
}
return formattedBrand
}

fun restartTheScopedSoftware(context: Context) {
var s = ""
context.resources.getStringArray(R.array.need_module).forEach {
Expand Down Expand Up @@ -44,4 +57,16 @@ object Tools {
@Suppress("DEPRECATION")
versionCode
}

@SuppressLint("PrivateApi")
fun getSystemProperties(key: String): String {
val ret: String = try {
Class.forName("android.os.SystemProperties").getDeclaredMethod("get", String::class.java).invoke(null, key) as String
} catch (iAE: IllegalArgumentException) {
throw iAE
} catch (e: Exception) {
""
}
return ret
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import cn.lyric.getter.databinding.FragmentHomeBinding
import cn.lyric.getter.tool.ActivityTools.getAppRules
import cn.lyric.getter.tool.BackupTools
import cn.lyric.getter.tool.ConfigTools.config
import cn.lyric.getter.tool.Tools.getPhoneName
import cn.lyric.getter.tool.Tools.restartTheScopedSoftware
import cn.lyric.getter.ui.adapter.NoticeAdapter
import cn.lyric.getter.ui.viewmodel.HomeViewModel
Expand Down Expand Up @@ -149,7 +150,7 @@ class HomeFragment : Fragment() {
}
}
}
deviceValue.text = "${Build.BRAND} ${Build.MODEL} Android${Build.VERSION.RELEASE}"
deviceValue.text = "$getPhoneName (${Build.DEVICE}) Android ${Build.VERSION.RELEASE}"
versionLabelValue.text = BuildConfig.VERSION_NAME
versionCodeValue.text = BuildConfig.VERSION_CODE.toString()
versionTypeValue.text = BuildConfig.BUILD_TYPE.toUpperFirstCaseAndLowerOthers()
Expand Down

0 comments on commit 7aa8dc1

Please sign in to comment.