Straighten out resuming suggestion on kept word (A5)

This is cleanup.
This also introduces a "deactivated" state to the last committed
word, that can be used for
Bug: 5875776

Change-Id: I1855adb8ac8123f6d2c5365b0ae899145e5c3ba1
diff --git a/java/src/com/android/inputmethod/latin/LastComposedWord.java b/java/src/com/android/inputmethod/latin/LastComposedWord.java
index accc630..767c3a7 100644
--- a/java/src/com/android/inputmethod/latin/LastComposedWord.java
+++ b/java/src/com/android/inputmethod/latin/LastComposedWord.java
@@ -47,6 +47,8 @@
     public final String mTypedWord;
     public final String mAutoCorrection;
 
+    private boolean mActive;
+
     public static final LastComposedWord NOT_A_COMPOSED_WORD =
             new LastComposedWord(COMMIT_TYPE_USER_TYPED_WORD, null, null, null, "", "");
 
@@ -58,10 +60,15 @@
         mYCoordinates = yCoordinates;
         mTypedWord = typedWord;
         mAutoCorrection = autoCorrection;
+        mActive = true;
     }
 
-    public boolean didAutoCorrectToAnotherWord() {
-        return !TextUtils.isEmpty(mAutoCorrection)
+    public void deactivate() {
+        mActive = false;
+    }
+
+    public boolean canCancelAutoCorrect() {
+        return mActive && !TextUtils.isEmpty(mAutoCorrection)
                 && !TextUtils.equals(mTypedWord, mAutoCorrection);
     }
 }
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 62b287e..94da0cf 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -977,8 +977,9 @@
                     .setHasMinimalSuggestion(false);
             // When in fullscreen mode, show completions generated by the application
             setSuggestions(builder.build());
-            mWordComposer.deleteAutoCorrection();
-            mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD;
+            // TODO: is this the right thing to do? What should we auto-correct to in
+            // this case? This says to keep whatever the user typed.
+            mWordComposer.setAutoCorrection(mWordComposer.getTypedWord());
             setSuggestionStripShown(true);
         }
     }
@@ -1393,7 +1394,7 @@
             // resuming here. The behavior needs to be different according to text field types,
             // and it would be much clearer to test for them explicitly here rather than
             // relying on implicit values like "whether the suggestion strip is displayed".
-            if (mLastComposedWord.didAutoCorrectToAnotherWord()) {
+            if (mLastComposedWord.canCancelAutoCorrect()) {
                 Utils.Stats.onAutoCorrectionCancellation();
                 cancelAutoCorrect(ic);
                 return;
@@ -1999,7 +2000,7 @@
         }
         // TODO: figure out here if this is an auto-correct or if the best word is actually
         // what user typed. Note: currently this is done much later in
-        // WordComposer#didAutoCorrectToAnotherWord by string equality of the remembered
+        // LastComposedWord#canCancelAutoCorrect by string equality of the remembered
         // strings.
         mLastComposedWord = mWordComposer.commitWord(commitType);
     }
@@ -2165,12 +2166,14 @@
 
     // "ic" must not be null
     private void cancelAutoCorrect(final InputConnection ic) {
-        mWordComposer.resumeSuggestionOnLastComposedWord(mLastComposedWord);
-        final String originallyTypedWord = mWordComposer.getTypedWord();
-        final CharSequence autoCorrectedTo = mWordComposer.getAutoCorrectionOrNull();
+        final String originallyTypedWord = mLastComposedWord.mTypedWord;
+        final CharSequence autoCorrectedTo = mLastComposedWord.mAutoCorrection;
         final int cancelLength = autoCorrectedTo.length();
         final CharSequence separator = ic.getTextBeforeCursor(1, 0);
         if (DEBUG) {
+            if (mWordComposer.isComposingWord()) {
+                throw new RuntimeException("cancelAutoCorrect, but we are composing a word");
+            }
             final String wordBeforeCursor =
                     ic.getTextBeforeCursor(cancelLength + 1, 0).subSequence(0, cancelLength)
                     .toString();
@@ -2189,9 +2192,7 @@
         ic.commitText(originallyTypedWord, 1);
         // Re-insert the separator
         ic.commitText(separator, 1);
-        mWordComposer.deleteAutoCorrection();
-        mLastComposedWord =
-                mWordComposer.commitWord(LastComposedWord.COMMIT_TYPE_CANCEL_AUTO_CORRECT);
+        mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD;
         Utils.Stats.onSeparator(separator.charAt(0), WordComposer.NOT_A_COORDINATE,
                 WordComposer.NOT_A_COORDINATE);
         mHandler.cancelUpdateBigramPredictions();
diff --git a/java/src/com/android/inputmethod/latin/WordComposer.java b/java/src/com/android/inputmethod/latin/WordComposer.java
index f994d68..bf132ed 100644
--- a/java/src/com/android/inputmethod/latin/WordComposer.java
+++ b/java/src/com/android/inputmethod/latin/WordComposer.java
@@ -310,13 +310,6 @@
     }
 
     /**
-     * Remove any auto-correction that may have been set.
-     */
-    public void deleteAutoCorrection() {
-        mCurrentWord.mAutoCorrection = null;
-    }
-
-    /**
      * @return the auto-correction for this word, or null if none.
      */
     public CharSequence getAutoCorrectionOrNull() {