getRootInActiveWindow 中的 AccessibilityNodeInfo:执行单击失败,Rect() 不包含 X、Y 坐标
AccessibilityNodeInfo in getRootInActiveWindow: Perform click fails with Rect() not contains X, Y coordenates
我正在尝试使用以下代码执行点击,但 here 说存在一些限制,我可以在我的测试中看到这一点。
但是这个bug好像是因为Rect()
不包含X,Y坐标,为什么每次点击在不支持的地方(可能有一些限制,就像在示例的自述文件中所说的那样)在 findSmallestNodeAtPoint()
例程中执行此行:
if (!bounds.contains(x, y)) {
System.out.println("ERROR DETECTED!!! :::::: NOT bounds.contains(x, y) :::::::");
return null;
}
在这个问题中,也提到了这个 Github 的例子,并给出了一个代码示例的答案,该代码示例 100%(测试)来自 android Nougat+(api 24 及以上),但在我的情况下,我需要将下面的代码修复到我的应用程序中,还支持在以前版本的 android.
中执行点击
那么已经知道这段代码哪里出错了,我想知道可以做些什么:
!bounds.contains(x, y)
代码的主要部分是:
private static void logNodeHierachy(AccessibilityNodeInfo nodeInfo, int depth) {
Rect bounds = new Rect();
nodeInfo.getBoundsInScreen(bounds);
StringBuilder sb = new StringBuilder();
if (depth > 0) {
for (int i=0; i<depth; i++) {
sb.append(" ");
}
sb.append("\u2514 ");
}
sb.append(nodeInfo.getClassName());
sb.append(" (" + nodeInfo.getChildCount() + ")");
sb.append(" " + bounds.toString());
if (nodeInfo.getText() != null) {
sb.append(" - \"" + nodeInfo.getText() + "\"");
}
System.out.println(sb.toString());
for (int i=0; i<nodeInfo.getChildCount(); i++) {
AccessibilityNodeInfo childNode = nodeInfo.getChild(i);
if (childNode != null) {
logNodeHierachy(childNode, depth + 1);
}
}
}
private static AccessibilityNodeInfo findSmallestNodeAtPoint(AccessibilityNodeInfo sourceNode, int x, int y) {
Rect bounds = new Rect();
sourceNode.getBoundsInScreen(bounds);
if (!bounds.contains(x, y)) {
System.out.println(":::::: NOT bounds.contains(x, y) :::::::");
return null;
}
for (int i=0; i<sourceNode.getChildCount(); i++) {
AccessibilityNodeInfo nearestSmaller = findSmallestNodeAtPoint(sourceNode.getChild(i), x, y);
if (nearestSmaller != null) {
return nearestSmaller;
}
}
return sourceNode;
}
public void click(int x, int y) {
System.out.println(String.format("Click [%d, %d]", x, y));
AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
if (nodeInfo == null) return;
AccessibilityNodeInfo nearestNodeToMouse = findSmallestNodeAtPoint(nodeInfo, x, y);
if (nearestNodeToMouse != null) {
logNodeHierachy(nearestNodeToMouse, 0);
nearestNodeToMouse.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
nodeInfo.recycle();
}
好吧,我回答我自己的问题,AccessibilityService
的专家已经认为我正在构建恶意软件而不喜欢帮助这类人。
显然似乎不可能逃避使用 Rect()
和 getBoundsInScreen()
基于 this 答案。
我还发现了另一个类似的代码,ref:
public void click(int x, int y) {
clickAtPosition(x, y, getRootInActiveWindow());
}
public static void clickAtPosition(int x, int y, AccessibilityNodeInfo node) {
if (node == null) return;
if (node.getChildCount() == 0) {
Rect buttonRect = new Rect();
node.getBoundsInScreen(buttonRect);
if (buttonRect.contains(x, y)) {
// Maybe we need to think if a large view covers item?
node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
System.out.println("1º - Node Information: " + node.toString());
}
} else {
Rect buttonRect = new Rect();
node.getBoundsInScreen(buttonRect);
if (buttonRect.contains(x, y)) {
// Maybe we need to think if a large view covers item?
node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
System.out.println("2º - Node Information: " + node.toString());
}
for (int i = 0; i < node.getChildCount(); i++) {
clickAtPosition(x, y, node.getChild(i));
}
}
}
也有麻烦。但是比上面问题链接的代码更好:D。
在我的测试中,只有无法执行单击 android 虚拟键盘。
我正在尝试使用以下代码执行点击,但 here 说存在一些限制,我可以在我的测试中看到这一点。
但是这个bug好像是因为Rect()
不包含X,Y坐标,为什么每次点击在不支持的地方(可能有一些限制,就像在示例的自述文件中所说的那样)在 findSmallestNodeAtPoint()
例程中执行此行:
if (!bounds.contains(x, y)) {
System.out.println("ERROR DETECTED!!! :::::: NOT bounds.contains(x, y) :::::::");
return null;
}
那么已经知道这段代码哪里出错了,我想知道可以做些什么:
!bounds.contains(x, y)
代码的主要部分是:
private static void logNodeHierachy(AccessibilityNodeInfo nodeInfo, int depth) {
Rect bounds = new Rect();
nodeInfo.getBoundsInScreen(bounds);
StringBuilder sb = new StringBuilder();
if (depth > 0) {
for (int i=0; i<depth; i++) {
sb.append(" ");
}
sb.append("\u2514 ");
}
sb.append(nodeInfo.getClassName());
sb.append(" (" + nodeInfo.getChildCount() + ")");
sb.append(" " + bounds.toString());
if (nodeInfo.getText() != null) {
sb.append(" - \"" + nodeInfo.getText() + "\"");
}
System.out.println(sb.toString());
for (int i=0; i<nodeInfo.getChildCount(); i++) {
AccessibilityNodeInfo childNode = nodeInfo.getChild(i);
if (childNode != null) {
logNodeHierachy(childNode, depth + 1);
}
}
}
private static AccessibilityNodeInfo findSmallestNodeAtPoint(AccessibilityNodeInfo sourceNode, int x, int y) {
Rect bounds = new Rect();
sourceNode.getBoundsInScreen(bounds);
if (!bounds.contains(x, y)) {
System.out.println(":::::: NOT bounds.contains(x, y) :::::::");
return null;
}
for (int i=0; i<sourceNode.getChildCount(); i++) {
AccessibilityNodeInfo nearestSmaller = findSmallestNodeAtPoint(sourceNode.getChild(i), x, y);
if (nearestSmaller != null) {
return nearestSmaller;
}
}
return sourceNode;
}
public void click(int x, int y) {
System.out.println(String.format("Click [%d, %d]", x, y));
AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
if (nodeInfo == null) return;
AccessibilityNodeInfo nearestNodeToMouse = findSmallestNodeAtPoint(nodeInfo, x, y);
if (nearestNodeToMouse != null) {
logNodeHierachy(nearestNodeToMouse, 0);
nearestNodeToMouse.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
nodeInfo.recycle();
}
好吧,我回答我自己的问题,AccessibilityService
的专家已经认为我正在构建恶意软件而不喜欢帮助这类人。
显然似乎不可能逃避使用 Rect()
和 getBoundsInScreen()
基于 this 答案。
我还发现了另一个类似的代码,ref:
public void click(int x, int y) {
clickAtPosition(x, y, getRootInActiveWindow());
}
public static void clickAtPosition(int x, int y, AccessibilityNodeInfo node) {
if (node == null) return;
if (node.getChildCount() == 0) {
Rect buttonRect = new Rect();
node.getBoundsInScreen(buttonRect);
if (buttonRect.contains(x, y)) {
// Maybe we need to think if a large view covers item?
node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
System.out.println("1º - Node Information: " + node.toString());
}
} else {
Rect buttonRect = new Rect();
node.getBoundsInScreen(buttonRect);
if (buttonRect.contains(x, y)) {
// Maybe we need to think if a large view covers item?
node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
System.out.println("2º - Node Information: " + node.toString());
}
for (int i = 0; i < node.getChildCount(); i++) {
clickAtPosition(x, y, node.getChild(i));
}
}
}
也有麻烦。但是比上面问题链接的代码更好:D。
在我的测试中,只有无法执行单击 android 虚拟键盘。