Merge "Fix inconsistent behavior with the back-to-the-main-keyboard key"
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index adebfc0..e8ea5e3 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -193,9 +193,12 @@
                     if (msg.arg2 == ARG2_WITH_TYPED_WORD) {
                         final Pair<SuggestedWords, String> p =
                                 (Pair<SuggestedWords, String>) msg.obj;
-                        latinIme.showSuggestionStripWithTypedWord(p.first, p.second);
+                        // [IL]: this is the only place where the second arg is not
+                        // suggestedWords.mTypedWord.
+                        latinIme.showSuggestionStrip(p.first, p.second);
                     } else {
-                        latinIme.showSuggestionStrip((SuggestedWords) msg.obj);
+                        final SuggestedWords suggestedWords = (SuggestedWords) msg.obj;
+                        latinIme.showSuggestionStrip(suggestedWords, suggestedWords.mTypedWord);
                     }
                 } else {
                     latinIme.showGesturePreviewAndSuggestionStrip((SuggestedWords) msg.obj,
@@ -1270,7 +1273,7 @@
     // This method must run on the UI Thread.
     private void showGesturePreviewAndSuggestionStrip(final SuggestedWords suggestedWords,
             final boolean dismissGestureFloatingPreviewText) {
-        showSuggestionStrip(suggestedWords);
+        showSuggestionStrip(suggestedWords, suggestedWords.mTypedWord);
         final MainKeyboardView mainKeyboardView = mKeyboardSwitcher.getMainKeyboardView();
         mainKeyboardView.showGestureFloatingPreviewText(suggestedWords);
         if (dismissGestureFloatingPreviewText) {
@@ -1324,25 +1327,12 @@
     }
 
     // TODO[IL]: Define a clear interface for this
-    public void setSuggestedWords(final SuggestedWords words, final boolean shouldShow) {
-        mInputLogic.mSuggestedWords = words;
-        final boolean newAutoCorrectionIndicator = words.mWillAutoCorrect;
-        // Put a blue underline to a word in TextView which will be auto-corrected.
-        if (mInputLogic.mIsAutoCorrectionIndicatorOn != newAutoCorrectionIndicator
-                && mInputLogic.mWordComposer.isComposingWord()) {
-            mInputLogic.mIsAutoCorrectionIndicatorOn = newAutoCorrectionIndicator;
-            final CharSequence textWithUnderline =
-                    mInputLogic.getTextWithUnderline(mInputLogic.mWordComposer.getTypedWord());
-            // TODO: when called from an updateSuggestionStrip() call that results from a posted
-            // message, this is called outside any batch edit. Potentially, this may result in some
-            // janky flickering of the screen, although the display speed makes it unlikely in
-            // the practice.
-            mInputLogic.mConnection.setComposingText(textWithUnderline, 1);
-        }
+    public void setSuggestedWords(final SuggestedWords suggestedWords, final boolean shouldShow) {
+        mInputLogic.setSuggestedWords(suggestedWords);
         if (mSuggestionStripView != null) {
-            mSuggestionStripView.setSuggestions(
-                    words, SubtypeLocaleUtils.isRtlLanguage(mSubtypeSwitcher.getCurrentSubtype()));
-            mKeyboardSwitcher.onAutoCorrectionStateChanged(words.mWillAutoCorrect);
+            mSuggestionStripView.setSuggestions(suggestedWords,
+                    SubtypeLocaleUtils.isRtlLanguage(mSubtypeSwitcher.getCurrentSubtype()));
+            mKeyboardSwitcher.onAutoCorrectionStateChanged(suggestedWords.mWillAutoCorrect);
             setSuggestionStripShownInternal(shouldShow, true /* needsInputViewShown */);
         }
     }
@@ -1418,7 +1408,8 @@
         }
     }
 
-    private void showSuggestionStripWithTypedWord(final SuggestedWords sourceSuggestedWords,
+    // TODO[IL]: Define a clean interface for this
+    public void showSuggestionStrip(final SuggestedWords sourceSuggestedWords,
             final String typedWord) {
         final SuggestedWords suggestedWords =
                 sourceSuggestedWords.isEmpty() ? SuggestedWords.EMPTY : sourceSuggestedWords;
@@ -1439,12 +1430,6 @@
         AccessibilityUtils.getInstance().setAutoCorrection(suggestedWords, typedWord);
     }
 
-    // TODO[IL]: Define a clean interface for this
-    public void showSuggestionStrip(final SuggestedWords suggestedWords) {
-        showSuggestionStripWithTypedWord(suggestedWords, suggestedWords.isEmpty() ? null
-                : suggestedWords.getWord(SuggestedWords.INDEX_OF_TYPED_WORD));
-    }
-
     // Called from {@link SuggestionStripView} through the {@link SuggestionStripView#Listener}
     // interface
     @Override
diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java
index bb34b7b..982a97a 100644
--- a/java/src/com/android/inputmethod/latin/SuggestedWords.java
+++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java
@@ -39,6 +39,7 @@
     public static final SuggestedWords EMPTY = new SuggestedWords(
             EMPTY_WORD_INFO_LIST, false, false, false, false, false);
 
+    public final String mTypedWord;
     public final boolean mTypedWordValid;
     // Note: this INCLUDES cases where the word will auto-correct to itself. A good definition
     // of what this flag means would be "the top suggestion is strong enough to auto-correct",
@@ -74,6 +75,7 @@
         mIsObsoleteSuggestions = isObsoleteSuggestions;
         mIsPrediction = isPrediction;
         mSequenceNumber = sequenceNumber;
+        mTypedWord = suggestedWordInfoList.isEmpty() ? null : getWord(INDEX_OF_TYPED_WORD);
     }
 
     public boolean isEmpty() {
diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
index 16e0320..ce3ef53 100644
--- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
+++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
@@ -482,6 +482,25 @@
                 SuggestedWords.EMPTY, true /* dismissGestureFloatingPreviewText */);
     }
 
+    // TODO: on the long term, this method should become private, but it will be difficult.
+    // Especially, how do we deal with InputMethodService.onDisplayCompletions?
+    public void setSuggestedWords(final SuggestedWords suggestedWords) {
+        mSuggestedWords = suggestedWords;
+        final boolean newAutoCorrectionIndicator = suggestedWords.mWillAutoCorrect;
+        // Put a blue underline to a word in TextView which will be auto-corrected.
+        if (mIsAutoCorrectionIndicatorOn != newAutoCorrectionIndicator
+                && mWordComposer.isComposingWord()) {
+            mIsAutoCorrectionIndicatorOn = newAutoCorrectionIndicator;
+            final CharSequence textWithUnderline =
+                    getTextWithUnderline(mWordComposer.getTypedWord());
+            // TODO: when called from an updateSuggestionStrip() call that results from a posted
+            // message, this is called outside any batch edit. Potentially, this may result in some
+            // janky flickering of the screen, although the display speed makes it unlikely in
+            // the practice.
+            mConnection.setComposingText(textWithUnderline, 1);
+        }
+    }
+
     /**
      * Handle inputting a code point to the editor.
      *
@@ -1101,7 +1120,7 @@
         final SuggestedWords suggestedWords = holder.get(null,
                 Constants.GET_SUGGESTED_WORDS_TIMEOUT);
         if (suggestedWords != null) {
-            mLatinIME.showSuggestionStrip(suggestedWords);
+            mLatinIME.showSuggestionStrip(suggestedWords, suggestedWords.mTypedWord);
         }
     }
 
@@ -1603,8 +1622,10 @@
             final int indexOfLastSpace = batchInputText.lastIndexOf(Constants.CODE_SPACE) + 1;
             if (0 != indexOfLastSpace) {
                 mConnection.commitText(batchInputText.substring(0, indexOfLastSpace), 1);
-                mLatinIME.showSuggestionStrip(
-                        suggestedWords.getSuggestedWordsForLastWordOfPhraseGesture());
+                final SuggestedWords suggestedWordsForLastWordOfPhraseGesture =
+                        suggestedWords.getSuggestedWordsForLastWordOfPhraseGesture();
+                mLatinIME.showSuggestionStrip(suggestedWordsForLastWordOfPhraseGesture,
+                        suggestedWordsForLastWordOfPhraseGesture.mTypedWord);
             }
             final String lastWord = batchInputText.substring(indexOfLastSpace);
             mWordComposer.setBatchInputWord(lastWord);