Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 43c34c89 authored by Arpit Singh's avatar Arpit Singh
Browse files

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
parent 3d8cc938
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -371,7 +371,7 @@ status_t NativeInputEventReceiver::consumeEvents(JNIEnv* env,
                }
            }

            jobject inputEventObj;
            ScopedLocalRef<jobject> 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());
+4 −4
Original line number Diff line number Diff line
@@ -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<jobject> keyEventObj =
                        android_view_KeyEvent_obtainAsCopy(env, events.itemAt(i));
                if (!keyEventObj.get()) break; // threw OOM exception
                env->SetObjectArrayElement(result, jsize(i), keyEventObj.get());
            }
        }
    }
+12 −10
Original line number Diff line number Diff line
@@ -94,21 +94,23 @@ static struct {

// ----------------------------------------------------------------------------

jobject android_view_KeyEvent_obtainAsCopy(JNIEnv* env, const KeyEvent& event) {
ScopedLocalRef<jobject> android_view_KeyEvent_obtainAsCopy(JNIEnv* env, const KeyEvent& event) {
    ScopedLocalRef<jbyteArray> hmac = toJbyteArray(env, event.getHmac());
    jobject eventObj =
            env->CallStaticObjectMethod(gKeyEventClassInfo.clazz, gKeyEventClassInfo.obtain,
                                        event.getId(), event.getDownTime(), event.getEventTime(),
    ScopedLocalRef<jobject>
            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);
                                                 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<jobject>(env);
    }
    return eventObj;
}
+5 −2
Original line number Diff line number Diff line
@@ -17,17 +17,20 @@
#ifndef _ANDROID_VIEW_KEYEVENT_H
#define _ANDROID_VIEW_KEYEVENT_H

#include "jni.h"
#include <nativehelper/scoped_local_ref.h>
#include <utils/Errors.h>
#include <utils/threads.h>

#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<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. */
+13 −10
Original line number Diff line number Diff line
@@ -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,
static void android_view_MotionEvent_setNativePtr(JNIEnv* env, ScopedLocalRef<jobject>& eventObj,
                                                  MotionEvent* event) {
    env->SetLongField(eventObj, gMotionEventClassInfo.mNativePtr,
    env->SetLongField(eventObj.get(), gMotionEventClassInfo.mNativePtr,
                      reinterpret_cast<jlong>(event));
}

jobject android_view_MotionEvent_obtainAsCopy(JNIEnv* env, const MotionEvent& event) {
ScopedLocalRef<jobject> android_view_MotionEvent_obtainAsCopy(JNIEnv* env,
                                                              const MotionEvent& event) {
    std::unique_ptr<MotionEvent> destEvent = std::make_unique<MotionEvent>();
    destEvent->copyFrom(&event, true);
    return android_view_MotionEvent_obtainFromNative(env, std::move(destEvent));
}

jobject android_view_MotionEvent_obtainFromNative(JNIEnv* env, std::unique_ptr<MotionEvent> event) {
ScopedLocalRef<jobject> android_view_MotionEvent_obtainFromNative(
        JNIEnv* env, std::unique_ptr<MotionEvent> event) {
    if (event == nullptr) {
        return nullptr;
        return ScopedLocalRef<jobject>(env);
    }
    jobject eventObj =
            env->CallStaticObjectMethod(gMotionEventClassInfo.clazz, gMotionEventClassInfo.obtain);
    if (env->ExceptionCheck() || !eventObj) {
    ScopedLocalRef<jobject> 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.");
    }
Loading