Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
d613b10efc | ||
|
cc4e1e20de |
@ -149,6 +149,10 @@ public class Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int injectText(String text) {
|
private int injectText(String text) {
|
||||||
|
if (device.injectTextPaste(text)) {
|
||||||
|
// The best method (fastest and UTF-8) worked!
|
||||||
|
return text.length();
|
||||||
|
}
|
||||||
int successCount = 0;
|
int successCount = 0;
|
||||||
for (char c : text.toCharArray()) {
|
for (char c : text.toCharArray()) {
|
||||||
if (!injectChar(c)) {
|
if (!injectChar(c)) {
|
||||||
|
@ -167,15 +167,44 @@ public final class Device {
|
|||||||
return injectEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
|
return injectEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean injectKeyEvent(int action, int keyCode, int repeat, int metaState) {
|
public boolean injectKeyEvent(int action, int keyCode, int repeat, int metaState, int mode) {
|
||||||
long now = SystemClock.uptimeMillis();
|
long now = SystemClock.uptimeMillis();
|
||||||
KeyEvent event = new KeyEvent(now, now, action, keyCode, repeat, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
|
KeyEvent event = new KeyEvent(now, now, action, keyCode, repeat, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
|
||||||
InputDevice.SOURCE_KEYBOARD);
|
InputDevice.SOURCE_KEYBOARD);
|
||||||
return injectEvent(event);
|
return injectEvent(event, mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean injectKeyEvent(int action, int keyCode, int repeat, int metaState) {
|
||||||
|
return injectKeyEvent(action, keyCode, repeat, metaState, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean injectKeycode(int keyCode, int mode) {
|
||||||
|
return injectKeyEvent(KeyEvent.ACTION_DOWN, keyCode, 0, 0) && injectKeyEvent(KeyEvent.ACTION_UP, keyCode, 0, 0, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean injectKeycode(int keyCode) {
|
public boolean injectKeycode(int keyCode) {
|
||||||
return injectKeyEvent(KeyEvent.ACTION_DOWN, keyCode, 0, 0) && injectKeyEvent(KeyEvent.ACTION_UP, keyCode, 0, 0);
|
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() {
|
public boolean isScreenOn() {
|
||||||
@ -206,9 +235,13 @@ public final class Device {
|
|||||||
return s.toString();
|
return s.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean rawSetClipboardText(String text) {
|
||||||
|
return serviceManager.getClipboardManager().setText(text);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean setClipboardText(String text) {
|
public boolean setClipboardText(String text) {
|
||||||
isSettingClipboard.set(true);
|
isSettingClipboard.set(true);
|
||||||
boolean ok = serviceManager.getClipboardManager().setText(text);
|
boolean ok = rawSetClipboardText(text);
|
||||||
isSettingClipboard.set(false);
|
isSettingClipboard.set(false);
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user