perf: delayRule

This commit is contained in:
lisonge 2024-09-10 00:00:13 +08:00
parent 51852de92b
commit f859fa1d15

View File

@ -36,6 +36,7 @@ import li.songe.gkd.data.ActionResult
import li.songe.gkd.data.AppRule import li.songe.gkd.data.AppRule
import li.songe.gkd.data.AttrInfo import li.songe.gkd.data.AttrInfo
import li.songe.gkd.data.GkdAction import li.songe.gkd.data.GkdAction
import li.songe.gkd.data.ResolvedRule
import li.songe.gkd.data.RpcError import li.songe.gkd.data.RpcError
import li.songe.gkd.data.RuleStatus import li.songe.gkd.data.RuleStatus
import li.songe.gkd.data.clearNodeCache import li.songe.gkd.data.clearNodeCache
@ -145,35 +146,44 @@ class GkdAbService : CompositionAbService({
var lastContentEventTime = 0L var lastContentEventTime = 0L
val events = mutableListOf<AccessibilityNodeInfo>() val events = mutableListOf<AccessibilityNodeInfo>()
var queryTaskJob: Job? = null var queryTaskJob: Job? = null
fun newQueryTask(byEvent: Boolean = false, byForced: Boolean = false) { fun newQueryTask(
byEvent: Boolean = false,
byForced: Boolean = false,
delayRule: ResolvedRule? = null
) {
if (!storeFlow.value.enableMatch) return if (!storeFlow.value.enableMatch) return
queryTaskJob = scope.launchTry(queryThread) { queryTaskJob = scope.launchTry(queryThread) {
var latestEvent = synchronized(events) { var latestEvent = if (delayRule != null) {// 延迟规则不消耗事件
val size = events.size null
if (size == 0 && byEvent) return@launchTry } else {
val pair = if (size > 1) { synchronized(events) {
if (META.debuggable) { val size = events.size
Log.d("latestEvent", "丢弃事件=$size") if (size == 0 && byEvent) return@launchTry
val node = if (size > 1) {
if (META.debuggable) {
Log.d("latestEvent", "丢弃事件=$size")
}
null
} else {
events.lastOrNull()
} }
null events.clear()
} else { node
events.lastOrNull()
} }
events.clear()
pair
} }
val activityRule = getAndUpdateCurrentRules() val activityRule = getAndUpdateCurrentRules()
if (activityRule.currentRules.isEmpty()) { if (activityRule.currentRules.isEmpty()) {
return@launchTry return@launchTry
} }
clearNodeCache() clearNodeCache()
for (rule in (activityRule.currentRules)) { // 规则数量有可能过多导致耗时过长 for (rule in activityRule.currentRules) { // 规则数量有可能过多导致耗时过长
if (delayRule != null && delayRule !== rule) continue
val statusCode = rule.status val statusCode = rule.status
if (statusCode == RuleStatus.Status3 && rule.matchDelayJob == null) { if (statusCode == RuleStatus.Status3 && rule.matchDelayJob == null) {
rule.matchDelayJob = scope.launch(actionThread) { rule.matchDelayJob = scope.launch(actionThread) {
delay(rule.matchDelay) delay(rule.matchDelay)
rule.matchDelayJob = null rule.matchDelayJob = null
newQueryTask() newQueryTask(delayRule = rule)
} }
} }
if (statusCode != RuleStatus.StatusOk) continue if (statusCode != RuleStatus.StatusOk) continue
@ -223,7 +233,7 @@ class GkdAbService : CompositionAbService({
rule.actionDelayJob = scope.launch(actionThread) { rule.actionDelayJob = scope.launch(actionThread) {
delay(rule.actionDelay) delay(rule.actionDelay)
rule.actionDelayJob = null rule.actionDelayJob = null
newQueryTask() newQueryTask(delayRule = rule)
} }
continue continue
} }