Reputation: 1110
I am using an AccessibilityService
in my app to paste some text in the EditText
of another app.
I am using the following code to achieve the same,
Bundle bundle = new Bundle();
bundle.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT,
AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD);
bundle.putBoolean(AccessibilityNodeInfo.ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN,
true);
eventSource.performAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
bundle);
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("text", stringToCopy);
clipboard.setPrimaryClip(clip);
eventSource.performAction(AccessibilityNodeInfo.ACTION_PASTE);
This works fine for normal cases. However, when I open up an AlertDialog
from the AccessibilityService
using
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
and execute the above code on dialog button press, it behaves abnormally.
Case 1: Works fine and text gets pasted from dialog
Case 2: Text gets pasted but selection is not made and therefore the previous word remains in the field
Case 3: Nothing happens, just the dialog dismisses.
However, in all cases the text is getting copied to Clipboard and can be pasted manually.
Any clues?
Upvotes: 3
Views: 2121
Reputation: 1110
I finally got it to work, and this is how,
Initially I was calling, Dialog.dismiss()
and AccessibilityNodeInfo.performAction()
at the same time, which was causing a conflict as the Window
was holding the view of the Dialog
as the current active view and thus was not able to link to the AccessibilityNode
.
I modified my code to this,
mDialog.dismiss();
new Handler().postDelayed(() -> {
Bundle bundle = new Bundle();
bundle.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT,
AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD);
bundle.putBoolean(AccessibilityNodeInfo.ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN,
true);
eventSource.performAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
bundle);
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("text", stringToCopy);
clipboard.setPrimaryClip(clip);
eventSource.performAction(AccessibilityNodeInfo.ACTION_PASTE);
}, 300);
Now it works every time properly, after providing a delay in calling the performAction()
and give enough time for the Dialog
to dismiss completely.
Upvotes: 4