From e83e79cb055fbfe5171fb79a2224e7d9e2cda4d2 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Thu, 19 Jun 2014 15:07:17 +0900 Subject: [PATCH] [CS1] Some initial clean up Bug: 13238601 Change-Id: I54ee9a85ecfee3c50c8db3eb4cd6f391a775a630 --- .../latin/DictionaryFacilitator.java | 14 ++----- .../android/inputmethod/latin/Suggest.java | 41 ++++++++----------- .../SuggestionStripLayoutHelper.java | 5 ++- .../latin/utils/SuggestionResults.java | 8 ++++ 4 files changed, 31 insertions(+), 37 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java b/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java index 8c4870d08..304c450ab 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java +++ b/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java @@ -485,7 +485,7 @@ public class DictionaryFacilitator { public SuggestionResults getSuggestionResults(final WordComposer composer, final PrevWordsInfo prevWordsInfo, final ProximityInfo proximityInfo, final boolean blockOffensiveWords, final int[] additionalFeaturesOptions, - final int sessionId, final ArrayList rawSuggestions) { + final int sessionId) { final Dictionaries dictionaries = mDictionaries; final SuggestionResults suggestionResults = new SuggestionResults(dictionaries.mLocale, SuggestedWords.MAX_SUGGESTIONS); @@ -499,21 +499,13 @@ public class DictionaryFacilitator { languageWeight); if (null == dictionarySuggestions) continue; suggestionResults.addAll(dictionarySuggestions); - if (null != rawSuggestions) { - rawSuggestions.addAll(dictionarySuggestions); + if (null != suggestionResults.mRawSuggestions) { + suggestionResults.mRawSuggestions.addAll(dictionarySuggestions); } } return suggestionResults; } - public boolean isValidMainDictWord(final String word) { - final Dictionary mainDict = mDictionaries.getDict(Dictionary.TYPE_MAIN); - if (TextUtils.isEmpty(word) || mainDict == null) { - return false; - } - return mainDict.isValidWord(word); - } - public boolean isValidWord(final String word, final boolean ignoreCase) { if (TextUtils.isEmpty(word)) { return false; diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index 670d856e3..7364fc979 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -100,30 +100,28 @@ public final class Suggest { ? typedWord.substring(0, typedWord.length() - trailingSingleQuotesCount) : typedWord; - final ArrayList rawSuggestions; - if (ProductionFlag.INCLUDE_RAW_SUGGESTIONS) { - rawSuggestions = new ArrayList<>(); - } else { - rawSuggestions = null; - } final SuggestionResults suggestionResults = mDictionaryFacilitator.getSuggestionResults( wordComposer, prevWordsInfo, proximityInfo, blockOffensiveWords, - additionalFeaturesOptions, SESSION_TYPING, rawSuggestions); + additionalFeaturesOptions, SESSION_TYPING); + final boolean isPrediction = !wordComposer.isComposingWord(); + final boolean shouldMakeSuggestionsAllUpperCase = wordComposer.isAllUpperCase() + && !wordComposer.isResumed(); final boolean isOnlyFirstCharCapitalized = wordComposer.isOrWillBeOnlyFirstCharCapitalized(); + // 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 boolean isAllUpperCase = wordComposer.isAllUpperCase() && !wordComposer.isResumed(); final String firstSuggestion; final String whitelistedWord; if (suggestionResults.isEmpty()) { whitelistedWord = firstSuggestion = null; } else { final SuggestedWordInfo firstSuggestedWordInfo = getTransformedSuggestedWordInfo( - suggestionResults.first(), suggestionResults.mLocale, isAllUpperCase, - isOnlyFirstCharCapitalized, trailingSingleQuotesCount); + suggestionResults.first(), suggestionResults.mLocale, + shouldMakeSuggestionsAllUpperCase, isOnlyFirstCharCapitalized, + trailingSingleQuotesCount); firstSuggestion = firstSuggestedWordInfo.mWord; if (!firstSuggestedWordInfo.isKindOf(SuggestedWordInfo.KIND_WHITELIST)) { whitelistedWord = null; @@ -132,8 +130,6 @@ public final class Suggest { } } - final boolean isPrediction = !wordComposer.isComposingWord(); - // 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 @@ -174,11 +170,12 @@ public final class Suggest { final ArrayList suggestionsContainer = new ArrayList<>(suggestionResults); final int suggestionsCount = suggestionsContainer.size(); - if (isOnlyFirstCharCapitalized || isAllUpperCase || 0 != trailingSingleQuotesCount) { + if (isOnlyFirstCharCapitalized || shouldMakeSuggestionsAllUpperCase + || 0 != trailingSingleQuotesCount) { for (int i = 0; i < suggestionsCount; ++i) { final SuggestedWordInfo wordInfo = suggestionsContainer.get(i); final SuggestedWordInfo transformedWordInfo = getTransformedSuggestedWordInfo( - wordInfo, suggestionResults.mLocale, isAllUpperCase, + wordInfo, suggestionResults.mLocale, shouldMakeSuggestionsAllUpperCase, isOnlyFirstCharCapitalized, trailingSingleQuotesCount); suggestionsContainer.set(i, transformedWordInfo); } @@ -200,12 +197,13 @@ public final class Suggest { suggestionsList = suggestionsContainer; } - callback.onGetSuggestedWords(new SuggestedWords(suggestionsList, rawSuggestions, + callback.onGetSuggestedWords(new SuggestedWords(suggestionsList, + suggestionResults.mRawSuggestions, // 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. !isPrediction && !allowsToBeAutoCorrected /* typedWordValid */, - hasAutoCorrection, /* willAutoCorrect */ + hasAutoCorrection /* willAutoCorrect */, false /* isObsoleteSuggestions */, isPrediction, sequenceNumber)); } @@ -216,15 +214,9 @@ public final class Suggest { final boolean blockOffensiveWords, final int[] additionalFeaturesOptions, final int sessionId, final int sequenceNumber, final OnGetSuggestedWordsCallback callback) { - final ArrayList rawSuggestions; - if (ProductionFlag.INCLUDE_RAW_SUGGESTIONS) { - rawSuggestions = new ArrayList<>(); - } else { - rawSuggestions = null; - } final SuggestionResults suggestionResults = mDictionaryFacilitator.getSuggestionResults( wordComposer, prevWordsInfo, proximityInfo, blockOffensiveWords, - additionalFeaturesOptions, sessionId, rawSuggestions); + additionalFeaturesOptions, sessionId); final ArrayList suggestionsContainer = new ArrayList<>(suggestionResults); final int suggestionsCount = suggestionsContainer.size(); @@ -257,7 +249,8 @@ public final class Suggest { // 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, rawSuggestions, + callback.onGetSuggestedWords(new SuggestedWords(suggestionsContainer, + suggestionResults.mRawSuggestions, true /* typedWordValid */, false /* willAutoCorrect */, false /* isObsoleteSuggestions */, diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripLayoutHelper.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripLayoutHelper.java index 46f5cdee0..ad5aad747 100644 --- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripLayoutHelper.java +++ b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripLayoutHelper.java @@ -49,6 +49,7 @@ import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.PunctuationSuggestions; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.SuggestedWords; +import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.utils.AutoCorrectionUtils; import com.android.inputmethod.latin.utils.ResourceUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; @@ -250,8 +251,8 @@ final class SuggestionStripLayoutHelper { final int positionInStrip = getPositionInSuggestionStrip(indexInSuggestedWords, suggestedWords); // Use identity for strings, not #equals : it's the typed word if it's the same object - final boolean isTypedWord = - suggestedWords.getWord(indexInSuggestedWords) == suggestedWords.mTypedWord; + final boolean isTypedWord = suggestedWords.getInfo(indexInSuggestedWords).isKindOf( + SuggestedWordInfo.KIND_TYPED); final int color; if (positionInStrip == mCenterPositionInStrip && suggestedWords.mWillAutoCorrect) { diff --git a/java/src/com/android/inputmethod/latin/utils/SuggestionResults.java b/java/src/com/android/inputmethod/latin/utils/SuggestionResults.java index 0b362c48a..5c109a68c 100644 --- a/java/src/com/android/inputmethod/latin/utils/SuggestionResults.java +++ b/java/src/com/android/inputmethod/latin/utils/SuggestionResults.java @@ -17,7 +17,9 @@ package com.android.inputmethod.latin.utils; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; +import com.android.inputmethod.latin.define.ProductionFlag; +import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; import java.util.Locale; @@ -29,6 +31,7 @@ import java.util.TreeSet; */ public final class SuggestionResults extends TreeSet { public final Locale mLocale; + public final ArrayList mRawSuggestions; private final int mCapacity; public SuggestionResults(final Locale locale, final int capacity) { @@ -40,6 +43,11 @@ public final class SuggestionResults extends TreeSet { super(comparator); mLocale = locale; mCapacity = capacity; + if (ProductionFlag.INCLUDE_RAW_SUGGESTIONS) { + mRawSuggestions = new ArrayList<>(); + } else { + mRawSuggestions = null; + } } @Override