fix: 规则触发匹配不一致 (#465)

This commit is contained in:
lisonge 2024-01-09 15:16:05 +08:00
parent 8a8de5d801
commit 0f0c54d053
6 changed files with 37 additions and 31 deletions

View File

@ -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) }
}
}

View File

@ -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(

View File

@ -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) }
}
}

View File

@ -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
}
}

View File

@ -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

View File

@ -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
}