aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/event
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/event')
-rw-r--r--java/src/com/android/inputmethod/event/Combiner.java14
-rw-r--r--java/src/com/android/inputmethod/event/CombinerChain.java69
-rw-r--r--java/src/com/android/inputmethod/event/DeadKeyCombiner.java5
-rw-r--r--java/src/com/android/inputmethod/event/Event.java45
-rw-r--r--java/src/com/android/inputmethod/event/EventDecoderSpec.java26
-rw-r--r--java/src/com/android/inputmethod/event/EventInterpreter.java58
6 files changed, 115 insertions, 102 deletions
diff --git a/java/src/com/android/inputmethod/event/Combiner.java b/java/src/com/android/inputmethod/event/Combiner.java
index ab6b70c04..c3869a299 100644
--- a/java/src/com/android/inputmethod/event/Combiner.java
+++ b/java/src/com/android/inputmethod/event/Combiner.java
@@ -16,14 +16,22 @@
package com.android.inputmethod.event;
+import java.util.ArrayList;
+
/**
- * A generic interface for combiners.
+ * A generic interface for combiners. Combiners are objects that transform chains of input events
+ * into committable strings and manage feedback to show to the user on the combining state.
*/
public interface Combiner {
/**
- * Combine an event with the existing state and return the new event.
+ * Process an event, possibly combining it with the existing state and return the new event.
+ *
+ * If this event does not result in any new event getting passed down the chain, this method
+ * returns null. It may also modify the previous event list if appropriate.
+ *
+ * @param previousEvents the previous events in this composition.
* @param event the event to combine with the existing state.
* @return the resulting event.
*/
- Event combine(Event event);
+ Event processEvent(ArrayList<Event> previousEvents, Event event);
}
diff --git a/java/src/com/android/inputmethod/event/CombinerChain.java b/java/src/com/android/inputmethod/event/CombinerChain.java
new file mode 100644
index 000000000..0e01c819a
--- /dev/null
+++ b/java/src/com/android/inputmethod/event/CombinerChain.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2014 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;
+
+import com.android.inputmethod.latin.utils.CollectionUtils;
+
+import java.util.ArrayList;
+
+/**
+ * This class implements the logic chain between receiving events and generating code points.
+ *
+ * Event sources are multiple. It may be a hardware keyboard, a D-PAD, a software keyboard,
+ * or any exotic input source.
+ * This class will orchestrate the composing chain that starts with an event as its input. Each
+ * composer will be given turns one after the other.
+ * The output is composed of two sequences of code points: the first, representing the already
+ * finished combining part, will be shown normally as the composing string, while the second is
+ * feedback on the composing state and will typically be shown with different styling such as
+ * a colored background.
+ */
+public class CombinerChain {
+ // TODO: Create an object type to represent input material + visual feedback + decoding state
+
+ private final ArrayList<Combiner> mCombiners;
+
+ /**
+ * Create an combiner chain.
+ *
+ * The combiner chain takes events as inputs and outputs code points and combining state.
+ * For example, if the input language is Japanese, the combining chain will typically perform
+ * kana conversion.
+ *
+ * @param combinerList A list of combiners to be applied in order.
+ */
+ public CombinerChain(final Combiner... combinerList) {
+ mCombiners = CollectionUtils.newArrayList();
+ // The dead key combiner is always active, and always first
+ mCombiners.add(new DeadKeyCombiner());
+ }
+
+ // Pass a new event through the whole chain.
+ public void processEvent(final ArrayList<Event> previousEvents, final Event newEvent) {
+ final ArrayList<Event> modifiablePreviousEvents = new ArrayList<Event>(previousEvents);
+ Event event = newEvent;
+ for (final Combiner combiner : mCombiners) {
+ // A combiner can never return more than one event; it can return several
+ // code points, but they should be encapsulated within one event.
+ event = combiner.processEvent(modifiablePreviousEvents, event);
+ if (null == event) {
+ // Combiners return null if they eat the event.
+ return;
+ }
+ }
+ }
+}
diff --git a/java/src/com/android/inputmethod/event/DeadKeyCombiner.java b/java/src/com/android/inputmethod/event/DeadKeyCombiner.java
index ae8639713..f77ce6347 100644
--- a/java/src/com/android/inputmethod/event/DeadKeyCombiner.java
+++ b/java/src/com/android/inputmethod/event/DeadKeyCombiner.java
@@ -21,14 +21,17 @@ import android.view.KeyCharacterMap;
import com.android.inputmethod.latin.Constants;
+import java.util.ArrayList;
+
/**
* A combiner that handles dead keys.
*/
public class DeadKeyCombiner implements Combiner {
+ // TODO: make this a list of events instead
final StringBuilder mDeadSequence = new StringBuilder();
@Override
- public Event combine(final Event event) {
+ public Event processEvent(final ArrayList<Event> previousEvents, final Event event) {
if (null == event) return null; // Just in case some combiner is broken
if (TextUtils.isEmpty(mDeadSequence)) {
if (event.isDead()) {
diff --git a/java/src/com/android/inputmethod/event/Event.java b/java/src/com/android/inputmethod/event/Event.java
index bd4143d25..db4023436 100644
--- a/java/src/com/android/inputmethod/event/Event.java
+++ b/java/src/com/android/inputmethod/event/Event.java
@@ -52,6 +52,8 @@ public class Event {
final public static int EVENT_GESTURE = 4;
// An event corresponding to the manual pick of a suggestion.
final public static int EVENT_SUGGESTION_PICKED = 5;
+ // An event corresponding to a string generated by some software process.
+ final public static int EVENT_SOFTWARE_GENERATED_STRING = 6;
// 0 is a valid code point, so we use -1 here.
final public static int NOT_A_CODE_POINT = -1;
@@ -71,6 +73,9 @@ public class Event {
// it's not relevant.
final public int mCodePoint;
+ // If applicable, this contains the string that should be input.
+ final public CharSequence mText;
+
// The key code associated with the event, if relevant. This is relevant whenever this event
// has been triggered by a key press, but not for a gesture for example. This has conceptually
// no link to the code point, although keys that enter a straight code point may often set
@@ -96,9 +101,11 @@ public class Event {
final public Event mNextEvent;
// This method is private - to create a new event, use one of the create* utility methods.
- private Event(final int type, final int codePoint, final int keyCode, final int x, final int y,
- final SuggestedWordInfo suggestedWordInfo, final int flags, final Event next) {
+ private Event(final int type, final CharSequence text, final int codePoint, final int keyCode,
+ final int x, final int y, final SuggestedWordInfo suggestedWordInfo, final int flags,
+ final Event next) {
mType = type;
+ mText = text;
mCodePoint = codePoint;
mKeyCode = keyCode;
mX = x;
@@ -123,13 +130,13 @@ public class Event {
public static Event createSoftwareKeypressEvent(final int codePoint, final int keyCode,
final int x, final int y) {
- return new Event(EVENT_INPUT_KEYPRESS, codePoint, keyCode, x, y,
+ return new Event(EVENT_INPUT_KEYPRESS, null /* text */, codePoint, keyCode, x, y,
null /* suggestedWordInfo */, FLAG_NONE, null);
}
public static Event createHardwareKeypressEvent(final int codePoint, final int keyCode,
final Event next) {
- return new Event(EVENT_INPUT_KEYPRESS, codePoint, keyCode,
+ return new Event(EVENT_INPUT_KEYPRESS, null /* text */, codePoint, keyCode,
Constants.EXTERNAL_KEYBOARD_COORDINATE, Constants.EXTERNAL_KEYBOARD_COORDINATE,
null /* suggestedWordInfo */, FLAG_NONE, next);
}
@@ -137,7 +144,7 @@ public class Event {
// This creates an input event for a dead character. @see {@link #FLAG_DEAD}
public static Event createDeadEvent(final int codePoint, final int keyCode, final Event next) {
// TODO: add an argument or something if we ever create a software layout with dead keys.
- return new Event(EVENT_INPUT_KEYPRESS, codePoint, keyCode,
+ return new Event(EVENT_INPUT_KEYPRESS, null /* text */, codePoint, keyCode,
Constants.EXTERNAL_KEYBOARD_COORDINATE, Constants.EXTERNAL_KEYBOARD_COORDINATE,
null /* suggestedWordInfo */, FLAG_DEAD, next);
}
@@ -151,7 +158,7 @@ public class Event {
*/
public static Event createEventForCodePointFromUnknownSource(final int codePoint) {
// TODO: should we have a different type of event for this? After all, it's not a key press.
- return new Event(EVENT_INPUT_KEYPRESS, codePoint, NOT_A_KEY_CODE,
+ return new Event(EVENT_INPUT_KEYPRESS, null /* text */, codePoint, NOT_A_KEY_CODE,
Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE,
null /* suggestedWordInfo */, FLAG_NONE, null /* next */);
}
@@ -167,7 +174,7 @@ public class Event {
public static Event createEventForCodePointFromAlreadyTypedText(final int codePoint,
final int x, final int y) {
// TODO: should we have a different type of event for this? After all, it's not a key press.
- return new Event(EVENT_INPUT_KEYPRESS, codePoint, NOT_A_KEY_CODE, x, y,
+ return new Event(EVENT_INPUT_KEYPRESS, null /* text */, codePoint, NOT_A_KEY_CODE, x, y,
null /* suggestedWordInfo */, FLAG_NONE, null /* next */);
}
@@ -176,13 +183,28 @@ public class Event {
* @return an event for this suggestion pick.
*/
public static Event createSuggestionPickedEvent(final SuggestedWordInfo suggestedWordInfo) {
- return new Event(EVENT_SUGGESTION_PICKED, NOT_A_CODE_POINT, NOT_A_KEY_CODE,
+ return new Event(EVENT_SUGGESTION_PICKED, suggestedWordInfo.mWord,
+ NOT_A_CODE_POINT, NOT_A_KEY_CODE,
Constants.SUGGESTION_STRIP_COORDINATE, Constants.SUGGESTION_STRIP_COORDINATE,
suggestedWordInfo, FLAG_NONE, null);
}
+ /**
+ * Creates an input event with a CharSequence. This is used by some software processes whose
+ * output is a string, possibly with styling. Examples include press on a multi-character key,
+ * or combination that outputs a string.
+ * @param text the CharSequence associated with this event.
+ * @param keyCode the key code, or NOT_A_KEYCODE if not applicable.
+ * @return an event for this text.
+ */
+ public static Event createSoftwareTextEvent(final CharSequence text, final int keyCode) {
+ return new Event(EVENT_SOFTWARE_GENERATED_STRING, text, NOT_A_CODE_POINT, keyCode,
+ Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE,
+ null /* suggestedWordInfo */, FLAG_NONE, null /* next */);
+ }
+
public static Event createNotHandledEvent() {
- return new Event(EVENT_NOT_HANDLED, NOT_A_CODE_POINT, NOT_A_KEY_CODE,
+ return new Event(EVENT_NOT_HANDLED, null /* text */, NOT_A_CODE_POINT, NOT_A_KEY_CODE,
Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE,
null /* suggestedWordInfo */, FLAG_NONE, null);
}
@@ -198,11 +220,6 @@ public class Event {
return EVENT_INPUT_KEYPRESS == mType && Constants.SUGGESTION_STRIP_COORDINATE == mX;
}
- // TODO: remove this method - we should not have to test this
- public boolean isCommittable() {
- return EVENT_INPUT_KEYPRESS == mType || EVENT_MODE_KEY == mType || EVENT_TOGGLE == mType;
- }
-
public boolean isHandled() {
return EVENT_NOT_HANDLED != mType;
}
diff --git a/java/src/com/android/inputmethod/event/EventDecoderSpec.java b/java/src/com/android/inputmethod/event/EventDecoderSpec.java
deleted file mode 100644
index 303b4b4c9..000000000
--- a/java/src/com/android/inputmethod/event/EventDecoderSpec.java
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * 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;
-
-/**
- * Class describing a decoder chain. This will depend on the language and the input medium (soft
- * or hard keyboard for example).
- */
-public class EventDecoderSpec {
- public EventDecoderSpec() {
- }
-}
diff --git a/java/src/com/android/inputmethod/event/EventInterpreter.java b/java/src/com/android/inputmethod/event/EventInterpreter.java
deleted file mode 100644
index bcf10fc58..000000000
--- a/java/src/com/android/inputmethod/event/EventInterpreter.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * 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;
-
-import com.android.inputmethod.latin.utils.CollectionUtils;
-
-import java.util.ArrayList;
-
-/**
- * This class implements the logic between receiving events and generating code points.
- *
- * Event sources are multiple. It may be a hardware keyboard, a D-PAD, a software keyboard,
- * or any exotic input source.
- * This class will orchestrate the decoding chain that starts with an event and ends up with
- * a stream of code points + decoding state.
- */
-public class EventInterpreter {
- // TODO: Implement an object pool for events, as we'll create a lot of them
- // TODO: Create a combiner
- // TODO: Create an object type to represent input material + visual feedback + decoding state
-
- private final EventDecoderSpec mDecoderSpec;
- private final ArrayList<Combiner> mCombiners;
-
- /**
- * 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.
- */
- public EventInterpreter(final EventDecoderSpec specification) {
- mDecoderSpec = null != specification ? specification : new EventDecoderSpec();
- mCombiners = CollectionUtils.newArrayList();
- mCombiners.add(new DeadKeyCombiner());
- }
-}