diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin')
6 files changed, 106 insertions, 314 deletions
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index d96b858eb..f41972e8b 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; @@ -241,18 +242,13 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar public static class UIHandler extends StaticInnerHandlerWrapper<LatinIME> { private static final int MSG_UPDATE_SHIFT_STATE = 1; private static final int MSG_VOICE_RESULTS = 2; - private static final int MSG_FADEOUT_LANGUAGE_ON_SPACEBAR = 3; - private static final int MSG_DISMISS_LANGUAGE_ON_SPACEBAR = 4; - private static final int MSG_SPACE_TYPED = 5; - private static final int MSG_SET_BIGRAM_PREDICTIONS = 6; - private static final int MSG_PENDING_IMS_CALLBACK = 7; - private static final int MSG_UPDATE_SUGGESTIONS = 8; - - private int mDelayBeforeFadeoutLanguageOnSpacebar; + private static final int MSG_SPACE_TYPED = 4; + private static final int MSG_SET_BIGRAM_PREDICTIONS = 5; + private static final int MSG_PENDING_IMS_CALLBACK = 6; + private static final int MSG_UPDATE_SUGGESTIONS = 7; + private int mDelayUpdateSuggestions; private int mDelayUpdateShiftState; - private int mDurationOfFadeoutLanguageOnSpacebar; - private float mFinalFadeoutFactorOfLanguageOnSpacebar; private long mDoubleSpacesTurnIntoPeriodTimeout; public UIHandler(LatinIME outerInstance) { @@ -261,16 +257,10 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar public void onCreate() { final Resources res = getOuterInstance().getResources(); - mDelayBeforeFadeoutLanguageOnSpacebar = res.getInteger( - R.integer.config_delay_before_fadeout_language_on_spacebar); mDelayUpdateSuggestions = res.getInteger(R.integer.config_delay_update_suggestions); mDelayUpdateShiftState = res.getInteger(R.integer.config_delay_update_shift_state); - mDurationOfFadeoutLanguageOnSpacebar = res.getInteger( - R.integer.config_duration_of_fadeout_language_on_spacebar); - mFinalFadeoutFactorOfLanguageOnSpacebar = res.getInteger( - R.integer.config_final_fadeout_percentage_of_language_on_spacebar) / 100.0f; mDoubleSpacesTurnIntoPeriodTimeout = res.getInteger( R.integer.config_double_spaces_turn_into_period_timeout); } @@ -279,7 +269,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar public void handleMessage(Message msg) { final LatinIME latinIme = getOuterInstance(); final KeyboardSwitcher switcher = latinIme.mKeyboardSwitcher; - final LatinKeyboardView inputView = switcher.getKeyboardView(); switch (msg.what) { case MSG_UPDATE_SUGGESTIONS: latinIme.updateSuggestions(); @@ -295,17 +284,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar latinIme.mVoiceProxy.handleVoiceResults(latinIme.preferCapitalization() || (keyboard != null && keyboard.isShiftedOrShiftLocked())); break; - case MSG_FADEOUT_LANGUAGE_ON_SPACEBAR: - setSpacebarTextFadeFactor(inputView, - (1.0f + mFinalFadeoutFactorOfLanguageOnSpacebar) / 2, - (Keyboard)msg.obj); - sendMessageDelayed(obtainMessage(MSG_DISMISS_LANGUAGE_ON_SPACEBAR, msg.obj), - mDurationOfFadeoutLanguageOnSpacebar); - break; - case MSG_DISMISS_LANGUAGE_ON_SPACEBAR: - setSpacebarTextFadeFactor(inputView, mFinalFadeoutFactorOfLanguageOnSpacebar, - (Keyboard)msg.obj); - break; } } @@ -344,41 +322,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar sendMessage(obtainMessage(MSG_VOICE_RESULTS)); } - private static void setSpacebarTextFadeFactor(LatinKeyboardView inputView, - float fadeFactor, Keyboard oldKeyboard) { - if (inputView == null) return; - final Keyboard keyboard = inputView.getKeyboard(); - if (keyboard == oldKeyboard) { - inputView.updateSpacebar(fadeFactor, - SubtypeSwitcher.getInstance().needsToDisplayLanguage( - keyboard.mId.mLocale)); - } - } - - public void startDisplayLanguageOnSpacebar(boolean localeChanged) { - final LatinIME latinIme = getOuterInstance(); - removeMessages(MSG_FADEOUT_LANGUAGE_ON_SPACEBAR); - removeMessages(MSG_DISMISS_LANGUAGE_ON_SPACEBAR); - final LatinKeyboardView inputView = latinIme.mKeyboardSwitcher.getKeyboardView(); - if (inputView != null) { - final Keyboard keyboard = latinIme.mKeyboardSwitcher.getKeyboard(); - // The language is always displayed when the delay is negative. - final boolean needsToDisplayLanguage = localeChanged - || mDelayBeforeFadeoutLanguageOnSpacebar < 0; - // The language is never displayed when the delay is zero. - if (mDelayBeforeFadeoutLanguageOnSpacebar != 0) { - setSpacebarTextFadeFactor(inputView, - needsToDisplayLanguage ? 1.0f : mFinalFadeoutFactorOfLanguageOnSpacebar, - keyboard); - } - // The fadeout animation will start when the delay is positive. - if (localeChanged && mDelayBeforeFadeoutLanguageOnSpacebar > 0) { - sendMessageDelayed(obtainMessage(MSG_FADEOUT_LANGUAGE_ON_SPACEBAR, keyboard), - mDelayBeforeFadeoutLanguageOnSpacebar); - } - } - } - public void startDoubleSpacesTimer() { removeMessages(MSG_SPACE_TYPED); sendMessageDelayed(obtainMessage(MSG_SPACE_TYPED), mDoubleSpacesTurnIntoPeriodTimeout); @@ -980,13 +923,14 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar return; } - final List<CharSequence> applicationSuggestedWords = + final List<SuggestedWords.SuggestedWordInfo> applicationSuggestedWords = SuggestedWords.Builder.getFromApplicationSpecifiedCompletions( applicationSpecifiedCompletions); - SuggestedWords.Builder builder = new SuggestedWords.Builder() - .addWords(applicationSuggestedWords, null) - .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; @@ -1840,8 +1784,15 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar 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 abd1dc692..0a4aea140 100644 --- a/java/src/com/android/inputmethod/latin/SettingsValues.java +++ b/java/src/com/android/inputmethod/latin/SettingsValues.java @@ -28,6 +28,7 @@ import com.android.inputmethod.compat.InputTypeCompatUtils; import com.android.inputmethod.compat.VibratorCompatWrapper; import com.android.inputmethod.keyboard.internal.KeySpecParser; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -174,28 +175,42 @@ public class SettingsValues { } private static SuggestedWords createSuggestPuncList(final String[] puncs) { - final SuggestedWords.Builder builder = new SuggestedWords.Builder(); + final ArrayList<SuggestedWords.SuggestedWordInfo> puncList = + new ArrayList<SuggestedWords.SuggestedWordInfo>(); if (puncs != null) { for (final String puncSpec : puncs) { - builder.addWord(KeySpecParser.getLabel(puncSpec)); + puncList.add(new SuggestedWords.SuggestedWordInfo( + KeySpecParser.getLabel(puncSpec))); } } - return builder.setIsPunctuationSuggestions().build(); + final SuggestedWords.Builder builder = new SuggestedWords.Builder(puncList, + false /* typedWordValid */, + false /* hasMinimalSuggestion */, + false /* allowsToBeAutoCorrected */, + true /* isPunctuationSuggestions */); + return builder.build(); } private static SuggestedWords createSuggestPuncOutputTextList(final String[] puncs) { - final SuggestedWords.Builder builder = new SuggestedWords.Builder(); + final ArrayList<SuggestedWords.SuggestedWordInfo> puncOutputTextList = + new ArrayList<SuggestedWords.SuggestedWordInfo>(); if (puncs != null) { for (final String puncSpec : puncs) { final String outputText = KeySpecParser.getOutputText(puncSpec); if (outputText != null) { - builder.addWord(outputText); + puncOutputTextList.add(new SuggestedWords.SuggestedWordInfo(outputText)); } else { - builder.addWord(KeySpecParser.getLabel(puncSpec)); + puncOutputTextList.add(new SuggestedWords.SuggestedWordInfo( + KeySpecParser.getLabel(puncSpec))); } } } - return builder.setIsPunctuationSuggestions().build(); + final SuggestedWords.Builder builder = new SuggestedWords.Builder(puncOutputTextList, + false /* typedWordValid */, + false /* hasMinimalSuggestion */, + false /* allowsToBeAutoCorrected */, + true /* isPunctuationSuggestions */); + return builder.build(); } private static String createWordSeparators(final String weakSpaceStrippers, 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 f17c1d95a..28d3b4437 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,9 +269,12 @@ public class Suggest implements Dictionary.WordCallback { StringUtils.removeDupes(mSuggestions); - return new SuggestedWords.Builder().addWords(mSuggestions, null) - .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 @@ -304,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(); @@ -327,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(); @@ -412,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. @@ -420,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(); @@ -445,13 +426,8 @@ public class Suggest implements Dictionary.WordCallback { scoreInfoList.add(new SuggestedWords.SuggestedWordInfo(mSuggestions.get(i), "--", false)); } - builder = new SuggestedWords.Builder().addWords(mSuggestions, scoreInfoList) - .setAllowsToBeAutoCorrected(allowsToBeAutoCorrected) - .setHasAutoCorrection(hasAutoCorrection); } else { - builder = new SuggestedWords.Builder().addWords(mSuggestions, null) - .setAllowsToBeAutoCorrected(allowsToBeAutoCorrected) - .setHasAutoCorrection(hasAutoCorrection); + scoreInfoList = SuggestedWords.Builder.getFromCharSequenceList(mSuggestions); } boolean autoCorrectionAvailable = hasAutoCorrection; @@ -461,8 +437,11 @@ public class Suggest implements Dictionary.WordCallback { } // Don't auto-correct words with multiple capital letter autoCorrectionAvailable &= !wordComposer.isMostlyCaps(); - builder.setTypedWordValid(!allowsToBeAutoCorrected).setHasMinimalSuggestion( - autoCorrectionAvailable); + builder = new SuggestedWords.Builder(scoreInfoList, + !allowsToBeAutoCorrected /* typedWordValid */, + autoCorrectionAvailable /* hasMinimalSuggestion */, + allowsToBeAutoCorrected /* allowsToBeAutoCorrected */, + false /* isPunctuationSuggestions */); if (allowsToBeAutoCorrected && builder.size() > 1 && mAutoCorrectionThreshold > 0 && Suggest.shouldBlockAutoCorrectionBySafetyNet(typedWord, builder.getWord(1))) { builder.setShouldBlockAutoCorrectionBySafetyNet(); @@ -540,7 +519,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()); @@ -557,10 +536,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); } @@ -571,40 +547,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 4a51e796d..144e67482 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; @@ -74,115 +73,66 @@ 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. - } - - public Builder addWords(List<CharSequence> words, - List<SuggestedWordInfo> suggestedWordInfoList) { - final int N = words.size(); - for (int i = 0; i < N; ++i) { - final CharSequence word = words.get(i); - SuggestedWordInfo suggestedWordInfo = null; - if (suggestedWordInfoList != null) { - suggestedWordInfo = suggestedWordInfoList.get(i); - } - if (suggestedWordInfo == null) { - suggestedWordInfo = new SuggestedWordInfo(word); - } - addWord(word, suggestedWordInfo); - } - return this; - } - - public Builder addWord(CharSequence word) { - return addWord(word, null, false); - } - - public Builder addWord(CharSequence word, CharSequence debugString, - boolean isPreviousSuggestedWord) { - SuggestedWordInfo info = new SuggestedWordInfo(word, debugString, - isPreviousSuggestedWord); - return addWord(word, info); + 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; } - /* 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); + public static ArrayList<SuggestedWordInfo> getFromCharSequenceList( + final List<CharSequence> wordList) { + final ArrayList<SuggestedWordInfo> result = new ArrayList<SuggestedWordInfo>(); + for (CharSequence word : wordList) { + if (null != word) result.add(new SuggestedWordInfo(word, null, false)); } - return this; + return result; } - public static List<CharSequence> getFromApplicationSpecifiedCompletions( + public static List<SuggestedWordInfo> getFromApplicationSpecifiedCompletions( final CompletionInfo[] infos) { - final ArrayList<CharSequence> result = new ArrayList<CharSequence>(); + final ArrayList<SuggestedWordInfo> result = new ArrayList<SuggestedWordInfo>(); for (CompletionInfo info : infos) { - if (null != info) result.add(info.getText()); + if (null != info) result.add(new SuggestedWordInfo(info.getText(), null, false)); } 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, 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, null, true); + suggestionsList.add(new SuggestedWordInfo(prevWord, null, true)); alreadySeen.add(prevWord); } } - mTypedWordValid = false; - mHasMinimalSuggestion = false; - return this; + return suggestionsList; } public SuggestedWords build() { @@ -203,10 +153,6 @@ public class SuggestedWords { return mAllowsToBeAutoCorrected; } - public boolean hasAutoCorrection() { - return mHasAutoCorrection; - } - @Override public String toString() { // Pretty-print method to help debug |