Merge "Move KeyDetector.printableCode to Keyboard"
diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java
index 7cbf665..e267aa6 100644
--- a/java/src/com/android/inputmethod/keyboard/Keyboard.java
+++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java
@@ -74,7 +74,6 @@
     public static final int CODE_SHIFT = -1;
     public static final int CODE_SWITCH_ALPHA_SYMBOL = -2;
     public static final int CODE_CAPSLOCK = -3;
-    public static final int CODE_CANCEL = -4;
     public static final int CODE_DELETE = -5;
     public static final int CODE_SETTINGS = -6;
     public static final int CODE_SHORTCUT = -7;
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 5d075b1..610a383 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -203,7 +203,6 @@
     private boolean mIsSettingsSuggestionStripOn;
     private boolean mApplicationSpecifiedCompletionOn;
 
-    private final StringBuilder mComposingStringBuilder = new StringBuilder();
     private WordComposer mWordComposer = new WordComposer();
     private CharSequence mBestWord;
     private boolean mHasUncommittedTypedChars;
@@ -756,7 +755,7 @@
 
         inputView.closing();
         mEnteredText = null;
-        mComposingStringBuilder.setLength(0);
+        mWordComposer.reset();
         mHasUncommittedTypedChars = false;
         mDeleteCount = 0;
         mSpaceState = SPACE_STATE_NONE;
@@ -928,10 +927,10 @@
                 // newly inserted punctuation.
                 mSpaceState = SPACE_STATE_NONE;
             }
