增加模块激活状态判断以及模块更新提醒

This commit is contained in:
2022-03-24 04:03:56 +08:00
parent 646f30d05a
commit bc2dd8a973
7 changed files with 154 additions and 16 deletions

View File

@@ -26,15 +26,11 @@ package com.fankes.miui.notify.application
import android.app.Application
import androidx.appcompat.app.AppCompatDelegate
import com.fankes.miui.notify.BuildConfig
class MNNApplication : Application() {
companion object {
/** 当前模块的包名 */
const val MODULE_PACKAGE_NAME = BuildConfig.APPLICATION_ID
/** 全局静态实例 */
private var context: MNNApplication? = null

View File

@@ -0,0 +1,54 @@
/*
* MIUINativeNotifyIcon - Fix the native notification bar icon function abandoned by the MIUI development team.
* Copyright (C) 2019-2022 Fankes Studio(qzmmcn@163.com)
* https://github.com/fankes/MIUINativeNotifyIcon
*
* This software is non-free but opensource software: you can redistribute it
* and/or modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
* <p>
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* and eula along with this software. If not, see
* <https://www.gnu.org/licenses/>
*
* This file is Created by fankes on 2022/3/24.
*/
@file:Suppress("MemberVisibilityCanBePrivate")
package com.fankes.miui.notify.const
import com.fankes.miui.notify.BuildConfig
/**
* 存储一些静态编译后的值
*/
object Const {
/** 当前模块的包名 */
const val MODULE_PACKAGE_NAME = BuildConfig.APPLICATION_ID
/** 当前模块的版本名称 */
const val MODULE_VERSION_NAME = BuildConfig.VERSION_NAME
/** 当前模块的版本号 */
const val MODULE_VERSION_CODE = BuildConfig.VERSION_CODE
/** 当前模块的版本校验 */
const val MODULE_VERSION_VERIFY = "${MODULE_VERSION_NAME}_${MODULE_VERSION_CODE}_202103240401"
/** 当前模块的版本校验标签 */
const val MODULE_VERSION_VERIFY_TAG = "module_version_verify"
/** 发送通讯广播号标签 */
const val MODULE_CHECKING_RECEIVER = "mnn_checking_action"
/** 接收通讯广播号标签 */
const val MODULE_HANDLER_RECEIVER = "mnn_handler_action"
}

View File

@@ -23,8 +23,10 @@
package com.fankes.miui.notify.hook
import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.Outline
@@ -38,8 +40,8 @@ import android.view.ViewGroup
import android.view.ViewOutlineProvider
import android.widget.ImageView
import androidx.core.graphics.drawable.toBitmap
import com.fankes.miui.notify.application.MNNApplication.Companion.MODULE_PACKAGE_NAME
import com.fankes.miui.notify.bean.IconDataBean
import com.fankes.miui.notify.const.Const
import com.fankes.miui.notify.hook.HookConst.ENABLE_COLOR_ICON_COMPAT
import com.fankes.miui.notify.hook.HookConst.ENABLE_HOOK_STATUS_ICON_COUNT
import com.fankes.miui.notify.hook.HookConst.ENABLE_MODULE
@@ -140,6 +142,32 @@ class HookEntry : YukiHookXposedInitProxy {
/** 缓存的通知小图标包装纸实例 */
private var notificationViewWrappers = HashSet<Any>()
/** 是否已经注册广播 */
private var isRegisterModuleReceiver = false
/** 模块广播接收器 */
private val moduleReceiver by lazy {
object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
context?.sendBroadcast(Intent().apply {
action = Const.MODULE_HANDLER_RECEIVER
putExtra("isAction", true)
putExtra("isValied", intent?.getStringExtra(Const.MODULE_VERSION_VERIFY_TAG) == Const.MODULE_VERSION_VERIFY)
})
}
}
}
/**
* 注册模块广播接收器
* @param context 实例
*/
private fun registerModuleReceiver(context: Context) {
if (isRegisterModuleReceiver) return
context.registerReceiver(moduleReceiver, IntentFilter().apply { addAction(Const.MODULE_CHECKING_RECEIVER) })
isRegisterModuleReceiver = true
}
/**
* 是否启用通知图标优化功能
* @param isHooking 是否判断启用通知功能 - 默认:是
@@ -605,7 +633,7 @@ class HookEntry : YukiHookXposedInitProxy {
(result as Icon).loadDrawable(context)
) { icon, isReplace -> if (isReplace) result = Icon.createWithBitmap(icon.toBitmap()) }
/** 刷新缓存 */
if (expandedNf?.compatOpPkgName == MODULE_PACKAGE_NAME &&
if (expandedNf?.compatOpPkgName == Const.MODULE_PACKAGE_NAME &&
expandedNf.notification?.channelId == IconRuleManagerTool.NOTIFY_CHANNEL
) recachingPrefs()
}
@@ -654,7 +682,12 @@ class HookEntry : YukiHookXposedInitProxy {
param(ExpandedNotificationClass.clazz)
}
}
afterHook { if (firstArgs != null) statusBarIconViews.add(instance()) }
afterHook {
if (firstArgs != null) instance<ImageView>().also {
registerModuleReceiver(it.context)
statusBarIconViews.add(it)
}
}
}
}
NotificationIconContainerClass.hook {

View File

@@ -24,13 +24,12 @@
package com.fankes.miui.notify.ui.activity
import android.content.ComponentName
import android.content.Intent
import android.content.*
import android.content.pm.PackageManager
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.fankes.miui.notify.BuildConfig
import com.fankes.miui.notify.R
import com.fankes.miui.notify.const.Const
import com.fankes.miui.notify.databinding.ActivityMainBinding
import com.fankes.miui.notify.databinding.DiaStatusIconCountBinding
import com.fankes.miui.notify.hook.HookConst.ENABLE_COLOR_ICON_COMPAT
@@ -48,13 +47,14 @@ import com.fankes.miui.notify.utils.tool.GithubReleaseTool
import com.fankes.miui.notify.utils.tool.SystemUITool
import com.highcapable.yukihookapi.hook.factory.isXposedModuleActive
import com.highcapable.yukihookapi.hook.factory.modulePrefs
import com.highcapable.yukihookapi.hook.xposed.YukiHookModuleStatus
class MainActivity : BaseActivity<ActivityMainBinding>() {
companion object {
/** 模块版本 */
private const val moduleVersion = BuildConfig.VERSION_NAME
private const val moduleVersion = Const.MODULE_VERSION_NAME
/** 预发布的版本标识 */
private const val pendingFlag = "[pending]"
@@ -63,6 +63,12 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
/** 警告对话框是否显示 */
private var isWarnDialogShowing = false
/** 模块是否激活 */
private var isModuleAction = false
/** 模块是否有效 */
private var isModuleValied = false
override fun onCreate() {
/** 设置文本 */
binding.mainTextVersion.text = "模块版本:$moduleVersion $pendingFlag"
@@ -235,13 +241,16 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
binding.linkWithFollowMe.setOnClickListener {
openBrowser(url = "https://www.coolapk.com/u/876977", packageName = "com.coolapk.market")
}
/** 注册广播检查模块激活状态 */
registerReceiver(hostReceiver, IntentFilter().apply { addAction(Const.MODULE_HANDLER_RECEIVER) })
}
/** 刷新模块状态 */
private fun refreshModuleStatus() {
binding.mainLinStatus.setBackgroundResource(
when {
isXposedModuleActive && isMiuiNotifyStyle -> R.drawable.bg_yellow_round
(isXposedModuleActive && isMiuiNotifyStyle) ||
(isXposedModuleActive && (!isModuleAction || !isModuleValied)) -> R.drawable.bg_yellow_round
isXposedModuleActive -> R.drawable.bg_green_round
else -> R.drawable.bg_dark_round
}
@@ -255,15 +264,26 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
binding.mainTextStatus.text =
when {
isXposedModuleActive && isMiuiNotifyStyle -> "模块已激活,但未在工作"
isXposedModuleActive && !isModuleAction &&
!modulePrefs.getBoolean(ENABLE_MODULE, default = true) -> "模块已停用"
isXposedModuleActive && !isModuleAction -> "模块已激活,请重启系统界面"
isXposedModuleActive && !isModuleValied -> "模块已更新,请重启系统界面"
isXposedModuleActive -> "模块已激活"
else -> "模块未激活"
}
binding.mainTextApiWay.isVisible = isXposedModuleActive
binding.mainTextApiWay.text = "Activate by ${YukiHookModuleStatus.executorName} API ${YukiHookModuleStatus.executorVersion}"
}
override fun onResume() {
super.onResume()
/** 刷新模块状态 */
refreshModuleStatus()
/** 发送广播检查模块激活状态 */
sendBroadcast(Intent().apply {
action = Const.MODULE_CHECKING_RECEIVER
putExtra(Const.MODULE_VERSION_VERIFY_TAG, Const.MODULE_VERSION_VERIFY)
})
/** 经典样式启用后给出警告 */
if (!isWarnDialogShowing && isXposedModuleActive && isMiuiNotifyStyle)
showDialog {
@@ -288,4 +308,21 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
noCancelable()
}
}
override fun onDestroy() {
super.onDestroy()
/** 取消注册广播 */
unregisterReceiver(hostReceiver)
}
/** 宿主广播接收器 */
private val hostReceiver by lazy {
object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
isModuleAction = intent?.getBooleanExtra("isAction", false) ?: false
isModuleValied = intent?.getBooleanExtra("isValied", false) ?: false
refreshModuleStatus()
}
}
}
}

View File

@@ -32,7 +32,7 @@ import android.content.Intent
import android.graphics.Bitmap
import android.graphics.drawable.Icon
import android.os.Build
import com.fankes.miui.notify.application.MNNApplication.Companion.MODULE_PACKAGE_NAME
import com.fankes.miui.notify.const.Const
import com.fankes.miui.notify.hook.HookEntry
import com.fankes.miui.notify.utils.factory.bitmap
import com.fankes.miui.notify.utils.factory.findAppIcon
@@ -132,8 +132,8 @@ object IconAdaptationTool {
context, packageName.hashCode(),
Intent().apply {
component = ComponentName(
MODULE_PACKAGE_NAME,
"$MODULE_PACKAGE_NAME.ui.activity.ConfigureActivity"
Const.MODULE_PACKAGE_NAME,
"${Const.MODULE_PACKAGE_NAME}.ui.activity.ConfigureActivity"
)
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}.apply {

View File

@@ -59,7 +59,10 @@ object SystemUITool {
* 刷新系统界面状态栏与通知图标
* @param context 实例
*/
fun refreshSystemUI(context: Context) = IconRuleManagerTool.refreshSystemUI(context)
fun refreshSystemUI(context: Context) =
if (isXposedModuleActive)
IconRuleManagerTool.refreshSystemUI(context)
else context.snake(msg = "模块没有激活,更改不会生效")
/**
* 显示需要重启系统界面的 [Snackbar]

View File

@@ -87,6 +87,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:ellipsize="end"
android:singleLine="true"
android:text="模块状态未知"
android:textColor="@color/white"
android:textSize="18sp" />
@@ -137,6 +139,19 @@
android:text="系统版本:%1"
android:textColor="@color/white"
android:textSize="13sp" />
<TextView
android:id="@+id/main_text_api_way"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:alpha="0.6"
android:ellipsize="end"
android:singleLine="true"
android:text="%1"
android:textColor="@color/white"
android:textSize="11sp"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>