diff options
Diffstat (limited to 'java/src')
-rw-r--r-- | java/src/com/android/inputmethod/latin/CandidateView.java | 27 | ||||
-rw-r--r-- | java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java | 32 |
2 files changed, 38 insertions, 21 deletions
diff --git a/java/src/com/android/inputmethod/latin/CandidateView.java b/java/src/com/android/inputmethod/latin/CandidateView.java index f499bc0bb..d46b4b5b5 100644 --- a/java/src/com/android/inputmethod/latin/CandidateView.java +++ b/java/src/com/android/inputmethod/latin/CandidateView.java @@ -272,9 +272,10 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo private static final int AUTO_CORRECT_BOLD = 0x01; private static final int AUTO_CORRECT_UNDERLINE = 0x02; private static final int AUTO_CORRECT_INVERT = 0x04; + private static final int VALID_TYPED_WORD_BOLD = 0x08; private final TextPaint mPaint; - private final int mAutoCorrectHighlight; + private final int mSuggestionStripOption; private final ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(); @@ -285,7 +286,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo super(words, dividers, infos); final TypedArray a = context.obtainStyledAttributes( attrs, R.styleable.CandidateView, defStyle, R.style.CandidateViewStyle); - mAutoCorrectHighlight = a.getInt(R.styleable.CandidateView_autoCorrectHighlight, 0); + mSuggestionStripOption = a.getInt(R.styleable.CandidateView_suggestionStripOption, 0); mColorTypedWord = a.getColor(R.styleable.CandidateView_colorTypedWord, 0); mColorAutoCorrect = a.getColor(R.styleable.CandidateView_colorAutoCorrect, 0); mColorSuggestedCandidate = a.getColor(R.styleable.CandidateView_colorSuggested, 0); @@ -313,15 +314,23 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo return mColorTypedWord; } - private CharSequence getStyledCandidateWord(CharSequence word, boolean isAutoCorrect) { - if (!isAutoCorrect) + private CharSequence getStyledCandidateWord(SuggestedWords suggestions, int pos) { + final CharSequence word = suggestions.getWord(pos); + final boolean isAutoCorrect = pos == 1 && willAutoCorrect(suggestions); + final boolean isTypedWordValid = pos == 0 && suggestions.mTypedWordValid; + if (!isAutoCorrect && !isTypedWordValid) return word; + final int len = word.length(); final Spannable spannedWord = new SpannableString(word); - if ((mAutoCorrectHighlight & AUTO_CORRECT_BOLD) != 0) + final int option = mSuggestionStripOption; + if ((isAutoCorrect && (option & AUTO_CORRECT_BOLD) != 0) + || (isTypedWordValid && (option & VALID_TYPED_WORD_BOLD) != 0)) { spannedWord.setSpan(BOLD_SPAN, 0, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); - if ((mAutoCorrectHighlight & AUTO_CORRECT_UNDERLINE) != 0) + } + if (isAutoCorrect && (option & AUTO_CORRECT_UNDERLINE) != 0) { spannedWord.setSpan(UNDERLINE_SPAN, 0, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); + } return spannedWord; } @@ -370,7 +379,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo } public CharSequence getInvertedText(CharSequence text) { - if ((mAutoCorrectHighlight & AUTO_CORRECT_INVERT) == 0) + if ((mSuggestionStripOption & AUTO_CORRECT_INVERT) == 0) return null; final int len = text.length(); final Spannable word = new SpannableString(text); @@ -457,9 +466,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo mTexts.clear(); final int count = Math.min(suggestions.size(), countInStrip); for (int pos = 0; pos < count; pos++) { - final CharSequence word = suggestions.getWord(pos); - final boolean isAutoCorrect = pos == 1 && willAutoCorrect(suggestions); - final CharSequence styled = getStyledCandidateWord(word, isAutoCorrect); + final CharSequence styled = getStyledCandidateWord(suggestions, pos); mTexts.add(styled); } for (int pos = count; pos < countInStrip; pos++) { diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java index 21176240f..502ebb52a 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -37,6 +37,7 @@ import com.android.inputmethod.latin.UserDictionary; import com.android.inputmethod.latin.Utils; import com.android.inputmethod.latin.WordComposer; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Locale; @@ -51,8 +52,9 @@ public class AndroidSpellCheckerService extends SpellCheckerService { private static final boolean DBG = false; private static final int POOL_SIZE = 2; + private final static String[] EMPTY_STRING_ARRAY = new String[0]; private final static SuggestionsInfo EMPTY_SUGGESTIONS_INFO = - new SuggestionsInfo(0, new String[0]); + new SuggestionsInfo(0, EMPTY_STRING_ARRAY); private Map<String, DictionaryPool> mDictionaryPools = Collections.synchronizedMap(new TreeMap<String, DictionaryPool>()); private Map<String, Dictionary> mUserDictionaries = @@ -65,14 +67,15 @@ public class AndroidSpellCheckerService extends SpellCheckerService { private static class SuggestionsGatherer implements WordCallback { private final int DEFAULT_SUGGESTION_LENGTH = 16; - private final String[] mSuggestions; + private final ArrayList<CharSequence> mSuggestions; private final int[] mScores; private final int mMaxLength; private int mLength = 0; + private boolean mSeenSuggestions = false; SuggestionsGatherer(final int maxLength) { mMaxLength = maxLength; - mSuggestions = new String[mMaxLength]; + mSuggestions = new ArrayList<CharSequence>(maxLength + 1); mScores = new int[mMaxLength]; } @@ -84,30 +87,37 @@ public class AndroidSpellCheckerService extends SpellCheckerService { // if it doesn't. See documentation for binarySearch. final int insertIndex = positionIndex >= 0 ? positionIndex : -positionIndex - 1; + mSeenSuggestions = true; if (mLength < mMaxLength) { final int copyLen = mLength - insertIndex; ++mLength; System.arraycopy(mScores, insertIndex, mScores, insertIndex + 1, copyLen); - System.arraycopy(mSuggestions, insertIndex, mSuggestions, insertIndex + 1, copyLen); + mSuggestions.add(insertIndex, new String(word, wordOffset, wordLength)); } else { if (insertIndex == 0) return true; System.arraycopy(mScores, 1, mScores, 0, insertIndex); - System.arraycopy(mSuggestions, 1, mSuggestions, 0, insertIndex); + mSuggestions.add(insertIndex, new String(word, wordOffset, wordLength)); + mSuggestions.remove(0); } mScores[insertIndex] = score; - mSuggestions[insertIndex] = new String(word, wordOffset, wordLength); return true; } public String[] getGatheredSuggestions() { - if (0 == mLength) return null; + if (!mSeenSuggestions) return null; + if (0 == mLength) return EMPTY_STRING_ARRAY; - final String[] results = new String[mLength]; - for (int i = mLength - 1; i >= 0; --i) { - results[mLength - i - 1] = mSuggestions[i]; + if (DBG) { + if (mLength != mSuggestions.size()) { + Log.e(TAG, "Suggestion size is not the same as stored mLength"); + } } - return results; + Collections.reverse(mSuggestions); + Utils.removeDupes(mSuggestions); + // This returns a String[], while toArray() returns an Object[] which cannot be cast + // into a String[]. + return mSuggestions.toArray(EMPTY_STRING_ARRAY); } } |