diff options
Diffstat (limited to 'java/src/com/android/inputmethod/event')
5 files changed, 177 insertions, 14 deletions
diff --git a/java/src/com/android/inputmethod/event/Event.java b/java/src/com/android/inputmethod/event/Event.java index c96a3362d..215e4dee5 100644 --- a/java/src/com/android/inputmethod/event/Event.java +++ b/java/src/com/android/inputmethod/event/Event.java @@ -29,8 +29,64 @@ package com.android.inputmethod.event; * The combiner should figure out what to do with this. */ public class Event { + // Should the types below be represented by separate classes instead? It would be cleaner + // but probably a bit too much + // An event we don't handle in Latin IME, for example pressing Ctrl on a hardware keyboard. + final public static int EVENT_NOT_HANDLED = 0; + // A character that is already final, for example pressing an alphabetic character on a + // hardware qwerty keyboard. + final public static int EVENT_COMMITTABLE = 1; + // A dead key, which means a character that should combine with what is coming next. Examples + // include the "^" character on an azerty keyboard which combines with "e" to make "ê", or + // AltGr+' on a dvorak international keyboard which combines with "e" to make "é". This is + // true regardless of the language or combining mode, and should be seen as a property of the + // key - a dead key followed by another key with which it can combine should be regarded as if + // the keyboard actually had such a key. + final public static int EVENT_DEAD = 2; + // A toggle event is triggered by a key that affects the previous character. An example would + // be a numeric key on a 10-key keyboard, which would toggle between 1 - a - b - c with + // repeated presses. + final public static int EVENT_TOGGLE = 3; + // A mode event instructs the combiner to change modes. The canonical example would be the + // hankaku/zenkaku key on a Japanese keyboard, or even the caps lock key on a qwerty keyboard + // if handled at the combiner level. + final public static int EVENT_MODE_KEY = 4; + + final private static int NOT_A_CODE_POINT = 0; + + 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; + static Event obtainEvent() { // TODO: create an event pool instead return new Event(); } + + public void setDeadEvent(final int codePoint) { + mType = EVENT_DEAD; + mCodePoint = codePoint; + } + + public void setCommittableEvent(final int codePoint) { + mType = EVENT_COMMITTABLE; + mCodePoint = codePoint; + } + + public void setNotHandledEvent() { + mType = EVENT_NOT_HANDLED; + mCodePoint = NOT_A_CODE_POINT; // Just in case + } + + public boolean isCommittable() { + return EVENT_COMMITTABLE == mType; + } + + public int getCodePoint() { + return mCodePoint; + } } diff --git a/java/src/com/android/inputmethod/event/EventInterpreter.java b/java/src/com/android/inputmethod/event/EventInterpreter.java index 443c269a2..1bd0cca00 100644 --- a/java/src/com/android/inputmethod/event/EventInterpreter.java +++ b/java/src/com/android/inputmethod/event/EventInterpreter.java @@ -16,8 +16,12 @@ package com.android.inputmethod.event; +import android.util.SparseArray; import android.view.KeyEvent; +import com.android.inputmethod.latin.Constants; +import com.android.inputmethod.latin.LatinIME; + /** * This class implements the logic between receiving events and generating code points. * @@ -32,18 +36,45 @@ public class EventInterpreter { // TODO: Create an object type to represent input material + visual feedback + decoding state // TODO: Create an interface to call back to Latin IME through the above object - // TODO: replace this with an associative container to bind device id -> decoder - HardwareEventDecoder mHardwareEventDecoder; - SoftwareEventDecoder mSoftwareEventDecoder; + final EventDecoderSpec mDecoderSpec; + final SparseArray<HardwareEventDecoder> mHardwareEventDecoders; + final SoftwareEventDecoder mSoftwareEventDecoder; + final LatinIME mLatinIme; - public EventInterpreter() { - this(null); + /** + * Create a default interpreter. + * + * This creates a default interpreter that does nothing. A default interpreter should normally + * only be used for fallback purposes, when we really don't know what we want to do with input. + * + * @param latinIme a reference to the ime. + */ + public EventInterpreter(final LatinIME latinIme) { + this(null, latinIme); } - public EventInterpreter(final EventDecoderSpec specification) { - // TODO: create the decoding chain from a specification. The decoders should be - // created lazily - mHardwareEventDecoder = new HardwareKeyboardEventDecoder(0); + /** + * Create an event interpreter according to a specification. + * + * The specification contains information about what to do with events. Typically, it will + * contain information about the type of keyboards - for example, if hardware keyboard(s) is/are + * attached, their type will be included here so that the decoder knows what to do with each + * keypress (a 10-key keyboard is not handled like a qwerty-ish keyboard). + * It also contains information for combining characters. For example, if the input language + * is Japanese, the specification will typically request kana conversion. + * Also note that the specification can be null. This means that we need to create a default + * interpreter that does no specific combining, and assumes the most common cases. + * + * @param specification the specification for event interpretation. null for default. + * @param latinIme a reference to the ime. + */ + public EventInterpreter(final EventDecoderSpec specification, final LatinIME latinIme) { + mDecoderSpec = null != specification ? specification : new EventDecoderSpec(); + // For both, we expect to have only one decoder in almost all cases, hence the default + // capacity of 1. + mHardwareEventDecoders = new SparseArray<HardwareEventDecoder>(1); + mSoftwareEventDecoder = new SoftwareKeyboardEventDecoder(); + mLatinIme = latinIme; } // Helper method to decode a hardware key event into a generic event, and execute any @@ -60,15 +91,26 @@ public class EventInterpreter { } private HardwareEventDecoder getHardwareKeyEventDecoder(final int deviceId) { - // TODO: look up the decoder by device id. It should be created lazily - return mHardwareEventDecoder; + final HardwareEventDecoder decoder = mHardwareEventDecoders.get(deviceId); + if (null != decoder) return decoder; + // TODO: create the decoder according to the specification + final HardwareEventDecoder newDecoder = new HardwareKeyboardEventDecoder(deviceId); + mHardwareEventDecoders.put(deviceId, newDecoder); + return newDecoder; } private SoftwareEventDecoder getSoftwareEventDecoder() { + // Within the context of Latin IME, since we never present several software interfaces + // at the time, we should never need multiple software event decoders at a time. return mSoftwareEventDecoder; } private boolean onEvent(final Event event) { + if (event.isCommittable()) { + mLatinIme.onCodeInput(event.getCodePoint(), + Constants.EXTERNAL_KEYBOARD_COORDINATE, Constants.EXTERNAL_KEYBOARD_COORDINATE); + return true; + } // TODO: Classify the event - input or non-input (see design doc) // TODO: IF action event // Send decoded action back to LatinIME diff --git a/java/src/com/android/inputmethod/event/HardwareKeyboardEventDecoder.java b/java/src/com/android/inputmethod/event/HardwareKeyboardEventDecoder.java index 9aa6a273e..2dbc9f00b 100644 --- a/java/src/com/android/inputmethod/event/HardwareKeyboardEventDecoder.java +++ b/java/src/com/android/inputmethod/event/HardwareKeyboardEventDecoder.java @@ -16,10 +16,17 @@ package com.android.inputmethod.event; +import android.view.KeyCharacterMap; import android.view.KeyEvent; +import com.android.inputmethod.latin.Constants; + /** * A hardware event decoder for a hardware qwerty-ish keyboard. + * + * The events are always hardware keypresses, but they can be key down or key up events, they + * can be dead keys, they can be meta keys like shift or ctrl... This does not deal with + * 10-key like keyboards; a different decoder is used for this. */ public class HardwareKeyboardEventDecoder implements HardwareEventDecoder { final int mDeviceId; @@ -30,7 +37,33 @@ public class HardwareKeyboardEventDecoder implements HardwareEventDecoder { } @Override - public Event decodeHardwareKey(KeyEvent keyEvent) { - return Event.obtainEvent(); + 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. + final int codePointAndFlags = keyEvent.getUnicodeChar(); + // The keyCode is the abstraction used by the KeyEvent to represent different keys that + // do not necessarily map to a unicode character. This represents a physical key, like + // 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; + } + 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); + } else { + // A committable character. This should be committed right away, taking into + // account the current state. + event.setCommittableEvent(codePointAndFlags); + } + } else { + event.setNotHandledEvent(); + } + return event; } } diff --git a/java/src/com/android/inputmethod/event/SoftwareEventDecoder.java b/java/src/com/android/inputmethod/event/SoftwareEventDecoder.java index 0b80d5c0e..d81ee0b37 100644 --- a/java/src/com/android/inputmethod/event/SoftwareEventDecoder.java +++ b/java/src/com/android/inputmethod/event/SoftwareEventDecoder.java @@ -17,7 +17,12 @@ package com.android.inputmethod.event; /** - * An event decoder for software events. + * An event decoder for events out of a software keyboard. + * + * This defines the interface for an event decoder that supports events out of a software keyboard. + * This differs significantly from hardware keyboard event decoders in several respects. First, + * a software keyboard does not have a scancode/layout system; the keypresses that insert + * characters output unicode characters directly. */ public interface SoftwareEventDecoder extends EventDecoder { public Event decodeSoftwareEvent(); diff --git a/java/src/com/android/inputmethod/event/SoftwareKeyboardEventDecoder.java b/java/src/com/android/inputmethod/event/SoftwareKeyboardEventDecoder.java new file mode 100644 index 000000000..de91567c7 --- /dev/null +++ b/java/src/com/android/inputmethod/event/SoftwareKeyboardEventDecoder.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.event; + +/** + * A decoder for events from software keyboard, like the ones displayed by Latin IME. + */ +public class SoftwareKeyboardEventDecoder implements SoftwareEventDecoder { + @Override + public Event decodeSoftwareEvent() { + return null; + } +} |