From 6f7218627eda110a8454053f8ecb7b80edfdc8ce Mon Sep 17 00:00:00 2001 From: satok Date: Wed, 19 Jan 2011 17:29:27 +0900 Subject: [PATCH] Dim previously suggested words Change-Id: Id673c03bfa22ea9ce1bedb5174d8309a37a2a460 --- .../inputmethod/latin/CandidateView.java | 28 +++++- .../android/inputmethod/latin/LatinIME.java | 2 +- .../android/inputmethod/latin/Suggest.java | 16 ++-- .../inputmethod/latin/SuggestedWords.java | 91 +++++++++++++------ .../inputmethod/voice/VoiceIMEConnector.java | 2 +- 5 files changed, 97 insertions(+), 42 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/CandidateView.java b/java/src/com/android/inputmethod/latin/CandidateView.java index d2d1f22dd..fc45c7c75 100644 --- a/java/src/com/android/inputmethod/latin/CandidateView.java +++ b/java/src/com/android/inputmethod/latin/CandidateView.java @@ -16,8 +16,11 @@ package com.android.inputmethod.latin; +import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; + import android.content.Context; import android.content.res.Resources; +import android.graphics.Color; import android.graphics.Typeface; import android.os.Handler; import android.os.Message; @@ -43,6 +46,7 @@ import android.widget.PopupWindow; import android.widget.TextView; import java.util.ArrayList; +import java.util.List; public class CandidateView extends LinearLayout implements OnClickListener, OnLongClickListener { @@ -50,6 +54,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo private static final CharacterStyle UNDERLINE_SPAN = new UnderlineSpan(); private static final int MAX_SUGGESTIONS = 16; + private static boolean DBG = LatinImeLogger.sDBG; + private final ArrayList mWords = new ArrayList(); private final boolean mConfigCandidateHighlightFontColorEnabled; private final CharacterStyle mInvertedForegroundColorSpan; @@ -175,11 +181,12 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo final SuggestedWords suggestions = mSuggestions; clear(); final int count = suggestions.size(); - final Object[] debugInfo = suggestions.mDebugInfo; for (int i = 0; i < count; i++) { CharSequence word = suggestions.getWord(i); if (word == null) continue; final int wordLength = word.length(); + final List suggestedWordInfoList = + suggestions.mSuggestedWordInfoList; final View v = mWords.get(i); final TextView tv = (TextView)v.findViewById(R.id.candidate_word); @@ -209,10 +216,21 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo } 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); + + if (suggestedWordInfoList != null && suggestedWordInfoList.get(i) != null) { + final SuggestedWordInfo info = suggestedWordInfoList.get(i); + if (info.isPreviousSuggestedWord()) { + int color = tv.getCurrentTextColor(); + tv.setTextColor(Color.argb((int)(Color.alpha(color) * 0.5f), Color.red(color), + Color.green(color), Color.blue(color))); + } + final String debugString = info.getDebugString(); + if (DBG) { + if (!TextUtils.isEmpty(debugString)) { + dv.setText(debugString); + dv.setVisibility(VISIBLE); + } + } } else { dv.setVisibility(GONE); } diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 7089874eb..02a696088 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -1519,7 +1519,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mKeyboardSwitcher.setPreferredLetters(nextLettersFrequencies); boolean correctionAvailable = !mInputTypeNoAutoCorrect && !mJustReverted - && mSuggest.hasMinimalCorrection(); + && mSuggest.hasAutoCorrection(); final CharSequence typedWord = word.getTypedWord(); // If we're in basic correct final boolean typedWordValid = mSuggest.isValidWord(typedWord) || diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index 24c73e8ea..1772b2669 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -95,7 +95,7 @@ public class Suggest implements Dictionary.WordCallback { private ArrayList mSuggestions = new ArrayList(); ArrayList mBigramSuggestions = new ArrayList(); private ArrayList mStringPool = new ArrayList(); - private boolean mHaveAutoCorrection; + private boolean mHasAutoCorrection; private String mLowerOriginalWord; // TODO: Remove these member variables by passing more context to addWord() callback method @@ -200,7 +200,7 @@ public class Suggest implements Dictionary.WordCallback { public SuggestedWords.Builder getSuggestedWordBuilder(View view, WordComposer wordComposer, CharSequence prevWordForBigram) { LatinImeLogger.onStartSuggestion(prevWordForBigram); - mHaveAutoCorrection = false; + mHasAutoCorrection = false; mIsFirstCharCapitalized = wordComposer.isFirstCharCapitalized(); mIsAllUpperCase = wordComposer.isAllUpperCase(); collectGarbage(mSuggestions, mPrefMaxSuggestions); @@ -278,7 +278,7 @@ public class Suggest implements Dictionary.WordCallback { if (DBG) { Log.d(TAG, "Auto corrected by CORRECTION_FULL."); } - mHaveAutoCorrection = true; + mHasAutoCorrection = true; } } if (mMainDict != null) mMainDict.getWords(wordComposer, this, mNextLettersFrequencies); @@ -297,7 +297,7 @@ public class Suggest implements Dictionary.WordCallback { if (DBG) { Log.d(TAG, "Auto corrected by S-threthhold."); } - mHaveAutoCorrection = true; + mHasAutoCorrection = true; } } } @@ -342,7 +342,7 @@ public class Suggest implements Dictionary.WordCallback { if (DBG) { Log.d(TAG, "Auto corrected by AUTOTEXT."); } - mHaveAutoCorrection = true; + mHasAutoCorrection = true; mSuggestions.add(i + 1, autoText); i++; } @@ -350,7 +350,7 @@ public class Suggest implements Dictionary.WordCallback { } } removeDupes(); - return new SuggestedWords.Builder().addWords(mSuggestions); + return new SuggestedWords.Builder().addWords(mSuggestions, null); } public int[] getNextLettersFrequencies() { @@ -384,8 +384,8 @@ public class Suggest implements Dictionary.WordCallback { } } - public boolean hasMinimalCorrection() { - return mHaveAutoCorrection; + public boolean hasAutoCorrection() { + return mHasAutoCorrection; } private boolean compareCaseInsensitive(final String mLowerOriginalWord, diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java index 0fbbcdd91..4407e5b31 100644 --- a/java/src/com/android/inputmethod/latin/SuggestedWords.java +++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java @@ -29,10 +29,11 @@ public class SuggestedWords { public final boolean mIsApplicationSpecifiedCompletions; public final boolean mTypedWordValid; public final boolean mHasMinimalSuggestion; - public final Object[] mDebugInfo; + public final List mSuggestedWordInfoList; private SuggestedWords(List words, boolean isApplicationSpecifiedCompletions, - boolean typedWordValid, boolean hasMinamlSuggestion, Object[] debugInfo) { + boolean typedWordValid, boolean hasMinamlSuggestion, + List suggestedWordInfoList) { if (words != null) { mWords = words; } else { @@ -41,7 +42,7 @@ public class SuggestedWords { mIsApplicationSpecifiedCompletions = isApplicationSpecifiedCompletions; mTypedWordValid = typedWordValid; mHasMinimalSuggestion = hasMinamlSuggestion; - mDebugInfo = debugInfo; + mSuggestedWordInfoList = suggestedWordInfoList; } public int size() { @@ -61,38 +62,46 @@ public class SuggestedWords { } public static class Builder { - private List mWords; + private List mWords = new ArrayList(); private boolean mIsCompletions; private boolean mTypedWordValid; private boolean mHasMinimalSuggestion; - private Object[] mDebugInfo; + private List mSuggestedWordInfoList = + new ArrayList(); public Builder() { // Nothing to do here. } - public Builder addWords(List words) { - for (final CharSequence word : words) - addWord(word); - 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); + public Builder addWords(List words, + List suggestedWordInfoList) { + final int N = words.size(); + for (int i = 0; i < N; ++i) { + SuggestedWordInfo suggestedWordInfo = null; + if (suggestedWordInfoList != null) { + suggestedWordInfo = suggestedWordInfoList.get(i); + } + if (suggestedWordInfo == null) { + suggestedWordInfo = new SuggestedWordInfo(); + } + addWord(words.get(i), suggestedWordInfo); + } return this; } public Builder addWord(CharSequence word) { - if (mWords == null) - mWords = new ArrayList(); + return addWord(word, null, false); + } + + public Builder addWord(CharSequence word, CharSequence debugString, + boolean isPreviousSuggestedWord) { + SuggestedWordInfo info = new SuggestedWordInfo(debugString, isPreviousSuggestedWord); + return addWord(word, info); + } + + private Builder addWord(CharSequence word, SuggestedWordInfo suggestedWordInfo) { mWords.add(word); + mSuggestedWordInfoList.add(suggestedWordInfo); return this; } @@ -117,11 +126,12 @@ public class SuggestedWords { // and replace it with what the user currently typed. public Builder addTypedWordAndPreviousSuggestions(CharSequence typedWord, SuggestedWords previousSuggestions) { - if (mWords != null) mWords.clear(); - addWord(typedWord); + mWords.clear(); + mSuggestedWordInfoList.clear(); + addWord(typedWord, null, false); final int previousSize = previousSuggestions.size(); for (int pos = 1; pos < previousSize; pos++) - addWord(previousSuggestions.getWord(pos)); + addWord(previousSuggestions.getWord(pos), null, true); mIsCompletions = false; mTypedWordValid = false; mHasMinimalSuggestion = false; @@ -130,15 +140,42 @@ public class SuggestedWords { public SuggestedWords build() { return new SuggestedWords(mWords, mIsCompletions, mTypedWordValid, - mHasMinimalSuggestion, mDebugInfo); + mHasMinimalSuggestion, mSuggestedWordInfoList); } public int size() { - return mWords == null ? 0 : mWords.size(); + return mWords.size(); } public CharSequence getWord(int pos) { return mWords.get(pos); } } + + public static class SuggestedWordInfo { + private final CharSequence mDebugString; + private final boolean mPreviousSuggestedWord; + + public SuggestedWordInfo() { + mDebugString = ""; + mPreviousSuggestedWord = false; + } + + public SuggestedWordInfo(CharSequence debugString, boolean previousSuggestedWord) { + mDebugString = debugString; + mPreviousSuggestedWord = previousSuggestedWord; + } + + public String getDebugString() { + if (mDebugString == null) { + return ""; + } else { + return mDebugString.toString(); + } + } + + public boolean isPreviousSuggestedWord () { + return mPreviousSuggestedWord; + } + } } diff --git a/java/src/com/android/inputmethod/voice/VoiceIMEConnector.java b/java/src/com/android/inputmethod/voice/VoiceIMEConnector.java index a02dbcb55..6c9b7d527 100644 --- a/java/src/com/android/inputmethod/voice/VoiceIMEConnector.java +++ b/java/src/com/android/inputmethod/voice/VoiceIMEConnector.java @@ -437,7 +437,7 @@ public class VoiceIMEConnector implements VoiceInput.UiListener { builder.addWord(word); } } else { - builder.addWords(suggestions); + builder.addWords(suggestions, null); } builder.setTypedWordValid(true).setHasMinimalSuggestion(true); mService.setSuggestions(builder.build());