From 982aa9d78ce91db1a5efe7afd75e586756ba32aa Mon Sep 17 00:00:00 2001 From: Arpit Singh Date: Wed, 17 Jan 2024 15:30:41 +0000 Subject: Add NDK API to obtain Java InputEvent from Native AInputEvent This CL adds an NDK API to obtain a copy of native AInputEvent as Java InputEvent. Test: atest MotionEventTest KeyEventTest Bug: 298948992 Change-Id: Ia8ce8ab2bdfc54289eb2402a1beaee68dd1030b0 --- native/android/input.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'native/android/input.cpp') diff --git a/native/android/input.cpp b/native/android/input.cpp index 64e8efeaa4e8..6efb0280ac02 100644 --- a/native/android/input.cpp +++ b/native/android/input.cpp @@ -314,6 +314,23 @@ const AInputEvent* AMotionEvent_fromJava(JNIEnv* env, jobject motionEvent) { return event; } +jobject AInputEvent_toJava(JNIEnv* env, const AInputEvent* aInputEvent) { + LOG_ALWAYS_FATAL_IF(aInputEvent == nullptr, "Expected aInputEvent to be non-null"); + const int32_t eventType = AInputEvent_getType(aInputEvent); + switch (eventType) { + case AINPUT_EVENT_TYPE_MOTION: + return android::android_view_MotionEvent_obtainAsCopy(env, + static_cast( + *aInputEvent)); + case AINPUT_EVENT_TYPE_KEY: + return android::android_view_KeyEvent_fromNative(env, + static_cast( + *aInputEvent)); + default: + LOG_ALWAYS_FATAL("Unexpected event type %d in AInputEvent_toJava.", eventType); + } +} + void AInputQueue_attachLooper(AInputQueue* queue, ALooper* looper, int ident, ALooper_callbackFunc callback, void* data) { InputQueue* iq = static_cast(queue); -- cgit v1.2.3-59-g8ed1b From 14ee29abbf9d8353982de0353548023fb590bdf5 Mon Sep 17 00:00:00 2001 From: Arpit Singh Date: Fri, 16 Feb 2024 12:10:39 +0000 Subject: Rename android.view.InputEvent obtain copy from native methods Rename obtain Java InputEvent copy from native methods for Key and Motion events for consistency. This CL renames 1. android_view_MotionEvent_obtainAsCopy -> android_view_MotionEvent_obtainAsCopy 2. android_view_KeyEvent_fromNative -> android_view_KeyEvent_obtainAsCopy 3. android_view_KeyEvent_toNative -> android_view_KeyEvent_obtainAsCopy Bug: 324375527 Test: atest MotionEventTest KeyEventTest Change-Id: I2a2fee29fd8ffcce04a939d19d3f7a0ce0c5dcf1 --- core/jni/android_view_InputEventReceiver.cpp | 4 ++-- core/jni/android_view_InputEventSender.cpp | 2 +- core/jni/android_view_InputQueue.cpp | 2 +- core/jni/android_view_KeyCharacterMap.cpp | 2 +- core/jni/android_view_KeyEvent.cpp | 4 ++-- core/jni/android_view_KeyEvent.h | 4 ++-- native/android/input.cpp | 8 ++++---- .../com_android_server_input_InputManagerService.cpp | 17 +++++++++-------- 8 files changed, 22 insertions(+), 21 deletions(-) (limited to 'native/android/input.cpp') diff --git a/core/jni/android_view_InputEventReceiver.cpp b/core/jni/android_view_InputEventReceiver.cpp index f93b3068a229..a88314434340 100644 --- a/core/jni/android_view_InputEventReceiver.cpp +++ b/core/jni/android_view_InputEventReceiver.cpp @@ -378,8 +378,8 @@ status_t NativeInputEventReceiver::consumeEvents(JNIEnv* env, ALOGD("channel '%s' ~ Received key event.", getInputChannelName().c_str()); } inputEventObj = - android_view_KeyEvent_fromNative(env, - static_cast(*inputEvent)); + android_view_KeyEvent_obtainAsCopy(env, + static_cast(*inputEvent)); break; case InputEventType::MOTION: { diff --git a/core/jni/android_view_InputEventSender.cpp b/core/jni/android_view_InputEventSender.cpp index b5fbb22215b4..88b02baab924 100644 --- a/core/jni/android_view_InputEventSender.cpp +++ b/core/jni/android_view_InputEventSender.cpp @@ -359,7 +359,7 @@ static jboolean nativeSendKeyEvent(JNIEnv* env, jclass clazz, jlong senderPtr, jint seq, jobject eventObj) { sp sender = reinterpret_cast(senderPtr); - const KeyEvent event = android_view_KeyEvent_toNative(env, eventObj); + const KeyEvent event = android_view_KeyEvent_obtainAsCopy(env, eventObj); status_t status = sender->sendKeyEvent(seq, &event); return !status; } diff --git a/core/jni/android_view_InputQueue.cpp b/core/jni/android_view_InputQueue.cpp index a0d081d2cd26..50d2cbe2ce74 100644 --- a/core/jni/android_view_InputQueue.cpp +++ b/core/jni/android_view_InputQueue.cpp @@ -221,7 +221,7 @@ static jlong nativeSendKeyEvent(JNIEnv* env, jobject clazz, jlong ptr, jobject e jboolean predispatch) { InputQueue* queue = reinterpret_cast(ptr); KeyEvent* event = queue->createKeyEvent(); - *event = android_view_KeyEvent_toNative(env, eventObj); + *event = android_view_KeyEvent_obtainAsCopy(env, eventObj); if (predispatch) { event->setFlags(event->getFlags() | AKEY_EVENT_FLAG_PREDISPATCH); diff --git a/core/jni/android_view_KeyCharacterMap.cpp b/core/jni/android_view_KeyCharacterMap.cpp index a79e37afd4cd..3325e893980d 100644 --- a/core/jni/android_view_KeyCharacterMap.cpp +++ b/core/jni/android_view_KeyCharacterMap.cpp @@ -219,7 +219,7 @@ static jobjectArray nativeGetEvents(JNIEnv *env, jobject clazz, jlong ptr, result = env->NewObjectArray(jsize(events.size()), gKeyEventClassInfo.clazz, NULL); if (result) { for (size_t i = 0; i < events.size(); i++) { - jobject keyEventObj = android_view_KeyEvent_fromNative(env, events.itemAt(i)); + jobject keyEventObj = android_view_KeyEvent_obtainAsCopy(env, events.itemAt(i)); if (!keyEventObj) break; // threw OOM exception env->SetObjectArrayElement(result, jsize(i), keyEventObj); env->DeleteLocalRef(keyEventObj); diff --git a/core/jni/android_view_KeyEvent.cpp b/core/jni/android_view_KeyEvent.cpp index a9c991919361..908ab5ed2eac 100644 --- a/core/jni/android_view_KeyEvent.cpp +++ b/core/jni/android_view_KeyEvent.cpp @@ -94,7 +94,7 @@ static struct { // ---------------------------------------------------------------------------- -jobject android_view_KeyEvent_fromNative(JNIEnv* env, const KeyEvent& event) { +jobject android_view_KeyEvent_obtainAsCopy(JNIEnv* env, const KeyEvent& event) { ScopedLocalRef hmac = toJbyteArray(env, event.getHmac()); jobject eventObj = env->CallStaticObjectMethod(gKeyEventClassInfo.clazz, gKeyEventClassInfo.obtain, @@ -113,7 +113,7 @@ jobject android_view_KeyEvent_fromNative(JNIEnv* env, const KeyEvent& event) { return eventObj; } -KeyEvent android_view_KeyEvent_toNative(JNIEnv* env, jobject eventObj) { +KeyEvent android_view_KeyEvent_obtainAsCopy(JNIEnv* env, jobject eventObj) { jint id = env->GetIntField(eventObj, gKeyEventClassInfo.mId); jint deviceId = env->GetIntField(eventObj, gKeyEventClassInfo.mDeviceId); jint source = env->GetIntField(eventObj, gKeyEventClassInfo.mSource); diff --git a/core/jni/android_view_KeyEvent.h b/core/jni/android_view_KeyEvent.h index bc4876a7a835..eab7c972817b 100644 --- a/core/jni/android_view_KeyEvent.h +++ b/core/jni/android_view_KeyEvent.h @@ -27,11 +27,11 @@ class KeyEvent; /* Obtains an instance of a DVM KeyEvent object as a copy of a native KeyEvent instance. * Returns NULL on error. */ -extern jobject android_view_KeyEvent_fromNative(JNIEnv* env, const KeyEvent& event); +extern jobject android_view_KeyEvent_obtainAsCopy(JNIEnv* env, const KeyEvent& event); /* Copies the contents of a DVM KeyEvent object to a native KeyEvent instance. * Returns non-zero on error. */ -extern KeyEvent android_view_KeyEvent_toNative(JNIEnv* env, jobject eventObj); +extern KeyEvent android_view_KeyEvent_obtainAsCopy(JNIEnv* env, jobject eventObj); /* Recycles a DVM KeyEvent object. * Key events should only be recycled if they are owned by the system since user diff --git a/native/android/input.cpp b/native/android/input.cpp index 6efb0280ac02..4708e69447ec 100644 --- a/native/android/input.cpp +++ b/native/android/input.cpp @@ -87,7 +87,7 @@ int64_t AKeyEvent_getDownTime(const AInputEvent* key_event) { const AInputEvent* AKeyEvent_fromJava(JNIEnv* env, jobject keyEvent) { std::unique_ptr event = std::make_unique(); - *event = android::android_view_KeyEvent_toNative(env, keyEvent); + *event = android::android_view_KeyEvent_obtainAsCopy(env, keyEvent); return event.release(); } @@ -323,9 +323,9 @@ jobject AInputEvent_toJava(JNIEnv* env, const AInputEvent* aInputEvent) { static_cast( *aInputEvent)); case AINPUT_EVENT_TYPE_KEY: - return android::android_view_KeyEvent_fromNative(env, - static_cast( - *aInputEvent)); + return android::android_view_KeyEvent_obtainAsCopy(env, + static_cast( + *aInputEvent)); default: LOG_ALWAYS_FATAL("Unexpected event type %d in AInputEvent_toJava.", eventType); } diff --git a/services/core/jni/com_android_server_input_InputManagerService.cpp b/services/core/jni/com_android_server_input_InputManagerService.cpp index cbbcd965f4af..93c0c6258e9b 100644 --- a/services/core/jni/com_android_server_input_InputManagerService.cpp +++ b/services/core/jni/com_android_server_input_InputManagerService.cpp @@ -1510,8 +1510,8 @@ bool NativeInputManager::filterInputEvent(const InputEvent& inputEvent, uint32_t switch (inputEvent.getType()) { case InputEventType::KEY: inputEventObj.reset( - android_view_KeyEvent_fromNative(env, - static_cast(inputEvent))); + android_view_KeyEvent_obtainAsCopy(env, + static_cast(inputEvent))); break; case InputEventType::MOTION: inputEventObj.reset( @@ -1559,7 +1559,7 @@ void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent& keyEvent, const nsecs_t when = keyEvent.getEventTime(); JNIEnv* env = jniEnv(); - ScopedLocalRef keyEventObj(env, android_view_KeyEvent_fromNative(env, keyEvent)); + ScopedLocalRef keyEventObj(env, android_view_KeyEvent_obtainAsCopy(env, keyEvent)); if (!keyEventObj.get()) { ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing."); return; @@ -1639,7 +1639,7 @@ nsecs_t NativeInputManager::interceptKeyBeforeDispatching(const sp& tok // Token may be null ScopedLocalRef tokenObj(env, javaObjectForIBinder(env, token)); - ScopedLocalRef keyEventObj(env, android_view_KeyEvent_fromNative(env, keyEvent)); + ScopedLocalRef keyEventObj(env, android_view_KeyEvent_obtainAsCopy(env, keyEvent)); if (!keyEventObj.get()) { ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching."); return 0; @@ -1670,7 +1670,7 @@ std::optional NativeInputManager::dispatchUnhandledKey(const sp tokenObj(env, javaObjectForIBinder(env, token)); - ScopedLocalRef keyEventObj(env, android_view_KeyEvent_fromNative(env, keyEvent)); + ScopedLocalRef keyEventObj(env, android_view_KeyEvent_obtainAsCopy(env, keyEvent)); if (!keyEventObj.get()) { ALOGE("Failed to obtain key event object for dispatchUnhandledKey."); return {}; @@ -1689,7 +1689,8 @@ std::optional NativeInputManager::dispatchUnhandledKey(const sp(syncMode); if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) { - const KeyEvent keyEvent = android_view_KeyEvent_toNative(env, inputEventObj); + const KeyEvent keyEvent = android_view_KeyEvent_obtainAsCopy(env, inputEventObj); const InputEventInjectionResult result = im->getInputManager()->getDispatcher().injectInputEvent(&keyEvent, targetUid, mode, std::chrono::milliseconds( @@ -2101,7 +2102,7 @@ static jobject nativeVerifyInputEvent(JNIEnv* env, jobject nativeImplObj, jobjec NativeInputManager* im = getNativeInputManager(env, nativeImplObj); if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) { - const KeyEvent keyEvent = android_view_KeyEvent_toNative(env, inputEventObj); + const KeyEvent keyEvent = android_view_KeyEvent_obtainAsCopy(env, inputEventObj); std::unique_ptr verifiedEvent = im->getInputManager()->getDispatcher().verifyInputEvent(keyEvent); if (verifiedEvent == nullptr) { -- cgit v1.2.3-59-g8ed1b From 43c34c8991e5d2a11296d9449b3348189df8f616 Mon Sep 17 00:00:00 2001 From: Arpit Singh Date: Wed, 21 Feb 2024 17:25:23 +0000 Subject: Use ScopedLocalRef in InputEvent jni function This CL updates some jni functions that manipulate Key and Motion events to use ScopedLocalRef instead of plain jobject for better life-cycle tracking. Test: presubmit Bug: 324375527 Change-Id: I154b0606d3c0912f0df7a890faf7246b575863f5 --- core/jni/android_view_InputEventReceiver.cpp | 9 ++++----- core/jni/android_view_KeyCharacterMap.cpp | 8 ++++---- core/jni/android_view_KeyEvent.cpp | 22 +++++++++++---------- core/jni/android_view_KeyEvent.h | 7 +++++-- core/jni/android_view_MotionEvent.cpp | 23 ++++++++++++---------- core/jni/android_view_MotionEvent.h | 11 +++++++---- core/jni/android_view_MotionPredictor.cpp | 3 ++- native/android/input.cpp | 6 ++++-- ...om_android_server_input_InputManagerService.cpp | 17 ++++++++-------- 9 files changed, 59 insertions(+), 47 deletions(-) (limited to 'native/android/input.cpp') diff --git a/core/jni/android_view_InputEventReceiver.cpp b/core/jni/android_view_InputEventReceiver.cpp index a88314434340..f1b93db3e731 100644 --- a/core/jni/android_view_InputEventReceiver.cpp +++ b/core/jni/android_view_InputEventReceiver.cpp @@ -371,7 +371,7 @@ status_t NativeInputEventReceiver::consumeEvents(JNIEnv* env, } } - jobject inputEventObj; + ScopedLocalRef inputEventObj(env); switch (inputEvent->getType()) { case InputEventType::KEY: if (kDebugDispatchCycle) { @@ -447,20 +447,19 @@ status_t NativeInputEventReceiver::consumeEvents(JNIEnv* env, default: assert(false); // InputConsumer should prevent this from ever happening - inputEventObj = nullptr; } - if (inputEventObj) { + if (inputEventObj.get()) { if (kDebugDispatchCycle) { ALOGD("channel '%s' ~ Dispatching input event.", getInputChannelName().c_str()); } env->CallVoidMethod(receiverObj.get(), - gInputEventReceiverClassInfo.dispatchInputEvent, seq, inputEventObj); + gInputEventReceiverClassInfo.dispatchInputEvent, seq, + inputEventObj.get()); if (env->ExceptionCheck()) { ALOGE("Exception dispatching input event."); skipCallbacks = true; } - env->DeleteLocalRef(inputEventObj); } else { ALOGW("channel '%s' ~ Failed to obtain event object.", getInputChannelName().c_str()); diff --git a/core/jni/android_view_KeyCharacterMap.cpp b/core/jni/android_view_KeyCharacterMap.cpp index 3325e893980d..2b19ddfed5eb 100644 --- a/core/jni/android_view_KeyCharacterMap.cpp +++ b/core/jni/android_view_KeyCharacterMap.cpp @@ -219,10 +219,10 @@ static jobjectArray nativeGetEvents(JNIEnv *env, jobject clazz, jlong ptr, result = env->NewObjectArray(jsize(events.size()), gKeyEventClassInfo.clazz, NULL); if (result) { for (size_t i = 0; i < events.size(); i++) { - jobject keyEventObj = android_view_KeyEvent_obtainAsCopy(env, events.itemAt(i)); - if (!keyEventObj) break; // threw OOM exception - env->SetObjectArrayElement(result, jsize(i), keyEventObj); - env->DeleteLocalRef(keyEventObj); + ScopedLocalRef keyEventObj = + android_view_KeyEvent_obtainAsCopy(env, events.itemAt(i)); + if (!keyEventObj.get()) break; // threw OOM exception + env->SetObjectArrayElement(result, jsize(i), keyEventObj.get()); } } } diff --git a/core/jni/android_view_KeyEvent.cpp b/core/jni/android_view_KeyEvent.cpp index 908ab5ed2eac..ca8752f93e11 100644 --- a/core/jni/android_view_KeyEvent.cpp +++ b/core/jni/android_view_KeyEvent.cpp @@ -94,21 +94,23 @@ static struct { // ---------------------------------------------------------------------------- -jobject android_view_KeyEvent_obtainAsCopy(JNIEnv* env, const KeyEvent& event) { +ScopedLocalRef android_view_KeyEvent_obtainAsCopy(JNIEnv* env, const KeyEvent& event) { ScopedLocalRef hmac = toJbyteArray(env, event.getHmac()); - jobject eventObj = - env->CallStaticObjectMethod(gKeyEventClassInfo.clazz, gKeyEventClassInfo.obtain, - event.getId(), event.getDownTime(), event.getEventTime(), - event.getAction(), event.getKeyCode(), - event.getRepeatCount(), event.getMetaState(), - event.getDeviceId(), event.getScanCode(), event.getFlags(), - event.getSource(), event.getDisplayId(), hmac.get(), - nullptr); + ScopedLocalRef + eventObj(env, + env->CallStaticObjectMethod(gKeyEventClassInfo.clazz, + gKeyEventClassInfo.obtain, event.getId(), + event.getDownTime(), event.getEventTime(), + event.getAction(), event.getKeyCode(), + event.getRepeatCount(), event.getMetaState(), + event.getDeviceId(), event.getScanCode(), + event.getFlags(), event.getSource(), + event.getDisplayId(), hmac.get(), nullptr)); if (env->ExceptionCheck()) { ALOGE("An exception occurred while obtaining a key event."); LOGE_EX(env); env->ExceptionClear(); - return NULL; + return ScopedLocalRef(env); } return eventObj; } diff --git a/core/jni/android_view_KeyEvent.h b/core/jni/android_view_KeyEvent.h index eab7c972817b..838f0130bd40 100644 --- a/core/jni/android_view_KeyEvent.h +++ b/core/jni/android_view_KeyEvent.h @@ -17,17 +17,20 @@ #ifndef _ANDROID_VIEW_KEYEVENT_H #define _ANDROID_VIEW_KEYEVENT_H -#include "jni.h" +#include #include #include +#include "jni.h" + namespace android { class KeyEvent; /* Obtains an instance of a DVM KeyEvent object as a copy of a native KeyEvent instance. * Returns NULL on error. */ -extern jobject android_view_KeyEvent_obtainAsCopy(JNIEnv* env, const KeyEvent& event); +extern ScopedLocalRef android_view_KeyEvent_obtainAsCopy(JNIEnv* env, + const KeyEvent& event); /* Copies the contents of a DVM KeyEvent object to a native KeyEvent instance. * Returns non-zero on error. */ diff --git a/core/jni/android_view_MotionEvent.cpp b/core/jni/android_view_MotionEvent.cpp index 33fbdb83cf48..285dee364bb0 100644 --- a/core/jni/android_view_MotionEvent.cpp +++ b/core/jni/android_view_MotionEvent.cpp @@ -77,25 +77,28 @@ MotionEvent* android_view_MotionEvent_getNativePtr(JNIEnv* env, jobject eventObj env->GetLongField(eventObj, gMotionEventClassInfo.mNativePtr)); } -static void android_view_MotionEvent_setNativePtr(JNIEnv* env, jobject eventObj, - MotionEvent* event) { - env->SetLongField(eventObj, gMotionEventClassInfo.mNativePtr, - reinterpret_cast(event)); +static void android_view_MotionEvent_setNativePtr(JNIEnv* env, ScopedLocalRef& eventObj, + MotionEvent* event) { + env->SetLongField(eventObj.get(), gMotionEventClassInfo.mNativePtr, + reinterpret_cast(event)); } -jobject android_view_MotionEvent_obtainAsCopy(JNIEnv* env, const MotionEvent& event) { +ScopedLocalRef android_view_MotionEvent_obtainAsCopy(JNIEnv* env, + const MotionEvent& event) { std::unique_ptr destEvent = std::make_unique(); destEvent->copyFrom(&event, true); return android_view_MotionEvent_obtainFromNative(env, std::move(destEvent)); } -jobject android_view_MotionEvent_obtainFromNative(JNIEnv* env, std::unique_ptr event) { +ScopedLocalRef android_view_MotionEvent_obtainFromNative( + JNIEnv* env, std::unique_ptr event) { if (event == nullptr) { - return nullptr; + return ScopedLocalRef(env); } - jobject eventObj = - env->CallStaticObjectMethod(gMotionEventClassInfo.clazz, gMotionEventClassInfo.obtain); - if (env->ExceptionCheck() || !eventObj) { + ScopedLocalRef eventObj(env, + env->CallStaticObjectMethod(gMotionEventClassInfo.clazz, + gMotionEventClassInfo.obtain)); + if (env->ExceptionCheck() || !eventObj.get()) { LOGE_EX(env); LOG_ALWAYS_FATAL("An exception occurred while obtaining a Java motion event."); } diff --git a/core/jni/android_view_MotionEvent.h b/core/jni/android_view_MotionEvent.h index e81213608d68..b1bf1c435e26 100644 --- a/core/jni/android_view_MotionEvent.h +++ b/core/jni/android_view_MotionEvent.h @@ -17,21 +17,24 @@ #ifndef _ANDROID_VIEW_MOTIONEVENT_H #define _ANDROID_VIEW_MOTIONEVENT_H -#include "jni.h" +#include #include +#include "jni.h" + namespace android { class MotionEvent; /* Obtains an instance of a DVM MotionEvent object as a copy of a native MotionEvent instance. * Returns NULL on error. */ -extern jobject android_view_MotionEvent_obtainAsCopy(JNIEnv* env, const MotionEvent& event); +extern ScopedLocalRef android_view_MotionEvent_obtainAsCopy(JNIEnv* env, + const MotionEvent& event); /* Obtains an instance of a Java MotionEvent object, taking over the ownership of the provided * native MotionEvent instance. Crashes on error. */ -extern jobject android_view_MotionEvent_obtainFromNative(JNIEnv* env, - std::unique_ptr event); +extern ScopedLocalRef android_view_MotionEvent_obtainFromNative( + JNIEnv* env, std::unique_ptr event); /* Gets the underlying native MotionEvent instance within a DVM MotionEvent object. * Returns NULL if the event is NULL or if it is uninitialized. */ diff --git a/core/jni/android_view_MotionPredictor.cpp b/core/jni/android_view_MotionPredictor.cpp index de3e81c7088b..0707e99205aa 100644 --- a/core/jni/android_view_MotionPredictor.cpp +++ b/core/jni/android_view_MotionPredictor.cpp @@ -61,7 +61,8 @@ static jobject android_view_MotionPredictor_nativePredict(JNIEnv* env, jclass cl MotionPredictor* predictor = reinterpret_cast(ptr); return android_view_MotionEvent_obtainFromNative(env, predictor->predict(static_cast( - predictionTimeNanos))); + predictionTimeNanos))) + .release(); } static jboolean android_view_MotionPredictor_nativeIsPredictionAvailable(JNIEnv* env, jclass clazz, diff --git a/native/android/input.cpp b/native/android/input.cpp index 4708e69447ec..53699bc706ea 100644 --- a/native/android/input.cpp +++ b/native/android/input.cpp @@ -321,11 +321,13 @@ jobject AInputEvent_toJava(JNIEnv* env, const AInputEvent* aInputEvent) { case AINPUT_EVENT_TYPE_MOTION: return android::android_view_MotionEvent_obtainAsCopy(env, static_cast( - *aInputEvent)); + *aInputEvent)) + .release(); case AINPUT_EVENT_TYPE_KEY: return android::android_view_KeyEvent_obtainAsCopy(env, static_cast( - *aInputEvent)); + *aInputEvent)) + .release(); default: LOG_ALWAYS_FATAL("Unexpected event type %d in AInputEvent_toJava.", eventType); } diff --git a/services/core/jni/com_android_server_input_InputManagerService.cpp b/services/core/jni/com_android_server_input_InputManagerService.cpp index 93c0c6258e9b..2150eb33234f 100644 --- a/services/core/jni/com_android_server_input_InputManagerService.cpp +++ b/services/core/jni/com_android_server_input_InputManagerService.cpp @@ -1509,15 +1509,14 @@ bool NativeInputManager::filterInputEvent(const InputEvent& inputEvent, uint32_t ScopedLocalRef inputEventObj(env); switch (inputEvent.getType()) { case InputEventType::KEY: - inputEventObj.reset( + inputEventObj = android_view_KeyEvent_obtainAsCopy(env, - static_cast(inputEvent))); + static_cast(inputEvent)); break; case InputEventType::MOTION: - inputEventObj.reset( - android_view_MotionEvent_obtainAsCopy(env, - static_cast( - inputEvent))); + inputEventObj = android_view_MotionEvent_obtainAsCopy(env, + static_cast( + inputEvent)); break; default: return true; // dispatch the event normally @@ -1559,7 +1558,7 @@ void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent& keyEvent, const nsecs_t when = keyEvent.getEventTime(); JNIEnv* env = jniEnv(); - ScopedLocalRef keyEventObj(env, android_view_KeyEvent_obtainAsCopy(env, keyEvent)); + ScopedLocalRef keyEventObj = android_view_KeyEvent_obtainAsCopy(env, keyEvent); if (!keyEventObj.get()) { ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing."); return; @@ -1639,7 +1638,7 @@ nsecs_t NativeInputManager::interceptKeyBeforeDispatching(const sp& tok // Token may be null ScopedLocalRef tokenObj(env, javaObjectForIBinder(env, token)); - ScopedLocalRef keyEventObj(env, android_view_KeyEvent_obtainAsCopy(env, keyEvent)); + ScopedLocalRef keyEventObj = android_view_KeyEvent_obtainAsCopy(env, keyEvent); if (!keyEventObj.get()) { ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching."); return 0; @@ -1670,7 +1669,7 @@ std::optional NativeInputManager::dispatchUnhandledKey(const sp tokenObj(env, javaObjectForIBinder(env, token)); - ScopedLocalRef keyEventObj(env, android_view_KeyEvent_obtainAsCopy(env, keyEvent)); + ScopedLocalRef keyEventObj = android_view_KeyEvent_obtainAsCopy(env, keyEvent); if (!keyEventObj.get()) { ALOGE("Failed to obtain key event object for dispatchUnhandledKey."); return {}; -- cgit v1.2.3-59-g8ed1b