Skip to content

Commit

Permalink
feat: 支持长按 (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Nov 17, 2023
1 parent 594e303 commit 5a03ce5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/li/songe/gkd/data/AttrInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
49 changes: 45 additions & 4 deletions app/src/main/java/li/songe/gkd/data/Rule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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, _ ->
Expand All @@ -181,6 +219,9 @@ fun getActionFc(action: String?): ActionFc {
"clickNode" -> clickNode
"clickCenter" -> clickCenter
"back" -> backFc
"longClick" -> longClick
"longClickNode" -> longClickNode
"longClickCenter" -> longClickCenter
else -> click
}
}
1 change: 1 addition & 0 deletions app/src/main/java/li/songe/gkd/service/AbExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5a03ce5

Please sign in to comment.