Conditionally store the raw suggestions.

Bug: 12913520
Change-Id: Ie15f97d90c142c51089280252b35b5ad1d226e19
diff --git a/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java b/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java
index e68c6b7..259c137 100644
--- a/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java
+++ b/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java
@@ -430,12 +430,19 @@
     public void getSuggestions(final WordComposer composer,
             final String prevWord, final ProximityInfo proximityInfo,
             final boolean blockOffensiveWords, final int[] additionalFeaturesOptions,
-            final int sessionId, final Set<SuggestedWordInfo> suggestionSet) {
+            final int sessionId, final Set<SuggestedWordInfo> suggestionSet,
+            final ArrayList<SuggestedWordInfo> rawSuggestions) {
         for (final String key : mDictionaries.keySet()) {
             final Dictionary dictionary = mDictionaries.get(key);
             if (null == dictionary) continue;
-            suggestionSet.addAll(dictionary.getSuggestionsWithSessionId(composer, prevWord,
-                    proximityInfo, blockOffensiveWords, additionalFeaturesOptions, sessionId));
+            final ArrayList<SuggestedWordInfo> dictionarySuggestions =
+                    dictionary.getSuggestionsWithSessionId(composer, prevWord, proximityInfo,
+                            blockOffensiveWords, additionalFeaturesOptions, sessionId);
+            if (null == dictionarySuggestions) continue;
+            suggestionSet.addAll(dictionarySuggestions);
+            if (null != rawSuggestions) {
+                rawSuggestions.addAll(dictionarySuggestions);
+            }
         }
     }
 
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index e55c08d..ef9eb8c 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -997,7 +997,7 @@
                 SuggestedWords.getFromApplicationSpecifiedCompletions(
                         applicationSpecifiedCompletions);
         final SuggestedWords suggestedWords = new SuggestedWords(
-                applicationSuggestedWords,
+                applicationSuggestedWords, null /* rawSuggestions */,
                 false /* typedWordValid */,
                 false /* willAutoCorrect */,
                 false /* isPunctuationSuggestions */,
@@ -1417,7 +1417,7 @@
                     ? SuggestedWords.EMPTY : previousSuggestedWords;
             final ArrayList<SuggestedWords.SuggestedWordInfo> typedWordAndPreviousSuggestions =
                     SuggestedWords.getTypedWordAndPreviousSuggestions(typedWord, oldSuggestedWords);
-            return new SuggestedWords(typedWordAndPreviousSuggestions,
+            return new SuggestedWords(typedWordAndPreviousSuggestions, null /* rawSuggestions */,
                     false /* typedWordValid */,
                     false /* hasAutoCorrectionCandidate */,
                     false /* isPunctuationSuggestions */,
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index bb938a9..5dbee03 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -51,6 +51,7 @@
     private static final int SUPPRESS_SUGGEST_THRESHOLD = -2000000000;
 
     private static final boolean DBG = LatinImeLogger.sDBG;
+    private static final boolean INCLUDE_RAW_SUGGESTIONS = false;
 
     public final DictionaryFacilitatorForSuggest mDictionaryFacilitator;
 
@@ -124,9 +125,15 @@
         } else {
             wordComposerForLookup = wordComposer;
         }
+        final ArrayList<SuggestedWordInfo> rawSuggestions;
+        if (INCLUDE_RAW_SUGGESTIONS) {
+            rawSuggestions = CollectionUtils.newArrayList();
+        } else {
+            rawSuggestions = null;
+        }
         mDictionaryFacilitator.getSuggestions(wordComposerForLookup, prevWordForBigram,
                 proximityInfo, blockOffensiveWords, additionalFeaturesOptions, SESSION_TYPING,
-                suggestionsSet);
+                suggestionsSet, rawSuggestions);
         final String firstSuggestion;
         final String whitelistedWord;
         if (suggestionsSet.isEmpty()) {
@@ -217,7 +224,7 @@
             suggestionsList = suggestionsContainer;
         }
 
-        callback.onGetSuggestedWords(new SuggestedWords(suggestionsList,
+        callback.onGetSuggestedWords(new SuggestedWords(suggestionsList, rawSuggestions,
                 // TODO: this first argument is lying. If this is a whitelisted word which is an
                 // actual word, it says typedWordValid = false, which looks wrong. We should either
                 // rename the attribute or change the value.
@@ -237,8 +244,15 @@
             final OnGetSuggestedWordsCallback callback) {
         final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator,
                 SuggestedWords.MAX_SUGGESTIONS);
+        final ArrayList<SuggestedWordInfo> rawSuggestions;
+        if (INCLUDE_RAW_SUGGESTIONS) {
+            rawSuggestions = CollectionUtils.newArrayList();
+        } else {
+            rawSuggestions = null;
+        }
         mDictionaryFacilitator.getSuggestions(wordComposer, prevWordForBigram, proximityInfo,
-                blockOffensiveWords, additionalFeaturesOptions, sessionId, suggestionsSet);
+                blockOffensiveWords, additionalFeaturesOptions, sessionId, suggestionsSet,
+                rawSuggestions);
         for (SuggestedWordInfo wordInfo : suggestionsSet) {
             LatinImeLogger.onAddSuggestedWord(wordInfo.mWord, wordInfo.mSourceDict.mDictType);
         }
@@ -275,7 +289,7 @@
 
         // In the batch input mode, the most relevant suggested word should act as a "typed word"
         // (typedWordValid=true), not as an "auto correct word" (willAutoCorrect=false).
-        callback.onGetSuggestedWords(new SuggestedWords(suggestionsContainer,
+        callback.onGetSuggestedWords(new SuggestedWords(suggestionsContainer, rawSuggestions,
                 true /* typedWordValid */,
                 false /* willAutoCorrect */,
                 false /* isPunctuationSuggestions */,
diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java
index dbaf822..b2efc4a 100644
--- a/java/src/com/android/inputmethod/latin/SuggestedWords.java
+++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java
@@ -37,7 +37,7 @@
     private static final ArrayList<SuggestedWordInfo> EMPTY_WORD_INFO_LIST =
             CollectionUtils.newArrayList(0);
     public static final SuggestedWords EMPTY = new SuggestedWords(
-            EMPTY_WORD_INFO_LIST, false, false, false, false, false);
+            EMPTY_WORD_INFO_LIST, null /* rawSuggestions */, false, false, false, false, false);
 
     public final String mTypedWord;
     public final boolean mTypedWordValid;
@@ -50,25 +50,29 @@
     public final boolean mIsPrediction;
     public final int mSequenceNumber; // Sequence number for auto-commit.
     private final ArrayList<SuggestedWordInfo> mSuggestedWordInfoList;
+    public final ArrayList<SuggestedWordInfo> mRawSuggestions;
 
     public SuggestedWords(final ArrayList<SuggestedWordInfo> suggestedWordInfoList,
+            final ArrayList<SuggestedWordInfo> rawSuggestions,
             final boolean typedWordValid,
             final boolean willAutoCorrect,
             final boolean isPunctuationSuggestions,
             final boolean isObsoleteSuggestions,
             final boolean isPrediction) {
-        this(suggestedWordInfoList, typedWordValid, willAutoCorrect, isPunctuationSuggestions,
-                isObsoleteSuggestions, isPrediction, NOT_A_SEQUENCE_NUMBER);
+        this(suggestedWordInfoList, rawSuggestions, typedWordValid, willAutoCorrect,
+                isPunctuationSuggestions, isObsoleteSuggestions, isPrediction,
+                NOT_A_SEQUENCE_NUMBER);
     }
 
     public SuggestedWords(final ArrayList<SuggestedWordInfo> suggestedWordInfoList,
+            final ArrayList<SuggestedWordInfo> rawSuggestions,
             final boolean typedWordValid,
             final boolean willAutoCorrect,
             final boolean isPunctuationSuggestions,
             final boolean isObsoleteSuggestions,
             final boolean isPrediction,
             final int sequenceNumber) {
-        this(suggestedWordInfoList,
+        this(suggestedWordInfoList, rawSuggestions,
                 suggestedWordInfoList.isEmpty() ? null
                         : suggestedWordInfoList.get(INDEX_OF_TYPED_WORD).mWord,
                 typedWordValid, willAutoCorrect, isPunctuationSuggestions,
@@ -76,6 +80,7 @@
     }
 
     public SuggestedWords(final ArrayList<SuggestedWordInfo> suggestedWordInfoList,
+            final ArrayList<SuggestedWordInfo> rawSuggestions,
             final String typedWord,
             final boolean typedWordValid,
             final boolean willAutoCorrect,
@@ -84,6 +89,7 @@
             final boolean isPrediction,
             final int sequenceNumber) {
         mSuggestedWordInfoList = suggestedWordInfoList;
+        mRawSuggestions = rawSuggestions;
         mTypedWordValid = typedWordValid;
         mWillAutoCorrect = willAutoCorrect;
         mIsPunctuationSuggestions = isPunctuationSuggestions;
@@ -306,9 +312,9 @@
         }
         // We should never autocorrect, so we say the typed word is valid. Also, in this case,
         // no auto-correction should take place hence willAutoCorrect = false.
-        return new SuggestedWords(newSuggestions, typedWord, true /* typedWordValid */,
-                false /* willAutoCorrect */, mIsPunctuationSuggestions, mIsObsoleteSuggestions,
-                mIsPrediction, NOT_A_SEQUENCE_NUMBER);
+        return new SuggestedWords(newSuggestions, null /* rawSuggestions */, typedWord,
+                true /* typedWordValid */, false /* willAutoCorrect */, mIsPunctuationSuggestions,
+                mIsObsoleteSuggestions, mIsPrediction, NOT_A_SEQUENCE_NUMBER);
     }
 
     // Creates a new SuggestedWordInfo from the currently suggested words that removes all but the
@@ -326,7 +332,7 @@
                     info.mSourceDict, SuggestedWordInfo.NOT_AN_INDEX,
                     SuggestedWordInfo.NOT_A_CONFIDENCE));
         }
-        return new SuggestedWords(newSuggestions, mTypedWordValid,
+        return new SuggestedWords(newSuggestions, null /* rawSuggestions */, mTypedWordValid,
                 mWillAutoCorrect, mIsPunctuationSuggestions, mIsObsoleteSuggestions,
                 mIsPrediction);
     }
diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
index 3ecf5f0..6e90505 100644
--- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
+++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
@@ -1332,7 +1332,8 @@
         } else {
             // We found suggestion spans in the word. We'll create the SuggestedWords out of
             // them, and make willAutoCorrect false.
-            final SuggestedWords suggestedWords = new SuggestedWords(suggestions, typedWord,
+            final SuggestedWords suggestedWords = new SuggestedWords(suggestions,
+                    null /* rawSuggestions */, typedWord,
                     true /* typedWordValid */, false /* willAutoCorrect */,
                     false /* isPunctuationSuggestions */, false /* isObsoleteSuggestions */,
                     false /* isPrediction */, SuggestedWords.NOT_A_SEQUENCE_NUMBER);
diff --git a/java/src/com/android/inputmethod/latin/settings/SpacingAndPunctuations.java b/java/src/com/android/inputmethod/latin/settings/SpacingAndPunctuations.java
index 70cb2b2..60ca5ba 100644
--- a/java/src/com/android/inputmethod/latin/settings/SpacingAndPunctuations.java
+++ b/java/src/com/android/inputmethod/latin/settings/SpacingAndPunctuations.java
@@ -75,7 +75,7 @@
         final ArrayList<SuggestedWordInfo> puncList = CollectionUtils.newArrayList();
         if (puncs != null) {
             for (final String puncSpec : puncs) {
-                // TODO: Stop using KeySpceParser.getLabel().
+                // TODO: Stop using KeySpecParser.getLabel().
                 // TODO: Punctuation suggestions should honor RTL languages.
                 puncList.add(new SuggestedWordInfo(KeySpecParser.getLabel(puncSpec),
                         SuggestedWordInfo.MAX_SCORE, SuggestedWordInfo.KIND_HARDCODED,
@@ -84,7 +84,7 @@
                         SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */));
             }
         }
-        return new SuggestedWords(puncList,
+        return new SuggestedWords(puncList, null /* rawSuggestions */,
                 false /* typedWordValid */,
                 false /* hasAutoCorrectionCandidate */,
                 true /* isPunctuationSuggestions */,
diff --git a/tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java b/tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java
index 3753520..53839b6 100644
--- a/tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java
+++ b/tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java
@@ -46,7 +46,7 @@
         }
 
         final SuggestedWords words = new SuggestedWords(
-                list,
+                list, null /* rawSuggestions */,
                 false /* typedWordValid */,
                 false /* willAutoCorrect */,
                 false /* isPunctuationSuggestions */,