aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin')
-rw-r--r--java/src/com/android/inputmethod/latin/LatinIME.java27
-rw-r--r--java/src/com/android/inputmethod/latin/SettingsValues.java16
-rw-r--r--java/src/com/android/inputmethod/latin/StringBuilderPool.java70
-rw-r--r--java/src/com/android/inputmethod/latin/StringUtils.java10
-rw-r--r--java/src/com/android/inputmethod/latin/Suggest.java117
-rw-r--r--java/src/com/android/inputmethod/latin/SuggestedWords.java114
6 files changed, 104 insertions, 250 deletions
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index dc5bd2efc..def639dce 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -72,6 +72,7 @@ import com.android.inputmethod.latin.suggestions.SuggestionsView;
import java.io.FileDescriptor;
import java.io.PrintWriter;
+import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@@ -925,10 +926,11 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
final List<SuggestedWords.SuggestedWordInfo> applicationSuggestedWords =
SuggestedWords.Builder.getFromApplicationSpecifiedCompletions(
applicationSpecifiedCompletions);
- SuggestedWords.Builder builder = new SuggestedWords.Builder()
- .addWords(applicationSuggestedWords)
- .setTypedWordValid(false)
- .setHasMinimalSuggestion(false);
+ SuggestedWords.Builder builder = new SuggestedWords.Builder(applicationSuggestedWords,
+ false /* typedWordValid */,
+ false /* hasMinimalSuggestion */,
+ false /* allowsToBeAutoCorrected */,
+ false /* isPunctuationSuggestions */);
// When in fullscreen mode, show completions generated by the application
final SuggestedWords words = builder.build();
final boolean isAutoCorrection = false;
@@ -1767,6 +1769,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
// getSuggestedWordBuilder handles gracefully a null value of prevWord
final SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder(mWordComposer,
prevWord, mKeyboardSwitcher.getKeyboard().getProximityInfo(), mCorrectionMode);
+ final SuggestedWords suggestions = builder.build();
// 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
@@ -1774,16 +1777,24 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
// 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 || !builder.allowsToBeAutoCorrected()
+ if (suggestions.size() > 1 || typedWord.length() == 1
+ || !suggestions.mAllowsToBeAutoCorrected
|| mSuggestionsView.isShowingAddToDictionaryHint()) {
- showSuggestions(builder.build(), typedWord);
+ showSuggestions(suggestions, typedWord);
} else {
SuggestedWords previousSuggestions = mSuggestionsView.getSuggestions();
if (previousSuggestions == mSettingsValues.mSuggestPuncList) {
previousSuggestions = SuggestedWords.EMPTY;
}
- final SuggestedWords.Builder obsoleteSuggestionsBuilder = new SuggestedWords.Builder()
- .addTypedWordAndPreviousSuggestions(typedWord, previousSuggestions);
+ final ArrayList<SuggestedWords.SuggestedWordInfo> typedWordAndPreviousSuggestions =
+ SuggestedWords.Builder.getTypedWordAndPreviousSuggestions(
+ typedWord, previousSuggestions);
+ final SuggestedWords.Builder obsoleteSuggestionsBuilder =
+ new SuggestedWords.Builder(typedWordAndPreviousSuggestions,
+ false /* typedWordValid */,
+ false /* hasMinimalSuggestion */,
+ false /* allowsToBeAutoCorrected */,
+ false /* isPunctuationSuggestions */);
showSuggestions(obsoleteSuggestionsBuilder.build(), typedWord);
}
}
diff --git a/java/src/com/android/inputmethod/latin/SettingsValues.java b/java/src/com/android/inputmethod/latin/SettingsValues.java
index 7e7702002..0a4aea140 100644
--- a/java/src/com/android/inputmethod/latin/SettingsValues.java
+++ b/java/src/com/android/inputmethod/latin/SettingsValues.java
@@ -183,9 +183,11 @@ public class SettingsValues {
KeySpecParser.getLabel(puncSpec)));
}
}
- final SuggestedWords.Builder builder = new SuggestedWords.Builder()
- .addWords(puncList)
- .setIsPunctuationSuggestions();
+ final SuggestedWords.Builder builder = new SuggestedWords.Builder(puncList,
+ false /* typedWordValid */,
+ false /* hasMinimalSuggestion */,
+ false /* allowsToBeAutoCorrected */,
+ true /* isPunctuationSuggestions */);
return builder.build();
}
@@ -203,9 +205,11 @@ public class SettingsValues {
}
}
}
- final SuggestedWords.Builder builder = new SuggestedWords.Builder()
- .addWords(puncOutputTextList)
- .setIsPunctuationSuggestions();
+ final SuggestedWords.Builder builder = new SuggestedWords.Builder(puncOutputTextList,
+ false /* typedWordValid */,
+ false /* hasMinimalSuggestion */,
+ false /* allowsToBeAutoCorrected */,
+ true /* isPunctuationSuggestions */);
return builder.build();
}
diff --git a/java/src/com/android/inputmethod/latin/StringBuilderPool.java b/java/src/com/android/inputmethod/latin/StringBuilderPool.java
deleted file mode 100644
index a663ed43e..000000000
--- a/java/src/com/android/inputmethod/latin/StringBuilderPool.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
-package com.android.inputmethod.latin;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-/**
- * A pool of string builders to be used from anywhere.
- */
-public class StringBuilderPool {
- // Singleton
- private static final StringBuilderPool sInstance = new StringBuilderPool();
- private static final boolean DEBUG = false;
- private StringBuilderPool() {}
- // TODO: Make this a normal array with a size of 20, or a ConcurrentQueue
- private final List<StringBuilder> mPool =
- Collections.synchronizedList(new ArrayList<StringBuilder>());
-
- public static StringBuilder getStringBuilder(final int initialSize) {
- // TODO: although the pool is synchronized, the following is not thread-safe.
- // Two threads entering this at the same time could take the same size of the pool and the
- // second to attempt removing this index from the pool would crash with an
- // IndexOutOfBoundsException.
- // At the moment this pool is only used in Suggest.java and only in one thread so it's
- // okay. The simplest thing to do here is probably to replace the ArrayList with a
- // ConcurrentQueue.
- final int poolSize = sInstance.mPool.size();
- final StringBuilder sb = poolSize > 0 ? (StringBuilder) sInstance.mPool.remove(poolSize - 1)
- : new StringBuilder(initialSize);
- sb.setLength(0);
- return sb;
- }
-
- public static void recycle(final StringBuilder garbage) {
- if (DEBUG) {
- final int gid = garbage.hashCode();
- for (final StringBuilder q : sInstance.mPool) {
- if (gid == q.hashCode()) throw new RuntimeException("Duplicate id " + gid);
- }
- }
- sInstance.mPool.add(garbage);
- }
-
- public static void ensureCapacity(final int capacity, final int initialSize) {
- for (int i = sInstance.mPool.size(); i < capacity; ++i) {
- final StringBuilder sb = new StringBuilder(initialSize);
- sInstance.mPool.add(sb);
- }
- }
-
- public static int getSize() {
- return sInstance.mPool.size();
- }
-}
diff --git a/java/src/com/android/inputmethod/latin/StringUtils.java b/java/src/com/android/inputmethod/latin/StringUtils.java
index 81c3b4edf..7b34cae63 100644
--- a/java/src/com/android/inputmethod/latin/StringUtils.java
+++ b/java/src/com/android/inputmethod/latin/StringUtils.java
@@ -142,7 +142,7 @@ public class StringUtils {
for (int j = 0; j < i; j++) {
CharSequence previous = suggestions.get(j);
if (TextUtils.equals(cur, previous)) {
- removeFromSuggestions(suggestions, i);
+ suggestions.remove(i);
i--;
break;
}
@@ -151,14 +151,6 @@ public class StringUtils {
}
}
- private static void removeFromSuggestions(final ArrayList<CharSequence> suggestions,
- final int index) {
- final CharSequence garbage = suggestions.remove(index);
- if (garbage instanceof StringBuilder) {
- StringBuilderPool.recycle((StringBuilder)garbage);
- }
- }
-
public static String getFullDisplayName(Locale locale, boolean returnsNameInThisLocale) {
if (returnsNameInThisLocale) {
return toTitleCase(SubtypeLocale.getFullDisplayName(locale), locale);
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index e04a4e8d1..4dee4f3b4 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -98,7 +98,7 @@ public class Suggest implements Dictionary.WordCallback {
private int[] mBigramScores = new int[PREF_MAX_BIGRAMS];
private ArrayList<CharSequence> mSuggestions = new ArrayList<CharSequence>();
- ArrayList<CharSequence> mBigramSuggestions = new ArrayList<CharSequence>();
+ private ArrayList<CharSequence> mBigramSuggestions = new ArrayList<CharSequence>();
private CharSequence mConsideredWord;
// TODO: Remove these member variables by passing more context to addWord() callback method
@@ -122,7 +122,6 @@ public class Suggest implements Dictionary.WordCallback {
private void initWhitelistAndAutocorrectAndPool(final Context context, final Locale locale) {
mWhiteListDictionary = new WhitelistDictionary(context, locale);
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_WHITELIST, mWhiteListDictionary);
- StringBuilderPool.ensureCapacity(mPrefMaxSuggestions, getApproxMaxWordLength());
}
private void initAsynchronously(final Context context, final int dictionaryResId,
@@ -181,7 +180,7 @@ public class Suggest implements Dictionary.WordCallback {
return mUnigramDictionaries;
}
- public int getApproxMaxWordLength() {
+ public static int getApproxMaxWordLength() {
return APPROX_MAX_WORD_LENGTH;
}
@@ -216,27 +215,11 @@ public class Suggest implements Dictionary.WordCallback {
mAutoCorrectionThreshold = threshold;
}
- /**
- * Number of suggestions to generate from the input key sequence. This has
- * to be a number between 1 and 100 (inclusive).
- * @param maxSuggestions
- * @throws IllegalArgumentException if the number is out of range
- */
- public void setMaxSuggestions(int maxSuggestions) {
- if (maxSuggestions < 1 || maxSuggestions > 100) {
- throw new IllegalArgumentException("maxSuggestions must be between 1 and 100");
- }
- mPrefMaxSuggestions = maxSuggestions;
- mScores = new int[mPrefMaxSuggestions];
- mBigramScores = new int[PREF_MAX_BIGRAMS];
- collectGarbage(mSuggestions, mPrefMaxSuggestions);
- StringBuilderPool.ensureCapacity(mPrefMaxSuggestions, getApproxMaxWordLength());
- }
-
- private CharSequence capitalizeWord(boolean all, boolean first, CharSequence word) {
+ private static CharSequence capitalizeWord(final boolean all, final boolean first,
+ final CharSequence word) {
if (TextUtils.isEmpty(word) || !(all || first)) return word;
final int wordLength = word.length();
- final StringBuilder sb = StringBuilderPool.getStringBuilder(getApproxMaxWordLength());
+ final StringBuilder sb = new StringBuilder(getApproxMaxWordLength());
// TODO: Must pay attention to locale when changing case.
if (all) {
sb.append(word.toString().toUpperCase());
@@ -250,12 +233,7 @@ public class Suggest implements Dictionary.WordCallback {
}
protected void addBigramToSuggestions(CharSequence bigram) {
- // TODO: Try to be a little more shrewd with resource allocation.
- // At the moment we copy this object because the StringBuilders are pooled (see
- // StringBuilderPool.java) and when we are finished using mSuggestions and
- // mBigramSuggestions we will take everything from both and insert them back in the
- // pool, so we can't allow the same object to be in both lists at the same time.
- final StringBuilder sb = StringBuilderPool.getStringBuilder(getApproxMaxWordLength());
+ final StringBuilder sb = new StringBuilder(getApproxMaxWordLength());
sb.append(bigram);
mSuggestions.add(sb);
}
@@ -266,7 +244,7 @@ public class Suggest implements Dictionary.WordCallback {
mIsFirstCharCapitalized = false;
mIsAllUpperCase = false;
mTrailingSingleQuotesCount = 0;
- collectGarbage(mSuggestions, mPrefMaxSuggestions);
+ mSuggestions = new ArrayList<CharSequence>(mPrefMaxSuggestions);
Arrays.fill(mScores, 0);
// Treating USER_TYPED as UNIGRAM suggestion for logging now.
@@ -274,7 +252,7 @@ public class Suggest implements Dictionary.WordCallback {
mConsideredWord = "";
Arrays.fill(mBigramScores, 0);
- collectGarbage(mBigramSuggestions, PREF_MAX_BIGRAMS);
+ mBigramSuggestions = new ArrayList<CharSequence>(PREF_MAX_BIGRAMS);
CharSequence lowerPrevWord = prevWordForBigram.toString().toLowerCase();
if (mMainDict != null && mMainDict.isValidWord(lowerPrevWord)) {
@@ -291,10 +269,12 @@ public class Suggest implements Dictionary.WordCallback {
StringUtils.removeDupes(mSuggestions);
- return new SuggestedWords.Builder()
- .addWords(SuggestedWords.Builder.getFromCharSequenceList(mSuggestions))
- .setAllowsToBeAutoCorrected(false)
- .setHasAutoCorrection(false);
+ return new SuggestedWords.Builder(
+ SuggestedWords.Builder.getFromCharSequenceList(mSuggestions),
+ false /* typedWordValid */,
+ false /* hasMinimalSuggestion */,
+ false /* allowsToBeAutoCorrected */,
+ false /* isPunctuationSuggestions */);
}
// TODO: cleanup dictionaries looking up and suggestions building with SuggestedWords.Builder
@@ -305,7 +285,7 @@ public class Suggest implements Dictionary.WordCallback {
mIsFirstCharCapitalized = wordComposer.isFirstCharCapitalized();
mIsAllUpperCase = wordComposer.isAllUpperCase();
mTrailingSingleQuotesCount = wordComposer.trailingSingleQuotesCount();
- collectGarbage(mSuggestions, mPrefMaxSuggestions);
+ mSuggestions = new ArrayList<CharSequence>(mPrefMaxSuggestions);
Arrays.fill(mScores, 0);
final String typedWord = wordComposer.getTypedWord();
@@ -328,7 +308,7 @@ public class Suggest implements Dictionary.WordCallback {
if (wordComposer.size() <= 1 && (correctionMode == CORRECTION_FULL_BIGRAM)) {
// At first character typed, search only the bigrams
Arrays.fill(mBigramScores, 0);
- collectGarbage(mBigramSuggestions, PREF_MAX_BIGRAMS);
+ mBigramSuggestions = new ArrayList<CharSequence>(PREF_MAX_BIGRAMS);
if (!TextUtils.isEmpty(prevWordForBigram)) {
CharSequence lowerPrevWord = prevWordForBigram.toString().toLowerCase();
@@ -413,6 +393,7 @@ public class Suggest implements Dictionary.WordCallback {
StringUtils.removeDupes(mSuggestions);
final SuggestedWords.Builder builder;
+ final ArrayList<SuggestedWords.SuggestedWordInfo> scoreInfoList;
if (DBG) {
// TODO: this doesn't take into account the fact that removing dupes from mSuggestions
// may have made mScores[] and mSuggestions out of sync.
@@ -421,8 +402,7 @@ public class Suggest implements Dictionary.WordCallback {
double normalizedScore = BinaryDictionary.calcNormalizedScore(
typedWord, autoCorrectionSuggestion.toString(),
autoCorrectionSuggestionScore);
- ArrayList<SuggestedWords.SuggestedWordInfo> scoreInfoList =
- new ArrayList<SuggestedWords.SuggestedWordInfo>();
+ scoreInfoList = new ArrayList<SuggestedWords.SuggestedWordInfo>();
scoreInfoList.add(new SuggestedWords.SuggestedWordInfo(autoCorrectionSuggestion, "+",
false));
final int suggestionsSize = mSuggestions.size();
@@ -446,14 +426,8 @@ public class Suggest implements Dictionary.WordCallback {
scoreInfoList.add(new SuggestedWords.SuggestedWordInfo(mSuggestions.get(i),
"--", false));
}
- builder = new SuggestedWords.Builder().addWords(scoreInfoList)
- .setAllowsToBeAutoCorrected(allowsToBeAutoCorrected)
- .setHasAutoCorrection(hasAutoCorrection);
} else {
- builder = new SuggestedWords.Builder()
- .addWords(SuggestedWords.Builder.getFromCharSequenceList(mSuggestions))
- .setAllowsToBeAutoCorrected(allowsToBeAutoCorrected)
- .setHasAutoCorrection(hasAutoCorrection);
+ scoreInfoList = SuggestedWords.Builder.getFromCharSequenceList(mSuggestions);
}
boolean autoCorrectionAvailable = hasAutoCorrection;
@@ -463,10 +437,20 @@ public class Suggest implements Dictionary.WordCallback {
}
// Don't auto-correct words with multiple capital letter
autoCorrectionAvailable &= !wordComposer.isMostlyCaps();
- builder.setTypedWordValid(!allowsToBeAutoCorrected).setHasMinimalSuggestion(
- autoCorrectionAvailable);
- if (allowsToBeAutoCorrected && builder.size() > 1 && mAutoCorrectionThreshold > 0
- && Suggest.shouldBlockAutoCorrectionBySafetyNet(typedWord, builder.getWord(1))) {
+ final boolean shouldBlockAutoCorrectionBySatefyNet;
+ if (allowsToBeAutoCorrected && scoreInfoList.size() > 1 && mAutoCorrectionThreshold > 0
+ && Suggest.shouldBlockAutoCorrectionBySafetyNet(typedWord,
+ scoreInfoList.get(1).mWord)) {
+ shouldBlockAutoCorrectionBySatefyNet = true;
+ } else {
+ shouldBlockAutoCorrectionBySatefyNet = false;
+ }
+ builder = new SuggestedWords.Builder(scoreInfoList,
+ !allowsToBeAutoCorrected /* typedWordValid */,
+ autoCorrectionAvailable /* hasMinimalSuggestion */,
+ allowsToBeAutoCorrected /* allowsToBeAutoCorrected */,
+ false /* isPunctuationSuggestions */);
+ if (shouldBlockAutoCorrectionBySatefyNet) {
builder.setShouldBlockAutoCorrectionBySafetyNet();
}
return builder;
@@ -542,7 +526,7 @@ public class Suggest implements Dictionary.WordCallback {
System.arraycopy(sortedScores, pos, sortedScores, pos + 1, prefMaxSuggestions - pos - 1);
sortedScores[pos] = score;
- final StringBuilder sb = StringBuilderPool.getStringBuilder(getApproxMaxWordLength());
+ final StringBuilder sb = new StringBuilder(getApproxMaxWordLength());
// TODO: Must pay attention to locale when changing case.
if (mIsAllUpperCase) {
sb.append(new String(word, offset, length).toUpperCase());
@@ -559,10 +543,7 @@ public class Suggest implements Dictionary.WordCallback {
}
suggestions.add(pos, sb);
if (suggestions.size() > prefMaxSuggestions) {
- final CharSequence garbage = suggestions.remove(prefMaxSuggestions);
- if (garbage instanceof StringBuilder) {
- StringBuilderPool.recycle((StringBuilder)garbage);
- }
+ suggestions.remove(prefMaxSuggestions);
} else {
LatinImeLogger.onAddSuggestedWord(sb.toString(), dicTypeId, dataTypeForLog);
}
@@ -573,40 +554,22 @@ public class Suggest implements Dictionary.WordCallback {
// TODO This is almost O(n^2). Might need fix.
// search whether the word appeared in bigram data
int bigramSuggestSize = mBigramSuggestions.size();
- for(int i = 0; i < bigramSuggestSize; i++) {
- if(mBigramSuggestions.get(i).length() == length) {
+ for (int i = 0; i < bigramSuggestSize; i++) {
+ if (mBigramSuggestions.get(i).length() == length) {
boolean chk = true;
- for(int j = 0; j < length; j++) {
- if(mBigramSuggestions.get(i).charAt(j) != word[offset+j]) {
+ for (int j = 0; j < length; j++) {
+ if (mBigramSuggestions.get(i).charAt(j) != word[offset+j]) {
chk = false;
break;
}
}
- if(chk) return i;
+ if (chk) return i;
}
}
return -1;
}
- private static void collectGarbage(ArrayList<CharSequence> suggestions,
- int prefMaxSuggestions) {
- int poolSize = StringBuilderPool.getSize();
- int garbageSize = suggestions.size();
- while (poolSize < prefMaxSuggestions && garbageSize > 0) {
- final CharSequence garbage = suggestions.get(garbageSize - 1);
- if (garbage instanceof StringBuilder) {
- StringBuilderPool.recycle((StringBuilder)garbage);
- poolSize++;
- }
- garbageSize--;
- }
- if (poolSize == prefMaxSuggestions + 1) {
- Log.w("Suggest", "String pool got too big: " + poolSize);
- }
- suggestions.clear();
- }
-
public void close() {
final Set<Dictionary> dictionaries = new HashSet<Dictionary>();
dictionaries.addAll(mUnigramDictionaries.values());
diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java
index 7dd85f65b..bc89941a1 100644
--- a/java/src/com/android/inputmethod/latin/SuggestedWords.java
+++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java
@@ -16,7 +16,6 @@
package com.android.inputmethod.latin;
-import android.text.TextUtils;
import android.view.inputmethod.CompletionInfo;
import java.util.ArrayList;
@@ -26,21 +25,25 @@ import java.util.List;
public class SuggestedWords {
public static final SuggestedWords EMPTY = new SuggestedWords(false, false, false, false,
- Collections.<SuggestedWordInfo>emptyList());
+ false, Collections.<SuggestedWordInfo>emptyList());
public final boolean mTypedWordValid;
public final boolean mHasAutoCorrectionCandidate;
public final boolean mIsPunctuationSuggestions;
+ public final boolean mAllowsToBeAutoCorrected;
private final List<SuggestedWordInfo> mSuggestedWordInfoList;
- SuggestedWords(boolean typedWordValid,
- boolean hasAutoCorrectionCandidate, boolean isPunctuationSuggestions,
- boolean shouldBlockAutoCorrectionBySafetyNet,
- List<SuggestedWordInfo> suggestedWordInfoList) {
+ SuggestedWords(final boolean typedWordValid,
+ final boolean hasAutoCorrectionCandidate,
+ final boolean isPunctuationSuggestions,
+ final boolean shouldBlockAutoCorrectionBySafetyNet,
+ final boolean allowsToBeAutoCorrected,
+ final List<SuggestedWordInfo> suggestedWordInfoList) {
mTypedWordValid = typedWordValid;
mHasAutoCorrectionCandidate = hasAutoCorrectionCandidate
&& !shouldBlockAutoCorrectionBySafetyNet;
mIsPunctuationSuggestions = isPunctuationSuggestions;
+ mAllowsToBeAutoCorrected = allowsToBeAutoCorrected;
mSuggestedWordInfoList = suggestedWordInfoList;
}
@@ -74,44 +77,26 @@ public class SuggestedWords {
}
public static class Builder {
- private boolean mTypedWordValid;
- private boolean mHasMinimalSuggestion;
- private boolean mIsPunctuationSuggestions;
+ private final boolean mTypedWordValid;
+ private final boolean mHasMinimalSuggestion;
+ private final boolean mIsPunctuationSuggestions;
private boolean mShouldBlockAutoCorrectionBySafetyNet;
- private boolean mAllowsToBeAutoCorrected;
- private boolean mHasAutoCorrection;
- private List<SuggestedWordInfo> mSuggestedWordInfoList =
- new ArrayList<SuggestedWordInfo>();
-
- public Builder() {
- // Nothing to do here.
- }
-
- // TODO: the following method is a wrapper to satisfy tests. Update tests and remove it.
- public Builder addWords(final List<CharSequence> words,
- final List<SuggestedWordInfo> suggestedWordInfoList) {
- return addWords(suggestedWordInfoList);
- }
-
- public Builder addWords(List<SuggestedWordInfo> suggestedWordInfoList) {
- final int N = suggestedWordInfoList.size();
- for (int i = 0; i < N; ++i) {
- SuggestedWordInfo suggestedWordInfo = suggestedWordInfoList.get(i);
- addWord(suggestedWordInfo.mWord, suggestedWordInfo);
- }
- return this;
- }
-
- /* package for tests */
- Builder addWord(CharSequence word, SuggestedWordInfo suggestedWordInfo) {
- if (!TextUtils.isEmpty(suggestedWordInfo.mWord)) {
- // It's okay if suggestedWordInfo is null since it's checked where it's used.
- mSuggestedWordInfoList.add(suggestedWordInfo);
- }
- return this;
+ private final boolean mAllowsToBeAutoCorrected;
+ private final List<SuggestedWordInfo> mSuggestedWordInfoList;
+
+ public Builder(final List<SuggestedWordInfo> suggestedWordInfoList,
+ final boolean typedWordValid,
+ final boolean hasMinimalSuggestion,
+ final boolean allowsToBeAutoCorrected,
+ final boolean isPunctuationSuggestions) {
+ mSuggestedWordInfoList = suggestedWordInfoList;
+ mTypedWordValid = typedWordValid;
+ mHasMinimalSuggestion = hasMinimalSuggestion;
+ mAllowsToBeAutoCorrected = allowsToBeAutoCorrected;
+ mIsPunctuationSuggestions = isPunctuationSuggestions;
}
- public static List<SuggestedWordInfo> getFromCharSequenceList(
+ public static ArrayList<SuggestedWordInfo> getFromCharSequenceList(
final List<CharSequence> wordList) {
final ArrayList<SuggestedWordInfo> result = new ArrayList<SuggestedWordInfo>();
for (CharSequence word : wordList) {
@@ -129,62 +114,35 @@ public class SuggestedWords {
return result;
}
- public Builder setTypedWordValid(boolean typedWordValid) {
- mTypedWordValid = typedWordValid;
- return this;
- }
-
- public Builder setHasMinimalSuggestion(boolean hasMinimalSuggestion) {
- mHasMinimalSuggestion = hasMinimalSuggestion;
- return this;
- }
-
- public Builder setIsPunctuationSuggestions() {
- mIsPunctuationSuggestions = true;
- return this;
- }
-
public Builder setShouldBlockAutoCorrectionBySafetyNet() {
mShouldBlockAutoCorrectionBySafetyNet = true;
return this;
}
- public Builder setAllowsToBeAutoCorrected(final boolean allowsToBeAutoCorrected) {
- mAllowsToBeAutoCorrected = allowsToBeAutoCorrected;
- return this;
- }
-
- public Builder setHasAutoCorrection(final boolean hasAutoCorrection) {
- mHasAutoCorrection = hasAutoCorrection;
- 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,
- SuggestedWords previousSuggestions) {
- mSuggestedWordInfoList.clear();
+ public static ArrayList<SuggestedWordInfo> getTypedWordAndPreviousSuggestions(
+ final CharSequence typedWord, final SuggestedWords previousSuggestions) {
+ final ArrayList<SuggestedWordInfo> suggestionsList = new ArrayList<SuggestedWordInfo>();
final HashSet<String> alreadySeen = new HashSet<String>();
- addWord(typedWord, new SuggestedWordInfo(typedWord, null, false));
+ suggestionsList.add(new SuggestedWordInfo(typedWord, null, false));
alreadySeen.add(typedWord.toString());
final int previousSize = previousSuggestions.size();
for (int pos = 1; pos < previousSize; pos++) {
final String prevWord = previousSuggestions.getWord(pos).toString();
// Filter out duplicate suggestion.
if (!alreadySeen.contains(prevWord)) {
- addWord(prevWord, new SuggestedWordInfo(prevWord, null, true));
+ suggestionsList.add(new SuggestedWordInfo(prevWord, null, true));
alreadySeen.add(prevWord);
}
}
- mTypedWordValid = false;
- mHasMinimalSuggestion = false;
- return this;
+ return suggestionsList;
}
public SuggestedWords build() {
return new SuggestedWords(mTypedWordValid, mHasMinimalSuggestion,
mIsPunctuationSuggestions, mShouldBlockAutoCorrectionBySafetyNet,
- mSuggestedWordInfoList);
+ mAllowsToBeAutoCorrected, mSuggestedWordInfoList);
}
public int size() {
@@ -199,10 +157,6 @@ public class SuggestedWords {
return mAllowsToBeAutoCorrected;
}
- public boolean hasAutoCorrection() {
- return mHasAutoCorrection;
- }
-
@Override
public String toString() {
// Pretty-print method to help debug
@@ -216,7 +170,7 @@ public class SuggestedWords {
}
public static class SuggestedWordInfo {
- private final CharSequence mWord;
+ public final CharSequence mWord;
private final CharSequence mDebugString;
private final boolean mPreviousSuggestedWord;