fix: 修复 HyperOS 3.0 状态栏小图标反色问题 (#248)

* fix: 修复 HyperOS 3.0 状态栏小图标反色问题

Signed-off-by: ghhccghk <2137610394@qq.com>

* style: optimizing code style

---------

Signed-off-by: ghhccghk <2137610394@qq.com>
Co-authored-by: fankesyooni <qzmmcn@163.com>
This commit is contained in:
李太白
2025-09-08 22:22:54 +08:00
committed by GitHub
parent 30fdbdbcb2
commit 97538445e1

View File

@@ -20,10 +20,11 @@
* *
* This file is created by fankes on 2022/3/25. * This file is created by fankes on 2022/3/25.
*/ */
@file:Suppress("StaticFieldLeak", "ConstPropertyName") @file:Suppress("ConstPropertyName")
package com.fankes.miui.notify.hook.entity package com.fankes.miui.notify.hook.entity
import android.annotation.SuppressLint
import android.app.Notification import android.app.Notification
import android.app.NotificationManager import android.app.NotificationManager
import android.app.WallpaperManager import android.app.WallpaperManager
@@ -67,6 +68,7 @@ import com.fankes.miui.notify.utils.factory.isMIOS
import com.fankes.miui.notify.utils.factory.isNotSystemInDarkMode import com.fankes.miui.notify.utils.factory.isNotSystemInDarkMode
import com.fankes.miui.notify.utils.factory.isSystemInDarkMode import com.fankes.miui.notify.utils.factory.isSystemInDarkMode
import com.fankes.miui.notify.utils.factory.isUpperOfAndroidS import com.fankes.miui.notify.utils.factory.isUpperOfAndroidS
import com.fankes.miui.notify.utils.factory.miosVersionCode
import com.fankes.miui.notify.utils.factory.miuiIncrementalVersion import com.fankes.miui.notify.utils.factory.miuiIncrementalVersion
import com.fankes.miui.notify.utils.factory.round import com.fankes.miui.notify.utils.factory.round
import com.fankes.miui.notify.utils.factory.runInSafe import com.fankes.miui.notify.utils.factory.runInSafe
@@ -215,6 +217,7 @@ object SystemUIHooker : YukiBaseHooker() {
private var isUsingCachingMethod = false private var isUsingCachingMethod = false
/** 状态栏通知图标容器 */ /** 状态栏通知图标容器 */
@SuppressLint("StaticFieldLeak")
private var notificationIconContainer: ViewGroup? = null private var notificationIconContainer: ViewGroup? = null
/** 通知栏通知控制器 */ /** 通知栏通知控制器 */
@@ -659,8 +662,10 @@ object SystemUIHooker : YukiBaseHooker() {
/** 获取 [StatusBarNotification] 实例 */ /** 获取 [StatusBarNotification] 实例 */
val notifyInstance = asResolver().optional().firstFieldOrNull { name = "mNotification" }?.get<StatusBarNotification>() ?: return false val notifyInstance = asResolver().optional().firstFieldOrNull { name = "mNotification" }?.get<StatusBarNotification>() ?: return false
val appContext = context.createPackageContext(notifyInstance.nfPkgName, Context.CONTEXT_IGNORE_SECURITY)
/** 获取通知小图标 */ /** 获取通知小图标 */
val iconDrawable = notifyInstance.notification?.smallIcon?.loadDrawable(context) ?: return false val iconDrawable = notifyInstance.notification?.smallIcon?.loadDrawable(appContext) ?: return false
/** 判断是否不是灰度图标 */ /** 判断是否不是灰度图标 */
val isGrayscaleIcon = notifyInstance.isXmsf.not() && isGrayscaleIcon(context, iconDrawable) val isGrayscaleIcon = notifyInstance.isXmsf.not() && isGrayscaleIcon(context, iconDrawable)
@@ -693,6 +698,7 @@ object SystemUIHooker : YukiBaseHooker() {
* @param animColor 动画过渡颜色 * @param animColor 动画过渡颜色
*/ */
private fun updateStatusBarIconColor(iconView: ImageView, isDarkIconMode: Boolean = this.isDarkIconMode, animColor: Int? = null) { private fun updateStatusBarIconColor(iconView: ImageView, isDarkIconMode: Boolean = this.isDarkIconMode, animColor: Int? = null) {
if (miosVersionCode >= 3) return
if (iconView.isGrayscaleIcon()) { if (iconView.isGrayscaleIcon()) {
/** /**
* 防止图标不是纯黑的问题 * 防止图标不是纯黑的问题
@@ -947,11 +953,15 @@ object SystemUIHooker : YukiBaseHooker() {
parameters(Notification::class, Context::class) parameters(Notification::class, Context::class)
}?.hook()?.after { }?.hook()?.after {
val nf = args().first().cast<Notification>() val nf = args().first().cast<Notification>()
val appname = nf?.extras?.getString("miui.opPkg")
val context = args(index = 1).cast<Context>() val context = args(index = 1).cast<Context>()
val iconBitmap = nf?.smallIcon?.loadDrawable(context)?.toBitmap() if (appname != null) {
result = if (context != null && iconBitmap != null && !iconBitmap.isRecycled) val appContext = context?.createPackageContext(appname, Context.CONTEXT_IGNORE_SECURITY)
iconBitmap.toDrawable(context.resources) val iconBitmap = nf.smallIcon?.loadDrawable(appContext)?.toBitmap()
else null result = if (context != null && iconBitmap != null && !iconBitmap.isRecycled)
iconBitmap.toDrawable(context.resources)
else null
}
} }
/** /**
* 强制回写系统的状态栏图标样式为原生 * 强制回写系统的状态栏图标样式为原生
@@ -986,59 +996,61 @@ object SystemUIHooker : YukiBaseHooker() {
} }
} }
} }
/** 焦点通知深色模式切换点 */ if (miosVersionCode < 3) {
FocusedNotifPromptViewClass?.resolve()?.optional()?.apply { /** 焦点通知深色模式切换点 */
firstMethodOrNull { FocusedNotifPromptViewClass?.resolve()?.optional()?.apply {
name = "onDarkChanged" firstMethodOrNull {
parameters(ArrayList::class, Float::class, Int::class, Int::class, Int::class, Boolean::class) name = "onDarkChanged"
}?.hook()?.after { parameters(ArrayList::class, Float::class, Int::class, Int::class, Int::class, Boolean::class)
val isDark = args(index = 1).float() }?.hook()?.after {
val mIcon = firstFieldOrNull { name = "mIcon" }?.of(instance)?.get() val isDark = args(index = 1).float()
if (ConfigData.isEnableModuleLog) val mIcon = firstFieldOrNull { name = "mIcon" }?.of(instance)?.get()
YLog.debug("FocusedNotifPromptView DEBUG $isDark $mIcon") if (ConfigData.isEnableModuleLog)
if (focusedIcon || ConfigData.isEnableFocusNotificationFix) YLog.debug("FocusedNotifPromptView DEBUG $isDark $mIcon")
mIcon?.asResolver()?.optional()?.firstMethodOrNull { if (focusedIcon || ConfigData.isEnableFocusNotificationFix)
name = "setColorFilter" mIcon?.asResolver()?.optional()?.firstMethodOrNull {
superclass() name = "setColorFilter"
}?.invoke(if (isDark <= 0.5f) Color.WHITE else Color.BLACK) superclass()
}?.invoke(if (isDark <= 0.5f) Color.WHITE else Color.BLACK)
}
} }
} /** 去他妈的焦点通知彩色图标 */
/** 去他妈的焦点通知彩色图标 */ FocusUtilsClass?.resolve()?.optional()?.apply {
FocusUtilsClass?.resolve()?.optional()?.apply { fun HookParam.hookTickerDarkIcon(isDark: Boolean) {
fun HookParam.hookTickerDarkIcon(isDark: Boolean) { (globalContext ?: args().first().cast())?.also { context ->
(globalContext ?: args().first().cast())?.also { context -> val expandedNf = args().first().cast<StatusBarNotification?>()
val expandedNf = args().first().cast<StatusBarNotification?>() val small = expandedNf?.notification?.smallIcon
val small = expandedNf?.notification?.smallIcon /** Hook 状态栏小图标 */
/** Hook 状态栏小图标 */ compatStatusIcon(
compatStatusIcon( context = context,
context = context, nf = expandedNf,
nf = expandedNf, iconDrawable = small?.loadDrawable(context)
iconDrawable = small?.loadDrawable(context) ).also { pair ->
).also { pair -> focusedIcon = pair.second
focusedIcon = pair.second val originalBitmap = pair.first?.toBitmap()
val originalBitmap = pair.first?.toBitmap() val bitmap = originalBitmap?.scale(50, 50)
val bitmap = originalBitmap?.scale(50, 50) result = Icon.createWithBitmap(bitmap).apply {
result = Icon.createWithBitmap(bitmap).apply { if (pair.second || ConfigData.isEnableFocusNotificationFix)
if (pair.second || ConfigData.isEnableFocusNotificationFix) setTint(if (isDark) Color.BLACK else Color.WHITE)
setTint(if (isDark) Color.BLACK else Color.WHITE) }
} }
} }
} }
firstMethodOrNull {
name = "getStatusBarTickerDarkIcon"
parameters {
(it.first() == classOf<StatusBarNotification>() ||
it.first() == ExpandedNotificationClass) && it.size == 1
}
}?.hook()?.after { hookTickerDarkIcon(isDark = true) }
firstMethodOrNull {
name = "getStatusBarTickerIcon"
parameters {
(it.first() == classOf<StatusBarNotification>() ||
it.first() == ExpandedNotificationClass) && it.size == 1
}
}?.hook()?.after { hookTickerDarkIcon(isDark = false) }
} }
firstMethodOrNull {
name = "getStatusBarTickerDarkIcon"
parameters {
(it.first() == classOf<StatusBarNotification>() ||
it.first() == ExpandedNotificationClass) && it.size == 1
}
}?.hook()?.after { hookTickerDarkIcon(isDark = true) }
firstMethodOrNull {
name = "getStatusBarTickerIcon"
parameters {
(it.first() == classOf<StatusBarNotification>() ||
it.first() == ExpandedNotificationClass) && it.size == 1
}
}?.hook()?.after { hookTickerDarkIcon(isDark = false) }
} }
/** 注入状态栏通知图标实例 */ /** 注入状态栏通知图标实例 */
StatusBarIconViewClass.resolve().optional().firstMethodOrNull { StatusBarIconViewClass.resolve().optional().firstMethodOrNull {