Skip to content

Commit

Permalink
fix: 规则触发匹配不一致 (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Jan 9, 2024
1 parent 8a8de5d commit 0f0c54d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 31 deletions.
11 changes: 5 additions & 6 deletions app/src/main/kotlin/li/songe/gkd/data/AppRule.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package li.songe.gkd.data

import li.songe.gkd.service.TopActivity

class AppRule(
rule: RawSubscription.RawAppRule,
subsItem: SubsItem,
Expand All @@ -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) }
}
}
4 changes: 2 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/data/ComplexSnapshot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
17 changes: 8 additions & 9 deletions app/src/main/kotlin/li/songe/gkd/data/GlobalRule.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package li.songe.gkd.data

import li.songe.gkd.service.TopActivity
import li.songe.gkd.service.launcherAppId

data class GlobalApp(
Expand Down Expand Up @@ -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) }
}

}
11 changes: 5 additions & 6 deletions app/src/main/kotlin/li/songe/gkd/data/ResolvedRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down
8 changes: 3 additions & 5 deletions app/src/main/kotlin/li/songe/gkd/service/AbState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
17 changes: 14 additions & 3 deletions app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -269,7 +280,7 @@ class GkdAbService : CompositionAbService({
}
}

if (getCurrentRules().currentRules.isEmpty()) {
if (getAndUpdateCurrentRules().currentRules.isEmpty()) {
// 放在 evAppId != rightAppId 的前面使得 TopActivity 能借助 lastTopActivity 恢复
return@launch
}
Expand Down

0 comments on commit 0f0c54d

Please sign in to comment.