Pass virtual input event timestamps to JNI layer

Test: atest VirtualDeviceManagerServiceTest
Bug: 271946580
Change-Id: I2839a044db928da9e41477a16bf0231906f68377
This commit is contained in:
Biswarup Pal
2023-04-05 22:50:05 +00:00
parent b4a8a82fa9
commit 83bcfb4ef1
3 changed files with 84 additions and 52 deletions

View File

@@ -367,7 +367,7 @@ class InputController {
"Could not send key event to input device for given token");
}
return mNativeWrapper.writeDpadKeyEvent(inputDeviceDescriptor.getNativePointer(),
event.getKeyCode(), event.getAction());
event.getKeyCode(), event.getAction(), event.getEventTimeNanos());
}
}
@@ -380,7 +380,7 @@ class InputController {
"Could not send key event to input device for given token");
}
return mNativeWrapper.writeKeyEvent(inputDeviceDescriptor.getNativePointer(),
event.getKeyCode(), event.getAction());
event.getKeyCode(), event.getAction(), event.getEventTimeNanos());
}
}
@@ -398,7 +398,7 @@ class InputController {
"Display id associated with this mouse is not currently targetable");
}
return mNativeWrapper.writeButtonEvent(inputDeviceDescriptor.getNativePointer(),
event.getButtonCode(), event.getAction());
event.getButtonCode(), event.getAction(), event.getEventTimeNanos());
}
}
@@ -412,7 +412,8 @@ class InputController {
}
return mNativeWrapper.writeTouchEvent(inputDeviceDescriptor.getNativePointer(),
event.getPointerId(), event.getToolType(), event.getAction(), event.getX(),
event.getY(), event.getPressure(), event.getMajorAxisSize());
event.getY(), event.getPressure(), event.getMajorAxisSize(),
event.getEventTimeNanos());
}
}
@@ -430,7 +431,7 @@ class InputController {
"Display id associated with this mouse is not currently targetable");
}
return mNativeWrapper.writeRelativeEvent(inputDeviceDescriptor.getNativePointer(),
event.getRelativeX(), event.getRelativeY());
event.getRelativeX(), event.getRelativeY(), event.getEventTimeNanos());
}
}
@@ -448,7 +449,7 @@ class InputController {
"Display id associated with this mouse is not currently targetable");
}
return mNativeWrapper.writeScrollEvent(inputDeviceDescriptor.getNativePointer(),
event.getXAxisMovement(), event.getYAxisMovement());
event.getXAxisMovement(), event.getYAxisMovement(), event.getEventTimeNanos());
}
}
@@ -514,15 +515,19 @@ class InputController {
private static native long nativeOpenUinputTouchscreen(String deviceName, int vendorId,
int productId, String phys, int height, int width);
private static native void nativeCloseUinput(long ptr);
private static native boolean nativeWriteDpadKeyEvent(long ptr, int androidKeyCode, int action);
private static native boolean nativeWriteKeyEvent(long ptr, int androidKeyCode, int action);
private static native boolean nativeWriteButtonEvent(long ptr, int buttonCode, int action);
private static native boolean nativeWriteDpadKeyEvent(long ptr, int androidKeyCode, int action,
long eventTimeNanos);
private static native boolean nativeWriteKeyEvent(long ptr, int androidKeyCode, int action,
long eventTimeNanos);
private static native boolean nativeWriteButtonEvent(long ptr, int buttonCode, int action,
long eventTimeNanos);
private static native boolean nativeWriteTouchEvent(long ptr, int pointerId, int toolType,
int action, float locationX, float locationY, float pressure, float majorAxisSize);
int action, float locationX, float locationY, float pressure, float majorAxisSize,
long eventTimeNanos);
private static native boolean nativeWriteRelativeEvent(long ptr, float relativeX,
float relativeY);
float relativeY, long eventTimeNanos);
private static native boolean nativeWriteScrollEvent(long ptr, float xAxisMovement,
float yAxisMovement);
float yAxisMovement, long eventTimeNanos);
/** Wrapper around the static native methods for tests. */
@VisibleForTesting
@@ -550,32 +555,37 @@ class InputController {
nativeCloseUinput(ptr);
}
public boolean writeDpadKeyEvent(long ptr, int androidKeyCode, int action) {
return nativeWriteDpadKeyEvent(ptr, androidKeyCode, action);
public boolean writeDpadKeyEvent(long ptr, int androidKeyCode, int action,
long eventTimeNanos) {
return nativeWriteDpadKeyEvent(ptr, androidKeyCode, action, eventTimeNanos);
}
public boolean writeKeyEvent(long ptr, int androidKeyCode, int action) {
return nativeWriteKeyEvent(ptr, androidKeyCode, action);
public boolean writeKeyEvent(long ptr, int androidKeyCode, int action,
long eventTimeNanos) {
return nativeWriteKeyEvent(ptr, androidKeyCode, action, eventTimeNanos);
}
public boolean writeButtonEvent(long ptr, int buttonCode, int action) {
return nativeWriteButtonEvent(ptr, buttonCode, action);
public boolean writeButtonEvent(long ptr, int buttonCode, int action,
long eventTimeNanos) {
return nativeWriteButtonEvent(ptr, buttonCode, action, eventTimeNanos);
}
public boolean writeTouchEvent(long ptr, int pointerId, int toolType, int action,
float locationX, float locationY, float pressure, float majorAxisSize) {
float locationX, float locationY, float pressure, float majorAxisSize,
long eventTimeNanos) {
return nativeWriteTouchEvent(ptr, pointerId, toolType,
action, locationX, locationY,
pressure, majorAxisSize);
pressure, majorAxisSize, eventTimeNanos);
}
public boolean writeRelativeEvent(long ptr, float relativeX, float relativeY) {
return nativeWriteRelativeEvent(ptr, relativeX, relativeY);
public boolean writeRelativeEvent(long ptr, float relativeX, float relativeY,
long eventTimeNanos) {
return nativeWriteRelativeEvent(ptr, relativeX, relativeY, eventTimeNanos);
}
public boolean writeScrollEvent(long ptr, float xAxisMovement, float yAxisMovement) {
return nativeWriteScrollEvent(ptr, xAxisMovement,
yAxisMovement);
public boolean writeScrollEvent(long ptr, float xAxisMovement, float yAxisMovement,
long eventTimeNanos) {
return nativeWriteScrollEvent(ptr, xAxisMovement, yAxisMovement, eventTimeNanos);
}
}

View File

@@ -233,44 +233,50 @@ static void nativeCloseUinput(JNIEnv* env, jobject thiz, jlong ptr) {
// Native methods for VirtualDpad
static bool nativeWriteDpadKeyEvent(JNIEnv* env, jobject thiz, jlong ptr, jint androidKeyCode,
jint action) {
jint action, jlong eventTimeNanos) {
VirtualDpad* virtualDpad = reinterpret_cast<VirtualDpad*>(ptr);
return virtualDpad->writeDpadKeyEvent(androidKeyCode, action);
return virtualDpad->writeDpadKeyEvent(androidKeyCode, action,
std::chrono::nanoseconds(eventTimeNanos));
}
// Native methods for VirtualKeyboard
static bool nativeWriteKeyEvent(JNIEnv* env, jobject thiz, jlong ptr, jint androidKeyCode,
jint action) {
jint action, jlong eventTimeNanos) {
VirtualKeyboard* virtualKeyboard = reinterpret_cast<VirtualKeyboard*>(ptr);
return virtualKeyboard->writeKeyEvent(androidKeyCode, action);
return virtualKeyboard->writeKeyEvent(androidKeyCode, action,
std::chrono::nanoseconds(eventTimeNanos));
}
// Native methods for VirtualTouchscreen
static bool nativeWriteTouchEvent(JNIEnv* env, jobject thiz, jlong ptr, jint pointerId,
jint toolType, jint action, jfloat locationX, jfloat locationY,
jfloat pressure, jfloat majorAxisSize) {
jfloat pressure, jfloat majorAxisSize, jlong eventTimeNanos) {
VirtualTouchscreen* virtualTouchscreen = reinterpret_cast<VirtualTouchscreen*>(ptr);
return virtualTouchscreen->writeTouchEvent(pointerId, toolType, action, locationX, locationY,
pressure, majorAxisSize);
pressure, majorAxisSize,
std::chrono::nanoseconds(eventTimeNanos));
}
// Native methods for VirtualMouse
static bool nativeWriteButtonEvent(JNIEnv* env, jobject thiz, jlong ptr, jint buttonCode,
jint action) {
jint action, jlong eventTimeNanos) {
VirtualMouse* virtualMouse = reinterpret_cast<VirtualMouse*>(ptr);
return virtualMouse->writeButtonEvent(buttonCode, action);
return virtualMouse->writeButtonEvent(buttonCode, action,
std::chrono::nanoseconds(eventTimeNanos));
}
static bool nativeWriteRelativeEvent(JNIEnv* env, jobject thiz, jlong ptr, jfloat relativeX,
jfloat relativeY) {
jfloat relativeY, jlong eventTimeNanos) {
VirtualMouse* virtualMouse = reinterpret_cast<VirtualMouse*>(ptr);
return virtualMouse->writeRelativeEvent(relativeX, relativeY);
return virtualMouse->writeRelativeEvent(relativeX, relativeY,
std::chrono::nanoseconds(eventTimeNanos));
}
static bool nativeWriteScrollEvent(JNIEnv* env, jobject thiz, jlong ptr, jfloat xAxisMovement,
jfloat yAxisMovement) {
jfloat yAxisMovement, jlong eventTimeNanos) {
VirtualMouse* virtualMouse = reinterpret_cast<VirtualMouse*>(ptr);
return virtualMouse->writeScrollEvent(xAxisMovement, yAxisMovement);
return virtualMouse->writeScrollEvent(xAxisMovement, yAxisMovement,
std::chrono::nanoseconds(eventTimeNanos));
}
static JNINativeMethod methods[] = {
@@ -283,12 +289,12 @@ static JNINativeMethod methods[] = {
{"nativeOpenUinputTouchscreen", "(Ljava/lang/String;IILjava/lang/String;II)J",
(void*)nativeOpenUinputTouchscreen},
{"nativeCloseUinput", "(J)V", (void*)nativeCloseUinput},
{"nativeWriteDpadKeyEvent", "(JII)Z", (void*)nativeWriteDpadKeyEvent},
{"nativeWriteKeyEvent", "(JII)Z", (void*)nativeWriteKeyEvent},
{"nativeWriteButtonEvent", "(JII)Z", (void*)nativeWriteButtonEvent},
{"nativeWriteTouchEvent", "(JIIIFFFF)Z", (void*)nativeWriteTouchEvent},
{"nativeWriteRelativeEvent", "(JFF)Z", (void*)nativeWriteRelativeEvent},
{"nativeWriteScrollEvent", "(JFF)Z", (void*)nativeWriteScrollEvent},
{"nativeWriteDpadKeyEvent", "(JIIJ)Z", (void*)nativeWriteDpadKeyEvent},
{"nativeWriteKeyEvent", "(JIIJ)Z", (void*)nativeWriteKeyEvent},
{"nativeWriteButtonEvent", "(JIIJ)Z", (void*)nativeWriteButtonEvent},
{"nativeWriteTouchEvent", "(JIIIFFFFJ)Z", (void*)nativeWriteTouchEvent},
{"nativeWriteRelativeEvent", "(JFFJ)Z", (void*)nativeWriteRelativeEvent},
{"nativeWriteScrollEvent", "(JFFJ)Z", (void*)nativeWriteScrollEvent},
};
int register_android_server_companion_virtual_InputController(JNIEnv* env) {

View File

@@ -1160,6 +1160,7 @@ public class VirtualDeviceManagerServiceTest {
final int fd = 1;
final int keyCode = KeyEvent.KEYCODE_A;
final int action = VirtualKeyEvent.ACTION_UP;
final long eventTimeNanos = 5000L;
mInputController.addDeviceForTesting(BINDER, fd,
InputController.InputDeviceDescriptor.TYPE_KEYBOARD, DISPLAY_ID_1, PHYS,
DEVICE_NAME_1, INPUT_DEVICE_ID);
@@ -1167,8 +1168,9 @@ public class VirtualDeviceManagerServiceTest {
mDeviceImpl.sendKeyEvent(BINDER, new VirtualKeyEvent.Builder()
.setKeyCode(keyCode)
.setAction(action)
.setEventTimeNanos(eventTimeNanos)
.build());
verify(mNativeWrapperMock).writeKeyEvent(fd, keyCode, action);
verify(mNativeWrapperMock).writeKeyEvent(fd, keyCode, action, eventTimeNanos);
}
@Test
@@ -1188,14 +1190,17 @@ public class VirtualDeviceManagerServiceTest {
final int fd = 1;
final int buttonCode = VirtualMouseButtonEvent.BUTTON_BACK;
final int action = VirtualMouseButtonEvent.ACTION_BUTTON_PRESS;
final long eventTimeNanos = 5000L;
mInputController.addDeviceForTesting(BINDER, fd,
InputController.InputDeviceDescriptor.TYPE_MOUSE, DISPLAY_ID_1, PHYS,
DEVICE_NAME_1, INPUT_DEVICE_ID);
doReturn(DISPLAY_ID_1).when(mInputManagerInternalMock).getVirtualMousePointerDisplayId();
mDeviceImpl.sendButtonEvent(BINDER, new VirtualMouseButtonEvent.Builder()
.setButtonCode(buttonCode)
.setAction(action).build());
verify(mNativeWrapperMock).writeButtonEvent(fd, buttonCode, action);
.setAction(action)
.setEventTimeNanos(eventTimeNanos)
.build());
verify(mNativeWrapperMock).writeButtonEvent(fd, buttonCode, action, eventTimeNanos);
}
@Test
@@ -1229,13 +1234,17 @@ public class VirtualDeviceManagerServiceTest {
final int fd = 1;
final float x = -0.2f;
final float y = 0.7f;
final long eventTimeNanos = 5000L;
mInputController.addDeviceForTesting(BINDER, fd,
InputController.InputDeviceDescriptor.TYPE_MOUSE, DISPLAY_ID_1, PHYS, DEVICE_NAME_1,
INPUT_DEVICE_ID);
doReturn(DISPLAY_ID_1).when(mInputManagerInternalMock).getVirtualMousePointerDisplayId();
mDeviceImpl.sendRelativeEvent(BINDER, new VirtualMouseRelativeEvent.Builder()
.setRelativeX(x).setRelativeY(y).build());
verify(mNativeWrapperMock).writeRelativeEvent(fd, x, y);
.setRelativeX(x)
.setRelativeY(y)
.setEventTimeNanos(eventTimeNanos)
.build());
verify(mNativeWrapperMock).writeRelativeEvent(fd, x, y, eventTimeNanos);
}
@Test
@@ -1270,14 +1279,17 @@ public class VirtualDeviceManagerServiceTest {
final int fd = 1;
final float x = 0.5f;
final float y = 1f;
final long eventTimeNanos = 5000L;
mInputController.addDeviceForTesting(BINDER, fd,
InputController.InputDeviceDescriptor.TYPE_MOUSE, DISPLAY_ID_1, PHYS, DEVICE_NAME_1,
INPUT_DEVICE_ID);
doReturn(DISPLAY_ID_1).when(mInputManagerInternalMock).getVirtualMousePointerDisplayId();
mDeviceImpl.sendScrollEvent(BINDER, new VirtualMouseScrollEvent.Builder()
.setXAxisMovement(x)
.setYAxisMovement(y).build());
verify(mNativeWrapperMock).writeScrollEvent(fd, x, y);
.setYAxisMovement(y)
.setEventTimeNanos(eventTimeNanos)
.build());
verify(mNativeWrapperMock).writeScrollEvent(fd, x, y, eventTimeNanos);
}
@Test
@@ -1318,6 +1330,7 @@ public class VirtualDeviceManagerServiceTest {
final float x = 100.5f;
final float y = 200.5f;
final int action = VirtualTouchEvent.ACTION_UP;
final long eventTimeNanos = 5000L;
mInputController.addDeviceForTesting(BINDER, fd,
InputController.InputDeviceDescriptor.TYPE_TOUCHSCREEN, DISPLAY_ID_1, PHYS,
DEVICE_NAME_1, INPUT_DEVICE_ID);
@@ -1327,9 +1340,10 @@ public class VirtualDeviceManagerServiceTest {
.setAction(action)
.setPointerId(pointerId)
.setToolType(toolType)
.setEventTimeNanos(eventTimeNanos)
.build());
verify(mNativeWrapperMock).writeTouchEvent(fd, pointerId, toolType, action, x, y, Float.NaN,
Float.NaN);
Float.NaN, eventTimeNanos);
}
@Test
@@ -1342,6 +1356,7 @@ public class VirtualDeviceManagerServiceTest {
final int action = VirtualTouchEvent.ACTION_UP;
final float pressure = 1.0f;
final float majorAxisSize = 10.0f;
final long eventTimeNanos = 5000L;
mInputController.addDeviceForTesting(BINDER, fd,
InputController.InputDeviceDescriptor.TYPE_TOUCHSCREEN, DISPLAY_ID_1, PHYS,
DEVICE_NAME_1, INPUT_DEVICE_ID);
@@ -1353,9 +1368,10 @@ public class VirtualDeviceManagerServiceTest {
.setToolType(toolType)
.setPressure(pressure)
.setMajorAxisSize(majorAxisSize)
.setEventTimeNanos(eventTimeNanos)
.build());
verify(mNativeWrapperMock).writeTouchEvent(fd, pointerId, toolType, action, x, y, pressure,
majorAxisSize);
majorAxisSize, eventTimeNanos);
}
@Test