Fix: too many calls to getSuggestedWordsForTypingInput

This reverts commit d941ea18 and builds on it to fix the behavior
of the broken cases.
It also fixes a small, related bug that probably has existed for
a very long time: predictions not displayed when cancelling
double-space-to-period.

Bug: 15148015
Change-Id: I1f9358f8b6f5804f831643611576be347e83999d
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index 9ec697b..6c2f17c 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -18,7 +18,6 @@
 
 import android.text.TextUtils;
 
-import com.android.inputmethod.event.Event;
 import com.android.inputmethod.keyboard.ProximityInfo;
 import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
 import com.android.inputmethod.latin.define.ProductionFlag;
diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
index 2530f64..71be18c 100644
--- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
+++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
@@ -786,10 +786,11 @@
         final int codePoint = inputTransaction.mEvent.mCodePoint;
         final SettingsValues settingsValues = inputTransaction.mSettingsValues;
         boolean didAutoCorrect = false;
+        final boolean wasComposingWord = mWordComposer.isComposingWord();
         // We avoid sending spaces in languages without spaces if we were composing.
         final boolean shouldAvoidSendingCode = Constants.CODE_SPACE == codePoint
                 && !settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces
-                && mWordComposer.isComposingWord();
+                && wasComposingWord;
         if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) {
             // If we are in the middle of a recorrection, we need to commit the recorrection
             // first so that we can insert the separator at the current cursor position.
@@ -840,13 +841,16 @@
         if (Constants.CODE_SPACE == codePoint) {
             if (maybeDoubleSpacePeriod(inputTransaction)) {
                 inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW);
+                inputTransaction.setRequiresUpdateSuggestions();
                 mSpaceState = SpaceState.DOUBLE;
             } else if (!mSuggestedWords.isPunctuationSuggestions()) {
                 mSpaceState = SpaceState.WEAK;
             }
 
             startDoubleSpacePeriodCountdown(inputTransaction);
-            inputTransaction.setRequiresUpdateSuggestions();
+            if (wasComposingWord) {
+                inputTransaction.setRequiresUpdateSuggestions();
+            }
         } else {
             if (swapWeakSpace) {
                 swapSwapperAndSpace(inputTransaction);
@@ -943,6 +947,11 @@
                 if (mConnection.revertDoubleSpacePeriod()) {
                     // No need to reset mSpaceState, it has already be done (that's why we
                     // receive it as a parameter)
+                    inputTransaction.setRequiresUpdateSuggestions();
+                    mWordComposer.setCapitalizedModeAndPreviousWordAtStartComposingTime(
+                            WordComposer.CAPS_MODE_OFF,
+                            getPrevWordsInfoFromNthPreviousWordForSuggestion(
+                                    inputTransaction.mSettingsValues.mSpacingAndPunctuations, 1));
                     return;
                 }
             } else if (SpaceState.SWAP_PUNCTUATION == inputTransaction.mSpaceState) {
diff --git a/tests/src/com/android/inputmethod/latin/InputLogicTests.java b/tests/src/com/android/inputmethod/latin/InputLogicTests.java
index a944416..460f600 100644
--- a/tests/src/com/android/inputmethod/latin/InputLogicTests.java
+++ b/tests/src/com/android/inputmethod/latin/InputLogicTests.java
@@ -481,6 +481,27 @@
                 suggestedWords.size() > 0 ? suggestedWords.getWord(0) : null);
     }
 
+    public void testPredictionsWithDoubleSpaceToPeriod() {
+        final String WORD_TO_TYPE = "Barack ";
+        type(WORD_TO_TYPE);
+        sleep(DELAY_TO_WAIT_FOR_PREDICTIONS);
+        runMessages();
+        // No need to test here, testPredictionsAfterSpace is testing it already
+        type(" ");
+        sleep(DELAY_TO_WAIT_FOR_PREDICTIONS);
+        runMessages();
+        // Test the predictions have been cleared
+        SuggestedWords suggestedWords = mLatinIME.getSuggestedWordsForTest();
+        assertEquals("predictions cleared after double-space-to-period", suggestedWords.size(), 0);
+        type(Constants.CODE_DELETE);
+        sleep(DELAY_TO_WAIT_FOR_PREDICTIONS);
+        runMessages();
+        // Test the first prediction is displayed
+        suggestedWords = mLatinIME.getSuggestedWordsForTest();
+        assertEquals("predictions after cancel double-space-to-period", "Obama",
+                suggestedWords.size() > 0 ? suggestedWords.getWord(0) : null);
+    }
+
     public void testPredictionsAfterManualPick() {
         final String WORD_TO_TYPE = "Barack";
         type(WORD_TO_TYPE);