diff options
Diffstat (limited to 'java/src')
15 files changed, 376 insertions, 318 deletions
diff --git a/java/src/com/android/inputmethod/deprecated/recorrection/RecorrectionSuggestionEntries.java b/java/src/com/android/inputmethod/deprecated/recorrection/RecorrectionSuggestionEntries.java index 5e6c87044..f33a46277 100644 --- a/java/src/com/android/inputmethod/deprecated/recorrection/RecorrectionSuggestionEntries.java +++ b/java/src/com/android/inputmethod/deprecated/recorrection/RecorrectionSuggestionEntries.java @@ -57,6 +57,7 @@ public class RecorrectionSuggestionEntries { private static SuggestedWords.Builder getTypedSuggestions( Suggest suggest, KeyboardSwitcher keyboardSwitcher, WordComposer word) { - return suggest.getSuggestedWordBuilder(keyboardSwitcher.getKeyboardView(), word, null); + return suggest.getSuggestedWordBuilder(keyboardSwitcher.getKeyboardView(), word, null, + keyboardSwitcher.getLatinKeyboard().getProximityInfo()); } } diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java index d5e75d01b..518bc8e9e 100644 --- a/java/src/com/android/inputmethod/keyboard/Keyboard.java +++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java @@ -149,8 +149,8 @@ public class Keyboard { mMostCommonKeyWidth, mKeys); } - public int getProximityInfo() { - return mProximityInfo.getNativeProximityInfo(); + public ProximityInfo getProximityInfo() { + return mProximityInfo; } public boolean hasShiftLockKey() { diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java index 0779964af..811470c26 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java @@ -187,13 +187,16 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha } public void save() { + if (mCurrentId == null) { + return; + } mIsAlphabetMode = isAlphabetMode(); if (mIsAlphabetMode) { mIsShiftLocked = isShiftLocked(); mIsShifted = !mIsShiftLocked && isShiftedOrShiftLocked(); } else { mIsShiftLocked = false; - mIsShifted = mSymbolsShiftedKeyboardId.equals(mCurrentId); + mIsShifted = mCurrentId.equals(mSymbolsShiftedKeyboardId); } mIsValid = true; } diff --git a/java/src/com/android/inputmethod/keyboard/ProximityInfo.java b/java/src/com/android/inputmethod/keyboard/ProximityInfo.java index aadedc69d..07dae168f 100644 --- a/java/src/com/android/inputmethod/keyboard/ProximityInfo.java +++ b/java/src/com/android/inputmethod/keyboard/ProximityInfo.java @@ -19,6 +19,7 @@ package com.android.inputmethod.keyboard; import com.android.inputmethod.latin.Utils; import java.util.Arrays; +import java.util.Collections; import java.util.List; public class ProximityInfo { @@ -54,6 +55,10 @@ public class ProximityInfo { computeNearestNeighbors(keyWidth, keys); } + public static ProximityInfo getDummyProximityInfo() { + return new ProximityInfo(1, 1, 1, 1, 1, Collections.<Key>emptyList()); + } + private int mNativeProximityInfo; static { Utils.loadNativeLibrary(); diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index 9748d6006..6a6a0a4ee 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -156,10 +156,11 @@ public class BinaryDictionary extends Dictionary { } } + // proximityInfo may not be null. @Override - public void getWords(final WordComposer codes, final WordCallback callback) { - final int count = getSuggestions(codes, mKeyboardSwitcher.getLatinKeyboard(), - mOutputChars, mScores); + public void getWords(final WordComposer codes, final WordCallback callback, + final ProximityInfo proximityInfo) { + final int count = getSuggestions(codes, proximityInfo, mOutputChars, mScores); for (int j = 0; j < count; ++j) { if (mScores[j] < 1) break; @@ -179,8 +180,9 @@ public class BinaryDictionary extends Dictionary { return mNativeDict != 0; } - /* package for test */ int getSuggestions(final WordComposer codes, final Keyboard keyboard, - char[] outputChars, int[] scores) { + // proximityInfo may not be null. + /* package for test */ int getSuggestions(final WordComposer codes, + final ProximityInfo proximityInfo, char[] outputChars, int[] scores) { if (!isValidDictionary()) return -1; final int codesSize = codes.size(); @@ -196,9 +198,8 @@ public class BinaryDictionary extends Dictionary { Arrays.fill(outputChars, (char) 0); Arrays.fill(scores, 0); - final int proximityInfo = keyboard == null ? 0 : keyboard.getProximityInfo(); return getSuggestionsNative( - mNativeDict, proximityInfo, + mNativeDict, proximityInfo.getNativeProximityInfo(), codes.getXCoordinates(), codes.getYCoordinates(), mInputCodes, codesSize, mFlags, outputChars, scores); } diff --git a/java/src/com/android/inputmethod/latin/CandidateView.java b/java/src/com/android/inputmethod/latin/CandidateView.java index 9b39e36a0..915e73ccb 100644 --- a/java/src/com/android/inputmethod/latin/CandidateView.java +++ b/java/src/com/android/inputmethod/latin/CandidateView.java @@ -76,8 +76,6 @@ public class CandidateView extends LinearLayout implements OnClickListener { private final ArrayList<TextView> mInfos = new ArrayList<TextView>(); private final ArrayList<View> mDividers = new ArrayList<View>(); - private final int mCandidateStripHeight; - private final PopupWindow mPreviewPopup; private final TextView mPreviewText; @@ -149,19 +147,113 @@ public class CandidateView extends LinearLayout implements OnClickListener { public final int mDividerWidth; public final int mDividerHeight; public final int mControlWidth; + public final int mCandidateStripHeight; + + protected final List<TextView> mWords; + protected final List<View> mDividers; + protected final List<TextView> mInfos; - protected CandidateViewParams(TextView word, View divider, View control) { + protected CandidateViewParams(List<TextView> words, List<View> dividers, + List<TextView> infos, View control) { + mWords = words; + mDividers = dividers; + mInfos = infos; + + final TextView word = words.get(0); + final View divider = dividers.get(0); mPadding = word.getCompoundPaddingLeft() + word.getCompoundPaddingRight(); divider.measure(WRAP_CONTENT, MATCH_PARENT); mDividerWidth = divider.getMeasuredWidth(); mDividerHeight = divider.getMeasuredHeight(); mControlWidth = control.getMeasuredWidth(); + + final Resources res = word.getResources(); + mCandidateStripHeight = res.getDimensionPixelOffset(R.dimen.candidate_strip_height); } } private static class SuggestionsPaneParams extends CandidateViewParams { - public SuggestionsPaneParams(List<TextView> words, List<View> dividers, View control) { - super(words.get(0), dividers.get(0), control); + public SuggestionsPaneParams(List<TextView> words, List<View> dividers, + List<TextView> infos, View control) { + super(words, dividers, infos, control); + } + + public int layout(SuggestedWords suggestions, ViewGroup paneView, int from, int textColor, + int paneWidth) { + final int count = Math.min(mWords.size(), suggestions.size()); + View centeringFrom = null, lastView = null; + int x = 0, y = 0; + for (int index = from; index < count; index++) { + final int pos = index; + final TextView word = mWords.get(pos); + final View divider = mDividers.get(pos); + final TextPaint paint = word.getPaint(); + word.setTextColor(textColor); + final CharSequence styled = suggestions.getWord(pos); + + final TextView info; + if (DBG) { + final CharSequence debugInfo = getDebugInfo(suggestions, index); + if (debugInfo != null) { + info = mInfos.get(index); + info.setText(debugInfo); + } else { + info = null; + } + } else { + info = null; + } + + final CharSequence text; + final float scaleX; + paint.setTextScaleX(1.0f); + final int textWidth = getTextWidth(styled, paint); + int available = paneWidth - x - mPadding; + if (textWidth >= available) { + // Needs new row, centering previous row. + centeringCandidates(paneView, centeringFrom, lastView, x, paneWidth); + x = 0; + y += mCandidateStripHeight; + } + if (x != 0) { + // Add divider if this isn't the left most suggestion in current row. + paneView.addView(divider); + FrameLayoutCompatUtils.placeViewAt(divider, x, y + + (mCandidateStripHeight - mDividerHeight) / 2, mDividerWidth, + mDividerHeight); + x += mDividerWidth; + } + available = paneWidth - x - mPadding; + text = getEllipsizedText(styled, available, paint); + scaleX = paint.getTextScaleX(); + word.setText(text); + word.setTextScaleX(scaleX); + paneView.addView(word); + lastView = word; + if (x == 0) + centeringFrom = word; + word.measure(WRAP_CONTENT, + MeasureSpec.makeMeasureSpec(mCandidateStripHeight, MeasureSpec.EXACTLY)); + final int width = word.getMeasuredWidth(); + final int height = word.getMeasuredHeight(); + FrameLayoutCompatUtils.placeViewAt(word, x, y + (mCandidateStripHeight - height) + / 2, width, height); + x += width; + if (info != null) { + paneView.addView(info); + lastView = info; + info.measure(WRAP_CONTENT, WRAP_CONTENT); + final int infoWidth = info.getMeasuredWidth(); + FrameLayoutCompatUtils.placeViewAt(info, x - infoWidth, y, infoWidth, + info.getMeasuredHeight()); + } + } + if (x != 0) { + // Centering last candidates row. + centeringCandidates(paneView, centeringFrom, lastView, x, paneWidth); + } + + return count - from; } } @@ -182,25 +274,25 @@ public class CandidateView extends LinearLayout implements OnClickListener { private static final int AUTO_CORRECT_UNDERLINE = 0x02; private static final int AUTO_CORRECT_INVERT = 0x04; - public final TextPaint mPaint; + private final TextPaint mPaint; private final int mAutoCorrectHighlight; private final ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(); private SuggestedWords mSuggestedWords; - public int mCountInStrip; + private int mCountInStrip; // True if the mCountInStrip suggestions can fit in suggestion strip in equally divided // width without squeezing the text. - public boolean mCanUseFixedWidthColumns; - public int mMaxWidth; - public int mAvailableWidthForWords; - public int mConstantWidthForPaddings; - public int mVariableWidthForWords; - public float mScaleX; + private boolean mCanUseFixedWidthColumns; + private int mMaxWidth; + private int mAvailableWidthForWords; + private int mConstantWidthForPaddings; + private int mVariableWidthForWords; + private float mScaleX; public SuggestionsStripParams(Context context, AttributeSet attrs, int defStyle, - List<TextView> words, List<View> dividers, View control) { - super(words.get(0), dividers.get(0), control); + List<TextView> words, List<View> dividers, List<TextView> infos, View control) { + super(words, dividers, infos, control); final TypedArray a = context.obtainStyledAttributes( attrs, R.styleable.CandidateView, defStyle, R.style.CandidateViewStyle); mAutoCorrectHighlight = a.getInt(R.styleable.CandidateView_autoCorrectHighlight, 0); @@ -220,24 +312,11 @@ public class CandidateView extends LinearLayout implements OnClickListener { mPaint.setTextSize(textSize); } - public CharSequence getWord(int pos) { - return mTexts.get(pos); + public int getTextColor() { + return mColorTypedWord; } - public CharSequence getDebugInfo(int pos) { - if (DBG) { - final SuggestedWordInfo wordInfo = mSuggestedWords.getInfo(pos); - if (wordInfo != null) { - final CharSequence debugInfo = wordInfo.getDebugString(); - if (!TextUtils.isEmpty(debugInfo)) { - return debugInfo; - } - } - } - return null; - } - - public CharSequence getStyledCandidateWord(CharSequence word, boolean isAutoCorrect) { + private CharSequence getStyledCandidateWord(CharSequence word, boolean isAutoCorrect) { if (!isAutoCorrect) return word; final int len = word.length(); @@ -249,7 +328,7 @@ public class CandidateView extends LinearLayout implements OnClickListener { return spannedWord; } - public int getWordPosition(int index) { + private int getWordPosition(int index) { if (index >= 2) { return index; } @@ -258,7 +337,7 @@ public class CandidateView extends LinearLayout implements OnClickListener { return willAutoCorrect ? 1 - index : index; } - public int getCandidateTextColor(int pos) { + private int getCandidateTextColor(int pos) { final SuggestedWords suggestions = mSuggestedWords; final boolean isAutoCorrect = suggestions.mHasMinimalSuggestion && ((pos == 1 && !suggestions.mTypedWordValid) @@ -300,7 +379,8 @@ public class CandidateView extends LinearLayout implements OnClickListener { return word; } - public void layoutStrip(SuggestedWords suggestions, int maxWidth) { + public int layout(SuggestedWords suggestions, ViewGroup stripView, ViewGroup paneView, + int stripWidth) { mSuggestedWords = suggestions; final int maxCount = suggestions.isPunctuationSuggestions() ? PUNCTUATIONS_IN_STRIP : mCandidateCountInStrip; @@ -308,7 +388,68 @@ public class CandidateView extends LinearLayout implements OnClickListener { setupTexts(suggestions, size); mCountInStrip = Math.min(maxCount, size); mScaleX = 1.0f; + calculateParameters(size, stripWidth); + + int infoX = 0; + for (int index = 0; index < mCountInStrip; index++) { + final int pos = getWordPosition(index); + final TextView word = mWords.get(pos); + final View divider = mDividers.get(pos); + final TextPaint paint = word.getPaint(); + // TODO: Reorder candidates in strip as appropriate. The center candidate should + // hold the word when space is typed (valid typed word or auto corrected word). + word.setTextColor(getCandidateTextColor(pos)); + final CharSequence styled = mTexts.get(pos); + + final TextView info; + if (DBG) { + final CharSequence debugInfo = getDebugInfo(mSuggestedWords, index); + if (debugInfo != null) { + info = mInfos.get(index); + info.setText(debugInfo); + } else { + info = null; + } + } else { + info = null; + } + final CharSequence text; + final float scaleX; + if (index == 0 && mCountInStrip == 1) { + text = getEllipsizedText(styled, mMaxWidth, paint); + scaleX = paint.getTextScaleX(); + } else { + text = styled; + scaleX = mScaleX; + } + word.setText(text); + word.setTextScaleX(scaleX); + if (index != 0) { + // Add divider if this isn't the left most suggestion in candidate strip. + stripView.addView(divider); + } + stripView.addView(word); + if (mCanUseFixedWidthColumns) { + setLayoutWeight(word, 1.0f, mCandidateStripHeight); + } else { + final int width = getTextWidth(text, paint) + mPadding; + setLayoutWeight(word, width, mCandidateStripHeight); + } + if (info != null) { + paneView.addView(info); + info.measure(WRAP_CONTENT, WRAP_CONTENT); + final int width = info.getMeasuredWidth(); + final int y = info.getMeasuredHeight(); + FrameLayoutCompatUtils.placeViewAt(info, infoX, 0, width, y); + infoX += width * 2; + } + } + + return mCountInStrip; + } + + private void calculateParameters(int size, int maxWidth) { do { mMaxWidth = maxWidth; if (size > mCountInStrip) { @@ -334,7 +475,7 @@ public class CandidateView extends LinearLayout implements OnClickListener { } while (mCountInStrip > 1); } - public void tryLayout() { + private void tryLayout() { final int maxCount = mCountInStrip; final int dividers = mDividerWidth * (maxCount - 1); mConstantWidthForPaddings = dividers + mPadding * maxCount; @@ -396,8 +537,7 @@ public class CandidateView extends LinearLayout implements OnClickListener { setBackgroundDrawable(LinearLayoutCompatUtils.getBackgroundDrawable( context, attrs, defStyle, R.style.CandidateViewStyle)); - Resources res = context.getResources(); - LayoutInflater inflater = LayoutInflater.from(context); + final LayoutInflater inflater = LayoutInflater.from(context); inflater.inflate(R.layout.candidates_strip, this); mPreviewPopup = new PopupWindow(context); @@ -408,7 +548,6 @@ public class CandidateView extends LinearLayout implements OnClickListener { mPreviewPopup.setBackgroundDrawable(null); mCandidatesStrip = (ViewGroup)findViewById(R.id.candidates_strip); - mCandidateStripHeight = res.getDimensionPixelOffset(R.dimen.candidate_strip_height); for (int i = 0; i < MAX_SUGGESTIONS; i++) { final TextView word = (TextView)inflater.inflate(R.layout.candidate_word, null); word.setTag(i); @@ -457,8 +596,9 @@ public class CandidateView extends LinearLayout implements OnClickListener { mCandidatesPaneControl.measure(WRAP_CONTENT, WRAP_CONTENT); mStripParams = new SuggestionsStripParams(context, attrs, defStyle, - mWords, mDividers, mCandidatesPaneControl); - mPaneParams = new SuggestionsPaneParams(mWords, mDividers, mCandidatesPaneControl); + mWords, mDividers, mInfos, mCandidatesPaneControl); + mPaneParams = new SuggestionsPaneParams( + mWords, mDividers, mInfos, mCandidatesPaneControl); } /** @@ -490,128 +630,35 @@ public class CandidateView extends LinearLayout implements OnClickListener { private void updateSuggestions() { clear(); closeCandidatesPane(); - final SuggestedWords suggestions = mSuggestions; - if (suggestions.size() == 0) + if (mSuggestions.size() == 0) return; - final int paneWidth = getWidth(); - final SuggestionsStripParams stripParams = mStripParams; - final SuggestionsPaneParams paneParams = mPaneParams; - stripParams.layoutStrip(suggestions, paneWidth); + final int width = getWidth(); + final int countInStrip = mStripParams.layout( + mSuggestions, mCandidatesStrip, mCandidatesPane, width); + final int countInPane = mPaneParams.layout( + mSuggestions, mCandidatesPane, countInStrip, mStripParams.getTextColor(), width); - final int count = Math.min(mWords.size(), suggestions.size()); - if (count <= stripParams.mCountInStrip && !DBG) { + if (countInPane <= 0 && !DBG) { mCandidatesPaneControl.setVisibility(GONE); } else { mCandidatesPaneControl.setVisibility(VISIBLE); mExpandCandidatesPane.setVisibility(VISIBLE); mExpandCandidatesPane.setEnabled(true); } + } - final int countInStrip = stripParams.mCountInStrip; - View centeringFrom = null, lastView = null; - int x = 0, y = 0, infoX = 0; - for (int index = 0; index < count; index++) { - final int pos = stripParams.getWordPosition(index); - final TextView word = mWords.get(pos); - final View divider = mDividers.get(pos); - final TextPaint paint = word.getPaint(); - // TODO: Reorder candidates in strip as appropriate. The center candidate should hold - // the word when space is typed (valid typed word or auto corrected word). - word.setTextColor(stripParams.getCandidateTextColor(pos)); - final CharSequence styled = stripParams.getWord(pos); - - final TextView info; - if (DBG) { - final CharSequence debugInfo = stripParams.getDebugInfo(index); - if (debugInfo != null) { - info = mInfos.get(index); - info.setText(debugInfo); - } else { - info = null; - } - } else { - info = null; - } - - final CharSequence text; - final float scaleX; - if (index < countInStrip) { - if (index == 0 && stripParams.mCountInStrip == 1) { - text = getEllipsizedText(styled, stripParams.mMaxWidth, paint); - scaleX = paint.getTextScaleX(); - } else { - text = styled; - scaleX = stripParams.mScaleX; - } - word.setText(text); - word.setTextScaleX(scaleX); - if (index != 0) { - // Add divider if this isn't the left most suggestion in candidate strip. - mCandidatesStrip.addView(divider); - } - mCandidatesStrip.addView(word); - if (stripParams.mCanUseFixedWidthColumns) { - setLayoutWeight(word, 1.0f, mCandidateStripHeight); - } else { - final int width = getTextWidth(text, paint) + stripParams.mPadding; - setLayoutWeight(word, width, mCandidateStripHeight); - } - if (info != null) { - mCandidatesPane.addView(info); - info.measure(WRAP_CONTENT, WRAP_CONTENT); - final int width = info.getMeasuredWidth(); - y = info.getMeasuredHeight(); - FrameLayoutCompatUtils.placeViewAt(info, infoX, 0, width, y); - infoX += width * 2; - } - } else { - paint.setTextScaleX(1.0f); - final int textWidth = getTextWidth(styled, paint); - int available = paneWidth - x - paneParams.mPadding; - if (textWidth >= available) { - // Needs new row, centering previous row. - centeringCandidates(centeringFrom, lastView, x, paneWidth); - x = 0; - y += mCandidateStripHeight; - } - if (x != 0) { - // Add divider if this isn't the left most suggestion in current row. - mCandidatesPane.addView(divider); - FrameLayoutCompatUtils.placeViewAt( - divider, x, y + (mCandidateStripHeight - paneParams.mDividerHeight) / 2, - paneParams.mDividerWidth, paneParams.mDividerHeight); - x += paneParams.mDividerWidth; - } - available = paneWidth - x - paneParams.mPadding; - text = getEllipsizedText(styled, available, paint); - scaleX = paint.getTextScaleX(); - word.setText(text); - word.setTextScaleX(scaleX); - mCandidatesPane.addView(word); - lastView = word; - if (x == 0) centeringFrom = word; - word.measure(WRAP_CONTENT, - MeasureSpec.makeMeasureSpec(mCandidateStripHeight, MeasureSpec.EXACTLY)); - final int width = word.getMeasuredWidth(); - final int height = word.getMeasuredHeight(); - FrameLayoutCompatUtils.placeViewAt( - word, x, y + (mCandidateStripHeight - height) / 2, width, height); - x += width; - if (info != null) { - mCandidatesPane.addView(info); - lastView = info; - info.measure(WRAP_CONTENT, WRAP_CONTENT); - final int infoWidth = info.getMeasuredWidth(); - FrameLayoutCompatUtils.placeViewAt( - info, x - infoWidth, y, infoWidth, info.getMeasuredHeight()); + private static CharSequence getDebugInfo(SuggestedWords suggestions, int pos) { + if (DBG) { + final SuggestedWordInfo wordInfo = suggestions.getInfo(pos); + if (wordInfo != null) { + final CharSequence debugInfo = wordInfo.getDebugString(); + if (!TextUtils.isEmpty(debugInfo)) { + return debugInfo; } } } - if (x != 0) { - // Centering last candidates row. - centeringCandidates(centeringFrom, lastView, x, paneWidth); - } + return null; } private static void setLayoutWeight(View v, float weight, int height) { @@ -624,13 +671,13 @@ public class CandidateView extends LinearLayout implements OnClickListener { } } - private void centeringCandidates(View from, View to, int width, int paneWidth) { - final ViewGroup pane = mCandidatesPane; - final int fromIndex = pane.indexOfChild(from); - final int toIndex = pane.indexOfChild(to); - final int offset = (paneWidth - width) / 2; + private static void centeringCandidates(ViewGroup parent, View from, View to, int width, + int parentWidth) { + final int fromIndex = parent.indexOfChild(from); + final int toIndex = parent.indexOfChild(to); + final int offset = (parentWidth - width) / 2; for (int index = fromIndex; index <= toIndex; index++) { - offsetMargin(pane.getChildAt(index), offset, 0); + offsetMargin(parent.getChildAt(index), offset, 0); } } diff --git a/java/src/com/android/inputmethod/latin/Dictionary.java b/java/src/com/android/inputmethod/latin/Dictionary.java index c7737b9a2..c35b42877 100644 --- a/java/src/com/android/inputmethod/latin/Dictionary.java +++ b/java/src/com/android/inputmethod/latin/Dictionary.java @@ -1,12 +1,12 @@ /* * Copyright (C) 2008 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 @@ -16,6 +16,8 @@ package com.android.inputmethod.latin; +import com.android.inputmethod.keyboard.ProximityInfo; + /** * Abstract base class for a dictionary that can do a fuzzy search for words based on a set of key * strokes. @@ -25,7 +27,7 @@ public abstract class Dictionary { * Whether or not to replicate the typed word in the suggested list, even if it's valid. */ protected static final boolean INCLUDE_TYPED_WORD_IF_VALID = false; - + /** * The weight to give to a word if it's length is the same as the number of typed characters. */ @@ -57,13 +59,15 @@ public abstract class Dictionary { } /** - * Searches for words in the dictionary that match the characters in the composer. Matched + * Searches for words in the dictionary that match the characters in the composer. Matched * words are added through the callback object. * @param composer the key sequence to match * @param callback the callback object to send matched words to as possible candidates + * @param proximityInfo the object for key proximity. May be ignored by some implementations. * @see WordCallback#addWord(char[], int, int, int, int, DataType) */ - abstract public void getWords(final WordComposer composer, final WordCallback callback); + abstract public void getWords(final WordComposer composer, final WordCallback callback, + final ProximityInfo proximityInfo); /** * Searches for pairs in the bigram dictionary that matches the previous word and all the @@ -83,7 +87,7 @@ public abstract class Dictionary { * @return true if the word exists, false otherwise */ abstract public boolean isValidWord(CharSequence word); - + /** * Compares the contents of the character array with the typed word and returns true if they * are the same. diff --git a/java/src/com/android/inputmethod/latin/DictionaryCollection.java b/java/src/com/android/inputmethod/latin/DictionaryCollection.java index 107840331..739153044 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryCollection.java +++ b/java/src/com/android/inputmethod/latin/DictionaryCollection.java @@ -16,6 +16,8 @@ package com.android.inputmethod.latin; +import com.android.inputmethod.keyboard.ProximityInfo; + import java.util.Collection; import java.util.Collections; import java.util.List; @@ -47,9 +49,10 @@ public class DictionaryCollection extends Dictionary { } @Override - public void getWords(final WordComposer composer, final WordCallback callback) { + public void getWords(final WordComposer composer, final WordCallback callback, + final ProximityInfo proximityInfo) { for (final Dictionary dict : mDictionaries) - dict.getWords(composer, callback); + dict.getWords(composer, callback, proximityInfo); } @Override diff --git a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java index 97a4a1816..35d1541ff 100644 --- a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java +++ b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java @@ -20,6 +20,7 @@ import android.content.Context; import android.os.AsyncTask; import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.keyboard.ProximityInfo; import java.util.LinkedList; @@ -193,7 +194,8 @@ public class ExpandableDictionary extends Dictionary { } @Override - public void getWords(final WordComposer codes, final WordCallback callback) { + public void getWords(final WordComposer codes, final WordCallback callback, + final ProximityInfo proximityInfo) { synchronized (mUpdatingLock) { // If we need to update, start off a background task if (mRequiresReload) startDictionaryLoadingTaskLocked(); diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 836c55a6e..6c91c454c 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -1600,7 +1600,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } // getSuggestedWordBuilder handles gracefully a null value of prevWord final SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder( - mKeyboardSwitcher.getKeyboardView(), wordComposer, prevWord); + mKeyboardSwitcher.getKeyboardView(), wordComposer, prevWord, + mKeyboardSwitcher.getLatinKeyboard().getProximityInfo()); boolean autoCorrectionAvailable = !mInputTypeNoAutoCorrect && mSuggest.hasAutoCorrection(); final CharSequence typedWord = wordComposer.getTypedWord(); @@ -1825,7 +1826,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar final CharSequence prevWord = EditingUtils.getThisWord(getCurrentInputConnection(), mSettingsValues.mWordSeparators); SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder( - mKeyboardSwitcher.getKeyboardView(), sEmptyWordComposer, prevWord); + mKeyboardSwitcher.getKeyboardView(), sEmptyWordComposer, prevWord, + mKeyboardSwitcher.getLatinKeyboard().getProximityInfo()); if (builder.size() > 0) { // Explicitly supply an empty typed word (the no-second-arg version of diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index c2452b947..933a94176 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -22,6 +22,8 @@ import android.text.TextUtils; import android.util.Log; import android.view.View; +import com.android.inputmethod.keyboard.ProximityInfo; + import java.io.File; import java.util.ArrayList; import java.util.Arrays; @@ -263,9 +265,10 @@ public class Suggest implements Dictionary.WordCallback { * @param prevWordForBigram previous word (used only for bigram) * @return suggested words object. */ - public SuggestedWords getSuggestions(View view, WordComposer wordComposer, - CharSequence prevWordForBigram) { - return getSuggestedWordBuilder(view, wordComposer, prevWordForBigram).build(); + public SuggestedWords getSuggestions(final View view, final WordComposer wordComposer, + final CharSequence prevWordForBigram, final ProximityInfo proximityInfo) { + return getSuggestedWordBuilder(view, wordComposer, prevWordForBigram, + proximityInfo).build(); } private CharSequence capitalizeWord(boolean all, boolean first, CharSequence word) { @@ -299,8 +302,9 @@ public class Suggest implements Dictionary.WordCallback { } // TODO: cleanup dictionaries looking up and suggestions building with SuggestedWords.Builder - public SuggestedWords.Builder getSuggestedWordBuilder(View view, WordComposer wordComposer, - CharSequence prevWordForBigram) { + public SuggestedWords.Builder getSuggestedWordBuilder(final View view, + final WordComposer wordComposer, CharSequence prevWordForBigram, + final ProximityInfo proximityInfo) { LatinImeLogger.onStartSuggestion(prevWordForBigram); mAutoCorrection.init(); mIsFirstCharCapitalized = wordComposer.isFirstCharCapitalized(); @@ -365,7 +369,7 @@ public class Suggest implements Dictionary.WordCallback { if (key.equals(DICT_KEY_USER_UNIGRAM) || key.equals(DICT_KEY_WHITELIST)) continue; final Dictionary dictionary = mUnigramDictionaries.get(key); - dictionary.getWords(wordComposer, this); + dictionary.getWords(wordComposer, this, proximityInfo); } } CharSequence autoText = null; diff --git a/java/src/com/android/inputmethod/latin/UserDictionary.java b/java/src/com/android/inputmethod/latin/UserDictionary.java index f93d24fe6..6608d8268 100644 --- a/java/src/com/android/inputmethod/latin/UserDictionary.java +++ b/java/src/com/android/inputmethod/latin/UserDictionary.java @@ -26,6 +26,8 @@ import android.net.Uri; import android.os.RemoteException; import android.provider.UserDictionary.Words; +import com.android.inputmethod.keyboard.ProximityInfo; + public class UserDictionary extends ExpandableDictionary { private static final String[] PROJECTION_QUERY = { @@ -150,8 +152,9 @@ public class UserDictionary extends ExpandableDictionary { } @Override - public synchronized void getWords(final WordComposer codes, final WordCallback callback) { - super.getWords(codes, callback); + public synchronized void getWords(final WordComposer codes, final WordCallback callback, + final ProximityInfo proximityInfo) { + super.getWords(codes, callback, proximityInfo); } @Override diff --git a/java/src/com/android/inputmethod/latin/WhitelistDictionary.java b/java/src/com/android/inputmethod/latin/WhitelistDictionary.java index 4377373d2..639c96681 100644 --- a/java/src/com/android/inputmethod/latin/WhitelistDictionary.java +++ b/java/src/com/android/inputmethod/latin/WhitelistDictionary.java @@ -21,6 +21,8 @@ import android.text.TextUtils; import android.util.Log; import android.util.Pair; +import com.android.inputmethod.keyboard.ProximityInfo; + import java.util.HashMap; public class WhitelistDictionary extends Dictionary { @@ -89,7 +91,8 @@ public class WhitelistDictionary extends Dictionary { // Not used for WhitelistDictionary. We use getWhitelistedWord() in Suggest.java instead @Override - public void getWords(WordComposer composer, WordCallback callback) { + public void getWords(final WordComposer composer, final WordCallback callback, + final ProximityInfo proximityInfo) { } @Override diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java index 4a822d7b0..848dcbed5 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -16,35 +16,130 @@ package com.android.inputmethod.latin.spellcheck; +import android.content.res.Resources; import android.service.textservice.SpellCheckerService; import android.util.Log; import android.view.textservice.SuggestionsInfo; import android.view.textservice.TextInfo; +import com.android.inputmethod.compat.ArraysCompatUtils; +import com.android.inputmethod.keyboard.Key; +import com.android.inputmethod.keyboard.ProximityInfo; +import com.android.inputmethod.latin.Dictionary; +import com.android.inputmethod.latin.Dictionary.DataType; +import com.android.inputmethod.latin.Dictionary.WordCallback; +import com.android.inputmethod.latin.DictionaryFactory; +import com.android.inputmethod.latin.Utils; +import com.android.inputmethod.latin.WordComposer; + +import java.util.Collections; +import java.util.List; +import java.util.LinkedList; +import java.util.Locale; +import java.util.Map; +import java.util.TreeMap; + /** * Service for spell checking, using LatinIME's dictionaries and mechanisms. */ public class AndroidSpellCheckerService extends SpellCheckerService { private static final String TAG = AndroidSpellCheckerService.class.getSimpleName(); private static final boolean DBG = true; + + private final static String[] emptyArray = new String[0]; + private final ProximityInfo mProximityInfo = ProximityInfo.getDummyProximityInfo(); + private final Map<String, Dictionary> mDictionaries = + Collections.synchronizedMap(new TreeMap<String, Dictionary>()); + + private static class SuggestionsGatherer implements WordCallback { + private final int DEFAULT_SUGGESTION_LENGTH = 16; + private final String[] mSuggestions; + private final int[] mScores; + private final int mMaxLength; + private int mLength = 0; + + SuggestionsGatherer(final int maxLength) { + mMaxLength = maxLength; + mSuggestions = new String[mMaxLength]; + mScores = new int[mMaxLength]; + } + + @Override + synchronized public boolean addWord(char[] word, int wordOffset, int wordLength, int score, + int dicTypeId, DataType dataType) { + final int positionIndex = ArraysCompatUtils.binarySearch(mScores, 0, mLength, score); + // binarySearch returns the index if the element exists, and -<insertion index> - 1 + // if it doesn't. See documentation for binarySearch. + final int insertIndex = positionIndex >= 0 ? positionIndex : -positionIndex - 1; + + 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); + } else { + if (insertIndex == 0) return true; + System.arraycopy(mScores, 1, mScores, 0, insertIndex); + System.arraycopy(mSuggestions, 1, mSuggestions, 0, insertIndex); + } + mScores[insertIndex] = score; + mSuggestions[insertIndex] = new String(word, wordOffset, wordLength); + + return true; + } + + public String[] getGatheredSuggestions() { + if (0 == mLength) return null; + + final String[] results = new String[mLength]; + for (int i = mLength - 1; i >= 0; --i) { + results[mLength - i - 1] = mSuggestions[i]; + } + return results; + } + } + + private Dictionary getDictionary(final String locale) { + Dictionary dictionary = mDictionaries.get(locale); + if (null == dictionary) { + final Resources resources = getResources(); + final int fallbackResourceId = Utils.getMainDictionaryResourceId(resources); + final Locale localeObject = Utils.constructLocaleFromString(locale); + dictionary = DictionaryFactory.createDictionaryFromManager(this, localeObject, + fallbackResourceId); + mDictionaries.put(locale, dictionary); + } + return dictionary; + } + + // Note : this must be reentrant + /** + * Gets a list of suggestions for a specific string. + * + * This returns a list of possible corrections for the text passed as an + * arguments. It may split or group words, and even perform grammatical + * analysis. + */ @Override - public SuggestionsInfo getSuggestions(TextInfo textInfo, int suggestionsLimit, - String locale) { - // TODO: implement this + public SuggestionsInfo getSuggestions(final TextInfo textInfo, final int suggestionsLimit, + final String locale) { + final Dictionary dictionary = getDictionary(locale); final String text = textInfo.getText(); - if (DBG) { - Log.w(TAG, "getSuggestions: " + text); - } - String[] candidates0 = new String[] {text, "candidate1", "candidate2", "candidate3"}; - String[] candidates1 = new String[] {text, "candidateA", "candidateB"}; - final int textLength = textInfo.getText().length() % 3; - if (textLength % 3 == 0) { - return new SuggestionsInfo(2 - | SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY, candidates0); - } else if (textLength % 3 == 1) { - return new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY, candidates1); - } else { - return new SuggestionsInfo(0, null); + + final SuggestionsGatherer suggestionsGatherer = new SuggestionsGatherer(suggestionsLimit); + final WordComposer composer = new WordComposer(); + final int length = text.length(); + for (int i = 0; i < length; ++i) { + int character = text.codePointAt(i); + composer.add(character, new int[] { character }, + WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); } + dictionary.getWords(composer, suggestionsGatherer, mProximityInfo); + final boolean isInDict = dictionary.isValidWord(text); + final String[] suggestions = suggestionsGatherer.getGatheredSuggestions(); + + final int flags = (isInDict ? SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY : 0) + | (null != suggestions ? SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO : 0); + return new SuggestionsInfo(flags, suggestions); } } diff --git a/java/src/com/android/inputmethod/latin/spellcheck/SpellChecker.java b/java/src/com/android/inputmethod/latin/spellcheck/SpellChecker.java deleted file mode 100644 index 63c6d69d7..000000000 --- a/java/src/com/android/inputmethod/latin/spellcheck/SpellChecker.java +++ /dev/null @@ -1,115 +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.spellcheck; - -import android.content.Context; -import android.content.res.Resources; - -import com.android.inputmethod.compat.ArraysCompatUtils; -import com.android.inputmethod.latin.Dictionary; -import com.android.inputmethod.latin.Dictionary.DataType; -import com.android.inputmethod.latin.Dictionary.WordCallback; -import com.android.inputmethod.latin.DictionaryFactory; -import com.android.inputmethod.latin.Utils; -import com.android.inputmethod.latin.WordComposer; - -import java.util.LinkedList; -import java.util.List; -import java.util.Locale; - -/** - * Implements spell checking methods. - */ -public class SpellChecker { - - public final Dictionary mDictionary; - - public SpellChecker(final Context context, final Locale locale) { - final Resources resources = context.getResources(); - final int fallbackResourceId = Utils.getMainDictionaryResourceId(resources); - mDictionary = DictionaryFactory.createDictionaryFromManager(context, locale, - fallbackResourceId); - } - - // Note : this must be reentrant - /** - * Finds out whether a word is in the dictionary or not. - * - * @param text the sequence containing the word to check for. - * @param start the index of the first character of the word in text. - * @param end the index of the next-to-last character in text. - * @return true if the word is in the dictionary, false otherwise. - */ - public boolean isCorrect(final CharSequence text, final int start, final int end) { - return mDictionary.isValidWord(text.subSequence(start, end)); - } - - private static class SuggestionsGatherer implements WordCallback { - private final int DEFAULT_SUGGESTION_LENGTH = 16; - private final List<String> mSuggestions = new LinkedList<String>(); - private int[] mScores = new int[DEFAULT_SUGGESTION_LENGTH]; - private int mLength = 0; - - @Override - synchronized public boolean addWord(char[] word, int wordOffset, int wordLength, int score, - int dicTypeId, DataType dataType) { - if (mLength >= mScores.length) { - final int newLength = mScores.length * 2; - mScores = new int[newLength]; - } - final int positionIndex = ArraysCompatUtils.binarySearch(mScores, 0, mLength, score); - // binarySearch returns the index if the element exists, and -<insertion index> - 1 - // if it doesn't. See documentation for binarySearch. - final int insertionIndex = positionIndex >= 0 ? positionIndex : -positionIndex - 1; - System.arraycopy(mScores, insertionIndex, mScores, insertionIndex + 1, - mLength - insertionIndex); - mLength += 1; - mScores[insertionIndex] = score; - mSuggestions.add(insertionIndex, new String(word, wordOffset, wordLength)); - return true; - } - - public List<String> getGatheredSuggestions() { - return mSuggestions; - } - } - - // Note : this must be reentrant - /** - * Gets a list of suggestions for a specific string. - * - * This returns a list of possible corrections for the text passed as an - * arguments. It may split or group words, and even perform grammatical - * analysis. - * - * @param text the sequence containing the word to check for. - * @param start the index of the first character of the word in text. - * @param end the index of the next-to-last character in text. - * @return a list of possible suggestions to replace the text. - */ - public List<String> getSuggestions(final CharSequence text, final int start, final int end) { - final SuggestionsGatherer suggestionsGatherer = new SuggestionsGatherer(); - final WordComposer composer = new WordComposer(); - for (int i = start; i < end; ++i) { - int character = text.charAt(i); - composer.add(character, new int[] { character }, - WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); - } - mDictionary.getWords(composer, suggestionsGatherer); - return suggestionsGatherer.getGatheredSuggestions(); - } -} |