Export static method to power off screen in Device
PR <https://github.com/Genymobile/scrcpy/pull/824>
@@ -153,13 +153,17 @@ public final class Device {
return Build.MODEL;
}
+ public static boolean supportsInputEvents(int displayId) {
+ return displayId == 0 || Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q;
+ }
+
public boolean supportsInputEvents() {
return supportsInputEvents;
}
- public boolean injectEvent(InputEvent inputEvent, int mode) {
- if (!supportsInputEvents()) {
- throw new AssertionError("Could not inject input event if !supportsInputEvents()");
+ public static boolean injectEvent(InputEvent inputEvent, int mode, int displayId) {
+ if (!supportsInputEvents(displayId)) {
+ return false;
}
if (displayId != 0 && !InputManager.setDisplayId(inputEvent, displayId)) {
@@ -169,10 +173,29 @@ public final class Device {
return SERVICE_MANAGER.getInputManager().injectInputEvent(inputEvent, mode);
}
+ public boolean injectEvent(InputEvent inputEvent, int mode) {
+ if (!supportsInputEvents()) {
+ throw new AssertionError("Could not inject input event if !supportsInputEvents()");
+ }
+
+ return injectEvent(inputEvent, mode, displayId);
+ }
+
+ public static boolean injectEventOnDisplay(InputEvent event, int displayId) {
+ return injectEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC, displayId);
+ }
+
public boolean injectEvent(InputEvent event) {
return injectEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
}
+ public static boolean injectKeyEvent(int action, int keyCode, int repeat, int metaState, int displayId) {
+ long now = SystemClock.uptimeMillis();
+ KeyEvent event = new KeyEvent(now, now, action, keyCode, repeat, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
+ InputDevice.SOURCE_KEYBOARD);
+ return injectEventOnDisplay(event, displayId);
+ }
+
public boolean injectKeyEvent(int action, int keyCode, int repeat, int metaState) {
long now = SystemClock.uptimeMillis();
KeyEvent event = new KeyEvent(now, now, action, keyCode, repeat, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
@@ -180,6 +203,10 @@ public final class Device {
return injectEvent(event);
}
+ public static boolean injectKeycode(int keyCode, int displayId) {
+ return injectKeyEvent(KeyEvent.ACTION_DOWN, keyCode, 0, 0, displayId) && injectKeyEvent(KeyEvent.ACTION_UP, keyCode, 0, 0, displayId);
+ }
+
public boolean injectKeycode(int keyCode) {
return injectKeyEvent(KeyEvent.ACTION_DOWN, keyCode, 0, 0) && injectKeyEvent(KeyEvent.ACTION_UP, keyCode, 0, 0);
}
@@ -249,6 +276,13 @@ public final class Device {
return SurfaceControl.setDisplayPowerMode(d, mode);
}
+ public static boolean powerOffScreen(int displayId) {
+ if (!isScreenOn()) {
+ return true;
+ }
+ return injectKeycode(KeyEvent.KEYCODE_POWER, displayId);
+ }
+
/**
* Disable auto-rotation (if enabled), set the screen rotation and re-enable auto-rotation (if it was enabled).
*/
