I attempted to use the accessibility service to click a button on the node below. The node is clickable, and it clicks only first time, even when I manually write text in the textfield, but not when I enter text using code.
Node:
[androidx.core.view.accessibility.AccessibilityNodeInfoCompat@81f3a; boundsInParent: Rect(0, 0 - 87, 62); boundsInScreen: Rect(585, 953 - 672, 1015); packageName: com.example.android; className: android.widget.TextView; text: Send; contentDescription: null; viewId: null; checkable: false; checked: false; focusable: true; focused: false; selected: false; clickable: true; longClickable: false; enabled: true; password: false; scrollable: false; [ACTION_FOCUS, ACTION_SELECT, ACTION_CLEAR_SELECTION, ACTION_CLICK, ACTION_ACCESSIBILITY_FOCUS, ACTION_NEXT_AT_MOVEMENT_GRANULARITY, ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY, ACTION_SET_SELECTION, ACTION_SHOW_ON_SCREEN]]
code:
public void onAccessibilityEvent(AccessibilityEvent event) {
if (getRootInActiveWindow() == null) {
return;
}
AccessibilityNodeInfoCompat rootInActiveWindow = AccessibilityNodeInfoCompat.wrap(getRootInActiveWindow());
new Handler().postDelayed(() -> {
List<AccessibilityNodeInfoCompat> replyNode = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.example.android:id/comments");
if (!replyNode.isEmpty() && !isreply1) {
System.out.println("Found nodes:" + replyNode);
System.out.println("Number of nodes:" + replyNode.size());
AccessibilityNodeInfoCompat replybtnNode = replyNode.get(0);
replybtnNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
isreply1 = true;
} else {
System.out.println("No nodes found");
}
}, 6000);
new Handler().postDelayed(() -> {
AccessibilityNodeInfoCompat replyNode2 = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.example.android:id/comments_input").get(0);
if (!isreply2) {
System.out.println("Input field");
replyNode2.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
replyNode2.performAction(AccessibilityNodeInfo.ACTION_CLICK);
String commentText = "Nice";
Bundle arguments = new Bundle();
arguments.putCharSequence(
AccessibilityNodeInfoCompat.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,
commentText
);
replyNode2.performAction(AccessibilityNodeInfoCompat.ACTION_SET_TEXT, arguments);
isreply2 = true;
}
}, 9000);
new Handler().postDelayed(() -> {
List<AccessibilityNodeInfoCompat> replyNode3 = rootInActiveWindow.findAccessibilityNodeInfosByText("Send");
if (!replyNode3.isEmpty()) {
System.out.println("Found reply nodes:" + replyNode3);
System.out.println("Number of reply nodes:" + replyNode3.size());
AccessibilityNodeInfoCompat replybtnNode2 = replyNode3.get(0);
replybtnNode2.performAction(AccessibilityNodeInfo.ACTION_CLICK);
} else {
System.out.println("No reply nodes found");
}
}, 12000);
}