Export some logic out of LatinIME

Hopefully that will end in Suggest/SuggestedWords being autonomous
and won't need the logic spoon-fed to them

Change-Id: I915661bce13c69c8a5b8e5d4a8c41e18fea594cf
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 4400c7e..e4961d6 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -1823,39 +1823,22 @@
         final SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder(mWordComposer,
                 prevWord, mKeyboardSwitcher.getKeyboard().getProximityInfo(), mCorrectionMode);
 
-        // Here, we want to promote a whitelisted word if exists.
-        // TODO: Change this scheme - a boolean is not enough. A whitelisted word may be "valid"
-        // but still autocorrected from - in the case the whitelist only capitalizes the word.
-        // The whitelist should be case-insensitive, so it's not possible to be consistent with
-        // a boolean flag. Right now this is handled with a slight hack in
-        // WhitelistDictionary#shouldForciblyAutoCorrectFrom.
-        final boolean allowsToBeAutoCorrected = AutoCorrection.allowsToBeAutoCorrected(
-                mSuggest.getUnigramDictionaries(),
-                // If the typed string ends with a single quote, for dictionary lookup purposes
-                // we behave as if the single quote was not here. Here, we are looking up the
-                // typed string in the dictionary (to avoid autocorrecting from an existing
-                // word, so for consistency this lookup should be made WITHOUT the trailing
-                // single quote.
-                quotesCount > 0
-                        ? typedWord.subSequence(0, typedWord.length() - quotesCount) : typedWord,
-                preferCapitalization());
-
         // Basically, we update the suggestion strip only when suggestion count > 1.  However,
         // there is an exception: We update the suggestion strip whenever typed word's length
         // is 1 or typed word is found in dictionary, regardless of suggestion count.  Actually,
         // in most cases, suggestion count is 1 when typed word's length is 1, but we do always
         // need to clear the previous state when the user starts typing a word (i.e. typed word's
         // length == 1).
-        if (builder.size() > 1 || typedWord.length() == 1 || (!allowsToBeAutoCorrected)
+        if (builder.size() > 1 || typedWord.length() == 1 || !builder.allowsToBeAutoCorrected()
                 || mSuggestionsView.isShowingAddToDictionaryHint()) {
             boolean autoCorrectionAvailable = mSuggest.hasAutoCorrection();
             if (mCorrectionMode == Suggest.CORRECTION_FULL
                     || mCorrectionMode == Suggest.CORRECTION_FULL_BIGRAM) {
-                autoCorrectionAvailable |= (!allowsToBeAutoCorrected);
+                autoCorrectionAvailable |= !builder.allowsToBeAutoCorrected();
             }
             // Don't auto-correct words with multiple capital letter
             autoCorrectionAvailable &= !mWordComposer.isMostlyCaps();
-            builder.setTypedWordValid(!allowsToBeAutoCorrected).setHasMinimalSuggestion(
+            builder.setTypedWordValid(!builder.allowsToBeAutoCorrected()).setHasMinimalSuggestion(
                     autoCorrectionAvailable);
             if (Suggest.shouldBlockAutoCorrectionBySafetyNet(builder, mSuggest,
                     mSettingsValues.mAutoCorrectionThreshold)) {
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index b6a7ce7..4773ac5 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -282,6 +282,14 @@
                 Dictionary.UNIGRAM);
         mConsideredWord = consideredWord;
 
+        // TODO: Change this scheme - a boolean is not enough. A whitelisted word may be "valid"
+        // but still autocorrected from - in the case the whitelist only capitalizes the word.
+        // The whitelist should be case-insensitive, so it's not possible to be consistent with
+        // a boolean flag. Right now this is handled with a slight hack in
+        // WhitelistDictionary#shouldForciblyAutoCorrectFrom.
+        final boolean allowsToBeAutoCorrected = AutoCorrection.allowsToBeAutoCorrected(
+                getUnigramDictionaries(), consideredWord, wordComposer.isFirstCharCapitalized());
+
         if (wordComposer.size() <= 1 && (correctionMode == CORRECTION_FULL_BIGRAM)) {
             // At first character typed, search only the bigrams
             Arrays.fill(mBigramScores, 0);
@@ -393,9 +401,11 @@
             for (int i = mScores.length; i < mSuggestions.size(); ++i) {
                 scoreInfoList.add(new SuggestedWords.SuggestedWordInfo("--", false));
             }
-            return new SuggestedWords.Builder().addWords(mSuggestions, scoreInfoList);
+            return new SuggestedWords.Builder().addWords(mSuggestions, scoreInfoList)
+                    .setAllowsToBeAutoCorrected(allowsToBeAutoCorrected);
         }
-        return new SuggestedWords.Builder().addWords(mSuggestions, null);
+        return new SuggestedWords.Builder().addWords(mSuggestions, null)
+                .setAllowsToBeAutoCorrected(allowsToBeAutoCorrected);
     }
 
     public boolean hasAutoCorrection() {
diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java
index aad975e..c92c720 100644
--- a/java/src/com/android/inputmethod/latin/SuggestedWords.java
+++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java
@@ -90,6 +90,7 @@
         private boolean mHasMinimalSuggestion;
         private boolean mIsPunctuationSuggestions;
         private boolean mShouldBlockAutoCorrectionBySafetyNet;
+        private boolean mAllowsToBeAutoCorrected;
         private List<SuggestedWordInfo> mSuggestedWordInfoList =
                 new ArrayList<SuggestedWordInfo>();
 
@@ -159,6 +160,11 @@
             return this;
         }
 
+        public Builder setAllowsToBeAutoCorrected(final boolean allowsToBeAutoCorrected) {
+            mAllowsToBeAutoCorrected = allowsToBeAutoCorrected;
+            return this;
+        }
+
         // Should get rid of the first one (what the user typed previously) from suggestions
         // and replace it with what the user currently typed.
         public Builder addTypedWordAndPreviousSuggestions(CharSequence typedWord,
@@ -200,6 +206,10 @@
             return mTypedWordValid;
         }
 
+        public boolean allowsToBeAutoCorrected() {
+            return mAllowsToBeAutoCorrected;
+        }
+
         @Override
         public String toString() {
             // Pretty-print method to help debug