|
|
|
@ -167,15 +167,44 @@ public final class Device {
|
|
|
|
|
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();
|
|
|
|
|
KeyEvent event = new KeyEvent(now, now, action, keyCode, repeat, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
|
|
|
|
|
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) {
|
|
|
|
|
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() {
|
|
|
|
@ -206,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;
|
|
|
|
|
}
|
|
|
|
|