Simplify InputManager wrapper

Use the public InputManager API.

PR #6009 <https://github.com/Genymobile/scrcpy/pull/6009>

Signed-off-by: Romain Vimont <rom@rom1v.com>
This commit is contained in:
Simon Chan 2025-04-25 12:31:22 +08:00 committed by Romain Vimont
commit 41ed40f5f9

View file

@ -1,5 +1,6 @@
package com.genymobile.scrcpy.wrappers; package com.genymobile.scrcpy.wrappers;
import com.genymobile.scrcpy.FakeContext;
import com.genymobile.scrcpy.util.Ln; import com.genymobile.scrcpy.util.Ln;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
@ -16,40 +17,26 @@ public final class InputManager {
public static final int INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT = 1; public static final int INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT = 1;
public static final int INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH = 2; public static final int INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH = 2;
private final Object manager; private final android.hardware.input.InputManager manager;
private Method injectInputEventMethod;
private long lastPermissionLogDate; private long lastPermissionLogDate;
private static Method injectInputEventMethod;
private static Method setDisplayIdMethod; private static Method setDisplayIdMethod;
private static Method setActionButtonMethod; private static Method setActionButtonMethod;
static InputManager create() { static InputManager create() {
try { android.hardware.input.InputManager manager = (android.hardware.input.InputManager) FakeContext.get()
Class<?> inputManagerClass = getInputManagerClass(); .getSystemService(FakeContext.INPUT_SERVICE);
Method getInstanceMethod = inputManagerClass.getDeclaredMethod("getInstance"); return new InputManager(manager);
Object im = getInstanceMethod.invoke(null);
return new InputManager(im);
} catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
} }
private static Class<?> getInputManagerClass() { private InputManager(android.hardware.input.InputManager manager) {
try {
// Parts of the InputManager class have been moved to a new InputManagerGlobal class in Android 14 preview
return Class.forName("android.hardware.input.InputManagerGlobal");
} catch (ClassNotFoundException e) {
return android.hardware.input.InputManager.class;
}
}
private InputManager(Object manager) {
this.manager = manager; this.manager = manager;
} }
private Method getInjectInputEventMethod() throws NoSuchMethodException { private static Method getInjectInputEventMethod() throws NoSuchMethodException {
if (injectInputEventMethod == null) { if (injectInputEventMethod == null) {
injectInputEventMethod = manager.getClass().getMethod("injectInputEvent", InputEvent.class, int.class); injectInputEventMethod = android.hardware.input.InputManager.class.getMethod("injectInputEvent", InputEvent.class, int.class);
} }
return injectInputEventMethod; return injectInputEventMethod;
} }