From 33b3cf6abced4683fdb6d7c774bf82503f9460ca Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Sat, 28 Jan 2023 14:35:07 +0800 Subject: [PATCH] Fix status bar icon color in some MIUI like MIPUSH notification cannot reverse color problem in SystemUIHooker --- .../miui/notify/hook/entity/SystemUIHooker.kt | 87 ++++++++++++++----- 1 file changed, 67 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/fankes/miui/notify/hook/entity/SystemUIHooker.kt b/app/src/main/java/com/fankes/miui/notify/hook/entity/SystemUIHooker.kt index edf08c5..c4d62db 100644 --- a/app/src/main/java/com/fankes/miui/notify/hook/entity/SystemUIHooker.kt +++ b/app/src/main/java/com/fankes/miui/notify/hook/entity/SystemUIHooker.kt @@ -61,6 +61,7 @@ import com.highcapable.yukihookapi.hook.log.loggerD import com.highcapable.yukihookapi.hook.log.loggerW import com.highcapable.yukihookapi.hook.type.android.* import com.highcapable.yukihookapi.hook.type.java.BooleanType +import com.highcapable.yukihookapi.hook.type.java.FloatType import com.highcapable.yukihookapi.hook.type.java.IntType import top.defaults.drawabletoolbox.DrawableBuilder @@ -88,6 +89,9 @@ object SystemUIHooker : YukiBaseHooker() { private const val NotificationChildrenContainerClass = "$SYSTEMUI_PACKAGE_NAME.statusbar.notification.stack.NotificationChildrenContainer" + /** 原生存在的类 */ + private const val NotificationIconAreaControllerClass = "$SYSTEMUI_PACKAGE_NAME.statusbar.phone.NotificationIconAreaController" + /** 原生存在的类 */ private const val ContrastColorUtilClass = "com.android.internal.util.ContrastColorUtil" @@ -146,6 +150,9 @@ object SystemUIHooker : YukiBaseHooker() { /** 缓存的通知图标优化数组 */ private var iconDatas = ArrayList() + /** 当前是否处于深色图标模式 - 跟随 Hook 保存 */ + private var isDarkIconMode = false + /** 是否显示通知图标 - 跟随 Hook 保存 */ private var isShowNotificationIcons = true @@ -542,6 +549,30 @@ object SystemUIHooker : YukiBaseHooker() { } ?: true.also { printLogcat(tag = "IconColor", context, expandedNf = null, isCustom = false, isGrayscale = false) } } + /** + * 更新状态栏通知图标颜色 + * @param container 当前通知图标容器实例 + * @param isDarkIconMode 是否为深色图标模式 + */ + private fun updateStatusBarIconColor(container: ViewGroup, isDarkIconMode: Boolean = this.isDarkIconMode) { + if (container.childCount > 0) container.children.forEach { + (it as? ImageView?)?.also { iconView -> + val notification = iconView.current().field { name = "mNotification" }.cast() + if (hasIgnoreStatusBarIconColor(iconView.context, notification)) { + iconView.alpha = 1f + iconView.colorFilter = null + } else { + /** + * 防止图标不是纯黑的问题 + * 图标在任何场景下跟随状态栏其它图标保持半透明 + */ + iconView.alpha = if (isDarkIconMode) 0.8f else 0.95f + iconView.setColorFilter(if (isDarkIconMode) Color.BLACK else Color.WHITE) + } + } + } + } + /** * 从 [NotificationViewWrapperClass] 中获取 [ExpandableNotificationRowClass] * @return [Pair] - ([Boolean] 通知是否展开,[Any] 通知 Row 实例) @@ -686,33 +717,49 @@ object SystemUIHooker : YukiBaseHooker() { } } } - /** 注入状态栏通知图标实例 */ - StatusBarIconViewClass.hook { - /** Hook 状态栏通知图标的颜色 */ + /** 注入状态栏通知图标容器管理实例 */ + NotificationIconAreaControllerClass.hook { + /** Hook 深色图标模式改变 */ injectMember { method { - name { it == "updateDecorColor" || it == "updateIconColor" } - emptyParam() - }.all() + name = "onDarkChanged" + param(RectClass, FloatType, IntType) + } afterHook { - instance().also { - val notification = field { name = "mNotification" }.get(it).cast() - val currentSetColor = field { name = "mCurrentSetColor" }.get(it).int() - if (hasIgnoreStatusBarIconColor(it.context, notification)) { - it.alpha = 1f - it.colorFilter = null - } else { - /** - * 防止图标不是纯黑的问题 - * 图标在任何场景下跟随状态栏其它图标保持半透明 - * MIUI 11、12 进行单独判断 - */ - it.alpha = if (currentSetColor.isWhiteColor) 0.95f else 0.8f - it.setColorFilter(if (currentSetColor.isWhiteColor) currentSetColor else Color.BLACK) + field { name = "mNotificationIcons" }.get(instance).cast()?.also { + /** 重新设置通知图标容器实例 */ + notificationIconContainer = it + when (args(index = 1).float()) { + 1.0f -> { + isDarkIconMode = true + updateStatusBarIconColor(it, isDarkIconMode = true) + } + 0.0f -> { + isDarkIconMode = false + updateStatusBarIconColor(it, isDarkIconMode = false) + } + else -> updateStatusBarIconColor(it, isDarkIconMode = false) } } } } + /** Hook 更新通知图标事件 */ + injectMember { + method { + name = "updateNotificationIcons" + paramCount = 1 + } + afterHook { + field { name = "mNotificationIcons" }.get(instance).cast()?.also { + /** 重新设置通知图标容器实例 */ + notificationIconContainer = it + updateStatusBarIconColor(it) + } + } + } + } + /** 注入状态栏通知图标实例 */ + StatusBarIconViewClass.hook { /** 注册广播 */ injectMember { method {