2009-03-13 22:11:42 +00:00
|
|
|
/*
|
2011-09-01 05:54:28 +00:00
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
2010-12-06 12:26:38 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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
|
2010-12-06 12:26:38 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2010-12-06 12:26:38 +00:00
|
|
|
*
|
2009-03-13 22:11:42 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2009-03-13 22:11:42 +00:00
|
|
|
*/
|
|
|
|
|
2011-12-16 02:26:14 +00:00
|
|
|
package com.android.inputmethod.latin.suggestions;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
|
|
|
import android.content.Context;
|
2010-02-11 01:45:35 +00:00
|
|
|
import android.content.res.Resources;
|
2011-06-15 02:49:57 +00:00
|
|
|
import android.content.res.TypedArray;
|
2011-09-16 06:30:41 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.Canvas;
|
2011-01-19 08:29:27 +00:00
|
|
|
import android.graphics.Color;
|
2011-09-16 06:30:41 +00:00
|
|
|
import android.graphics.Paint;
|
|
|
|
import android.graphics.Paint.Align;
|
|
|
|
import android.graphics.Rect;
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.graphics.Typeface;
|
2011-09-16 06:30:41 +00:00
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
2011-07-04 06:42:20 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
2010-12-09 05:04:50 +00:00
|
|
|
import android.text.Spannable;
|
|
|
|
import android.text.SpannableString;
|
2010-12-10 06:24:28 +00:00
|
|
|
import android.text.Spanned;
|
2011-06-25 18:00:51 +00:00
|
|
|
import android.text.TextPaint;
|
2010-12-06 12:26:38 +00:00
|
|
|
import android.text.TextUtils;
|
2010-12-09 05:04:50 +00:00
|
|
|
import android.text.style.CharacterStyle;
|
2011-06-30 01:09:21 +00:00
|
|
|
import android.text.style.StyleSpan;
|
2010-12-09 05:04:50 +00:00
|
|
|
import android.text.style.UnderlineSpan;
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.util.AttributeSet;
|
2011-09-20 10:17:19 +00:00
|
|
|
import android.view.GestureDetector;
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.view.Gravity;
|
|
|
|
import android.view.LayoutInflater;
|
2011-08-24 05:44:46 +00:00
|
|
|
import android.view.MotionEvent;
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.view.View;
|
2010-12-06 12:26:38 +00:00
|
|
|
import android.view.View.OnClickListener;
|
2011-08-06 07:45:58 +00:00
|
|
|
import android.view.View.OnLongClickListener;
|
2010-12-10 06:24:28 +00:00
|
|
|
import android.view.ViewGroup;
|
2010-12-06 12:26:38 +00:00
|
|
|
import android.widget.LinearLayout;
|
2011-09-05 07:15:28 +00:00
|
|
|
import android.widget.RelativeLayout;
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
2012-10-01 05:26:38 +00:00
|
|
|
import com.android.inputmethod.keyboard.Keyboard;
|
|
|
|
import com.android.inputmethod.keyboard.KeyboardSwitcher;
|
2013-01-21 05:41:33 +00:00
|
|
|
import com.android.inputmethod.keyboard.MainKeyboardView;
|
2011-08-24 05:44:46 +00:00
|
|
|
import com.android.inputmethod.keyboard.MoreKeysPanel;
|
2012-04-02 13:55:12 +00:00
|
|
|
import com.android.inputmethod.keyboard.ViewLayoutUtils;
|
2012-06-28 11:32:01 +00:00
|
|
|
import com.android.inputmethod.latin.AutoCorrection;
|
2012-08-21 07:34:55 +00:00
|
|
|
import com.android.inputmethod.latin.CollectionUtils;
|
2012-10-15 20:59:35 +00:00
|
|
|
import com.android.inputmethod.latin.Constants;
|
2011-12-16 02:26:14 +00:00
|
|
|
import com.android.inputmethod.latin.LatinImeLogger;
|
|
|
|
import com.android.inputmethod.latin.R;
|
2012-08-29 13:02:00 +00:00
|
|
|
import com.android.inputmethod.latin.ResourceUtils;
|
2011-12-16 02:26:14 +00:00
|
|
|
import com.android.inputmethod.latin.SuggestedWords;
|
2013-04-04 08:45:21 +00:00
|
|
|
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
|
2012-03-15 05:56:09 +00:00
|
|
|
import com.android.inputmethod.latin.Utils;
|
2012-04-09 17:53:17 +00:00
|
|
|
import com.android.inputmethod.latin.define.ProductionFlag;
|
2013-05-01 08:02:27 +00:00
|
|
|
import com.android.inputmethod.latin.suggestions.MoreSuggestions.MoreSuggestionsListener;
|
2012-07-20 18:02:39 +00:00
|
|
|
import com.android.inputmethod.research.ResearchLogger;
|
2011-06-06 08:23:18 +00:00
|
|
|
|
2010-09-29 05:30:01 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2012-09-27 09:16:16 +00:00
|
|
|
public final class SuggestionStripView extends RelativeLayout implements OnClickListener,
|
2011-09-05 07:15:28 +00:00
|
|
|
OnLongClickListener {
|
2011-06-09 05:22:37 +00:00
|
|
|
public interface Listener {
|
2012-12-13 12:59:13 +00:00
|
|
|
public void addWordToUserDictionary(String word);
|
2013-04-04 08:45:21 +00:00
|
|
|
public void pickSuggestionManually(int index, SuggestedWordInfo word);
|
2011-06-09 05:22:37 +00:00
|
|
|
}
|
|
|
|
|
2011-06-14 07:28:57 +00:00
|
|
|
// The maximum number of suggestions available. See {@link Suggest#mPrefMaxSuggestions}.
|
2011-08-24 05:44:46 +00:00
|
|
|
public static final int MAX_SUGGESTIONS = 18;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2011-12-16 02:26:14 +00:00
|
|
|
static final boolean DBG = LatinImeLogger.sDBG;
|
2011-01-19 08:29:27 +00:00
|
|
|
|
2011-09-01 05:54:28 +00:00
|
|
|
private final ViewGroup mSuggestionsStrip;
|
2013-01-21 05:41:33 +00:00
|
|
|
MainKeyboardView mMainKeyboardView;
|
2011-06-30 01:09:21 +00:00
|
|
|
|
2011-08-24 05:44:46 +00:00
|
|
|
private final View mMoreSuggestionsContainer;
|
|
|
|
private final MoreSuggestionsView mMoreSuggestionsView;
|
|
|
|
private final MoreSuggestions.Builder mMoreSuggestionsBuilder;
|
|
|
|
|
2012-08-21 07:34:55 +00:00
|
|
|
private final ArrayList<TextView> mWords = CollectionUtils.newArrayList();
|
|
|
|
private final ArrayList<TextView> mInfos = CollectionUtils.newArrayList();
|
|
|
|
private final ArrayList<View> mDividers = CollectionUtils.newArrayList();
|
2011-06-30 01:09:21 +00:00
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
Listener mListener;
|
2013-05-01 08:02:27 +00:00
|
|
|
private SuggestedWords mSuggestedWords = SuggestedWords.EMPTY;
|
2010-02-03 23:35:49 +00:00
|
|
|
|
2012-07-23 06:28:28 +00:00
|
|
|
private final SuggestionStripViewParams mParams;
|
2011-08-06 07:45:58 +00:00
|
|
|
private static final float MIN_TEXT_XSCALE = 0.70f;
|
2011-06-25 18:00:51 +00:00
|
|
|
|
2012-09-27 09:16:16 +00:00
|
|
|
private static final class SuggestionStripViewParams {
|
2011-09-02 07:53:38 +00:00
|
|
|
private static final int DEFAULT_SUGGESTIONS_COUNT_IN_STRIP = 3;
|
2012-08-28 04:10:03 +00:00
|
|
|
private static final float DEFAULT_CENTER_SUGGESTION_PERCENTILE = 0.40f;
|
2011-09-05 08:22:34 +00:00
|
|
|
private static final int DEFAULT_MAX_MORE_SUGGESTIONS_ROW = 2;
|
2011-09-08 02:41:46 +00:00
|
|
|
private static final int PUNCTUATIONS_IN_STRIP = 5;
|
2011-09-02 07:53:38 +00:00
|
|
|
|
2011-06-30 01:09:21 +00:00
|
|
|
public final int mPadding;
|
|
|
|
public final int mDividerWidth;
|
2011-09-01 05:54:28 +00:00
|
|
|
public final int mSuggestionsStripHeight;
|
2011-09-02 07:53:38 +00:00
|
|
|
public final int mSuggestionsCountInStrip;
|
2012-03-07 06:30:16 +00:00
|
|
|
public final int mMoreSuggestionsRowHeight;
|
|
|
|
private int mMaxMoreSuggestionsRow;
|
2011-09-05 08:22:34 +00:00
|
|
|
public final float mMinMoreSuggestionsWidth;
|
2011-09-06 09:33:01 +00:00
|
|
|
public final int mMoreSuggestionsBottomGap;
|
2011-08-04 09:18:45 +00:00
|
|
|
|
2012-03-16 15:50:51 +00:00
|
|
|
private final ArrayList<TextView> mWords;
|
|
|
|
private final ArrayList<View> mDividers;
|
|
|
|
private final ArrayList<TextView> mInfos;
|
2011-08-04 05:38:22 +00:00
|
|
|
|
2011-12-15 07:38:17 +00:00
|
|
|
private final int mColorValidTypedWord;
|
2011-08-04 05:38:22 +00:00
|
|
|
private final int mColorTypedWord;
|
|
|
|
private final int mColorAutoCorrect;
|
2011-09-01 05:54:28 +00:00
|
|
|
private final int mColorSuggested;
|
2011-09-13 03:35:52 +00:00
|
|
|
private final float mAlphaObsoleted;
|
2011-09-01 05:54:28 +00:00
|
|
|
private final float mCenterSuggestionWeight;
|
|
|
|
private final int mCenterSuggestionIndex;
|
|
|
|
private final Drawable mMoreSuggestionsHint;
|
2011-09-23 21:59:33 +00:00
|
|
|
private static final String MORE_SUGGESTIONS_HINT = "\u2026";
|
2012-03-29 06:58:26 +00:00
|
|
|
private static final String LEFTWARDS_ARROW = "\u2190";
|
2011-08-04 05:38:22 +00:00
|
|
|
|
|
|
|
private static final CharacterStyle BOLD_SPAN = new StyleSpan(Typeface.BOLD);
|
|
|
|
private static final CharacterStyle UNDERLINE_SPAN = new UnderlineSpan();
|
|
|
|
private static final int AUTO_CORRECT_BOLD = 0x01;
|
|
|
|
private static final int AUTO_CORRECT_UNDERLINE = 0x02;
|
2011-09-21 06:04:50 +00:00
|
|
|
private static final int VALID_TYPED_WORD_BOLD = 0x04;
|
2011-08-04 05:38:22 +00:00
|
|
|
|
2011-08-17 01:18:58 +00:00
|
|
|
private final int mSuggestionStripOption;
|
2011-06-30 01:09:21 +00:00
|
|
|
|
2012-08-21 07:34:55 +00:00
|
|
|
private final ArrayList<CharSequence> mTexts = CollectionUtils.newArrayList();
|
2011-06-30 01:09:21 +00:00
|
|
|
|
2011-08-06 07:45:58 +00:00
|
|
|
public boolean mMoreSuggestionsAvailable;
|
|
|
|
|
2012-04-24 03:44:50 +00:00
|
|
|
private final TextView mWordToSaveView;
|
2012-03-29 06:58:26 +00:00
|
|
|
private final TextView mLeftwardsArrowView;
|
2011-08-26 06:13:30 +00:00
|
|
|
private final TextView mHintToSaveView;
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
public SuggestionStripViewParams(final Context context, final AttributeSet attrs,
|
|
|
|
final int defStyle, final ArrayList<TextView> words, final ArrayList<View> dividers,
|
|
|
|
final ArrayList<TextView> infos) {
|
2011-09-02 07:53:38 +00:00
|
|
|
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(
|
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
|
|
|
|
mDividerWidth = divider.getMeasuredWidth();
|
|
|
|
|
|
|
|
final Resources res = word.getResources();
|
|
|
|
mSuggestionsStripHeight = res.getDimensionPixelSize(R.dimen.suggestions_strip_height);
|
|
|
|
|
2012-07-23 06:28:28 +00:00
|
|
|
final TypedArray a = context.obtainStyledAttributes(attrs,
|
|
|
|
R.styleable.SuggestionStripView, defStyle, R.style.SuggestionStripViewStyle);
|
|
|
|
mSuggestionStripOption = a.getInt(
|
|
|
|
R.styleable.SuggestionStripView_suggestionStripOption, 0);
|
2012-08-29 13:02:00 +00:00
|
|
|
final float alphaValidTypedWord = ResourceUtils.getFraction(a,
|
2012-08-28 04:10:03 +00:00
|
|
|
R.styleable.SuggestionStripView_alphaValidTypedWord, 1.0f);
|
2012-08-29 13:02:00 +00:00
|
|
|
final float alphaTypedWord = ResourceUtils.getFraction(a,
|
2012-08-28 04:10:03 +00:00
|
|
|
R.styleable.SuggestionStripView_alphaTypedWord, 1.0f);
|
2012-08-29 13:02:00 +00:00
|
|
|
final float alphaAutoCorrect = ResourceUtils.getFraction(a,
|
2012-08-28 04:10:03 +00:00
|
|
|
R.styleable.SuggestionStripView_alphaAutoCorrect, 1.0f);
|
2012-08-29 13:02:00 +00:00
|
|
|
final float alphaSuggested = ResourceUtils.getFraction(a,
|
2012-08-28 04:10:03 +00:00
|
|
|
R.styleable.SuggestionStripView_alphaSuggested, 1.0f);
|
2012-08-29 13:02:00 +00:00
|
|
|
mAlphaObsoleted = ResourceUtils.getFraction(a,
|
2012-08-28 04:10:03 +00:00
|
|
|
R.styleable.SuggestionStripView_alphaSuggested, 1.0f);
|
2012-07-23 06:28:28 +00:00
|
|
|
mColorValidTypedWord = applyAlpha(a.getColor(
|
|
|
|
R.styleable.SuggestionStripView_colorValidTypedWord, 0), alphaValidTypedWord);
|
|
|
|
mColorTypedWord = applyAlpha(a.getColor(
|
|
|
|
R.styleable.SuggestionStripView_colorTypedWord, 0), alphaTypedWord);
|
|
|
|
mColorAutoCorrect = applyAlpha(a.getColor(
|
|
|
|
R.styleable.SuggestionStripView_colorAutoCorrect, 0), alphaAutoCorrect);
|
|
|
|
mColorSuggested = applyAlpha(a.getColor(
|
|
|
|
R.styleable.SuggestionStripView_colorSuggested, 0), alphaSuggested);
|
2011-09-01 05:54:28 +00:00
|
|
|
mSuggestionsCountInStrip = a.getInt(
|
2012-07-23 06:28:28 +00:00
|
|
|
R.styleable.SuggestionStripView_suggestionsCountInStrip,
|
2011-09-01 05:54:28 +00:00
|
|
|
DEFAULT_SUGGESTIONS_COUNT_IN_STRIP);
|
2012-08-29 13:02:00 +00:00
|
|
|
mCenterSuggestionWeight = ResourceUtils.getFraction(a,
|
2012-07-23 06:28:28 +00:00
|
|
|
R.styleable.SuggestionStripView_centerSuggestionPercentile,
|
2011-09-13 03:35:52 +00:00
|
|
|
DEFAULT_CENTER_SUGGESTION_PERCENTILE);
|
2011-09-05 08:22:34 +00:00
|
|
|
mMaxMoreSuggestionsRow = a.getInt(
|
2012-07-23 06:28:28 +00:00
|
|
|
R.styleable.SuggestionStripView_maxMoreSuggestionsRow,
|
2011-09-05 08:22:34 +00:00
|
|
|
DEFAULT_MAX_MORE_SUGGESTIONS_ROW);
|
2012-08-29 13:02:00 +00:00
|
|
|
mMinMoreSuggestionsWidth = ResourceUtils.getFraction(a,
|
2012-08-28 04:10:03 +00:00
|
|
|
R.styleable.SuggestionStripView_minMoreSuggestionsWidth, 1.0f);
|
2011-08-04 05:38:22 +00:00
|
|
|
a.recycle();
|
|
|
|
|
2011-09-16 06:30:41 +00:00
|
|
|
mMoreSuggestionsHint = getMoreSuggestionsHint(res,
|
|
|
|
res.getDimension(R.dimen.more_suggestions_hint_text_size), mColorAutoCorrect);
|
2011-09-01 05:54:28 +00:00
|
|
|
mCenterSuggestionIndex = mSuggestionsCountInStrip / 2;
|
2011-09-06 09:33:01 +00:00
|
|
|
mMoreSuggestionsBottomGap = res.getDimensionPixelOffset(
|
|
|
|
R.dimen.more_suggestions_bottom_gap);
|
2012-03-07 06:30:16 +00:00
|
|
|
mMoreSuggestionsRowHeight = res.getDimensionPixelSize(
|
|
|
|
R.dimen.more_suggestions_row_height);
|
2011-08-05 09:54:52 +00:00
|
|
|
|
2011-08-26 06:13:30 +00:00
|
|
|
final LayoutInflater inflater = LayoutInflater.from(context);
|
2011-09-01 05:54:28 +00:00
|
|
|
mWordToSaveView = (TextView)inflater.inflate(R.layout.suggestion_word, null);
|
2012-03-29 06:58:26 +00:00
|
|
|
mLeftwardsArrowView = (TextView)inflater.inflate(R.layout.hint_add_to_dictionary, null);
|
|
|
|
mHintToSaveView = (TextView)inflater.inflate(R.layout.hint_add_to_dictionary, null);
|
2011-06-30 01:09:21 +00:00
|
|
|
}
|
|
|
|
|
2012-03-07 06:30:16 +00:00
|
|
|
public int getMaxMoreSuggestionsRow() {
|
|
|
|
return mMaxMoreSuggestionsRow;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getMoreSuggestionsHeight() {
|
|
|
|
return mMaxMoreSuggestionsRow * mMoreSuggestionsRowHeight + mMoreSuggestionsBottomGap;
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
public int setMoreSuggestionsHeight(final int remainingHeight) {
|
2012-03-07 06:30:16 +00:00
|
|
|
final int currentHeight = getMoreSuggestionsHeight();
|
|
|
|
if (currentHeight <= remainingHeight) {
|
|
|
|
return currentHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMaxMoreSuggestionsRow = (remainingHeight - mMoreSuggestionsBottomGap)
|
|
|
|
/ mMoreSuggestionsRowHeight;
|
|
|
|
final int newHeight = getMoreSuggestionsHeight();
|
|
|
|
return newHeight;
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize,
|
|
|
|
final int color) {
|
2011-09-16 06:30:41 +00:00
|
|
|
final Paint paint = new Paint();
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setTextAlign(Align.CENTER);
|
|
|
|
paint.setTextSize(textSize);
|
|
|
|
paint.setColor(color);
|
|
|
|
final Rect bounds = new Rect();
|
2011-09-24 00:38:45 +00:00
|
|
|
paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds);
|
2011-09-16 06:30:41 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
private CharSequence getStyledSuggestionWord(final SuggestedWords suggestedWords,
|
|
|
|
final int pos) {
|
2012-10-03 06:19:43 +00:00
|
|
|
final String word = suggestedWords.getWord(pos);
|
2012-03-05 13:08:16 +00:00
|
|
|
final boolean isAutoCorrect = pos == 1 && suggestedWords.willAutoCorrect();
|
|
|
|
final boolean isTypedWordValid = pos == 0 && suggestedWords.mTypedWordValid;
|
2011-08-17 01:18:58 +00:00
|
|
|
if (!isAutoCorrect && !isTypedWordValid)
|
2011-08-04 05:38:22 +00:00
|
|
|
return word;
|
2011-08-17 01:18:58 +00:00
|
|
|
|
2011-08-04 05:38:22 +00:00
|
|
|
final int len = word.length();
|
|
|
|
final Spannable spannedWord = new SpannableString(word);
|
2011-08-17 01:18:58 +00:00
|
|
|
final int option = mSuggestionStripOption;
|
|
|
|
if ((isAutoCorrect && (option & AUTO_CORRECT_BOLD) != 0)
|
|
|
|
|| (isTypedWordValid && (option & VALID_TYPED_WORD_BOLD) != 0)) {
|
2011-08-04 05:38:22 +00:00
|
|
|
spannedWord.setSpan(BOLD_SPAN, 0, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
2011-08-17 01:18:58 +00:00
|
|
|
}
|
|
|
|
if (isAutoCorrect && (option & AUTO_CORRECT_UNDERLINE) != 0) {
|
2011-08-04 05:38:22 +00:00
|
|
|
spannedWord.setSpan(UNDERLINE_SPAN, 0, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
2011-08-17 01:18:58 +00:00
|
|
|
}
|
2011-08-04 05:38:22 +00:00
|
|
|
return spannedWord;
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
private int getWordPosition(final int index, final SuggestedWords suggestedWords) {
|
2011-08-05 09:54:52 +00:00
|
|
|
// TODO: This works for 3 suggestions. Revisit this algorithm when there are 5 or more
|
|
|
|
// suggestions.
|
2012-03-05 13:08:16 +00:00
|
|
|
final int centerPos = suggestedWords.willAutoCorrect() ? 1 : 0;
|
2011-09-01 05:54:28 +00:00
|
|
|
if (index == mCenterSuggestionIndex) {
|
2011-08-05 09:54:52 +00:00
|
|
|
return centerPos;
|
|
|
|
} else if (index == centerPos) {
|
2011-09-01 05:54:28 +00:00
|
|
|
return mCenterSuggestionIndex;
|
2011-08-05 09:54:52 +00:00
|
|
|
} else {
|
2011-08-04 05:38:22 +00:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
private int getSuggestionTextColor(final int index, final SuggestedWords suggestedWords,
|
|
|
|
final int pos) {
|
2011-08-04 05:38:22 +00:00
|
|
|
// TODO: Need to revisit this logic with bigram suggestions
|
2011-09-01 05:54:28 +00:00
|
|
|
final boolean isSuggested = (pos != 0);
|
2011-08-04 05:38:22 +00:00
|
|
|
|
|
|
|
final int color;
|
2012-03-05 13:08:16 +00:00
|
|
|
if (index == mCenterSuggestionIndex && suggestedWords.willAutoCorrect()) {
|
2011-08-04 05:38:22 +00:00
|
|
|
color = mColorAutoCorrect;
|
2012-03-05 13:08:16 +00:00
|
|
|
} else if (index == mCenterSuggestionIndex && suggestedWords.mTypedWordValid) {
|
2011-12-15 07:38:17 +00:00
|
|
|
color = mColorValidTypedWord;
|
2011-09-01 05:54:28 +00:00
|
|
|
} else if (isSuggested) {
|
|
|
|
color = mColorSuggested;
|
2011-08-04 05:38:22 +00:00
|
|
|
} else {
|
|
|
|
color = mColorTypedWord;
|
|
|
|
}
|
2012-03-12 07:50:58 +00:00
|
|
|
if (LatinImeLogger.sDBG && suggestedWords.size() > 1) {
|
2012-03-09 13:00:23 +00:00
|
|
|
// If we auto-correct, then the autocorrection is in slot 0 and the typed word
|
|
|
|
// is in slot 1.
|
2012-06-28 11:32:01 +00:00
|
|
|
if (index == mCenterSuggestionIndex
|
|
|
|
&& AutoCorrection.shouldBlockAutoCorrectionBySafetyNet(
|
2012-10-03 06:19:43 +00:00
|
|
|
suggestedWords.getWord(1), suggestedWords.getWord(0))) {
|
2011-10-13 05:25:26 +00:00
|
|
|
return 0xFFFF0000;
|
|
|
|
}
|
|
|
|
}
|
2011-08-05 09:54:52 +00:00
|
|
|
|
2012-03-15 05:08:04 +00:00
|
|
|
if (suggestedWords.mIsObsoleteSuggestions && isSuggested) {
|
2011-09-13 03:35:52 +00:00
|
|
|
return applyAlpha(color, mAlphaObsoleted);
|
2011-08-04 05:38:22 +00:00
|
|
|
} else {
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int applyAlpha(final int color, final float alpha) {
|
|
|
|
final int newAlpha = (int)(Color.alpha(color) * alpha);
|
|
|
|
return Color.argb(newAlpha, Color.red(color), Color.green(color), Color.blue(color));
|
|
|
|
}
|
|
|
|
|
2011-10-18 03:08:49 +00:00
|
|
|
private static void addDivider(final ViewGroup stripView, final View divider) {
|
|
|
|
stripView.addView(divider);
|
|
|
|
final LinearLayout.LayoutParams params =
|
|
|
|
(LinearLayout.LayoutParams)divider.getLayoutParams();
|
|
|
|
params.gravity = Gravity.CENTER;
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
public void layout(final SuggestedWords suggestedWords, final ViewGroup stripView,
|
|
|
|
final ViewGroup placer, final int stripWidth) {
|
2012-03-05 13:08:16 +00:00
|
|
|
if (suggestedWords.mIsPunctuationSuggestions) {
|
|
|
|
layoutPunctuationSuggestions(suggestedWords, stripView);
|
2011-09-01 05:13:01 +00:00
|
|
|
return;
|
2011-08-05 09:54:52 +00:00
|
|
|
}
|
|
|
|
|
2011-09-01 05:54:28 +00:00
|
|
|
final int countInStrip = mSuggestionsCountInStrip;
|
2012-03-05 13:08:16 +00:00
|
|
|
setupTexts(suggestedWords, countInStrip);
|
|
|
|
mMoreSuggestionsAvailable = (suggestedWords.size() > countInStrip);
|
2011-08-05 09:54:52 +00:00
|
|
|
int x = 0;
|
|
|
|
for (int index = 0; index < countInStrip; index++) {
|
2012-03-05 13:08:16 +00:00
|
|
|
final int pos = getWordPosition(index, suggestedWords);
|
2011-08-05 09:54:52 +00:00
|
|
|
|
|
|
|
if (index != 0) {
|
|
|
|
final View divider = mDividers.get(pos);
|
2011-09-01 05:54:28 +00:00
|
|
|
// Add divider if this isn't the left most suggestion in suggestions strip.
|
2011-10-18 03:08:49 +00:00
|
|
|
addDivider(stripView, divider);
|
2011-08-26 10:30:57 +00:00
|
|
|
x += divider.getMeasuredWidth();
|
2011-08-05 09:54:52 +00:00
|
|
|
}
|
|
|
|
|
2011-08-04 09:18:45 +00:00
|
|
|
final CharSequence styled = mTexts.get(pos);
|
2011-08-05 09:54:52 +00:00
|
|
|
final TextView word = mWords.get(pos);
|
2011-09-01 05:54:28 +00:00
|
|
|
if (index == mCenterSuggestionIndex && mMoreSuggestionsAvailable) {
|
2011-08-06 07:45:58 +00:00
|
|
|
// TODO: This "more suggestions hint" should have nicely designed icon.
|
|
|
|
word.setCompoundDrawablesWithIntrinsicBounds(
|
2011-09-01 05:54:28 +00:00
|
|
|
null, null, null, mMoreSuggestionsHint);
|
2011-08-24 08:35:42 +00:00
|
|
|
// HACK: To align with other TextView that has no compound drawables.
|
2011-09-01 05:54:28 +00:00
|
|
|
word.setCompoundDrawablePadding(-mMoreSuggestionsHint.getIntrinsicHeight());
|
2011-08-06 07:45:58 +00:00
|
|
|
} else {
|
2011-08-24 08:35:42 +00:00
|
|
|
word.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
|
2011-08-06 07:45:58 +00:00
|
|
|
}
|
|
|
|
|
2011-09-01 05:54:28 +00:00
|
|
|
// Disable this suggestion if the suggestion is null or empty.
|
2011-08-05 09:54:52 +00:00
|
|
|
word.setEnabled(!TextUtils.isEmpty(styled));
|
2012-03-05 13:08:16 +00:00
|
|
|
word.setTextColor(getSuggestionTextColor(index, suggestedWords, pos));
|
2011-09-01 05:54:28 +00:00
|
|
|
final int width = getSuggestionWidth(index, stripWidth);
|
2011-08-05 09:54:52 +00:00
|
|
|
final CharSequence text = getEllipsizedText(styled, width, word.getPaint());
|
|
|
|
final float scaleX = word.getTextScaleX();
|
|
|
|
word.setText(text); // TextView.setText() resets text scale x to 1.0.
|
|
|
|
word.setTextScaleX(scaleX);
|
|
|
|
stripView.addView(word);
|
2011-08-31 04:00:32 +00:00
|
|
|
setLayoutWeight(
|
2011-09-01 05:54:28 +00:00
|
|
|
word, getSuggestionWeight(index), ViewGroup.LayoutParams.MATCH_PARENT);
|
2011-08-26 10:30:57 +00:00
|
|
|
x += word.getMeasuredWidth();
|
2011-08-04 09:18:45 +00:00
|
|
|
|
2012-03-15 05:56:09 +00:00
|
|
|
if (DBG && pos < suggestedWords.size()) {
|
2012-10-03 06:19:43 +00:00
|
|
|
final String debugInfo = Utils.getDebugInfo(suggestedWords, pos);
|
2011-08-04 09:18:45 +00:00
|
|
|
if (debugInfo != null) {
|
2011-08-05 09:54:52 +00:00
|
|
|
final TextView info = mInfos.get(pos);
|
2011-08-04 09:18:45 +00:00
|
|
|
info.setText(debugInfo);
|
2011-08-26 10:30:57 +00:00
|
|
|
placer.addView(info);
|
2011-08-31 04:00:32 +00:00
|
|
|
info.measure(ViewGroup.LayoutParams.WRAP_CONTENT,
|
|
|
|
ViewGroup.LayoutParams.WRAP_CONTENT);
|
2011-08-05 09:54:52 +00:00
|
|
|
final int infoWidth = info.getMeasuredWidth();
|
2011-08-04 09:18:45 +00:00
|
|
|
final int y = info.getMeasuredHeight();
|
2012-04-02 13:55:12 +00:00
|
|
|
ViewLayoutUtils.placeViewAt(
|
2011-08-26 10:30:57 +00:00
|
|
|
info, x - infoWidth, y, infoWidth, info.getMeasuredHeight());
|
2011-08-04 09:18:45 +00:00
|
|
|
}
|
2011-08-05 09:54:52 +00:00
|
|
|
}
|
2011-08-04 09:18:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
private int getSuggestionWidth(final int index, final int maxWidth) {
|
2011-09-01 05:54:28 +00:00
|
|
|
final int paddings = mPadding * mSuggestionsCountInStrip;
|
|
|
|
final int dividers = mDividerWidth * (mSuggestionsCountInStrip - 1);
|
2011-08-05 09:54:52 +00:00
|
|
|
final int availableWidth = maxWidth - paddings - dividers;
|
2011-09-01 05:54:28 +00:00
|
|
|
return (int)(availableWidth * getSuggestionWeight(index));
|
2011-08-05 09:54:52 +00:00
|
|
|
}
|
2011-06-30 01:09:21 +00:00
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
private float getSuggestionWeight(final int index) {
|
2011-09-01 05:54:28 +00:00
|
|
|
if (index == mCenterSuggestionIndex) {
|
|
|
|
return mCenterSuggestionWeight;
|
2011-08-05 09:54:52 +00:00
|
|
|
} else {
|
|
|
|
// TODO: Revisit this for cases of 5 or more suggestions
|
2011-09-01 05:54:28 +00:00
|
|
|
return (1.0f - mCenterSuggestionWeight) / (mSuggestionsCountInStrip - 1);
|
2011-06-30 01:09:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
private void setupTexts(final SuggestedWords suggestedWords, final int countInStrip) {
|
2011-06-30 01:09:21 +00:00
|
|
|
mTexts.clear();
|
2012-03-05 13:08:16 +00:00
|
|
|
final int count = Math.min(suggestedWords.size(), countInStrip);
|
2011-08-05 09:54:52 +00:00
|
|
|
for (int pos = 0; pos < count; pos++) {
|
2012-03-05 13:08:16 +00:00
|
|
|
final CharSequence styled = getStyledSuggestionWord(suggestedWords, pos);
|
2011-06-30 01:09:21 +00:00
|
|
|
mTexts.add(styled);
|
|
|
|
}
|
2011-08-05 09:54:52 +00:00
|
|
|
for (int pos = count; pos < countInStrip; pos++) {
|
|
|
|
// Make this inactive for touches in layout().
|
|
|
|
mTexts.add(null);
|
|
|
|
}
|
2011-06-30 01:09:21 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
private void layoutPunctuationSuggestions(final SuggestedWords suggestedWords,
|
|
|
|
final ViewGroup stripView) {
|
2012-03-05 13:08:16 +00:00
|
|
|
final int countInStrip = Math.min(suggestedWords.size(), PUNCTUATIONS_IN_STRIP);
|
2011-08-05 09:54:52 +00:00
|
|
|
for (int index = 0; index < countInStrip; index++) {
|
|
|
|
if (index != 0) {
|
2011-09-01 05:54:28 +00:00
|
|
|
// Add divider if this isn't the left most suggestion in suggestions strip.
|
2011-10-18 03:08:49 +00:00
|
|
|
addDivider(stripView, mDividers.get(index));
|
2011-08-05 09:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final TextView word = mWords.get(index);
|
|
|
|
word.setEnabled(true);
|
2011-12-15 07:38:17 +00:00
|
|
|
word.setTextColor(mColorAutoCorrect);
|
2012-10-03 06:19:43 +00:00
|
|
|
final String text = suggestedWords.getWord(index);
|
2011-08-05 09:54:52 +00:00
|
|
|
word.setText(text);
|
|
|
|
word.setTextScaleX(1.0f);
|
2011-08-06 07:45:58 +00:00
|
|
|
word.setCompoundDrawables(null, null, null, null);
|
2011-08-05 09:54:52 +00:00
|
|
|
stripView.addView(word);
|
2011-09-01 05:54:28 +00:00
|
|
|
setLayoutWeight(word, 1.0f, mSuggestionsStripHeight);
|
2011-08-05 09:54:52 +00:00
|
|
|
}
|
2011-08-06 07:45:58 +00:00
|
|
|
mMoreSuggestionsAvailable = false;
|
2011-06-30 01:09:21 +00:00
|
|
|
}
|
2011-08-26 06:13:30 +00:00
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
public void layoutAddToDictionaryHint(final String word, final ViewGroup stripView,
|
2012-10-03 05:46:47 +00:00
|
|
|
final int stripWidth, final CharSequence hintText, final OnClickListener listener) {
|
2011-08-26 06:13:30 +00:00
|
|
|
final int width = stripWidth - mDividerWidth - mPadding * 2;
|
|
|
|
|
|
|
|
final TextView wordView = mWordToSaveView;
|
|
|
|
wordView.setTextColor(mColorTypedWord);
|
2011-09-01 05:54:28 +00:00
|
|
|
final int wordWidth = (int)(width * mCenterSuggestionWeight);
|
2011-08-26 06:13:30 +00:00
|
|
|
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);
|
2011-09-01 05:54:28 +00:00
|
|
|
setLayoutWeight(wordView, mCenterSuggestionWeight, ViewGroup.LayoutParams.MATCH_PARENT);
|
2011-08-26 06:13:30 +00:00
|
|
|
|
|
|
|
stripView.addView(mDividers.get(0));
|
|
|
|
|
2012-03-29 06:58:26 +00:00
|
|
|
final TextView leftArrowView = mLeftwardsArrowView;
|
|
|
|
leftArrowView.setTextColor(mColorAutoCorrect);
|
|
|
|
leftArrowView.setText(LEFTWARDS_ARROW);
|
|
|
|
stripView.addView(leftArrowView);
|
|
|
|
|
2011-08-26 06:13:30 +00:00
|
|
|
final TextView hintView = mHintToSaveView;
|
2012-03-29 06:58:26 +00:00
|
|
|
hintView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
|
2011-08-26 06:13:30 +00:00
|
|
|
hintView.setTextColor(mColorAutoCorrect);
|
2012-03-29 06:58:26 +00:00
|
|
|
final int hintWidth = width - wordWidth - leftArrowView.getWidth();
|
2011-10-27 15:33:15 +00:00
|
|
|
final float hintScaleX = getTextScaleX(hintText, hintWidth, hintView.getPaint());
|
|
|
|
hintView.setText(hintText);
|
2011-08-26 06:13:30 +00:00
|
|
|
hintView.setTextScaleX(hintScaleX);
|
|
|
|
stripView.addView(hintView);
|
2011-08-31 04:00:32 +00:00
|
|
|
setLayoutWeight(
|
2011-09-01 05:54:28 +00:00
|
|
|
hintView, 1.0f - mCenterSuggestionWeight, ViewGroup.LayoutParams.MATCH_PARENT);
|
2012-04-24 03:44:50 +00:00
|
|
|
|
|
|
|
wordView.setOnClickListener(listener);
|
|
|
|
leftArrowView.setOnClickListener(listener);
|
|
|
|
hintView.setOnClickListener(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public CharSequence getAddToDictionaryWord() {
|
|
|
|
return (CharSequence)mWordToSaveView.getTag();
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
public boolean isAddToDictionaryShowing(final View v) {
|
2012-04-24 03:44:50 +00:00
|
|
|
return v == mWordToSaveView || v == mHintToSaveView || v == mLeftwardsArrowView;
|
2011-08-26 06:13:30 +00:00
|
|
|
}
|
2011-12-15 10:12:41 +00:00
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
private static void setLayoutWeight(final View v, final float weight, final int height) {
|
2011-12-15 10:12:41 +00:00
|
|
|
final ViewGroup.LayoutParams lp = v.getLayoutParams();
|
|
|
|
if (lp instanceof LinearLayout.LayoutParams) {
|
|
|
|
final LinearLayout.LayoutParams llp = (LinearLayout.LayoutParams)lp;
|
|
|
|
llp.weight = weight;
|
|
|
|
llp.width = 0;
|
|
|
|
llp.height = height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
private static float getTextScaleX(final CharSequence text, final int maxWidth,
|
|
|
|
final TextPaint paint) {
|
2011-12-15 10:12:41 +00:00
|
|
|
paint.setTextScaleX(1.0f);
|
|
|
|
final int width = getTextWidth(text, paint);
|
|
|
|
if (width <= maxWidth) {
|
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
return maxWidth / (float)width;
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
private static CharSequence getEllipsizedText(final CharSequence text, final int maxWidth,
|
|
|
|
final TextPaint paint) {
|
2011-12-15 10:12:41 +00:00
|
|
|
if (text == null) return null;
|
|
|
|
paint.setTextScaleX(1.0f);
|
|
|
|
final int width = getTextWidth(text, paint);
|
|
|
|
if (width <= maxWidth) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
final float scaleX = maxWidth / (float)width;
|
|
|
|
if (scaleX >= MIN_TEXT_XSCALE) {
|
|
|
|
paint.setTextScaleX(scaleX);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that TextUtils.ellipsize() use text-x-scale as 1.0 if ellipsize is needed. To
|
|
|
|
// get squeezed and ellipsized text, passes enlarged width (maxWidth / MIN_TEXT_XSCALE).
|
|
|
|
final CharSequence ellipsized = TextUtils.ellipsize(
|
|
|
|
text, paint, maxWidth / MIN_TEXT_XSCALE, TextUtils.TruncateAt.MIDDLE);
|
|
|
|
paint.setTextScaleX(MIN_TEXT_XSCALE);
|
|
|
|
return ellipsized;
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
private static int getTextWidth(final CharSequence text, final TextPaint paint) {
|
2011-12-15 10:12:41 +00:00
|
|
|
if (TextUtils.isEmpty(text)) return 0;
|
|
|
|
final Typeface savedTypeface = paint.getTypeface();
|
|
|
|
paint.setTypeface(getTextTypeface(text));
|
|
|
|
final int len = text.length();
|
|
|
|
final float[] widths = new float[len];
|
|
|
|
final int count = paint.getTextWidths(text, 0, len, widths);
|
|
|
|
int width = 0;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
width += Math.round(widths[i] + 0.5f);
|
|
|
|
}
|
|
|
|
paint.setTypeface(savedTypeface);
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
private static Typeface getTextTypeface(final CharSequence text) {
|
2011-12-15 10:12:41 +00:00
|
|
|
if (!(text instanceof SpannableString))
|
|
|
|
return Typeface.DEFAULT;
|
|
|
|
|
|
|
|
final SpannableString ss = (SpannableString)text;
|
|
|
|
final StyleSpan[] styles = ss.getSpans(0, text.length(), StyleSpan.class);
|
|
|
|
if (styles.length == 0)
|
|
|
|
return Typeface.DEFAULT;
|
|
|
|
|
|
|
|
switch (styles[0].getStyle()) {
|
|
|
|
case Typeface.BOLD: return Typeface.DEFAULT_BOLD;
|
|
|
|
// TODO: BOLD_ITALIC, ITALIC case?
|
|
|
|
default: return Typeface.DEFAULT;
|
|
|
|
}
|
|
|
|
}
|
2011-06-30 01:09:21 +00:00
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
2012-07-23 06:28:28 +00:00
|
|
|
* Construct a {@link SuggestionStripView} for showing suggestions to be picked by the user.
|
2009-03-13 22:11:42 +00:00
|
|
|
* @param context
|
|
|
|
* @param attrs
|
|
|
|
*/
|
2012-10-03 05:46:47 +00:00
|
|
|
public SuggestionStripView(final Context context, final AttributeSet attrs) {
|
2012-07-23 06:28:28 +00:00
|
|
|
this(context, attrs, R.attr.suggestionStripViewStyle);
|
2011-06-15 02:49:57 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
public SuggestionStripView(final Context context, final AttributeSet attrs,
|
|
|
|
final int defStyle) {
|
2011-09-05 07:15:28 +00:00
|
|
|
super(context, attrs, defStyle);
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2011-08-04 09:18:45 +00:00
|
|
|
final LayoutInflater inflater = LayoutInflater.from(context);
|
2011-09-01 05:54:28 +00:00
|
|
|
inflater.inflate(R.layout.suggestions_strip, this);
|
2011-06-14 07:28:57 +00:00
|
|
|
|
2011-09-01 05:54:28 +00:00
|
|
|
mSuggestionsStrip = (ViewGroup)findViewById(R.id.suggestions_strip);
|
2011-08-05 09:54:52 +00:00
|
|
|
for (int pos = 0; pos < MAX_SUGGESTIONS; pos++) {
|
2011-09-01 05:54:28 +00:00
|
|
|
final TextView word = (TextView)inflater.inflate(R.layout.suggestion_word, null);
|
2011-08-05 09:54:52 +00:00
|
|
|
word.setTag(pos);
|
2011-06-24 05:19:59 +00:00
|
|
|
word.setOnClickListener(this);
|
2011-08-06 07:45:58 +00:00
|
|
|
word.setOnLongClickListener(this);
|
2011-06-24 05:19:59 +00:00
|
|
|
mWords.add(word);
|
2011-09-01 05:54:28 +00:00
|
|
|
final View divider = inflater.inflate(R.layout.suggestion_divider, null);
|
2011-08-05 09:54:52 +00:00
|
|
|
divider.setTag(pos);
|
2011-07-28 21:15:24 +00:00
|
|
|
divider.setOnClickListener(this);
|
|
|
|
mDividers.add(divider);
|
2011-09-01 05:54:28 +00:00
|
|
|
mInfos.add((TextView)inflater.inflate(R.layout.suggestion_info, null));
|
2010-09-30 03:10:03 +00:00
|
|
|
}
|
2010-09-29 05:30:01 +00:00
|
|
|
|
2012-07-23 06:28:28 +00:00
|
|
|
mParams = new SuggestionStripViewParams(
|
|
|
|
context, attrs, defStyle, mWords, mDividers, mInfos);
|
2011-08-24 05:44:46 +00:00
|
|
|
|
|
|
|
mMoreSuggestionsContainer = inflater.inflate(R.layout.more_suggestions, null);
|
|
|
|
mMoreSuggestionsView = (MoreSuggestionsView)mMoreSuggestionsContainer
|
|
|
|
.findViewById(R.id.more_suggestions_view);
|
2013-04-11 03:08:36 +00:00
|
|
|
mMoreSuggestionsBuilder = new MoreSuggestions.Builder(context, mMoreSuggestionsView);
|
2011-09-29 05:11:21 +00:00
|
|
|
|
2011-09-06 02:37:51 +00:00
|
|
|
final Resources res = context.getResources();
|
|
|
|
mMoreSuggestionsModalTolerance = res.getDimensionPixelOffset(
|
|
|
|
R.dimen.more_suggestions_modal_tolerance);
|
2011-09-20 10:17:19 +00:00
|
|
|
mMoreSuggestionsSlidingDetector = new GestureDetector(
|
|
|
|
context, mMoreSuggestionsSlidingListener);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-09-30 03:10:03 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
2011-06-09 05:22:37 +00:00
|
|
|
* A connection back to the input method.
|
2009-03-13 22:11:42 +00:00
|
|
|
* @param listener
|
|
|
|
*/
|
2012-10-03 05:46:47 +00:00
|
|
|
public void setListener(final Listener listener, final View inputView) {
|
2011-06-09 05:22:37 +00:00
|
|
|
mListener = listener;
|
2013-01-21 05:41:33 +00:00
|
|
|
mMainKeyboardView = (MainKeyboardView)inputView.findViewById(R.id.keyboard_view);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
public void setSuggestions(final SuggestedWords suggestedWords) {
|
2010-12-06 12:26:38 +00:00
|
|
|
clear();
|
2012-03-05 13:08:16 +00:00
|
|
|
mSuggestedWords = suggestedWords;
|
|
|
|
mParams.layout(mSuggestedWords, mSuggestionsStrip, this, getWidth());
|
2013-03-18 09:21:18 +00:00
|
|
|
if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
|
2012-07-23 06:28:28 +00:00
|
|
|
ResearchLogger.suggestionStripView_setSuggestions(mSuggestedWords);
|
2012-04-09 17:53:17 +00:00
|
|
|
}
|
2011-08-04 09:18:45 +00:00
|
|
|
}
|
2011-06-30 01:09:21 +00:00
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
public int setMoreSuggestionsHeight(final int remainingHeight) {
|
2012-03-07 06:30:16 +00:00
|
|
|
return mParams.setMoreSuggestionsHeight(remainingHeight);
|
|
|
|
}
|
2011-06-25 18:00:51 +00:00
|
|
|
|
2010-08-26 19:22:58 +00:00
|
|
|
public boolean isShowingAddToDictionaryHint() {
|
2011-09-01 05:54:28 +00:00
|
|
|
return mSuggestionsStrip.getChildCount() > 0
|
2012-04-24 03:44:50 +00:00
|
|
|
&& mParams.isAddToDictionaryShowing(mSuggestionsStrip.getChildAt(0));
|
2010-08-26 19:22:58 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
public void showAddToDictionaryHint(final String word, final CharSequence hintText) {
|
2011-08-26 06:13:30 +00:00
|
|
|
clear();
|
2012-04-24 03:44:50 +00:00
|
|
|
mParams.layoutAddToDictionaryHint(word, mSuggestionsStrip, getWidth(), hintText, this);
|
2010-02-11 01:45:35 +00:00
|
|
|
}
|
|
|
|
|
2010-08-03 18:47:42 +00:00
|
|
|
public boolean dismissAddToDictionaryHint() {
|
2011-08-26 06:13:30 +00:00
|
|
|
if (isShowingAddToDictionaryHint()) {
|
|
|
|
clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-08-03 18:47:42 +00:00
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
public void clear() {
|
2011-09-01 05:54:28 +00:00
|
|
|
mSuggestionsStrip.removeAllViews();
|
2011-09-05 07:15:28 +00:00
|
|
|
removeAllViews();
|
|
|
|
addView(mSuggestionsStrip);
|
2011-09-01 08:50:51 +00:00
|
|
|
dismissMoreSuggestions();
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2013-05-01 08:02:27 +00:00
|
|
|
private final MoreSuggestionsListener mMoreSuggestionsListener = new MoreSuggestionsListener() {
|
2011-08-24 05:44:46 +00:00
|
|
|
@Override
|
2013-05-01 08:02:27 +00:00
|
|
|
public void onSuggestionSelected(final int index, final SuggestedWordInfo wordInfo) {
|
2013-04-04 08:45:21 +00:00
|
|
|
mListener.pickSuggestionManually(index, wordInfo);
|
2011-09-01 08:50:51 +00:00
|
|
|
dismissMoreSuggestions();
|
2011-08-24 05:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCancelInput() {
|
2011-09-01 08:50:51 +00:00
|
|
|
dismissMoreSuggestions();
|
2011-08-24 05:44:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private final MoreKeysPanel.Controller mMoreSuggestionsController =
|
|
|
|
new MoreKeysPanel.Controller() {
|
|
|
|
@Override
|
2012-12-03 06:49:10 +00:00
|
|
|
public boolean onDismissMoreKeysPanel() {
|
2013-01-21 05:41:33 +00:00
|
|
|
return mMainKeyboardView.onDismissMoreKeysPanel();
|
2012-12-03 06:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-01-22 05:32:47 +00:00
|
|
|
public void onShowMoreKeysPanel(final MoreKeysPanel panel) {
|
2013-01-21 05:41:33 +00:00
|
|
|
mMainKeyboardView.onShowMoreKeysPanel(panel);
|
2011-08-24 05:44:46 +00:00
|
|
|
}
|
2012-12-10 20:38:29 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCancelMoreKeysPanel() {
|
|
|
|
dismissMoreSuggestions();
|
|
|
|
}
|
2011-08-24 05:44:46 +00:00
|
|
|
};
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
boolean dismissMoreSuggestions() {
|
2012-12-03 06:49:10 +00:00
|
|
|
return mMoreSuggestionsView.dismissMoreKeysPanel();
|
2011-09-01 08:50:51 +00:00
|
|
|
}
|
|
|
|
|
2011-08-06 07:45:58 +00:00
|
|
|
@Override
|
2012-10-03 05:46:47 +00:00
|
|
|
public boolean onLongClick(final View view) {
|
2012-10-15 20:59:35 +00:00
|
|
|
KeyboardSwitcher.getInstance().hapticAndAudioFeedback(Constants.NOT_A_CODE);
|
2011-09-20 10:17:19 +00:00
|
|
|
return showMoreSuggestions();
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:46:47 +00:00
|
|
|
boolean showMoreSuggestions() {
|
2012-10-01 05:26:38 +00:00
|
|
|
final Keyboard parentKeyboard = KeyboardSwitcher.getInstance().getKeyboard();
|
|
|
|
if (parentKeyboard == null) {
|
|
|
|
return false;
|
2011-08-06 07:45:58 +00:00
|
|
|
}
|
2012-10-01 05:26:38 +00:00
|
|
|
final SuggestionStripViewParams params = mParams;
|
|
|
|
if (!params.mMoreSuggestionsAvailable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
final int stripWidth = getWidth();
|
|
|
|
final View container = mMoreSuggestionsContainer;
|
|
|
|
final int maxWidth = stripWidth - container.getPaddingLeft() - container.getPaddingRight();
|
|
|
|
final MoreSuggestions.Builder builder = mMoreSuggestionsBuilder;
|
|
|
|
builder.layout(mSuggestedWords, params.mSuggestionsCountInStrip, maxWidth,
|
|
|
|
(int)(maxWidth * params.mMinMoreSuggestionsWidth),
|
|
|
|
params.getMaxMoreSuggestionsRow(), parentKeyboard);
|
|
|
|
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 = -params.mMoreSuggestionsBottomGap;
|
|
|
|
moreKeysPanel.showMoreKeysPanel(this, mMoreSuggestionsController, pointX, pointY,
|
2012-12-03 06:49:10 +00:00
|
|
|
mMoreSuggestionsListener);
|
2012-10-01 05:26:38 +00:00
|
|
|
mMoreSuggestionsMode = MORE_SUGGESTIONS_CHECKING_MODAL_OR_SLIDING;
|
|
|
|
mOriginX = mLastX;
|
|
|
|
mOriginY = mLastY;
|
|
|
|
for (int i = 0; i < params.mSuggestionsCountInStrip; i++) {
|
|
|
|
mWords.get(i).setPressed(false);
|
|
|
|
}
|
|
|
|
return true;
|
2011-08-06 07:45:58 +00:00
|
|
|
}
|
|
|
|
|
2011-09-06 02:37:51 +00:00
|
|
|
// Working variables for onLongClick and dispatchTouchEvent.
|
2011-09-16 09:24:03 +00:00
|
|
|
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;
|
2011-09-06 02:37:51 +00:00
|
|
|
private int mLastX;
|
|
|
|
private int mLastY;
|
|
|
|
private int mOriginX;
|
|
|
|
private int mOriginY;
|
|
|
|
private final int mMoreSuggestionsModalTolerance;
|
2011-09-20 10:17:19 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
2011-09-06 02:37:51 +00:00
|
|
|
|
2010-12-06 12:26:38 +00:00
|
|
|
@Override
|
2012-10-03 05:46:47 +00:00
|
|
|
public boolean dispatchTouchEvent(final MotionEvent me) {
|
2013-04-15 03:57:10 +00:00
|
|
|
if (!mMoreSuggestionsView.isShowingInParent()) {
|
2011-09-06 02:37:51 +00:00
|
|
|
mLastX = (int)me.getX();
|
|
|
|
mLastY = (int)me.getY();
|
2011-09-20 10:17:19 +00:00
|
|
|
if (mMoreSuggestionsSlidingDetector.onTouchEvent(me)) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-08-24 05:44:46 +00:00
|
|
|
return super.dispatchTouchEvent(me);
|
|
|
|
}
|
2011-09-06 06:25:58 +00:00
|
|
|
|
|
|
|
final MoreKeysPanel moreKeysPanel = mMoreSuggestionsView;
|
2011-08-24 05:44:46 +00:00
|
|
|
final int action = me.getAction();
|
|
|
|
final long eventTime = me.getEventTime();
|
|
|
|
final int index = me.getActionIndex();
|
|
|
|
final int id = me.getPointerId(index);
|
2011-09-06 06:25:58 +00:00
|
|
|
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);
|
|
|
|
|
2011-09-16 09:24:03 +00:00
|
|
|
if (mMoreSuggestionsMode == MORE_SUGGESTIONS_CHECKING_MODAL_OR_SLIDING) {
|
2011-09-13 03:50:12 +00:00
|
|
|
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.
|
2011-09-16 09:24:03 +00:00
|
|
|
mMoreSuggestionsMode = MORE_SUGGESTIONS_IN_SLIDING_MODE;
|
2011-09-06 02:37:51 +00:00
|
|
|
} else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) {
|
|
|
|
// Decided to be in the modal input mode
|
2011-09-16 09:24:03 +00:00
|
|
|
mMoreSuggestionsMode = MORE_SUGGESTIONS_IN_MODAL_MODE;
|
2013-04-15 03:57:10 +00:00
|
|
|
mMoreSuggestionsView.adjustVerticalCorrectionForModalMode();
|
2011-09-06 02:37:51 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-16 09:24:03 +00:00
|
|
|
// MORE_SUGGESTIONS_IN_SLIDING_MODE
|
2012-12-03 02:47:52 +00:00
|
|
|
mMoreSuggestionsView.processMotionEvent(action, translatedX, translatedY, id, eventTime);
|
2011-08-24 05:44:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-03 05:46:47 +00:00
|
|
|
public void onClick(final View view) {
|
2012-04-24 03:44:50 +00:00
|
|
|
if (mParams.isAddToDictionaryShowing(view)) {
|
2012-07-05 02:28:05 +00:00
|
|
|
mListener.addWordToUserDictionary(mParams.getAddToDictionaryWord().toString());
|
2011-06-27 07:57:18 +00:00
|
|
|
clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-14 07:28:57 +00:00
|
|
|
final Object tag = view.getTag();
|
|
|
|
if (!(tag instanceof Integer))
|
|
|
|
return;
|
|
|
|
final int index = (Integer) tag;
|
2012-03-05 13:08:16 +00:00
|
|
|
if (index >= mSuggestedWords.size())
|
2011-06-10 00:53:27 +00:00
|
|
|
return;
|
2011-06-14 07:28:57 +00:00
|
|
|
|
2013-04-04 08:45:21 +00:00
|
|
|
final SuggestedWordInfo wordInfo = mSuggestedWords.getInfo(index);
|
|
|
|
mListener.pickSuggestionManually(index, wordInfo);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2011-01-18 08:22:01 +00:00
|
|
|
|
2009-03-31 21:50:26 +00:00
|
|
|
@Override
|
2011-09-27 06:18:02 +00:00
|
|
|
protected void onDetachedFromWindow() {
|
2009-03-31 21:50:26 +00:00
|
|
|
super.onDetachedFromWindow();
|
2012-06-14 18:54:04 +00:00
|
|
|
dismissMoreSuggestions();
|
2009-03-31 21:50:26 +00:00
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|