diff options
Diffstat (limited to 'java/src/com/android/inputmethod')
21 files changed, 301 insertions, 159 deletions
diff --git a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java index 4929dd948..5c54fa548 100644 --- a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java +++ b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java @@ -53,7 +53,8 @@ public class SuggestionSpanUtils { public static CharSequence getTextWithSuggestionSpan(Context context, CharSequence pickedWord, SuggestedWords suggestedWords) { if (TextUtils.isEmpty(pickedWord) || CONSTRUCTOR_SuggestionSpan == null - || suggestedWords == null || suggestedWords.size() == 0) { + || suggestedWords == null || suggestedWords.size() == 0 + || suggestedWords.getInfo(0).isObsoleteSuggestedWord()) { return pickedWord; } diff --git a/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java b/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java index 7eb5acda8..b6e0ec9cf 100644 --- a/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java +++ b/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java @@ -18,6 +18,7 @@ package com.android.inputmethod.deprecated.languageswitcher; import com.android.inputmethod.keyboard.internal.KeyboardBuilder; import com.android.inputmethod.latin.DictionaryFactory; +import com.android.inputmethod.latin.LocaleUtils; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.Settings; import com.android.inputmethod.latin.SharedPreferencesCompat; @@ -155,7 +156,7 @@ public class InputLanguageSelection extends PreferenceActivity { private Pair<Long, Boolean> hasDictionaryOrLayout(Locale locale) { if (locale == null) return new Pair<Long, Boolean>(null, false); final Resources res = getResources(); - final Locale saveLocale = Utils.setSystemLocale(res, locale); + final Locale saveLocale = LocaleUtils.setSystemLocale(res, locale); final Long dictionaryId = DictionaryFactory.getDictionaryId(this, locale); boolean hasLayout = false; @@ -174,7 +175,7 @@ public class InputLanguageSelection extends PreferenceActivity { } catch (XmlPullParserException e) { } catch (IOException e) { } - Utils.setSystemLocale(res, saveLocale); + LocaleUtils.setSystemLocale(res, saveLocale); return new Pair<Long, Boolean>(dictionaryId, hasLayout); } diff --git a/java/src/com/android/inputmethod/deprecated/languageswitcher/LanguageSwitcher.java b/java/src/com/android/inputmethod/deprecated/languageswitcher/LanguageSwitcher.java index 1eedb5ee1..8070942d0 100644 --- a/java/src/com/android/inputmethod/deprecated/languageswitcher/LanguageSwitcher.java +++ b/java/src/com/android/inputmethod/deprecated/languageswitcher/LanguageSwitcher.java @@ -18,9 +18,9 @@ package com.android.inputmethod.deprecated.languageswitcher; import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinImeLogger; +import com.android.inputmethod.latin.LocaleUtils; import com.android.inputmethod.latin.Settings; import com.android.inputmethod.latin.SharedPreferencesCompat; -import com.android.inputmethod.latin.Utils; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; @@ -126,7 +126,7 @@ public class LanguageSwitcher { private void constructLocales() { mLocales.clear(); for (final String lang : mSelectedLanguageArray) { - final Locale locale = Utils.constructLocaleFromString(lang); + final Locale locale = LocaleUtils.constructLocaleFromString(lang); mLocales.add(locale); } } diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java b/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java index 864091289..6f5420882 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java @@ -76,4 +76,21 @@ public interface KeyboardActionListener { * @return true if the request has been consumed, false otherwise. */ public boolean onCustomRequest(int requestCode); + + public static class Adapter implements KeyboardActionListener { + @Override + public void onPress(int primaryCode, boolean withSliding) {} + @Override + public void onRelease(int primaryCode, boolean withSliding) {} + @Override + public void onCodeInput(int primaryCode, int[] keyCodes, int x, int y) {} + @Override + public void onTextInput(CharSequence text) {} + @Override + public void onCancelInput() {} + @Override + public boolean onCustomRequest(int requestCode) { + return false; + } + } } diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java index b1212f424..e43ae55a8 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java @@ -33,6 +33,7 @@ import com.android.inputmethod.keyboard.internal.ModifierKeyState; import com.android.inputmethod.keyboard.internal.ShiftKeyState; import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinImeLogger; +import com.android.inputmethod.latin.LocaleUtils; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.Settings; import com.android.inputmethod.latin.SubtypeSwitcher; @@ -258,11 +259,11 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha final SoftReference<LatinKeyboard> ref = mKeyboardCache.get(id); LatinKeyboard keyboard = (ref == null) ? null : ref.get(); if (keyboard == null) { - final Locale savedLocale = Utils.setSystemLocale(mResources, id.mLocale); + final Locale savedLocale = LocaleUtils.setSystemLocale(mResources, id.mLocale); try { keyboard = new LatinKeyboard.Builder(mThemeContext).load(id).build(); } finally { - Utils.setSystemLocale(mResources, savedLocale); + LocaleUtils.setSystemLocale(mResources, savedLocale); } mKeyboardCache.put(id, new SoftReference<LatinKeyboard>(keyboard)); diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java index be04b5a52..5fa9b8fc4 100644 --- a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java @@ -81,6 +81,7 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke private int mOldPointerCount = 1; private int mOldKeyIndex; + private final boolean mConfigShowMiniKeyboardAtTouchedPoint; protected KeyDetector mKeyDetector; // To detect double tap. @@ -225,6 +226,8 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke a.recycle(); final Resources res = getResources(); + mConfigShowMiniKeyboardAtTouchedPoint = res.getBoolean( + R.bool.config_show_mini_keyboard_at_touched_point); final float keyHysteresisDistance = res.getDimension(R.dimen.key_hysteresis_distance); mKeyDetector = new KeyDetector(keyHysteresisDistance); @@ -459,7 +462,13 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke mPopupPanel = popupPanel; mPopupPanelPointerTrackerId = tracker.mPointerId; - popupPanel.showPopupPanel(this, parentKey, tracker, mPopupWindow); + final Keyboard keyboard = getKeyboard(); + mPopupPanel.setShifted(keyboard.isShiftedOrShiftLocked()); + final int pointX = (mConfigShowMiniKeyboardAtTouchedPoint) ? tracker.getLastX() + : parentKey.mX + parentKey.mWidth / 2; + final int pointY = parentKey.mY - keyboard.mVerticalGap; + popupPanel.showPopupPanel( + this, this, pointX, pointY, mPopupWindow, getKeyboardActionListener()); final int translatedX = popupPanel.translateX(tracker.getLastX()); final int translatedY = popupPanel.translateY(tracker.getLastY()); tracker.onShowPopupPanel(translatedX, translatedY, SystemClock.uptimeMillis(), popupPanel); @@ -486,6 +495,9 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke @Override public boolean onTouchEvent(MotionEvent me) { + if (getKeyboard() == null) { + return false; + } return mTouchScreenRegulator.onTouchEvent(me); } diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java index d33cb442b..7ae62200d 100644 --- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java +++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java @@ -63,13 +63,12 @@ public class PointerTracker { public TimerProxy getTimerProxy(); } - public interface DrawingProxy { + public interface DrawingProxy extends PopupPanel.Controller { public void invalidateKey(Key key); public TextView inflateKeyPreviewText(); public void showKeyPreview(int keyIndex, PointerTracker tracker); public void cancelShowKeyPreview(PointerTracker tracker); public void dismissKeyPreview(PointerTracker tracker); - public boolean dismissPopupPanel(); } public interface TimerProxy { @@ -140,20 +139,8 @@ public class PointerTracker { private boolean mIgnoreModifierKey; // Empty {@link KeyboardActionListener} - private static final KeyboardActionListener EMPTY_LISTENER = new KeyboardActionListener() { - @Override - public void onPress(int primaryCode, boolean withSliding) {} - @Override - public void onRelease(int primaryCode, boolean withSliding) {} - @Override - public void onCodeInput(int primaryCode, int[] keyCodes, int x, int y) {} - @Override - public void onTextInput(CharSequence text) {} - @Override - public void onCancelInput() {} - @Override - public boolean onCustomRequest(int requestCode) { return false; } - }; + private static final KeyboardActionListener EMPTY_LISTENER = + new KeyboardActionListener.Adapter(); public static void init(boolean hasDistinctMultitouch, Context context) { if (hasDistinctMultitouch) { @@ -284,7 +271,7 @@ public class PointerTracker { mListener.onCancelInput(); } - public void setKeyDetectorInner(KeyDetector keyDetector) { + private void setKeyDetectorInner(KeyDetector keyDetector) { mKeyDetector = keyDetector; mKeyboard = keyDetector.getKeyboard(); mKeys = mKeyboard.mKeys; diff --git a/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java b/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java index 2396222bc..c4428dc60 100644 --- a/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java @@ -36,12 +36,12 @@ import java.util.List; */ public class PopupMiniKeyboardView extends KeyboardView implements PopupPanel { private final int[] mCoordinates = new int[2]; - private final boolean mConfigShowMiniKeyboardAtTouchedPoint; private final KeyDetector mKeyDetector; private final int mVerticalCorrection; - private LatinKeyboardView mParentKeyboardView; + private Controller mController; + private KeyboardActionListener mListener; private int mOriginX; private int mOriginY; @@ -101,33 +101,31 @@ public class PopupMiniKeyboardView extends KeyboardView implements PopupPanel { public void cancelKeyTimers() {} }; - private final KeyboardActionListener mListner = new KeyboardActionListener() { + private final KeyboardActionListener mMiniKeyboardListener = + new KeyboardActionListener.Adapter() { @Override public void onCodeInput(int primaryCode, int[] keyCodes, int x, int y) { - mParentKeyboardView.getKeyboardActionListener() - .onCodeInput(primaryCode, keyCodes, x, y); + mListener.onCodeInput(primaryCode, keyCodes, x, y); } @Override public void onTextInput(CharSequence text) { - mParentKeyboardView.getKeyboardActionListener().onTextInput(text); + mListener.onTextInput(text); } @Override public void onCancelInput() { - mParentKeyboardView.getKeyboardActionListener().onCancelInput(); + mListener.onCancelInput(); } @Override public void onPress(int primaryCode, boolean withSliding) { - mParentKeyboardView.getKeyboardActionListener().onPress(primaryCode, withSliding); + mListener.onPress(primaryCode, withSliding); } @Override public void onRelease(int primaryCode, boolean withSliding) { - mParentKeyboardView.getKeyboardActionListener().onRelease(primaryCode, withSliding); + mListener.onRelease(primaryCode, withSliding); } - @Override - public boolean onCustomRequest(int requestCode) { return false; } }; public PopupMiniKeyboardView(Context context, AttributeSet attrs) { @@ -144,8 +142,6 @@ public class PopupMiniKeyboardView extends KeyboardView implements PopupPanel { a.recycle(); final Resources res = context.getResources(); - mConfigShowMiniKeyboardAtTouchedPoint = res.getBoolean( - R.bool.config_show_mini_keyboard_at_touched_point); // Override default ProximityKeyDetector. mKeyDetector = new MiniKeyboardKeyDetector(res.getDimension( R.dimen.mini_keyboard_slide_allowance)); @@ -179,7 +175,7 @@ public class PopupMiniKeyboardView extends KeyboardView implements PopupPanel { @Override public KeyboardActionListener getKeyboardActionListener() { - return mListner; + return mMiniKeyboardListener; } @Override @@ -200,41 +196,52 @@ public class PopupMiniKeyboardView extends KeyboardView implements PopupPanel { } @Override - public void showPopupPanel(LatinKeyboardView parentKeyboardView, Key parentKey, - PointerTracker tracker, PopupWindow window) { - mParentKeyboardView = parentKeyboardView; + public void setShifted(boolean shifted) { + final MiniKeyboard miniKeyboard = (MiniKeyboard)getKeyboard(); + if (miniKeyboard.setShifted(shifted)) { + invalidateAllKeys(); + } + } + + @Override + public void showPopupPanel(View parentView, Controller controller, int pointX, int pointY, + PopupWindow window, KeyboardActionListener listener) { + mController = controller; + mListener = listener; final View container = (View)getParent(); final MiniKeyboard miniKeyboard = (MiniKeyboard)getKeyboard(); - final Keyboard parentKeyboard = parentKeyboardView.getKeyboard(); - parentKeyboardView.getLocationInWindow(mCoordinates); - final int pointX = (mConfigShowMiniKeyboardAtTouchedPoint) ? tracker.getLastX() - : parentKey.mX + parentKey.mWidth / 2; - final int pointY = parentKey.mY; + parentView.getLocationInWindow(mCoordinates); final int miniKeyboardLeft = pointX - miniKeyboard.getDefaultCoordX() - + parentKeyboardView.getPaddingLeft(); - final int x = Math.max(0, Math.min(miniKeyboardLeft, - parentKeyboardView.getWidth() - miniKeyboard.mOccupiedWidth)) - - container.getPaddingLeft() + mCoordinates[0]; - final int y = pointY - parentKeyboard.mVerticalGap + + parentView.getPaddingLeft(); + final int x = wrapUp(Math.max(0, Math.min(miniKeyboardLeft, + parentView.getWidth() - miniKeyboard.mOccupiedWidth)) + - container.getPaddingLeft() + mCoordinates[0], + container.getMeasuredWidth(), 0, parentView.getWidth()); + final int y = pointY - (container.getMeasuredHeight() - container.getPaddingBottom()) - + parentKeyboardView.getPaddingTop() + mCoordinates[1]; + + parentView.getPaddingTop() + mCoordinates[1]; - if (miniKeyboard.setShifted(parentKeyboard.isShiftedOrShiftLocked())) { - invalidateAllKeys(); - } window.setContentView(container); window.setWidth(container.getMeasuredWidth()); window.setHeight(container.getMeasuredHeight()); - window.showAtLocation(parentKeyboardView, Gravity.NO_GRAVITY, x, y); + window.showAtLocation(parentView, Gravity.NO_GRAVITY, x, y); mOriginX = x + container.getPaddingLeft() - mCoordinates[0]; mOriginY = y + container.getPaddingTop() - mCoordinates[1]; } + private static int wrapUp(int x, int width, int left, int right) { + if (x < left) + return left; + if (x + width > right) + return right - width; + return x; + } + @Override public boolean dismissPopupPanel() { - return mParentKeyboardView.dismissPopupPanel(); + return mController.dismissPopupPanel(); } @Override diff --git a/java/src/com/android/inputmethod/keyboard/PopupPanel.java b/java/src/com/android/inputmethod/keyboard/PopupPanel.java index db637c50b..5e51fd54a 100644 --- a/java/src/com/android/inputmethod/keyboard/PopupPanel.java +++ b/java/src/com/android/inputmethod/keyboard/PopupPanel.java @@ -16,18 +16,27 @@ package com.android.inputmethod.keyboard; +import android.view.View; import android.widget.PopupWindow; public interface PopupPanel extends PointerTracker.KeyEventHandler { + public interface Controller { + public boolean dismissPopupPanel(); + } + + public void setShifted(boolean shifted); + /** * Show popup panel. - * @param parentKeyboardView the parent KeyboardView that has the parent key. - * @param parentKey the parent key that is the source of this popup panel - * @param tracker the pointer tracker that pressesd the parent key + * @param parentView the parent view of this popup panel + * @param controller the controller that can dismiss this popup panel + * @param pointX x coordinate of this popup panel + * @param pointY y coordinate of this popup panel * @param window PopupWindow to be used to show this popup panel + * @param listener the listener that will receive keyboard action from this popup panel. */ - public void showPopupPanel(LatinKeyboardView parentKeyboardView, Key parentKey, - PointerTracker tracker, PopupWindow window); + public void showPopupPanel(View parentView, Controller controller, int pointX, int pointY, + PopupWindow window, KeyboardActionListener listener); /** * Translate X-coordinate of touch event to the local X-coordinate of this PopupPanel. diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java index 360c944d2..5546d48c0 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java @@ -157,9 +157,9 @@ class BinaryDictionaryGetter { private static AssetFileAddress loadFallbackResource(final Context context, final int fallbackResId, final Locale locale) { final Resources res = context.getResources(); - final Locale savedLocale = Utils.setSystemLocale(res, locale); + final Locale savedLocale = LocaleUtils.setSystemLocale(res, locale); final AssetFileDescriptor afd = res.openRawResourceFd(fallbackResId); - Utils.setSystemLocale(res, savedLocale); + LocaleUtils.setSystemLocale(res, savedLocale); if (afd == null) { Log.e(TAG, "Found the resource but cannot read it. Is it compressed? resId=" diff --git a/java/src/com/android/inputmethod/latin/CandidateView.java b/java/src/com/android/inputmethod/latin/CandidateView.java index 5f20c70b4..f445abf48 100644 --- a/java/src/com/android/inputmethod/latin/CandidateView.java +++ b/java/src/com/android/inputmethod/latin/CandidateView.java @@ -64,6 +64,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo private static final boolean DBG = LatinImeLogger.sDBG; + private final ViewGroup mCandidatesPlacer; private final ViewGroup mCandidatesStrip; private ViewGroup mCandidatesPane; private ViewGroup mCandidatesPaneContainer; @@ -76,13 +77,9 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo private final PopupWindow mPreviewPopup; private final TextView mPreviewText; - private final View mTouchToSave; - private final TextView mWordToSave; - private Listener mListener; private SuggestedWords mSuggestions = SuggestedWords.EMPTY; private boolean mShowingAutoCorrectionInverted; - private boolean mShowingAddToDictionary; private final SuggestionsStripParams mStripParams; private final SuggestionsPaneParams mPaneParams; @@ -94,7 +91,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo private static final int MSG_HIDE_PREVIEW = 0; private static final int MSG_UPDATE_SUGGESTION = 1; - private static final long DELAY_HIDE_PREVIEW = 1000; + private static final long DELAY_HIDE_PREVIEW = 1300; private static final long DELAY_UPDATE_SUGGESTION = 300; public UiHandler(CandidateView outerInstance) { @@ -239,8 +236,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo lastView = info; info.measure(WRAP_CONTENT, WRAP_CONTENT); final int infoWidth = info.getMeasuredWidth(); - FrameLayoutCompatUtils.placeViewAt(info, x - infoWidth, y, infoWidth, - info.getMeasuredHeight()); + FrameLayoutCompatUtils.placeViewAt( + info, x - infoWidth, y, infoWidth, info.getMeasuredHeight()); } } if (x != 0) { @@ -274,13 +271,16 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo private static final int AUTO_CORRECT_INVERT = 0x04; private static final int VALID_TYPED_WORD_BOLD = 0x08; - private final TextPaint mPaint; private final int mSuggestionStripOption; private final ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(); public boolean mMoreSuggestionsAvailable; + public final TextView mWordToSaveView; + private final TextView mHintToSaveView; + private final CharSequence mHintToSaveText; + public SuggestionsStripParams(Context context, AttributeSet attrs, int defStyle, List<TextView> words, List<View> dividers, List<TextView> infos) { super(words, dividers, infos); @@ -305,9 +305,10 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo mInvertedForegroundColorSpan = new ForegroundColorSpan(mColorTypedWord ^ 0x00ffffff); mInvertedBackgroundColorSpan = new BackgroundColorSpan(mColorTypedWord); - mPaint = new TextPaint(); - final float textSize = res.getDimension(R.dimen.candidate_text_size); - mPaint.setTextSize(textSize); + final LayoutInflater inflater = LayoutInflater.from(context); + mWordToSaveView = (TextView)inflater.inflate(R.layout.candidate_word, null); + mHintToSaveView = (TextView)inflater.inflate(R.layout.candidate_word, null); + mHintToSaveText = context.getText(R.string.hint_add_to_dictionary); } public int getTextColor() { @@ -366,7 +367,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo final SuggestedWordInfo info = (pos < suggestions.size()) ? suggestions.getInfo(pos) : null; - if (info != null && info.isPreviousSuggestedWord()) { + if (info != null && info.isObsoleteSuggestedWord()) { return applyAlpha(color, 0.5f); } else { return color; @@ -388,7 +389,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo return word; } - public int layout(SuggestedWords suggestions, ViewGroup stripView, ViewGroup paneView, + public int layout(SuggestedWords suggestions, ViewGroup stripView, ViewGroup placer, int stripWidth) { if (suggestions.isPunctuationSuggestions()) { return layoutPunctuationSuggestions(suggestions, stripView); @@ -405,6 +406,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo final View divider = mDividers.get(pos); // Add divider if this isn't the left most suggestion in candidate strip. stripView.addView(divider); + x += divider.getMeasuredWidth(); } final CharSequence styled = mTexts.get(pos); @@ -429,18 +431,19 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo word.setTextScaleX(scaleX); stripView.addView(word); setLayoutWeight(word, getCandidateWeight(index), MATCH_PARENT); + x += word.getMeasuredWidth(); if (DBG) { final CharSequence debugInfo = getDebugInfo(suggestions, pos); if (debugInfo != null) { final TextView info = mInfos.get(pos); info.setText(debugInfo); - paneView.addView(info); + placer.addView(info); info.measure(WRAP_CONTENT, WRAP_CONTENT); final int infoWidth = info.getMeasuredWidth(); final int y = info.getMeasuredHeight(); - FrameLayoutCompatUtils.placeViewAt(info, x, 0, infoWidth, y); - x += infoWidth * 2; + FrameLayoutCompatUtils.placeViewAt( + info, x - infoWidth, y, infoWidth, info.getMeasuredHeight()); } } } @@ -498,6 +501,33 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo mMoreSuggestionsAvailable = false; return countInStrip; } + + public void layoutAddToDictionaryHint(CharSequence word, ViewGroup stripView, + int stripWidth) { + final int width = stripWidth - mDividerWidth - mPadding * 2; + + final TextView wordView = mWordToSaveView; + wordView.setTextColor(mColorTypedWord); + final int wordWidth = (int)(width * mCenterCandidateWeight); + final CharSequence text = getEllipsizedText(word, wordWidth, wordView.getPaint()); + final float wordScaleX = wordView.getTextScaleX(); + wordView.setTag(word); + wordView.setText(text); + wordView.setTextScaleX(wordScaleX); + stripView.addView(wordView); + setLayoutWeight(wordView, mCenterCandidateWeight, MATCH_PARENT); + + stripView.addView(mDividers.get(0)); + + final TextView hintView = mHintToSaveView; + hintView.setTextColor(mColorAutoCorrect); + final int hintWidth = width - wordWidth; + final float hintScaleX = getTextScaleX(mHintToSaveText, hintWidth, hintView.getPaint()); + hintView.setText(mHintToSaveText); + hintView.setTextScaleX(hintScaleX); + stripView.addView(hintView); + setLayoutWeight(hintView, 1.0f - mCenterCandidateWeight, MATCH_PARENT); + } } /** @@ -517,7 +547,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo super(context, attrs); if (defStyle != R.attr.candidateViewStyle) { throw new IllegalArgumentException( - "can't accept defStyle other than R.attr.candidayeViewStyle: defStyle=" + "can't accept defStyle other than R.attr.candidateViewStyle: defStyle=" + defStyle); } setBackgroundDrawable(LinearLayoutCompatUtils.getBackgroundDrawable( @@ -532,6 +562,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo mPreviewPopup.setContentView(mPreviewText); mPreviewPopup.setBackgroundDrawable(null); + mCandidatesPlacer = (ViewGroup)findViewById(R.id.candidates_placer); mCandidatesStrip = (ViewGroup)findViewById(R.id.candidates_strip); for (int pos = 0; pos < MAX_SUGGESTIONS; pos++) { final TextView word = (TextView)inflater.inflate(R.layout.candidate_word, null); @@ -546,13 +577,10 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo mInfos.add((TextView)inflater.inflate(R.layout.candidate_info, null)); } - mTouchToSave = findViewById(R.id.touch_to_save); - mWordToSave = (TextView)findViewById(R.id.word_to_save); - mWordToSave.setOnClickListener(this); - mStripParams = new SuggestionsStripParams(context, attrs, defStyle, mWords, mDividers, mInfos); mPaneParams = new SuggestionsPaneParams(mWords, mDividers, mInfos); + mStripParams.mWordToSaveView.setOnClickListener(this); } /** @@ -588,7 +616,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo final int width = getWidth(); final int countInStrip = mStripParams.layout( - mSuggestions, mCandidatesStrip, mCandidatesPane, width); + mSuggestions, mCandidatesStrip, mCandidatesPlacer, width); mPaneParams.layout( mSuggestions, mCandidatesPane, countInStrip, mStripParams.getTextColor(), width); } @@ -636,6 +664,15 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo } } + private static float getTextScaleX(CharSequence text, int maxWidth, TextPaint paint) { + paint.setTextScaleX(1.0f); + final int width = getTextWidth(text, paint); + if (width <= maxWidth) { + return 1.0f; + } + return maxWidth / (float)width; + } + private static CharSequence getEllipsizedText(CharSequence text, int maxWidth, TextPaint paint) { if (text == null) return null; @@ -718,20 +755,21 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo } public boolean isShowingAddToDictionaryHint() { - return mShowingAddToDictionary; + return mCandidatesStrip.getChildCount() > 0 + && mCandidatesStrip.getChildAt(0) == mStripParams.mWordToSaveView; } public void showAddToDictionaryHint(CharSequence word) { - mWordToSave.setText(word); - mShowingAddToDictionary = true; - mCandidatesStrip.setVisibility(GONE); - mTouchToSave.setVisibility(VISIBLE); + clear(); + mStripParams.layoutAddToDictionaryHint(word, mCandidatesStrip, getWidth()); } public boolean dismissAddToDictionaryHint() { - if (!mShowingAddToDictionary) return false; - clear(); - return true; + if (isShowingAddToDictionaryHint()) { + clear(); + return true; + } + return false; } public SuggestedWords getSuggestions() { @@ -739,10 +777,9 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo } public void clear() { - mShowingAddToDictionary = false; mShowingAutoCorrectionInverted = false; - mTouchToSave.setVisibility(GONE); - mCandidatesStrip.setVisibility(VISIBLE); + mCandidatesPlacer.removeAllViews(); + mCandidatesPlacer.addView(mCandidatesStrip); mCandidatesStrip.removeAllViews(); mCandidatesPane.removeAllViews(); closeCandidatesPane(); @@ -752,7 +789,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo mPreviewPopup.dismiss(); } - private void showPreview(int index, CharSequence word) { + private void showPreview(View view, CharSequence word) { if (TextUtils.isEmpty(word)) return; @@ -761,9 +798,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo previewText.setText(word); previewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); - View v = mWords.get(index); final int[] offsetInWindow = new int[2]; - v.getLocationInWindow(offsetInWindow); + view.getLocationInWindow(offsetInWindow); final int posX = offsetInWindow[0]; final int posY = offsetInWindow[1] - previewText.getMeasuredHeight(); final PopupWindow previewPopup = mPreviewPopup; @@ -778,7 +814,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo private void addToDictionary(CharSequence word) { if (mListener.addWordToDictionary(word.toString())) { - showPreview(0, getContext().getString(R.string.added_word, word)); + final CharSequence message = getContext().getString(R.string.added_word, word); + showPreview(mStripParams.mWordToSaveView, message); } } @@ -793,8 +830,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo @Override public void onClick(View view) { - if (view == mWordToSave) { - addToDictionary(((TextView)view).getText()); + if (view == mStripParams.mWordToSaveView) { + addToDictionary((CharSequence)view.getTag()); clear(); return; } diff --git a/java/src/com/android/inputmethod/latin/DictionaryFactory.java b/java/src/com/android/inputmethod/latin/DictionaryFactory.java index 9642151d7..dfaad26bc 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryFactory.java +++ b/java/src/com/android/inputmethod/latin/DictionaryFactory.java @@ -83,9 +83,9 @@ public class DictionaryFactory { try { final Resources res = context.getResources(); if (null != locale) { - final Locale savedLocale = Utils.setSystemLocale(res, locale); + final Locale savedLocale = LocaleUtils.setSystemLocale(res, locale); afd = res.openRawResourceFd(resId); - Utils.setSystemLocale(res, savedLocale); + LocaleUtils.setSystemLocale(res, savedLocale); } else { afd = res.openRawResourceFd(resId); } @@ -145,7 +145,7 @@ public class DictionaryFactory { */ public static boolean isDictionaryAvailable(Context context, Locale locale) { final Resources res = context.getResources(); - final Locale saveLocale = Utils.setSystemLocale(res, locale); + final Locale saveLocale = LocaleUtils.setSystemLocale(res, locale); final int resourceId = Utils.getMainDictionaryResourceId(res); final AssetFileDescriptor afd = res.openRawResourceFd(resourceId); @@ -156,14 +156,14 @@ public class DictionaryFactory { /* Um, what can we do here exactly? */ } - Utils.setSystemLocale(res, saveLocale); + LocaleUtils.setSystemLocale(res, saveLocale); return hasDictionary; } // TODO: Do not use the size of the dictionary as an unique dictionary ID. - public static Long getDictionaryId(Context context, Locale locale) { + public static Long getDictionaryId(final Context context, final Locale locale) { final Resources res = context.getResources(); - final Locale saveLocale = Utils.setSystemLocale(res, locale); + final Locale saveLocale = LocaleUtils.setSystemLocale(res, locale); final int resourceId = Utils.getMainDictionaryResourceId(res); final AssetFileDescriptor afd = res.openRawResourceFd(resourceId); @@ -175,7 +175,7 @@ public class DictionaryFactory { } catch (java.io.IOException e) { } - Utils.setSystemLocale(res, saveLocale); + LocaleUtils.setSystemLocale(res, saveLocale); return size; } diff --git a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java index 9f4777f5a..2b78b9065 100644 --- a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java +++ b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java @@ -202,7 +202,11 @@ public class ExpandableDictionary extends Dictionary { // Currently updating contacts, don't return any results. if (mUpdatingDictionary) return; } + getWordsInner(codes, callback, proximityInfo); + } + protected final void getWordsInner(final WordComposer codes, final WordCallback callback, + final ProximityInfo proximityInfo) { mInputLength = codes.size(); if (mCodes.length < mInputLength) mCodes = new int[mInputLength][]; // Cache the codes so that we don't have to lookup an array list @@ -223,8 +227,7 @@ public class ExpandableDictionary extends Dictionary { if (mRequiresReload) startDictionaryLoadingTaskLocked(); if (mUpdatingDictionary) return false; } - final int freq = getWordFrequency(word); - return freq > -1; + return getWordFrequency(word) > -1; } /** @@ -464,7 +467,7 @@ public class ExpandableDictionary extends Dictionary { } /** - * Used only for testing purposes + * Used for testing purposes and in the spell checker * This function will wait for loading from database to be done */ void waitForDictionaryLoading() { @@ -477,6 +480,11 @@ public class ExpandableDictionary extends Dictionary { } } + protected final void blockingReloadDictionaryIfRequired() { + reloadDictionaryIfRequired(); + waitForDictionaryLoading(); + } + // Local to reverseLookUp, but do not allocate each time. private final char[] mLookedUpString = new char[MAX_WORD_LENGTH]; diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 552517bc8..229bf0f4c 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -479,10 +479,10 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar private void initSuggest() { final String localeStr = mSubtypeSwitcher.getInputLocaleStr(); - final Locale keyboardLocale = Utils.constructLocaleFromString(localeStr); + final Locale keyboardLocale = LocaleUtils.constructLocaleFromString(localeStr); final Resources res = mResources; - final Locale savedLocale = Utils.setSystemLocale(res, keyboardLocale); + final Locale savedLocale = LocaleUtils.setSystemLocale(res, keyboardLocale); final ContactsDictionary oldContactsDictionary; if (mSuggest != null) { oldContactsDictionary = mSuggest.getContactsDictionary(); @@ -514,7 +514,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar updateCorrectionMode(); - Utils.setSystemLocale(res, savedLocale); + LocaleUtils.setSystemLocale(res, savedLocale); } /** @@ -551,7 +551,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar /* package private */ void resetSuggestMainDict() { final String localeStr = mSubtypeSwitcher.getInputLocaleStr(); - final Locale keyboardLocale = Utils.constructLocaleFromString(localeStr); + final Locale keyboardLocale = LocaleUtils.constructLocaleFromString(localeStr); int mainDicResId = Utils.getMainDictionaryResourceId(mResources); mSuggest.resetMainDict(this, mainDicResId, keyboardLocale); } diff --git a/java/src/com/android/inputmethod/latin/LocaleUtils.java b/java/src/com/android/inputmethod/latin/LocaleUtils.java index 054f1f9b8..efa9bfee3 100644 --- a/java/src/com/android/inputmethod/latin/LocaleUtils.java +++ b/java/src/com/android/inputmethod/latin/LocaleUtils.java @@ -16,8 +16,13 @@ package com.android.inputmethod.latin; +import android.content.res.Configuration; +import android.content.res.Resources; import android.text.TextUtils; +import java.util.HashMap; +import java.util.Locale; + /** * A class to help with handling Locales in string form. * @@ -30,6 +35,10 @@ public class LocaleUtils { private final static String TAG = LocaleUtils.class.getSimpleName(); + private LocaleUtils() { + // Intentional empty constructor for utility class. + } + // Locale match level constants. // A higher level of match is guaranteed to have a higher numerical value. // Some room is left within constants to add match cases that may arise necessary @@ -154,4 +163,46 @@ public class LocaleUtils { public static boolean isMatch(int level) { return LOCALE_MATCH <= level; } + + /** + * Sets the system locale for this process. + * + * @param res the resources to use. Pass current resources. + * @param newLocale the locale to change to. + * @return the old locale. + */ + public static Locale setSystemLocale(final Resources res, final Locale newLocale) { + final Configuration conf = res.getConfiguration(); + final Locale saveLocale = conf.locale; + conf.locale = newLocale; + res.updateConfiguration(conf, res.getDisplayMetrics()); + return saveLocale; + } + + private static final HashMap<String, Locale> sLocaleCache = new HashMap<String, Locale>(); + + /** + * Creates a locale from a string specification. + */ + public static Locale constructLocaleFromString(final String localeStr) { + if (localeStr == null) + return null; + synchronized (sLocaleCache) { + if (sLocaleCache.containsKey(localeStr)) + return sLocaleCache.get(localeStr); + Locale retval = null; + String[] localeParams = localeStr.split("_", 3); + if (localeParams.length == 1) { + retval = new Locale(localeParams[0]); + } else if (localeParams.length == 2) { + retval = new Locale(localeParams[0], localeParams[1]); + } else if (localeParams.length == 3) { + retval = new Locale(localeParams[0], localeParams[1], localeParams[2]); + } + if (retval != null) { + sLocaleCache.put(localeStr, retval); + } + return retval; + } + } } diff --git a/java/src/com/android/inputmethod/latin/Settings.java b/java/src/com/android/inputmethod/latin/Settings.java index 87a713f5c..a5eed9015 100644 --- a/java/src/com/android/inputmethod/latin/Settings.java +++ b/java/src/com/android/inputmethod/latin/Settings.java @@ -128,8 +128,8 @@ public class Settings extends InputMethodSettingsActivity final Resources res = context.getResources(); final Locale savedLocale; if (null != localeStr) { - final Locale keyboardLocale = Utils.constructLocaleFromString(localeStr); - savedLocale = Utils.setSystemLocale(res, keyboardLocale); + final Locale keyboardLocale = LocaleUtils.constructLocaleFromString(localeStr); + savedLocale = LocaleUtils.setSystemLocale(res, keyboardLocale); } else { savedLocale = null; } @@ -191,7 +191,7 @@ public class Settings extends InputMethodSettingsActivity mVoiceKeyEnabled = voiceMode != null && !voiceMode.equals(voiceModeOff); mVoiceKeyOnMain = voiceMode != null && voiceMode.equals(voiceModeMain); - Utils.setSystemLocale(res, savedLocale); + LocaleUtils.setSystemLocale(res, savedLocale); } public boolean isSuggestedPunctuation(int code) { diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java index d969e39eb..87d854940 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java +++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java @@ -267,7 +267,7 @@ public class SubtypeSwitcher { // "en" --> language: en // "" --> the system locale if (!TextUtils.isEmpty(inputLocaleStr)) { - mInputLocale = Utils.constructLocaleFromString(inputLocaleStr); + mInputLocale = LocaleUtils.constructLocaleFromString(inputLocaleStr); mInputLocaleStr = inputLocaleStr; } else { mInputLocale = mSystemLocale; diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java index c1c46fa47..b177d144a 100644 --- a/java/src/com/android/inputmethod/latin/SuggestedWords.java +++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java @@ -199,7 +199,7 @@ public class SuggestedWords { } } - public boolean isPreviousSuggestedWord () { + public boolean isObsoleteSuggestedWord () { return mPreviousSuggestedWord; } } diff --git a/java/src/com/android/inputmethod/latin/SynchronouslyLoadedUserDictionary.java b/java/src/com/android/inputmethod/latin/SynchronouslyLoadedUserDictionary.java new file mode 100644 index 000000000..4a812b3a9 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/SynchronouslyLoadedUserDictionary.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin; + +import android.content.Context; + +import com.android.inputmethod.keyboard.ProximityInfo; + +public class SynchronouslyLoadedUserDictionary extends UserDictionary { + + public SynchronouslyLoadedUserDictionary(final Context context, final String locale) { + super(context, locale); + } + + @Override + public void getWords(final WordComposer codes, final WordCallback callback, + final ProximityInfo proximityInfo) { + blockingReloadDictionaryIfRequired(); + getWordsInner(codes, callback, proximityInfo); + } + + @Override + public synchronized boolean isValidWord(CharSequence word) { + blockingReloadDictionaryIfRequired(); + return getWordFrequency(word) > -1; + } +} diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java index ff051dcbb..60a4cfb38 100644 --- a/java/src/com/android/inputmethod/latin/Utils.java +++ b/java/src/com/android/inputmethod/latin/Utils.java @@ -705,38 +705,6 @@ public class Utils { return (int) (dip * scale + 0.5); } - public static Locale setSystemLocale(Resources res, Locale newLocale) { - final Configuration conf = res.getConfiguration(); - final Locale saveLocale = conf.locale; - conf.locale = newLocale; - res.updateConfiguration(conf, res.getDisplayMetrics()); - return saveLocale; - } - - private static final HashMap<String, Locale> sLocaleCache = new HashMap<String, Locale>(); - - public static Locale constructLocaleFromString(String localeStr) { - if (localeStr == null) - return null; - synchronized (sLocaleCache) { - if (sLocaleCache.containsKey(localeStr)) - return sLocaleCache.get(localeStr); - Locale retval = null; - String[] localeParams = localeStr.split("_", 3); - if (localeParams.length == 1) { - retval = new Locale(localeParams[0]); - } else if (localeParams.length == 2) { - retval = new Locale(localeParams[0], localeParams[1]); - } else if (localeParams.length == 3) { - retval = new Locale(localeParams[0], localeParams[1], localeParams[2]); - } - if (retval != null) { - sLocaleCache.put(localeStr, retval); - } - return retval; - } - } - /** * Remove duplicates from an array of strings. * @@ -783,7 +751,7 @@ public class Utils { } public static String getMiddleDisplayLanguage(Locale locale) { - return toTitleCase((constructLocaleFromString( + return toTitleCase((LocaleUtils.constructLocaleFromString( locale.getLanguage()).getDisplayLanguage(locale)), locale); } diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java index 502ebb52a..a6a5b6dd6 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -33,6 +33,8 @@ import com.android.inputmethod.latin.Dictionary.DataType; import com.android.inputmethod.latin.Dictionary.WordCallback; import com.android.inputmethod.latin.DictionaryCollection; import com.android.inputmethod.latin.DictionaryFactory; +import com.android.inputmethod.latin.LocaleUtils; +import com.android.inputmethod.latin.SynchronouslyLoadedUserDictionary; import com.android.inputmethod.latin.UserDictionary; import com.android.inputmethod.latin.Utils; import com.android.inputmethod.latin.WordComposer; @@ -139,7 +141,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService { private DictionaryPool getDictionaryPool(final String locale) { DictionaryPool pool = mDictionaryPools.get(locale); if (null == pool) { - final Locale localeObject = Utils.constructLocaleFromString(locale); + final Locale localeObject = LocaleUtils.constructLocaleFromString(locale); pool = new DictionaryPool(POOL_SIZE, this, localeObject); mDictionaryPools.put(locale, pool); } @@ -155,7 +157,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService { final String localeStr = locale.toString(); Dictionary userDict = mUserDictionaries.get(localeStr); if (null == userDict) { - userDict = new UserDictionary(this, localeStr); + userDict = new SynchronouslyLoadedUserDictionary(this, localeStr); mUserDictionaries.put(localeStr, userDict); } dictionaryCollection.addDictionary(userDict); @@ -172,7 +174,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService { public void onCreate() { final String localeString = getLocale(); mDictionaryPool = getDictionaryPool(localeString); - mLocale = Utils.constructLocaleFromString(localeString); + mLocale = LocaleUtils.constructLocaleFromString(localeString); } // Note : this must be reentrant |