Add a new method for text injection

On Android >= 7, inject text using the clipboard.

This is faster and allows to inject UTF-8.
This commit is contained in:
Romain Vimont 2020-05-25 02:29:23 +02:00
parent cc4e1e20de
commit d613b10efc
2 changed files with 30 additions and 1 deletions

View File

@ -149,6 +149,10 @@ public class Controller {
}
private int injectText(String text) {
if (device.injectTextPaste(text)) {
// The best method (fastest and UTF-8) worked!
return text.length();
}
int successCount = 0;
for (char c : text.toCharArray()) {
if (!injectChar(c)) {

View File

@ -186,6 +186,27 @@ public final class Device {
return injectKeycode(keyCode, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
}
public boolean injectTextPaste(String text) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
return false;
}
// On Android >= 7, we can inject UTF-8 text as follow:
// - set the clipboard
// - inject the PASTE key event
// - restore the clipboard
String clipboardBackup = getClipboardText();
isSettingClipboard.set(true);
rawSetClipboardText(text);
boolean ok = injectKeycode(KeyEvent.KEYCODE_PASTE, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT);
rawSetClipboardText(clipboardBackup);
isSettingClipboard.set(false);
return ok;
}
public boolean isScreenOn() {
return serviceManager.getPowerManager().isScreenOn();
}
@ -214,9 +235,13 @@ public final class Device {
return s.toString();
}
private boolean rawSetClipboardText(String text) {
return serviceManager.getClipboardManager().setText(text);
}
public boolean setClipboardText(String text) {
isSettingClipboard.set(true);
boolean ok = serviceManager.getClipboardManager().setText(text);
boolean ok = rawSetClipboardText(text);
isSettingClipboard.set(false);
return ok;
}