From b740886aeb47f8f3fb32bca7a7faa13d5756bd74 Mon Sep 17 00:00:00 2001
From: Jean Chalard <jchalard@google.com>
Date: Thu, 19 Jun 2014 16:27:04 +0900
Subject: [PATCH] [CS4] Remove useless tests and processing

Since we have a de-duped and transformed collection, we
can just read from it instead of computing it separately.

Bug: 13238601
Change-Id: I0aa4b0d91d3b350f9449e13658bfb587fc538764
---
 .../android/inputmethod/latin/Suggest.java    | 31 ++++++-------------
 .../inputmethod/latin/SuggestedWords.java     | 16 +++++++---
 2 files changed, 21 insertions(+), 26 deletions(-)

diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index 9d03e8a433..2a56861b41 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -123,21 +123,15 @@ public final class Suggest {
                 suggestionsContainer.set(i, transformedWordInfo);
             }
         }
-        SuggestedWordInfo.removeDups(typedWord, suggestionsContainer);
+        final boolean didRemoveTypedWord =
+                SuggestedWordInfo.removeDups(typedWord, suggestionsContainer);
 
-        // If resumed, then we don't want to upcase everything: resuming on a fully-capitalized
-        // words is rarely done to switch to another fully-capitalized word, but usually to a
-        // normal, non-capitalized suggestion.
-        final String firstSuggestion;
         final String whitelistedWord;
-        if (suggestionResults.isEmpty()) {
+        if (suggestionsContainer.isEmpty()) {
             whitelistedWord = firstSuggestion = null;
         } else {
-            final SuggestedWordInfo firstSuggestedWordInfo = getTransformedSuggestedWordInfo(
-                    suggestionResults.first(), suggestionResults.mLocale,
-                    shouldMakeSuggestionsAllUpperCase, isOnlyFirstCharCapitalized,
-                    trailingSingleQuotesCount);
-            firstSuggestion = firstSuggestedWordInfo.mWord;
+            final SuggestedWordInfo firstSuggestedWordInfo = suggestionsContainer.get(0);
+            final String firstSuggestion = firstSuggestedWordInfo.mWord;
             if (!firstSuggestedWordInfo.isKindOf(SuggestedWordInfo.KIND_WHITELIST)) {
                 whitelistedWord = null;
             } else {
@@ -145,17 +139,10 @@ public final class Suggest {
             }
         }
 
-        // We allow auto-correction if we have a whitelisted word, or if the word is not a valid
-        // word of more than 1 char, except if the first suggestion is the same as the typed string
-        // because in this case if it's strong enough to auto-correct that will mistakenly designate
-        // the second candidate for auto-correction.
-        // TODO: stop relying on indices to find where is the auto-correction in the suggested
-        // words, and correct this test.
-        final boolean allowsToBeAutoCorrected = (null != whitelistedWord
-                && !whitelistedWord.equals(typedWord))
-                || (consideredWord.length() > 1 && !mDictionaryFacilitator.isValidWord(
-                        consideredWord, isOnlyFirstCharCapitalized)
-                        && !typedWord.equals(firstSuggestion));
+        // We allow auto-correction if we have a whitelisted word, or if the word had more than
+        // one char and was not suggested.
+        final boolean allowsToBeAutoCorrected = (null != whitelistedWord)
+                || (consideredWord.length() > 1 && !didRemoveTypedWord);
 
         final boolean hasAutoCorrection;
         // TODO: using isCorrectionEnabled here is not very good. It's probably useless, because
diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java
index f22af79911..e587b18c9a 100644
--- a/java/src/com/android/inputmethod/latin/SuggestedWords.java
+++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java
@@ -330,29 +330,37 @@ public class SuggestedWords {
         }
 
         // This will always remove the higher index if a duplicate is found.
-        public static void removeDups(final String typedWord,
+        public static boolean removeDups(final String typedWord,
                 ArrayList<SuggestedWordInfo> candidates) {
             if (candidates.isEmpty()) {
-                return;
+                return false;
             }
+            final boolean didRemoveTypedWord;
             if (!TextUtils.isEmpty(typedWord)) {
-                removeSuggestedWordInfoFrom(typedWord, candidates, -1 /* startIndexExclusive */);
+                didRemoveTypedWord = removeSuggestedWordInfoFrom(typedWord, candidates,
+                        -1 /* startIndexExclusive */);
+            } else {
+                didRemoveTypedWord = false;
             }
             for (int i = 0; i < candidates.size(); ++i) {
                 removeSuggestedWordInfoFrom(candidates.get(i).mWord, candidates,
                         i /* startIndexExclusive */);
             }
+            return didRemoveTypedWord;
         }
 
-        private static void removeSuggestedWordInfoFrom(final String word,
+        private static boolean removeSuggestedWordInfoFrom(final String word,
                 final ArrayList<SuggestedWordInfo> candidates, final int startIndexExclusive) {
+            boolean didRemove = false;
             for (int i = startIndexExclusive + 1; i < candidates.size(); ++i) {
                 final SuggestedWordInfo previous = candidates.get(i);
                 if (word.equals(previous.mWord)) {
+                    didRemove = true;
                     candidates.remove(i);
                     --i;
                 }
             }
+            return didRemove;
         }
     }
 
-- 
GitLab