1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.latin.suggestions; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.content.res.TypedArray; 22 import android.graphics.Bitmap; 23 import android.graphics.Canvas; 24 import android.graphics.Color; 25 import android.graphics.Paint; 26 import android.graphics.Paint.Align; 27 import android.graphics.Rect; 28 import android.graphics.Typeface; 29 import android.graphics.drawable.BitmapDrawable; 30 import android.graphics.drawable.Drawable; 31 import android.text.Spannable; 32 import android.text.SpannableString; 33 import android.text.Spanned; 34 import android.text.TextPaint; 35 import android.text.TextUtils; 36 import android.text.style.CharacterStyle; 37 import android.text.style.StyleSpan; 38 import android.text.style.UnderlineSpan; 39 import android.util.AttributeSet; 40 import android.view.Gravity; 41 import android.view.LayoutInflater; 42 import android.view.View; 43 import android.view.View.OnClickListener; 44 import android.view.ViewGroup; 45 import android.widget.LinearLayout; 46 import android.widget.TextView; 47 48 import com.android.inputmethod.latin.LatinImeLogger; 49 import com.android.inputmethod.latin.R; 50 import com.android.inputmethod.latin.SuggestedWords; 51 import com.android.inputmethod.latin.utils.AutoCorrectionUtils; 52 import com.android.inputmethod.latin.utils.ResourceUtils; 53 import com.android.inputmethod.latin.utils.ViewLayoutUtils; 54 55 import java.util.ArrayList; 56 57 final class SuggestionStripLayoutHelper { 58 private static final int DEFAULT_SUGGESTIONS_COUNT_IN_STRIP = 3; 59 private static final float DEFAULT_CENTER_SUGGESTION_PERCENTILE = 0.40f; 60 private static final int DEFAULT_MAX_MORE_SUGGESTIONS_ROW = 2; 61 private static final int PUNCTUATIONS_IN_STRIP = 5; 62 private static final float MIN_TEXT_XSCALE = 0.70f; 63 64 public final int mPadding; 65 public final int mDividerWidth; 66 public final int mSuggestionsStripHeight; 67 public final int mSuggestionsCountInStrip; 68 public final int mMoreSuggestionsRowHeight; 69 private int mMaxMoreSuggestionsRow; 70 public final float mMinMoreSuggestionsWidth; 71 public final int mMoreSuggestionsBottomGap; 72 public boolean mMoreSuggestionsAvailable; 73 74 // The index of these {@link ArrayList} is the position in the suggestion strip. The indices 75 // increase towards the right for LTR scripts and the left for RTL scripts, starting with 0. 76 // The position of the most important suggestion is in {@link #mCenterPositionInStrip} 77 private final ArrayList<TextView> mWordViews; 78 private final ArrayList<View> mDividerViews; 79 private final ArrayList<TextView> mDebugInfoViews; 80 81 private final int mColorValidTypedWord; 82 private final int mColorTypedWord; 83 private final int mColorAutoCorrect; 84 private final int mColorSuggested; 85 private final float mAlphaObsoleted; 86 private final float mCenterSuggestionWeight; 87 private final int mCenterPositionInStrip; 88 private final int mTypedWordPositionWhenAutocorrect; 89 private final Drawable mMoreSuggestionsHint; 90 private static final String MORE_SUGGESTIONS_HINT = "\u2026"; 91 private static final String LEFTWARDS_ARROW = "\u2190"; 92 93 private static final CharacterStyle BOLD_SPAN = new StyleSpan(Typeface.BOLD); 94 private static final CharacterStyle UNDERLINE_SPAN = new UnderlineSpan(); 95 96 private final int mSuggestionStripOption; 97 // These constants are the flag values of 98 // {@link R.styleable#SuggestionStripView_suggestionStripOption} attribute. 99 private static final int AUTO_CORRECT_BOLD = 0x01; 100 private static final int AUTO_CORRECT_UNDERLINE = 0x02; 101 private static final int VALID_TYPED_WORD_BOLD = 0x04; 102 103 private final TextView mWordToSaveView; 104 private final TextView mLeftwardsArrowView; 105 private final TextView mHintToSaveView; 106 107 public SuggestionStripLayoutHelper(final Context context, final AttributeSet attrs, 108 final int defStyle, final ArrayList<TextView> wordViews, 109 final ArrayList<View> dividerViews, final ArrayList<TextView> debugInfoViews) { 110 mWordViews = wordViews; 111 mDividerViews = dividerViews; 112 mDebugInfoViews = debugInfoViews; 113 114 final TextView wordView = wordViews.get(0); 115 final View dividerView = dividerViews.get(0); 116 mPadding = wordView.getCompoundPaddingLeft() + wordView.getCompoundPaddingRight(); 117 dividerView.measure( 118 ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 119 mDividerWidth = dividerView.getMeasuredWidth(); 120 121 final Resources res = wordView.getResources(); 122 mSuggestionsStripHeight = res.getDimensionPixelSize(R.dimen.suggestions_strip_height); 123 124 final TypedArray a = context.obtainStyledAttributes(attrs, 125 R.styleable.SuggestionStripView, defStyle, R.style.SuggestionStripView); 126 mSuggestionStripOption = a.getInt( 127 R.styleable.SuggestionStripView_suggestionStripOption, 0); 128 mAlphaObsoleted = ResourceUtils.getFraction(a, 129 R.styleable.SuggestionStripView_alphaObsoleted, 1.0f); 130 mColorValidTypedWord = a.getColor(R.styleable.SuggestionStripView_colorValidTypedWord, 0); 131 mColorTypedWord = a.getColor(R.styleable.SuggestionStripView_colorTypedWord, 0); 132 mColorAutoCorrect = a.getColor(R.styleable.SuggestionStripView_colorAutoCorrect, 0); 133 mColorSuggested = a.getColor(R.styleable.SuggestionStripView_colorSuggested, 0); 134 mSuggestionsCountInStrip = a.getInt( 135 R.styleable.SuggestionStripView_suggestionsCountInStrip, 136 DEFAULT_SUGGESTIONS_COUNT_IN_STRIP); 137 mCenterSuggestionWeight = ResourceUtils.getFraction(a, 138 R.styleable.SuggestionStripView_centerSuggestionPercentile, 139 DEFAULT_CENTER_SUGGESTION_PERCENTILE); 140 mMaxMoreSuggestionsRow = a.getInt( 141 R.styleable.SuggestionStripView_maxMoreSuggestionsRow, 142 DEFAULT_MAX_MORE_SUGGESTIONS_ROW); 143 mMinMoreSuggestionsWidth = ResourceUtils.getFraction(a, 144 R.styleable.SuggestionStripView_minMoreSuggestionsWidth, 1.0f); 145 a.recycle(); 146 147 mMoreSuggestionsHint = getMoreSuggestionsHint(res, 148 res.getDimension(R.dimen.more_suggestions_hint_text_size), mColorAutoCorrect); 149 mCenterPositionInStrip = mSuggestionsCountInStrip / 2; 150 // Assuming there are at least three suggestions. Also, note that the suggestions are 151 // laid out according to script direction, so this is left of the center for LTR scripts 152 // and right of the center for RTL scripts. 153 mTypedWordPositionWhenAutocorrect = mCenterPositionInStrip - 1; 154 mMoreSuggestionsBottomGap = res.getDimensionPixelOffset( 155 R.dimen.more_suggestions_bottom_gap); 156 mMoreSuggestionsRowHeight = res.getDimensionPixelSize(R.dimen.more_suggestions_row_height); 157 158 final LayoutInflater inflater = LayoutInflater.from(context); 159 mWordToSaveView = (TextView)inflater.inflate(R.layout.suggestion_word, null); 160 mLeftwardsArrowView = (TextView)inflater.inflate(R.layout.hint_add_to_dictionary, null); 161 mHintToSaveView = (TextView)inflater.inflate(R.layout.hint_add_to_dictionary, null); 162 } 163 164 public int getMaxMoreSuggestionsRow() { 165 return mMaxMoreSuggestionsRow; 166 } 167 168 private int getMoreSuggestionsHeight() { 169 return mMaxMoreSuggestionsRow * mMoreSuggestionsRowHeight + mMoreSuggestionsBottomGap; 170 } 171 172 public int setMoreSuggestionsHeight(final int remainingHeight) { 173 final int currentHeight = getMoreSuggestionsHeight(); 174 if (currentHeight <= remainingHeight) { 175 return currentHeight; 176 } 177 178 mMaxMoreSuggestionsRow = (remainingHeight - mMoreSuggestionsBottomGap) 179 / mMoreSuggestionsRowHeight; 180 final int newHeight = getMoreSuggestionsHeight(); 181 return newHeight; 182 } 183 184 private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, 185 final int color) { 186 final Paint paint = new Paint(); 187 paint.setAntiAlias(true); 188 paint.setTextAlign(Align.CENTER); 189 paint.setTextSize(textSize); 190 paint.setColor(color); 191 final Rect bounds = new Rect(); 192 paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds); 193 final int width = Math.round(bounds.width() + 0.5f); 194 final int height = Math.round(bounds.height() + 0.5f); 195 final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888); 196 final Canvas canvas = new Canvas(buffer); 197 canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint); 198 return new BitmapDrawable(res, buffer); 199 } 200 201 private CharSequence getStyledSuggestedWord(final SuggestedWords suggestedWords, 202 final int indexInSuggestedWords) { 203 if (indexInSuggestedWords >= suggestedWords.size()) { 204 return null; 205 } 206 final String word = suggestedWords.getWord(indexInSuggestedWords); 207 final boolean isAutoCorrect = indexInSuggestedWords == 1 208 && suggestedWords.willAutoCorrect(); 209 final boolean isTypedWordValid = indexInSuggestedWords == 0 210 && suggestedWords.mTypedWordValid; 211 if (!isAutoCorrect && !isTypedWordValid) { 212 return word; 213 } 214 215 final int len = word.length(); 216 final Spannable spannedWord = new SpannableString(word); 217 final int option = mSuggestionStripOption; 218 if ((isAutoCorrect && (option & AUTO_CORRECT_BOLD) != 0) 219 || (isTypedWordValid && (option & VALID_TYPED_WORD_BOLD) != 0)) { 220 spannedWord.setSpan(BOLD_SPAN, 0, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 221 } 222 if (isAutoCorrect && (option & AUTO_CORRECT_UNDERLINE) != 0) { 223 spannedWord.setSpan(UNDERLINE_SPAN, 0, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 224 } 225 return spannedWord; 226 } 227 228 private int getPositionInSuggestionStrip(final int indexInSuggestedWords, 229 final SuggestedWords suggestedWords) { 230 final int indexToDisplayMostImportantSuggestion; 231 final int indexToDisplaySecondMostImportantSuggestion; 232 if (suggestedWords.willAutoCorrect()) { 233 indexToDisplayMostImportantSuggestion = SuggestedWords.INDEX_OF_AUTO_CORRECTION; 234 indexToDisplaySecondMostImportantSuggestion = SuggestedWords.INDEX_OF_TYPED_WORD; 235 } else { 236 indexToDisplayMostImportantSuggestion = SuggestedWords.INDEX_OF_TYPED_WORD; 237 indexToDisplaySecondMostImportantSuggestion = SuggestedWords.INDEX_OF_AUTO_CORRECTION; 238 } 239 if (indexInSuggestedWords == indexToDisplayMostImportantSuggestion) { 240 return mCenterPositionInStrip; 241 } 242 if (indexInSuggestedWords == indexToDisplaySecondMostImportantSuggestion) { 243 return mTypedWordPositionWhenAutocorrect; 244 } 245 // If neither of those, the order in the suggestion strip is the same as in SuggestedWords. 246 return indexInSuggestedWords; 247 } 248 249 private int getSuggestionTextColor(final int indexInSuggestedWords, 250 final SuggestedWords suggestedWords) { 251 final int positionInStrip = 252 getPositionInSuggestionStrip(indexInSuggestedWords, suggestedWords); 253 // TODO: Need to revisit this logic with bigram suggestions 254 final boolean isSuggested = (indexInSuggestedWords != SuggestedWords.INDEX_OF_TYPED_WORD); 255 256 final int color; 257 if (positionInStrip == mCenterPositionInStrip && suggestedWords.willAutoCorrect()) { 258 color = mColorAutoCorrect; 259 } else if (positionInStrip == mCenterPositionInStrip && suggestedWords.mTypedWordValid) { 260 color = mColorValidTypedWord; 261 } else if (isSuggested) { 262 color = mColorSuggested; 263 } else { 264 color = mColorTypedWord; 265 } 266 if (LatinImeLogger.sDBG && suggestedWords.size() > 1) { 267 // If we auto-correct, then the autocorrection is in slot 0 and the typed word 268 // is in slot 1. 269 if (positionInStrip == mCenterPositionInStrip 270 && AutoCorrectionUtils.shouldBlockAutoCorrectionBySafetyNet( 271 suggestedWords.getWord(SuggestedWords.INDEX_OF_AUTO_CORRECTION), 272 suggestedWords.getWord(SuggestedWords.INDEX_OF_TYPED_WORD))) { 273 return 0xFFFF0000; 274 } 275 } 276 277 if (suggestedWords.mIsObsoleteSuggestions && isSuggested) { 278 return applyAlpha(color, mAlphaObsoleted); 279 } 280 return color; 281 } 282 283 private static int applyAlpha(final int color, final float alpha) { 284 final int newAlpha = (int)(Color.alpha(color) * alpha); 285 return Color.argb(newAlpha, Color.red(color), Color.green(color), Color.blue(color)); 286 } 287 288 private static void addDivider(final ViewGroup stripView, final View dividerView) { 289 stripView.addView(dividerView); 290 final LinearLayout.LayoutParams params = 291 (LinearLayout.LayoutParams)dividerView.getLayoutParams(); 292 params.gravity = Gravity.CENTER; 293 } 294 295 public void layout(final SuggestedWords suggestedWords, final ViewGroup stripView, 296 final ViewGroup placerView) { 297 if (suggestedWords.mIsPunctuationSuggestions) { 298 layoutPunctuationSuggestions(suggestedWords, stripView); 299 return; 300 } 301 302 final int countInStrip = mSuggestionsCountInStrip; 303 setupWordViewsTextAndColor(suggestedWords, countInStrip); 304 final TextView centerWordView = mWordViews.get(mCenterPositionInStrip); 305 final int availableStripWidth = placerView.getWidth() 306 - placerView.getPaddingRight() - placerView.getPaddingLeft(); 307 final int centerWidth = getSuggestionWidth(mCenterPositionInStrip, availableStripWidth); 308 if (getTextScaleX(centerWordView.getText(), centerWidth, centerWordView.getPaint()) 309 < MIN_TEXT_XSCALE) { 310 // Layout only the most relevant suggested word at the center of the suggestion strip 311 // by consolidating all slots in the strip. 312 mMoreSuggestionsAvailable = (suggestedWords.size() > 1); 313 layoutWord(mCenterPositionInStrip, availableStripWidth - mPadding); 314 stripView.addView(centerWordView); 315 setLayoutWeight(centerWordView, 1.0f, ViewGroup.LayoutParams.MATCH_PARENT); 316 if (SuggestionStripView.DBG) { 317 layoutDebugInfo(mCenterPositionInStrip, placerView, availableStripWidth); 318 } 319 return; 320 } 321 322 mMoreSuggestionsAvailable = (suggestedWords.size() > countInStrip); 323 int x = 0; 324 for (int positionInStrip = 0; positionInStrip < countInStrip; positionInStrip++) { 325 if (positionInStrip != 0) { 326 final View divider = mDividerViews.get(positionInStrip); 327 // Add divider if this isn't the left most suggestion in suggestions strip. 328 addDivider(stripView, divider); 329 x += divider.getMeasuredWidth(); 330 } 331 332 final int width = getSuggestionWidth(positionInStrip, availableStripWidth); 333 final TextView wordView = layoutWord(positionInStrip, width); 334 stripView.addView(wordView); 335 setLayoutWeight(wordView, getSuggestionWeight(positionInStrip), 336 ViewGroup.LayoutParams.MATCH_PARENT); 337 x += wordView.getMeasuredWidth(); 338 339 if (SuggestionStripView.DBG) { 340 layoutDebugInfo(positionInStrip, placerView, x); 341 } 342 } 343 } 344 345 /** 346 * Format appropriately the suggested word in {@link #mWordViews} specified by 347 * <code>positionInStrip</code>. When the suggested word doesn't exist, the corresponding 348 * {@link TextView} will be disabled and never respond to user interaction. The suggested word 349 * may be shrunk or ellipsized to fit in the specified width. 350 * 351 * The <code>positionInStrip</code> argument is the index in the suggestion strip. The indices 352 * increase towards the right for LTR scripts and the left for RTL scripts, starting with 0. 353 * The position of the most important suggestion is in {@link #mCenterPositionInStrip}. This 354 * usually doesn't match the index in <code>suggedtedWords</code> -- see 355 * {@link #getPositionInSuggestionStrip(int,SuggestedWords)}. 356 * 357 * @param positionInStrip the position in the suggestion strip. 358 * @param width the maximum width for layout in pixels. 359 * @return the {@link TextView} containing the suggested word appropriately formatted. 360 */ 361 private TextView layoutWord(final int positionInStrip, final int width) { 362 final TextView wordView = mWordViews.get(positionInStrip); 363 final CharSequence word = wordView.getText(); 364 if (positionInStrip == mCenterPositionInStrip && mMoreSuggestionsAvailable) { 365 // TODO: This "more suggestions hint" should have a nicely designed icon. 366 wordView.setCompoundDrawablesWithIntrinsicBounds( 367 null, null, null, mMoreSuggestionsHint); 368 // HACK: Align with other TextViews that have no compound drawables. 369 wordView.setCompoundDrawablePadding(-mMoreSuggestionsHint.getIntrinsicHeight()); 370 } else { 371 wordView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); 372 } 373 374 // Disable this suggestion if the suggestion is null or empty. 375 wordView.setEnabled(!TextUtils.isEmpty(word)); 376 final CharSequence text = getEllipsizedText(word, width, wordView.getPaint()); 377 final float scaleX = getTextScaleX(word, width, wordView.getPaint()); 378 wordView.setText(text); // TextView.setText() resets text scale x to 1.0. 379 wordView.setTextScaleX(Math.max(scaleX, MIN_TEXT_XSCALE)); 380 return wordView; 381 } 382 383 private void layoutDebugInfo(final int positionInStrip, final ViewGroup placerView, 384 final int x) { 385 final TextView debugInfoView = mDebugInfoViews.get(positionInStrip); 386 final CharSequence debugInfo = debugInfoView.getText(); 387 if (debugInfo == null) { 388 return; 389 } 390 placerView.addView(debugInfoView); 391 debugInfoView.measure( 392 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 393 final int infoWidth = debugInfoView.getMeasuredWidth(); 394 final int y = debugInfoView.getMeasuredHeight(); 395 ViewLayoutUtils.placeViewAt( 396 debugInfoView, x - infoWidth, y, infoWidth, debugInfoView.getMeasuredHeight()); 397 } 398 399 private int getSuggestionWidth(final int positionInStrip, final int maxWidth) { 400 final int paddings = mPadding * mSuggestionsCountInStrip; 401 final int dividers = mDividerWidth * (mSuggestionsCountInStrip - 1); 402 final int availableWidth = maxWidth - paddings - dividers; 403 return (int)(availableWidth * getSuggestionWeight(positionInStrip)); 404 } 405 406 private float getSuggestionWeight(final int positionInStrip) { 407 if (positionInStrip == mCenterPositionInStrip) { 408 return mCenterSuggestionWeight; 409 } 410 // TODO: Revisit this for cases of 5 or more suggestions 411 return (1.0f - mCenterSuggestionWeight) / (mSuggestionsCountInStrip - 1); 412 } 413 414 private void setupWordViewsTextAndColor(final SuggestedWords suggestedWords, 415 final int countInStrip) { 416 // Clear all suggestions first 417 for (int positionInStrip = 0; positionInStrip < countInStrip; ++positionInStrip) { 418 mWordViews.get(positionInStrip).setText(null); 419 // Make this inactive for touches in {@link #layoutWord(int,int)}. 420 if (SuggestionStripView.DBG) { 421 mDebugInfoViews.get(positionInStrip).setText(null); 422 } 423 } 424 final int count = Math.min(suggestedWords.size(), countInStrip); 425 for (int indexInSuggestedWords = 0; indexInSuggestedWords < count; 426 indexInSuggestedWords++) { 427 final int positionInStrip = 428 getPositionInSuggestionStrip(indexInSuggestedWords, suggestedWords); 429 final TextView wordView = mWordViews.get(positionInStrip); 430 // {@link TextView#getTag()} is used to get the index in suggestedWords at 431 // {@link SuggestionStripView#onClick(View)}. 432 wordView.setTag(indexInSuggestedWords); 433 wordView.setText(getStyledSuggestedWord(suggestedWords, indexInSuggestedWords)); 434 wordView.setTextColor(getSuggestionTextColor(positionInStrip, suggestedWords)); 435 if (SuggestionStripView.DBG) { 436 mDebugInfoViews.get(positionInStrip).setText( 437 suggestedWords.getDebugString(indexInSuggestedWords)); 438 } 439 } 440 } 441 442 private void layoutPunctuationSuggestions(final SuggestedWords suggestedWords, 443 final ViewGroup stripView) { 444 final int countInStrip = Math.min(suggestedWords.size(), PUNCTUATIONS_IN_STRIP); 445 for (int positionInStrip = 0; positionInStrip < countInStrip; positionInStrip++) { 446 if (positionInStrip != 0) { 447 // Add divider if this isn't the left most suggestion in suggestions strip. 448 addDivider(stripView, mDividerViews.get(positionInStrip)); 449 } 450 451 final TextView wordView = mWordViews.get(positionInStrip); 452 wordView.setEnabled(true); 453 wordView.setTextColor(mColorAutoCorrect); 454 // {@link TextView#getTag()} is used to get the index in suggestedWords at 455 // {@link SuggestionStripView#onClick(View)}. 456 wordView.setTag(positionInStrip); 457 wordView.setText(suggestedWords.getWord(positionInStrip)); 458 wordView.setTextScaleX(1.0f); 459 wordView.setCompoundDrawables(null, null, null, null); 460 stripView.addView(wordView); 461 setLayoutWeight(wordView, 1.0f, mSuggestionsStripHeight); 462 } 463 mMoreSuggestionsAvailable = (suggestedWords.size() > countInStrip); 464 } 465 466 public void layoutAddToDictionaryHint(final String word, final ViewGroup stripView, 467 final int stripWidth, final CharSequence hintText, final OnClickListener listener) { 468 final int width = stripWidth - mDividerWidth - mPadding * 2; 469 470 final TextView wordView = mWordToSaveView; 471 wordView.setTextColor(mColorTypedWord); 472 final int wordWidth = (int)(width * mCenterSuggestionWeight); 473 final CharSequence text = getEllipsizedText(word, wordWidth, wordView.getPaint()); 474 final float wordScaleX = wordView.getTextScaleX(); 475 // {@link TextView#setTag()} is used to hold the word to be added to dictionary. The word 476 // will be extracted at {@link #getAddToDictionaryWord()}. 477 wordView.setTag(word); 478 wordView.setText(text); 479 wordView.setTextScaleX(wordScaleX); 480 stripView.addView(wordView); 481 setLayoutWeight(wordView, mCenterSuggestionWeight, ViewGroup.LayoutParams.MATCH_PARENT); 482 483 stripView.addView(mDividerViews.get(0)); 484 485 final TextView leftArrowView = mLeftwardsArrowView; 486 leftArrowView.setTextColor(mColorAutoCorrect); 487 leftArrowView.setText(LEFTWARDS_ARROW); 488 stripView.addView(leftArrowView); 489 490 final TextView hintView = mHintToSaveView; 491 hintView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL); 492 hintView.setTextColor(mColorAutoCorrect); 493 final int hintWidth = width - wordWidth - leftArrowView.getWidth(); 494 final float hintScaleX = getTextScaleX(hintText, hintWidth, hintView.getPaint()); 495 hintView.setText(hintText); 496 hintView.setTextScaleX(hintScaleX); 497 stripView.addView(hintView); 498 setLayoutWeight( 499 hintView, 1.0f - mCenterSuggestionWeight, ViewGroup.LayoutParams.MATCH_PARENT); 500 501 wordView.setOnClickListener(listener); 502 leftArrowView.setOnClickListener(listener); 503 hintView.setOnClickListener(listener); 504 } 505 506 public String getAddToDictionaryWord() { 507 // String tag is set at 508 // {@link #layoutAddToDictionaryHint(String,ViewGroup,int,CharSequence,OnClickListener}. 509 return (String)mWordToSaveView.getTag(); 510 } 511 512 public boolean isAddToDictionaryShowing(final View v) { 513 return v == mWordToSaveView || v == mHintToSaveView || v == mLeftwardsArrowView; 514 } 515 516 private static void setLayoutWeight(final View v, final float weight, final int height) { 517 final ViewGroup.LayoutParams lp = v.getLayoutParams(); 518 if (lp instanceof LinearLayout.LayoutParams) { 519 final LinearLayout.LayoutParams llp = (LinearLayout.LayoutParams)lp; 520 llp.weight = weight; 521 llp.width = 0; 522 llp.height = height; 523 } 524 } 525 526 private static float getTextScaleX(final CharSequence text, final int maxWidth, 527 final TextPaint paint) { 528 paint.setTextScaleX(1.0f); 529 final int width = getTextWidth(text, paint); 530 if (width <= maxWidth) { 531 return 1.0f; 532 } 533 return maxWidth / (float)width; 534 } 535 536 private static CharSequence getEllipsizedText(final CharSequence text, final int maxWidth, 537 final TextPaint paint) { 538 if (text == null) { 539 return null; 540 } 541 final float scaleX = getTextScaleX(text, maxWidth, paint); 542 if (scaleX >= MIN_TEXT_XSCALE) { 543 paint.setTextScaleX(scaleX); 544 return text; 545 } 546 547 // Note that TextUtils.ellipsize() use text-x-scale as 1.0 if ellipsize is needed. To 548 // get squeezed and ellipsized text, passes enlarged width (maxWidth / MIN_TEXT_XSCALE). 549 final float upscaledWidth = maxWidth / MIN_TEXT_XSCALE; 550 CharSequence ellipsized = TextUtils.ellipsize( 551 text, paint, upscaledWidth, TextUtils.TruncateAt.MIDDLE); 552 // For an unknown reason, ellipsized seems to return a text that does indeed fit inside the 553 // passed width according to paint.measureText, but not according to paint.getTextWidths. 554 // But when rendered, the text seems to actually take up as many pixels as returned by 555 // paint.getTextWidths, hence problem. 556 // To save this case, we compare the measured size of the new text, and if it's too much, 557 // try it again removing the difference. This may still give a text too long by one or 558 // two pixels so we take an additional 2 pixels cushion and call it a day. 559 // TODO: figure out why getTextWidths and measureText don't agree with each other, and 560 // remove the following code. 561 final float ellipsizedTextWidth = getTextWidth(ellipsized, paint); 562 if (upscaledWidth <= ellipsizedTextWidth) { 563 ellipsized = TextUtils.ellipsize( 564 text, paint, upscaledWidth - (ellipsizedTextWidth - upscaledWidth) - 2, 565 TextUtils.TruncateAt.MIDDLE); 566 } 567 paint.setTextScaleX(MIN_TEXT_XSCALE); 568 return ellipsized; 569 } 570 571 private static int getTextWidth(final CharSequence text, final TextPaint paint) { 572 if (TextUtils.isEmpty(text)) { 573 return 0; 574 } 575 final Typeface savedTypeface = paint.getTypeface(); 576 paint.setTypeface(getTextTypeface(text)); 577 final int len = text.length(); 578 final float[] widths = new float[len]; 579 final int count = paint.getTextWidths(text, 0, len, widths); 580 int width = 0; 581 for (int i = 0; i < count; i++) { 582 width += Math.round(widths[i] + 0.5f); 583 } 584 paint.setTypeface(savedTypeface); 585 return width; 586 } 587 588 private static Typeface getTextTypeface(final CharSequence text) { 589 if (!(text instanceof SpannableString)) { 590 return Typeface.DEFAULT; 591 } 592 593 final SpannableString ss = (SpannableString)text; 594 final StyleSpan[] styles = ss.getSpans(0, text.length(), StyleSpan.class); 595 if (styles.length == 0) { 596 return Typeface.DEFAULT; 597 } 598 599 if (styles[0].getStyle() == Typeface.BOLD) { 600 return Typeface.DEFAULT_BOLD; 601 } 602 // TODO: BOLD_ITALIC, ITALIC case? 603 return Typeface.DEFAULT; 604 } 605 } 606