3

I try to implement auto-login functionality in third party applications.

I try to copy and paste username/password in login page. To achieve it I use Android accessibility services.

I am able to paste text on different devices. But on Samsung devices the username/password is pasting in wrong input fileds.

The log shows me that the username/password is copying as expected. But while pasting it pastes wrong text.

This is what I do:

private ClipboardManager mClipboardManager;
mClipboardManager = (ClipboardManager) getApplication().getApplicationContext().getSystemService(Context.CLIPBOARD_SERVICE);

ClipData clip = ClipData.newPlainText("", textToPaste);
mClipboardManager.setPrimaryClip(clip);
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_PASTE);
clip = ClipData.newPlainText("", "");
mClipboardManager.setPrimaryClip(clip);

clip = ClipData.newPlainText("", textToPaste2);
mClipboardManager.setPrimaryClip(clip);
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_PASTE);
clip = ClipData.newPlainText("", "");
mClipboardManager.setPrimaryClip(clip);

Could you please help me to find the solution? How can I paste the correct text?

Yuliya Tarasenko
  • 437
  • 2
  • 19

1 Answers1

2

I also had this issue, and I've noticed it's an open question here as well and it hasn't been properly addressed. The issue seems to be a race condition with Samsung's own clipboard and how they've implemented Accessibility to work with it. I've got a functional but not so pretty solution working:

if (Build.MANUFACTURER.toString().equals("samsung")) {
    Handler handler = new Handler();
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            getCurrentNode().performAction(AccessibilityNodeInfo.ACTION_PASTE);
        }
    };
    handler.postDelayed(runnable, 100);
} else {
    getCurrentNode().performAction(AccessibilityNodeInfo.ACTION_PASTE);
}

So essentially we wait a little (100ms, YMMV) for the clipboard to settle before doing our paste. I admit this isn't perfect, but this will work for API 18+.

Another option for API 21+ is to use AccessibilityNodeInfo.ACTION_SET_TEXT as opposed to paste (for text only, obviously), and inserting the correct piece of text that way. Here's an example of how that works:

Bundle arguments = new Bundle();
arguments.putString(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "node text plus pasted text");
getCurrentNode().performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);

NOTE: the above does not manage the cursor and so will always place it at the end of the textfield. You can manage the cursor with AccessibilityNodeInfo.ACTION_SET_CURSOR and a bundle (in a similar manner to above for setting the text)

stewjacks
  • 471
  • 6
  • 12