Skip to content

Commit

Permalink
chore: Xiaomi devices can restart systemui without root
Browse files Browse the repository at this point in the history
  • Loading branch information
YuKongA committed Jan 26, 2025
1 parent 1b6f449 commit 64495b6
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 38 deletions.
23 changes: 12 additions & 11 deletions app/src/main/kotlin/statusbar/lyric/hook/module/SystemUILyric.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.graphics.toColorInt
import cn.lyric.getter.api.data.ExtraData
import cn.lyric.getter.api.data.LyricData
import cn.lyric.getter.api.listener.LyricListener
Expand Down Expand Up @@ -86,17 +87,17 @@ import statusbar.lyric.tools.Tools.getObjectField
import statusbar.lyric.tools.Tools.getObjectFieldIfExist
import statusbar.lyric.tools.Tools.goMainThread
import statusbar.lyric.tools.Tools.ifNotNull
import statusbar.lyric.tools.Tools.isHyperOS
import statusbar.lyric.tools.Tools.isLandscape
import statusbar.lyric.tools.Tools.isNot
import statusbar.lyric.tools.Tools.isNotNull
import statusbar.lyric.tools.Tools.isNull
import statusbar.lyric.tools.Tools.isPad
import statusbar.lyric.tools.Tools.isTargetView
import statusbar.lyric.tools.Tools.isXiaomi
import statusbar.lyric.tools.Tools.observableChange
import statusbar.lyric.tools.Tools.setObjectField
import statusbar.lyric.tools.Tools.shell
import statusbar.lyric.tools.XiaomiUtils.isHyperOS
import statusbar.lyric.tools.XiaomiUtils.isXiaomi
import statusbar.lyric.view.LyricSwitchView
import statusbar.lyric.view.TitleDialog
import java.io.File
Expand Down Expand Up @@ -183,7 +184,7 @@ class SystemUILyric : BaseHook() {
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
if (config.lyricGradientColor.isNotEmpty()) {
config.lyricGradientColor.trim().split(",").map { Color.parseColor(it.trim()) }.let { colors ->
config.lyricGradientColor.trim().split(",").map { it.trim().toColorInt() }.let { colors ->
if (colors.isEmpty()) {
setTextColor(Color.WHITE)
} else if (colors.size < 2) {
Expand Down Expand Up @@ -731,8 +732,8 @@ class SystemUILyric : BaseHook() {
val blurRadio = config.mHyperOSTextureRadio
val cornerRadius = cornerRadius(config.mHyperOSTextureCorner.toFloat())
val blendModes = arrayOf(
intArrayOf(106, Color.parseColor(config.mHyperOSTextureBgColor)),
intArrayOf(3, Color.parseColor(config.mHyperOSTextureBgColor))
intArrayOf(106, config.mHyperOSTextureBgColor.toColorInt()),
intArrayOf(3, config.mHyperOSTextureBgColor.toColorInt())
)
lyricLayout.setBackgroundBlur(blurRadio, cornerRadius, blendModes)
}
Expand Down Expand Up @@ -907,7 +908,7 @@ class SystemUILyric : BaseHook() {
if (config.lyricColor.isEmpty()) {
setTextColor(clockView.currentTextColor)
} else {
setTextColor(Color.parseColor(config.lyricColor))
setTextColor(config.lyricColor.toColorInt())
}
}
setLetterSpacings(config.lyricLetterSpacing / 100f)
Expand All @@ -919,13 +920,13 @@ class SystemUILyric : BaseHook() {
setBackgroundColor(Color.TRANSPARENT)
background = GradientDrawable().apply {
cornerRadius = config.lyricBackgroundRadius.toFloat()
setColor(Color.parseColor(config.lyricBackgroundColor))
setColor(config.lyricBackgroundColor.toColorInt())
}
} else {
setBackgroundColor(Color.parseColor(config.lyricBackgroundColor))
setBackgroundColor(config.lyricBackgroundColor.toColorInt())
}
} else {
config.lyricBackgroundColor.trim().split(",").map { Color.parseColor(it.trim()) }
config.lyricBackgroundColor.trim().split(",").map { it.trim().toColorInt() }
.let { colors ->
val gradientDrawable = GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT, colors.toIntArray()
Expand Down Expand Up @@ -977,12 +978,12 @@ class SystemUILyric : BaseHook() {
if (config.iconColor.isEmpty()) {
setColorFilter(clockView.currentTextColor, PorterDuff.Mode.SRC_IN)
} else {
setColorFilter(Color.parseColor(config.iconColor), PorterDuff.Mode.SRC_IN)
setColorFilter(config.iconColor.toColorInt(), PorterDuff.Mode.SRC_IN)
}
if (config.iconBgColor.isEmpty()) {
setBackgroundColor(Color.TRANSPARENT)
} else {
setBackgroundColor(Color.parseColor(config.iconBgColor))
setBackgroundColor(config.iconBgColor.toColorInt())
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/kotlin/statusbar/lyric/tools/ActivityTools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import statusbar.lyric.R
import statusbar.lyric.data.Data
import statusbar.lyric.tools.LogTools.log
import kotlin.system.exitProcess
import androidx.core.graphics.toColorInt
import androidx.core.net.toUri

@SuppressLint("StaticFieldLeak")
object ActivityTools {
Expand Down Expand Up @@ -73,7 +75,7 @@ object ActivityTools {
unit(default)
} else {
try {
Color.parseColor(value)
value.toColorInt()
} catch (_: Exception) {
showToastOnLooper(context.getString(R.string.color_error))
return
Expand All @@ -89,7 +91,7 @@ object ActivityTools {
try {
value.split(",").forEach {
if (it.isNotEmpty()) {
Color.parseColor(it.trim())
it.trim().toColorInt()
}
}
} catch (_: Exception) {
Expand All @@ -109,7 +111,7 @@ object ActivityTools {
}

fun openUrl(url: String) {
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
context.startActivity(Intent(Intent.ACTION_VIEW, url.toUri()))
}

fun restartApp() {
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/kotlin/statusbar/lyric/tools/LyricViewTools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import android.view.animation.ScaleAnimation
import android.view.animation.TranslateAnimation
import android.widget.ImageView
import statusbar.lyric.view.LyricSwitchView
import androidx.core.view.isGone
import androidx.core.view.isVisible

object LyricViewTools {
private var animaList: ArrayList<Int> = arrayListOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Expand Down Expand Up @@ -113,7 +115,7 @@ object LyricViewTools {
}

fun View.hideView(anim: Boolean = true) {
if (visibility == View.GONE) return
if (isGone) return
if (anim) {
val alphaAnimation = getAlphaAnimation(false).apply {
setAnimationListener(object : Animation.AnimationListener {
Expand All @@ -132,7 +134,7 @@ object LyricViewTools {
}

fun View.showView() {
if (visibility == View.VISIBLE) return
if (isVisible) return
val alphaAnimation = getAlphaAnimation(true).apply {
setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation) {}
Expand Down
18 changes: 2 additions & 16 deletions app/src/main/kotlin/statusbar/lyric/tools/Tools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import android.content.Context
import android.content.SharedPreferences
import android.content.res.Configuration
import android.icu.text.SimpleDateFormat
import android.icu.util.TimeZone
import android.os.Build
import android.os.Handler
import android.os.Looper
Expand Down Expand Up @@ -55,23 +54,10 @@ object Tools {

private var index: Int = 0

val buildTime = SimpleDateFormat("yyyy/M/d HH:mm:ss", Locale.CHINA).apply {
timeZone = TimeZone.getTimeZone("UTC+8")
}.format(BuildConfig.BUILD_TIME)

val isXiaomi by lazy { isPresent("android.provider.MiuiSettings") }
val buildTime: String = SimpleDateFormat("yyyy/M/d H:m:s", Locale.CHINA).format(BuildConfig.BUILD_TIME)

val isPad by lazy { getSystemProperties("ro.build.characteristics") == "tablet" }

val isHyperOS by lazy {
try {
getSystemProperties("ro.mi.os.version.incremental")
Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE
} catch (_: Exception) {
false
}
}

val getPhoneName by lazy {
val xiaomiMarketName = getSystemProperties("ro.product.marketname")
val vivoMarketName = getSystemProperties("ro.vivo.market.name")
Expand All @@ -92,7 +78,7 @@ object Tools {
fun dp2px(context: Context, dpValue: Float): Int =
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.resources.displayMetrics).toInt()

private fun isPresent(name: String): Boolean {
internal fun isPresent(name: String): Boolean {
return try {
Objects.requireNonNull(Thread.currentThread().contextClassLoader).loadClass(name)
true
Expand Down
39 changes: 39 additions & 0 deletions app/src/main/kotlin/statusbar/lyric/tools/XiaomiUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package statusbar.lyric.tools

import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import statusbar.lyric.BuildConfig
import statusbar.lyric.tools.Tools.getSystemProperties
import statusbar.lyric.tools.Tools.isPresent

object XiaomiUtils {

val isXiaomi by lazy { isPresent("android.provider.MiuiSettings") }

val isHyperOS by lazy {
try {
getSystemProperties("ro.mi.os.version.incremental")
Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE
} catch (_: Exception) {
false
}
}

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
@SuppressLint("WrongConstant")
fun restartXiaomiSystemUI(context: Context) {
val bundle = Bundle().apply {
putString("package_name", BuildConfig.APPLICATION_ID)
putString("strong_toast_category", "text_bitmap")
putString("param", "{\"a\":{\"a\":{\"a\":\"a\"}}}") // 哎嘿,包崩溃的
putString("status_bar_strong_toast", "show_custom_strong_toast")
}
val service = context.getSystemService(Context.STATUS_BAR_SERVICE)
service.javaClass.getMethod("setStatus", Int::class.javaPrimitiveType, String::class.java, Bundle::class.java)
.invoke(service, 1, "strong_toast_action", bundle)
}

}
11 changes: 8 additions & 3 deletions app/src/main/kotlin/statusbar/lyric/ui/page/MenuPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ import statusbar.lyric.tools.Tools
import statusbar.lyric.tools.Tools.bigTextOne
import statusbar.lyric.tools.Tools.buildTime
import statusbar.lyric.tools.Tools.getPhoneName
import statusbar.lyric.tools.XiaomiUtils
import statusbar.lyric.tools.XiaomiUtils.isHyperOS
import statusbar.lyric.tools.XiaomiUtils.isXiaomi
import top.yukonga.miuix.kmp.basic.BasicComponent
import top.yukonga.miuix.kmp.basic.BasicComponentDefaults
import top.yukonga.miuix.kmp.basic.ButtonDefaults
Expand Down Expand Up @@ -287,7 +290,6 @@ fun MenuPage(navController: NavController, currentStartDestination: MutableState
ResetDialog(showDialog = showResetDialog)
}


@Composable
fun ResetDialog(showDialog: MutableState<Boolean>) {
SuperDialog(
Expand Down Expand Up @@ -326,7 +328,6 @@ fun ResetDialog(showDialog: MutableState<Boolean>) {
}
}


@Composable
fun RestartDialog(showDialog: MutableState<Boolean>) {
SuperDialog(
Expand All @@ -344,7 +345,11 @@ fun RestartDialog(showDialog: MutableState<Boolean>) {
modifier = Modifier.weight(1f),
text = stringResource(R.string.ok),
onClick = {
Tools.shell("killall com.android.systemui", true)
if (isXiaomi && isHyperOS && Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
XiaomiUtils.restartXiaomiSystemUI(context)
} else {
Tools.shell("killall com.android.systemui", true)
}
dismissDialog(showDialog)
}
)
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/kotlin/statusbar/lyric/view/TitleDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import statusbar.lyric.R
import statusbar.lyric.config.XposedOwnSP.config
import statusbar.lyric.tools.LyricViewTools
import statusbar.lyric.tools.Tools.dp2px
import androidx.core.graphics.toColorInt
import androidx.core.view.isVisible

@SuppressLint("InternalInsetResource", "DiscouragedApi")
class TitleDialog(context: Context) : Dialog(context) {
Expand Down Expand Up @@ -86,8 +88,8 @@ class TitleDialog(context: Context) : Dialog(context) {
gravity = Gravity.CENTER
background = GradientDrawable().apply {
cornerRadius = config.titleBackgroundRadius.toFloat()
setColor(Color.parseColor(config.titleColorAndTransparency))
setStroke(config.titleBackgroundStrokeWidth, Color.parseColor(config.titleBackgroundStrokeColorAndTransparency))
setColor(config.titleColorAndTransparency.toColorInt())
setStroke(config.titleBackgroundStrokeWidth, config.titleBackgroundStrokeColorAndTransparency.toColorInt())
}
setPadding(40, 5, 40, 5)
}
Expand Down Expand Up @@ -136,7 +138,7 @@ class TitleDialog(context: Context) : Dialog(context) {

fun showTitle(title: String) {
setTitle(title)
if (root.visibility == View.VISIBLE) {
if (root.isVisible) {
delayedHide()
return
}
Expand Down

0 comments on commit 64495b6

Please sign in to comment.