From 7e181fe1010c8eac7814cc67a0c4b3864a10b151 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Fri, 10 Dec 2010 20:50:30 +0900 Subject: [PATCH] Introduce SuggestedWords class to represent suggestions list Change-Id: I81677a785640d37296be8b42c7a74bd0c00edf46 --- java/res/layout/candidate.xml | 15 ++- .../inputmethod/latin/CandidateView.java | 120 +++++++----------- .../android/inputmethod/latin/LatinIME.java | 91 +++++++------ .../android/inputmethod/latin/Suggest.java | 19 ++- .../inputmethod/latin/SuggestedWords.java | 120 ++++++++++++++++++ .../inputmethod/voice/VoiceIMEConnector.java | 16 ++- .../inputmethod/latin/SuggestHelper.java | 20 +-- 7 files changed, 254 insertions(+), 147 deletions(-) create mode 100644 java/src/com/android/inputmethod/latin/SuggestedWords.java diff --git a/java/res/layout/candidate.xml b/java/res/layout/candidate.xml index 37179d2af..03be5505b 100644 --- a/java/res/layout/candidate.xml +++ b/java/res/layout/candidate.xml @@ -23,6 +23,7 @@ android:layout_width="match_parent" android:layout_height="@dimen/candidate_strip_height" android:orientation="horizontal" + android:paddingRight="12dip" > + android:paddingLeft="12dip" /> + diff --git a/java/src/com/android/inputmethod/latin/CandidateView.java b/java/src/com/android/inputmethod/latin/CandidateView.java index 03ba1192b..784077a76 100644 --- a/java/src/com/android/inputmethod/latin/CandidateView.java +++ b/java/src/com/android/inputmethod/latin/CandidateView.java @@ -43,11 +43,9 @@ import android.widget.PopupWindow; import android.widget.TextView; import java.util.ArrayList; -import java.util.List; public class CandidateView extends LinearLayout implements OnClickListener, OnLongClickListener { private LatinIME mService; - private final ArrayList mSuggestions = new ArrayList(); private final ArrayList mWords = new ArrayList(); private final TextView mPreviewText; @@ -64,27 +62,12 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo private final CharacterStyle mInvertedForegroundColorSpan; private final CharacterStyle mInvertedBackgroundColorSpan; - private boolean mShowingCompletions; + private SuggestedWords mSuggestions = SuggestedWords.EMPTY; private boolean mShowingAutoCorrectionInverted; - private boolean mShowingAddToDictionary; private final UiHandler mHandler = new UiHandler(); - private static class UpdateSuggestionsArgs { - public final List mSuggestions; - public final boolean mCompletions; - public final boolean mTypedWordValid; - public final boolean mHaveMinimalSuggestion; - public UpdateSuggestionsArgs(List suggestions, boolean completions, - boolean typedWordValid, boolean haveMinimalSuggestion) { - mSuggestions = suggestions; - mCompletions = completions; - mTypedWordValid = typedWordValid; - mHaveMinimalSuggestion = haveMinimalSuggestion; - } - } - private class UiHandler extends Handler { private static final int MSG_HIDE_PREVIEW = 0; private static final int MSG_UPDATE_SUGGESTION = 1; @@ -99,9 +82,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo hidePreview(); break; case MSG_UPDATE_SUGGESTION: - UpdateSuggestionsArgs args = (UpdateSuggestionsArgs)msg.obj; - updateSuggestions(args.mSuggestions, args.mCompletions, args.mTypedWordValid, - args.mHaveMinimalSuggestion); + updateSuggestions((SuggestedWords)msg.obj); break; } } @@ -115,11 +96,9 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo removeMessages(MSG_HIDE_PREVIEW); } - public void postUpdateSuggestions(List suggestions, boolean completions, - boolean typedWordValid, boolean haveMinimalSuggestion) { + public void postUpdateSuggestions(SuggestedWords suggestions) { cancelUpdateSuggestions(); - sendMessageDelayed(obtainMessage(MSG_UPDATE_SUGGESTION, new UpdateSuggestionsArgs( - suggestions, completions, typedWordValid, haveMinimalSuggestion)), + sendMessageDelayed(obtainMessage(MSG_UPDATE_SUGGESTION, suggestions), DELAY_UPDATE_SUGGESTION); } @@ -167,7 +146,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo tv.setOnLongClickListener(this); ImageView divider = (ImageView)v.findViewById(R.id.candidate_divider); // Do not display divider of first candidate. - divider.setVisibility(i == 0 ? View.GONE : View.VISIBLE); + divider.setVisibility(i == 0 ? GONE : VISIBLE); mWords.add(v); } @@ -182,44 +161,35 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo mService = listener; } - public void setSuggestions(List suggestions, boolean completions, - boolean typedWordValid, boolean haveMinimalSuggestion) { - // Don't update suggestions when there is zero or only one suggestion found. - if (suggestions != null && suggestions.size() <= 1) + public void setSuggestions(SuggestedWords suggestions) { + // Don't update suggestions when there is only one suggestion found. + // Empty (size zero) suggestions will be passed in order to clear candidate view. + if (suggestions == null || suggestions.size() == 1) return; if (mShowingAutoCorrectionInverted) { - mHandler.postUpdateSuggestions(suggestions, completions, typedWordValid, - haveMinimalSuggestion); + mHandler.postUpdateSuggestions(suggestions); } else { - updateSuggestions(suggestions, completions, typedWordValid, haveMinimalSuggestion); + updateSuggestions(suggestions); } } - private void updateSuggestions(List suggestions, boolean completions, - boolean typedWordValid, boolean haveMinimalSuggestion) { + private void updateSuggestions(SuggestedWords suggestions) { clear(); - if (suggestions != null) { - int insertCount = Math.min(suggestions.size(), MAX_SUGGESTIONS); - for (CharSequence suggestion : suggestions) { - mSuggestions.add(suggestion); - if (--insertCount == 0) - break; - } - } - - final int count = mSuggestions.size(); - boolean existsAutoCompletion = false; - + mSuggestions = suggestions; + final int count = suggestions.size(); + final Object[] debugInfo = suggestions.mDebugInfo; for (int i = 0; i < count; i++) { - CharSequence suggestion = mSuggestions.get(i); - if (suggestion == null) continue; - final int wordLength = suggestion.length(); + CharSequence word = suggestions.getWord(i); + if (word == null) continue; + final int wordLength = word.length(); final View v = mWords.get(i); final TextView tv = (TextView)v.findViewById(R.id.candidate_word); + final TextView dv = (TextView)v.findViewById(R.id.candidate_debug_info); tv.setTextColor(mColorNormal); - if (haveMinimalSuggestion - && ((i == 1 && !typedWordValid) || (i == 0 && typedWordValid))) { + if (suggestions.mHasMinimalSuggestion + && ((i == 1 && !suggestions.mTypedWordValid) || + (i == 0 && suggestions.mTypedWordValid))) { final CharacterStyle style; if (mConfigCandidateHighlightFontColorEnabled) { style = BOLD_SPAN; @@ -227,10 +197,9 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo } else { style = UNDERLINE_SPAN; } - final Spannable word = new SpannableString(suggestion); - word.setSpan(style, 0, wordLength, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); - suggestion = word; - existsAutoCompletion = true; + final Spannable spannedWord = new SpannableString(word); + spannedWord.setSpan(style, 0, wordLength, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); + word = spannedWord; } else if (i != 0 || (wordLength == 1 && count > 1)) { // HACK: even if i == 0, we use mColorOther when this // suggestion's length is 1 @@ -239,16 +208,18 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo if (mConfigCandidateHighlightFontColorEnabled) tv.setTextColor(mColorOther); } - tv.setText(suggestion); + tv.setText(word); tv.setClickable(true); + if (debugInfo != null && i < debugInfo.length && debugInfo[i] != null + && !TextUtils.isEmpty(debugInfo[i].toString())) { + dv.setText(debugInfo[i].toString()); + dv.setVisibility(VISIBLE); + } else { + dv.setVisibility(GONE); + } addView(v); } - mShowingCompletions = completions; - // TODO: Move this call back to LatinIME - if (mConfigCandidateHighlightFontColorEnabled) - mService.onAutoCompletionStateChanged(existsAutoCompletion); - scrollTo(0, getScrollY()); requestLayout(); } @@ -267,15 +238,19 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo mShowingAutoCorrectionInverted = true; } + public boolean isConfigCandidateHighlightFontColorEnabled() { + return mConfigCandidateHighlightFontColorEnabled; + } + public boolean isShowingAddToDictionaryHint() { return mShowingAddToDictionary; } public void showAddToDictionaryHint(CharSequence word) { - ArrayList suggestions = new ArrayList(); - suggestions.add(word); - suggestions.add(getContext().getText(R.string.hint_add_to_dictionary)); - setSuggestions(suggestions, false, false, false); + SuggestedWords.Builder builder = new SuggestedWords.Builder() + .addWord(word) + .addWord(getContext().getText(R.string.hint_add_to_dictionary)); + setSuggestions(builder.build()); mShowingAddToDictionary = true; // Disable R.string.hint_add_to_dictionary button TextView tv = (TextView)getChildAt(1).findViewById(R.id.candidate_word); @@ -288,14 +263,11 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo return true; } - /* package */ List getSuggestions() { + public SuggestedWords getSuggestions() { return mSuggestions; } public void clear() { - // Don't call mSuggestions.clear() because it's being used for logging - // in LatinIME.pickSuggestionManually(). - mSuggestions.clear(); mShowingAddToDictionary = false; mShowingAutoCorrectionInverted = false; removeAllViews(); @@ -337,7 +309,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo @Override public boolean onLongClick(View view) { int index = (Integer) view.getTag(); - CharSequence word = mSuggestions.get(index); + CharSequence word = mSuggestions.getWord(index); if (word.length() < 2) return false; addToDictionary(word); @@ -347,12 +319,12 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo @Override public void onClick(View view) { int index = (Integer) view.getTag(); - CharSequence word = mSuggestions.get(index); + CharSequence word = mSuggestions.getWord(index); if (mShowingAddToDictionary && index == 0) { addToDictionary(word); } else { - if (!mShowingCompletions) { - TextEntryState.acceptedSuggestion(mSuggestions.get(0), word); + if (!mSuggestions.mIsApplicationSpecifiedCompletions) { + TextEntryState.acceptedSuggestion(mSuggestions.getWord(0), word); } mService.pickSuggestionManually(index, word); } diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 99b6c739a..46b735ab2 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -77,7 +77,6 @@ import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; -import java.util.List; import java.util.Locale; /** @@ -162,7 +161,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // Keep track of the last selection range to decide if we need to show word alternatives private int mLastSelectionStart; private int mLastSelectionEnd; - private List mSuggestPuncList; + private SuggestedWords mSuggestPuncList; // Input type is such that we should not auto-correct private boolean mInputTypeNoAutoCorrect; @@ -212,7 +211,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen return mChosenWord; } - public abstract List getAlternatives(); + public abstract SuggestedWords.Builder getAlternatives(); } public class TypedWordAlternatives extends WordAlternatives { @@ -233,7 +232,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } @Override - public List getAlternatives() { + public SuggestedWords.Builder getAlternatives() { return getTypedSuggestions(word); } } @@ -655,7 +654,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen super.onFinishInput(); LatinImeLogger.commit(); - onAutoCompletionStateChanged(false); + mKeyboardSwitcher.onAutoCompletionStateChanged(false); mVoiceConnector.flushVoiceInputLogs(mConfigurationChanging); @@ -790,7 +789,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen @Override public void hideWindow() { LatinImeLogger.commit(); - onAutoCompletionStateChanged(false); + mKeyboardSwitcher.onAutoCompletionStateChanged(false); if (TRACE) Debug.stopMethodTracing(); if (mOptionsDialog != null && mOptionsDialog.isShowing()) { @@ -818,13 +817,12 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen return; } - List stringList = new ArrayList(); - for (int i = 0; i < completions.length; i++) { - CompletionInfo ci = completions[i]; - if (ci != null) stringList.add(ci.getText()); - } + SuggestedWords.Builder builder = new SuggestedWords.Builder() + .setApplicationSpecifiedCompletions(completions) + .setTypedWordValid(true) + .setHasMinimalSuggestion(true); // When in fullscreen mode, show completions generated by the application - setSuggestions(stringList, true, true, true); + setSuggestions(builder.build()); mBestWord = null; setCandidatesViewShown(true); } @@ -1382,7 +1380,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } private boolean isShowingPunctuationList() { - return mSuggestPuncList.equals(mCandidateView.getSuggestions()); + return mSuggestPuncList == mCandidateView.getSuggestions(); } private boolean isSuggestionShown() { @@ -1422,22 +1420,18 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } public void clearSuggestions() { - setSuggestions(null, false, false, false); + setSuggestions(SuggestedWords.EMPTY); } - public void setSuggestions( - List suggestions, - boolean completions, - boolean typedWordValid, - boolean haveMinimalSuggestion) { - + public void setSuggestions(SuggestedWords words) { if (mVoiceConnector.getAndResetIsShowingHint()) { setCandidatesView(mCandidateViewContainer); } if (mCandidateView != null) { - mCandidateView.setSuggestions( - suggestions, completions, typedWordValid, haveMinimalSuggestion); + mCandidateView.setSuggestions(words); + if (mCandidateView.isConfigCandidateHighlightFontColorEnabled()) + mKeyboardSwitcher.onAutoCompletionStateChanged(words.hasAutoCorrectionWord()); } } @@ -1457,16 +1451,16 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen showSuggestions(mWord); } - private List getTypedSuggestions(WordComposer word) { - List stringList = mSuggest.getSuggestions( + private SuggestedWords.Builder getTypedSuggestions(WordComposer word) { + return mSuggest.getSuggestedWordBuilder( mKeyboardSwitcher.getInputView(), word, false, null); - return stringList; } private void showCorrections(WordAlternatives alternatives) { mKeyboardSwitcher.setPreferredLetters(null); - List stringList = alternatives.getAlternatives(); - showSuggestions(stringList, alternatives.getOriginalWord(), false, false); + SuggestedWords.Builder builder = alternatives.getAlternatives(); + builder.setTypedWordValid(false).setHasMinimalSuggestion(false); + showSuggestions(builder.build(), alternatives.getOriginalWord()); } private void showSuggestions(WordComposer word) { @@ -1474,7 +1468,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // TODO Maybe need better way of retrieving previous word CharSequence prevWord = EditingUtils.getPreviousWord(getCurrentInputConnection(), mWordSeparators); - List stringList = mSuggest.getSuggestions( + SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder( mKeyboardSwitcher.getInputView(), word, false, prevWord); // long stopTime = System.currentTimeMillis(); // TIME MEASUREMENT! // Log.d("LatinIME","Suggest Total Time - " + (stopTime - startTime)); @@ -1497,15 +1491,16 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen correctionAvailable &= !word.isMostlyCaps(); correctionAvailable &= !TextEntryState.isCorrecting(); - showSuggestions(stringList, typedWord, typedWordValid, correctionAvailable); + builder.setTypedWordValid(typedWordValid).setHasMinimalSuggestion(correctionAvailable); + showSuggestions(builder.build(), typedWord); } - private void showSuggestions(List stringList, CharSequence typedWord, - boolean typedWordValid, boolean correctionAvailable) { - setSuggestions(stringList, false, typedWordValid, correctionAvailable); - if (stringList.size() > 0) { - if (correctionAvailable && !typedWordValid && stringList.size() > 1) { - mBestWord = stringList.get(1); + private void showSuggestions(SuggestedWords suggestedWords, CharSequence typedWord) { + setSuggestions(suggestedWords); + if (suggestedWords.size() > 0) { + if (suggestedWords.mHasMinimalSuggestion + && !suggestedWords.mTypedWordValid && suggestedWords.size() > 1) { + mBestWord = suggestedWords.getWord(1); } else { mBestWord = typedWord; } @@ -1534,7 +1529,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } public void pickSuggestionManually(int index, CharSequence suggestion) { - List suggestions = mCandidateView.getSuggestions(); + SuggestedWords suggestions = mCandidateView.getSuggestions(); mVoiceConnector.flushAndLogAllTextModificationCounters(index, suggestion, mWordSeparators); final boolean correcting = TextEntryState.isCorrecting(); @@ -1565,7 +1560,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // Word separators are suggested before the user inputs something. // So, LatinImeLogger logs "" as a user's input. LatinImeLogger.logOnManualSuggestion( - "", suggestion.toString(), index, suggestions); + "", suggestion.toString(), index, suggestions.mWords); final char primaryCode = suggestion.charAt(0); onKey(primaryCode, new int[]{primaryCode}, KeyboardView.NOT_A_TOUCH_COORDINATE, KeyboardView.NOT_A_TOUCH_COORDINATE); @@ -1583,7 +1578,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen addToBigramDictionary(suggestion, 1); } LatinImeLogger.logOnManualSuggestion(mComposing.toString(), suggestion.toString(), - index, suggestions); + index, suggestions.mWords); TextEntryState.acceptedSuggestion(mComposing.toString(), suggestion); // Follow it with a space if (mAutoSpace && !correcting) { @@ -1718,7 +1713,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen private void setPunctuationSuggestions() { setCandidatesViewShown(isCandidateStripVisible()); - setSuggestions(mSuggestPuncList, false, false, false); + setSuggestions(mSuggestPuncList); } private void addToDictionaries(CharSequence suggestion, int frequencyDelta) { @@ -2106,13 +2101,17 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } private void initSuggestPuncList() { - mSuggestPuncList = new ArrayList(); - mSuggestPuncs = mResources.getString(R.string.suggested_punctuations); - if (mSuggestPuncs != null) { - for (int i = 0; i < mSuggestPuncs.length(); i++) { - mSuggestPuncList.add(mSuggestPuncs.subSequence(i, i + 1)); + if (mSuggestPuncs != null || mSuggestPuncList != null) + return; + SuggestedWords.Builder builder = new SuggestedWords.Builder(); + String puncs = mResources.getString(R.string.suggested_punctuations); + if (puncs != null) { + for (int i = 0; i < puncs.length(); i++) { + builder.addWord(puncs.subSequence(i, i + 1)); } } + mSuggestPuncList = builder.build(); + mSuggestPuncs = puncs; } private boolean isSuggestedPunctuation(int code) { @@ -2192,10 +2191,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen System.out.println("CPS = " + ((CPS_BUFFER_SIZE * 1000f) / total)); } - public void onAutoCompletionStateChanged(boolean isAutoCompletion) { - mKeyboardSwitcher.onAutoCompletionStateChanged(isAutoCompletion); - } - @Override public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype) { SubtypeSwitcher.getInstance().updateSubtype(subtype); diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index a6df187e6..237d7cb67 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -25,12 +25,10 @@ import android.view.View; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; -import java.util.List; /** * This class loads a dictionary and provides a list of suggestions for a given sequence of * characters. This includes corrections and completions. - * @hide pending API Council Approval */ public class Suggest implements Dictionary.WordCallback { @@ -191,14 +189,21 @@ public class Suggest implements Dictionary.WordCallback { } /** - * Returns a list of words that match the list of character codes passed in. - * This list will be overwritten the next time this function is called. + * Returns a object which represents suggested words that match the list of character codes + * passed in. This object contents will be overwritten the next time this function is called. * @param view a view for retrieving the context for AutoText * @param wordComposer contains what is currently being typed * @param prevWordForBigram previous word (used only for bigram) - * @return list of suggestions. + * @return suggested words object. */ - public List getSuggestions(View view, WordComposer wordComposer, + public SuggestedWords getSuggestions(View view, WordComposer wordComposer, + boolean includeTypedWordIfValid, CharSequence prevWordForBigram) { + return getSuggestedWordBuilder(view, wordComposer, includeTypedWordIfValid, + prevWordForBigram).build(); + } + + // TODO: cleanup dictionaries looking up and suggestions building with SuggestedWords.Builder + public SuggestedWords.Builder getSuggestedWordBuilder(View view, WordComposer wordComposer, boolean includeTypedWordIfValid, CharSequence prevWordForBigram) { LatinImeLogger.onStartSuggestion(prevWordForBigram); mHaveCorrection = false; @@ -342,7 +347,7 @@ public class Suggest implements Dictionary.WordCallback { } } removeDupes(); - return mSuggestions; + return new SuggestedWords.Builder().setWords(mSuggestions); } public int[] getNextLettersFrequencies() { diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java new file mode 100644 index 000000000..3a3176e54 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2010 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 android.view.inputmethod.CompletionInfo; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class SuggestedWords { + public static final SuggestedWords EMPTY = new SuggestedWords(null, false, false, false, null); + + public final List mWords; + public final boolean mIsApplicationSpecifiedCompletions; + public final boolean mTypedWordValid; + public final boolean mHasMinimalSuggestion; + public final Object[] mDebugInfo; + + private SuggestedWords(List words, boolean isApplicationSpecifiedCompletions, + boolean typedWordValid, boolean hasMinamlSuggestion, Object[] debugInfo) { + if (words != null) { + mWords = words; + } else { + mWords = Collections.emptyList(); + } + mIsApplicationSpecifiedCompletions = isApplicationSpecifiedCompletions; + mTypedWordValid = typedWordValid; + mHasMinimalSuggestion = hasMinamlSuggestion; + mDebugInfo = debugInfo; + } + + public int size() { + return mWords.size(); + } + + public CharSequence getWord(int pos) { + return mWords.get(pos); + } + + public boolean hasAutoCorrectionWord() { + return mHasMinimalSuggestion && ((size() >= 1 && !mTypedWordValid) || mTypedWordValid); + } + + public static class Builder { + private List mWords; + private boolean mIsCompletions; + private boolean mTypedWordVallid; + private boolean mHasMinimalSuggestion; + private Object[] mDebugInfo; + + public Builder() { + // Nothing to do here. + } + + public Builder setWords(List words) { + mWords = words; + return this; + } + + public Builder setDebugInfo(Object[] debuginfo) { + mDebugInfo = debuginfo; + return this; + } + + public Builder addWord(int pos, CharSequence word) { + if (mWords == null) + mWords = new ArrayList(); + mWords.add(pos, word); + return this; + } + + public Builder addWord(CharSequence word) { + if (mWords == null) + mWords = new ArrayList(); + mWords.add(word); + return this; + } + + public Builder setApplicationSpecifiedCompletions(CompletionInfo[] infos) { + for (CompletionInfo info : infos) + addWord(info.getText()); + mIsCompletions = true; + return this; + } + + public Builder setTypedWordValid(boolean typedWordValid) { + mTypedWordVallid = typedWordValid; + return this; + } + + public Builder setHasMinimalSuggestion(boolean hasMinamlSuggestion) { + mHasMinimalSuggestion = hasMinamlSuggestion; + return this; + } + + public CharSequence getWord(int pos) { + return mWords.get(pos); + } + + public SuggestedWords build() { + return new SuggestedWords(mWords, mIsCompletions, mTypedWordVallid, + mHasMinimalSuggestion, mDebugInfo); + } + } +} diff --git a/java/src/com/android/inputmethod/voice/VoiceIMEConnector.java b/java/src/com/android/inputmethod/voice/VoiceIMEConnector.java index 2052d9348..c532a0baf 100644 --- a/java/src/com/android/inputmethod/voice/VoiceIMEConnector.java +++ b/java/src/com/android/inputmethod/voice/VoiceIMEConnector.java @@ -22,6 +22,7 @@ import com.android.inputmethod.latin.LatinIME.UIHandler; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.SharedPreferencesCompat; import com.android.inputmethod.latin.SubtypeSwitcher; +import com.android.inputmethod.latin.SuggestedWords; import android.app.AlertDialog; import android.content.Context; @@ -424,17 +425,20 @@ public class VoiceIMEConnector implements VoiceInput.UiListener { if (mWordToSuggestions.containsKey(selectedWord)) { mShowingVoiceSuggestions = true; List suggestions = mWordToSuggestions.get(selectedWord); + SuggestedWords.Builder builder = new SuggestedWords.Builder(); // If the first letter of touching is capitalized, make all the suggestions // start with a capital letter. if (Character.isUpperCase(touching.mWord.charAt(0))) { - for (int i = 0; i < suggestions.size(); i++) { - String origSugg = (String) suggestions.get(i); - String capsSugg = origSugg.toUpperCase().charAt(0) - + origSugg.subSequence(1, origSugg.length()).toString(); - suggestions.set(i, capsSugg); + for (CharSequence word : suggestions) { + String str = word.toString(); + word = Character.toUpperCase(str.charAt(0)) + str.substring(1); + builder.addWord(word); } + } else { + builder.setWords(suggestions); } - mContext.setSuggestions(suggestions, false, true, true); + builder.setTypedWordValid(true).setHasMinimalSuggestion(true); + mContext.setSuggestions(builder.build()); mContext.setCandidatesViewShown(true); return true; } diff --git a/tests/src/com/android/inputmethod/latin/SuggestHelper.java b/tests/src/com/android/inputmethod/latin/SuggestHelper.java index 932098a39..25ceef6b7 100644 --- a/tests/src/com/android/inputmethod/latin/SuggestHelper.java +++ b/tests/src/com/android/inputmethod/latin/SuggestHelper.java @@ -116,30 +116,30 @@ public class SuggestHelper { return word; } - private boolean isDefaultSuggestion(List suggestions, CharSequence word) { + private boolean isDefaultSuggestion(SuggestedWords suggestions, CharSequence word) { // Check if either the word is what you typed or the first alternative return suggestions.size() > 0 && (/*TextUtils.equals(suggestions.get(0), word) || */ - (suggestions.size() > 1 && TextUtils.equals(suggestions.get(1), word))); + (suggestions.size() > 1 && TextUtils.equals(suggestions.getWord(1), word))); } boolean isDefaultSuggestion(CharSequence typed, CharSequence expected) { WordComposer word = createWordComposer(typed); - List suggestions = mSuggest.getSuggestions(null, word, false, null); + SuggestedWords suggestions = mSuggest.getSuggestions(null, word, false, null); return isDefaultSuggestion(suggestions, expected); } boolean isDefaultCorrection(CharSequence typed, CharSequence expected) { WordComposer word = createWordComposer(typed); - List suggestions = mSuggest.getSuggestions(null, word, false, null); + SuggestedWords suggestions = mSuggest.getSuggestions(null, word, false, null); return isDefaultSuggestion(suggestions, expected) && mSuggest.hasMinimalCorrection(); } boolean isASuggestion(CharSequence typed, CharSequence expected) { WordComposer word = createWordComposer(typed); - List suggestions = mSuggest.getSuggestions(null, word, false, null); + SuggestedWords suggestions = mSuggest.getSuggestions(null, word, false, null); for (int i = 1; i < suggestions.size(); i++) { - if (TextUtils.equals(suggestions.get(i), expected)) return true; + if (TextUtils.equals(suggestions.getWord(i), expected)) return true; } return false; } @@ -155,7 +155,7 @@ public class SuggestHelper { CharSequence expected) { WordComposer word = createWordComposer(typed); getBigramSuggestions(previous, typed); - List suggestions = mSuggest.getSuggestions(null, word, false, previous); + SuggestedWords suggestions = mSuggest.getSuggestions(null, word, false, previous); return isDefaultSuggestion(suggestions, expected); } @@ -163,7 +163,7 @@ public class SuggestHelper { CharSequence expected) { WordComposer word = createWordComposer(typed); getBigramSuggestions(previous, typed); - List suggestions = mSuggest.getSuggestions(null, word, false, previous); + SuggestedWords suggestions = mSuggest.getSuggestions(null, word, false, previous); return isDefaultSuggestion(suggestions, expected) && mSuggest.hasMinimalCorrection(); } @@ -171,9 +171,9 @@ public class SuggestHelper { CharSequence expected) { WordComposer word = createWordComposer(typed); getBigramSuggestions(previous, typed); - List suggestions = mSuggest.getSuggestions(null, word, false, previous); + SuggestedWords suggestions = mSuggest.getSuggestions(null, word, false, previous); for (int i = 1; i < suggestions.size(); i++) { - if (TextUtils.equals(suggestions.get(i), expected)) return true; + if (TextUtils.equals(suggestions.getWord(i), expected)) return true; } return false; }