feat: vid quickFind

This commit is contained in:
lisonge 2023-12-14 23:10:14 +08:00
parent cd70a02221
commit 327716da09
2 changed files with 44 additions and 21 deletions

View File

@ -57,7 +57,6 @@ fun AccessibilityNodeInfo.getDepth(): Int {
return depth
}
fun AccessibilityNodeInfo.querySelector(
selector: Selector,
quickFind: Boolean = false,
@ -69,21 +68,27 @@ fun AccessibilityNodeInfo.querySelector(
}
return null
}
if (quickFind) {
val canQuickFind = selector.canQuickFind
if (canQuickFind != null) {
// 使用 findAccessibilityNodeInfosByXX 无法查询深层次节点
val trackNodes = mutableListOf<AccessibilityNodeInfo>()
(if (selector.canQuickFind!!.first) {
findAccessibilityNodeInfosByViewId(canQuickFind.second)
if (quickFind && selector.canQf) {
val qfIdValue = selector.qfIdValue
val qfVidValue = selector.qfVidValue
val qfTextValue = selector.qfTextValue
val nodes = (if (qfIdValue != null) {
findAccessibilityNodeInfosByViewId(qfIdValue)
} else if (qfVidValue != null) {
findAccessibilityNodeInfosByViewId("$packageName:id/$qfVidValue")
} else if (qfTextValue != null) {
findAccessibilityNodeInfosByText(qfTextValue)
} else {
findAccessibilityNodeInfosByText(canQuickFind.second)
}).forEach { childNode ->
emptyList()
})
if (nodes.isNotEmpty()) {
val trackNodes = mutableListOf<AccessibilityNodeInfo>()
nodes.forEach { childNode ->
val targetNode = selector.match(childNode, abTransform, trackNodes)
if (targetNode != null) return targetNode
}
return null
}
return null
}
// 在一些开屏广告的界面会造成1-2s的阻塞
return abTransform.querySelector(this, selector)
@ -113,6 +118,13 @@ private val getChildren: (AccessibilityNodeInfo) -> Sequence<AccessibilityNodeIn
private val getAttr: (AccessibilityNodeInfo, String) -> Any? = { node, name ->
when (name) {
"id" -> node.viewIdResourceName
"vid" -> node.viewIdResourceName?.let { id ->
id.subSequence(
(node.packageName?.length ?: 0) + ":id/".length,
id.length
)
}
"name" -> node.className
"text" -> node.text
"text.length" -> node.text?.length

View File

@ -40,21 +40,32 @@ class Selector internal constructor(private val propertyWrapper: PropertyWrapper
return propertyWrapper.matchTracks(node, transform, trackNodes)
}
// true: use findById, false: use findByText
val canQuickFind = propertyWrapper.propertySegment.expressions.firstOrNull().let { e ->
if (e is BinaryExpression && e.value is String) {
if (e.name == "id" && e.operator == CompareOperator.Equal) {
true to e.value
} else if (e.name == "text" && (e.operator == CompareOperator.Equal || e.operator == CompareOperator.Start || e.operator == CompareOperator.Include || e.operator == CompareOperator.End)) {
false to e.value
} else {
null
}
val qfIdValue = propertyWrapper.propertySegment.expressions.firstOrNull().let { e ->
if (e is BinaryExpression && e.name == "id" && e.operator == CompareOperator.Equal && e.value is String) {
e.value
} else {
null
}
}
val qfVidValue = propertyWrapper.propertySegment.expressions.firstOrNull().let { e ->
if (e is BinaryExpression && e.name == "vid" && e.operator == CompareOperator.Equal && e.value is String) {
e.value
} else {
null
}
}
val qfTextValue = propertyWrapper.propertySegment.expressions.firstOrNull().let { e ->
if (e is BinaryExpression && e.name == "id" && (e.operator == CompareOperator.Equal || e.operator == CompareOperator.Start || e.operator == CompareOperator.Include || e.operator == CompareOperator.End) && e.value is String) {
e.value
} else {
null
}
}
val canQf = qfIdValue != null || qfVidValue != null || qfTextValue != null
// 主动查询
val isMatchRoot = propertyWrapper.propertySegment.expressions.firstOrNull().let { e ->
e is BinaryExpression && e.name == "depth" && e.operator == CompareOperator.Equal && e.value == 0