diff --git a/app/src/main/java/com/fankes/coloros/notify/data/ConfigData.kt b/app/src/main/java/com/fankes/coloros/notify/data/ConfigData.kt index 98372f0..ea8f51a 100644 --- a/app/src/main/java/com/fankes/coloros/notify/data/ConfigData.kt +++ b/app/src/main/java/com/fankes/coloros/notify/data/ConfigData.kt @@ -76,6 +76,9 @@ object ConfigData { /** 启用通知图标优化 */ val ENABLE_NOTIFY_ICON_FIX = PrefsData("_notify_icon_fix", true) + /** 使用占位符修补未适配的通知图标 */ + val ENABLE_NOTIFY_ICON_FIX_PLACEHOLDER = PrefsData("_notify_icon_fix_placeholder", false) + /** 提醒未适配通知图标的新安装应用 */ val ENABLE_NOTIFY_ICON_FIX_NOTIFY = PrefsData("_notify_icon_fix_notify", true) @@ -311,6 +314,16 @@ object ConfigData { putBoolean(ENABLE_NOTIFY_ICON_FIX, value) } + /** + * 是否使用占位符修补未适配的通知图标 + * @return [Boolean] + */ + var isEnableNotifyIconFixPlaceholder + get() = getBoolean(ENABLE_NOTIFY_ICON_FIX_PLACEHOLDER) + set(value) { + putBoolean(ENABLE_NOTIFY_ICON_FIX_PLACEHOLDER, value) + } + /** * 是否提醒未适配通知图标的新安装应用 * @return [Boolean] diff --git a/app/src/main/java/com/fankes/coloros/notify/hook/entity/SystemUIHooker.kt b/app/src/main/java/com/fankes/coloros/notify/hook/entity/SystemUIHooker.kt index 142eb77..810af6f 100644 --- a/app/src/main/java/com/fankes/coloros/notify/hook/entity/SystemUIHooker.kt +++ b/app/src/main/java/com/fankes/coloros/notify/hook/entity/SystemUIHooker.kt @@ -344,29 +344,33 @@ object SystemUIHooker : YukiBaseHooker() { * @param context 实例 * @param isGrayscaleIcon 是否为灰度图标 * @param packageName APP 包名 - * @return [Pair] - ([Drawable] 位图 or null,[Int] 颜色) + * @return [Triple] - ([Drawable] 位图,[Int] 颜色,[Boolean] 是否为占位符图标) */ - private fun compatCustomIcon(context: Context, isGrayscaleIcon: Boolean, packageName: String): Pair { - var customPair: Pair? = null + private fun compatCustomIcon(context: Context, isGrayscaleIcon: Boolean, packageName: String): Triple { + /** 防止模块资源注入失败重新注入 */ + context.injectModuleAppResources() + var customPair: Triple? = null val statSysAdbIcon = runCatching { context.resources.drawableOf("com.android.internal.R\$drawable".toClass().field { name = "stat_sys_adb" }.get().int()) }.getOrNull() ?: context.resources.drawableOf(R.drawable.ic_unsupported) when { /** 替换系统图标为 Android 默认 */ (packageName == PackageName.SYSTEM_FRAMEWORK || packageName == PackageName.SYSTEMUI) && isGrayscaleIcon.not() -> - customPair = Pair(statSysAdbIcon, 0) + customPair = Triple(statSysAdbIcon, 0, false) /** 替换自定义通知图标 */ ConfigData.isEnableNotifyIconFix -> run { iconDatas.takeIf { it.isNotEmpty() }?.forEach { if (packageName == it.packageName && isAppNotifyHookOf(it)) { if (isGrayscaleIcon.not() || isAppNotifyHookAllOf(it)) - customPair = Pair(BitmapDrawable(context.resources, it.iconBitmap), it.iconColor) + customPair = Triple(BitmapDrawable(context.resources, it.iconBitmap), it.iconColor, false) return@run } } + if (isGrayscaleIcon.not() && ConfigData.isEnableNotifyIconFixPlaceholder) + customPair = Triple(context.resources.drawableOf(R.drawable.ic_unsupported), 0, true) } } - return customPair ?: Pair(null, 0) + return customPair ?: Triple(null, 0, false) } /** @@ -408,7 +412,7 @@ object SystemUIHooker : YukiBaseHooker() { iconColor: Int, iconView: ImageView ) = runInSafe { - compatCustomIcon(context, isGrayscaleIcon, packageName).also { customPair -> + compatCustomIcon(context, isGrayscaleIcon, packageName).also { customTriple -> /** 设置一个用于替换的图标 */ val placeholderView = ImageView(context) /** 克隆之前图标的所有布局信息 */ @@ -425,11 +429,11 @@ object SystemUIHooker : YukiBaseHooker() { /** 设置默认样式 */ setDefaultNotifyIconViewStyle() } - customPair.first != null || isGrayscaleIcon -> placeholderView.apply { + (customTriple.first != null && customTriple.third.not()) || isGrayscaleIcon -> placeholderView.apply { /** 设置不要裁切到边界 */ clipToOutline = false /** 重新设置图标 */ - setImageDrawable(customPair.first ?: drawable) + setImageDrawable(customTriple.first ?: drawable) /** 旧版风格 */ val oldStyle = (if (context.isSystemInDarkMode) 0xffdcdcdc else 0xff707173).toInt() @@ -442,10 +446,10 @@ object SystemUIHooker : YukiBaseHooker() { (if (context.isSystemInDarkMode) 0xff707173 else oldStyle).toInt() /** 旧版图标着色 */ - val oldApplyColor = customPair.second.takeIf { it != 0 } ?: iconColor.takeIf { it != 0 } ?: oldStyle + val oldApplyColor = customTriple.second.takeIf { it != 0 } ?: iconColor.takeIf { it != 0 } ?: oldStyle /** 新版图标着色 */ - val newApplyColor = customPair.second.takeIf { it != 0 } ?: iconColor.takeIf { it != 0 } ?: md3Style + val newApplyColor = customTriple.second.takeIf { it != 0 } ?: iconColor.takeIf { it != 0 } ?: md3Style /** 判断风格并开始 Hook */ if (ConfigData.isEnableMd3NotifyIconStyle) { @@ -471,7 +475,7 @@ object SystemUIHooker : YukiBaseHooker() { } } /** 打印日志 */ - printLogcat(tag = "NotifyIcon", iconView.context, packageName, isCustom = customPair.first != null, isGrayscaleIcon) + printLogcat(tag = "NotifyIcon", iconView.context, packageName, isCustom = customTriple.first != null, isGrayscaleIcon) } } diff --git a/app/src/main/java/com/fankes/coloros/notify/ui/activity/MainActivity.kt b/app/src/main/java/com/fankes/coloros/notify/ui/activity/MainActivity.kt index 3427acb..84d479e 100644 --- a/app/src/main/java/com/fankes/coloros/notify/ui/activity/MainActivity.kt +++ b/app/src/main/java/com/fankes/coloros/notify/ui/activity/MainActivity.kt @@ -213,6 +213,7 @@ class MainActivity : BaseActivity() { binding.notifyIconFixSwitch.bind(ConfigData.ENABLE_NOTIFY_ICON_FIX) { onInitialize { binding.notifyIconFixButton.isVisible = it + binding.notifyIconFixPlaceholderItem.isVisible = it binding.notifyIconFixNotifyItem.isVisible = it binding.notifyIconAutoSyncItem.isVisible = it } @@ -221,6 +222,26 @@ class MainActivity : BaseActivity() { SystemUITool.refreshSystemUI(context = this@MainActivity) } } + binding.notifyIconFixPlaceholderSwitch.bind(ConfigData.ENABLE_NOTIFY_ICON_FIX_PLACEHOLDER) { + isAutoApplyChanges = false + onChanged { + /** 应用更改并刷新系统界面 */ + fun applyChangesAndRefresh() { + applyChanges() + SystemUITool.refreshSystemUI(context = this@MainActivity) + } + if (it) showDialog { + title = "注意" + msg = "开启这个功能后,当发现未适配的彩色通知图标时," + + "状态栏中显示的通知图标将会使用预置的占位符图标进行修补," + + "通知栏中显示的通知图标保持原始图标不变。\n\n" + + "此功能的作用仅为临时修复破坏规范的通知图标,仍然继续开启吗?" + confirmButton { applyChangesAndRefresh() } + cancelButton { cancelChanges() } + noCancelable() + } else applyChangesAndRefresh() + } + } binding.notifyIconFixNotifySwitch.bind(ConfigData.ENABLE_NOTIFY_ICON_FIX_NOTIFY) { onChanged { SystemUITool.refreshSystemUI(context = this@MainActivity, isRefreshCacheOnly = true) } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index e7e3e9d..22a5ad3 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -848,6 +848,34 @@ android:textColor="@color/colorTextDark" android:textSize="12sp" /> + + + + + + +