Skip to content

Commit 1f71fbd

Browse files
committed
feat: add classic (MIUI) style notification icon replacement
1 parent 8f01e64 commit 1f71fbd

4 files changed

Lines changed: 64 additions & 21 deletions

File tree

app/src/main/java/com/fankes/miui/notify/data/ConfigData.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ object ConfigData {
6060
/** 通知栏中的通知图标圆角程度 */
6161
val NOTIFY_ICON_CORNER_SIZE = PrefsData("_notify_icon_corner", 15)
6262

63+
/** 替换 MIUI 样式通知栏的通知图标 */
64+
val ENABLE_REPLACE_MIUI_STYLE_NOTIFY_ICON = PrefsData("_replace_miui_style_notify_icon", true)
65+
6366
/** 强制通知栏中的通知图标使用系统着色 */
6467
val ENABLE_NOTIFY_ICON_FORCE_SYSTEM_COLOR = PrefsData("_notify_icon_force_system_color", false)
6568

@@ -260,6 +263,16 @@ object ConfigData {
260263
putInt(NOTIFY_ICON_CORNER_SIZE, value)
261264
}
262265

266+
/**
267+
* 是否替换 MIUI 样式通知栏的通知图标
268+
* @return [Boolean]
269+
*/
270+
var isEnableReplaceMiuiStyleNotifyIcon
271+
get() = getBoolean(ENABLE_REPLACE_MIUI_STYLE_NOTIFY_ICON)
272+
set(value) {
273+
putBoolean(ENABLE_REPLACE_MIUI_STYLE_NOTIFY_ICON, value)
274+
}
275+
263276
/**
264277
* 是否强制通知栏中的通知图标使用系统着色
265278
* @return [Boolean]

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

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ object SystemUIHooker : YukiBaseHooker() {
269269
return xmsfPkg.ifBlank { targetPkg.ifBlank { packageName } }
270270
}
271271

272+
/**
273+
* 获取 MIUI 自己设置的通知图标
274+
* @return [Icon] or null
275+
*/
276+
@Suppress("DEPRECATION")
277+
private val StatusBarNotification.miuiAppIcon get() = notification?.extras?.getParcelable<Icon?>("miui.appIcon")
278+
272279
/**
273280
* 打印日志
274281
* @param tag 标识
@@ -528,11 +535,8 @@ object SystemUIHooker : YukiBaseHooker() {
528535
loggerDebug(tag = "Notification Panel Icon", context, notifyInstance, isCustom = customIcon != null, isGrayscaleIcon)
529536
/** 处理自定义通知图标优化 */
530537
when {
531-
ConfigData.isEnableNotifyIconForceAppIcon -> {
532-
@Suppress("DEPRECATION")
533-
val miuiAppIcon = notifyInstance.notification?.extras?.getParcelable<Icon?>("miui.appIcon")
534-
setDefaultNotifyIcon(drawable = miuiAppIcon?.loadDrawable(context) ?: context.appIconOf(notifyInstance.nfPkgName))
535-
}
538+
ConfigData.isEnableNotifyIconForceAppIcon ->
539+
setDefaultNotifyIcon(drawable = notifyInstance.miuiAppIcon?.loadDrawable(context) ?: context.appIconOf(notifyInstance.nfPkgName))
536540
customIcon != null -> iconView.apply {
537541
/** 设置不要裁切到边界 */
538542
clipToOutline = false
@@ -982,14 +986,11 @@ object SystemUIHooker : YukiBaseHooker() {
982986
/** 修改 MIUI 风格通知栏的通知图标 */
983987
MiuiNotificationViewWrapperClass?.apply {
984988
constructor().hook().after {
989+
val nf = instance.getRowPair().second.getSbn() ?: return@after
985990
field { name = "mAppIcon" }.get(instance).cast<ImageView>()?.clone {
986-
compatNotifyIcon(
987-
context = context,
988-
nf = instance.getRowPair().second.getSbn(),
989-
iconView = this,
990-
isUseMaterial3Style = true,
991-
isMiuiPanel = true
992-
)
991+
if (ConfigData.isEnableReplaceMiuiStyleNotifyIcon || ConfigData.isEnableNotifyIconForceAppIcon)
992+
compatNotifyIcon(context, nf, iconView = this, isUseMaterial3Style = true, isMiuiPanel = true)
993+
else setImageDrawable(nf.miuiAppIcon?.loadDrawable(context) ?: context.appIconOf(nf.packageName))
993994
}
994995
}
995996
}
@@ -1001,15 +1002,12 @@ object SystemUIHooker : YukiBaseHooker() {
10011002
param(BooleanType)
10021003
}.hook().after {
10031004
field { name = "mAppIcon" }.get(instance).cast<ImageView>()?.apply {
1004-
compatNotifyIcon(
1005-
context = context,
1006-
nf = NotificationChildrenContainerClass.field {
1007-
name = "mContainingNotification"
1008-
}.get(instance).any()?.getSbn(),
1009-
iconView = this,
1010-
isUseMaterial3Style = true,
1011-
isMiuiPanel = true
1012-
)
1005+
val nf = NotificationChildrenContainerClass.field {
1006+
name = "mContainingNotification"
1007+
}.get(instance).any()?.getSbn() ?: return@after
1008+
if (ConfigData.isEnableReplaceMiuiStyleNotifyIcon || ConfigData.isEnableNotifyIconForceAppIcon)
1009+
compatNotifyIcon(context, nf, iconView = this, isUseMaterial3Style = true, isMiuiPanel = true)
1010+
else setImageDrawable(nf.miuiAppIcon?.loadDrawable(context) ?: context.appIconOf(nf.packageName))
10131011
}
10141012
}
10151013
}

app/src/main/java/com/fankes/miui/notify/ui/activity/MainActivity.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
238238
} else applyChangesAndRefresh()
239239
}
240240
}
241+
binding.miuiNotifyIconReplacementSwitch.bind(ConfigData.ENABLE_REPLACE_MIUI_STYLE_NOTIFY_ICON) {
242+
onChanged { SystemUITool.refreshSystemUI(context = this@MainActivity) }
243+
}
241244
binding.notifyIconForceSystemColorSwitch.bind(ConfigData.ENABLE_NOTIFY_ICON_FORCE_SYSTEM_COLOR) {
242245
isAutoApplyChanges = false
243246
onChanged {
@@ -263,6 +266,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
263266
binding.notifyIconCustomCornerItem,
264267
binding.notifyIconForceSystemColorItem
265268
).forEach { e -> e.isVisible = isLowerAndroidR.not() && it.not() }
269+
binding.miuiNotifyIconReplacementItem.isVisible = it.not()
266270
}
267271
onChanged {
268272
/** 应用更改并刷新系统界面 */

app/src/main/res/layout/activity_main.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,34 @@
883883
android:textStyle="bold" />
884884
</LinearLayout>
885885

886+
<LinearLayout
887+
android:id="@+id/miui_notify_icon_replacement_item"
888+
android:layout_width="match_parent"
889+
android:layout_height="wrap_content"
890+
android:orientation="vertical">
891+
892+
<com.fankes.miui.notify.ui.widget.MaterialSwitch
893+
android:id="@+id/miui_notify_icon_replacement_switch"
894+
android:layout_width="match_parent"
895+
android:layout_height="wrap_content"
896+
android:paddingLeft="15dp"
897+
android:paddingRight="15dp"
898+
android:text="替换经典样式通知栏的通知图标"
899+
android:textColor="@color/colorTextGray"
900+
android:textSize="15sp" />
901+
902+
<TextView
903+
android:layout_width="match_parent"
904+
android:layout_height="wrap_content"
905+
android:alpha="0.6"
906+
android:lineSpacingExtra="6dp"
907+
android:paddingLeft="15dp"
908+
android:paddingRight="15dp"
909+
android:text="此选项默认开启,开启后经典 (MIUI) 样式的下拉通知栏中的通知图标将同样应用替换后的通知图标,否则将保持系统自己设置的图标。(此功能无法对所有系统版本兼容)"
910+
android:textColor="@color/colorTextDark"
911+
android:textSize="12sp" />
912+
</LinearLayout>
913+
886914
<LinearLayout
887915
android:id="@+id/notify_icon_force_system_color_item"
888916
android:layout_width="match_parent"

0 commit comments

Comments
 (0)