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

This commit is contained in:
2023-02-05 02:32:15 +08:00
parent 881f4a46e5
commit 4c8b1b0d53
4 changed files with 80 additions and 11 deletions

View File

@@ -66,6 +66,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)
@@ -274,6 +277,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]

View File

@@ -46,6 +46,7 @@ import androidx.core.graphics.drawable.toBitmap
import androidx.core.graphics.drawable.toDrawable
import androidx.core.view.children
import androidx.core.view.isVisible
import com.fankes.miui.notify.R
import com.fankes.miui.notify.bean.IconDataBean
import com.fankes.miui.notify.const.PackageName
import com.fankes.miui.notify.data.ConfigData
@@ -346,20 +347,24 @@ object SystemUIHooker : YukiBaseHooker() {
* @param context 实例
* @param isGrayscaleIcon 是否为灰度图标
* @param packageName APP 包名
* @return [Pair] - ([Drawable] 小图标,[Int] 颜色)
* @return [Triple] - ([Drawable] 小图标,[Int] 颜色,[Boolean] 是否为占位符图标)
*/
private fun compatCustomIcon(context: Context, isGrayscaleIcon: Boolean, packageName: String): Pair<Drawable?, Int> {
var customPair: Pair<Drawable?, Int>? = null
private fun compatCustomIcon(context: Context, isGrayscaleIcon: Boolean, packageName: String): Triple<Drawable?, Int, Boolean> {
/** 防止模块资源注入失败重新注入 */
context.injectModuleAppResources()
var customPair: Triple<Drawable?, Int, Boolean>? = null
if (ConfigData.isEnableNotifyIconFix) run {
iconDatas.takeIf { it.isNotEmpty() }?.forEach {
if (packageName == it.packageName && isAppNotifyHookOf(it)) {
if (isGrayscaleIcon.not() || isAppNotifyHookAllOf(it))
customPair = Pair(it.iconBitmap.toDrawable(context.resources), it.iconColor)
customPair = Triple(it.iconBitmap.toDrawable(context.resources), 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)
}
/**
@@ -375,17 +380,17 @@ object SystemUIHooker : YukiBaseHooker() {
expandedNf?.let { notifyInstance ->
if (iconDrawable == null) return@let Pair(null, false)
/** 判断是否不是灰度图标 */
val isNotGrayscaleIcon = notifyInstance.isXmsf || isGrayscaleIcon(context, iconDrawable, notifyInstance).not()
val isGrayscaleIcon = notifyInstance.isXmsf.not() && isGrayscaleIcon(context, iconDrawable, notifyInstance)
/** 目标彩色通知 APP 图标 */
val customIcon = compatCustomIcon(context, isNotGrayscaleIcon.not(), notifyInstance.nfPkgName).first
val customIcon = compatCustomIcon(context, isGrayscaleIcon, notifyInstance.nfPkgName).first
/** 打印日志 */
printLogcat(tag = "StatusIcon", context, notifyInstance, isCustom = customIcon != null, isNotGrayscaleIcon.not())
printLogcat(tag = "StatusIcon", context, notifyInstance, isCustom = customIcon != null, isGrayscaleIcon)
when {
/** 处理自定义通知图标优化 */
customIcon != null -> Pair(customIcon, true)
/** 若不是灰度图标自动处理为圆角 */
isNotGrayscaleIcon -> Pair(notifyInstance.compatPushingIcon(context, iconDrawable).rounded(context), true)
isGrayscaleIcon.not() -> Pair(notifyInstance.compatPushingIcon(context, iconDrawable).rounded(context), true)
/** 否则返回原始小图标 */
else -> Pair(notifyInstance.notification.smallIcon.loadDrawable(context), false)
}
@@ -469,11 +474,13 @@ object SystemUIHooker : YukiBaseHooker() {
val isGrayscaleIcon = notifyInstance.isXmsf.not() && isGrayscaleIcon(context, iconDrawable, notifyInstance)
/** 自定义默认小图标 */
var customIcon: Drawable?
var customIcon: Drawable? = null
/** 自定义默认小图标颜色 */
var customIconColor: Int
var customIconColor = 0
compatCustomIcon(context, isGrayscaleIcon, notifyInstance.nfPkgName).also {
/** 不处理占位符图标 */
if (it.third) return@also
customIcon = it.first
customIconColor = if (isUseMaterial3Style || isExpanded)
(it.second.takeIf { e -> e != 0 } ?: (if (isUseMaterial3Style) context.systemAccentColor else 0)) else 0

View File

@@ -227,6 +227,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
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
}
@@ -235,6 +236,26 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
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) }
}

View File

@@ -979,6 +979,34 @@
android:textColor="@color/colorTextDark"
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.miui.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
android:id="@+id/notify_icon_fix_notify_item"
android:layout_width="match_parent"