diff --git a/core/java/android/view/KeyEvent.java b/core/java/android/view/KeyEvent.java index c638717b13c05..c91096e5aa259 100644 --- a/core/java/android/view/KeyEvent.java +++ b/core/java/android/view/KeyEvent.java @@ -19,6 +19,7 @@ package android.view; import static android.view.Display.INVALID_DISPLAY; import android.annotation.NonNull; +import android.annotation.Nullable; import android.annotation.TestApi; import android.compat.annotation.UnsupportedAppUsage; import android.os.Build; @@ -1269,6 +1270,7 @@ public class KeyEvent extends InputEvent implements Parcelable { @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) private int mSource; private int mDisplayId; + private @Nullable byte[] mHmac; @UnsupportedAppUsage private int mMetaState; @UnsupportedAppUsage @@ -1546,6 +1548,7 @@ public class KeyEvent extends InputEvent implements Parcelable { mDeviceId = origEvent.mDeviceId; mSource = origEvent.mSource; mDisplayId = origEvent.mDisplayId; + mHmac = origEvent.mHmac == null ? null : origEvent.mHmac.clone(); mScanCode = origEvent.mScanCode; mFlags = origEvent.mFlags; mCharacters = origEvent.mCharacters; @@ -1573,6 +1576,7 @@ public class KeyEvent extends InputEvent implements Parcelable { mDeviceId = origEvent.mDeviceId; mSource = origEvent.mSource; mDisplayId = origEvent.mDisplayId; + mHmac = null; // Don't copy HMAC, it will be invalid because eventTime is changing mScanCode = origEvent.mScanCode; mFlags = origEvent.mFlags; mCharacters = origEvent.mCharacters; @@ -1600,7 +1604,8 @@ public class KeyEvent extends InputEvent implements Parcelable { */ public static KeyEvent obtain(long downTime, long eventTime, int action, int code, int repeat, int metaState, - int deviceId, int scancode, int flags, int source, int displayId, String characters) { + int deviceId, int scancode, int flags, int source, int displayId, @Nullable byte[] hmac, + String characters) { KeyEvent ev = obtain(); ev.mDownTime = downTime; ev.mEventTime = eventTime; @@ -1613,6 +1618,7 @@ public class KeyEvent extends InputEvent implements Parcelable { ev.mFlags = flags; ev.mSource = source; ev.mDisplayId = displayId; + ev.mHmac = hmac; ev.mCharacters = characters; return ev; } @@ -1627,7 +1633,7 @@ public class KeyEvent extends InputEvent implements Parcelable { int code, int repeat, int metaState, int deviceId, int scancode, int flags, int source, String characters) { return obtain(downTime, eventTime, action, code, repeat, metaState, deviceId, scancode, - flags, source, INVALID_DISPLAY, characters); + flags, source, INVALID_DISPLAY, null /* hmac */, characters); } /** @@ -1650,6 +1656,7 @@ public class KeyEvent extends InputEvent implements Parcelable { ev.mFlags = other.mFlags; ev.mSource = other.mSource; ev.mDisplayId = other.mDisplayId; + ev.mHmac = other.mHmac == null ? null : other.mHmac.clone(); ev.mCharacters = other.mCharacters; return ev; } @@ -1738,6 +1745,7 @@ public class KeyEvent extends InputEvent implements Parcelable { mDeviceId = origEvent.mDeviceId; mSource = origEvent.mSource; mDisplayId = origEvent.mDisplayId; + mHmac = null; // Don't copy the hmac, it will be invalid since action is changing mScanCode = origEvent.mScanCode; mFlags = origEvent.mFlags; // Don't copy mCharacters, since one way or the other we'll lose it @@ -3091,6 +3099,7 @@ public class KeyEvent extends InputEvent implements Parcelable { mDeviceId = in.readInt(); mSource = in.readInt(); mDisplayId = in.readInt(); + mHmac = in.createByteArray(); mAction = in.readInt(); mKeyCode = in.readInt(); mRepeatCount = in.readInt(); @@ -3109,6 +3118,7 @@ public class KeyEvent extends InputEvent implements Parcelable { out.writeInt(mDeviceId); out.writeInt(mSource); out.writeInt(mDisplayId); + out.writeByteArray(mHmac); out.writeInt(mAction); out.writeInt(mKeyCode); out.writeInt(mRepeatCount); diff --git a/core/jni/android_view_InputEventSender.cpp b/core/jni/android_view_InputEventSender.cpp index f90d1cf27d7c3..84acf9ac6989d 100644 --- a/core/jni/android_view_InputEventSender.cpp +++ b/core/jni/android_view_InputEventSender.cpp @@ -113,10 +113,13 @@ status_t NativeInputEventSender::sendKeyEvent(uint32_t seq, const KeyEvent* even } uint32_t publishedSeq = mNextPublishedSeq++; - status_t status = mInputPublisher.publishKeyEvent(publishedSeq, - event->getDeviceId(), event->getSource(), event->getDisplayId(), event->getAction(), - event->getFlags(), event->getKeyCode(), event->getScanCode(), event->getMetaState(), - event->getRepeatCount(), event->getDownTime(), event->getEventTime()); + status_t status = + mInputPublisher.publishKeyEvent(publishedSeq, event->getDeviceId(), event->getSource(), + event->getDisplayId(), event->getHmac(), + event->getAction(), event->getFlags(), + event->getKeyCode(), event->getScanCode(), + event->getMetaState(), event->getRepeatCount(), + event->getDownTime(), event->getEventTime()); if (status) { ALOGW("Failed to send key event on channel '%s'. status=%d", getInputChannelName().c_str(), status); @@ -134,17 +137,24 @@ status_t NativeInputEventSender::sendMotionEvent(uint32_t seq, const MotionEvent uint32_t publishedSeq; for (size_t i = 0; i <= event->getHistorySize(); i++) { publishedSeq = mNextPublishedSeq++; - status_t status = mInputPublisher.publishMotionEvent(publishedSeq, - event->getDeviceId(), event->getSource(), event->getDisplayId(), - event->getAction(), event->getActionButton(), event->getFlags(), - event->getEdgeFlags(), event->getMetaState(), event->getButtonState(), - event->getClassification(), - event->getXOffset(), event->getYOffset(), - event->getXPrecision(), event->getYPrecision(), - event->getRawXCursorPosition(), event->getRawYCursorPosition(), - event->getDownTime(), event->getHistoricalEventTime(i), - event->getPointerCount(), event->getPointerProperties(), - event->getHistoricalRawPointerCoords(0, i)); + status_t status = + mInputPublisher.publishMotionEvent(publishedSeq, event->getDeviceId(), + event->getSource(), event->getDisplayId(), + event->getHmac(), event->getAction(), + event->getActionButton(), event->getFlags(), + event->getEdgeFlags(), event->getMetaState(), + event->getButtonState(), + event->getClassification(), event->getXScale(), + event->getYScale(), event->getXOffset(), + event->getYOffset(), event->getXPrecision(), + event->getYPrecision(), + event->getRawXCursorPosition(), + event->getRawYCursorPosition(), + event->getDownTime(), + event->getHistoricalEventTime(i), + event->getPointerCount(), + event->getPointerProperties(), + event->getHistoricalRawPointerCoords(0, i)); if (status) { ALOGW("Failed to send motion event sample on channel '%s'. status=%d", getInputChannelName().c_str(), status); diff --git a/core/jni/android_view_KeyEvent.cpp b/core/jni/android_view_KeyEvent.cpp index f0107723a43e6..57979bd9b546f 100644 --- a/core/jni/android_view_KeyEvent.cpp +++ b/core/jni/android_view_KeyEvent.cpp @@ -20,15 +20,53 @@ #include #include -#include #include +#include #include +#include +#include #include "android_view_KeyEvent.h" #include "core_jni_helpers.h" namespace android { +/** + * Convert an std::array of bytes into a Java object. + */ +template +static ScopedLocalRef toJbyteArray(JNIEnv* env, const std::array& data) { + ScopedLocalRef array(env, env->NewByteArray(N)); + if (array.get() == nullptr) { + jniThrowException(env, "java/lang/OutOfMemoryError", nullptr); + return array; + } + static_assert(sizeof(char) == sizeof(uint8_t)); + env->SetByteArrayRegion(array.get(), 0, N, reinterpret_cast(data.data())); + return array; +} + +/** + * Convert a Java object into an std::array of bytes of size N. + * If the object is null, or the length is unexpected, return std::nullopt. + */ +template +static std::optional> fromJobject(JNIEnv* env, jobject object) { + if (object == nullptr) { + return std::nullopt; + } + jbyteArray javaArray = reinterpret_cast(object); + ScopedByteArrayRO bytes(env, javaArray); + if (bytes.size() != N) { + ALOGE("Could not initialize array from java object, expected length %zu but got %zu", N, + bytes.size()); + return std::nullopt; + } + std::array array; + std::move(bytes.get(), bytes.get() + N, array.begin()); + return array; +} + // ---------------------------------------------------------------------------- static struct { @@ -40,6 +78,7 @@ static struct { jfieldID mDeviceId; jfieldID mSource; jfieldID mDisplayId; + jfieldID mHmac; jfieldID mMetaState; jfieldID mAction; jfieldID mKeyCode; @@ -54,20 +93,16 @@ static struct { // ---------------------------------------------------------------------------- jobject android_view_KeyEvent_fromNative(JNIEnv* env, const KeyEvent* event) { - jobject eventObj = env->CallStaticObjectMethod(gKeyEventClassInfo.clazz, - gKeyEventClassInfo.obtain, - nanoseconds_to_milliseconds(event->getDownTime()), - nanoseconds_to_milliseconds(event->getEventTime()), - event->getAction(), - event->getKeyCode(), - event->getRepeatCount(), - event->getMetaState(), - event->getDeviceId(), - event->getScanCode(), - event->getFlags(), - event->getSource(), - event->getDisplayId(), - NULL); + ScopedLocalRef hmac = toJbyteArray(env, event->getHmac()); + jobject eventObj = + env->CallStaticObjectMethod(gKeyEventClassInfo.clazz, gKeyEventClassInfo.obtain, + nanoseconds_to_milliseconds(event->getDownTime()), + nanoseconds_to_milliseconds(event->getEventTime()), + event->getAction(), event->getKeyCode(), + event->getRepeatCount(), event->getMetaState(), + event->getDeviceId(), event->getScanCode(), + event->getFlags(), event->getSource(), + event->getDisplayId(), hmac.get(), NULL); if (env->ExceptionCheck()) { ALOGE("An exception occurred while obtaining a key event."); LOGE_EX(env); @@ -82,6 +117,11 @@ status_t android_view_KeyEvent_toNative(JNIEnv* env, jobject eventObj, jint deviceId = env->GetIntField(eventObj, gKeyEventClassInfo.mDeviceId); jint source = env->GetIntField(eventObj, gKeyEventClassInfo.mSource); jint displayId = env->GetIntField(eventObj, gKeyEventClassInfo.mDisplayId); + jobject hmacObj = env->GetObjectField(eventObj, gKeyEventClassInfo.mHmac); + std::optional> hmac = fromJobject<32>(env, hmacObj); + if (!hmac) { + hmac = INVALID_HMAC; + } jint metaState = env->GetIntField(eventObj, gKeyEventClassInfo.mMetaState); jint action = env->GetIntField(eventObj, gKeyEventClassInfo.mAction); jint keyCode = env->GetIntField(eventObj, gKeyEventClassInfo.mKeyCode); @@ -91,10 +131,9 @@ status_t android_view_KeyEvent_toNative(JNIEnv* env, jobject eventObj, jlong downTime = env->GetLongField(eventObj, gKeyEventClassInfo.mDownTime); jlong eventTime = env->GetLongField(eventObj, gKeyEventClassInfo.mEventTime); - event->initialize(deviceId, source, displayId, action, flags, keyCode, scanCode, metaState, - repeatCount, - milliseconds_to_nanoseconds(downTime), - milliseconds_to_nanoseconds(eventTime)); + event->initialize(deviceId, source, displayId, *hmac, action, flags, keyCode, scanCode, + metaState, repeatCount, milliseconds_to_nanoseconds(downTime), + milliseconds_to_nanoseconds(eventTime)); return OK; } @@ -134,8 +173,9 @@ int register_android_view_KeyEvent(JNIEnv* env) { jclass clazz = FindClassOrDie(env, "android/view/KeyEvent"); gKeyEventClassInfo.clazz = MakeGlobalRefOrDie(env, clazz); - gKeyEventClassInfo.obtain = GetStaticMethodIDOrDie(env, gKeyEventClassInfo.clazz, - "obtain", "(JJIIIIIIIIILjava/lang/String;)Landroid/view/KeyEvent;"); + gKeyEventClassInfo.obtain = + GetStaticMethodIDOrDie(env, gKeyEventClassInfo.clazz, "obtain", + "(JJIIIIIIIII[BLjava/lang/String;)Landroid/view/KeyEvent;"); gKeyEventClassInfo.recycle = GetMethodIDOrDie(env, gKeyEventClassInfo.clazz, "recycle", "()V"); @@ -143,6 +183,7 @@ int register_android_view_KeyEvent(JNIEnv* env) { gKeyEventClassInfo.mSource = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mSource", "I"); gKeyEventClassInfo.mDisplayId = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mDisplayId", "I"); + gKeyEventClassInfo.mHmac = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mHmac", "[B"); gKeyEventClassInfo.mMetaState = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mMetaState", "I"); gKeyEventClassInfo.mAction = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mAction", "I"); diff --git a/core/jni/android_view_KeyEvent.h b/core/jni/android_view_KeyEvent.h index 586eb2f2a139a..dab6bb75c91ec 100644 --- a/core/jni/android_view_KeyEvent.h +++ b/core/jni/android_view_KeyEvent.h @@ -42,4 +42,4 @@ extern status_t android_view_KeyEvent_recycle(JNIEnv* env, jobject eventObj); } // namespace android -#endif // _ANDROID_OS_KEYEVENT_H +#endif // _ANDROID_VIEW_KEYEVENT_H diff --git a/core/jni/android_view_MotionEvent.cpp b/core/jni/android_view_MotionEvent.cpp index feb9fe39e0a85..3335fb23cfb7b 100644 --- a/core/jni/android_view_MotionEvent.cpp +++ b/core/jni/android_view_MotionEvent.cpp @@ -330,14 +330,12 @@ static void pointerPropertiesFromNative(JNIEnv* env, const PointerProperties* po // ---------------------------------------------------------------------------- -static jlong android_view_MotionEvent_nativeInitialize(JNIEnv* env, jclass clazz, - jlong nativePtr, - jint deviceId, jint source, jint displayId, jint action, jint flags, jint edgeFlags, - jint metaState, jint buttonState, jint classification, - jfloat xOffset, jfloat yOffset, jfloat xPrecision, jfloat yPrecision, - jlong downTimeNanos, jlong eventTimeNanos, - jint pointerCount, jobjectArray pointerPropertiesObjArray, - jobjectArray pointerCoordsObjArray) { +static jlong android_view_MotionEvent_nativeInitialize( + JNIEnv* env, jclass clazz, jlong nativePtr, jint deviceId, jint source, jint displayId, + jint action, jint flags, jint edgeFlags, jint metaState, jint buttonState, + jint classification, jfloat xOffset, jfloat yOffset, jfloat xPrecision, jfloat yPrecision, + jlong downTimeNanos, jlong eventTimeNanos, jint pointerCount, + jobjectArray pointerPropertiesObjArray, jobjectArray pointerCoordsObjArray) { if (!validatePointerCount(env, pointerCount) || !validatePointerPropertiesArray(env, pointerPropertiesObjArray, pointerCount) || !validatePointerCoordsObjArray(env, pointerCoordsObjArray, pointerCount)) { @@ -371,11 +369,12 @@ static jlong android_view_MotionEvent_nativeInitialize(JNIEnv* env, jclass clazz env->DeleteLocalRef(pointerCoordsObj); } - event->initialize(deviceId, source, displayId, action, 0, flags, edgeFlags, metaState, - buttonState, static_cast(classification), - xOffset, yOffset, xPrecision, yPrecision, - AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, - downTimeNanos, eventTimeNanos, pointerCount, pointerProperties, rawPointerCoords); + event->initialize(deviceId, source, displayId, INVALID_HMAC, action, 0, flags, edgeFlags, + metaState, buttonState, static_cast(classification), + 1 /*xScale*/, 1 /*yScale*/, xOffset, yOffset, xPrecision, yPrecision, + AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, + downTimeNanos, eventTimeNanos, pointerCount, pointerProperties, + rawPointerCoords); return reinterpret_cast(event); @@ -757,155 +756,76 @@ static void android_view_MotionEvent_nativeScale(jlong nativePtr, jfloat scale) // ---------------------------------------------------------------------------- static const JNINativeMethod gMotionEventMethods[] = { - /* name, signature, funcPtr */ - { "nativeInitialize", - "(JIIIIIIIIIFFFFJJI[Landroid/view/MotionEvent$PointerProperties;" - "[Landroid/view/MotionEvent$PointerCoords;)J", - (void*)android_view_MotionEvent_nativeInitialize }, - { "nativeDispose", - "(J)V", - (void*)android_view_MotionEvent_nativeDispose }, - { "nativeAddBatch", - "(JJ[Landroid/view/MotionEvent$PointerCoords;I)V", - (void*)android_view_MotionEvent_nativeAddBatch }, - { "nativeReadFromParcel", - "(JLandroid/os/Parcel;)J", - (void*)android_view_MotionEvent_nativeReadFromParcel }, - { "nativeWriteToParcel", - "(JLandroid/os/Parcel;)V", - (void*)android_view_MotionEvent_nativeWriteToParcel }, - { "nativeAxisToString", "(I)Ljava/lang/String;", - (void*)android_view_MotionEvent_nativeAxisToString }, - { "nativeAxisFromString", "(Ljava/lang/String;)I", - (void*)android_view_MotionEvent_nativeAxisFromString }, - { "nativeGetPointerProperties", - "(JILandroid/view/MotionEvent$PointerProperties;)V", - (void*)android_view_MotionEvent_nativeGetPointerProperties }, - { "nativeGetPointerCoords", - "(JIILandroid/view/MotionEvent$PointerCoords;)V", - (void*)android_view_MotionEvent_nativeGetPointerCoords }, + /* name, signature, funcPtr */ + {"nativeInitialize", + "(JIIIIIIIIIFFFFJJI[Landroid/view/MotionEvent$PointerProperties;" + "[Landroid/view/MotionEvent$PointerCoords;)J", + (void*)android_view_MotionEvent_nativeInitialize}, + {"nativeDispose", "(J)V", (void*)android_view_MotionEvent_nativeDispose}, + {"nativeAddBatch", "(JJ[Landroid/view/MotionEvent$PointerCoords;I)V", + (void*)android_view_MotionEvent_nativeAddBatch}, + {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J", + (void*)android_view_MotionEvent_nativeReadFromParcel}, + {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V", + (void*)android_view_MotionEvent_nativeWriteToParcel}, + {"nativeAxisToString", "(I)Ljava/lang/String;", + (void*)android_view_MotionEvent_nativeAxisToString}, + {"nativeAxisFromString", "(Ljava/lang/String;)I", + (void*)android_view_MotionEvent_nativeAxisFromString}, + {"nativeGetPointerProperties", "(JILandroid/view/MotionEvent$PointerProperties;)V", + (void*)android_view_MotionEvent_nativeGetPointerProperties}, + {"nativeGetPointerCoords", "(JIILandroid/view/MotionEvent$PointerCoords;)V", + (void*)android_view_MotionEvent_nativeGetPointerCoords}, - // --------------- @FastNative ---------------------- - { "nativeGetPointerId", - "(JI)I", - (void*)android_view_MotionEvent_nativeGetPointerId }, - { "nativeGetToolType", - "(JI)I", - (void*)android_view_MotionEvent_nativeGetToolType }, - { "nativeGetEventTimeNanos", - "(JI)J", - (void*)android_view_MotionEvent_nativeGetEventTimeNanos }, - { "nativeGetRawAxisValue", - "(JIII)F", - (void*)android_view_MotionEvent_nativeGetRawAxisValue }, - { "nativeGetAxisValue", - "(JIII)F", - (void*)android_view_MotionEvent_nativeGetAxisValue }, - { "nativeTransform", - "(JLandroid/graphics/Matrix;)V", - (void*)android_view_MotionEvent_nativeTransform }, + // --------------- @FastNative ---------------------- + {"nativeGetPointerId", "(JI)I", (void*)android_view_MotionEvent_nativeGetPointerId}, + {"nativeGetToolType", "(JI)I", (void*)android_view_MotionEvent_nativeGetToolType}, + {"nativeGetEventTimeNanos", "(JI)J", + (void*)android_view_MotionEvent_nativeGetEventTimeNanos}, + {"nativeGetRawAxisValue", "(JIII)F", (void*)android_view_MotionEvent_nativeGetRawAxisValue}, + {"nativeGetAxisValue", "(JIII)F", (void*)android_view_MotionEvent_nativeGetAxisValue}, + {"nativeTransform", "(JLandroid/graphics/Matrix;)V", + (void*)android_view_MotionEvent_nativeTransform}, - // --------------- @CriticalNative ------------------ + // --------------- @CriticalNative ------------------ - { "nativeCopy", - "(JJZ)J", - (void*)android_view_MotionEvent_nativeCopy }, - { "nativeGetDeviceId", - "(J)I", - (void*)android_view_MotionEvent_nativeGetDeviceId }, - { "nativeGetSource", - "(J)I", - (void*)android_view_MotionEvent_nativeGetSource }, - { "nativeSetSource", - "(JI)V", - (void*)android_view_MotionEvent_nativeSetSource }, - { "nativeGetDisplayId", - "(J)I", - (void*)android_view_MotionEvent_nativeGetDisplayId }, - { "nativeSetDisplayId", - "(JI)V", - (void*)android_view_MotionEvent_nativeSetDisplayId }, - { "nativeGetAction", - "(J)I", - (void*)android_view_MotionEvent_nativeGetAction }, - { "nativeSetAction", - "(JI)V", - (void*)android_view_MotionEvent_nativeSetAction }, - { "nativeGetActionButton", - "(J)I", - (void*)android_view_MotionEvent_nativeGetActionButton}, - { "nativeSetActionButton", - "(JI)V", - (void*)android_view_MotionEvent_nativeSetActionButton}, - { "nativeIsTouchEvent", - "(J)Z", - (void*)android_view_MotionEvent_nativeIsTouchEvent }, - { "nativeGetFlags", - "(J)I", - (void*)android_view_MotionEvent_nativeGetFlags }, - { "nativeSetFlags", - "(JI)V", - (void*)android_view_MotionEvent_nativeSetFlags }, - { "nativeGetEdgeFlags", - "(J)I", - (void*)android_view_MotionEvent_nativeGetEdgeFlags }, - { "nativeSetEdgeFlags", - "(JI)V", - (void*)android_view_MotionEvent_nativeSetEdgeFlags }, - { "nativeGetMetaState", - "(J)I", - (void*)android_view_MotionEvent_nativeGetMetaState }, - { "nativeGetButtonState", - "(J)I", - (void*)android_view_MotionEvent_nativeGetButtonState }, - { "nativeSetButtonState", - "(JI)V", - (void*)android_view_MotionEvent_nativeSetButtonState }, - { "nativeGetClassification", - "(J)I", - (void*)android_view_MotionEvent_nativeGetClassification }, - { "nativeOffsetLocation", - "(JFF)V", - (void*)android_view_MotionEvent_nativeOffsetLocation }, - { "nativeGetXOffset", - "(J)F", - (void*)android_view_MotionEvent_nativeGetXOffset }, - { "nativeGetYOffset", - "(J)F", - (void*)android_view_MotionEvent_nativeGetYOffset }, - { "nativeGetXPrecision", - "(J)F", - (void*)android_view_MotionEvent_nativeGetXPrecision }, - { "nativeGetYPrecision", - "(J)F", - (void*)android_view_MotionEvent_nativeGetYPrecision }, - { "nativeGetXCursorPosition", - "(J)F", - (void*)android_view_MotionEvent_nativeGetXCursorPosition }, - { "nativeGetYCursorPosition", - "(J)F", - (void*)android_view_MotionEvent_nativeGetYCursorPosition }, - { "nativeSetCursorPosition", - "(JFF)V", - (void*)android_view_MotionEvent_nativeSetCursorPosition }, - { "nativeGetDownTimeNanos", - "(J)J", - (void*)android_view_MotionEvent_nativeGetDownTimeNanos }, - { "nativeSetDownTimeNanos", - "(JJ)V", - (void*)android_view_MotionEvent_nativeSetDownTimeNanos }, - { "nativeGetPointerCount", - "(J)I", - (void*)android_view_MotionEvent_nativeGetPointerCount }, - { "nativeFindPointerIndex", - "(JI)I", - (void*)android_view_MotionEvent_nativeFindPointerIndex }, - { "nativeGetHistorySize", - "(J)I", - (void*)android_view_MotionEvent_nativeGetHistorySize }, - { "nativeScale", - "(JF)V", - (void*)android_view_MotionEvent_nativeScale }, + {"nativeCopy", "(JJZ)J", (void*)android_view_MotionEvent_nativeCopy}, + {"nativeGetDeviceId", "(J)I", (void*)android_view_MotionEvent_nativeGetDeviceId}, + {"nativeGetSource", "(J)I", (void*)android_view_MotionEvent_nativeGetSource}, + {"nativeSetSource", "(JI)V", (void*)android_view_MotionEvent_nativeSetSource}, + {"nativeGetDisplayId", "(J)I", (void*)android_view_MotionEvent_nativeGetDisplayId}, + {"nativeSetDisplayId", "(JI)V", (void*)android_view_MotionEvent_nativeSetDisplayId}, + {"nativeGetAction", "(J)I", (void*)android_view_MotionEvent_nativeGetAction}, + {"nativeSetAction", "(JI)V", (void*)android_view_MotionEvent_nativeSetAction}, + {"nativeGetActionButton", "(J)I", (void*)android_view_MotionEvent_nativeGetActionButton}, + {"nativeSetActionButton", "(JI)V", (void*)android_view_MotionEvent_nativeSetActionButton}, + {"nativeIsTouchEvent", "(J)Z", (void*)android_view_MotionEvent_nativeIsTouchEvent}, + {"nativeGetFlags", "(J)I", (void*)android_view_MotionEvent_nativeGetFlags}, + {"nativeSetFlags", "(JI)V", (void*)android_view_MotionEvent_nativeSetFlags}, + {"nativeGetEdgeFlags", "(J)I", (void*)android_view_MotionEvent_nativeGetEdgeFlags}, + {"nativeSetEdgeFlags", "(JI)V", (void*)android_view_MotionEvent_nativeSetEdgeFlags}, + {"nativeGetMetaState", "(J)I", (void*)android_view_MotionEvent_nativeGetMetaState}, + {"nativeGetButtonState", "(J)I", (void*)android_view_MotionEvent_nativeGetButtonState}, + {"nativeSetButtonState", "(JI)V", (void*)android_view_MotionEvent_nativeSetButtonState}, + {"nativeGetClassification", "(J)I", + (void*)android_view_MotionEvent_nativeGetClassification}, + {"nativeOffsetLocation", "(JFF)V", (void*)android_view_MotionEvent_nativeOffsetLocation}, + {"nativeGetXOffset", "(J)F", (void*)android_view_MotionEvent_nativeGetXOffset}, + {"nativeGetYOffset", "(J)F", (void*)android_view_MotionEvent_nativeGetYOffset}, + {"nativeGetXPrecision", "(J)F", (void*)android_view_MotionEvent_nativeGetXPrecision}, + {"nativeGetYPrecision", "(J)F", (void*)android_view_MotionEvent_nativeGetYPrecision}, + {"nativeGetXCursorPosition", "(J)F", + (void*)android_view_MotionEvent_nativeGetXCursorPosition}, + {"nativeGetYCursorPosition", "(J)F", + (void*)android_view_MotionEvent_nativeGetYCursorPosition}, + {"nativeSetCursorPosition", "(JFF)V", + (void*)android_view_MotionEvent_nativeSetCursorPosition}, + {"nativeGetDownTimeNanos", "(J)J", (void*)android_view_MotionEvent_nativeGetDownTimeNanos}, + {"nativeSetDownTimeNanos", "(JJ)V", (void*)android_view_MotionEvent_nativeSetDownTimeNanos}, + {"nativeGetPointerCount", "(J)I", (void*)android_view_MotionEvent_nativeGetPointerCount}, + {"nativeFindPointerIndex", "(JI)I", (void*)android_view_MotionEvent_nativeFindPointerIndex}, + {"nativeGetHistorySize", "(J)I", (void*)android_view_MotionEvent_nativeGetHistorySize}, + {"nativeScale", "(JF)V", (void*)android_view_MotionEvent_nativeScale}, }; int register_android_view_MotionEvent(JNIEnv* env) { diff --git a/core/jni/android_view_MotionEvent.h b/core/jni/android_view_MotionEvent.h index 0cf1fb2f22036..9ce4bf367688f 100644 --- a/core/jni/android_view_MotionEvent.h +++ b/core/jni/android_view_MotionEvent.h @@ -38,4 +38,4 @@ extern status_t android_view_MotionEvent_recycle(JNIEnv* env, jobject eventObj); } // namespace android -#endif // _ANDROID_OS_KEYEVENT_H +#endif // _ANDROID_VIEW_MOTIONEVENT_H diff --git a/core/tests/coretests/src/android/view/KeyEventTest.java b/core/tests/coretests/src/android/view/KeyEventTest.java index 88d3bdbe3c929..14999c7f5642b 100644 --- a/core/tests/coretests/src/android/view/KeyEventTest.java +++ b/core/tests/coretests/src/android/view/KeyEventTest.java @@ -20,8 +20,10 @@ import static android.view.Display.INVALID_DISPLAY; import static org.junit.Assert.assertEquals; +import android.os.Parcel; + +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; @@ -40,6 +42,7 @@ public class KeyEventTest { private static final int SCAN_CODE = 0; private static final int FLAGS = 0; private static final int SOURCE = InputDevice.SOURCE_KEYBOARD; + private static final byte[] HMAC = null; private static final String CHARACTERS = null; @Test @@ -65,25 +68,15 @@ public class KeyEventTest { KeyEvent keyEvent = KeyEvent.obtain(DOWN_TIME, EVENT_TIME, ACTION, KEYCODE, REPEAT, METASTATE, DEVICE_ID, SCAN_CODE, FLAGS, SOURCE, CHARACTERS); KeyEvent keyEvent2 = KeyEvent.obtain(keyEvent); - assertEquals(keyEvent.getDownTime(), keyEvent2.getDownTime()); - assertEquals(keyEvent.getEventTime(), keyEvent2.getEventTime()); - assertEquals(keyEvent.getAction(), keyEvent2.getAction()); - assertEquals(keyEvent.getKeyCode(), keyEvent2.getKeyCode()); - assertEquals(keyEvent.getRepeatCount(), keyEvent2.getRepeatCount()); - assertEquals(keyEvent.getMetaState(), keyEvent2.getMetaState()); - assertEquals(keyEvent.getDeviceId(), keyEvent2.getDeviceId()); - assertEquals(keyEvent.getScanCode(), keyEvent2.getScanCode()); - assertEquals(keyEvent.getFlags(), keyEvent2.getFlags()); - assertEquals(keyEvent.getSource(), keyEvent2.getSource()); - assertEquals(keyEvent.getDisplayId(), keyEvent2.getDisplayId()); - assertEquals(keyEvent.getCharacters(), keyEvent2.getCharacters()); + compareKeys(keyEvent, keyEvent2); } @Test public void testObtainWithDisplayId() { final int displayId = 5; KeyEvent keyEvent = KeyEvent.obtain(DOWN_TIME, EVENT_TIME, ACTION, KEYCODE, REPEAT, - METASTATE, DEVICE_ID, SCAN_CODE, FLAGS, SOURCE, displayId, CHARACTERS); + METASTATE, DEVICE_ID, SCAN_CODE, FLAGS, SOURCE, displayId, null /* hmac*/, + CHARACTERS); assertEquals(DOWN_TIME, keyEvent.getDownTime()); assertEquals(EVENT_TIME, keyEvent.getEventTime()); assertEquals(ACTION, keyEvent.getAction()); @@ -97,4 +90,44 @@ public class KeyEventTest { assertEquals(displayId, keyEvent.getDisplayId()); assertEquals(CHARACTERS, keyEvent.getCharacters()); } + + @Test + public void testParcelUnparcel() { + KeyEvent key1 = createKey(); + Parcel parcel = Parcel.obtain(); + key1.writeToParcel(parcel, 0 /*flags*/); + parcel.setDataPosition(0); + + KeyEvent key2 = KeyEvent.CREATOR.createFromParcel(parcel); + parcel.recycle(); + + compareKeys(key1, key2); + } + + @Test + public void testConstructor() { + KeyEvent key1 = createKey(); + KeyEvent key2 = new KeyEvent(key1); + compareKeys(key1, key2); + } + + private static KeyEvent createKey() { + return KeyEvent.obtain(DOWN_TIME, EVENT_TIME, ACTION, KEYCODE, REPEAT, + METASTATE, DEVICE_ID, SCAN_CODE, FLAGS, SOURCE, INVALID_DISPLAY, HMAC, CHARACTERS); + } + + private static void compareKeys(KeyEvent key1, KeyEvent key2) { + assertEquals(key1.getDownTime(), key2.getDownTime()); + assertEquals(key1.getEventTime(), key2.getEventTime()); + assertEquals(key1.getAction(), key2.getAction()); + assertEquals(key1.getKeyCode(), key2.getKeyCode()); + assertEquals(key1.getRepeatCount(), key2.getRepeatCount()); + assertEquals(key1.getMetaState(), key2.getMetaState()); + assertEquals(key1.getDeviceId(), key2.getDeviceId()); + assertEquals(key1.getScanCode(), key2.getScanCode()); + assertEquals(key1.getFlags(), key2.getFlags()); + assertEquals(key1.getSource(), key2.getSource()); + assertEquals(key1.getDisplayId(), key2.getDisplayId()); + assertEquals(key1.getCharacters(), key2.getCharacters()); + } } diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java index da0d82047cbec..7c76656e1e6fb 100644 --- a/services/core/java/com/android/server/policy/PhoneWindowManager.java +++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java @@ -3075,7 +3075,7 @@ public class PhoneWindowManager implements WindowManagerPolicy { event.getAction(), fallbackAction.keyCode, event.getRepeatCount(), fallbackAction.metaState, event.getDeviceId(), event.getScanCode(), - flags, event.getSource(), event.getDisplayId(), null); + flags, event.getSource(), event.getDisplayId(), null /* hmac */, null); if (!interceptFallback(focusedToken, fallbackEvent, policyFlags)) { fallbackEvent.recycle();