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

Commit 323e664b authored by Automerger Merge Worker's avatar Automerger Merge Worker
Browse files

Add ID to InputEvent. am: c8362b24

Change-Id: I788a34a23a73d4af229f01e33d7cd3de1d52c544
parents e4dd53af c8362b24
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -233,6 +233,21 @@ public abstract class InputEvent implements Parcelable {
        return mSeq;
    }

    /**
     * Gets the ID of this event. This is generated when an event is created and preserved until its
     * last stage. It won't change just because the event crosses process boundary, but should
     * change when making a copy with modifications.
     * <p>
     * To avoid exposing app usage to other processes this ID is generated from a CSPRNG. Therefore
     * there isn't 100% guarantee on the uniqueness of this ID, though the chance of ID collisions
     * is considerably low. The rule of thumb is not to rely on the uniqueness for production logic,
     * but a good source for tracking an event (e.g. logging and profiling).
     *
     * @return The ID of this event.
     * @hide
     */
    public abstract int getId();

    public int describeContents() {
        return 0;
    }
+40 −5
Original line number Diff line number Diff line
@@ -1265,6 +1265,7 @@ public class KeyEvent extends InputEvent implements Parcelable {

    private KeyEvent mNext;

    private int mId;
    @UnsupportedAppUsage
    private int mDeviceId;
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
@@ -1350,9 +1351,9 @@ public class KeyEvent extends InputEvent implements Parcelable {

    private static native String nativeKeyCodeToString(int keyCode);
    private static native int nativeKeyCodeFromString(String keyCode);
    private static native int nativeNextId();

    private KeyEvent() {
    }
    private KeyEvent() {}

    /**
     * Create a new key event.
@@ -1362,6 +1363,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
     * @param code The key code.
     */
    public KeyEvent(int action, int code) {
        mId = nativeNextId();
        mAction = action;
        mKeyCode = code;
        mRepeatCount = 0;
@@ -1383,6 +1385,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
     */
    public KeyEvent(long downTime, long eventTime, int action,
                    int code, int repeat) {
        mId = nativeNextId();
        mDownTime = downTime;
        mEventTime = eventTime;
        mAction = action;
@@ -1407,6 +1410,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
     */
    public KeyEvent(long downTime, long eventTime, int action,
                    int code, int repeat, int metaState) {
        mId = nativeNextId();
        mDownTime = downTime;
        mEventTime = eventTime;
        mAction = action;
@@ -1435,6 +1439,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
    public KeyEvent(long downTime, long eventTime, int action,
                    int code, int repeat, int metaState,
                    int deviceId, int scancode) {
        mId = nativeNextId();
        mDownTime = downTime;
        mEventTime = eventTime;
        mAction = action;
@@ -1465,6 +1470,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
    public KeyEvent(long downTime, long eventTime, int action,
                    int code, int repeat, int metaState,
                    int deviceId, int scancode, int flags) {
        mId = nativeNextId();
        mDownTime = downTime;
        mEventTime = eventTime;
        mAction = action;
@@ -1497,6 +1503,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
    public KeyEvent(long downTime, long eventTime, int action,
                    int code, int repeat, int metaState,
                    int deviceId, int scancode, int flags, int source) {
        mId = nativeNextId();
        mDownTime = downTime;
        mEventTime = eventTime;
        mAction = action;
@@ -1523,6 +1530,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
     * @param flags The flags for this key event
     */
    public KeyEvent(long time, String characters, int deviceId, int flags) {
        mId = nativeNextId();
        mDownTime = time;
        mEventTime = time;
        mCharacters = characters;
@@ -1539,6 +1547,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
     * Make an exact copy of an existing key event.
     */
    public KeyEvent(KeyEvent origEvent) {
        mId = origEvent.mId;
        mDownTime = origEvent.mDownTime;
        mEventTime = origEvent.mEventTime;
        mAction = origEvent.mAction;
@@ -1567,6 +1576,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
     */
    @Deprecated
    public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
        mId = nativeNextId();  // Not an exact copy so assign a new ID.
        mDownTime = origEvent.mDownTime;
        mEventTime = eventTime;
        mAction = origEvent.mAction;
@@ -1598,15 +1608,16 @@ public class KeyEvent extends InputEvent implements Parcelable {
    }

    /**
     * Obtains a (potentially recycled) key event.
     * Obtains a (potentially recycled) key event. Used by native code to create a Java object.
     *
     * @hide
     */
    public static KeyEvent obtain(long downTime, long eventTime, int action,
    public static KeyEvent obtain(int id, long downTime, long eventTime, int action,
            int code, int repeat, int metaState,
            int deviceId, int scancode, int flags, int source, int displayId, @Nullable byte[] hmac,
            String characters) {
        KeyEvent ev = obtain();
        ev.mId = id;
        ev.mDownTime = downTime;
        ev.mEventTime = eventTime;
        ev.mAction = action;
@@ -1623,6 +1634,18 @@ public class KeyEvent extends InputEvent implements Parcelable {
        return ev;
    }

    /**
     * Obtains a (potentially recycled) key event.
     *
     * @hide
     */
    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) {
        return obtain(nativeNextId(), downTime, eventTime, action, code, repeat, metaState,
                deviceId, scanCode, flags, source, displayId, null /* hmac */, characters);
    }

    /**
     * Obtains a (potentially recycled) key event.
     *
@@ -1633,7 +1656,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, null /* hmac */, characters);
                flags, source, INVALID_DISPLAY, characters);
    }

    /**
@@ -1645,6 +1668,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
     */
    public static KeyEvent obtain(KeyEvent other) {
        KeyEvent ev = obtain();
        ev.mId = other.mId;
        ev.mDownTime = other.mDownTime;
        ev.mEventTime = other.mEventTime;
        ev.mAction = other.mAction;
@@ -1695,6 +1719,12 @@ public class KeyEvent extends InputEvent implements Parcelable {
        // Do nothing.
    }

    /** @hide */
    @Override
    public int getId() {
        return mId;
    }

    /**
     * Create a new key event that is the same as the given one, but whose
     * event time and repeat count are replaced with the given value.
@@ -1723,6 +1753,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
    public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
            int newRepeat, int newFlags) {
        KeyEvent ret = new KeyEvent(event);
        ret.mId = nativeNextId();  // Not an exact copy so assign a new ID.
        ret.mEventTime = eventTime;
        ret.mRepeatCount = newRepeat;
        ret.mFlags = newFlags;
@@ -1736,6 +1767,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
     * @param action The new action code of the event.
     */
    private KeyEvent(KeyEvent origEvent, int action) {
        mId = nativeNextId();  // Not an exact copy so assign a new ID.
        mDownTime = origEvent.mDownTime;
        mEventTime = origEvent.mEventTime;
        mAction = action;
@@ -1772,6 +1804,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
     */
    public static KeyEvent changeFlags(KeyEvent event, int flags) {
        event = new KeyEvent(event);
        event.mId = nativeNextId();  // Not an exact copy so assign a new ID.
        event.mFlags = flags;
        return event;
    }
@@ -3095,6 +3128,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
    }

    private KeyEvent(Parcel in) {
        mId = in.readInt();
        mDeviceId = in.readInt();
        mSource = in.readInt();
        mDisplayId = in.readInt();
@@ -3114,6 +3148,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(PARCEL_TOKEN_KEY_EVENT);

        out.writeInt(mId);
        out.writeInt(mDeviceId);
        out.writeInt(mSource);
        out.writeInt(mDisplayId);
+8 −0
Original line number Diff line number Diff line
@@ -1550,6 +1550,8 @@ public final class MotionEvent extends InputEvent implements Parcelable {
    private static native long nativeCopy(long destNativePtr, long sourceNativePtr,
            boolean keepHistory);
    @CriticalNative
    private static native int nativeGetId(long nativePtr);
    @CriticalNative
    private static native int nativeGetDeviceId(long nativePtr);
    @CriticalNative
    private static native int nativeGetSource(long nativePtr);
@@ -2024,6 +2026,12 @@ public final class MotionEvent extends InputEvent implements Parcelable {
        }
    }

    /** @hide */
    @Override
    public int getId() {
        return nativeGetId(mNativePtr);
    }

    /** {@inheritDoc} */
    @Override
    public final int getDeviceId() {
+6 −4
Original line number Diff line number Diff line
@@ -5379,7 +5379,8 @@ public final class ViewRootImpl implements ViewParent,
            if (mTracePrefix == null) {
                mTracePrefix = getClass().getSimpleName();
            }
            Trace.traceBegin(traceTag, mTracePrefix + " seq#=" + q.mEvent.getSequenceNumber());
            Trace.traceBegin(traceTag, mTracePrefix + " id=0x"
                    + Integer.toHexString(q.mEvent.getId()));
        }
    }

@@ -7986,12 +7987,13 @@ public final class ViewRootImpl implements ViewParent,

    private void deliverInputEvent(QueuedInputEvent q) {
        Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, "deliverInputEvent",
                q.mEvent.getSequenceNumber());
                q.mEvent.getId());

        if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
            Trace.traceBegin(Trace.TRACE_TAG_VIEW, "deliverInputEvent src=0x"
                    + Integer.toHexString(q.mEvent.getSource()) + " eventTimeNano="
                    + q.mEvent.getEventTimeNano() + " seq#=" + q.mEvent.getSequenceNumber());
                    + q.mEvent.getEventTimeNano() + " id=0x"
                    + Integer.toHexString(q.mEvent.getId()));
        }
        try {
            if (mInputEventConsistencyVerifier != null) {
@@ -8032,7 +8034,7 @@ public final class ViewRootImpl implements ViewParent,

    private void finishInputEvent(QueuedInputEvent q) {
        Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, "deliverInputEvent",
                q.mEvent.getSequenceNumber());
                q.mEvent.getId());

        if (q.mReceiver != null) {
            boolean handled = (q.mFlags & QueuedInputEvent.FLAG_FINISHED_HANDLED) != 0;
+14 −6
Original line number Diff line number Diff line
@@ -75,6 +75,7 @@ static struct {
    jmethodID obtain;
    jmethodID recycle;

    jfieldID mId;
    jfieldID mDeviceId;
    jfieldID mSource;
    jfieldID mDisplayId;
@@ -96,6 +97,7 @@ jobject android_view_KeyEvent_fromNative(JNIEnv* env, const KeyEvent* event) {
    ScopedLocalRef<jbyteArray> hmac = toJbyteArray(env, event->getHmac());
    jobject eventObj =
            env->CallStaticObjectMethod(gKeyEventClassInfo.clazz, gKeyEventClassInfo.obtain,
                                        event->getId(),
                                        nanoseconds_to_milliseconds(event->getDownTime()),
                                        nanoseconds_to_milliseconds(event->getEventTime()),
                                        event->getAction(), event->getKeyCode(),
@@ -114,6 +116,7 @@ jobject android_view_KeyEvent_fromNative(JNIEnv* env, const KeyEvent* event) {

status_t android_view_KeyEvent_toNative(JNIEnv* env, jobject eventObj,
        KeyEvent* event) {
    jint id = env->GetIntField(eventObj, gKeyEventClassInfo.mId);
    jint deviceId = env->GetIntField(eventObj, gKeyEventClassInfo.mDeviceId);
    jint source = env->GetIntField(eventObj, gKeyEventClassInfo.mSource);
    jint displayId = env->GetIntField(eventObj, gKeyEventClassInfo.mDisplayId);
@@ -131,7 +134,7 @@ 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, *hmac, action, flags, keyCode, scanCode,
    event->initialize(id, deviceId, source, displayId, *hmac, action, flags, keyCode, scanCode,
                      metaState, repeatCount, milliseconds_to_nanoseconds(downTime),
                      milliseconds_to_nanoseconds(eventTime));
    return OK;
@@ -159,6 +162,9 @@ static jint android_view_KeyEvent_nativeKeyCodeFromString(JNIEnv* env, jobject c
    return KeyEvent::getKeyCodeFromLabel(keyLabel.c_str());
}

static jint android_view_KeyEvent_nativeNextId() {
    return static_cast<jint>(InputEvent::nextId());
}

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

@@ -167,6 +173,7 @@ static const JNINativeMethod g_methods[] = {
         (void*)android_view_KeyEvent_nativeKeyCodeToString},
        {"nativeKeyCodeFromString", "(Ljava/lang/String;)I",
         (void*)android_view_KeyEvent_nativeKeyCodeFromString},
        {"nativeNextId", "()I", (void*)android_view_KeyEvent_nativeNextId},
};

int register_android_view_KeyEvent(JNIEnv* env) {
@@ -175,10 +182,11 @@ int register_android_view_KeyEvent(JNIEnv* env) {

    gKeyEventClassInfo.obtain =
            GetStaticMethodIDOrDie(env, gKeyEventClassInfo.clazz, "obtain",
                                   "(JJIIIIIIIII[BLjava/lang/String;)Landroid/view/KeyEvent;");
                                   "(IJJIIIIIIIII[BLjava/lang/String;)Landroid/view/KeyEvent;");
    gKeyEventClassInfo.recycle = GetMethodIDOrDie(env, gKeyEventClassInfo.clazz,
            "recycle", "()V");

    gKeyEventClassInfo.mId = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mId", "I");
    gKeyEventClassInfo.mDeviceId = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mDeviceId", "I");
    gKeyEventClassInfo.mSource = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mSource", "I");
    gKeyEventClassInfo.mDisplayId = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mDisplayId",
Loading