-            if (((mComposingStringBuilder.length() > 0 && mHasUncommittedTypedChars)
+            if (((mWordComposer.size() > 0 && mHasUncommittedTypedChars)
                     || mVoiceProxy.isVoiceInputHighlighted())
                     && (selectionChanged || candidatesCleared)) {
-                mComposingStringBuilder.setLength(0);
+                mWordComposer.reset();
                 mHasUncommittedTypedChars = false;
                 TextEntryState.reset();
                 updateSuggestions();
@@ -1146,13 +1145,14 @@
     public void commitTyped(final InputConnection ic) {
         if (!mHasUncommittedTypedChars) return;
         mHasUncommittedTypedChars = false;
-        if (mComposingStringBuilder.length() > 0) {
+        final CharSequence typedWord = mWordComposer.getTypedWord();
+        if (typedWord.length() > 0) {
             if (ic != null) {
-                ic.commitText(mComposingStringBuilder, 1);
+                ic.commitText(typedWord, 1);
             }
-            mCommittedLength = mComposingStringBuilder.length();
-            TextEntryState.acceptedTyped(mComposingStringBuilder);
-            addToUserUnigramAndBigramDictionaries(mComposingStringBuilder,
+            mCommittedLength = typedWord.length();
+            TextEntryState.acceptedTyped(typedWord);
+            addToUserUnigramAndBigramDictionaries(typedWord,
                     UserUnigramDictionary.FREQUENCY_FOR_TYPED);
         }
         updateSuggestions();
@@ -1334,11 +1334,6 @@
                 switcher.toggleAlphabetAndSymbols();
             }
             break;
-        case Keyboard.CODE_CANCEL:
-            if (!isShowingOptionDialog()) {
-                handleClose();
-            }
-            break;
         case Keyboard.CODE_SETTINGS:
             onSettingsKeyPressed();
             break;
@@ -1408,17 +1403,16 @@
 
         final boolean deleteChar = !mHasUncommittedTypedChars;
         if (mHasUncommittedTypedChars) {
-            final int length = mComposingStringBuilder.length();
+            final int length = mWordComposer.size();
             if (length > 0) {
-                mComposingStringBuilder.delete(length - 1, length);
                 mWordComposer.deleteLast();
                 final CharSequence textWithUnderline =
                         mComposingStateManager.isAutoCorrectionIndicatorOn()
                                 ? SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline(
-                                            this, mComposingStringBuilder)
-                                : mComposingStringBuilder;
+                                            this, mWordComposer.getTypedWord())
+                                : mWordComposer.getTypedWord();
                 ic.setComposingText(textWithUnderline, 1);
-                if (mComposingStringBuilder.length() == 0) {
+                if (mWordComposer.size() == 0) {
                     mHasUncommittedTypedChars = false;
                 }
                 if (1 == length) {
@@ -1521,7 +1515,6 @@
                 // Reset entirely the composing state anyway, then start composing a new word unless
                 // the character is a single quote.
                 mHasUncommittedTypedChars = (Keyboard.CODE_SINGLE_QUOTE != code);
-                mComposingStringBuilder.setLength(0);
                 mWordComposer.reset();
                 clearSuggestions();
                 mComposingStateManager.onFinishComposingText();
@@ -1551,7 +1544,6 @@
             }
         }
         if (mHasUncommittedTypedChars) {
-            mComposingStringBuilder.append((char) code);
             mWordComposer.add(code, keyCodes, x, y);
             if (ic != null) {
                 // If it's the first letter, make note of auto-caps state
@@ -1562,8 +1554,8 @@
                 final CharSequence textWithUnderline =
                         mComposingStateManager.isAutoCorrectionIndicatorOn()
                                 ? SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline(
-                                        this, mComposingStringBuilder)
-                                : mComposingStringBuilder;
+                                        this, mWordComposer.getTypedWord())
+                                : mWordComposer.getTypedWord();
                 ic.setComposingText(textWithUnderline, 1);
             }
             mHandler.postUpdateSuggestions();
@@ -1751,8 +1743,8 @@
                 }
                 final CharSequence textWithUnderline = newAutoCorrectionIndicator
                         ? SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline(
-                                this, mComposingStringBuilder)
-                        : mComposingStringBuilder;
+                                this, mWordComposer.getTypedWord())
+                        : mWordComposer.getTypedWord();
                 if (!TextUtils.isEmpty(textWithUnderline)) {
                     ic.setComposingText(textWithUnderline, 1);
                 }
@@ -1945,9 +1937,9 @@
         } else {
             addToOnlyBigramDictionary(suggestion, 1);
         }
-        LatinImeLogger.logOnManualSuggestion(mComposingStringBuilder.toString(),
+        LatinImeLogger.logOnManualSuggestion(mWordComposer.getTypedWord().toString(),
                 suggestion.toString(), index, suggestions.mWords);
-        TextEntryState.acceptedSuggestion(mComposingStringBuilder.toString(), suggestion);
+        TextEntryState.acceptedSuggestion(mWordComposer.getTypedWord().toString(), suggestion);
         // Follow it with a space
         if (mInsertSpaceOnPickSuggestionManually) {
             sendMagicSpace();
@@ -2157,8 +2149,6 @@
     private void restartSuggestionsOnWordBeforeCursor(final InputConnection ic,
             final CharSequence word) {
         mWordComposer.setComposingWord(word, mKeyboardSwitcher.getLatinKeyboard());
-        mComposingStringBuilder.setLength(0);
-        mComposingStringBuilder.append(word);
         // mBestWord will be set appropriately by updateSuggestions() called by the handler
         mBestWord = null;
         mHasUncommittedTypedChars = true;
@@ -2171,7 +2161,7 @@
 
     // "ic" must not be null
     private void revertLastWord(final InputConnection ic) {
-        if (mHasUncommittedTypedChars || mComposingStringBuilder.length() <= 0) {
+        if (mHasUncommittedTypedChars || mWordComposer.size() <= 0) {
             sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
             return;
         }
@@ -2186,21 +2176,19 @@
         // the cursor.
         if (!TextUtils.isEmpty(separator)
                 && mSettingsValues.isWordSeparator(separator.charAt(0))
-                && !TextUtils.equals(mComposingStringBuilder, textToTheLeft)) {
-            ic.commitText(mComposingStringBuilder, 1);
-            TextEntryState.acceptedTyped(mComposingStringBuilder);
+                && !TextUtils.equals(mWordComposer.getTypedWord(), textToTheLeft)) {
+            ic.commitText(mWordComposer.getTypedWord(), 1);
+            TextEntryState.acceptedTyped(mWordComposer.getTypedWord());
             ic.commitText(separator, 1);
             TextEntryState.typedCharacter(separator.charAt(0), true,
                     WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE);
-            // Clear composing text
-            mComposingStringBuilder.setLength(0);
         } else {
             // Note: this relies on the last word still being held in the WordComposer
             // Note: in the interest of code simplicity, we may want to just call
             // restartSuggestionsOnWordBeforeCursorIfAtEndOfWord instead, but retrieving
             // the old WordComposer allows to reuse the actual typed coordinates.
             mHasUncommittedTypedChars = true;
-            ic.setComposingText(mComposingStringBuilder, 1);
+            ic.setComposingText(mWordComposer.getTypedWord(), 1);
             TextEntryState.backspace();
         }
         mHandler.cancelUpdateBigramPredictions();
@@ -2479,7 +2467,6 @@
         final Keyboard keyboard = mKeyboardSwitcher.getLatinKeyboard();
         final int keyboardMode = keyboard != null ? keyboard.mId.mMode : -1;
         p.println("  Keyboard mode = " + keyboardMode);
-        p.println("  mComposingStringBuilder=" + mComposingStringBuilder.toString());
         p.println("  mIsSuggestionsRequested=" + mIsSettingsSuggestionStripOn);
         p.println("  mCorrectionMode=" + mCorrectionMode);
         p.println("  mHasUncommittedTypedChars=" + mHasUncommittedTypedChars);
diff --git a/java/src/com/android/inputmethod/latin/TextEntryState.java b/java/src/com/android/inputmethod/latin/TextEntryState.java
index a6041b3..6b44fc5 100644
--- a/java/src/com/android/inputmethod/latin/TextEntryState.java
+++ b/java/src/com/android/inputmethod/latin/TextEntryState.java
@@ -19,6 +19,7 @@
 import com.android.inputmethod.keyboard.Keyboard;
 import com.android.inputmethod.latin.Utils.RingCharBuffer;
 
+import android.text.TextUtils;
 import android.util.Log;
 
 public class TextEntryState {
@@ -45,7 +46,7 @@
 
     public static void acceptedDefault(CharSequence typedWord, CharSequence actualWord,
             int separatorCode) {
-        if (typedWord == null) return;
+        if (TextUtils.isEmpty(typedWord)) return;
         setState(ACCEPTED_DEFAULT);
         LatinImeLogger.logOnAutoCorrection(
                 typedWord.toString(), actualWord.toString(), separatorCode);
@@ -57,7 +58,7 @@
     // (see "case ACCEPTED_DEFAULT" in typedCharacter() below),
     // and should be restored back to State.ACCEPTED_DEFAULT after processing for each sub-state.
     public static void backToAcceptedDefault(CharSequence typedWord) {
-        if (typedWord == null) return;
+        if (TextUtils.isEmpty(typedWord)) return;
         switch (sState) {
         case SPACE_AFTER_ACCEPTED:
         case PUNCTUATION_AFTER_ACCEPTED:
diff --git a/java/src/com/android/inputmethod/latin/WordComposer.java b/java/src/com/android/inputmethod/latin/WordComposer.java
index 44c89f7..dfb00c8 100644
--- a/java/src/com/android/inputmethod/latin/WordComposer.java
+++ b/java/src/com/android/inputmethod/latin/WordComposer.java
@@ -231,9 +231,6 @@
      * @return the word that was typed so far
      */
     public String getTypedWord() {
-        if (size() == 0) {
-            return null;
-        }
         return mTypedWord.toString();
     }