Added color notification icons placeholder icon function in SystemUIHooker, ConfigData, MainActivity, activity_main

This commit is contained in:
2023-02-05 02:32:35 +08:00
parent bae57c87d8
commit 4ad7acb56c
4 changed files with 78 additions and 12 deletions

View File

@@ -76,6 +76,9 @@ object ConfigData {
/** 启用通知图标优化 */ /** 启用通知图标优化 */
val ENABLE_NOTIFY_ICON_FIX = PrefsData("_notify_icon_fix", true) 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) val ENABLE_NOTIFY_ICON_FIX_NOTIFY = PrefsData("_notify_icon_fix_notify", true)
@@ -311,6 +314,16 @@ object ConfigData {
putBoolean(ENABLE_NOTIFY_ICON_FIX, value) 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] * @return [Boolean]

View File

@@ -344,29 +344,33 @@ object SystemUIHooker : YukiBaseHooker() {
* @param context 实例 * @param context 实例
* @param isGrayscaleIcon 是否为灰度图标 * @param isGrayscaleIcon 是否为灰度图标
* @param packageName APP 包名 * @param packageName APP 包名
* @return [Pair] - ([Drawable] 位图 or null,[Int] 颜色) * @return [Triple] - ([Drawable] 位图,[Int] 颜色,[Boolean] 是否为占位符图标)
*/ */
private fun compatCustomIcon(context: Context, isGrayscaleIcon: Boolean, packageName: String): Pair<Drawable?, Int> { private fun compatCustomIcon(context: Context, isGrayscaleIcon: Boolean, packageName: String): Triple<Drawable?, Int, Boolean> {
var customPair: Pair<Drawable?, Int>? = null /** 防止模块资源注入失败重新注入 */
context.injectModuleAppResources()
var customPair: Triple<Drawable?, Int, Boolean>? = null
val statSysAdbIcon = runCatching { val statSysAdbIcon = runCatching {
context.resources.drawableOf("com.android.internal.R\$drawable".toClass().field { name = "stat_sys_adb" }.get().int()) context.resources.drawableOf("com.android.internal.R\$drawable".toClass().field { name = "stat_sys_adb" }.get().int())
}.getOrNull() ?: context.resources.drawableOf(R.drawable.ic_unsupported) }.getOrNull() ?: context.resources.drawableOf(R.drawable.ic_unsupported)
when { when {
/** 替换系统图标为 Android 默认 */ /** 替换系统图标为 Android 默认 */
(packageName == PackageName.SYSTEM_FRAMEWORK || packageName == PackageName.SYSTEMUI) && isGrayscaleIcon.not() -> (packageName == PackageName.SYSTEM_FRAMEWORK || packageName == PackageName.SYSTEMUI) && isGrayscaleIcon.not() ->
customPair = Pair(statSysAdbIcon, 0) customPair = Triple(statSysAdbIcon, 0, false)
/** 替换自定义通知图标 */ /** 替换自定义通知图标 */
ConfigData.isEnableNotifyIconFix -> run { ConfigData.isEnableNotifyIconFix -> run {
iconDatas.takeIf { it.isNotEmpty() }?.forEach { iconDatas.takeIf { it.isNotEmpty() }?.forEach {
if (packageName == it.packageName && isAppNotifyHookOf(it)) { if (packageName == it.packageName && isAppNotifyHookOf(it)) {
if (isGrayscaleIcon.not() || isAppNotifyHookAllOf(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 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, iconColor: Int,
iconView: ImageView iconView: ImageView
) = runInSafe { ) = runInSafe {
compatCustomIcon(context, isGrayscaleIcon, packageName).also { customPair -> compatCustomIcon(context, isGrayscaleIcon, packageName).also { customTriple ->
/** 设置一个用于替换的图标 */ /** 设置一个用于替换的图标 */
val placeholderView = ImageView(context) val placeholderView = ImageView(context)
/** 克隆之前图标的所有布局信息 */ /** 克隆之前图标的所有布局信息 */
@@ -425,11 +429,11 @@ object SystemUIHooker : YukiBaseHooker() {
/** 设置默认样式 */ /** 设置默认样式 */
setDefaultNotifyIconViewStyle() setDefaultNotifyIconViewStyle()
} }
customPair.first != null || isGrayscaleIcon -> placeholderView.apply { (customTriple.first != null && customTriple.third.not()) || isGrayscaleIcon -> placeholderView.apply {
/** 设置不要裁切到边界 */ /** 设置不要裁切到边界 */
clipToOutline = false clipToOutline = false
/** 重新设置图标 */ /** 重新设置图标 */
setImageDrawable(customPair.first ?: drawable) setImageDrawable(customTriple.first ?: drawable)
/** 旧版风格 */ /** 旧版风格 */
val oldStyle = (if (context.isSystemInDarkMode) 0xffdcdcdc else 0xff707173).toInt() val oldStyle = (if (context.isSystemInDarkMode) 0xffdcdcdc else 0xff707173).toInt()
@@ -442,10 +446,10 @@ object SystemUIHooker : YukiBaseHooker() {
(if (context.isSystemInDarkMode) 0xff707173 else oldStyle).toInt() (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 */ /** 判断风格并开始 Hook */
if (ConfigData.isEnableMd3NotifyIconStyle) { 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)
} }
} }

View File

@@ -213,6 +213,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
binding.notifyIconFixSwitch.bind(ConfigData.ENABLE_NOTIFY_ICON_FIX) { binding.notifyIconFixSwitch.bind(ConfigData.ENABLE_NOTIFY_ICON_FIX) {
onInitialize { onInitialize {
binding.notifyIconFixButton.isVisible = it binding.notifyIconFixButton.isVisible = it
binding.notifyIconFixPlaceholderItem.isVisible = it
binding.notifyIconFixNotifyItem.isVisible = it binding.notifyIconFixNotifyItem.isVisible = it
binding.notifyIconAutoSyncItem.isVisible = it binding.notifyIconAutoSyncItem.isVisible = it
} }
@@ -221,6 +222,26 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
SystemUITool.refreshSystemUI(context = this@MainActivity) 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) { binding.notifyIconFixNotifySwitch.bind(ConfigData.ENABLE_NOTIFY_ICON_FIX_NOTIFY) {
onChanged { SystemUITool.refreshSystemUI(context = this@MainActivity, isRefreshCacheOnly = true) } onChanged { SystemUITool.refreshSystemUI(context = this@MainActivity, isRefreshCacheOnly = true) }
} }

View File

@@ -848,6 +848,34 @@
android:textColor="@color/colorTextDark" android:textColor="@color/colorTextDark"
android:textSize="12sp" /> android:textSize="12sp" />
<LinearLayout
android:id="@+id/notify_icon_fix_placeholder_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:orientation="vertical">
<com.fankes.coloros.notify.ui.widget.MaterialSwitch
android:id="@+id/notify_icon_fix_placeholder_switch"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="5dp"
android:text="使用占位符修补未适配的通知图标"
android:textColor="@color/colorTextGray"
android:textSize="15sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:alpha="0.6"
android:lineSpacingExtra="6dp"
android:text="此选项默认关闭,当发现未适配的彩色通知图标时,状态栏中显示的通知图标将会使用预置的占位符图标进行修补,通知栏中显示的通知图标保持原始图标不变。"
android:textColor="@color/colorTextDark"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/notify_icon_fix_notify_item" android:id="@+id/notify_icon_fix_notify_item"
android:layout_width="match_parent" android:layout_width="match_parent"