From 0f0c54d053cd6abc219f8a0e400727a3050a1b74 Mon Sep 17 00:00:00 2001 From: lisonge Date: Tue, 9 Jan 2024 15:16:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=84=E5=88=99=E8=A7=A6=E5=8F=91?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E4=B8=8D=E4=B8=80=E8=87=B4=20(#465)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/kotlin/li/songe/gkd/data/AppRule.kt | 11 +++++------ .../kotlin/li/songe/gkd/data/ComplexSnapshot.kt | 4 ++-- .../main/kotlin/li/songe/gkd/data/GlobalRule.kt | 17 ++++++++--------- .../kotlin/li/songe/gkd/data/ResolvedRule.kt | 11 +++++------ .../main/kotlin/li/songe/gkd/service/AbState.kt | 8 +++----- .../kotlin/li/songe/gkd/service/GkdAbService.kt | 17 ++++++++++++++--- 6 files changed, 37 insertions(+), 31 deletions(-) diff --git a/app/src/main/kotlin/li/songe/gkd/data/AppRule.kt b/app/src/main/kotlin/li/songe/gkd/data/AppRule.kt index 9c28a6b95..22aa056ff 100644 --- a/app/src/main/kotlin/li/songe/gkd/data/AppRule.kt +++ b/app/src/main/kotlin/li/songe/gkd/data/AppRule.kt @@ -1,7 +1,5 @@ package li.songe.gkd.data -import li.songe.gkd.service.TopActivity - class AppRule( rule: RawSubscription.RawAppRule, subsItem: SubsItem, @@ -25,9 +23,10 @@ class AppRule( ) + (excludeData.activityMap[app.id] ?: emptyList()) override val type = "app" - override fun matchActivity(topActivity: TopActivity): Boolean { - topActivity.activityId ?: return true - if (excludeActivityIds.any { topActivity.activityId.startsWith(it) }) return false - return activityIds.isEmpty() || activityIds.any { topActivity.activityId.startsWith(it) } + override fun matchActivity(appId: String, activityId: String?): Boolean { + if (appId != app.id) return false + activityId ?: return true + if (excludeActivityIds.any { activityId.startsWith(it) }) return false + return activityIds.isEmpty() || activityIds.any { activityId.startsWith(it) } } } diff --git a/app/src/main/kotlin/li/songe/gkd/data/ComplexSnapshot.kt b/app/src/main/kotlin/li/songe/gkd/data/ComplexSnapshot.kt index ddee0b93d..f5ee219d0 100644 --- a/app/src/main/kotlin/li/songe/gkd/data/ComplexSnapshot.kt +++ b/app/src/main/kotlin/li/songe/gkd/data/ComplexSnapshot.kt @@ -5,7 +5,7 @@ import com.blankj.utilcode.util.ScreenUtils import kotlinx.serialization.Serializable import li.songe.gkd.BuildConfig import li.songe.gkd.service.GkdAbService -import li.songe.gkd.service.getCurrentRules +import li.songe.gkd.service.getAndUpdateCurrentRules import li.songe.gkd.service.safeActiveWindow @Serializable @@ -33,7 +33,7 @@ data class ComplexSnapshot( fun createComplexSnapshot(): ComplexSnapshot { val currentAbNode = GkdAbService.service?.safeActiveWindow val appId = currentAbNode?.packageName?.toString() - val currentActivityId = getCurrentRules().topActivity.activityId + val currentActivityId = getAndUpdateCurrentRules().topActivity.activityId val appInfo = if (appId == null) null else AppUtils.getAppInfo(appId) return ComplexSnapshot( diff --git a/app/src/main/kotlin/li/songe/gkd/data/GlobalRule.kt b/app/src/main/kotlin/li/songe/gkd/data/GlobalRule.kt index 93512370f..5a345e3d6 100644 --- a/app/src/main/kotlin/li/songe/gkd/data/GlobalRule.kt +++ b/app/src/main/kotlin/li/songe/gkd/data/GlobalRule.kt @@ -1,6 +1,5 @@ package li.songe.gkd.data -import li.songe.gkd.service.TopActivity import li.songe.gkd.service.launcherAppId data class GlobalApp( @@ -40,18 +39,18 @@ class GlobalRule( override val type = "global" private val excludeAppIds = apps.filter { e -> !e.value.enable }.keys - override fun matchActivity(topActivity: TopActivity): Boolean { - if (!matchLauncher && topActivity.appId == launcherAppId) return false - if (!super.matchActivity(topActivity)) return false - if (excludeAppIds.contains(topActivity.appId)) { + override fun matchActivity(appId: String, activityId: String?): Boolean { + if (!matchLauncher && appId == launcherAppId) return false + if (!super.matchActivity(appId, activityId)) return false + if (excludeAppIds.contains(appId)) { return false } - val app = apps[topActivity.appId] ?: return matchAnyApp - topActivity.activityId ?: return true - if (app.excludeActivityIds.any { e -> e.startsWith(topActivity.activityId) }) { + val app = apps[appId] ?: return matchAnyApp + activityId ?: return true + if (app.excludeActivityIds.any { e -> e.startsWith(activityId) }) { return false } - return app.activityIds.isEmpty() || app.activityIds.any { e -> e.startsWith(topActivity.activityId) } + return app.activityIds.isEmpty() || app.activityIds.any { e -> e.startsWith(activityId) } } } diff --git a/app/src/main/kotlin/li/songe/gkd/data/ResolvedRule.kt b/app/src/main/kotlin/li/songe/gkd/data/ResolvedRule.kt index eb7dd0143..ae1f794c6 100644 --- a/app/src/main/kotlin/li/songe/gkd/data/ResolvedRule.kt +++ b/app/src/main/kotlin/li/songe/gkd/data/ResolvedRule.kt @@ -2,7 +2,6 @@ package li.songe.gkd.data import android.view.accessibility.AccessibilityNodeInfo import kotlinx.coroutines.Job -import li.songe.gkd.service.TopActivity import li.songe.gkd.service.lastTriggerRule import li.songe.gkd.service.lastTriggerTime import li.songe.gkd.service.querySelector @@ -154,13 +153,13 @@ sealed class ResolvedRule( val excludeData = ExcludeData.parse(exclude) abstract val type: String - open fun matchActivity(topActivity: TopActivity): Boolean { - if (excludeData.appIds.contains(topActivity.appId)) { + open fun matchActivity(appId: String, activityId: String? = null): Boolean { + if (excludeData.appIds.contains(appId)) { return false } - topActivity.activityId ?: return true - excludeData.activityMap[topActivity.appId]?.let { activityIds -> - if (activityIds.any { a -> topActivity.activityId.startsWith(a) }) { + activityId ?: return true + excludeData.activityMap[appId]?.let { activityIds -> + if (activityIds.any { a -> activityId.startsWith(a) }) { return false } } diff --git a/app/src/main/kotlin/li/songe/gkd/service/AbState.kt b/app/src/main/kotlin/li/songe/gkd/service/AbState.kt index 4093b1d40..c6c456114 100644 --- a/app/src/main/kotlin/li/songe/gkd/service/AbState.kt +++ b/app/src/main/kotlin/li/songe/gkd/service/AbState.kt @@ -54,7 +54,7 @@ private fun getFixTopActivity(): TopActivity { return topActivityFlow.value } -fun getCurrentRules(): ActivityRule { +fun getAndUpdateCurrentRules(): ActivityRule { val topActivity = getFixTopActivity() val oldActivityRule = activityRuleFlow.value val allRules = allRulesFlow.value @@ -66,12 +66,10 @@ fun getCurrentRules(): ActivityRule { topActivity = topActivity, appRules = (allRules.appIdToRules[topActivity.appId] ?: emptyList()).filter { rule -> - rule.matchActivity(topActivity) + rule.matchActivity(topActivity.appId, topActivity.activityId) }, globalRules = allRulesFlow.value.globalRules.filter { r -> - r.matchActivity( - topActivity - ) + r.matchActivity(topActivity.appId, topActivity.activityId) }, ) activityRuleFlow.value = newActivityRule diff --git a/app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt b/app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt index f9f280bb8..f705b63d3 100644 --- a/app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt +++ b/app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt @@ -141,7 +141,7 @@ class GkdAbService : CompositionAbService({ queryThread } queryTaskJob = scope.launchTry(ctx) { - val activityRule = getCurrentRules() + val activityRule = getAndUpdateCurrentRules() for (rule in (activityRule.currentRules)) { val statusCode = rule.status if (statusCode == RuleStatus.Status3 && rule.matchDelayJob == null) { @@ -153,8 +153,19 @@ class GkdAbService : CompositionAbService({ } if (statusCode != RuleStatus.StatusOk) continue val nodeVal = (eventNode ?: safeActiveWindow) ?: continue + val appId = nodeVal.packageName?.toString() ?: break + if (topActivityFlow.value.appId != appId) { + eventExecutor.execute { + if (topActivityFlow.value.appId != appId) { + topActivityFlow.value = TopActivity(appId = appId) + getAndUpdateCurrentRules() + } + } + return@launchTry + } + if (!rule.matchActivity(appId)) continue val target = rule.query(nodeVal) ?: continue - if (activityRule !== getCurrentRules()) break + if (activityRule !== getAndUpdateCurrentRules()) break if (rule.checkDelay() && rule.actionDelayJob == null) { rule.actionDelayJob = scope.launch(queryThread) { delay(rule.actionDelay) @@ -269,7 +280,7 @@ class GkdAbService : CompositionAbService({ } } - if (getCurrentRules().currentRules.isEmpty()) { + if (getAndUpdateCurrentRules().currentRules.isEmpty()) { // 放在 evAppId != rightAppId 的前面使得 TopActivity 能借助 lastTopActivity 恢复 return@launch }