Skip to content

Commit b979941

Browse files
authored
fix: skip media notifications to avoid systemui lag (#93)
* fix: skip media notifications to avoid systemui lag * fix: packageName * fix: import
1 parent 58679cb commit b979941

1 file changed

Lines changed: 40 additions & 7 deletions

File tree

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

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
/*
23
* ColorOSNotifyIcon - Optimize notification icons for ColorOS and adapt to native notification icon specifications.
34
* Copyright (C) 20174 Fankes Studio(qzmmcn@163.com)
@@ -34,6 +35,7 @@ import android.graphics.Color
3435
import android.graphics.Outline
3536
import android.graphics.drawable.Drawable
3637
import android.graphics.drawable.Icon
38+
import android.media.session.MediaSessionManager
3739
import android.os.Build
3840
import android.os.SystemClock
3941
import android.service.notification.StatusBarNotification
@@ -256,6 +258,9 @@ object SystemUIHooker : YukiBaseHooker() {
256258
/** 状态栏通知图标数组 */
257259
private var notificationIconInstances = ArrayList<View>()
258260

261+
/** 媒体会话管理器 */
262+
private var mediaSessionManager: MediaSessionManager? = null
263+
259264
/** 媒体通知 [View] */
260265
private var notificationPlayerView: View? = null
261266

@@ -700,6 +705,34 @@ object SystemUIHooker : YukiBaseHooker() {
700705
IconPackParams(param = this).iconDatas.apply { if (isNotEmpty()) forEach { iconDatas.add(it) } }
701706
}
702707

708+
/** 获取媒体会话管理器 */
709+
fun getMediaSessionManager(context: Context): MediaSessionManager {
710+
if (mediaSessionManager == null)
711+
mediaSessionManager = context.getSystemService(Context.MEDIA_SESSION_SERVICE) as MediaSessionManager
712+
713+
return mediaSessionManager!!
714+
}
715+
716+
/** 判断是否为媒体通知 */
717+
fun isMediaNotificationAOSP(notification: Notification?): Boolean {
718+
if (notification == null) return false
719+
720+
return notification.javaClass.resolve().firstMethod { name = "isMediaNotification" }.of(notification).invoke<Boolean>() == true
721+
}
722+
723+
/** 通过媒体会话和AOSP方法判断是否为媒体通知 */
724+
fun isMediaNotification(context: Context, notification: Notification, packageName: String): Boolean {
725+
if (isMediaNotificationAOSP(notification)) return true
726+
727+
val mediaSessionManager: MediaSessionManager = getMediaSessionManager(context)
728+
729+
for (mediaController in mediaSessionManager.getActiveSessions(null))
730+
if (packageName == mediaController.getPackageName() && notification.contentView != null)
731+
return true
732+
733+
return false
734+
}
735+
703736
/**
704737
* 刷新缓存数据
705738
* @param isRefreshCacheOnly 仅刷新缓存不刷新图标和通知改变 - 默认:否
@@ -786,7 +819,7 @@ object SystemUIHooker : YukiBaseHooker() {
786819
NotificationEntryClass.resolve().optional().firstMethodOrNull {
787820
name = "getSbn"
788821
}?.of(args().first().any())?.invokeQuietly<StatusBarNotification>()?.also { nf ->
789-
nf.notification.smallIcon.loadDrawable(context)?.also { iconDrawable ->
822+
if (!isMediaNotification(context, nf.notification, nf.packageName)) nf.notification.smallIcon.loadDrawable(context)?.also { iconDrawable ->
790823
compatStatusIcon(
791824
context = context,
792825
nf = nf,
@@ -920,7 +953,7 @@ object SystemUIHooker : YukiBaseHooker() {
920953
}?.invoke<StatusBarNotification>()
921954
}.also { nf ->
922955
nf?.notification?.also {
923-
it.smallIcon.loadDrawable(context)?.also { iconDrawable ->
956+
if (!isMediaNotification(context, it, context.packageName)) it.smallIcon.loadDrawable(context)?.also { iconDrawable ->
924957
compatNotifyIcon(
925958
context = context,
926959
nf = nf,
@@ -954,7 +987,7 @@ object SystemUIHooker : YukiBaseHooker() {
954987
}?.invoke<StatusBarNotification>()
955988
}.also { nf ->
956989
nf?.notification?.also {
957-
it.smallIcon.loadDrawable(context)?.also { iconDrawable ->
990+
if (!isMediaNotification(context, it, context.packageName)) it.smallIcon.loadDrawable(context)?.also { iconDrawable ->
958991
compatNotifyIcon(
959992
context = context,
960993
nf = nf,
@@ -997,7 +1030,7 @@ object SystemUIHooker : YukiBaseHooker() {
9971030
if (context == null) return@also
9981031

9991032
nf?.notification?.also {
1000-
it.smallIcon.loadDrawable(context)?.also { iconDrawable ->
1033+
if (!isMediaNotification(context, it, context.packageName)) it.smallIcon.loadDrawable(context)?.also { iconDrawable ->
10011034
compatNotifyIcon(
10021035
context = context,
10031036
nf = nf,
@@ -1071,7 +1104,7 @@ object SystemUIHooker : YukiBaseHooker() {
10711104
}?.invoke<StatusBarNotification>()
10721105
}.also { nf ->
10731106
nf?.notification?.also {
1074-
it.smallIcon.loadDrawable(context)?.also { iconDrawable ->
1107+
if (!isMediaNotification(context, it, context.packageName)) it.smallIcon.loadDrawable(context)?.also { iconDrawable ->
10751108
/** 执行替换 */
10761109
compatNotifyIcon(
10771110
context = context,
@@ -1102,7 +1135,7 @@ object SystemUIHooker : YukiBaseHooker() {
11021135
}?.invoke<StatusBarNotification>()
11031136
}.also { nf ->
11041137
nf?.notification?.also {
1105-
it.smallIcon.loadDrawable(context)?.also { iconDrawable ->
1138+
if (!isMediaNotification(context, it, context.packageName)) it.smallIcon.loadDrawable(context)?.also { iconDrawable ->
11061139
/** 执行替换 */
11071140
fun doParse() {
11081141
compatNotifyIcon(
@@ -1126,4 +1159,4 @@ object SystemUIHooker : YukiBaseHooker() {
11261159
}
11271160
}
11281161
}
1129-
}
1162+
}

0 commit comments

Comments
 (0)