diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin/SuggestionsView.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/SuggestionsView.java | 149 |
1 files changed, 82 insertions, 67 deletions
diff --git a/java/src/com/android/inputmethod/latin/SuggestionsView.java b/java/src/com/android/inputmethod/latin/SuggestionsView.java index 13beb4479..776cea26a 100644 --- a/java/src/com/android/inputmethod/latin/SuggestionsView.java +++ b/java/src/com/android/inputmethod/latin/SuggestionsView.java @@ -19,8 +19,14 @@ package com.android.inputmethod.latin; import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; +import android.graphics.Bitmap; +import android.graphics.Canvas; import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Paint.Align; +import android.graphics.Rect; import android.graphics.Typeface; +import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Message; import android.os.SystemClock; @@ -29,12 +35,11 @@ import android.text.SpannableString; import android.text.Spanned; import android.text.TextPaint; import android.text.TextUtils; -import android.text.style.BackgroundColorSpan; import android.text.style.CharacterStyle; -import android.text.style.ForegroundColorSpan; import android.text.style.StyleSpan; import android.text.style.UnderlineSpan; import android.util.AttributeSet; +import android.view.GestureDetector; import android.view.Gravity; import android.view.LayoutInflater; import android.view.MotionEvent; @@ -86,7 +91,6 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, private Listener mListener; private SuggestedWords mSuggestions = SuggestedWords.EMPTY; - private boolean mShowingAutoCorrectionInverted; private final SuggestionsViewParams mParams; private static final float MIN_TEXT_XSCALE = 0.70f; @@ -95,10 +99,8 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, private static class UiHandler extends StaticInnerHandlerWrapper<SuggestionsView> { private static final int MSG_HIDE_PREVIEW = 0; - private static final int MSG_UPDATE_SUGGESTION = 1; private static final long DELAY_HIDE_PREVIEW = 1300; - private static final long DELAY_UPDATE_SUGGESTION = 300; public UiHandler(SuggestionsView outerInstance) { super(outerInstance); @@ -111,9 +113,6 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, case MSG_HIDE_PREVIEW: suggestionsView.hidePreview(); break; - case MSG_UPDATE_SUGGESTION: - suggestionsView.updateSuggestions(); - break; } } @@ -126,19 +125,8 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, removeMessages(MSG_HIDE_PREVIEW); } - public void postUpdateSuggestions() { - cancelUpdateSuggestions(); - sendMessageDelayed(obtainMessage(MSG_UPDATE_SUGGESTION), - DELAY_UPDATE_SUGGESTION); - } - - public void cancelUpdateSuggestions() { - removeMessages(MSG_UPDATE_SUGGESTION); - } - public void cancelAllMessages() { cancelHidePreview(); - cancelUpdateSuggestions(); } } @@ -167,15 +155,13 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, private final float mCenterSuggestionWeight; private final int mCenterSuggestionIndex; private final Drawable mMoreSuggestionsHint; + private static final String MORE_SUGGESTIONS_HINT = "..."; private static final CharacterStyle BOLD_SPAN = new StyleSpan(Typeface.BOLD); private static final CharacterStyle UNDERLINE_SPAN = new UnderlineSpan(); - private final CharacterStyle mInvertedForegroundColorSpan; - private final CharacterStyle mInvertedBackgroundColorSpan; 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 static final int VALID_TYPED_WORD_BOLD = 0x04; private final int mSuggestionStripOption; @@ -225,7 +211,6 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, mCenterSuggestionWeight = getPercent(a, R.styleable.SuggestionsView_centerSuggestionPercentile, DEFAULT_CENTER_SUGGESTION_PERCENTILE); - mMoreSuggestionsHint = a.getDrawable(R.styleable.SuggestionsView_moreSuggestionsHint); mMaxMoreSuggestionsRow = a.getInt( R.styleable.SuggestionsView_maxMoreSuggestionsRow, DEFAULT_MAX_MORE_SUGGESTIONS_ROW); @@ -233,19 +218,35 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, R.styleable.SuggestionsView_minMoreSuggestionsWidth); a.recycle(); + mMoreSuggestionsHint = getMoreSuggestionsHint(res, + res.getDimension(R.dimen.more_suggestions_hint_text_size), mColorAutoCorrect); mCenterSuggestionIndex = mSuggestionsCountInStrip / 2; mMoreSuggestionsBottomGap = res.getDimensionPixelOffset( R.dimen.more_suggestions_bottom_gap); - mInvertedForegroundColorSpan = new ForegroundColorSpan(mColorTypedWord ^ 0x00ffffff); - mInvertedBackgroundColorSpan = new BackgroundColorSpan(mColorTypedWord); - final LayoutInflater inflater = LayoutInflater.from(context); mWordToSaveView = (TextView)inflater.inflate(R.layout.suggestion_word, null); mHintToSaveView = (TextView)inflater.inflate(R.layout.suggestion_word, null); mHintToSaveText = context.getText(R.string.hint_add_to_dictionary); } + private static Drawable getMoreSuggestionsHint(Resources res, float textSize, int color) { + final Paint paint = new Paint(); + paint.setAntiAlias(true); + paint.setTextAlign(Align.CENTER); + paint.setTextSize(textSize); + paint.setColor(color); + final Rect bounds = new Rect(); + paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds); + final int width = Math.round(bounds.width() + 0.5f); + final int height = Math.round(bounds.height() + 0.5f); + final Bitmap buffer = Bitmap.createBitmap( + width, (height * 3 / 2), Bitmap.Config.ARGB_8888); + final Canvas canvas = new Canvas(buffer); + canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint); + return new BitmapDrawable(res, buffer); + } + // Read integer value in TypedArray as percent. private static float getPercent(TypedArray a, int index, int defValue) { return a.getInt(index, defValue) / 100.0f; @@ -320,16 +321,6 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, return Color.argb(newAlpha, Color.red(color), Color.green(color), Color.blue(color)); } - public CharSequence getInvertedText(CharSequence text) { - if ((mSuggestionStripOption & AUTO_CORRECT_INVERT) == 0) - return null; - final int len = text.length(); - final Spannable word = new SpannableString(text); - word.setSpan(mInvertedBackgroundColorSpan, 0, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); - word.setSpan(mInvertedForegroundColorSpan, 0, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); - return word; - } - public void layout(SuggestedWords suggestions, ViewGroup stripView, ViewGroup placer, int stripWidth) { if (suggestions.isPunctuationSuggestions()) { @@ -522,8 +513,25 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, final Resources res = context.getResources(); mMoreSuggestionsModalTolerance = res.getDimensionPixelOffset( R.dimen.more_suggestions_modal_tolerance); + mMoreSuggestionsSlidingDetector = new GestureDetector( + context, mMoreSuggestionsSlidingListener); } + private final View.OnTouchListener mMoreSuggestionsCanceller = new View.OnTouchListener() { + @Override + public boolean onTouch(View view, MotionEvent me) { + if (!mMoreSuggestionsWindow.isShowing()) return false; + + switch (me.getAction()) { + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_POINTER_UP: + return mMoreSuggestionsView.dismissMoreKeysPanel(); + default: + return true; + } + } + }; + /** * A connection back to the input method. * @param listener @@ -534,21 +542,11 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, } public void setSuggestions(SuggestedWords suggestions) { - if (suggestions == null) + if (suggestions == null || suggestions.size() == 0) return; - mSuggestions = suggestions; - if (mShowingAutoCorrectionInverted) { - mHandler.postUpdateSuggestions(); - } else { - updateSuggestions(); - } - } - private void updateSuggestions() { clear(); - if (mSuggestions.size() == 0) - return; - + mSuggestions = suggestions; mParams.layout(mSuggestions, mSuggestionsStrip, this, getWidth()); } @@ -637,15 +635,6 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, } } - public void onAutoCorrectionInverted(CharSequence autoCorrectedWord) { - final CharSequence inverted = mParams.getInvertedText(autoCorrectedWord); - if (inverted == null) - return; - final TextView tv = mWords.get(1); - tv.setText(inverted); - mShowingAutoCorrectionInverted = true; - } - public boolean isShowingAddToDictionaryHint() { return mSuggestionsStrip.getChildCount() > 0 && mSuggestionsStrip.getChildAt(0) == mParams.mWordToSaveView; @@ -669,7 +658,6 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, } public void clear() { - mShowingAutoCorrectionInverted = false; mSuggestionsStrip.removeAllViews(); removeAllViews(); addView(mSuggestionsStrip); @@ -739,6 +727,7 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, if (mMoreSuggestionsWindow.isShowing()) { mMoreSuggestionsWindow.dismiss(); mKeyboardView.dimEntireKeyboard(false); + mKeyboardView.setOnTouchListener(null); return true; } return false; @@ -750,6 +739,10 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, @Override public boolean onLongClick(View view) { + return showMoreSuggestions(); + } + + private boolean showMoreSuggestions() { final SuggestionsViewParams params = mParams; if (params.mMoreSuggestionsAvailable) { final int stripWidth = getWidth(); @@ -770,29 +763,51 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, moreKeysPanel.showMoreKeysPanel( this, mMoreSuggestionsController, pointX, pointY, mMoreSuggestionsWindow, mMoreSuggestionsListener); - mCheckingIfModalOrSlidingMode = true; + mMoreSuggestionsMode = MORE_SUGGESTIONS_CHECKING_MODAL_OR_SLIDING; mOriginX = mLastX; mOriginY = mLastY; - view.setPressed(false); mKeyboardView.dimEntireKeyboard(true); + mKeyboardView.setOnTouchListener(mMoreSuggestionsCanceller); + for (int i = 0; i < params.mSuggestionsCountInStrip; i++) { + mWords.get(i).setPressed(false); + } return true; } return false; } // Working variables for onLongClick and dispatchTouchEvent. - private boolean mCheckingIfModalOrSlidingMode; + private int mMoreSuggestionsMode = MORE_SUGGESTIONS_IN_MODAL_MODE; + private static final int MORE_SUGGESTIONS_IN_MODAL_MODE = 0; + private static final int MORE_SUGGESTIONS_CHECKING_MODAL_OR_SLIDING = 1; + private static final int MORE_SUGGESTIONS_IN_SLIDING_MODE = 2; private int mLastX; private int mLastY; private int mOriginX; private int mOriginY; private final int mMoreSuggestionsModalTolerance; + private final GestureDetector mMoreSuggestionsSlidingDetector; + private final GestureDetector.OnGestureListener mMoreSuggestionsSlidingListener = + new GestureDetector.SimpleOnGestureListener() { + @Override + public boolean onScroll(MotionEvent down, MotionEvent me, float deltaX, float deltaY) { + final float dy = me.getY() - down.getY(); + if (deltaY > 0 && dy < 0) { + return showMoreSuggestions(); + } + return false; + } + }; @Override public boolean dispatchTouchEvent(MotionEvent me) { - if (!mMoreSuggestionsWindow.isShowing()) { + if (!mMoreSuggestionsWindow.isShowing() + || mMoreSuggestionsMode == MORE_SUGGESTIONS_IN_MODAL_MODE) { mLastX = (int)me.getX(); mLastY = (int)me.getY(); + if (mMoreSuggestionsSlidingDetector.onTouchEvent(me)) { + return true; + } return super.dispatchTouchEvent(me); } @@ -807,22 +822,22 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, final int translatedX = moreKeysPanel.translateX(x); final int translatedY = moreKeysPanel.translateY(y); - if (mCheckingIfModalOrSlidingMode) { + if (mMoreSuggestionsMode == MORE_SUGGESTIONS_CHECKING_MODAL_OR_SLIDING) { if (Math.abs(x - mOriginX) >= mMoreSuggestionsModalTolerance || mOriginY - y >= mMoreSuggestionsModalTolerance) { // Decided to be in the sliding input mode only when the touch point has been moved // upward. - mCheckingIfModalOrSlidingMode = false; + mMoreSuggestionsMode = MORE_SUGGESTIONS_IN_SLIDING_MODE; tracker.onShowMoreKeysPanel( translatedX, translatedY, SystemClock.uptimeMillis(), moreKeysPanel); } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) { // Decided to be in the modal input mode - mCheckingIfModalOrSlidingMode = false; + mMoreSuggestionsMode = MORE_SUGGESTIONS_IN_MODAL_MODE; } return true; } - // Process sliding motion events + // MORE_SUGGESTIONS_IN_SLIDING_MODE tracker.processMotionEvent(action, translatedX, translatedY, eventTime, moreKeysPanel); return true; } |