Skip to content

Commit

Permalink
Fix NoSuchMethodException for injectInputEvent()
Browse files Browse the repository at this point in the history
Some devices with modified ROMs expose a different signature for
injectInputEvent().

Fixes #2250 <#2250>
PR #2946 <#2946>

Signed-off-by: Romain Vimont <[email protected]>
  • Loading branch information
thomasrebele authored and rom1v committed Jan 16, 2022
1 parent 60bf133 commit 46b174a
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public final class InputManager {

private final IInterface manager;
private Method injectInputEventMethod;
boolean alternativeInjectInputEventMethod;

private static Method setDisplayIdMethod;

Expand All @@ -25,14 +26,23 @@ public InputManager(IInterface manager) {

private Method getInjectInputEventMethod() throws NoSuchMethodException {
if (injectInputEventMethod == null) {
injectInputEventMethod = manager.getClass().getMethod("injectInputEvent", InputEvent.class, int.class);
try {
injectInputEventMethod = manager.getClass().getMethod("injectInputEvent", InputEvent.class, int.class);
} catch (NoSuchMethodException e) {
injectInputEventMethod = manager.getClass().getMethod("injectInputEvent", InputEvent.class, int.class, int.class);
alternativeInjectInputEventMethod = true;
}
}
return injectInputEventMethod;
}

public boolean injectInputEvent(InputEvent inputEvent, int mode) {
try {
Method method = getInjectInputEventMethod();
if (alternativeInjectInputEventMethod) {
// See <https://github.com/Genymobile/scrcpy/issues/2250>
return (boolean) method.invoke(manager, inputEvent, mode, 0);
}
return (boolean) method.invoke(manager, inputEvent, mode);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
Ln.e("Could not invoke method", e);
Expand Down

0 comments on commit 46b174a

Please sign in to comment.