Skip to content

Commit

Permalink
fix: Android 14 #78
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtr126 committed Feb 5, 2024
1 parent 777c3d8 commit 6c24cf2
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions app/src/main/java/xtr/keymapper/server/Input.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package xtr.keymapper.server;

import android.hardware.input.InputManager;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.SystemClock;
Expand All @@ -17,7 +17,7 @@
public class Input {

static Method injectInputEventMethod;
static InputManager im;
static Object inputManager;
static int inputSource = InputDevice.SOURCE_TOUCHSCREEN;

private final PointersState pointersState = new PointersState();
Expand Down Expand Up @@ -80,7 +80,7 @@ public void injectTouch(int action, int pointerId, float pressure, float x, floa
0, 0, 1f, 1f,
0, 0, inputSource, 0);
try {
injectInputEventMethod.invoke(im, motionEvent, 0);
injectInputEventMethod.invoke(inputManager, motionEvent, 0);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace(System.out);
}
Expand Down Expand Up @@ -108,7 +108,7 @@ private void injectScroll(ScrollEvent event, float value) {
0, 0, 1f, 1f, 0, 0,
InputDevice.SOURCE_MOUSE, 0);
try {
injectInputEventMethod.invoke(im, motionEvent, 0);
injectInputEventMethod.invoke(inputManager, motionEvent, 0);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace(System.out);
}
Expand Down Expand Up @@ -189,7 +189,14 @@ private void next() {
String methodName = "getInstance";
Object[] objArr = new Object[0];
try {
im = (InputManager) InputManager.class.getDeclaredMethod(methodName)
Class<?> inputManagerClass;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
inputManagerClass = Class.forName("android.hardware.input.InputManagerGlobal");
} else {
inputManagerClass = android.hardware.input.InputManager.class;
}

inputManager = inputManagerClass.getDeclaredMethod(methodName)
.invoke(null, objArr);
//Make MotionEvent.obtain() method accessible
methodName = "obtain";
Expand All @@ -199,7 +206,7 @@ private void next() {
//Get the reference to injectInputEvent method
methodName = "injectInputEvent";

injectInputEventMethod = InputManager.class.getMethod(methodName, android.view.InputEvent.class, Integer.TYPE);
injectInputEventMethod = inputManagerClass.getMethod(methodName, android.view.InputEvent.class, Integer.TYPE);

} catch (Exception e) {
e.printStackTrace(System.out);
Expand Down

0 comments on commit 6c24cf2

Please sign in to comment.