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

Commit 125ad223 authored by Jean Chalard's avatar Jean Chalard Committed by Android (Google) Code Review
Browse files

Merge "Abandon the idea of an Event pool and make Event immutable (B1)"

parents e4d07ac7 ae74b8cf
Loading
Loading
Loading
Loading
+12 −18
Original line number Diff line number Diff line
@@ -54,39 +54,33 @@ public class Event {

    final private static int NOT_A_CODE_POINT = 0;

    private int mType; // The type of event - one of the constants above
    final private int mType; // The type of event - one of the constants above
    // The code point associated with the event, if relevant. This is a unicode code point, and
    // has nothing to do with other representations of the key. It is only relevant if this event
    // is the right type: COMMITTABLE or DEAD or TOGGLE, but for a mode key like hankaku/zenkaku or
    // ctrl, there is no code point associated so this should be NOT_A_CODE_POINT to avoid
    // unintentional use of its value when it's not relevant.
    private int mCodePoint;
    final public int mCodePoint;

    static Event obtainEvent() {
        // TODO: create an event pool instead
        return new Event();
    // This method is private - to create a new event, use one of the create* utility methods.
    private Event(final int type, final int codePoint) {
        mType = type;
        mCodePoint = codePoint;
    }

    public void setDeadEvent(final int codePoint) {
        mType = EVENT_DEAD;
        mCodePoint = codePoint;
    public static Event createDeadEvent(final int codePoint) {
        return new Event(EVENT_DEAD, codePoint);
    }

    public void setCommittableEvent(final int codePoint) {
        mType = EVENT_COMMITTABLE;
        mCodePoint = codePoint;
    public static Event createCommittableEvent(final int codePoint) {
        return new Event(EVENT_COMMITTABLE, codePoint);
    }

    public void setNotHandledEvent() {
        mType = EVENT_NOT_HANDLED;
        mCodePoint = NOT_A_CODE_POINT; // Just in case
    public static Event createNotHandledEvent() {
        return new Event(EVENT_NOT_HANDLED, NOT_A_CODE_POINT);
    }

    public boolean isCommittable() {
        return EVENT_COMMITTABLE == mType;
    }

    public int getCodePoint() {
        return mCodePoint;
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -107,7 +107,7 @@ public class EventInterpreter {

    private boolean onEvent(final Event event) {
        if (event.isCommittable()) {
            mLatinIme.onCodeInput(event.getCodePoint(),
            mLatinIme.onCodeInput(event.mCodePoint,
                    Constants.EXTERNAL_KEYBOARD_COORDINATE, Constants.EXTERNAL_KEYBOARD_COORDINATE);
            return true;
        }
+4 −7
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@ public class HardwareKeyboardEventDecoder implements HardwareEventDecoder {

    @Override
    public Event decodeHardwareKey(final KeyEvent keyEvent) {
        final Event event = Event.obtainEvent();
        // KeyEvent#getUnicodeChar() does not exactly returns a unicode char, but rather a value
        // that includes both the unicode char in the lower 21 bits and flags in the upper bits,
        // hence the name "codePointAndFlags". {@see KeyEvent#getUnicodeChar()} for more info.
@@ -48,22 +47,20 @@ public class HardwareKeyboardEventDecoder implements HardwareEventDecoder {
        // the key for 'A' or Space, but also Backspace or Ctrl or Caps Lock.
        final int keyCode = keyEvent.getKeyCode();
        if (KeyEvent.KEYCODE_DEL == keyCode) {
            event.setCommittableEvent(Constants.CODE_DELETE);
            return event;
            return Event.createCommittableEvent(Constants.CODE_DELETE);
        }
        if (keyEvent.isPrintingKey() || KeyEvent.KEYCODE_SPACE == keyCode
                || KeyEvent.KEYCODE_ENTER == keyCode) {
            if (0 != (codePointAndFlags & KeyCharacterMap.COMBINING_ACCENT)) {
                // A dead key.
                event.setDeadEvent(codePointAndFlags & KeyCharacterMap.COMBINING_ACCENT_MASK);
                return Event.createDeadEvent(codePointAndFlags & KeyCharacterMap.COMBINING_ACCENT_MASK);
            } else {
                // A committable character. This should be committed right away, taking into
                // account the current state.
                event.setCommittableEvent(codePointAndFlags);
                return Event.createCommittableEvent(codePointAndFlags);
            }
        } else {
            event.setNotHandledEvent();
            return Event.createNotHandledEvent();
        }
        return event;
    }
}