Skip to content

Commit 033e94e

Browse files
ghhccghkfankes
andauthored
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>
1 parent c54d11b commit 033e94e

1 file changed

Lines changed: 66 additions & 54 deletions

File tree

app/src/main/java/com/fankes/miui/notify/hook/entity/SystemUIHooker.kt

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
*
2121
* This file is created by fankes on 2022/3/25.
2222
*/
23-
@file:Suppress("StaticFieldLeak", "ConstPropertyName")
23+
@file:Suppress("ConstPropertyName")
2424

2525
package com.fankes.miui.notify.hook.entity
2626

27+
import android.annotation.SuppressLint
2728
import android.app.Notification
2829
import android.app.NotificationManager
2930
import android.app.WallpaperManager
@@ -67,6 +68,7 @@ import com.fankes.miui.notify.utils.factory.isMIOS
6768
import com.fankes.miui.notify.utils.factory.isNotSystemInDarkMode
6869
import com.fankes.miui.notify.utils.factory.isSystemInDarkMode
6970
import com.fankes.miui.notify.utils.factory.isUpperOfAndroidS
71+
import com.fankes.miui.notify.utils.factory.miosVersionCode
7072
import com.fankes.miui.notify.utils.factory.miuiIncrementalVersion
7173
import com.fankes.miui.notify.utils.factory.round
7274
import com.fankes.miui.notify.utils.factory.runInSafe
@@ -215,6 +217,7 @@ object SystemUIHooker : YukiBaseHooker() {
215217
private var isUsingCachingMethod = false
216218

217219
/** 状态栏通知图标容器 */
220+
@SuppressLint("StaticFieldLeak")
218221
private var notificationIconContainer: ViewGroup? = null
219222

220223
/** 通知栏通知控制器 */
@@ -659,8 +662,10 @@ object SystemUIHooker : YukiBaseHooker() {
659662
/** 获取 [StatusBarNotification] 实例 */
660663
val notifyInstance = asResolver().optional().firstFieldOrNull { name = "mNotification" }?.get<StatusBarNotification>() ?: return false
661664

665+
val appContext = context.createPackageContext(notifyInstance.nfPkgName, Context.CONTEXT_IGNORE_SECURITY)
666+
662667
/** 获取通知小图标 */
663-
val iconDrawable = notifyInstance.notification?.smallIcon?.loadDrawable(context) ?: return false
668+
val iconDrawable = notifyInstance.notification?.smallIcon?.loadDrawable(appContext) ?: return false
664669

665670
/** 判断是否不是灰度图标 */
666671
val isGrayscaleIcon = notifyInstance.isXmsf.not() && isGrayscaleIcon(context, iconDrawable)
@@ -693,6 +698,7 @@ object SystemUIHooker : YukiBaseHooker() {
693698
* @param animColor 动画过渡颜色
694699
*/
695700
private fun updateStatusBarIconColor(iconView: ImageView, isDarkIconMode: Boolean = this.isDarkIconMode, animColor: Int? = null) {
701+
if (miosVersionCode >= 3) return
696702
if (iconView.isGrayscaleIcon()) {
697703
/**
698704
* 防止图标不是纯黑的问题
@@ -947,11 +953,15 @@ object SystemUIHooker : YukiBaseHooker() {
947953
parameters(Notification::class, Context::class)
948954
}?.hook()?.after {
949955
val nf = args().first().cast<Notification>()
956+
val appname = nf?.extras?.getString("miui.opPkg")
950957
val context = args(index = 1).cast<Context>()
951-
val iconBitmap = nf?.smallIcon?.loadDrawable(context)?.toBitmap()
952-
result = if (context != null && iconBitmap != null && !iconBitmap.isRecycled)
953-
iconBitmap.toDrawable(context.resources)
954-
else null
958+
if (appname != null) {
959+
val appContext = context?.createPackageContext(appname, Context.CONTEXT_IGNORE_SECURITY)
960+
val iconBitmap = nf.smallIcon?.loadDrawable(appContext)?.toBitmap()
961+
result = if (context != null && iconBitmap != null && !iconBitmap.isRecycled)
962+
iconBitmap.toDrawable(context.resources)
963+
else null
964+
}
955965
}
956966
/**
957967
* 强制回写系统的状态栏图标样式为原生
@@ -986,59 +996,61 @@ object SystemUIHooker : YukiBaseHooker() {
986996
}
987997
}
988998
}
989-
/** 焦点通知深色模式切换点 */
990-
FocusedNotifPromptViewClass?.resolve()?.optional()?.apply {
991-
firstMethodOrNull {
992-
name = "onDarkChanged"
993-
parameters(ArrayList::class, Float::class, Int::class, Int::class, Int::class, Boolean::class)
994-
}?.hook()?.after {
995-
val isDark = args(index = 1).float()
996-
val mIcon = firstFieldOrNull { name = "mIcon" }?.of(instance)?.get()
997-
if (ConfigData.isEnableModuleLog)
998-
YLog.debug("FocusedNotifPromptView DEBUG $isDark $mIcon")
999-
if (focusedIcon || ConfigData.isEnableFocusNotificationFix)
1000-
mIcon?.asResolver()?.optional()?.firstMethodOrNull {
1001-
name = "setColorFilter"
1002-
superclass()
1003-
}?.invoke(if (isDark <= 0.5f) Color.WHITE else Color.BLACK)
999+
if (miosVersionCode < 3) {
1000+
/** 焦点通知深色模式切换点 */
1001+
FocusedNotifPromptViewClass?.resolve()?.optional()?.apply {
1002+
firstMethodOrNull {
1003+
name = "onDarkChanged"
1004+
parameters(ArrayList::class, Float::class, Int::class, Int::class, Int::class, Boolean::class)
1005+
}?.hook()?.after {
1006+
val isDark = args(index = 1).float()
1007+
val mIcon = firstFieldOrNull { name = "mIcon" }?.of(instance)?.get()
1008+
if (ConfigData.isEnableModuleLog)
1009+
YLog.debug("FocusedNotifPromptView DEBUG $isDark $mIcon")
1010+
if (focusedIcon || ConfigData.isEnableFocusNotificationFix)
1011+
mIcon?.asResolver()?.optional()?.firstMethodOrNull {
1012+
name = "setColorFilter"
1013+
superclass()
1014+
}?.invoke(if (isDark <= 0.5f) Color.WHITE else Color.BLACK)
1015+
}
10041016
}
1005-
}
1006-
/** 去他妈的焦点通知彩色图标 */
1007-
FocusUtilsClass?.resolve()?.optional()?.apply {
1008-
fun HookParam.hookTickerDarkIcon(isDark: Boolean) {
1009-
(globalContext ?: args().first().cast())?.also { context ->
1010-
val expandedNf = args().first().cast<StatusBarNotification?>()
1011-
val small = expandedNf?.notification?.smallIcon
1012-
/** Hook 状态栏小图标 */
1013-
compatStatusIcon(
1014-
context = context,
1015-
nf = expandedNf,
1016-
iconDrawable = small?.loadDrawable(context)
1017-
).also { pair ->
1018-
focusedIcon = pair.second
1019-
val originalBitmap = pair.first?.toBitmap()
1020-
val bitmap = originalBitmap?.scale(50, 50)
1021-
result = Icon.createWithBitmap(bitmap).apply {
1022-
if (pair.second || ConfigData.isEnableFocusNotificationFix)
1023-
setTint(if (isDark) Color.BLACK else Color.WHITE)
1017+
/** 去他妈的焦点通知彩色图标 */
1018+
FocusUtilsClass?.resolve()?.optional()?.apply {
1019+
fun HookParam.hookTickerDarkIcon(isDark: Boolean) {
1020+
(globalContext ?: args().first().cast())?.also { context ->
1021+
val expandedNf = args().first().cast<StatusBarNotification?>()
1022+
val small = expandedNf?.notification?.smallIcon
1023+
/** Hook 状态栏小图标 */
1024+
compatStatusIcon(
1025+
context = context,
1026+
nf = expandedNf,
1027+
iconDrawable = small?.loadDrawable(context)
1028+
).also { pair ->
1029+
focusedIcon = pair.second
1030+
val originalBitmap = pair.first?.toBitmap()
1031+
val bitmap = originalBitmap?.scale(50, 50)
1032+
result = Icon.createWithBitmap(bitmap).apply {
1033+
if (pair.second || ConfigData.isEnableFocusNotificationFix)
1034+
setTint(if (isDark) Color.BLACK else Color.WHITE)
1035+
}
10241036
}
10251037
}
10261038
}
1039+
firstMethodOrNull {
1040+
name = "getStatusBarTickerDarkIcon"
1041+
parameters {
1042+
(it.first() == classOf<StatusBarNotification>() ||
1043+
it.first() == ExpandedNotificationClass) && it.size == 1
1044+
}
1045+
}?.hook()?.after { hookTickerDarkIcon(isDark = true) }
1046+
firstMethodOrNull {
1047+
name = "getStatusBarTickerIcon"
1048+
parameters {
1049+
(it.first() == classOf<StatusBarNotification>() ||
1050+
it.first() == ExpandedNotificationClass) && it.size == 1
1051+
}
1052+
}?.hook()?.after { hookTickerDarkIcon(isDark = false) }
10271053
}
1028-
firstMethodOrNull {
1029-
name = "getStatusBarTickerDarkIcon"
1030-
parameters {
1031-
(it.first() == classOf<StatusBarNotification>() ||
1032-
it.first() == ExpandedNotificationClass) && it.size == 1
1033-
}
1034-
}?.hook()?.after { hookTickerDarkIcon(isDark = true) }
1035-
firstMethodOrNull {
1036-
name = "getStatusBarTickerIcon"
1037-
parameters {
1038-
(it.first() == classOf<StatusBarNotification>() ||
1039-
it.first() == ExpandedNotificationClass) && it.size == 1
1040-
}
1041-
}?.hook()?.after { hookTickerDarkIcon(isDark = false) }
10421054
}
10431055
/** 注入状态栏通知图标实例 */
10441056
StatusBarIconViewClass.resolve().optional().firstMethodOrNull {

0 commit comments

Comments
 (0)