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;
31653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.text.Spannable;
32653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.text.SpannableString;
33653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.text.Spanned;
34653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.text.TextPaint;
35653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.text.TextUtils;
36653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.text.style.CharacterStyle;
37653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.text.style.StyleSpan;
38653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.text.style.UnderlineSpan;
39653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.util.AttributeSet;
40653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.view.Gravity;
41653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.view.View;
42a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaokaimport android.view.ViewGroup;
43653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.widget.LinearLayout;
44653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport android.widget.TextView;
45653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
46ae42cd7e43488aead5310c5fcb7467b8ce9c2dd6Tadashi G. Takaokaimport com.android.inputmethod.accessibility.AccessibilityUtils;
472637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaokaimport com.android.inputmethod.annotations.UsedForTesting;
48a273319c59ad24070ee5b35d72f044df496faa02Tadashi G. Takaokaimport com.android.inputmethod.latin.PunctuationSuggestions;
49653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport com.android.inputmethod.latin.R;
50653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport com.android.inputmethod.latin.SuggestedWords;
51e83e79cb055fbfe5171fb79a2224e7d9e2cda4d2Jean Chalardimport com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
522637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaokaimport com.android.inputmethod.latin.settings.Settings;
532637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaokaimport com.android.inputmethod.latin.settings.SettingsValues;
54e28eba5074664d5716b8e58b8d0a235746b261ebKen Wakasaimport com.android.inputmethod.latin.utils.ResourceUtils;
55ccf4a310279b13bbf0b6aac76a0878178c1dfb7dTadashi G. Takaokaimport com.android.inputmethod.latin.utils.ViewLayoutUtils;
56653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
57653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokaimport java.util.ArrayList;
58653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
59049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaokaimport javax.annotation.Nonnull;
60049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaokaimport javax.annotation.Nullable;
61049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka
62653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaokafinal class SuggestionStripLayoutHelper {
63653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private static final int DEFAULT_SUGGESTIONS_COUNT_IN_STRIP = 3;
64653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private static final float DEFAULT_CENTER_SUGGESTION_PERCENTILE = 0.40f;
65653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private static final int DEFAULT_MAX_MORE_SUGGESTIONS_ROW = 2;
66653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private static final int PUNCTUATIONS_IN_STRIP = 5;
67653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private static final float MIN_TEXT_XSCALE = 0.70f;
68653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
69653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    public final int mPadding;
70653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    public final int mDividerWidth;
71653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    public final int mSuggestionsStripHeight;
724f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka    private final int mSuggestionsCountInStrip;
73653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    public final int mMoreSuggestionsRowHeight;
74653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private int mMaxMoreSuggestionsRow;
75653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    public final float mMinMoreSuggestionsWidth;
76653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    public final int mMoreSuggestionsBottomGap;
772637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka    private boolean mMoreSuggestionsAvailable;
78653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
79a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka    // The index of these {@link ArrayList} is the position in the suggestion strip. The indices
80a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka    // increase towards the right for LTR scripts and the left for RTL scripts, starting with 0.
81a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka    // The position of the most important suggestion is in {@link #mCenterPositionInStrip}
82500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka    private final ArrayList<TextView> mWordViews;
83500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka    private final ArrayList<View> mDividerViews;
84500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka    private final ArrayList<TextView> mDebugInfoViews;
85653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
86653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private final int mColorValidTypedWord;
87653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private final int mColorTypedWord;
88653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private final int mColorAutoCorrect;
89653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private final int mColorSuggested;
90653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private final float mAlphaObsoleted;
91653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private final float mCenterSuggestionWeight;
92500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka    private final int mCenterPositionInStrip;
93d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard    private final int mTypedWordPositionWhenAutocorrect;
94653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private final Drawable mMoreSuggestionsHint;
95653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private static final String MORE_SUGGESTIONS_HINT = "\u2026";
96653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
97653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private static final CharacterStyle BOLD_SPAN = new StyleSpan(Typeface.BOLD);
98653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private static final CharacterStyle UNDERLINE_SPAN = new UnderlineSpan();
99a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka
100a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard    private final int mSuggestionStripOptions;
101a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka    // These constants are the flag values of
102a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard    // {@link R.styleable#SuggestionStripView_suggestionStripOptions} attribute.
103653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private static final int AUTO_CORRECT_BOLD = 0x01;
104653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private static final int AUTO_CORRECT_UNDERLINE = 0x02;
105653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private static final int VALID_TYPED_WORD_BOLD = 0x04;
106653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
107653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    public SuggestionStripLayoutHelper(final Context context, final AttributeSet attrs,
108500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka            final int defStyle, final ArrayList<TextView> wordViews,
109500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka            final ArrayList<View> dividerViews, final ArrayList<TextView> debugInfoViews) {
110500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        mWordViews = wordViews;
111500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        mDividerViews = dividerViews;
112500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        mDebugInfoViews = debugInfoViews;
113500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka
114500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        final TextView wordView = wordViews.get(0);
115500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        final View dividerView = dividerViews.get(0);
116500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        mPadding = wordView.getCompoundPaddingLeft() + wordView.getCompoundPaddingRight();
117500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        dividerView.measure(
118500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
119500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        mDividerWidth = dividerView.getMeasuredWidth();
120500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka
121500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        final Resources res = wordView.getResources();
1222fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa        mSuggestionsStripHeight = res.getDimensionPixelSize(
1232fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa                R.dimen.config_suggestions_strip_height);
124653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
125653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final TypedArray a = context.obtainStyledAttributes(attrs,
12616ed1868a16455ef9f5485696309d518f80aea1cTadashi G. Takaoka                R.styleable.SuggestionStripView, defStyle, R.style.SuggestionStripView);
127a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard        mSuggestionStripOptions = a.getInt(
128a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard                R.styleable.SuggestionStripView_suggestionStripOptions, 0);
129653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        mAlphaObsoleted = ResourceUtils.getFraction(a,
1301f39d36458d09bfdfcf05ee0fcf7fc6116582099Tadashi G. Takaoka                R.styleable.SuggestionStripView_alphaObsoleted, 1.0f);
1311f39d36458d09bfdfcf05ee0fcf7fc6116582099Tadashi G. Takaoka        mColorValidTypedWord = a.getColor(R.styleable.SuggestionStripView_colorValidTypedWord, 0);
1321f39d36458d09bfdfcf05ee0fcf7fc6116582099Tadashi G. Takaoka        mColorTypedWord = a.getColor(R.styleable.SuggestionStripView_colorTypedWord, 0);
1331f39d36458d09bfdfcf05ee0fcf7fc6116582099Tadashi G. Takaoka        mColorAutoCorrect = a.getColor(R.styleable.SuggestionStripView_colorAutoCorrect, 0);
1341f39d36458d09bfdfcf05ee0fcf7fc6116582099Tadashi G. Takaoka        mColorSuggested = a.getColor(R.styleable.SuggestionStripView_colorSuggested, 0);
135653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        mSuggestionsCountInStrip = a.getInt(
136653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka                R.styleable.SuggestionStripView_suggestionsCountInStrip,
137653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka                DEFAULT_SUGGESTIONS_COUNT_IN_STRIP);
138653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        mCenterSuggestionWeight = ResourceUtils.getFraction(a,
139653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka                R.styleable.SuggestionStripView_centerSuggestionPercentile,
140653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka                DEFAULT_CENTER_SUGGESTION_PERCENTILE);
141653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        mMaxMoreSuggestionsRow = a.getInt(
142653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka                R.styleable.SuggestionStripView_maxMoreSuggestionsRow,
143653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka                DEFAULT_MAX_MORE_SUGGESTIONS_ROW);
144653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        mMinMoreSuggestionsWidth = ResourceUtils.getFraction(a,
145653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka                R.styleable.SuggestionStripView_minMoreSuggestionsWidth, 1.0f);
146653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        a.recycle();
147653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
148653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        mMoreSuggestionsHint = getMoreSuggestionsHint(res,
1492fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa                res.getDimension(R.dimen.config_more_suggestions_hint_text_size),
1502fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa                mColorAutoCorrect);
151500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        mCenterPositionInStrip = mSuggestionsCountInStrip / 2;
152d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard        // Assuming there are at least three suggestions. Also, note that the suggestions are
153d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard        // laid out according to script direction, so this is left of the center for LTR scripts
154d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard        // and right of the center for RTL scripts.
155d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard        mTypedWordPositionWhenAutocorrect = mCenterPositionInStrip - 1;
156653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        mMoreSuggestionsBottomGap = res.getDimensionPixelOffset(
1572fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa                R.dimen.config_more_suggestions_bottom_gap);
1582fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa        mMoreSuggestionsRowHeight = res.getDimensionPixelSize(
1592fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa                R.dimen.config_more_suggestions_row_height);
160653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
161653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
162653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    public int getMaxMoreSuggestionsRow() {
163653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        return mMaxMoreSuggestionsRow;
164653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
165653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
166c160a3932f74fea72c5347798c001d4ae961864cKen Wakasa    private int getMoreSuggestionsHeight() {
167c160a3932f74fea72c5347798c001d4ae961864cKen Wakasa        return mMaxMoreSuggestionsRow * mMoreSuggestionsRowHeight + mMoreSuggestionsBottomGap;
168c160a3932f74fea72c5347798c001d4ae961864cKen Wakasa    }
169c160a3932f74fea72c5347798c001d4ae961864cKen Wakasa
17016a43d2bd9c5d80f8e50f5a7682b657ee10621e1Yohei Yukawa    public void setMoreSuggestionsHeight(final int remainingHeight) {
171c160a3932f74fea72c5347798c001d4ae961864cKen Wakasa        final int currentHeight = getMoreSuggestionsHeight();
172c160a3932f74fea72c5347798c001d4ae961864cKen Wakasa        if (currentHeight <= remainingHeight) {
17316a43d2bd9c5d80f8e50f5a7682b657ee10621e1Yohei Yukawa            return;
174c160a3932f74fea72c5347798c001d4ae961864cKen Wakasa        }
175c160a3932f74fea72c5347798c001d4ae961864cKen Wakasa
176653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        mMaxMoreSuggestionsRow = (remainingHeight - mMoreSuggestionsBottomGap)
177653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka                / mMoreSuggestionsRowHeight;
178653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
179653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
180653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize,
181653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            final int color) {
182653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final Paint paint = new Paint();
183653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        paint.setAntiAlias(true);
184653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        paint.setTextAlign(Align.CENTER);
185653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        paint.setTextSize(textSize);
186653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        paint.setColor(color);
187653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final Rect bounds = new Rect();
188653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds);
189653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final int width = Math.round(bounds.width() + 0.5f);
190653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final int height = Math.round(bounds.height() + 0.5f);
191653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888);
192653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final Canvas canvas = new Canvas(buffer);
193653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint);
194b0dd5cc7b235bf6e0aa59e83a58b1e902a2ff372Mohammadinamul Sheik        BitmapDrawable bitmapDrawable = new BitmapDrawable(res, buffer);
195b0dd5cc7b235bf6e0aa59e83a58b1e902a2ff372Mohammadinamul Sheik        bitmapDrawable.setTargetDensity(canvas);
196b0dd5cc7b235bf6e0aa59e83a58b1e902a2ff372Mohammadinamul Sheik        return bitmapDrawable;
197653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
198653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
199a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka    private CharSequence getStyledSuggestedWord(final SuggestedWords suggestedWords,
200653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            final int indexInSuggestedWords) {
20172ac390ce64fe2825ac59029402e5f372303c8c3Tadashi G. Takaoka        if (indexInSuggestedWords >= suggestedWords.size()) {
20272ac390ce64fe2825ac59029402e5f372303c8c3Tadashi G. Takaoka            return null;
20372ac390ce64fe2825ac59029402e5f372303c8c3Tadashi G. Takaoka        }
204a273319c59ad24070ee5b35d72f044df496faa02Tadashi G. Takaoka        final String word = suggestedWords.getLabel(indexInSuggestedWords);
205a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard        // TODO: don't use the index to decide whether this is the auto-correction/typed word, as
206a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard        // this is brittle
207a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard        final boolean isAutoCorrection = suggestedWords.mWillAutoCorrect
208a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard                && indexInSuggestedWords == SuggestedWords.INDEX_OF_AUTO_CORRECTION;
209a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard        final boolean isTypedWordValid = suggestedWords.mTypedWordValid
210a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard                && indexInSuggestedWords == SuggestedWords.INDEX_OF_TYPED_WORD;
211a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard        if (!isAutoCorrection && !isTypedWordValid) {
212653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            return word;
213500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        }
214653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
215653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final Spannable spannedWord = new SpannableString(word);
216a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard        final int options = mSuggestionStripOptions;
217a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard        if ((isAutoCorrection && (options & AUTO_CORRECT_BOLD) != 0)
218a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard                || (isTypedWordValid && (options & VALID_TYPED_WORD_BOLD) != 0)) {
219049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka            addStyleSpan(spannedWord, BOLD_SPAN);
220653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
221a0984662a17df0d5c293d62bf653a40491c2bd58Jean Chalard        if (isAutoCorrection && (options & AUTO_CORRECT_UNDERLINE) != 0) {
222049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka            addStyleSpan(spannedWord, UNDERLINE_SPAN);
223653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
224653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        return spannedWord;
225653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
226653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
2272637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka    /**
2282637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka     * Convert an index of {@link SuggestedWords} to position in the suggestion strip.
2292637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka     * @param indexInSuggestedWords the index of {@link SuggestedWords}.
2302637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka     * @param suggestedWords the suggested words list
2312637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka     * @return Non-negative integer of the position in the suggestion strip.
2322637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka     *         Negative integer if the word of the index shouldn't be shown on the suggestion strip.
2332637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka     */
234d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard    private int getPositionInSuggestionStrip(final int indexInSuggestedWords,
235653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            final SuggestedWords suggestedWords) {
2362637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        final SettingsValues settingsValues = Settings.getInstance().getCurrent();
2372637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        final boolean shouldOmitTypedWord = shouldOmitTypedWord(suggestedWords.mInputStyle,
2382637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                settingsValues.mGestureFloatingPreviewTextEnabled,
2396fb586a527e7ffe5da187c8b345cdf897b8481c7Tadashi G. Takaoka                settingsValues.mShouldShowLxxSuggestionUi);
2402637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        return getPositionInSuggestionStrip(indexInSuggestedWords, suggestedWords.mWillAutoCorrect,
2416fb586a527e7ffe5da187c8b345cdf897b8481c7Tadashi G. Takaoka                settingsValues.mShouldShowLxxSuggestionUi && shouldOmitTypedWord,
2422637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                mCenterPositionInStrip, mTypedWordPositionWhenAutocorrect);
2432637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka    }
2442637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka
2452637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka    @UsedForTesting
2462637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka    static boolean shouldOmitTypedWord(final int inputStyle,
2472637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            final boolean gestureFloatingPreviewTextEnabled,
2482637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            final boolean shouldShowUiToAcceptTypedWord) {
2492637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        final boolean omitTypedWord = (inputStyle == SuggestedWords.INPUT_STYLE_TYPING)
2502637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                || (inputStyle == SuggestedWords.INPUT_STYLE_TAIL_BATCH)
2512637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                || (inputStyle == SuggestedWords.INPUT_STYLE_UPDATE_BATCH
2522637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                        && gestureFloatingPreviewTextEnabled);
2532637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        return shouldShowUiToAcceptTypedWord && omitTypedWord;
2542637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka    }
2552637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka
2562637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka    @UsedForTesting
2572637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka    static int getPositionInSuggestionStrip(final int indexInSuggestedWords,
2582637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            final boolean willAutoCorrect, final boolean omitTypedWord,
2592637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            final int centerPositionInStrip, final int typedWordPositionWhenAutoCorrect) {
2602637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        if (omitTypedWord) {
2612637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            if (indexInSuggestedWords == SuggestedWords.INDEX_OF_TYPED_WORD) {
2622637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                // Ignore.
2632637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                return -1;
2642637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            }
2652637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            if (indexInSuggestedWords == SuggestedWords.INDEX_OF_AUTO_CORRECTION) {
2662637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                // Center in the suggestion strip.
2672637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                return centerPositionInStrip;
2682637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            }
2692637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            // If neither of those, the order in the suggestion strip is left of the center first
2702637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            // then right of the center, to both edges of the suggestion strip.
2712637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            // For example, center-1, center+1, center-2, center+2, and so on.
2722637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            final int n = indexInSuggestedWords;
2732637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            final int offsetFromCenter = (n % 2) == 0 ? -(n / 2) : (n / 2);
2742637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            final int positionInSuggestionStrip = centerPositionInStrip + offsetFromCenter;
2752637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            return positionInSuggestionStrip;
2762637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        }
277d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard        final int indexToDisplayMostImportantSuggestion;
278d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard        final int indexToDisplaySecondMostImportantSuggestion;
2792637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        if (willAutoCorrect) {
280d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard            indexToDisplayMostImportantSuggestion = SuggestedWords.INDEX_OF_AUTO_CORRECTION;
281d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard            indexToDisplaySecondMostImportantSuggestion = SuggestedWords.INDEX_OF_TYPED_WORD;
282d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard        } else {
283d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard            indexToDisplayMostImportantSuggestion = SuggestedWords.INDEX_OF_TYPED_WORD;
284d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard            indexToDisplaySecondMostImportantSuggestion = SuggestedWords.INDEX_OF_AUTO_CORRECTION;
285653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
286d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard        if (indexInSuggestedWords == indexToDisplayMostImportantSuggestion) {
2872637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            // Center in the suggestion strip.
2882637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            return centerPositionInStrip;
289500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        }
290d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard        if (indexInSuggestedWords == indexToDisplaySecondMostImportantSuggestion) {
2912637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            // Center-1.
2922637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            return typedWordPositionWhenAutoCorrect;
293d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard        }
2942637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        // If neither of those, the order in the suggestion strip is right of the center first
2952637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        // then left of the center, to both edges of the suggestion strip.
2962637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        // For example, Center+1, center-2, center+2, center-3, and so on.
2972637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        final int n = indexInSuggestedWords + 1;
2982637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        final int offsetFromCenter = (n % 2) == 0 ? -(n / 2) : (n / 2);
2992637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        final int positionInSuggestionStrip = centerPositionInStrip + offsetFromCenter;
3002637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        return positionInSuggestionStrip;
301653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
302653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
303ef3a45643e950cdd934763c59963cee4089f93e1Jean Chalard    private int getSuggestionTextColor(final SuggestedWords suggestedWords,
304ef3a45643e950cdd934763c59963cee4089f93e1Jean Chalard            final int indexInSuggestedWords) {
3050c34ee20a80884fea49b228745ce87132ae6f22fJean Chalard        // Use identity for strings, not #equals : it's the typed word if it's the same object
306e83e79cb055fbfe5171fb79a2224e7d9e2cda4d2Jean Chalard        final boolean isTypedWord = suggestedWords.getInfo(indexInSuggestedWords).isKindOf(
307e83e79cb055fbfe5171fb79a2224e7d9e2cda4d2Jean Chalard                SuggestedWordInfo.KIND_TYPED);
308653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
309653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final int color;
3102637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        if (indexInSuggestedWords == SuggestedWords.INDEX_OF_AUTO_CORRECTION
3112637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                && suggestedWords.mWillAutoCorrect) {
312653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            color = mColorAutoCorrect;
3130c34ee20a80884fea49b228745ce87132ae6f22fJean Chalard        } else if (isTypedWord && suggestedWords.mTypedWordValid) {
314653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            color = mColorValidTypedWord;
315783f1932393547b2bcb88e6dc454ce888ce1d4a1Jean Chalard        } else if (isTypedWord) {
316653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            color = mColorTypedWord;
317783f1932393547b2bcb88e6dc454ce888ce1d4a1Jean Chalard        } else {
318783f1932393547b2bcb88e6dc454ce888ce1d4a1Jean Chalard            color = mColorSuggested;
319653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
320783f1932393547b2bcb88e6dc454ce888ce1d4a1Jean Chalard        if (suggestedWords.mIsObsoleteSuggestions && !isTypedWord) {
321653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            return applyAlpha(color, mAlphaObsoleted);
322653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
323500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        return color;
324653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
325653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
326653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    private static int applyAlpha(final int color, final float alpha) {
327653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final int newAlpha = (int)(Color.alpha(color) * alpha);
328653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        return Color.argb(newAlpha, Color.red(color), Color.green(color), Color.blue(color));
329653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
330653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
331500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka    private static void addDivider(final ViewGroup stripView, final View dividerView) {
332500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        stripView.addView(dividerView);
333653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final LinearLayout.LayoutParams params =
334500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka                (LinearLayout.LayoutParams)dividerView.getLayoutParams();
335653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        params.gravity = Gravity.CENTER;
336653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
337653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
3384f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka    /**
3392637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka     * Layout suggestions to the suggestions strip. And returns the start index of more
3402637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka     * suggestions.
3414f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka     *
3424f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka     * @param suggestedWords suggestions to be shown in the suggestions strip.
3434f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka     * @param stripView the suggestions strip view.
3444f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka     * @param placerView the view where the debug info will be placed.
3452637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka     * @return the start index of more suggestions.
3464f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka     */
34724c275ccb9d12d6160b09c20afe17601b09957f1Dan Zivkovic    public int layoutAndReturnStartIndexOfMoreSuggestions(
34824c275ccb9d12d6160b09c20afe17601b09957f1Dan Zivkovic            final Context context,
34924c275ccb9d12d6160b09c20afe17601b09957f1Dan Zivkovic            final SuggestedWords suggestedWords,
35024c275ccb9d12d6160b09c20afe17601b09957f1Dan Zivkovic            final ViewGroup stripView,
35124c275ccb9d12d6160b09c20afe17601b09957f1Dan Zivkovic            final ViewGroup placerView) {
352a273319c59ad24070ee5b35d72f044df496faa02Tadashi G. Takaoka        if (suggestedWords.isPunctuationSuggestions()) {
3532637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            return layoutPunctuationsAndReturnStartIndexOfMoreSuggestions(
354a273319c59ad24070ee5b35d72f044df496faa02Tadashi G. Takaoka                    (PunctuationSuggestions)suggestedWords, stripView);
355653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
356653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
3576fb586a527e7ffe5da187c8b345cdf897b8481c7Tadashi G. Takaoka        final int wordCountToShow = suggestedWords.getWordCountToShow(
3586fb586a527e7ffe5da187c8b345cdf897b8481c7Tadashi G. Takaoka                Settings.getInstance().getCurrent().mShouldShowLxxSuggestionUi);
3592637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        final int startIndexOfMoreSuggestions = setupWordViewsAndReturnStartIndexOfMoreSuggestions(
3602637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                suggestedWords, mSuggestionsCountInStrip);
361043273b00fb0f42761a38eb53c9911ef022dd5aeTadashi G. Takaoka        final TextView centerWordView = mWordViews.get(mCenterPositionInStrip);
3622b479cccaeb0bb54121730cb5e47cca3f7517c67Tadashi G. Takaoka        final int stripWidth = stripView.getWidth();
3632b479cccaeb0bb54121730cb5e47cca3f7517c67Tadashi G. Takaoka        final int centerWidth = getSuggestionWidth(mCenterPositionInStrip, stripWidth);
3646fb586a527e7ffe5da187c8b345cdf897b8481c7Tadashi G. Takaoka        if (wordCountToShow == 1 || getTextScaleX(centerWordView.getText(), centerWidth,
3654f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka                centerWordView.getPaint()) < MIN_TEXT_XSCALE) {
366043273b00fb0f42761a38eb53c9911ef022dd5aeTadashi G. Takaoka            // Layout only the most relevant suggested word at the center of the suggestion strip
367043273b00fb0f42761a38eb53c9911ef022dd5aeTadashi G. Takaoka            // by consolidating all slots in the strip.
3682637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            final int countInStrip = 1;
3696fb586a527e7ffe5da187c8b345cdf897b8481c7Tadashi G. Takaoka            mMoreSuggestionsAvailable = (wordCountToShow > countInStrip);
37024c275ccb9d12d6160b09c20afe17601b09957f1Dan Zivkovic            layoutWord(context, mCenterPositionInStrip, stripWidth - mPadding);
371043273b00fb0f42761a38eb53c9911ef022dd5aeTadashi G. Takaoka            stripView.addView(centerWordView);
372043273b00fb0f42761a38eb53c9911ef022dd5aeTadashi G. Takaoka            setLayoutWeight(centerWordView, 1.0f, ViewGroup.LayoutParams.MATCH_PARENT);
373043273b00fb0f42761a38eb53c9911ef022dd5aeTadashi G. Takaoka            if (SuggestionStripView.DBG) {
3742b479cccaeb0bb54121730cb5e47cca3f7517c67Tadashi G. Takaoka                layoutDebugInfo(mCenterPositionInStrip, placerView, stripWidth);
375043273b00fb0f42761a38eb53c9911ef022dd5aeTadashi G. Takaoka            }
3762637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            final Integer lastIndex = (Integer)centerWordView.getTag();
3772637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            return (lastIndex == null ? 0 : lastIndex) + 1;
3782637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        }
3792637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka
3802637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        final int countInStrip = mSuggestionsCountInStrip;
3816fb586a527e7ffe5da187c8b345cdf897b8481c7Tadashi G. Takaoka        mMoreSuggestionsAvailable = (wordCountToShow > countInStrip);
3825f00fe09e9a611b647592188316e5999465df4d3Tadashi G. Takaoka        @SuppressWarnings("unused")
3832637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        int x = 0;
3842637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        for (int positionInStrip = 0; positionInStrip < countInStrip; positionInStrip++) {
3852637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            if (positionInStrip != 0) {
3862637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                final View divider = mDividerViews.get(positionInStrip);
3872637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                // Add divider if this isn't the left most suggestion in suggestions strip.
3882637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                addDivider(stripView, divider);
3892637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                x += divider.getMeasuredWidth();
3902637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            }
3912637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka
3922637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            final int width = getSuggestionWidth(positionInStrip, stripWidth);
39324c275ccb9d12d6160b09c20afe17601b09957f1Dan Zivkovic            final TextView wordView = layoutWord(context, positionInStrip, width);
3942637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            stripView.addView(wordView);
3952637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            setLayoutWeight(wordView, getSuggestionWeight(positionInStrip),
3962637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                    ViewGroup.LayoutParams.MATCH_PARENT);
3972637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            x += wordView.getMeasuredWidth();
3982637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka
3992637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            if (SuggestionStripView.DBG) {
4002637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                layoutDebugInfo(positionInStrip, placerView, x);
401653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            }
402653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
4032637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        return startIndexOfMoreSuggestions;
404653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
405653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
406653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    /**
407a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka     * Format appropriately the suggested word in {@link #mWordViews} specified by
408a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka     * <code>positionInStrip</code>. When the suggested word doesn't exist, the corresponding
409a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka     * {@link TextView} will be disabled and never respond to user interaction. The suggested word
410a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka     * may be shrunk or ellipsized to fit in the specified width.
411653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka     *
412500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka     * The <code>positionInStrip</code> argument is the index in the suggestion strip. The indices
413653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka     * increase towards the right for LTR scripts and the left for RTL scripts, starting with 0.
414a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka     * The position of the most important suggestion is in {@link #mCenterPositionInStrip}. This
415653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka     * usually doesn't match the index in <code>suggedtedWords</code> -- see
416d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard     * {@link #getPositionInSuggestionStrip(int,SuggestedWords)}.
417653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka     *
418a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka     * @param positionInStrip the position in the suggestion strip.
419653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka     * @param width the maximum width for layout in pixels.
420653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka     * @return the {@link TextView} containing the suggested word appropriately formatted.
421653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka     */
42224c275ccb9d12d6160b09c20afe17601b09957f1Dan Zivkovic    private TextView layoutWord(final Context context, final int positionInStrip, final int width) {
423a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka        final TextView wordView = mWordViews.get(positionInStrip);
424a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka        final CharSequence word = wordView.getText();
425500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        if (positionInStrip == mCenterPositionInStrip && mMoreSuggestionsAvailable) {
426653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            // TODO: This "more suggestions hint" should have a nicely designed icon.
427500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka            wordView.setCompoundDrawablesWithIntrinsicBounds(
428653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka                    null, null, null, mMoreSuggestionsHint);
429653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            // HACK: Align with other TextViews that have no compound drawables.
430500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka            wordView.setCompoundDrawablePadding(-mMoreSuggestionsHint.getIntrinsicHeight());
431653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        } else {
432500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka            wordView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
433653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
434f3c852efadaec7779c47b6130290279a400b5ab5Tadashi G. Takaoka        // {@link StyleSpan} in a content description may cause an issue of TTS/TalkBack.
435f3c852efadaec7779c47b6130290279a400b5ab5Tadashi G. Takaoka        // Use a simple {@link String} to avoid the issue.
43624c275ccb9d12d6160b09c20afe17601b09957f1Dan Zivkovic        wordView.setContentDescription(
43724c275ccb9d12d6160b09c20afe17601b09957f1Dan Zivkovic                TextUtils.isEmpty(word)
43824c275ccb9d12d6160b09c20afe17601b09957f1Dan Zivkovic                    ? context.getResources().getString(R.string.spoken_empty_suggestion)
43924c275ccb9d12d6160b09c20afe17601b09957f1Dan Zivkovic                    : word.toString());
440049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        final CharSequence text = getEllipsizedTextWithSettingScaleX(
441049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka                word, width, wordView.getPaint());
442049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        final float scaleX = wordView.getTextScaleX();
443500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        wordView.setText(text); // TextView.setText() resets text scale x to 1.0.
444049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        wordView.setTextScaleX(scaleX);
445ae42cd7e43488aead5310c5fcb7467b8ce9c2dd6Tadashi G. Takaoka        // A <code>wordView</code> should be disabled when <code>word</code> is empty in order to
446ae42cd7e43488aead5310c5fcb7467b8ce9c2dd6Tadashi G. Takaoka        // make it unclickable.
447ae42cd7e43488aead5310c5fcb7467b8ce9c2dd6Tadashi G. Takaoka        // With accessibility touch exploration on, <code>wordView</code> should be enabled even
448ae42cd7e43488aead5310c5fcb7467b8ce9c2dd6Tadashi G. Takaoka        // when it is empty to avoid announcing as "disabled".
449ae42cd7e43488aead5310c5fcb7467b8ce9c2dd6Tadashi G. Takaoka        wordView.setEnabled(!TextUtils.isEmpty(word)
450ae42cd7e43488aead5310c5fcb7467b8ce9c2dd6Tadashi G. Takaoka                || AccessibilityUtils.getInstance().isTouchExplorationEnabled());
451500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        return wordView;
452653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
453653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
454a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka    private void layoutDebugInfo(final int positionInStrip, final ViewGroup placerView,
455a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka            final int x) {
456a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka        final TextView debugInfoView = mDebugInfoViews.get(positionInStrip);
457a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka        final CharSequence debugInfo = debugInfoView.getText();
458653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        if (debugInfo == null) {
459653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            return;
460653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
461500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        placerView.addView(debugInfoView);
462500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        debugInfoView.measure(
463500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
464500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        final int infoWidth = debugInfoView.getMeasuredWidth();
465500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        final int y = debugInfoView.getMeasuredHeight();
466653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        ViewLayoutUtils.placeViewAt(
467500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka                debugInfoView, x - infoWidth, y, infoWidth, debugInfoView.getMeasuredHeight());
468653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
469653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
470500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka    private int getSuggestionWidth(final int positionInStrip, final int maxWidth) {
471653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final int paddings = mPadding * mSuggestionsCountInStrip;
472653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final int dividers = mDividerWidth * (mSuggestionsCountInStrip - 1);
473653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final int availableWidth = maxWidth - paddings - dividers;
474500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        return (int)(availableWidth * getSuggestionWeight(positionInStrip));
475653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
476653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
477500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka    private float getSuggestionWeight(final int positionInStrip) {
478500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        if (positionInStrip == mCenterPositionInStrip) {
479653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            return mCenterSuggestionWeight;
480653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
481500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        // TODO: Revisit this for cases of 5 or more suggestions
482500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        return (1.0f - mCenterSuggestionWeight) / (mSuggestionsCountInStrip - 1);
483653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
484653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
4852637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka    private int setupWordViewsAndReturnStartIndexOfMoreSuggestions(
4862637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            final SuggestedWords suggestedWords, final int maxSuggestionInStrip) {
487d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard        // Clear all suggestions first
4882637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        for (int positionInStrip = 0; positionInStrip < maxSuggestionInStrip; ++positionInStrip) {
48906d1cdd5dc73ef118e2a2853fc75dc911202f373Tadashi G. Takaoka            final TextView wordView = mWordViews.get(positionInStrip);
49006d1cdd5dc73ef118e2a2853fc75dc911202f373Tadashi G. Takaoka            wordView.setText(null);
49106d1cdd5dc73ef118e2a2853fc75dc911202f373Tadashi G. Takaoka            wordView.setTag(null);
492d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard            // Make this inactive for touches in {@link #layoutWord(int,int)}.
493d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard            if (SuggestionStripView.DBG) {
494d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard                mDebugInfoViews.get(positionInStrip).setText(null);
495d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard            }
496d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard        }
4972637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        int count = 0;
4982637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        int indexInSuggestedWords;
4992637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        for (indexInSuggestedWords = 0; indexInSuggestedWords < suggestedWords.size()
5002637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                && count < maxSuggestionInStrip; indexInSuggestedWords++) {
501d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard            final int positionInStrip =
502d7d5ff1f99f103438290f3045df185c502c4b60bJean Chalard                    getPositionInSuggestionStrip(indexInSuggestedWords, suggestedWords);
5032637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            if (positionInStrip < 0) {
5042637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka                continue;
5052637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            }
506a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka            final TextView wordView = mWordViews.get(positionInStrip);
507a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka            // {@link TextView#getTag()} is used to get the index in suggestedWords at
508a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka            // {@link SuggestionStripView#onClick(View)}.
509a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka            wordView.setTag(indexInSuggestedWords);
510a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka            wordView.setText(getStyledSuggestedWord(suggestedWords, indexInSuggestedWords));
511ef3a45643e950cdd934763c59963cee4089f93e1Jean Chalard            wordView.setTextColor(getSuggestionTextColor(suggestedWords, indexInSuggestedWords));
512a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka            if (SuggestionStripView.DBG) {
513a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka                mDebugInfoViews.get(positionInStrip).setText(
5144be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka                        suggestedWords.getDebugString(indexInSuggestedWords));
515a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka            }
5162637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka            count++;
517653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
5182637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka        return indexInSuggestedWords;
519653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
520653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
5212637be27c51be03e39b0db1c66312c4cc55bc7deTadashi G. Takaoka    private int layoutPunctuationsAndReturnStartIndexOfMoreSuggestions(
522a273319c59ad24070ee5b35d72f044df496faa02Tadashi G. Takaoka            final PunctuationSuggestions punctuationSuggestions, final ViewGroup stripView) {
523a273319c59ad24070ee5b35d72f044df496faa02Tadashi G. Takaoka        final int countInStrip = Math.min(punctuationSuggestions.size(), PUNCTUATIONS_IN_STRIP);
524a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka        for (int positionInStrip = 0; positionInStrip < countInStrip; positionInStrip++) {
525a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka            if (positionInStrip != 0) {
526653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka                // Add divider if this isn't the left most suggestion in suggestions strip.
527a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka                addDivider(stripView, mDividerViews.get(positionInStrip));
528653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            }
529653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
530a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka            final TextView wordView = mWordViews.get(positionInStrip);
531f3c852efadaec7779c47b6130290279a400b5ab5Tadashi G. Takaoka            final String punctuation = punctuationSuggestions.getLabel(positionInStrip);
5329b570b5e62581f41c40c4558d95c77ad5896934cTadashi G. Takaoka            // {@link TextView#getTag()} is used to get the index in suggestedWords at
5339b570b5e62581f41c40c4558d95c77ad5896934cTadashi G. Takaoka            // {@link SuggestionStripView#onClick(View)}.
5349b570b5e62581f41c40c4558d95c77ad5896934cTadashi G. Takaoka            wordView.setTag(positionInStrip);
535f3c852efadaec7779c47b6130290279a400b5ab5Tadashi G. Takaoka            wordView.setText(punctuation);
536f3c852efadaec7779c47b6130290279a400b5ab5Tadashi G. Takaoka            wordView.setContentDescription(punctuation);
537a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka            wordView.setTextScaleX(1.0f);
538a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka            wordView.setCompoundDrawables(null, null, null, null);
539f3c852efadaec7779c47b6130290279a400b5ab5Tadashi G. Takaoka            wordView.setTextColor(mColorAutoCorrect);
540a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka            stripView.addView(wordView);
541a0c3e02d7168dd4cee4cad51491537dadb0362ccTadashi G. Takaoka            setLayoutWeight(wordView, 1.0f, mSuggestionsStripHeight);
542653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
543a273319c59ad24070ee5b35d72f044df496faa02Tadashi G. Takaoka        mMoreSuggestionsAvailable = (punctuationSuggestions.size() > countInStrip);
5444f8a8f125e329b55e49d31a98b5368113440c755Tadashi G. Takaoka        return countInStrip;
545653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
546653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
54714e908c3486ae5996e66625e959ff45c5f3740efTadashi G. Takaoka    public void layoutImportantNotice(final View importantNoticeStrip,
5486abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka            final String importantNoticeTitle) {
5498a296e43c903a3377f28943ba4a59082ae7fe60aTadashi G. Takaoka        final TextView titleView = (TextView)importantNoticeStrip.findViewById(
5508a296e43c903a3377f28943ba4a59082ae7fe60aTadashi G. Takaoka                R.id.important_notice_title);
551a9c1a3da63c83dd68424a0851079a55d110cc63aTadashi G. Takaoka        final int width = titleView.getWidth() - titleView.getPaddingLeft()
552a9c1a3da63c83dd68424a0851079a55d110cc63aTadashi G. Takaoka                - titleView.getPaddingRight();
5538a296e43c903a3377f28943ba4a59082ae7fe60aTadashi G. Takaoka        titleView.setTextColor(mColorAutoCorrect);
554049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        titleView.setText(importantNoticeTitle); // TextView.setText() resets text scale x to 1.0.
55514e908c3486ae5996e66625e959ff45c5f3740efTadashi G. Takaoka        final float titleScaleX = getTextScaleX(importantNoticeTitle, width, titleView.getPaint());
5568dda9e480b6519ca57565c8aa507e14bff739b60Tadashi G. Takaoka        titleView.setTextScaleX(titleScaleX);
5578a296e43c903a3377f28943ba4a59082ae7fe60aTadashi G. Takaoka    }
5588a296e43c903a3377f28943ba4a59082ae7fe60aTadashi G. Takaoka
5598a296e43c903a3377f28943ba4a59082ae7fe60aTadashi G. Takaoka    static void setLayoutWeight(final View v, final float weight, final int height) {
560653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final ViewGroup.LayoutParams lp = v.getLayoutParams();
561653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        if (lp instanceof LinearLayout.LayoutParams) {
562653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            final LinearLayout.LayoutParams llp = (LinearLayout.LayoutParams)lp;
563653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            llp.weight = weight;
564653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            llp.width = 0;
565653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            llp.height = height;
566653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
567653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
568653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
569049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka    private static float getTextScaleX(@Nullable final CharSequence text, final int maxWidth,
570653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            final TextPaint paint) {
571653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        paint.setTextScaleX(1.0f);
572653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final int width = getTextWidth(text, paint);
5738dda9e480b6519ca57565c8aa507e14bff739b60Tadashi G. Takaoka        if (width <= maxWidth || maxWidth <= 0) {
574653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            return 1.0f;
575653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
576049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        return maxWidth / (float) width;
577653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
578653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
579049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka    @Nullable
580049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka    private static CharSequence getEllipsizedTextWithSettingScaleX(
581049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka            @Nullable final CharSequence text, final int maxWidth, @Nonnull final TextPaint paint) {
582500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        if (text == null) {
583500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka            return null;
584653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
585500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        final float scaleX = getTextScaleX(text, maxWidth, paint);
586653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        if (scaleX >= MIN_TEXT_XSCALE) {
587653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            paint.setTextScaleX(scaleX);
588653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            return text;
589653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
590653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
591049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        // <code>text</code> must be ellipsized with minimum text scale x.
592653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        paint.setTextScaleX(MIN_TEXT_XSCALE);
593049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        final boolean hasBoldStyle = hasStyleSpan(text, BOLD_SPAN);
594049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        final boolean hasUnderlineStyle = hasStyleSpan(text, UNDERLINE_SPAN);
595049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        // TextUtils.ellipsize erases any span object existed after ellipsized point.
596049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        // We have to restore these spans afterward.
597049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        final CharSequence ellipsizedText = TextUtils.ellipsize(
598049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka                text, paint, maxWidth, TextUtils.TruncateAt.MIDDLE);
599049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        if (!hasBoldStyle && !hasUnderlineStyle) {
600049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka            return ellipsizedText;
601049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        }
602049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        final Spannable spannableText = (ellipsizedText instanceof Spannable)
603049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka                ? (Spannable)ellipsizedText : new SpannableString(ellipsizedText);
604049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        if (hasBoldStyle) {
605049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka            addStyleSpan(spannableText, BOLD_SPAN);
606049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        }
607049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        if (hasUnderlineStyle) {
608049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka            addStyleSpan(spannableText, UNDERLINE_SPAN);
609049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        }
610049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        return spannableText;
611049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka    }
612049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka
613049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka    private static boolean hasStyleSpan(@Nullable final CharSequence text,
614049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka            final CharacterStyle style) {
615049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        if (text instanceof Spanned) {
616049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka            return ((Spanned)text).getSpanStart(style) >= 0;
617049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        }
618049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        return false;
619049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka    }
620049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka
621049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka    private static void addStyleSpan(@Nonnull final Spannable text, final CharacterStyle style) {
622049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        text.removeSpan(style);
623049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        text.setSpan(style, 0, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
624653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
625653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
626049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka    private static int getTextWidth(@Nullable final CharSequence text, final TextPaint paint) {
627500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        if (TextUtils.isEmpty(text)) {
628500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka            return 0;
629500a9752b1e61885f37b0fb26f09dfdd65da4e5eTadashi G. Takaoka        }
630049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        final int length = text.length();
631049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        final float[] widths = new float[length];
632049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        final int count;
633653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        final Typeface savedTypeface = paint.getTypeface();
634049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        try {
635049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka            paint.setTypeface(getTextTypeface(text));
636049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka            count = paint.getTextWidths(text, 0, length, widths);
637049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        } finally {
638049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka            paint.setTypeface(savedTypeface);
639049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        }
640653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        int width = 0;
641653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        for (int i = 0; i < count; i++) {
642653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka            width += Math.round(widths[i] + 0.5f);
643653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        }
644653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka        return width;
645653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
646653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka
647049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka    private static Typeface getTextTypeface(@Nullable final CharSequence text) {
648049d642cc4f5bfe911b2a705cd34e66c7ef780daTadashi G. Takaoka        return hasStyleSpan(text, BOLD_SPAN) ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT;
649653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka    }
650653603b7253d96c7fad674ed301de5bc8050b68cTadashi G. Takaoka}
651