Fill in EditText of any app from (Accessibility)Service?
Asked Answered
T

2

7

How does Lastpass manage this?!

AccessibilityNodeInfo has a setText() method, but I feel like this is a red herring as the docs state,

Note: Cannot be called from an AccessibilityService. This class is made immutable before being delivered to an AccessibilityService.

Another user asked a similar question a while back, but the recent updates to LastPass prove that it is indeed possible.

Set text in AccessibilityNodeInfo

Talcahuano answered 16/4, 2014 at 16:21 Comment(1)
They are probably using ACTION_PASTE and ClipboardManager.Russel
I
8

I found some better solution rather than ACTION_PASTE. I feel ACTION_PASTE makes delay and didn't work properly. ACTION_SET_TEXT works fine for me, check with you.

public void pasteText(AccessibilityNodeInfo node, String text) {
        Bundle arguments = new Bundle();
        arguments.putString(AccessibilityNodeInfoCompat.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, text);
        node.performAction(AccessibilityNodeInfoCompat.ACTION_SET_TEXT, arguments);
    }
Ionic answered 8/3, 2017 at 10:34 Comment(1)
Looks like a much better solution as it doesnt expose the content to the clipboard managers either.Talcahuano
T
4

I have figured this out and have it implemented in my app, TapN.

First get the original clipboard contents, save that, copy to the clipboard your content, then paste it, then copy the original content back.

    public void inputData(Context c, String data, AccessibilityNodeInfo source) {
    try {

            String lastClip = clipboard.getPrimaryClip().getItemAt(0).coerceToText(c)
                    .toString();
        } catch (Exception e) {
            lastClip = "";
        }
        Log.d("THE NODE INFO", source.toString());

        ClipData clip = ClipData.newPlainText("nfc_input", data);
        clipboard.setPrimaryClip(clip);

        Log.d("SENDING DATA", Boolean.toString(source.refresh()));
        Log.d("SENDING DATA", Boolean.toString(source
                .performAction(AccessibilityNodeInfo.ACTION_PASTE)));
        ClipData clip = ClipData.newPlainText("nfc_input", lastClip);
        clipboard.setPrimaryClip(clip);
}
Talcahuano answered 28/4, 2014 at 15:27 Comment(2)
API doc says ACTION_PASTE requires API level 18. How can we do the same for API level 14?Bemean
For below android 18 use AccessibilityNodeInfoCompat.ACTION_PASTEHirai

© 2022 - 2024 — McMap. All rights reserved.