feat: 支持长按 (#181)

This commit is contained in:
lisonge 2023-11-17 21:10:41 +08:00
parent 594e303cd7
commit 5a03ce5136
3 changed files with 48 additions and 4 deletions

View File

@ -15,6 +15,7 @@ data class AttrInfo(
val focusable: Boolean,
val checkable: Boolean,
val checked: Boolean,
val longClickable: Boolean,
val visibleToUser: Boolean,
val left: Int,
@ -51,6 +52,7 @@ data class AttrInfo(
focusable = node.isFocusable,
checkable = node.isCheckable,
checked = node.isChecked,
longClickable = node.isLongClickable,
visibleToUser = node.isVisibleToUser,
left = rect.left,

View File

@ -134,10 +134,6 @@ data class ActionResult(
val result: Boolean,
)
val click: ActionFc = { context, node ->
if (node.isClickable) clickNode(context, node) else clickCenter(context, node)
}
val clickNode: ActionFc = { _, node ->
ActionResult(
action = "clickNode", result = node.performAction(AccessibilityNodeInfo.ACTION_CLICK)
@ -166,7 +162,49 @@ val clickCenter: ActionFc = { context, node ->
false
}
)
}
val click: ActionFc = { context, node ->
if (node.isClickable) clickNode(context, node) else clickCenter(context, node)
}
val longClickNode: ActionFc = { _, node ->
ActionResult(
action = "longClickNode",
result = node.performAction(AccessibilityNodeInfo.ACTION_LONG_CLICK)
)
}
val longClickCenter: ActionFc = { context, node ->
val react = Rect()
node.getBoundsInScreen(react)
val x = (react.right + react.left) / 2f
val y = (react.bottom + react.top) / 2f
// 内部的 DEFAULT_LONG_PRESS_TIMEOUT 常量是 400
// 而 ViewConfiguration.getLongPressTimeout() 返回 300, 这将导致触发普通的 click 事件
ActionResult(
action = "longClickCenter",
result = if (0 <= x && 0 <= y && x <= ScreenUtils.getScreenWidth() && y <= ScreenUtils.getScreenHeight()) {
val gestureDescription = GestureDescription.Builder()
val path = Path()
path.moveTo(x, y)
gestureDescription.addStroke(
GestureDescription.StrokeDescription(
path, 0, 400L
)
)
// TODO 传入处理 callback
context.dispatchGesture(gestureDescription.build(), null, null)
true
} else {
false
}
)
}
val longClick: ActionFc = { context, node ->
if (node.isLongClickable) longClickNode(context, node) else longClickCenter(context, node)
}
val backFc: ActionFc = { context, _ ->
@ -181,6 +219,9 @@ fun getActionFc(action: String?): ActionFc {
"clickNode" -> clickNode
"clickCenter" -> clickCenter
"back" -> backFc
"longClick" -> longClick
"longClickNode" -> longClickNode
"longClickCenter" -> longClickCenter
else -> click
}
}

View File

@ -123,6 +123,7 @@ private val getAttr: (AccessibilityNodeInfo, String) -> Any? = { node, name ->
"checkable" -> node.isCheckable
"checked" -> node.isChecked
"focusable" -> node.isFocusable
"longClickable" -> node.isLongClickable
"visibleToUser" -> node.isVisibleToUser
"left" -> node.getTempRect().left