SuggestionStripLayoutHelper.java revision 2637be27c51be03e39b0db1c66312c4cc55bc7de
1653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka/* 2653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * Copyright (C) 2013 The Android Open Source Project 3653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * 4653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * Licensed under the Apache License, Version 2.0 (the "License"); 5653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * you may not use this file except in compliance with the License. 6653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * You may obtain a copy of the License at 7653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * 8653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * http://www.apache.org/licenses/LICENSE-2.0 9653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * 10653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * Unless required by applicable law or agreed to in writing, software 11653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * distributed under the License is distributed on an "AS IS" BASIS, 12653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * See the License for the specific language governing permissions and 14653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * limitations under the License. 15653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka */ 16653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 17653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokapackage com.android.inputmethod.latin.suggestions; 18653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 19653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.content.Context; 20653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.content.res.Resources; 21653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.content.res.TypedArray; 22653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.graphics.Bitmap; 23653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.graphics.Canvas; 24653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.graphics.Color; 25653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.graphics.Paint; 26a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaokaimport android.graphics.Paint.Align; 27653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.graphics.Rect; 28653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.graphics.Typeface; 29653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.graphics.drawable.BitmapDrawable; 30653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.graphics.drawable.Drawable; 31bcd30bf3e74c7c10d5b69a54cdf90c6b682a0747Tadashi G. Takaokaimport android.support.v4.view.ViewCompat; 32653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.text.Spannable; 33653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.text.SpannableString; 34653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.text.Spanned; 35653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.text.TextPaint; 36653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.text.TextUtils; 37653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.text.style.CharacterStyle; 38653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.text.style.StyleSpan; 39653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.text.style.UnderlineSpan; 40653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.util.AttributeSet; 41653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.view.Gravity; 42653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.view.View; 43a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaokaimport android.view.ViewGroup; 44653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.widget.LinearLayout; 45653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.widget.TextView; 46653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 47ae42cd7e43488aead5310c5fcb7467b8ce9c2dd6Tadashi G. Takaokaimport com.android.inputmethod.accessibility.AccessibilityUtils; 482637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaokaimport com.android.inputmethod.annotations.UsedForTesting; 49a273319c59ad24070ee5b35d72f044df496faa02Tadashi G. Takaokaimport com.android.inputmethod.latin.PunctuationSuggestions; 50653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport com.android.inputmethod.latin.R; 51653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport com.android.inputmethod.latin.SuggestedWords; 52e83e79cb055fbfe5171fb79a2224e7d9e2cda4d2Jean Chalardimport com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; 532dae79b1966a7970c25c8b79beec1c95c13f6c87Tadashi G. Takaokaimport com.android.inputmethod.latin.define.DebugFlags; 542637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaokaimport com.android.inputmethod.latin.settings.Settings; 552637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaokaimport com.android.inputmethod.latin.settings.SettingsValues; 56b03447e1af950888d901fccbd2cc3e3b4a11ef98Ken Wakasaimport com.android.inputmethod.latin.utils.AutoCorrectionUtils; 57e28eba5074664d5716b8e58b8d0a235746b261ebKen Wakasaimport com.android.inputmethod.latin.utils.ResourceUtils; 589b1a66843ddde552ea626a7b24c2c71ba23aa63aTadashi G. Takaokaimport com.android.inputmethod.latin.utils.SubtypeLocaleUtils; 59ccf4a310279b13bbf0b6aac76a0878178c1dfb7dTadashi G. Takaokaimport com.android.inputmethod.latin.utils.ViewLayoutUtils; 60653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 61653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport java.util.ArrayList; 62653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 63653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokafinal class SuggestionStripLayoutHelper { 64653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static final int DEFAULT_SUGGESTIONS_COUNT_IN_STRIP = 3; 65653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static final float DEFAULT_CENTER_SUGGESTION_PERCENTILE = 0.40f; 66653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static final int DEFAULT_MAX_MORE_SUGGESTIONS_ROW = 2; 67653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static final int PUNCTUATIONS_IN_STRIP = 5; 68653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static final float MIN_TEXT_XSCALE = 0.70f; 69653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 70653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka public final int mPadding; 71653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka public final int mDividerWidth; 72653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka public final int mSuggestionsStripHeight; 734f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka private final int mSuggestionsCountInStrip; 74653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka public final int mMoreSuggestionsRowHeight; 75653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private int mMaxMoreSuggestionsRow; 76653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka public final float mMinMoreSuggestionsWidth; 77653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka public final int mMoreSuggestionsBottomGap; 782637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka private boolean mMoreSuggestionsAvailable; 79653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 80a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka // The index of these {@link ArrayList} is the position in the suggestion strip. The indices 81a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka // increase towards the right for LTR scripts and the left for RTL scripts, starting with 0. 82a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka // The position of the most important suggestion is in {@link #mCenterPositionInStrip} 83500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka private final ArrayList<TextView> mWordViews; 84500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka private final ArrayList<View> mDividerViews; 85500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka private final ArrayList<TextView> mDebugInfoViews; 86653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 87653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private final int mColorValidTypedWord; 88653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private final int mColorTypedWord; 89653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private final int mColorAutoCorrect; 90653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private final int mColorSuggested; 91653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private final float mAlphaObsoleted; 92653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private final float mCenterSuggestionWeight; 93500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka private final int mCenterPositionInStrip; 94d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard private final int mTypedWordPositionWhenAutocorrect; 95653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private final Drawable mMoreSuggestionsHint; 96653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static final String MORE_SUGGESTIONS_HINT = "\u2026"; 97653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static final String LEFTWARDS_ARROW = "\u2190"; 98bcd30bf3e74c7c10d5b69a54cdf90c6b682a0747Tadashi G. Takaoka private static final String RIGHTWARDS_ARROW = "\u2192"; 99653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 100653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static final CharacterStyle BOLD_SPAN = new StyleSpan(Typeface.BOLD); 101653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static final CharacterStyle UNDERLINE_SPAN = new UnderlineSpan(); 102a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka 103a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard private final int mSuggestionStripOptions; 104a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka // These constants are the flag values of 105a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard // {@link R.styleable#SuggestionStripView_suggestionStripOptions} attribute. 106653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static final int AUTO_CORRECT_BOLD = 0x01; 107653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static final int AUTO_CORRECT_UNDERLINE = 0x02; 108653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static final int VALID_TYPED_WORD_BOLD = 0x04; 109653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 110653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka public SuggestionStripLayoutHelper(final Context context, final AttributeSet attrs, 111500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka final int defStyle, final ArrayList<TextView> wordViews, 112500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka final ArrayList<View> dividerViews, final ArrayList<TextView> debugInfoViews) { 113500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka mWordViews = wordViews; 114500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka mDividerViews = dividerViews; 115500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka mDebugInfoViews = debugInfoViews; 116500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka 117500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka final TextView wordView = wordViews.get(0); 118500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka final View dividerView = dividerViews.get(0); 119500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka mPadding = wordView.getCompoundPaddingLeft() + wordView.getCompoundPaddingRight(); 120500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka dividerView.measure( 121500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 122500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka mDividerWidth = dividerView.getMeasuredWidth(); 123500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka 124500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka final Resources res = wordView.getResources(); 1252fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa mSuggestionsStripHeight = res.getDimensionPixelSize( 1262fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa R.dimen.config_suggestions_strip_height); 127653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 128653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final TypedArray a = context.obtainStyledAttributes(attrs, 12916ed1868a16455ef9f5485696309d518f80aea1cTadashi G. Takaoka R.styleable.SuggestionStripView, defStyle, R.style.SuggestionStripView); 130a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard mSuggestionStripOptions = a.getInt( 131a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard R.styleable.SuggestionStripView_suggestionStripOptions, 0); 132653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka mAlphaObsoleted = ResourceUtils.getFraction(a, 1331f39d36458d09bfdfcf05ee0fcf7fc6116582099Tadashi G. Takaoka R.styleable.SuggestionStripView_alphaObsoleted, 1.0f); 1341f39d36458d09bfdfcf05ee0fcf7fc6116582099Tadashi G. Takaoka mColorValidTypedWord = a.getColor(R.styleable.SuggestionStripView_colorValidTypedWord, 0); 1351f39d36458d09bfdfcf05ee0fcf7fc6116582099Tadashi G. Takaoka mColorTypedWord = a.getColor(R.styleable.SuggestionStripView_colorTypedWord, 0); 1361f39d36458d09bfdfcf05ee0fcf7fc6116582099Tadashi G. Takaoka mColorAutoCorrect = a.getColor(R.styleable.SuggestionStripView_colorAutoCorrect, 0); 1371f39d36458d09bfdfcf05ee0fcf7fc6116582099Tadashi G. Takaoka mColorSuggested = a.getColor(R.styleable.SuggestionStripView_colorSuggested, 0); 138653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka mSuggestionsCountInStrip = a.getInt( 139653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka R.styleable.SuggestionStripView_suggestionsCountInStrip, 140653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka DEFAULT_SUGGESTIONS_COUNT_IN_STRIP); 141653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka mCenterSuggestionWeight = ResourceUtils.getFraction(a, 142653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka R.styleable.SuggestionStripView_centerSuggestionPercentile, 143653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka DEFAULT_CENTER_SUGGESTION_PERCENTILE); 144653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka mMaxMoreSuggestionsRow = a.getInt( 145653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka R.styleable.SuggestionStripView_maxMoreSuggestionsRow, 146653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka DEFAULT_MAX_MORE_SUGGESTIONS_ROW); 147653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka mMinMoreSuggestionsWidth = ResourceUtils.getFraction(a, 148653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka R.styleable.SuggestionStripView_minMoreSuggestionsWidth, 1.0f); 149653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka a.recycle(); 150653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 151653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka mMoreSuggestionsHint = getMoreSuggestionsHint(res, 1522fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa res.getDimension(R.dimen.config_more_suggestions_hint_text_size), 1532fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa mColorAutoCorrect); 154500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka mCenterPositionInStrip = mSuggestionsCountInStrip / 2; 155d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard // Assuming there are at least three suggestions. Also, note that the suggestions are 156d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard // laid out according to script direction, so this is left of the center for LTR scripts 157d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard // and right of the center for RTL scripts. 158d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard mTypedWordPositionWhenAutocorrect = mCenterPositionInStrip - 1; 159653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka mMoreSuggestionsBottomGap = res.getDimensionPixelOffset( 1602fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa R.dimen.config_more_suggestions_bottom_gap); 1612fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa mMoreSuggestionsRowHeight = res.getDimensionPixelSize( 1622fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa R.dimen.config_more_suggestions_row_height); 163653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 164653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 165653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka public int getMaxMoreSuggestionsRow() { 166653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka return mMaxMoreSuggestionsRow; 167653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 168653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 169c160a3932f74fea72c5347798c001d4ae961864cKen Wakasa private int getMoreSuggestionsHeight() { 170c160a3932f74fea72c5347798c001d4ae961864cKen Wakasa return mMaxMoreSuggestionsRow * mMoreSuggestionsRowHeight + mMoreSuggestionsBottomGap; 171c160a3932f74fea72c5347798c001d4ae961864cKen Wakasa } 172c160a3932f74fea72c5347798c001d4ae961864cKen Wakasa 17316a43d2bd9c5d80f8e50f5a7682b657ee10621e1Yohei Yukawa public void setMoreSuggestionsHeight(final int remainingHeight) { 174c160a3932f74fea72c5347798c001d4ae961864cKen Wakasa final int currentHeight = getMoreSuggestionsHeight(); 175c160a3932f74fea72c5347798c001d4ae961864cKen Wakasa if (currentHeight <= remainingHeight) { 17616a43d2bd9c5d80f8e50f5a7682b657ee10621e1Yohei Yukawa return; 177c160a3932f74fea72c5347798c001d4ae961864cKen Wakasa } 178c160a3932f74fea72c5347798c001d4ae961864cKen Wakasa 179653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka mMaxMoreSuggestionsRow = (remainingHeight - mMoreSuggestionsBottomGap) 180653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka / mMoreSuggestionsRowHeight; 181653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 182653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 183653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, 184653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final int color) { 185653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final Paint paint = new Paint(); 186653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka paint.setAntiAlias(true); 187653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka paint.setTextAlign(Align.CENTER); 188653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka paint.setTextSize(textSize); 189653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka paint.setColor(color); 190653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final Rect bounds = new Rect(); 191653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds); 192653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final int width = Math.round(bounds.width() + 0.5f); 193653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final int height = Math.round(bounds.height() + 0.5f); 194653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888); 195653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final Canvas canvas = new Canvas(buffer); 196653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint); 197653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka return new BitmapDrawable(res, buffer); 198653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 199653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 200a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka private CharSequence getStyledSuggestedWord(final SuggestedWords suggestedWords, 201653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final int indexInSuggestedWords) { 20272ac390ce64fe2825ac59029402e5f372303c8c3Tadashi G. Takaoka if (indexInSuggestedWords >= suggestedWords.size()) { 20372ac390ce64fe2825ac59029402e5f372303c8c3Tadashi G. Takaoka return null; 20472ac390ce64fe2825ac59029402e5f372303c8c3Tadashi G. Takaoka } 205a273319c59ad24070ee5b35d72f044df496faa02Tadashi G. Takaoka final String word = suggestedWords.getLabel(indexInSuggestedWords); 206a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard // TODO: don't use the index to decide whether this is the auto-correction/typed word, as 207a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard // this is brittle 208a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard final boolean isAutoCorrection = suggestedWords.mWillAutoCorrect 209a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard && indexInSuggestedWords == SuggestedWords.INDEX_OF_AUTO_CORRECTION; 210a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard final boolean isTypedWordValid = suggestedWords.mTypedWordValid 211a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard && indexInSuggestedWords == SuggestedWords.INDEX_OF_TYPED_WORD; 212a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard if (!isAutoCorrection && !isTypedWordValid) { 213653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka return word; 214500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka } 215653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 216653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final int len = word.length(); 217653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final Spannable spannedWord = new SpannableString(word); 218a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard final int options = mSuggestionStripOptions; 219a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard if ((isAutoCorrection && (options & AUTO_CORRECT_BOLD) != 0) 220a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard || (isTypedWordValid && (options & VALID_TYPED_WORD_BOLD) != 0)) { 221653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka spannedWord.setSpan(BOLD_SPAN, 0, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 222653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 223a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard if (isAutoCorrection && (options & AUTO_CORRECT_UNDERLINE) != 0) { 224653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka spannedWord.setSpan(UNDERLINE_SPAN, 0, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 225653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 226653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka return spannedWord; 227653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 228653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 2292637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka /** 2302637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka * Convert an index of {@link SuggestedWords} to position in the suggestion strip. 2312637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka * @param indexInSuggestedWords the index of {@link SuggestedWords}. 2322637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka * @param suggestedWords the suggested words list 2332637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka * @return Non-negative integer of the position in the suggestion strip. 2342637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka * Negative integer if the word of the index shouldn't be shown on the suggestion strip. 2352637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka */ 236d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard private int getPositionInSuggestionStrip(final int indexInSuggestedWords, 237653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final SuggestedWords suggestedWords) { 2382637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final SettingsValues settingsValues = Settings.getInstance().getCurrent(); 2392637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final boolean shouldOmitTypedWord = shouldOmitTypedWord(suggestedWords.mInputStyle, 2402637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka settingsValues.mGestureFloatingPreviewTextEnabled, 2412637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka settingsValues.mShouldShowUiToAcceptTypedWord); 2422637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka return getPositionInSuggestionStrip(indexInSuggestedWords, suggestedWords.mWillAutoCorrect, 2432637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka settingsValues.mShouldShowUiToAcceptTypedWord && shouldOmitTypedWord, 2442637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka mCenterPositionInStrip, mTypedWordPositionWhenAutocorrect); 2452637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka } 2462637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka 2472637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka @UsedForTesting 2482637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka static boolean shouldOmitTypedWord(final int inputStyle, 2492637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final boolean gestureFloatingPreviewTextEnabled, 2502637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final boolean shouldShowUiToAcceptTypedWord) { 2512637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final boolean omitTypedWord = (inputStyle == SuggestedWords.INPUT_STYLE_TYPING) 2522637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka || (inputStyle == SuggestedWords.INPUT_STYLE_TAIL_BATCH) 2532637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka || (inputStyle == SuggestedWords.INPUT_STYLE_UPDATE_BATCH 2542637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka && gestureFloatingPreviewTextEnabled); 2552637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka return shouldShowUiToAcceptTypedWord && omitTypedWord; 2562637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka } 2572637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka 2582637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka @UsedForTesting 2592637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka static int getPositionInSuggestionStrip(final int indexInSuggestedWords, 2602637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final boolean willAutoCorrect, final boolean omitTypedWord, 2612637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final int centerPositionInStrip, final int typedWordPositionWhenAutoCorrect) { 2622637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka if (omitTypedWord) { 2632637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka if (indexInSuggestedWords == SuggestedWords.INDEX_OF_TYPED_WORD) { 2642637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka // Ignore. 2652637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka return -1; 2662637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka } 2672637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka if (indexInSuggestedWords == SuggestedWords.INDEX_OF_AUTO_CORRECTION) { 2682637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka // Center in the suggestion strip. 2692637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka return centerPositionInStrip; 2702637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka } 2712637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka // If neither of those, the order in the suggestion strip is left of the center first 2722637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka // then right of the center, to both edges of the suggestion strip. 2732637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka // For example, center-1, center+1, center-2, center+2, and so on. 2742637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final int n = indexInSuggestedWords; 2752637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final int offsetFromCenter = (n % 2) == 0 ? -(n / 2) : (n / 2); 2762637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final int positionInSuggestionStrip = centerPositionInStrip + offsetFromCenter; 2772637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka return positionInSuggestionStrip; 2782637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka } 279d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard final int indexToDisplayMostImportantSuggestion; 280d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard final int indexToDisplaySecondMostImportantSuggestion; 2812637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka if (willAutoCorrect) { 282d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard indexToDisplayMostImportantSuggestion = SuggestedWords.INDEX_OF_AUTO_CORRECTION; 283d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard indexToDisplaySecondMostImportantSuggestion = SuggestedWords.INDEX_OF_TYPED_WORD; 284d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard } else { 285d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard indexToDisplayMostImportantSuggestion = SuggestedWords.INDEX_OF_TYPED_WORD; 286d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard indexToDisplaySecondMostImportantSuggestion = SuggestedWords.INDEX_OF_AUTO_CORRECTION; 287653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 288d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard if (indexInSuggestedWords == indexToDisplayMostImportantSuggestion) { 2892637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka // Center in the suggestion strip. 2902637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka return centerPositionInStrip; 291500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka } 292d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard if (indexInSuggestedWords == indexToDisplaySecondMostImportantSuggestion) { 2932637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka // Center-1. 2942637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka return typedWordPositionWhenAutoCorrect; 295d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard } 2962637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka // If neither of those, the order in the suggestion strip is right of the center first 2972637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka // then left of the center, to both edges of the suggestion strip. 2982637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka // For example, Center+1, center-2, center+2, center-3, and so on. 2992637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final int n = indexInSuggestedWords + 1; 3002637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final int offsetFromCenter = (n % 2) == 0 ? -(n / 2) : (n / 2); 3012637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final int positionInSuggestionStrip = centerPositionInStrip + offsetFromCenter; 3022637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka return positionInSuggestionStrip; 303653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 304653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 305ef3a45643e950cdd934763c59963cee4089f93e1Jean Chalard private int getSuggestionTextColor(final SuggestedWords suggestedWords, 306ef3a45643e950cdd934763c59963cee4089f93e1Jean Chalard final int indexInSuggestedWords) { 3070c34ee20a80884fea49b228745ce87132ae6f22fJean Chalard // Use identity for strings, not #equals : it's the typed word if it's the same object 308e83e79cb055fbfe5171fb79a2224e7d9e2cda4d2Jean Chalard final boolean isTypedWord = suggestedWords.getInfo(indexInSuggestedWords).isKindOf( 309e83e79cb055fbfe5171fb79a2224e7d9e2cda4d2Jean Chalard SuggestedWordInfo.KIND_TYPED); 310653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 311653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final int color; 3122637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka if (indexInSuggestedWords == SuggestedWords.INDEX_OF_AUTO_CORRECTION 3132637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka && suggestedWords.mWillAutoCorrect) { 314653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka color = mColorAutoCorrect; 3150c34ee20a80884fea49b228745ce87132ae6f22fJean Chalard } else if (isTypedWord && suggestedWords.mTypedWordValid) { 316653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka color = mColorValidTypedWord; 317783f1932393547b2bcb88e6dc454ce888ce1d4a1Jean Chalard } else if (isTypedWord) { 318653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka color = mColorTypedWord; 319783f1932393547b2bcb88e6dc454ce888ce1d4a1Jean Chalard } else { 320783f1932393547b2bcb88e6dc454ce888ce1d4a1Jean Chalard color = mColorSuggested; 321653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 3222dae79b1966a7970c25c8b79beec1c95c13f6c87Tadashi G. Takaoka if (DebugFlags.DEBUG_ENABLED && suggestedWords.size() > 1) { 323653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka // If we auto-correct, then the autocorrection is in slot 0 and the typed word 324653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka // is in slot 1. 3252637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka if (indexInSuggestedWords == SuggestedWords.INDEX_OF_AUTO_CORRECTION 3262637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka && suggestedWords.mWillAutoCorrect 327b03447e1af950888d901fccbd2cc3e3b4a11ef98Ken Wakasa && AutoCorrectionUtils.shouldBlockAutoCorrectionBySafetyNet( 328a273319c59ad24070ee5b35d72f044df496faa02Tadashi G. Takaoka suggestedWords.getLabel(SuggestedWords.INDEX_OF_AUTO_CORRECTION), 329a273319c59ad24070ee5b35d72f044df496faa02Tadashi G. Takaoka suggestedWords.getLabel(SuggestedWords.INDEX_OF_TYPED_WORD))) { 330653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka return 0xFFFF0000; 331653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 332653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 333653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 334783f1932393547b2bcb88e6dc454ce888ce1d4a1Jean Chalard if (suggestedWords.mIsObsoleteSuggestions && !isTypedWord) { 335653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka return applyAlpha(color, mAlphaObsoleted); 336653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 337500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka return color; 338653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 339653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 340653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static int applyAlpha(final int color, final float alpha) { 341653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final int newAlpha = (int)(Color.alpha(color) * alpha); 342653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka return Color.argb(newAlpha, Color.red(color), Color.green(color), Color.blue(color)); 343653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 344653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 345500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka private static void addDivider(final ViewGroup stripView, final View dividerView) { 346500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka stripView.addView(dividerView); 347653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final LinearLayout.LayoutParams params = 348500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka (LinearLayout.LayoutParams)dividerView.getLayoutParams(); 349653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka params.gravity = Gravity.CENTER; 350653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 351653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 3524f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka /** 3532637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka * Layout suggestions to the suggestions strip. And returns the start index of more 3542637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka * suggestions. 3554f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka * 3564f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka * @param suggestedWords suggestions to be shown in the suggestions strip. 3574f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka * @param stripView the suggestions strip view. 3584f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka * @param placerView the view where the debug info will be placed. 3592637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka * @return the start index of more suggestions. 3604f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka */ 3612637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka public int layoutAndReturnStartIndexOfMoreSuggestions(final SuggestedWords suggestedWords, 3624f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka final ViewGroup stripView, final ViewGroup placerView) { 363a273319c59ad24070ee5b35d72f044df496faa02Tadashi G. Takaoka if (suggestedWords.isPunctuationSuggestions()) { 3642637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka return layoutPunctuationsAndReturnStartIndexOfMoreSuggestions( 365a273319c59ad24070ee5b35d72f044df496faa02Tadashi G. Takaoka (PunctuationSuggestions)suggestedWords, stripView); 366653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 367653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 3682637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final int startIndexOfMoreSuggestions = setupWordViewsAndReturnStartIndexOfMoreSuggestions( 3692637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka suggestedWords, mSuggestionsCountInStrip); 370043273b00fb0f42761a38eb53c9911ef022dd5aeTadashi G. Takaoka final TextView centerWordView = mWordViews.get(mCenterPositionInStrip); 3712b479cccaeb0bb54121730cb5e47cca3f7517c67Tadashi G. Takaoka final int stripWidth = stripView.getWidth(); 3722b479cccaeb0bb54121730cb5e47cca3f7517c67Tadashi G. Takaoka final int centerWidth = getSuggestionWidth(mCenterPositionInStrip, stripWidth); 3734f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka if (suggestedWords.size() == 1 || getTextScaleX(centerWordView.getText(), centerWidth, 3744f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka centerWordView.getPaint()) < MIN_TEXT_XSCALE) { 375043273b00fb0f42761a38eb53c9911ef022dd5aeTadashi G. Takaoka // Layout only the most relevant suggested word at the center of the suggestion strip 376043273b00fb0f42761a38eb53c9911ef022dd5aeTadashi G. Takaoka // by consolidating all slots in the strip. 3772637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final int countInStrip = 1; 3784f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka mMoreSuggestionsAvailable = (suggestedWords.size() > countInStrip); 3792b479cccaeb0bb54121730cb5e47cca3f7517c67Tadashi G. Takaoka layoutWord(mCenterPositionInStrip, stripWidth - mPadding); 380043273b00fb0f42761a38eb53c9911ef022dd5aeTadashi G. Takaoka stripView.addView(centerWordView); 381043273b00fb0f42761a38eb53c9911ef022dd5aeTadashi G. Takaoka setLayoutWeight(centerWordView, 1.0f, ViewGroup.LayoutParams.MATCH_PARENT); 382043273b00fb0f42761a38eb53c9911ef022dd5aeTadashi G. Takaoka if (SuggestionStripView.DBG) { 3832b479cccaeb0bb54121730cb5e47cca3f7517c67Tadashi G. Takaoka layoutDebugInfo(mCenterPositionInStrip, placerView, stripWidth); 384043273b00fb0f42761a38eb53c9911ef022dd5aeTadashi G. Takaoka } 3852637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final Integer lastIndex = (Integer)centerWordView.getTag(); 3862637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka return (lastIndex == null ? 0 : lastIndex) + 1; 3872637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka } 3882637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka 3892637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final int countInStrip = mSuggestionsCountInStrip; 3902637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka mMoreSuggestionsAvailable = (suggestedWords.size() > countInStrip); 3912637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka int x = 0; 3922637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka for (int positionInStrip = 0; positionInStrip < countInStrip; positionInStrip++) { 3932637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka if (positionInStrip != 0) { 3942637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final View divider = mDividerViews.get(positionInStrip); 3952637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka // Add divider if this isn't the left most suggestion in suggestions strip. 3962637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka addDivider(stripView, divider); 3972637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka x += divider.getMeasuredWidth(); 3982637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka } 3992637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka 4002637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final int width = getSuggestionWidth(positionInStrip, stripWidth); 4012637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final TextView wordView = layoutWord(positionInStrip, width); 4022637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka stripView.addView(wordView); 4032637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka setLayoutWeight(wordView, getSuggestionWeight(positionInStrip), 4042637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka ViewGroup.LayoutParams.MATCH_PARENT); 4052637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka x += wordView.getMeasuredWidth(); 4062637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka 4072637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka if (SuggestionStripView.DBG) { 4082637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka layoutDebugInfo(positionInStrip, placerView, x); 409653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 410653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 4112637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka return startIndexOfMoreSuggestions; 412653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 413653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 414653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka /** 415a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka * Format appropriately the suggested word in {@link #mWordViews} specified by 416a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka * <code>positionInStrip</code>. When the suggested word doesn't exist, the corresponding 417a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka * {@link TextView} will be disabled and never respond to user interaction. The suggested word 418a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka * may be shrunk or ellipsized to fit in the specified width. 419653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * 420500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka * The <code>positionInStrip</code> argument is the index in the suggestion strip. The indices 421653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * increase towards the right for LTR scripts and the left for RTL scripts, starting with 0. 422a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka * The position of the most important suggestion is in {@link #mCenterPositionInStrip}. This 423653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * usually doesn't match the index in <code>suggedtedWords</code> -- see 424d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard * {@link #getPositionInSuggestionStrip(int,SuggestedWords)}. 425653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * 426a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka * @param positionInStrip the position in the suggestion strip. 427653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * @param width the maximum width for layout in pixels. 428653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka * @return the {@link TextView} containing the suggested word appropriately formatted. 429653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka */ 430a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka private TextView layoutWord(final int positionInStrip, final int width) { 431a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka final TextView wordView = mWordViews.get(positionInStrip); 432a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka final CharSequence word = wordView.getText(); 433500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka if (positionInStrip == mCenterPositionInStrip && mMoreSuggestionsAvailable) { 434653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka // TODO: This "more suggestions hint" should have a nicely designed icon. 435500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka wordView.setCompoundDrawablesWithIntrinsicBounds( 436653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka null, null, null, mMoreSuggestionsHint); 437653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka // HACK: Align with other TextViews that have no compound drawables. 438500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka wordView.setCompoundDrawablePadding(-mMoreSuggestionsHint.getIntrinsicHeight()); 439653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } else { 440500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka wordView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); 441653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 442f3c852efadaec7779c47b6130290279a400b5ab5Tadashi G. Takaoka // {@link StyleSpan} in a content description may cause an issue of TTS/TalkBack. 443f3c852efadaec7779c47b6130290279a400b5ab5Tadashi G. Takaoka // Use a simple {@link String} to avoid the issue. 444f3c852efadaec7779c47b6130290279a400b5ab5Tadashi G. Takaoka wordView.setContentDescription(TextUtils.isEmpty(word) ? null : word.toString()); 445500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka final CharSequence text = getEllipsizedText(word, width, wordView.getPaint()); 446c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard final float scaleX = getTextScaleX(word, width, wordView.getPaint()); 447500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka wordView.setText(text); // TextView.setText() resets text scale x to 1.0. 448c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard wordView.setTextScaleX(Math.max(scaleX, MIN_TEXT_XSCALE)); 449ae42cd7e43488aead5310c5fcb7467b8ce9c2dd6Tadashi G. Takaoka // A <code>wordView</code> should be disabled when <code>word</code> is empty in order to 450ae42cd7e43488aead5310c5fcb7467b8ce9c2dd6Tadashi G. Takaoka // make it unclickable. 451ae42cd7e43488aead5310c5fcb7467b8ce9c2dd6Tadashi G. Takaoka // With accessibility touch exploration on, <code>wordView</code> should be enabled even 452ae42cd7e43488aead5310c5fcb7467b8ce9c2dd6Tadashi G. Takaoka // when it is empty to avoid announcing as "disabled". 453ae42cd7e43488aead5310c5fcb7467b8ce9c2dd6Tadashi G. Takaoka wordView.setEnabled(!TextUtils.isEmpty(word) 454ae42cd7e43488aead5310c5fcb7467b8ce9c2dd6Tadashi G. Takaoka || AccessibilityUtils.getInstance().isTouchExplorationEnabled()); 455500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka return wordView; 456653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 457653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 458a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka private void layoutDebugInfo(final int positionInStrip, final ViewGroup placerView, 459a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka final int x) { 460a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka final TextView debugInfoView = mDebugInfoViews.get(positionInStrip); 461a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka final CharSequence debugInfo = debugInfoView.getText(); 462653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka if (debugInfo == null) { 463653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka return; 464653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 465500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka placerView.addView(debugInfoView); 466500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka debugInfoView.measure( 467500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 468500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka final int infoWidth = debugInfoView.getMeasuredWidth(); 469500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka final int y = debugInfoView.getMeasuredHeight(); 470653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka ViewLayoutUtils.placeViewAt( 471500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka debugInfoView, x - infoWidth, y, infoWidth, debugInfoView.getMeasuredHeight()); 472653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 473653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 474500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka private int getSuggestionWidth(final int positionInStrip, final int maxWidth) { 475653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final int paddings = mPadding * mSuggestionsCountInStrip; 476653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final int dividers = mDividerWidth * (mSuggestionsCountInStrip - 1); 477653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final int availableWidth = maxWidth - paddings - dividers; 478500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka return (int)(availableWidth * getSuggestionWeight(positionInStrip)); 479653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 480653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 481500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka private float getSuggestionWeight(final int positionInStrip) { 482500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka if (positionInStrip == mCenterPositionInStrip) { 483653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka return mCenterSuggestionWeight; 484653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 485500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka // TODO: Revisit this for cases of 5 or more suggestions 486500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka return (1.0f - mCenterSuggestionWeight) / (mSuggestionsCountInStrip - 1); 487653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 488653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 4892637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka private int setupWordViewsAndReturnStartIndexOfMoreSuggestions( 4902637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final SuggestedWords suggestedWords, final int maxSuggestionInStrip) { 491d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard // Clear all suggestions first 4922637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka for (int positionInStrip = 0; positionInStrip < maxSuggestionInStrip; ++positionInStrip) { 49306d1cdd5dc73ef118e2a2853fc75dc911202f373Tadashi G. Takaoka final TextView wordView = mWordViews.get(positionInStrip); 49406d1cdd5dc73ef118e2a2853fc75dc911202f373Tadashi G. Takaoka wordView.setText(null); 49506d1cdd5dc73ef118e2a2853fc75dc911202f373Tadashi G. Takaoka wordView.setTag(null); 496d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard // Make this inactive for touches in {@link #layoutWord(int,int)}. 497d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard if (SuggestionStripView.DBG) { 498d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard mDebugInfoViews.get(positionInStrip).setText(null); 499d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard } 500d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard } 5012637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka int count = 0; 5022637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka int indexInSuggestedWords; 5032637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka for (indexInSuggestedWords = 0; indexInSuggestedWords < suggestedWords.size() 5042637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka && count < maxSuggestionInStrip; indexInSuggestedWords++) { 505d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard final int positionInStrip = 506d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard getPositionInSuggestionStrip(indexInSuggestedWords, suggestedWords); 5072637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka if (positionInStrip < 0) { 5082637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka continue; 5092637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka } 510a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka final TextView wordView = mWordViews.get(positionInStrip); 511a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka // {@link TextView#getTag()} is used to get the index in suggestedWords at 512a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka // {@link SuggestionStripView#onClick(View)}. 513a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka wordView.setTag(indexInSuggestedWords); 514a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka wordView.setText(getStyledSuggestedWord(suggestedWords, indexInSuggestedWords)); 515ef3a45643e950cdd934763c59963cee4089f93e1Jean Chalard wordView.setTextColor(getSuggestionTextColor(suggestedWords, indexInSuggestedWords)); 516a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka if (SuggestionStripView.DBG) { 517a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka mDebugInfoViews.get(positionInStrip).setText( 5184be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka suggestedWords.getDebugString(indexInSuggestedWords)); 519a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka } 5202637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka count++; 521653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 5222637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka return indexInSuggestedWords; 523653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 524653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 5252637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka private int layoutPunctuationsAndReturnStartIndexOfMoreSuggestions( 526a273319c59ad24070ee5b35d72f044df496faa02Tadashi G. Takaoka final PunctuationSuggestions punctuationSuggestions, final ViewGroup stripView) { 527a273319c59ad24070ee5b35d72f044df496faa02Tadashi G. Takaoka final int countInStrip = Math.min(punctuationSuggestions.size(), PUNCTUATIONS_IN_STRIP); 528a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka for (int positionInStrip = 0; positionInStrip < countInStrip; positionInStrip++) { 529a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka if (positionInStrip != 0) { 530653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka // Add divider if this isn't the left most suggestion in suggestions strip. 531a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka addDivider(stripView, mDividerViews.get(positionInStrip)); 532653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 533653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 534a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka final TextView wordView = mWordViews.get(positionInStrip); 535f3c852efadaec7779c47b6130290279a400b5ab5Tadashi G. Takaoka final String punctuation = punctuationSuggestions.getLabel(positionInStrip); 5369b570b5e62581f41c40c4558d95c77ad5896934cTadashi G. Takaoka // {@link TextView#getTag()} is used to get the index in suggestedWords at 5379b570b5e62581f41c40c4558d95c77ad5896934cTadashi G. Takaoka // {@link SuggestionStripView#onClick(View)}. 5389b570b5e62581f41c40c4558d95c77ad5896934cTadashi G. Takaoka wordView.setTag(positionInStrip); 539f3c852efadaec7779c47b6130290279a400b5ab5Tadashi G. Takaoka wordView.setText(punctuation); 540f3c852efadaec7779c47b6130290279a400b5ab5Tadashi G. Takaoka wordView.setContentDescription(punctuation); 541a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka wordView.setTextScaleX(1.0f); 542a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka wordView.setCompoundDrawables(null, null, null, null); 543f3c852efadaec7779c47b6130290279a400b5ab5Tadashi G. Takaoka wordView.setTextColor(mColorAutoCorrect); 544a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka stripView.addView(wordView); 545a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka setLayoutWeight(wordView, 1.0f, mSuggestionsStripHeight); 546653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 547a273319c59ad24070ee5b35d72f044df496faa02Tadashi G. Takaoka mMoreSuggestionsAvailable = (punctuationSuggestions.size() > countInStrip); 5484f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka return countInStrip; 549653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 550653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 5512b479cccaeb0bb54121730cb5e47cca3f7517c67Tadashi G. Takaoka public void layoutAddToDictionaryHint(final String word, final ViewGroup addToDictionaryStrip) { 5522637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final boolean shouldShowUiToAcceptTypedWord = Settings.getInstance().getCurrent() 5532637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka .mShouldShowUiToAcceptTypedWord; 5542b479cccaeb0bb54121730cb5e47cca3f7517c67Tadashi G. Takaoka final int stripWidth = addToDictionaryStrip.getWidth(); 5552637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final int width = shouldShowUiToAcceptTypedWord ? stripWidth 5562637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka : stripWidth - mDividerWidth - mPadding * 2; 557653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 558148dedbc5dfa22d6db30b28ae9ede139206af429Tadashi G. Takaoka final TextView wordView = (TextView)addToDictionaryStrip.findViewById(R.id.word_to_save); 559653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka wordView.setTextColor(mColorTypedWord); 560653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final int wordWidth = (int)(width * mCenterSuggestionWeight); 561148dedbc5dfa22d6db30b28ae9ede139206af429Tadashi G. Takaoka final CharSequence wordToSave = getEllipsizedText(word, wordWidth, wordView.getPaint()); 562653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final float wordScaleX = wordView.getTextScaleX(); 563148dedbc5dfa22d6db30b28ae9ede139206af429Tadashi G. Takaoka wordView.setText(wordToSave); 564653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka wordView.setTextScaleX(wordScaleX); 565653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka setLayoutWeight(wordView, mCenterSuggestionWeight, ViewGroup.LayoutParams.MATCH_PARENT); 5662637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final int wordVisibility = shouldShowUiToAcceptTypedWord ? View.GONE : View.VISIBLE; 5672637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka wordView.setVisibility(wordVisibility); 5682637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka addToDictionaryStrip.findViewById(R.id.word_to_save_divider).setVisibility(wordVisibility); 569653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 5702637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final Resources res = addToDictionaryStrip.getResources(); 5712637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final CharSequence hintText; 5722637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final int hintWidth; 5732637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final float hintWeight; 574148dedbc5dfa22d6db30b28ae9ede139206af429Tadashi G. Takaoka final TextView hintView = (TextView)addToDictionaryStrip.findViewById( 575148dedbc5dfa22d6db30b28ae9ede139206af429Tadashi G. Takaoka R.id.hint_add_to_dictionary); 5762637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka if (shouldShowUiToAcceptTypedWord) { 5772637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka hintText = res.getText(R.string.hint_add_to_dictionary_without_word); 5782637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka hintWidth = width; 5792637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka hintWeight = 1.0f; 5802637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka hintView.setGravity(Gravity.CENTER); 5812637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka } else { 5822637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final boolean isRtlLanguage = (ViewCompat.getLayoutDirection(addToDictionaryStrip) 5832637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka == ViewCompat.LAYOUT_DIRECTION_RTL); 5842637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final String arrow = isRtlLanguage ? RIGHTWARDS_ARROW : LEFTWARDS_ARROW; 5852637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final boolean isRtlSystem = SubtypeLocaleUtils.isRtlLanguage( 5862637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka res.getConfiguration().locale); 5872637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final CharSequence hint = res.getText(R.string.hint_add_to_dictionary); 5882637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka hintText = (isRtlLanguage == isRtlSystem) ? (arrow + hint) : (hint + arrow); 5892637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka hintWidth = width - wordWidth; 5902637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka hintWeight = 1.0f - mCenterSuggestionWeight; 5912637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka hintView.setGravity(Gravity.CENTER_VERTICAL | Gravity.START); 5922637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka } 593653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka hintView.setTextColor(mColorAutoCorrect); 5942637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka final float hintScaleX = getTextScaleX(hintText, hintWidth, hintView.getPaint()); 5952637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka hintView.setText(hintText); 596653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka hintView.setTextScaleX(hintScaleX); 5972637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka setLayoutWeight(hintView, hintWeight, ViewGroup.LayoutParams.MATCH_PARENT); 598653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 599653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 60014e908c3486ae5996e66625e959ff45c5f3740efTadashi G. Takaoka public void layoutImportantNotice(final View importantNoticeStrip, 6016abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka final String importantNoticeTitle) { 6028a296e43c903a3377f28943ba4a59082ae7fe60aTadashi G. Takaoka final TextView titleView = (TextView)importantNoticeStrip.findViewById( 6038a296e43c903a3377f28943ba4a59082ae7fe60aTadashi G. Takaoka R.id.important_notice_title); 604a9c1a3da63c83dd68424a0851079a55d110cc63aTadashi G. Takaoka final int width = titleView.getWidth() - titleView.getPaddingLeft() 605a9c1a3da63c83dd68424a0851079a55d110cc63aTadashi G. Takaoka - titleView.getPaddingRight(); 6068a296e43c903a3377f28943ba4a59082ae7fe60aTadashi G. Takaoka titleView.setTextColor(mColorAutoCorrect); 6076abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka titleView.setText(importantNoticeTitle); 6088a296e43c903a3377f28943ba4a59082ae7fe60aTadashi G. Takaoka titleView.setTextScaleX(1.0f); // Reset textScaleX. 60914e908c3486ae5996e66625e959ff45c5f3740efTadashi G. Takaoka final float titleScaleX = getTextScaleX(importantNoticeTitle, width, titleView.getPaint()); 6108dda9e480b6519ca57565c8aa507e14bff739b60Tadashi G. Takaoka titleView.setTextScaleX(titleScaleX); 6118a296e43c903a3377f28943ba4a59082ae7fe60aTadashi G. Takaoka } 6128a296e43c903a3377f28943ba4a59082ae7fe60aTadashi G. Takaoka 6138a296e43c903a3377f28943ba4a59082ae7fe60aTadashi G. Takaoka static void setLayoutWeight(final View v, final float weight, final int height) { 614653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final ViewGroup.LayoutParams lp = v.getLayoutParams(); 615653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka if (lp instanceof LinearLayout.LayoutParams) { 616653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final LinearLayout.LayoutParams llp = (LinearLayout.LayoutParams)lp; 617653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka llp.weight = weight; 618653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka llp.width = 0; 619653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka llp.height = height; 620653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 621653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 622653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 623653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static float getTextScaleX(final CharSequence text, final int maxWidth, 624653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final TextPaint paint) { 625653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka paint.setTextScaleX(1.0f); 626653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final int width = getTextWidth(text, paint); 6278dda9e480b6519ca57565c8aa507e14bff739b60Tadashi G. Takaoka if (width <= maxWidth || maxWidth <= 0) { 628653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka return 1.0f; 629653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 630653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka return maxWidth / (float)width; 631653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 632653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 633653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static CharSequence getEllipsizedText(final CharSequence text, final int maxWidth, 634653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final TextPaint paint) { 635500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka if (text == null) { 636500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka return null; 637653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 638500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka final float scaleX = getTextScaleX(text, maxWidth, paint); 639653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka if (scaleX >= MIN_TEXT_XSCALE) { 640653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka paint.setTextScaleX(scaleX); 641653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka return text; 642653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 643653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 644653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka // Note that TextUtils.ellipsize() use text-x-scale as 1.0 if ellipsize is needed. To 645653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka // get squeezed and ellipsized text, passes enlarged width (maxWidth / MIN_TEXT_XSCALE). 646c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard final float upscaledWidth = maxWidth / MIN_TEXT_XSCALE; 647c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard CharSequence ellipsized = TextUtils.ellipsize( 648c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard text, paint, upscaledWidth, TextUtils.TruncateAt.MIDDLE); 649c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard // For an unknown reason, ellipsized seems to return a text that does indeed fit inside the 650c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard // passed width according to paint.measureText, but not according to paint.getTextWidths. 651c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard // But when rendered, the text seems to actually take up as many pixels as returned by 652c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard // paint.getTextWidths, hence problem. 653c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard // To save this case, we compare the measured size of the new text, and if it's too much, 654c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard // try it again removing the difference. This may still give a text too long by one or 655c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard // two pixels so we take an additional 2 pixels cushion and call it a day. 656c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard // TODO: figure out why getTextWidths and measureText don't agree with each other, and 657c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard // remove the following code. 658c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard final float ellipsizedTextWidth = getTextWidth(ellipsized, paint); 659c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard if (upscaledWidth <= ellipsizedTextWidth) { 660c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard ellipsized = TextUtils.ellipsize( 661c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard text, paint, upscaledWidth - (ellipsizedTextWidth - upscaledWidth) - 2, 662c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard TextUtils.TruncateAt.MIDDLE); 663c0bd0a244d7e222de13f1a8454b4463183604b75Jean Chalard } 664653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka paint.setTextScaleX(MIN_TEXT_XSCALE); 665653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka return ellipsized; 666653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 667653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 668653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static int getTextWidth(final CharSequence text, final TextPaint paint) { 669500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka if (TextUtils.isEmpty(text)) { 670500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka return 0; 671500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka } 672653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final Typeface savedTypeface = paint.getTypeface(); 673653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka paint.setTypeface(getTextTypeface(text)); 674653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final int len = text.length(); 675653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final float[] widths = new float[len]; 676653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final int count = paint.getTextWidths(text, 0, len, widths); 677653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka int width = 0; 678653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka for (int i = 0; i < count; i++) { 679653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka width += Math.round(widths[i] + 0.5f); 680653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 681653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka paint.setTypeface(savedTypeface); 682653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka return width; 683653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 684653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 685653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka private static Typeface getTextTypeface(final CharSequence text) { 686500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka if (!(text instanceof SpannableString)) { 687653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka return Typeface.DEFAULT; 688500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka } 689653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 690653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final SpannableString ss = (SpannableString)text; 691653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka final StyleSpan[] styles = ss.getSpans(0, text.length(), StyleSpan.class); 692500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka if (styles.length == 0) { 693653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka return Typeface.DEFAULT; 694500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka } 695653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka 696500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka if (styles[0].getStyle() == Typeface.BOLD) { 697500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka return Typeface.DEFAULT_BOLD; 698653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 699500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka // TODO: BOLD_ITALIC, ITALIC case? 700500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka return Typeface.DEFAULT; 701653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka } 702653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka} 703