diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin/SuggestionsView.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/SuggestionsView.java | 117 |
1 files changed, 68 insertions, 49 deletions
diff --git a/java/src/com/android/inputmethod/latin/SuggestionsView.java b/java/src/com/android/inputmethod/latin/SuggestionsView.java index 10cd73dd3..22aa5e259 100644 --- a/java/src/com/android/inputmethod/latin/SuggestionsView.java +++ b/java/src/com/android/inputmethod/latin/SuggestionsView.java @@ -35,7 +35,6 @@ import android.text.style.ForegroundColorSpan; import android.text.style.StyleSpan; import android.text.style.UnderlineSpan; import android.util.AttributeSet; -import android.util.DisplayMetrics; import android.view.Gravity; import android.view.LayoutInflater; import android.view.MotionEvent; @@ -45,10 +44,10 @@ import android.view.View.OnLongClickListener; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.PopupWindow; +import android.widget.RelativeLayout; import android.widget.TextView; import com.android.inputmethod.compat.FrameLayoutCompatUtils; -import com.android.inputmethod.compat.LinearLayoutCompatUtils; import com.android.inputmethod.keyboard.KeyboardActionListener; import com.android.inputmethod.keyboard.KeyboardView; import com.android.inputmethod.keyboard.MoreKeysPanel; @@ -58,7 +57,8 @@ import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import java.util.ArrayList; import java.util.List; -public class SuggestionsView extends LinearLayout implements OnClickListener, OnLongClickListener { +public class SuggestionsView extends RelativeLayout implements OnClickListener, + OnLongClickListener { public interface Listener { public boolean addWordToDictionary(String word); public void pickSuggestionManually(int index, CharSequence word); @@ -69,7 +69,6 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On private static final boolean DBG = LatinImeLogger.sDBG; - private final ViewGroup mSuggestionsPlacer; private final ViewGroup mSuggestionsStrip; private KeyboardView mKeyboardView; @@ -146,12 +145,16 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On private static class SuggestionsViewParams { private static final int DEFAULT_SUGGESTIONS_COUNT_IN_STRIP = 3; private static final int DEFAULT_CENTER_SUGGESTION_PERCENTILE = 40; + private static final int DEFAULT_MAX_MORE_SUGGESTIONS_ROW = 2; private static final int PUNCTUATIONS_IN_STRIP = 6; public final int mPadding; public final int mDividerWidth; public final int mSuggestionsStripHeight; public final int mSuggestionsCountInStrip; + public final int mMaxMoreSuggestionsRow; + public final float mMinMoreSuggestionsWidth; + public final int mMoreSuggestionsBottomGap; private final List<TextView> mWords; private final List<View> mDividers; @@ -211,10 +214,17 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On mCenterSuggestionWeight = a.getInt( R.styleable.SuggestionsView_centerSuggestionPercentile, DEFAULT_CENTER_SUGGESTION_PERCENTILE) / 100.0f; + mMaxMoreSuggestionsRow = a.getInt( + R.styleable.SuggestionsView_maxMoreSuggestionsRow, + DEFAULT_MAX_MORE_SUGGESTIONS_ROW); + mMinMoreSuggestionsWidth = getRatio(a, + R.styleable.SuggestionsView_minMoreSuggestionsWidth); a.recycle(); mCenterSuggestionIndex = mSuggestionsCountInStrip / 2; mMoreSuggestionsHint = res.getDrawable(R.drawable.more_suggestions_hint); + mMoreSuggestionsBottomGap = res.getDimensionPixelOffset( + R.dimen.more_suggestions_bottom_gap); mInvertedForegroundColorSpan = new ForegroundColorSpan(mColorTypedWord ^ 0x00ffffff); mInvertedBackgroundColorSpan = new BackgroundColorSpan(mColorTypedWord); @@ -225,6 +235,11 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On mHintToSaveText = context.getText(R.string.hint_add_to_dictionary); } + // Read fraction value in TypedArray as float. + private static float getRatio(TypedArray a, int index) { + return a.getFraction(index, 1000, 1000, 1) / 1000.0f; + } + private CharSequence getStyledSuggestionWord(SuggestedWords suggestions, int pos) { final CharSequence word = suggestions.getWord(pos); final boolean isAutoCorrect = pos == 1 && willAutoCorrect(suggestions); @@ -451,18 +466,7 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On } public SuggestionsView(Context context, AttributeSet attrs, int defStyle) { - // Note: Up to version 10 (Gingerbread) of the API, LinearLayout doesn't have 3-argument - // constructor. - // TODO: Call 3-argument constructor, super(context, attrs, defStyle), when we abandon - // backward compatibility with the version 10 or earlier of the API. - super(context, attrs); - if (defStyle != R.attr.suggestionsViewStyle) { - throw new IllegalArgumentException( - "can't accept defStyle other than R.attr.suggestionsViewStyle: defStyle=" - + defStyle); - } - setBackgroundDrawable(LinearLayoutCompatUtils.getBackgroundDrawable( - context, attrs, defStyle, R.style.SuggestionsViewStyle)); + super(context, attrs, defStyle); final LayoutInflater inflater = LayoutInflater.from(context); inflater.inflate(R.layout.suggestions_strip, this); @@ -474,7 +478,6 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On mPreviewPopup.setContentView(mPreviewText); mPreviewPopup.setBackgroundDrawable(null); - mSuggestionsPlacer = (ViewGroup)findViewById(R.id.suggestions_placer); mSuggestionsStrip = (ViewGroup)findViewById(R.id.suggestions_strip); for (int pos = 0; pos < MAX_SUGGESTIONS; pos++) { final TextView word = (TextView)inflater.inflate(R.layout.suggestion_word, null); @@ -500,6 +503,9 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On mMoreSuggestionsWindow.setWindowLayoutMode( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); mMoreSuggestionsWindow.setBackgroundDrawable(null); + final Resources res = context.getResources(); + mMoreSuggestionsModalTolerance = res.getDimensionPixelOffset( + R.dimen.more_suggestions_modal_tolerance); } /** @@ -527,7 +533,7 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On if (mSuggestions.size() == 0) return; - mParams.layout(mSuggestions, mSuggestionsStrip, mSuggestionsPlacer, getWidth()); + mParams.layout(mSuggestions, mSuggestionsStrip, this, getWidth()); } private static CharSequence getDebugInfo(SuggestedWords suggestions, int pos) { @@ -648,9 +654,9 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On public void clear() { mShowingAutoCorrectionInverted = false; - mSuggestionsPlacer.removeAllViews(); - mSuggestionsPlacer.addView(mSuggestionsStrip); mSuggestionsStrip.removeAllViews(); + removeAllViews(); + addView(mSuggestionsStrip); dismissMoreSuggestions(); } @@ -730,27 +736,23 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On final View container = mMoreSuggestionsContainer; final int maxWidth = stripWidth - container.getPaddingLeft() - container.getPaddingRight(); - final DisplayMetrics dm = getContext().getResources().getDisplayMetrics(); - // TODO: Revise how we determine the height - final int maxHeight = dm.heightPixels - mKeyboardView.getHeight() - getHeight() * 3; final MoreSuggestions.Builder builder = mMoreSuggestionsBuilder; - builder.layout(mSuggestions, params.mSuggestionsCountInStrip, maxWidth, maxHeight); + builder.layout(mSuggestions, params.mSuggestionsCountInStrip, maxWidth, + (int)(maxWidth * params.mMinMoreSuggestionsWidth), + params.mMaxMoreSuggestionsRow); mMoreSuggestionsView.setKeyboard(builder.build()); container.measure( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); final MoreKeysPanel moreKeysPanel = mMoreSuggestionsView; final int pointX = stripWidth / 2; - final int pointY = 0; + final int pointY = -params.mMoreSuggestionsBottomGap; moreKeysPanel.showMoreKeysPanel( this, mMoreSuggestionsController, pointX, pointY, mMoreSuggestionsWindow, mMoreSuggestionsListener); - // TODO: Should figure out how to select the pointer tracker correctly. - final PointerTracker tracker = PointerTracker.getPointerTracker(0, moreKeysPanel); - final int translatedX = moreKeysPanel.translateX(tracker.getLastX()); - final int translatedY = moreKeysPanel.translateY(tracker.getLastY()); - tracker.onShowMoreKeysPanel( - translatedX, translatedY, SystemClock.uptimeMillis(), moreKeysPanel); + mCheckingIfModalOrSlidingMode = true; + mOriginX = mLastX; + mOriginY = mLastY; view.setPressed(false); mKeyboardView.dimEntireKeyboard(true); return true; @@ -758,34 +760,51 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On return false; } + // Working variables for onLongClick and dispatchTouchEvent. + private boolean mCheckingIfModalOrSlidingMode; + private int mLastX; + private int mLastY; + private int mOriginX; + private int mOriginY; + private final int mMoreSuggestionsModalTolerance; + @Override public boolean dispatchTouchEvent(MotionEvent me) { if (!mMoreSuggestionsWindow.isShowing()) { + mLastX = (int)me.getX(); + mLastY = (int)me.getY(); return super.dispatchTouchEvent(me); } + + final MoreKeysPanel moreKeysPanel = mMoreSuggestionsView; final int action = me.getAction(); final long eventTime = me.getEventTime(); final int index = me.getActionIndex(); final int id = me.getPointerId(index); - final PointerTracker tracker = PointerTracker.getPointerTracker(id, mMoreSuggestionsView); - final int x = mMoreSuggestionsView.translateX((int)me.getX(index)); - final int y = mMoreSuggestionsView.translateY((int)me.getY(index)); - switch (action) { - case MotionEvent.ACTION_DOWN: - case MotionEvent.ACTION_POINTER_DOWN: - tracker.onDownEvent(x, y, eventTime, mMoreSuggestionsView); - break; - case MotionEvent.ACTION_UP: - case MotionEvent.ACTION_POINTER_UP: - tracker.onUpEvent(x, y, eventTime); - break; - case MotionEvent.ACTION_MOVE: - tracker.onMoveEvent(x, y, eventTime); - break; - case MotionEvent.ACTION_CANCEL: - tracker.onCancelEvent(x, y, eventTime); - break; + final PointerTracker tracker = PointerTracker.getPointerTracker(id, moreKeysPanel); + final int x = (int)me.getX(index); + final int y = (int)me.getY(index); + final int translatedX = moreKeysPanel.translateX(x); + final int translatedY = moreKeysPanel.translateY(y); + + if (mCheckingIfModalOrSlidingMode) { + final int deltaX = Math.abs(x - mOriginX); + final int deltaY = Math.abs(y - mOriginY); + if (deltaX >= mMoreSuggestionsModalTolerance + || deltaY >= mMoreSuggestionsModalTolerance) { + // Decided to be in the sliding input mode + mCheckingIfModalOrSlidingMode = false; + 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; + } + return true; } + + // Process sliding motion events + tracker.processMotionEvent(action, translatedX, translatedY, eventTime, moreKeysPanel); return true; } |