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

Commit bf0bf99a authored by Adam Wright's avatar Adam Wright Committed by Android (Google) Code Review
Browse files

Revert "Refactor KeyEvent constructors."

This reverts commit 1aa18e2f.

Reason for revert: DM P0 regression (b/270247000)

Change-Id: I8b976d0e390d33b133e54aaa51b3a7aea0563259
parent 1aa18e2f
Loading
Loading
Loading
Loading
+85 −49
Original line number Original line Diff line number Diff line
@@ -1352,7 +1352,6 @@ public class KeyEvent extends InputEvent implements Parcelable {
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
    private int mSource;
    private int mSource;
    private int mDisplayId = INVALID_DISPLAY;
    private int mDisplayId = INVALID_DISPLAY;
    // NOTE: mHmac is private and not used in this class, but it's used on native side / parcel.
    private @Nullable byte[] mHmac;
    private @Nullable byte[] mHmac;
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
    private int mMetaState;
    private int mMetaState;
@@ -1378,7 +1377,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
     */
     */
    private long mEventTime;
    private long mEventTime;
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
    private @Nullable String mCharacters;
    private String mCharacters;


    public interface Callback {
    public interface Callback {
        /**
        /**
@@ -1442,11 +1441,7 @@ public class KeyEvent extends InputEvent implements Parcelable {
    private static native int nativeKeyCodeFromString(String keyCode);
    private static native int nativeKeyCodeFromString(String keyCode);
    private static native int nativeNextId();
    private static native int nativeNextId();


    private KeyEvent() {
    private KeyEvent() {}
        this(/* downTime= */ 0, /* eventTime= */ 0, /* action= */ 0, /* code= */0, /* repeat= */ 0,
                /* metaState= */ 0, /* deviceId= */ 0, /* scancode= */ 0, /* flags= */ 0,
                /* source= */ 0);
    }


    /**
    /**
     * Create a new key event.
     * Create a new key event.
@@ -1456,9 +1451,11 @@ public class KeyEvent extends InputEvent implements Parcelable {
     * @param code The key code.
     * @param code The key code.
     */
     */
    public KeyEvent(int action, int code) {
    public KeyEvent(int action, int code) {
        this(/* downTime= */ 0, /* eventTime= */ 0, action, code, /* repeat= */ 0,
        mId = nativeNextId();
                /* metaState= */ 0, /* deviceId= */ KeyCharacterMap.VIRTUAL_KEYBOARD,
        mAction = action;
                /* scancode= */ 0, /* flags= */ 0, /* source= */ 0);
        mKeyCode = code;
        mRepeatCount = 0;
        mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
    }
    }


    /**
    /**
@@ -1476,9 +1473,13 @@ public class KeyEvent extends InputEvent implements Parcelable {
     */
     */
    public KeyEvent(long downTime, long eventTime, int action,
    public KeyEvent(long downTime, long eventTime, int action,
                    int code, int repeat) {
                    int code, int repeat) {
        this(downTime, eventTime, action, code, repeat, /* metaState= */ 0,
        mId = nativeNextId();
                KeyCharacterMap.VIRTUAL_KEYBOARD, /* scancode= */ 0, /* flags= */ 0,
        mDownTime = TimeUnit.NANOSECONDS.convert(downTime, TimeUnit.MILLISECONDS);
                /* source= */ 0);
        mEventTime = TimeUnit.NANOSECONDS.convert(eventTime, TimeUnit.MILLISECONDS);
        mAction = action;
        mKeyCode = code;
        mRepeatCount = repeat;
        mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
    }
    }


    /**
    /**
@@ -1497,8 +1498,14 @@ public class KeyEvent extends InputEvent implements Parcelable {
     */
     */
    public KeyEvent(long downTime, long eventTime, int action,
    public KeyEvent(long downTime, long eventTime, int action,
                    int code, int repeat, int metaState) {
                    int code, int repeat, int metaState) {
        this(downTime, eventTime, action, code, repeat, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD,
        mId = nativeNextId();
                /* scancode= */ 0, /* flags= */ 0, /* source= */ 0);
        mDownTime = TimeUnit.NANOSECONDS.convert(downTime, TimeUnit.MILLISECONDS);
        mEventTime = TimeUnit.NANOSECONDS.convert(eventTime, TimeUnit.MILLISECONDS);
        mAction = action;
        mKeyCode = code;
        mRepeatCount = repeat;
        mMetaState = metaState;
        mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
    }
    }


    /**
    /**
@@ -1520,8 +1527,15 @@ public class KeyEvent extends InputEvent implements Parcelable {
    public KeyEvent(long downTime, long eventTime, int action,
    public KeyEvent(long downTime, long eventTime, int action,
                    int code, int repeat, int metaState,
                    int code, int repeat, int metaState,
                    int deviceId, int scancode) {
                    int deviceId, int scancode) {
        this(downTime, eventTime, action, code, repeat, metaState, deviceId, scancode,
        mId = nativeNextId();
                /* flags= */ 0, /* source= */ 0);
        mDownTime = TimeUnit.NANOSECONDS.convert(downTime, TimeUnit.MILLISECONDS);
        mEventTime = TimeUnit.NANOSECONDS.convert(eventTime, TimeUnit.MILLISECONDS);
        mAction = action;
        mKeyCode = code;
        mRepeatCount = repeat;
        mMetaState = metaState;
        mDeviceId = deviceId;
        mScanCode = scancode;
    }
    }


    /**
    /**
@@ -1544,8 +1558,16 @@ public class KeyEvent extends InputEvent implements Parcelable {
    public KeyEvent(long downTime, long eventTime, int action,
    public KeyEvent(long downTime, long eventTime, int action,
                    int code, int repeat, int metaState,
                    int code, int repeat, int metaState,
                    int deviceId, int scancode, int flags) {
                    int deviceId, int scancode, int flags) {
        this(downTime, eventTime, action, code, repeat, metaState, deviceId, scancode, flags,
        mId = nativeNextId();
                /* source= */ 0);
        mDownTime = TimeUnit.NANOSECONDS.convert(downTime, TimeUnit.MILLISECONDS);
        mEventTime = TimeUnit.NANOSECONDS.convert(eventTime, TimeUnit.MILLISECONDS);
        mAction = action;
        mKeyCode = code;
        mRepeatCount = repeat;
        mMetaState = metaState;
        mDeviceId = deviceId;
        mScanCode = scancode;
        mFlags = flags;
    }
    }


    /**
    /**
@@ -1569,8 +1591,6 @@ public class KeyEvent extends InputEvent implements Parcelable {
    public KeyEvent(long downTime, long eventTime, int action,
    public KeyEvent(long downTime, long eventTime, int action,
                    int code, int repeat, int metaState,
                    int code, int repeat, int metaState,
                    int deviceId, int scancode, int flags, int source) {
                    int deviceId, int scancode, int flags, int source) {
        // NOTE: this is the canonical constructor, every other constructor that takes KeyEvent
        // attributes should call it
        mId = nativeNextId();
        mId = nativeNextId();
        mDownTime = TimeUnit.NANOSECONDS.convert(downTime, TimeUnit.MILLISECONDS);
        mDownTime = TimeUnit.NANOSECONDS.convert(downTime, TimeUnit.MILLISECONDS);
        mEventTime = TimeUnit.NANOSECONDS.convert(eventTime, TimeUnit.MILLISECONDS);
        mEventTime = TimeUnit.NANOSECONDS.convert(eventTime, TimeUnit.MILLISECONDS);
@@ -1597,18 +1617,36 @@ public class KeyEvent extends InputEvent implements Parcelable {
     * @param flags The flags for this key event
     * @param flags The flags for this key event
     */
     */
    public KeyEvent(long time, String characters, int deviceId, int flags) {
    public KeyEvent(long time, String characters, int deviceId, int flags) {
        this(/* downTime= */ time, /* eventTime= */ time, ACTION_MULTIPLE, KEYCODE_UNKNOWN,
        mId = nativeNextId();
                /* repeat= */ 0, /* metaState= */ 0, deviceId, /* scancode= */ 0, flags,
        mDownTime = TimeUnit.NANOSECONDS.convert(time, TimeUnit.MILLISECONDS);
                /* source= */ InputDevice.SOURCE_KEYBOARD);
        mEventTime = TimeUnit.NANOSECONDS.convert(time, TimeUnit.MILLISECONDS);
        mCharacters = characters;
        mAction = ACTION_MULTIPLE;
        mKeyCode = KEYCODE_UNKNOWN;
        mRepeatCount = 0;
        mDeviceId = deviceId;
        mFlags = flags;
        mSource = InputDevice.SOURCE_KEYBOARD;
    }
    }


    /**
    /**
     * Make an exact copy of an existing key event.
     * Make an exact copy of an existing key event.
     */
     */
    public KeyEvent(KeyEvent origEvent) {
    public KeyEvent(KeyEvent origEvent) {
        this(origEvent, origEvent.mId, origEvent.mEventTime, origEvent.mAction,
        mId = origEvent.mId;
                origEvent.mRepeatCount, origEvent.mHmac == null ? null : origEvent.mHmac.clone(),
        mDownTime = origEvent.mDownTime;
                origEvent.mCharacters);
        mEventTime = origEvent.mEventTime;
        mAction = origEvent.mAction;
        mKeyCode = origEvent.mKeyCode;
        mRepeatCount = origEvent.mRepeatCount;
        mMetaState = origEvent.mMetaState;
        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;
    }
    }


    /**
    /**
@@ -1624,30 +1662,20 @@ public class KeyEvent extends InputEvent implements Parcelable {
     */
     */
    @Deprecated
    @Deprecated
    public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
    public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
        // Not an exact copy so assign a new ID.
        mId = nativeNextId();  // Not an exact copy so assign a new ID.
        // Don't copy HMAC, it will be invalid because eventTime is changing
        this(origEvent, nativeNextId(),
                TimeUnit.NANOSECONDS.convert(eventTime, TimeUnit.MILLISECONDS), origEvent.mAction,
                newRepeat, /* hmac= */ null, origEvent.mCharacters);
    }

    // This is the canonical constructor that should be called for constructors that take a KeyEvent
    private KeyEvent(KeyEvent origEvent, int id, long eventTime, int action, int newRepeat,
            @Nullable byte[] hmac, @Nullable String characters) {
        mId = id;
        mDownTime = origEvent.mDownTime;
        mDownTime = origEvent.mDownTime;
        mEventTime = eventTime;
        mEventTime = TimeUnit.NANOSECONDS.convert(eventTime, TimeUnit.MILLISECONDS);
        mAction = action;
        mAction = origEvent.mAction;
        mKeyCode = origEvent.mKeyCode;
        mKeyCode = origEvent.mKeyCode;
        mRepeatCount = newRepeat;
        mRepeatCount = newRepeat;
        mMetaState = origEvent.mMetaState;
        mMetaState = origEvent.mMetaState;
        mDeviceId = origEvent.mDeviceId;
        mDeviceId = origEvent.mDeviceId;
        mSource = origEvent.mSource;
        mSource = origEvent.mSource;
        mDisplayId = origEvent.mDisplayId;
        mDisplayId = origEvent.mDisplayId;
        mHmac = hmac;
        mHmac = null; // Don't copy HMAC, it will be invalid because eventTime is changing
        mScanCode = origEvent.mScanCode;
        mScanCode = origEvent.mScanCode;
        mFlags = origEvent.mFlags;
        mFlags = origEvent.mFlags;
        mCharacters = characters;
        mCharacters = origEvent.mCharacters;
    }
    }


    private static KeyEvent obtain() {
    private static KeyEvent obtain() {
@@ -1829,11 +1857,21 @@ public class KeyEvent extends InputEvent implements Parcelable {
     * @param action The new action code of the event.
     * @param action The new action code of the event.
     */
     */
    private KeyEvent(KeyEvent origEvent, int action) {
    private KeyEvent(KeyEvent origEvent, int action) {
        // Not an exact copy so assign a new ID
        mId = nativeNextId();  // Not an exact copy so assign a new ID.
        // Don't copy the hmac, it will be invalid since action is changing
        mDownTime = origEvent.mDownTime;
        // Don't copy mCharacters, since one way or the other we'll lose it when changing action.
        mEventTime = origEvent.mEventTime;
        this(origEvent, nativeNextId(), origEvent.mEventTime, action, origEvent.mRepeatCount,
        mAction = action;
                /* hmac= */ null, /* characters= */ null);
        mKeyCode = origEvent.mKeyCode;
        mRepeatCount = origEvent.mRepeatCount;
        mMetaState = origEvent.mMetaState;
        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
        // when changing the action.
    }
    }


    /**
    /**
@@ -3181,8 +3219,6 @@ public class KeyEvent extends InputEvent implements Parcelable {
    }
    }


    private KeyEvent(Parcel in) {
    private KeyEvent(Parcel in) {
        // NOTE: ideally this constructor should call the canonical one, but that would require
        // changing the order the fields are written to the parcel, which could break native code
        mId = in.readInt();
        mId = in.readInt();
        mDeviceId = in.readInt();
        mDeviceId = in.readInt();
        mSource = in.readInt();
        mSource = in.readInt();