Merge "Fix KeyboardState to pass KeyboardStateMultiTouchTests"
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index d11aaeb..d36140d 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -1924,8 +1924,7 @@
             // Updating the predictions right away may be slow and feel unresponsive on slower
             // terminals. On the other hand if we just postUpdateBigramPredictions() it will
             // take a noticeable delay to update them which may feel uneasy.
-        }
-        if (showingAddToDictionaryHint) {
+        } else {
             if (mIsUserDictionaryAvailable) {
                 mSuggestionsView.showAddToDictionaryHint(
                         suggestion, mSettingsValues.mHintToSaveText);
@@ -1942,9 +1941,6 @@
      * Commits the chosen word to the text field and saves it for later retrieval.
      */
     private void commitChosenWord(final CharSequence bestWord, final int commitType) {
-        final KeyboardSwitcher switcher = mKeyboardSwitcher;
-        if (!switcher.isKeyboardAvailable())
-            return;
         final InputConnection ic = getCurrentInputConnection();
         if (ic != null) {
             mVoiceProxy.rememberReplacedWord(bestWord, mSettingsValues.mWordSeparators);
@@ -2158,6 +2154,16 @@
 
     // "ic" must not be null
     private void restartSuggestionsOnManuallyPickedTypedWord(final InputConnection ic) {
+        // Note: this relies on the last word still being held in the WordComposer, in
+        // the field for suggestion resuming.
+        // 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.
+        mWordComposer.resumeSuggestionOnKeptWord();
+        // We resume suggestion, and then we want to set the composing text to the content
+        // of the word composer again. But since we just manually picked a word, there is
+        // no composing text at the moment, so we have to delete the word before we set a
+        // new composing text.
         final int restartLength = mWordComposer.size();
         if (DEBUG) {
             final String wordBeforeCursor =
@@ -2171,13 +2177,8 @@
                         + wordBeforeCursor + "\"");
             }
         }
+        // Warning: this +1 takes into account the extra space added by the manual pick process.
         ic.deleteSurroundingText(restartLength + 1, 0);
-
-        // 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.
-        mWordComposer.resumeSuggestionOnKeptWord();
         ic.setComposingText(mWordComposer.getTypedWord(), 1);
         mHandler.cancelUpdateBigramPredictions();
         mHandler.postUpdateSuggestions();