From fb0bcaebc2d081f0b6992b49bfe2ce9b12298b51 Mon Sep 17 00:00:00 2001 From: Yu-Chen Lin Date: Sun, 21 Feb 2021 08:31:37 +0800 Subject: [PATCH] Export static method to power off screen in Device PR #824 Signed-off-by: Yu-Chen Lin Signed-off-by: Romain Vimont --- .../java/com/genymobile/scrcpy/Device.java | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/Device.java b/server/src/main/java/com/genymobile/scrcpy/Device.java index a8fdf677..624c9fa0 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Device.java +++ b/server/src/main/java/com/genymobile/scrcpy/Device.java @@ -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). */