Skip to content

Commit

Permalink
feat: matchRoot (gkd-kit#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Jul 4, 2024
1 parent a6ba66c commit 4f8409a
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 165 deletions.
9 changes: 9 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/data/RawSubscription.kt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ data class RawSubscription(
val actionCd: Long?
val actionDelay: Long?
val quickFind: Boolean?
val matchRoot: Boolean?
val matchDelay: Long?
val matchTime: Long?
val actionMaximum: Int?
Expand Down Expand Up @@ -255,6 +256,7 @@ data class RawSubscription(
override val actionCd: Long?,
override val actionDelay: Long?,
override val quickFind: Boolean?,
override val matchRoot: Boolean?,
override val matchDelay: Long?,
override val matchTime: Long?,
override val actionMaximum: Int?,
Expand Down Expand Up @@ -293,6 +295,7 @@ data class RawSubscription(
override val actionCd: Long?,
override val actionDelay: Long?,
override val quickFind: Boolean?,
override val matchRoot: Boolean?,
override val matchDelay: Long?,
override val matchTime: Long?,
override val actionMaximum: Int?,
Expand Down Expand Up @@ -329,6 +332,7 @@ data class RawSubscription(
override val actionCd: Long?,
override val actionDelay: Long?,
override val quickFind: Boolean?,
override val matchRoot: Boolean?,
override val actionMaximum: Int?,
override val order: Int?,
override val forcedTime: Long?,
Expand Down Expand Up @@ -373,6 +377,7 @@ data class RawSubscription(
override val actionCd: Long?,
override val actionDelay: Long?,
override val quickFind: Boolean?,
override val matchRoot: Boolean?,
override val actionMaximum: Int?,
override val order: Int?,
override val forcedTime: Long?,
Expand Down Expand Up @@ -586,6 +591,7 @@ data class RawSubscription(
preKeys = getIntIArray(jsonObject, "preKeys"),
action = getString(jsonObject, "action"),
quickFind = getBoolean(jsonObject, "quickFind"),
matchRoot = getBoolean(jsonObject, "matchRoot"),
actionMaximum = getInt(jsonObject, "actionMaximum"),
matchDelay = getLong(jsonObject, "matchDelay"),
matchTime = getLong(jsonObject, "matchTime"),
Expand Down Expand Up @@ -628,6 +634,7 @@ data class RawSubscription(
jsonToRuleRaw(it)
},
quickFind = getBoolean(jsonObject, "quickFind"),
matchRoot = getBoolean(jsonObject, "matchRoot"),
actionMaximum = getInt(jsonObject, "actionMaximum"),
matchDelay = getLong(jsonObject, "matchDelay"),
matchTime = getLong(jsonObject, "matchTime"),
Expand Down Expand Up @@ -681,6 +688,7 @@ data class RawSubscription(
actionCd = getLong(jsonObject, "actionCd"),
actionDelay = getLong(jsonObject, "actionDelay"),
quickFind = getBoolean(jsonObject, "quickFind"),
matchRoot = getBoolean(jsonObject, "matchRoot"),
actionMaximum = getInt(jsonObject, "actionMaximum"),
matchDelay = getLong(jsonObject, "matchDelay"),
matchTime = getLong(jsonObject, "matchTime"),
Expand Down Expand Up @@ -717,6 +725,7 @@ data class RawSubscription(
actionCd = getLong(jsonObject, "actionCd"),
actionDelay = getLong(jsonObject, "actionDelay"),
quickFind = getBoolean(jsonObject, "quickFind"),
matchRoot = getBoolean(jsonObject, "matchRoot"),
actionMaximum = getInt(jsonObject, "actionMaximum"),
matchDelay = getLong(jsonObject, "matchDelay"),
matchTime = getLong(jsonObject, "matchTime"),
Expand Down
43 changes: 34 additions & 9 deletions app/src/main/kotlin/li/songe/gkd/data/ResolvedRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package li.songe.gkd.data
import android.accessibilityservice.AccessibilityService
import android.view.accessibility.AccessibilityNodeInfo
import kotlinx.coroutines.Job
import li.songe.gkd.service.GkdAbService
import li.songe.gkd.service.createCacheTransform
import li.songe.gkd.service.createNoCacheTransform
import li.songe.gkd.service.lastTriggerRule
import li.songe.gkd.service.lastTriggerTime
import li.songe.gkd.service.querySelector
import li.songe.gkd.service.safeActiveWindow
import li.songe.gkd.util.ResolvedGroup
import li.songe.selector.Selector

Expand All @@ -32,6 +34,7 @@ sealed class ResolvedRule(
private val matchTime = rule.matchTime ?: group.matchTime
private val forcedTime = rule.forcedTime ?: group.forcedTime ?: 0L
private val quickFind = rule.quickFind ?: group.quickFind ?: false
private val matchRoot = rule.matchRoot ?: group.matchRoot ?: false
val order = rule.order ?: group.order ?: 0

private val actionCdKey = rule.actionCdKey ?: group.actionCdKey
Expand All @@ -50,7 +53,7 @@ sealed class ResolvedRule(

private val slowSelectors by lazy {
(matches + excludeMatches + anyMatches).filterNot { s ->
((quickFind && s.canQf) || s.isMatchRoot) && !s.connectKeys.contains(
((quickFind && s.quickFindValue.canQf) || s.isMatchRoot) && !s.connectKeys.contains(
"<<"
)
}
Expand Down Expand Up @@ -134,27 +137,49 @@ sealed class ResolvedRule(
private val transform = if (useCache) defaultCacheTransform else defaultTransform

fun query(
nodeInfo: AccessibilityNodeInfo?,
node: AccessibilityNodeInfo?,
isRootNode: Boolean,
): AccessibilityNodeInfo? {
val nodeInfo = if (matchRoot) {
val rootNode = (if (isRootNode) {
node
} else {
GkdAbService.service?.safeActiveWindow
}) ?: return null
rootNode.apply {
transform.cache.parentMap[this] = null
}
} else {
node
}
try {
if (nodeInfo == null) return null
var target: AccessibilityNodeInfo? = null
if (anyMatches.isNotEmpty()) {
for (selector in anyMatches) {
target = nodeInfo.querySelector(selector, quickFind, transform.transform)
?: break
target = nodeInfo.querySelector(
selector,
quickFind,
transform.transform,
isRootNode || matchRoot
) ?: break
}
if (target == null) return null
}
for (selector in matches) {
target = nodeInfo.querySelector(selector, quickFind, transform.transform)
?: return null
target = nodeInfo.querySelector(
selector,
quickFind,
transform.transform,
isRootNode || matchRoot
) ?: return null
}
for (selector in excludeMatches) {
nodeInfo.querySelector(
selector,
quickFind,
transform.transform
transform.transform,
isRootNode || matchRoot
)?.let { return null }
}
return target
Expand Down Expand Up @@ -248,7 +273,7 @@ fun getFixActivityIds(
}
}

private val defaultTransform = createNoCacheTransform()
private val defaultCacheTransform = createCacheTransform()
private val defaultTransform by lazy { createNoCacheTransform() }
private val defaultCacheTransform by lazy { createCacheTransform() }


Loading

0 comments on commit 4f8409a

Please sign in to comment.