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.java11
-rw-r--r--java/src/com/android/inputmethod/event/CombinerChain.java42
-rw-r--r--java/src/com/android/inputmethod/event/DeadKeyCombiner.java10
-rw-r--r--java/src/com/android/inputmethod/event/Event.java61
4 files changed, 106 insertions, 18 deletions
diff --git a/java/src/com/android/inputmethod/event/Combiner.java b/java/src/com/android/inputmethod/event/Combiner.java
index c3869a299..8b808c6b3 100644
--- a/java/src/com/android/inputmethod/event/Combiner.java
+++ b/java/src/com/android/inputmethod/event/Combiner.java
@@ -34,4 +34,15 @@ public interface Combiner {
* @return the resulting event.
*/
Event processEvent(ArrayList<Event> previousEvents, Event event);
+
+ /**
+ * Get the feedback that should be shown to the user for the current state of this combiner.
+ * @return A CharSequence representing the feedback to show users. It may include styles.
+ */
+ CharSequence getCombiningStateFeedback();
+
+ /**
+ * Reset the state of this combiner, for example when the cursor was moved.
+ */
+ void reset();
}
diff --git a/java/src/com/android/inputmethod/event/CombinerChain.java b/java/src/com/android/inputmethod/event/CombinerChain.java
index 0e01c819a..5ca9842c1 100644
--- a/java/src/com/android/inputmethod/event/CombinerChain.java
+++ b/java/src/com/android/inputmethod/event/CombinerChain.java
@@ -16,6 +16,8 @@
package com.android.inputmethod.event;
+import android.text.SpannableStringBuilder;
+
import com.android.inputmethod.latin.utils.CollectionUtils;
import java.util.ArrayList;
@@ -33,8 +35,10 @@ import java.util.ArrayList;
* a colored background.
*/
public class CombinerChain {
- // TODO: Create an object type to represent input material + visual feedback + decoding state
-
+ // The already combined text, as described above
+ private StringBuilder mCombinedText;
+ // The feedback on the composing state, as described above
+ private SpannableStringBuilder mStateFeedback;
private final ArrayList<Combiner> mCombiners;
/**
@@ -50,9 +54,23 @@ public class CombinerChain {
mCombiners = CollectionUtils.newArrayList();
// The dead key combiner is always active, and always first
mCombiners.add(new DeadKeyCombiner());
+ mCombinedText = new StringBuilder();
+ mStateFeedback = new SpannableStringBuilder();
}
- // Pass a new event through the whole chain.
+ public void reset() {
+ mCombinedText.setLength(0);
+ mStateFeedback.clear();
+ for (final Combiner c : mCombiners) {
+ c.reset();
+ }
+ }
+
+ /**
+ * Pass a new event through the whole chain.
+ * @param previousEvents the list of previous events in this composition
+ * @param newEvent the new event to process
+ */
public void processEvent(final ArrayList<Event> previousEvents, final Event newEvent) {
final ArrayList<Event> modifiablePreviousEvents = new ArrayList<Event>(previousEvents);
Event event = newEvent;
@@ -62,8 +80,24 @@ public class CombinerChain {
event = combiner.processEvent(modifiablePreviousEvents, event);
if (null == event) {
// Combiners return null if they eat the event.
- return;
+ break;
}
}
+ if (null != event) {
+ mCombinedText.append(event.getTextToCommit());
+ }
+ mStateFeedback.clear();
+ for (int i = mCombiners.size() - 1; i >= 0; --i) {
+ mStateFeedback.append(mCombiners.get(i).getCombiningStateFeedback());
+ }
+ }
+
+ /**
+ * Get the char sequence that should be displayed as the composing word. It may include
+ * styling spans.
+ */
+ public CharSequence getComposingWordWithCombiningFeedback() {
+ final SpannableStringBuilder s = new SpannableStringBuilder(mCombinedText);
+ return s.append(mStateFeedback);
}
}
diff --git a/java/src/com/android/inputmethod/event/DeadKeyCombiner.java b/java/src/com/android/inputmethod/event/DeadKeyCombiner.java
index f77ce6347..89e623a1d 100644
--- a/java/src/com/android/inputmethod/event/DeadKeyCombiner.java
+++ b/java/src/com/android/inputmethod/event/DeadKeyCombiner.java
@@ -61,4 +61,14 @@ public class DeadKeyCombiner implements Combiner {
}
}
}
+
+ @Override
+ public void reset() {
+ mDeadSequence.setLength(0);
+ }
+
+ @Override
+ public CharSequence getCombiningStateFeedback() {
+ return mDeadSequence;
+ }
}
diff --git a/java/src/com/android/inputmethod/event/Event.java b/java/src/com/android/inputmethod/event/Event.java
index bd4143d25..2bfe0732d 100644
--- a/java/src/com/android/inputmethod/event/Event.java
+++ b/java/src/com/android/inputmethod/event/Event.java
@@ -18,6 +18,7 @@ package com.android.inputmethod.event;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
+import com.android.inputmethod.latin.utils.StringUtils;
/**
* Class representing a generic input event as handled by Latin IME.
@@ -52,6 +53,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 +74,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 +102,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 +131,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 +145,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 +159,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 +175,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 +184,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,12 +221,22 @@ 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;
}
+
+ public CharSequence getTextToCommit() {
+ switch (mType) {
+ case EVENT_MODE_KEY:
+ case EVENT_NOT_HANDLED:
+ return "";
+ case EVENT_INPUT_KEYPRESS:
+ case EVENT_TOGGLE:
+ return StringUtils.newSingleCodePointString(mCodePoint);
+ case EVENT_GESTURE:
+ case EVENT_SOFTWARE_GENERATED_STRING:
+ return mText;
+ }
+ throw new RuntimeException("Unknown event type: " + mType);
+ }
}