TextView.java revision 4833ca2903e89eab93b353f00a1e4904a73d79bb
1/* 2 * Copyright (C) 2006 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 17package android.widget; 18 19import android.R; 20import android.content.ClipData; 21import android.content.ClipboardManager; 22import android.content.Context; 23import android.content.res.ColorStateList; 24import android.content.res.CompatibilityInfo; 25import android.content.res.Resources; 26import android.content.res.TypedArray; 27import android.content.res.XmlResourceParser; 28import android.graphics.Canvas; 29import android.graphics.Paint; 30import android.graphics.Path; 31import android.graphics.Rect; 32import android.graphics.RectF; 33import android.graphics.Typeface; 34import android.graphics.drawable.Drawable; 35import android.inputmethodservice.ExtractEditText; 36import android.os.Bundle; 37import android.os.Handler; 38import android.os.Message; 39import android.os.Parcel; 40import android.os.Parcelable; 41import android.os.SystemClock; 42import android.text.BoringLayout; 43import android.text.DynamicLayout; 44import android.text.Editable; 45import android.text.GetChars; 46import android.text.GraphicsOperations; 47import android.text.InputFilter; 48import android.text.InputType; 49import android.text.Layout; 50import android.text.ParcelableSpan; 51import android.text.Selection; 52import android.text.SpanWatcher; 53import android.text.Spannable; 54import android.text.SpannableString; 55import android.text.Spanned; 56import android.text.SpannedString; 57import android.text.StaticLayout; 58import android.text.TextDirectionHeuristic; 59import android.text.TextDirectionHeuristics; 60import android.text.TextPaint; 61import android.text.TextUtils; 62import android.text.TextUtils.TruncateAt; 63import android.text.TextWatcher; 64import android.text.method.AllCapsTransformationMethod; 65import android.text.method.ArrowKeyMovementMethod; 66import android.text.method.DateKeyListener; 67import android.text.method.DateTimeKeyListener; 68import android.text.method.DialerKeyListener; 69import android.text.method.DigitsKeyListener; 70import android.text.method.KeyListener; 71import android.text.method.LinkMovementMethod; 72import android.text.method.MetaKeyKeyListener; 73import android.text.method.MovementMethod; 74import android.text.method.PasswordTransformationMethod; 75import android.text.method.SingleLineTransformationMethod; 76import android.text.method.TextKeyListener; 77import android.text.method.TimeKeyListener; 78import android.text.method.TransformationMethod; 79import android.text.method.TransformationMethod2; 80import android.text.method.WordIterator; 81import android.text.style.CharacterStyle; 82import android.text.style.ClickableSpan; 83import android.text.style.ParagraphStyle; 84import android.text.style.SpellCheckSpan; 85import android.text.style.SuggestionSpan; 86import android.text.style.URLSpan; 87import android.text.style.UpdateAppearance; 88import android.text.util.Linkify; 89import android.util.AttributeSet; 90import android.util.FloatMath; 91import android.util.Log; 92import android.util.TypedValue; 93import android.view.AccessibilityIterators.TextSegmentIterator; 94import android.view.ActionMode; 95import android.view.DragEvent; 96import android.view.Gravity; 97import android.view.HapticFeedbackConstants; 98import android.view.KeyCharacterMap; 99import android.view.KeyEvent; 100import android.view.Menu; 101import android.view.MenuItem; 102import android.view.MotionEvent; 103import android.view.View; 104import android.view.ViewConfiguration; 105import android.view.ViewDebug; 106import android.view.ViewGroup.LayoutParams; 107import android.view.ViewRootImpl; 108import android.view.ViewTreeObserver; 109import android.view.accessibility.AccessibilityEvent; 110import android.view.accessibility.AccessibilityManager; 111import android.view.accessibility.AccessibilityNodeInfo; 112import android.view.animation.AnimationUtils; 113import android.view.inputmethod.BaseInputConnection; 114import android.view.inputmethod.CompletionInfo; 115import android.view.inputmethod.CorrectionInfo; 116import android.view.inputmethod.EditorInfo; 117import android.view.inputmethod.ExtractedText; 118import android.view.inputmethod.ExtractedTextRequest; 119import android.view.inputmethod.InputConnection; 120import android.view.inputmethod.InputMethodManager; 121import android.view.textservice.SpellCheckerSubtype; 122import android.view.textservice.TextServicesManager; 123import android.widget.RemoteViews.RemoteView; 124 125import com.android.internal.util.FastMath; 126import com.android.internal.widget.EditableInputConnection; 127 128import org.xmlpull.v1.XmlPullParserException; 129 130import java.io.IOException; 131import java.lang.ref.WeakReference; 132import java.util.ArrayList; 133import java.util.Locale; 134 135/** 136 * Displays text to the user and optionally allows them to edit it. A TextView 137 * is a complete text editor, however the basic class is configured to not 138 * allow editing; see {@link EditText} for a subclass that configures the text 139 * view for editing. 140 * 141 * <p> 142 * <b>XML attributes</b> 143 * <p> 144 * See {@link android.R.styleable#TextView TextView Attributes}, 145 * {@link android.R.styleable#View View Attributes} 146 * 147 * @attr ref android.R.styleable#TextView_text 148 * @attr ref android.R.styleable#TextView_bufferType 149 * @attr ref android.R.styleable#TextView_hint 150 * @attr ref android.R.styleable#TextView_textColor 151 * @attr ref android.R.styleable#TextView_textColorHighlight 152 * @attr ref android.R.styleable#TextView_textColorHint 153 * @attr ref android.R.styleable#TextView_textAppearance 154 * @attr ref android.R.styleable#TextView_textColorLink 155 * @attr ref android.R.styleable#TextView_textSize 156 * @attr ref android.R.styleable#TextView_textScaleX 157 * @attr ref android.R.styleable#TextView_typeface 158 * @attr ref android.R.styleable#TextView_textStyle 159 * @attr ref android.R.styleable#TextView_cursorVisible 160 * @attr ref android.R.styleable#TextView_maxLines 161 * @attr ref android.R.styleable#TextView_maxHeight 162 * @attr ref android.R.styleable#TextView_lines 163 * @attr ref android.R.styleable#TextView_height 164 * @attr ref android.R.styleable#TextView_minLines 165 * @attr ref android.R.styleable#TextView_minHeight 166 * @attr ref android.R.styleable#TextView_maxEms 167 * @attr ref android.R.styleable#TextView_maxWidth 168 * @attr ref android.R.styleable#TextView_ems 169 * @attr ref android.R.styleable#TextView_width 170 * @attr ref android.R.styleable#TextView_minEms 171 * @attr ref android.R.styleable#TextView_minWidth 172 * @attr ref android.R.styleable#TextView_gravity 173 * @attr ref android.R.styleable#TextView_scrollHorizontally 174 * @attr ref android.R.styleable#TextView_password 175 * @attr ref android.R.styleable#TextView_singleLine 176 * @attr ref android.R.styleable#TextView_selectAllOnFocus 177 * @attr ref android.R.styleable#TextView_includeFontPadding 178 * @attr ref android.R.styleable#TextView_maxLength 179 * @attr ref android.R.styleable#TextView_shadowColor 180 * @attr ref android.R.styleable#TextView_shadowDx 181 * @attr ref android.R.styleable#TextView_shadowDy 182 * @attr ref android.R.styleable#TextView_shadowRadius 183 * @attr ref android.R.styleable#TextView_autoLink 184 * @attr ref android.R.styleable#TextView_linksClickable 185 * @attr ref android.R.styleable#TextView_numeric 186 * @attr ref android.R.styleable#TextView_digits 187 * @attr ref android.R.styleable#TextView_phoneNumber 188 * @attr ref android.R.styleable#TextView_inputMethod 189 * @attr ref android.R.styleable#TextView_capitalize 190 * @attr ref android.R.styleable#TextView_autoText 191 * @attr ref android.R.styleable#TextView_editable 192 * @attr ref android.R.styleable#TextView_freezesText 193 * @attr ref android.R.styleable#TextView_ellipsize 194 * @attr ref android.R.styleable#TextView_drawableTop 195 * @attr ref android.R.styleable#TextView_drawableBottom 196 * @attr ref android.R.styleable#TextView_drawableRight 197 * @attr ref android.R.styleable#TextView_drawableLeft 198 * @attr ref android.R.styleable#TextView_drawableStart 199 * @attr ref android.R.styleable#TextView_drawableEnd 200 * @attr ref android.R.styleable#TextView_drawablePadding 201 * @attr ref android.R.styleable#TextView_lineSpacingExtra 202 * @attr ref android.R.styleable#TextView_lineSpacingMultiplier 203 * @attr ref android.R.styleable#TextView_marqueeRepeatLimit 204 * @attr ref android.R.styleable#TextView_inputType 205 * @attr ref android.R.styleable#TextView_imeOptions 206 * @attr ref android.R.styleable#TextView_privateImeOptions 207 * @attr ref android.R.styleable#TextView_imeActionLabel 208 * @attr ref android.R.styleable#TextView_imeActionId 209 * @attr ref android.R.styleable#TextView_editorExtras 210 */ 211@RemoteView 212public class TextView extends View implements ViewTreeObserver.OnPreDrawListener { 213 static final String LOG_TAG = "TextView"; 214 static final boolean DEBUG_EXTRACT = false; 215 216 // Enum for the "typeface" XML parameter. 217 // TODO: How can we get this from the XML instead of hardcoding it here? 218 private static final int SANS = 1; 219 private static final int SERIF = 2; 220 private static final int MONOSPACE = 3; 221 222 // Bitfield for the "numeric" XML parameter. 223 // TODO: How can we get this from the XML instead of hardcoding it here? 224 private static final int SIGNED = 2; 225 private static final int DECIMAL = 4; 226 227 /** 228 * Draw marquee text with fading edges as usual 229 */ 230 private static final int MARQUEE_FADE_NORMAL = 0; 231 232 /** 233 * Draw marquee text as ellipsize end while inactive instead of with the fade. 234 * (Useful for devices where the fade can be expensive if overdone) 235 */ 236 private static final int MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS = 1; 237 238 /** 239 * Draw marquee text with fading edges because it is currently active/animating. 240 */ 241 private static final int MARQUEE_FADE_SWITCH_SHOW_FADE = 2; 242 243 private static final int LINES = 1; 244 private static final int EMS = LINES; 245 private static final int PIXELS = 2; 246 247 private static final RectF TEMP_RECTF = new RectF(); 248 249 // XXX should be much larger 250 private static final int VERY_WIDE = 1024*1024; 251 private static final int ANIMATED_SCROLL_GAP = 250; 252 253 private static final InputFilter[] NO_FILTERS = new InputFilter[0]; 254 private static final Spanned EMPTY_SPANNED = new SpannedString(""); 255 256 private static final int CHANGE_WATCHER_PRIORITY = 100; 257 258 // New state used to change background based on whether this TextView is multiline. 259 private static final int[] MULTILINE_STATE_SET = { R.attr.state_multiline }; 260 261 // System wide time for last cut or copy action. 262 static long LAST_CUT_OR_COPY_TIME; 263 264 private ColorStateList mTextColor; 265 private ColorStateList mHintTextColor; 266 private ColorStateList mLinkTextColor; 267 private int mCurTextColor; 268 private int mCurHintTextColor; 269 private boolean mFreezesText; 270 private boolean mTemporaryDetach; 271 private boolean mDispatchTemporaryDetach; 272 273 private Editable.Factory mEditableFactory = Editable.Factory.getInstance(); 274 private Spannable.Factory mSpannableFactory = Spannable.Factory.getInstance(); 275 276 private float mShadowRadius, mShadowDx, mShadowDy; 277 278 private boolean mPreDrawRegistered; 279 280 private TextUtils.TruncateAt mEllipsize; 281 282 static class Drawables { 283 final Rect mCompoundRect = new Rect(); 284 Drawable mDrawableTop, mDrawableBottom, mDrawableLeft, mDrawableRight, 285 mDrawableStart, mDrawableEnd; 286 int mDrawableSizeTop, mDrawableSizeBottom, mDrawableSizeLeft, mDrawableSizeRight, 287 mDrawableSizeStart, mDrawableSizeEnd; 288 int mDrawableWidthTop, mDrawableWidthBottom, mDrawableHeightLeft, mDrawableHeightRight, 289 mDrawableHeightStart, mDrawableHeightEnd; 290 int mDrawablePadding; 291 } 292 Drawables mDrawables; 293 294 private CharWrapper mCharWrapper; 295 296 private Marquee mMarquee; 297 private boolean mRestartMarquee; 298 299 private int mMarqueeRepeatLimit = 3; 300 301 // The alignment to pass to Layout, or null if not resolved. 302 private Layout.Alignment mLayoutAlignment; 303 304 private boolean mResolvedDrawables; 305 306 /** 307 * On some devices the fading edges add a performance penalty if used 308 * extensively in the same layout. This mode indicates how the marquee 309 * is currently being shown, if applicable. (mEllipsize will == MARQUEE) 310 */ 311 private int mMarqueeFadeMode = MARQUEE_FADE_NORMAL; 312 313 /** 314 * When mMarqueeFadeMode is not MARQUEE_FADE_NORMAL, this stores 315 * the layout that should be used when the mode switches. 316 */ 317 private Layout mSavedMarqueeModeLayout; 318 319 @ViewDebug.ExportedProperty(category = "text") 320 private CharSequence mText; 321 private CharSequence mTransformed; 322 private BufferType mBufferType = BufferType.NORMAL; 323 324 private CharSequence mHint; 325 private Layout mHintLayout; 326 327 private MovementMethod mMovement; 328 329 private TransformationMethod mTransformation; 330 private boolean mAllowTransformationLengthChange; 331 private ChangeWatcher mChangeWatcher; 332 333 private ArrayList<TextWatcher> mListeners; 334 335 // display attributes 336 private final TextPaint mTextPaint; 337 private boolean mUserSetTextScaleX; 338 private Layout mLayout; 339 340 private int mGravity = Gravity.TOP | Gravity.START; 341 private boolean mHorizontallyScrolling; 342 343 private int mAutoLinkMask; 344 private boolean mLinksClickable = true; 345 346 private float mSpacingMult = 1.0f; 347 private float mSpacingAdd = 0.0f; 348 349 private int mMaximum = Integer.MAX_VALUE; 350 private int mMaxMode = LINES; 351 private int mMinimum = 0; 352 private int mMinMode = LINES; 353 354 private int mOldMaximum = mMaximum; 355 private int mOldMaxMode = mMaxMode; 356 357 private int mMaxWidth = Integer.MAX_VALUE; 358 private int mMaxWidthMode = PIXELS; 359 private int mMinWidth = 0; 360 private int mMinWidthMode = PIXELS; 361 362 private boolean mSingleLine; 363 private int mDesiredHeightAtMeasure = -1; 364 private boolean mIncludePad = true; 365 366 // tmp primitives, so we don't alloc them on each draw 367 private Rect mTempRect; 368 private long mLastScroll; 369 private Scroller mScroller; 370 371 private BoringLayout.Metrics mBoring, mHintBoring; 372 private BoringLayout mSavedLayout, mSavedHintLayout; 373 374 private TextDirectionHeuristic mTextDir; 375 376 private InputFilter[] mFilters = NO_FILTERS; 377 378 // It is possible to have a selection even when mEditor is null (programmatically set, like when 379 // a link is pressed). These highlight-related fields do not go in mEditor. 380 int mHighlightColor = 0x6633B5E5; 381 private Path mHighlightPath; 382 private final Paint mHighlightPaint; 383 private boolean mHighlightPathBogus = true; 384 385 // Although these fields are specific to editable text, they are not added to Editor because 386 // they are defined by the TextView's style and are theme-dependent. 387 int mCursorDrawableRes; 388 // These four fields, could be moved to Editor, since we know their default values and we 389 // could condition the creation of the Editor to a non standard value. This is however 390 // brittle since the hardcoded values here (such as 391 // com.android.internal.R.drawable.text_select_handle_left) would have to be updated if the 392 // default style is modified. 393 int mTextSelectHandleLeftRes; 394 int mTextSelectHandleRightRes; 395 int mTextSelectHandleRes; 396 int mTextEditSuggestionItemLayout; 397 398 /** 399 * EditText specific data, created on demand when one of the Editor fields is used. 400 * See {@link #createEditorIfNeeded(String)}. 401 */ 402 private Editor mEditor; 403 404 /* 405 * Kick-start the font cache for the zygote process (to pay the cost of 406 * initializing freetype for our default font only once). 407 */ 408 static { 409 Paint p = new Paint(); 410 p.setAntiAlias(true); 411 // We don't care about the result, just the side-effect of measuring. 412 p.measureText("H"); 413 } 414 415 /** 416 * Interface definition for a callback to be invoked when an action is 417 * performed on the editor. 418 */ 419 public interface OnEditorActionListener { 420 /** 421 * Called when an action is being performed. 422 * 423 * @param v The view that was clicked. 424 * @param actionId Identifier of the action. This will be either the 425 * identifier you supplied, or {@link EditorInfo#IME_NULL 426 * EditorInfo.IME_NULL} if being called due to the enter key 427 * being pressed. 428 * @param event If triggered by an enter key, this is the event; 429 * otherwise, this is null. 430 * @return Return true if you have consumed the action, else false. 431 */ 432 boolean onEditorAction(TextView v, int actionId, KeyEvent event); 433 } 434 435 public TextView(Context context) { 436 this(context, null); 437 } 438 439 public TextView(Context context, AttributeSet attrs) { 440 this(context, attrs, com.android.internal.R.attr.textViewStyle); 441 } 442 443 @SuppressWarnings("deprecation") 444 public TextView(Context context, AttributeSet attrs, int defStyle) { 445 super(context, attrs, defStyle); 446 mText = ""; 447 448 final Resources res = getResources(); 449 final CompatibilityInfo compat = res.getCompatibilityInfo(); 450 451 mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); 452 mTextPaint.density = res.getDisplayMetrics().density; 453 mTextPaint.setCompatibilityScaling(compat.applicationScale); 454 455 mHighlightPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 456 mHighlightPaint.setCompatibilityScaling(compat.applicationScale); 457 458 mMovement = getDefaultMovementMethod(); 459 460 mTransformation = null; 461 462 int textColorHighlight = 0; 463 ColorStateList textColor = null; 464 ColorStateList textColorHint = null; 465 ColorStateList textColorLink = null; 466 int textSize = 15; 467 int typefaceIndex = -1; 468 int styleIndex = -1; 469 boolean allCaps = false; 470 471 final Resources.Theme theme = context.getTheme(); 472 473 /* 474 * Look the appearance up without checking first if it exists because 475 * almost every TextView has one and it greatly simplifies the logic 476 * to be able to parse the appearance first and then let specific tags 477 * for this View override it. 478 */ 479 TypedArray a = theme.obtainStyledAttributes( 480 attrs, com.android.internal.R.styleable.TextViewAppearance, defStyle, 0); 481 TypedArray appearance = null; 482 int ap = a.getResourceId( 483 com.android.internal.R.styleable.TextViewAppearance_textAppearance, -1); 484 a.recycle(); 485 if (ap != -1) { 486 appearance = theme.obtainStyledAttributes( 487 ap, com.android.internal.R.styleable.TextAppearance); 488 } 489 if (appearance != null) { 490 int n = appearance.getIndexCount(); 491 for (int i = 0; i < n; i++) { 492 int attr = appearance.getIndex(i); 493 494 switch (attr) { 495 case com.android.internal.R.styleable.TextAppearance_textColorHighlight: 496 textColorHighlight = appearance.getColor(attr, textColorHighlight); 497 break; 498 499 case com.android.internal.R.styleable.TextAppearance_textColor: 500 textColor = appearance.getColorStateList(attr); 501 break; 502 503 case com.android.internal.R.styleable.TextAppearance_textColorHint: 504 textColorHint = appearance.getColorStateList(attr); 505 break; 506 507 case com.android.internal.R.styleable.TextAppearance_textColorLink: 508 textColorLink = appearance.getColorStateList(attr); 509 break; 510 511 case com.android.internal.R.styleable.TextAppearance_textSize: 512 textSize = appearance.getDimensionPixelSize(attr, textSize); 513 break; 514 515 case com.android.internal.R.styleable.TextAppearance_typeface: 516 typefaceIndex = appearance.getInt(attr, -1); 517 break; 518 519 case com.android.internal.R.styleable.TextAppearance_textStyle: 520 styleIndex = appearance.getInt(attr, -1); 521 break; 522 523 case com.android.internal.R.styleable.TextAppearance_textAllCaps: 524 allCaps = appearance.getBoolean(attr, false); 525 break; 526 } 527 } 528 529 appearance.recycle(); 530 } 531 532 boolean editable = getDefaultEditable(); 533 CharSequence inputMethod = null; 534 int numeric = 0; 535 CharSequence digits = null; 536 boolean phone = false; 537 boolean autotext = false; 538 int autocap = -1; 539 int buffertype = 0; 540 boolean selectallonfocus = false; 541 Drawable drawableLeft = null, drawableTop = null, drawableRight = null, 542 drawableBottom = null, drawableStart = null, drawableEnd = null; 543 int drawablePadding = 0; 544 int ellipsize = -1; 545 boolean singleLine = false; 546 int maxlength = -1; 547 CharSequence text = ""; 548 CharSequence hint = null; 549 int shadowcolor = 0; 550 float dx = 0, dy = 0, r = 0; 551 boolean password = false; 552 int inputType = EditorInfo.TYPE_NULL; 553 554 a = theme.obtainStyledAttributes( 555 attrs, com.android.internal.R.styleable.TextView, defStyle, 0); 556 557 int n = a.getIndexCount(); 558 for (int i = 0; i < n; i++) { 559 int attr = a.getIndex(i); 560 561 switch (attr) { 562 case com.android.internal.R.styleable.TextView_editable: 563 editable = a.getBoolean(attr, editable); 564 break; 565 566 case com.android.internal.R.styleable.TextView_inputMethod: 567 inputMethod = a.getText(attr); 568 break; 569 570 case com.android.internal.R.styleable.TextView_numeric: 571 numeric = a.getInt(attr, numeric); 572 break; 573 574 case com.android.internal.R.styleable.TextView_digits: 575 digits = a.getText(attr); 576 break; 577 578 case com.android.internal.R.styleable.TextView_phoneNumber: 579 phone = a.getBoolean(attr, phone); 580 break; 581 582 case com.android.internal.R.styleable.TextView_autoText: 583 autotext = a.getBoolean(attr, autotext); 584 break; 585 586 case com.android.internal.R.styleable.TextView_capitalize: 587 autocap = a.getInt(attr, autocap); 588 break; 589 590 case com.android.internal.R.styleable.TextView_bufferType: 591 buffertype = a.getInt(attr, buffertype); 592 break; 593 594 case com.android.internal.R.styleable.TextView_selectAllOnFocus: 595 selectallonfocus = a.getBoolean(attr, selectallonfocus); 596 break; 597 598 case com.android.internal.R.styleable.TextView_autoLink: 599 mAutoLinkMask = a.getInt(attr, 0); 600 break; 601 602 case com.android.internal.R.styleable.TextView_linksClickable: 603 mLinksClickable = a.getBoolean(attr, true); 604 break; 605 606 case com.android.internal.R.styleable.TextView_drawableLeft: 607 drawableLeft = a.getDrawable(attr); 608 break; 609 610 case com.android.internal.R.styleable.TextView_drawableTop: 611 drawableTop = a.getDrawable(attr); 612 break; 613 614 case com.android.internal.R.styleable.TextView_drawableRight: 615 drawableRight = a.getDrawable(attr); 616 break; 617 618 case com.android.internal.R.styleable.TextView_drawableBottom: 619 drawableBottom = a.getDrawable(attr); 620 break; 621 622 case com.android.internal.R.styleable.TextView_drawableStart: 623 drawableStart = a.getDrawable(attr); 624 break; 625 626 case com.android.internal.R.styleable.TextView_drawableEnd: 627 drawableEnd = a.getDrawable(attr); 628 break; 629 630 case com.android.internal.R.styleable.TextView_drawablePadding: 631 drawablePadding = a.getDimensionPixelSize(attr, drawablePadding); 632 break; 633 634 case com.android.internal.R.styleable.TextView_maxLines: 635 setMaxLines(a.getInt(attr, -1)); 636 break; 637 638 case com.android.internal.R.styleable.TextView_maxHeight: 639 setMaxHeight(a.getDimensionPixelSize(attr, -1)); 640 break; 641 642 case com.android.internal.R.styleable.TextView_lines: 643 setLines(a.getInt(attr, -1)); 644 break; 645 646 case com.android.internal.R.styleable.TextView_height: 647 setHeight(a.getDimensionPixelSize(attr, -1)); 648 break; 649 650 case com.android.internal.R.styleable.TextView_minLines: 651 setMinLines(a.getInt(attr, -1)); 652 break; 653 654 case com.android.internal.R.styleable.TextView_minHeight: 655 setMinHeight(a.getDimensionPixelSize(attr, -1)); 656 break; 657 658 case com.android.internal.R.styleable.TextView_maxEms: 659 setMaxEms(a.getInt(attr, -1)); 660 break; 661 662 case com.android.internal.R.styleable.TextView_maxWidth: 663 setMaxWidth(a.getDimensionPixelSize(attr, -1)); 664 break; 665 666 case com.android.internal.R.styleable.TextView_ems: 667 setEms(a.getInt(attr, -1)); 668 break; 669 670 case com.android.internal.R.styleable.TextView_width: 671 setWidth(a.getDimensionPixelSize(attr, -1)); 672 break; 673 674 case com.android.internal.R.styleable.TextView_minEms: 675 setMinEms(a.getInt(attr, -1)); 676 break; 677 678 case com.android.internal.R.styleable.TextView_minWidth: 679 setMinWidth(a.getDimensionPixelSize(attr, -1)); 680 break; 681 682 case com.android.internal.R.styleable.TextView_gravity: 683 setGravity(a.getInt(attr, -1)); 684 break; 685 686 case com.android.internal.R.styleable.TextView_hint: 687 hint = a.getText(attr); 688 break; 689 690 case com.android.internal.R.styleable.TextView_text: 691 text = a.getText(attr); 692 break; 693 694 case com.android.internal.R.styleable.TextView_scrollHorizontally: 695 if (a.getBoolean(attr, false)) { 696 setHorizontallyScrolling(true); 697 } 698 break; 699 700 case com.android.internal.R.styleable.TextView_singleLine: 701 singleLine = a.getBoolean(attr, singleLine); 702 break; 703 704 case com.android.internal.R.styleable.TextView_ellipsize: 705 ellipsize = a.getInt(attr, ellipsize); 706 break; 707 708 case com.android.internal.R.styleable.TextView_marqueeRepeatLimit: 709 setMarqueeRepeatLimit(a.getInt(attr, mMarqueeRepeatLimit)); 710 break; 711 712 case com.android.internal.R.styleable.TextView_includeFontPadding: 713 if (!a.getBoolean(attr, true)) { 714 setIncludeFontPadding(false); 715 } 716 break; 717 718 case com.android.internal.R.styleable.TextView_cursorVisible: 719 if (!a.getBoolean(attr, true)) { 720 setCursorVisible(false); 721 } 722 break; 723 724 case com.android.internal.R.styleable.TextView_maxLength: 725 maxlength = a.getInt(attr, -1); 726 break; 727 728 case com.android.internal.R.styleable.TextView_textScaleX: 729 setTextScaleX(a.getFloat(attr, 1.0f)); 730 break; 731 732 case com.android.internal.R.styleable.TextView_freezesText: 733 mFreezesText = a.getBoolean(attr, false); 734 break; 735 736 case com.android.internal.R.styleable.TextView_shadowColor: 737 shadowcolor = a.getInt(attr, 0); 738 break; 739 740 case com.android.internal.R.styleable.TextView_shadowDx: 741 dx = a.getFloat(attr, 0); 742 break; 743 744 case com.android.internal.R.styleable.TextView_shadowDy: 745 dy = a.getFloat(attr, 0); 746 break; 747 748 case com.android.internal.R.styleable.TextView_shadowRadius: 749 r = a.getFloat(attr, 0); 750 break; 751 752 case com.android.internal.R.styleable.TextView_enabled: 753 setEnabled(a.getBoolean(attr, isEnabled())); 754 break; 755 756 case com.android.internal.R.styleable.TextView_textColorHighlight: 757 textColorHighlight = a.getColor(attr, textColorHighlight); 758 break; 759 760 case com.android.internal.R.styleable.TextView_textColor: 761 textColor = a.getColorStateList(attr); 762 break; 763 764 case com.android.internal.R.styleable.TextView_textColorHint: 765 textColorHint = a.getColorStateList(attr); 766 break; 767 768 case com.android.internal.R.styleable.TextView_textColorLink: 769 textColorLink = a.getColorStateList(attr); 770 break; 771 772 case com.android.internal.R.styleable.TextView_textSize: 773 textSize = a.getDimensionPixelSize(attr, textSize); 774 break; 775 776 case com.android.internal.R.styleable.TextView_typeface: 777 typefaceIndex = a.getInt(attr, typefaceIndex); 778 break; 779 780 case com.android.internal.R.styleable.TextView_textStyle: 781 styleIndex = a.getInt(attr, styleIndex); 782 break; 783 784 case com.android.internal.R.styleable.TextView_password: 785 password = a.getBoolean(attr, password); 786 break; 787 788 case com.android.internal.R.styleable.TextView_lineSpacingExtra: 789 mSpacingAdd = a.getDimensionPixelSize(attr, (int) mSpacingAdd); 790 break; 791 792 case com.android.internal.R.styleable.TextView_lineSpacingMultiplier: 793 mSpacingMult = a.getFloat(attr, mSpacingMult); 794 break; 795 796 case com.android.internal.R.styleable.TextView_inputType: 797 inputType = a.getInt(attr, EditorInfo.TYPE_NULL); 798 break; 799 800 case com.android.internal.R.styleable.TextView_imeOptions: 801 createEditorIfNeeded("IME options specified in constructor"); 802 mEditor.createInputContentTypeIfNeeded(); 803 mEditor.mInputContentType.imeOptions = a.getInt(attr, 804 mEditor.mInputContentType.imeOptions); 805 break; 806 807 case com.android.internal.R.styleable.TextView_imeActionLabel: 808 createEditorIfNeeded("IME action label specified in constructor"); 809 mEditor.createInputContentTypeIfNeeded(); 810 mEditor.mInputContentType.imeActionLabel = a.getText(attr); 811 break; 812 813 case com.android.internal.R.styleable.TextView_imeActionId: 814 createEditorIfNeeded("IME action id specified in constructor"); 815 mEditor.createInputContentTypeIfNeeded(); 816 mEditor.mInputContentType.imeActionId = a.getInt(attr, 817 mEditor.mInputContentType.imeActionId); 818 break; 819 820 case com.android.internal.R.styleable.TextView_privateImeOptions: 821 setPrivateImeOptions(a.getString(attr)); 822 break; 823 824 case com.android.internal.R.styleable.TextView_editorExtras: 825 try { 826 setInputExtras(a.getResourceId(attr, 0)); 827 } catch (XmlPullParserException e) { 828 Log.w(LOG_TAG, "Failure reading input extras", e); 829 } catch (IOException e) { 830 Log.w(LOG_TAG, "Failure reading input extras", e); 831 } 832 break; 833 834 case com.android.internal.R.styleable.TextView_textCursorDrawable: 835 mCursorDrawableRes = a.getResourceId(attr, 0); 836 break; 837 838 case com.android.internal.R.styleable.TextView_textSelectHandleLeft: 839 mTextSelectHandleLeftRes = a.getResourceId(attr, 0); 840 break; 841 842 case com.android.internal.R.styleable.TextView_textSelectHandleRight: 843 mTextSelectHandleRightRes = a.getResourceId(attr, 0); 844 break; 845 846 case com.android.internal.R.styleable.TextView_textSelectHandle: 847 mTextSelectHandleRes = a.getResourceId(attr, 0); 848 break; 849 850 case com.android.internal.R.styleable.TextView_textEditSuggestionItemLayout: 851 mTextEditSuggestionItemLayout = a.getResourceId(attr, 0); 852 break; 853 854 case com.android.internal.R.styleable.TextView_textIsSelectable: 855 setTextIsSelectable(a.getBoolean(attr, false)); 856 break; 857 858 case com.android.internal.R.styleable.TextView_textAllCaps: 859 allCaps = a.getBoolean(attr, false); 860 break; 861 } 862 } 863 a.recycle(); 864 865 BufferType bufferType = BufferType.EDITABLE; 866 867 final int variation = 868 inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION); 869 final boolean passwordInputType = variation 870 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD); 871 final boolean webPasswordInputType = variation 872 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD); 873 final boolean numberPasswordInputType = variation 874 == (EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD); 875 876 if (inputMethod != null) { 877 Class<?> c; 878 879 try { 880 c = Class.forName(inputMethod.toString()); 881 } catch (ClassNotFoundException ex) { 882 throw new RuntimeException(ex); 883 } 884 885 try { 886 createEditorIfNeeded("inputMethod in ctor"); 887 mEditor.mKeyListener = (KeyListener) c.newInstance(); 888 } catch (InstantiationException ex) { 889 throw new RuntimeException(ex); 890 } catch (IllegalAccessException ex) { 891 throw new RuntimeException(ex); 892 } 893 try { 894 mEditor.mInputType = inputType != EditorInfo.TYPE_NULL 895 ? inputType 896 : mEditor.mKeyListener.getInputType(); 897 } catch (IncompatibleClassChangeError e) { 898 mEditor.mInputType = EditorInfo.TYPE_CLASS_TEXT; 899 } 900 } else if (digits != null) { 901 createEditorIfNeeded("digits in ctor"); 902 mEditor.mKeyListener = DigitsKeyListener.getInstance(digits.toString()); 903 // If no input type was specified, we will default to generic 904 // text, since we can't tell the IME about the set of digits 905 // that was selected. 906 mEditor.mInputType = inputType != EditorInfo.TYPE_NULL 907 ? inputType : EditorInfo.TYPE_CLASS_TEXT; 908 } else if (inputType != EditorInfo.TYPE_NULL) { 909 setInputType(inputType, true); 910 // If set, the input type overrides what was set using the deprecated singleLine flag. 911 singleLine = !isMultilineInputType(inputType); 912 } else if (phone) { 913 createEditorIfNeeded("dialer in ctor"); 914 mEditor.mKeyListener = DialerKeyListener.getInstance(); 915 mEditor.mInputType = inputType = EditorInfo.TYPE_CLASS_PHONE; 916 } else if (numeric != 0) { 917 createEditorIfNeeded("numeric in ctor"); 918 mEditor.mKeyListener = DigitsKeyListener.getInstance((numeric & SIGNED) != 0, 919 (numeric & DECIMAL) != 0); 920 inputType = EditorInfo.TYPE_CLASS_NUMBER; 921 if ((numeric & SIGNED) != 0) { 922 inputType |= EditorInfo.TYPE_NUMBER_FLAG_SIGNED; 923 } 924 if ((numeric & DECIMAL) != 0) { 925 inputType |= EditorInfo.TYPE_NUMBER_FLAG_DECIMAL; 926 } 927 mEditor.mInputType = inputType; 928 } else if (autotext || autocap != -1) { 929 TextKeyListener.Capitalize cap; 930 931 inputType = EditorInfo.TYPE_CLASS_TEXT; 932 933 switch (autocap) { 934 case 1: 935 cap = TextKeyListener.Capitalize.SENTENCES; 936 inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES; 937 break; 938 939 case 2: 940 cap = TextKeyListener.Capitalize.WORDS; 941 inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS; 942 break; 943 944 case 3: 945 cap = TextKeyListener.Capitalize.CHARACTERS; 946 inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_CHARACTERS; 947 break; 948 949 default: 950 cap = TextKeyListener.Capitalize.NONE; 951 break; 952 } 953 954 createEditorIfNeeded("text input in ctor"); 955 mEditor.mKeyListener = TextKeyListener.getInstance(autotext, cap); 956 mEditor.mInputType = inputType; 957 } else if (isTextSelectable()) { 958 // Prevent text changes from keyboard. 959 if (mEditor != null) { 960 mEditor.mKeyListener = null; 961 mEditor.mInputType = EditorInfo.TYPE_NULL; 962 } 963 bufferType = BufferType.SPANNABLE; 964 // So that selection can be changed using arrow keys and touch is handled. 965 setMovementMethod(ArrowKeyMovementMethod.getInstance()); 966 } else if (editable) { 967 createEditorIfNeeded("editable input in ctor"); 968 mEditor.mKeyListener = TextKeyListener.getInstance(); 969 mEditor.mInputType = EditorInfo.TYPE_CLASS_TEXT; 970 } else { 971 if (mEditor != null) mEditor.mKeyListener = null; 972 973 switch (buffertype) { 974 case 0: 975 bufferType = BufferType.NORMAL; 976 break; 977 case 1: 978 bufferType = BufferType.SPANNABLE; 979 break; 980 case 2: 981 bufferType = BufferType.EDITABLE; 982 break; 983 } 984 } 985 986 if (mEditor != null) mEditor.adjustInputType(password, passwordInputType, 987 webPasswordInputType, numberPasswordInputType); 988 989 if (selectallonfocus) { 990 createEditorIfNeeded("selectallonfocus in constructor"); 991 mEditor.mSelectAllOnFocus = true; 992 993 if (bufferType == BufferType.NORMAL) 994 bufferType = BufferType.SPANNABLE; 995 } 996 997 setCompoundDrawablesWithIntrinsicBounds( 998 drawableLeft, drawableTop, drawableRight, drawableBottom); 999 setRelativeDrawablesIfNeeded(drawableStart, drawableEnd); 1000 setCompoundDrawablePadding(drawablePadding); 1001 1002 // Same as setSingleLine(), but make sure the transformation method and the maximum number 1003 // of lines of height are unchanged for multi-line TextViews. 1004 setInputTypeSingleLine(singleLine); 1005 applySingleLine(singleLine, singleLine, singleLine); 1006 1007 if (singleLine && getKeyListener() == null && ellipsize < 0) { 1008 ellipsize = 3; // END 1009 } 1010 1011 switch (ellipsize) { 1012 case 1: 1013 setEllipsize(TextUtils.TruncateAt.START); 1014 break; 1015 case 2: 1016 setEllipsize(TextUtils.TruncateAt.MIDDLE); 1017 break; 1018 case 3: 1019 setEllipsize(TextUtils.TruncateAt.END); 1020 break; 1021 case 4: 1022 if (ViewConfiguration.get(context).isFadingMarqueeEnabled()) { 1023 setHorizontalFadingEdgeEnabled(true); 1024 mMarqueeFadeMode = MARQUEE_FADE_NORMAL; 1025 } else { 1026 setHorizontalFadingEdgeEnabled(false); 1027 mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS; 1028 } 1029 setEllipsize(TextUtils.TruncateAt.MARQUEE); 1030 break; 1031 } 1032 1033 setTextColor(textColor != null ? textColor : ColorStateList.valueOf(0xFF000000)); 1034 setHintTextColor(textColorHint); 1035 setLinkTextColor(textColorLink); 1036 if (textColorHighlight != 0) { 1037 setHighlightColor(textColorHighlight); 1038 } 1039 setRawTextSize(textSize); 1040 1041 if (allCaps) { 1042 setTransformationMethod(new AllCapsTransformationMethod(getContext())); 1043 } 1044 1045 if (password || passwordInputType || webPasswordInputType || numberPasswordInputType) { 1046 setTransformationMethod(PasswordTransformationMethod.getInstance()); 1047 typefaceIndex = MONOSPACE; 1048 } else if (mEditor != null && 1049 (mEditor.mInputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION)) 1050 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD)) { 1051 typefaceIndex = MONOSPACE; 1052 } 1053 1054 setTypefaceByIndex(typefaceIndex, styleIndex); 1055 1056 if (shadowcolor != 0) { 1057 setShadowLayer(r, dx, dy, shadowcolor); 1058 } 1059 1060 if (maxlength >= 0) { 1061 setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxlength) }); 1062 } else { 1063 setFilters(NO_FILTERS); 1064 } 1065 1066 setText(text, bufferType); 1067 if (hint != null) setHint(hint); 1068 1069 /* 1070 * Views are not normally focusable unless specified to be. 1071 * However, TextViews that have input or movement methods *are* 1072 * focusable by default. 1073 */ 1074 a = context.obtainStyledAttributes(attrs, 1075 com.android.internal.R.styleable.View, 1076 defStyle, 0); 1077 1078 boolean focusable = mMovement != null || getKeyListener() != null; 1079 boolean clickable = focusable; 1080 boolean longClickable = focusable; 1081 1082 n = a.getIndexCount(); 1083 for (int i = 0; i < n; i++) { 1084 int attr = a.getIndex(i); 1085 1086 switch (attr) { 1087 case com.android.internal.R.styleable.View_focusable: 1088 focusable = a.getBoolean(attr, focusable); 1089 break; 1090 1091 case com.android.internal.R.styleable.View_clickable: 1092 clickable = a.getBoolean(attr, clickable); 1093 break; 1094 1095 case com.android.internal.R.styleable.View_longClickable: 1096 longClickable = a.getBoolean(attr, longClickable); 1097 break; 1098 } 1099 } 1100 a.recycle(); 1101 1102 setFocusable(focusable); 1103 setClickable(clickable); 1104 setLongClickable(longClickable); 1105 1106 if (mEditor != null) mEditor.prepareCursorControllers(); 1107 1108 // If not explicitly specified this view is important for accessibility. 1109 if (getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) { 1110 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES); 1111 } 1112 } 1113 1114 private void setTypefaceByIndex(int typefaceIndex, int styleIndex) { 1115 Typeface tf = null; 1116 switch (typefaceIndex) { 1117 case SANS: 1118 tf = Typeface.SANS_SERIF; 1119 break; 1120 1121 case SERIF: 1122 tf = Typeface.SERIF; 1123 break; 1124 1125 case MONOSPACE: 1126 tf = Typeface.MONOSPACE; 1127 break; 1128 } 1129 1130 setTypeface(tf, styleIndex); 1131 } 1132 1133 private void setRelativeDrawablesIfNeeded(Drawable start, Drawable end) { 1134 boolean hasRelativeDrawables = (start != null) || (end != null); 1135 if (hasRelativeDrawables) { 1136 Drawables dr = mDrawables; 1137 if (dr == null) { 1138 mDrawables = dr = new Drawables(); 1139 } 1140 final Rect compoundRect = dr.mCompoundRect; 1141 int[] state = getDrawableState(); 1142 if (start != null) { 1143 start.setBounds(0, 0, start.getIntrinsicWidth(), start.getIntrinsicHeight()); 1144 start.setState(state); 1145 start.copyBounds(compoundRect); 1146 start.setCallback(this); 1147 1148 dr.mDrawableStart = start; 1149 dr.mDrawableSizeStart = compoundRect.width(); 1150 dr.mDrawableHeightStart = compoundRect.height(); 1151 } else { 1152 dr.mDrawableSizeStart = dr.mDrawableHeightStart = 0; 1153 } 1154 if (end != null) { 1155 end.setBounds(0, 0, end.getIntrinsicWidth(), end.getIntrinsicHeight()); 1156 end.setState(state); 1157 end.copyBounds(compoundRect); 1158 end.setCallback(this); 1159 1160 dr.mDrawableEnd = end; 1161 dr.mDrawableSizeEnd = compoundRect.width(); 1162 dr.mDrawableHeightEnd = compoundRect.height(); 1163 } else { 1164 dr.mDrawableSizeEnd = dr.mDrawableHeightEnd = 0; 1165 } 1166 } 1167 } 1168 1169 @Override 1170 public void setEnabled(boolean enabled) { 1171 if (enabled == isEnabled()) { 1172 return; 1173 } 1174 1175 if (!enabled) { 1176 // Hide the soft input if the currently active TextView is disabled 1177 InputMethodManager imm = InputMethodManager.peekInstance(); 1178 if (imm != null && imm.isActive(this)) { 1179 imm.hideSoftInputFromWindow(getWindowToken(), 0); 1180 } 1181 } 1182 1183 super.setEnabled(enabled); 1184 1185 if (enabled) { 1186 // Make sure IME is updated with current editor info. 1187 InputMethodManager imm = InputMethodManager.peekInstance(); 1188 if (imm != null) imm.restartInput(this); 1189 } 1190 1191 // Will change text color 1192 if (mEditor != null) { 1193 mEditor.invalidateTextDisplayList(); 1194 mEditor.prepareCursorControllers(); 1195 1196 // start or stop the cursor blinking as appropriate 1197 mEditor.makeBlink(); 1198 } 1199 } 1200 1201 /** 1202 * Sets the typeface and style in which the text should be displayed, 1203 * and turns on the fake bold and italic bits in the Paint if the 1204 * Typeface that you provided does not have all the bits in the 1205 * style that you specified. 1206 * 1207 * @attr ref android.R.styleable#TextView_typeface 1208 * @attr ref android.R.styleable#TextView_textStyle 1209 */ 1210 public void setTypeface(Typeface tf, int style) { 1211 if (style > 0) { 1212 if (tf == null) { 1213 tf = Typeface.defaultFromStyle(style); 1214 } else { 1215 tf = Typeface.create(tf, style); 1216 } 1217 1218 setTypeface(tf); 1219 // now compute what (if any) algorithmic styling is needed 1220 int typefaceStyle = tf != null ? tf.getStyle() : 0; 1221 int need = style & ~typefaceStyle; 1222 mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0); 1223 mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0); 1224 } else { 1225 mTextPaint.setFakeBoldText(false); 1226 mTextPaint.setTextSkewX(0); 1227 setTypeface(tf); 1228 } 1229 } 1230 1231 /** 1232 * Subclasses override this to specify that they have a KeyListener 1233 * by default even if not specifically called for in the XML options. 1234 */ 1235 protected boolean getDefaultEditable() { 1236 return false; 1237 } 1238 1239 /** 1240 * Subclasses override this to specify a default movement method. 1241 */ 1242 protected MovementMethod getDefaultMovementMethod() { 1243 return null; 1244 } 1245 1246 /** 1247 * Return the text the TextView is displaying. If setText() was called with 1248 * an argument of BufferType.SPANNABLE or BufferType.EDITABLE, you can cast 1249 * the return value from this method to Spannable or Editable, respectively. 1250 * 1251 * Note: The content of the return value should not be modified. If you want 1252 * a modifiable one, you should make your own copy first. 1253 * 1254 * @attr ref android.R.styleable#TextView_text 1255 */ 1256 @ViewDebug.CapturedViewProperty 1257 public CharSequence getText() { 1258 return mText; 1259 } 1260 1261 /** 1262 * Returns the length, in characters, of the text managed by this TextView 1263 */ 1264 public int length() { 1265 return mText.length(); 1266 } 1267 1268 /** 1269 * Return the text the TextView is displaying as an Editable object. If 1270 * the text is not editable, null is returned. 1271 * 1272 * @see #getText 1273 */ 1274 public Editable getEditableText() { 1275 return (mText instanceof Editable) ? (Editable)mText : null; 1276 } 1277 1278 /** 1279 * @return the height of one standard line in pixels. Note that markup 1280 * within the text can cause individual lines to be taller or shorter 1281 * than this height, and the layout may contain additional first- 1282 * or last-line padding. 1283 */ 1284 public int getLineHeight() { 1285 return FastMath.round(mTextPaint.getFontMetricsInt(null) * mSpacingMult + mSpacingAdd); 1286 } 1287 1288 /** 1289 * @return the Layout that is currently being used to display the text. 1290 * This can be null if the text or width has recently changes. 1291 */ 1292 public final Layout getLayout() { 1293 return mLayout; 1294 } 1295 1296 /** 1297 * @return the current key listener for this TextView. 1298 * This will frequently be null for non-EditText TextViews. 1299 * 1300 * @attr ref android.R.styleable#TextView_numeric 1301 * @attr ref android.R.styleable#TextView_digits 1302 * @attr ref android.R.styleable#TextView_phoneNumber 1303 * @attr ref android.R.styleable#TextView_inputMethod 1304 * @attr ref android.R.styleable#TextView_capitalize 1305 * @attr ref android.R.styleable#TextView_autoText 1306 */ 1307 public final KeyListener getKeyListener() { 1308 return mEditor == null ? null : mEditor.mKeyListener; 1309 } 1310 1311 /** 1312 * Sets the key listener to be used with this TextView. This can be null 1313 * to disallow user input. Note that this method has significant and 1314 * subtle interactions with soft keyboards and other input method: 1315 * see {@link KeyListener#getInputType() KeyListener.getContentType()} 1316 * for important details. Calling this method will replace the current 1317 * content type of the text view with the content type returned by the 1318 * key listener. 1319 * <p> 1320 * Be warned that if you want a TextView with a key listener or movement 1321 * method not to be focusable, or if you want a TextView without a 1322 * key listener or movement method to be focusable, you must call 1323 * {@link #setFocusable} again after calling this to get the focusability 1324 * back the way you want it. 1325 * 1326 * @attr ref android.R.styleable#TextView_numeric 1327 * @attr ref android.R.styleable#TextView_digits 1328 * @attr ref android.R.styleable#TextView_phoneNumber 1329 * @attr ref android.R.styleable#TextView_inputMethod 1330 * @attr ref android.R.styleable#TextView_capitalize 1331 * @attr ref android.R.styleable#TextView_autoText 1332 */ 1333 public void setKeyListener(KeyListener input) { 1334 setKeyListenerOnly(input); 1335 fixFocusableAndClickableSettings(); 1336 1337 if (input != null) { 1338 createEditorIfNeeded("input is not null"); 1339 try { 1340 mEditor.mInputType = mEditor.mKeyListener.getInputType(); 1341 } catch (IncompatibleClassChangeError e) { 1342 mEditor.mInputType = EditorInfo.TYPE_CLASS_TEXT; 1343 } 1344 // Change inputType, without affecting transformation. 1345 // No need to applySingleLine since mSingleLine is unchanged. 1346 setInputTypeSingleLine(mSingleLine); 1347 } else { 1348 if (mEditor != null) mEditor.mInputType = EditorInfo.TYPE_NULL; 1349 } 1350 1351 InputMethodManager imm = InputMethodManager.peekInstance(); 1352 if (imm != null) imm.restartInput(this); 1353 } 1354 1355 private void setKeyListenerOnly(KeyListener input) { 1356 if (mEditor == null && input == null) return; // null is the default value 1357 1358 createEditorIfNeeded("setKeyListenerOnly"); 1359 if (mEditor.mKeyListener != input) { 1360 mEditor.mKeyListener = input; 1361 if (input != null && !(mText instanceof Editable)) { 1362 setText(mText); 1363 } 1364 1365 setFilters((Editable) mText, mFilters); 1366 } 1367 } 1368 1369 /** 1370 * @return the movement method being used for this TextView. 1371 * This will frequently be null for non-EditText TextViews. 1372 */ 1373 public final MovementMethod getMovementMethod() { 1374 return mMovement; 1375 } 1376 1377 /** 1378 * Sets the movement method (arrow key handler) to be used for 1379 * this TextView. This can be null to disallow using the arrow keys 1380 * to move the cursor or scroll the view. 1381 * <p> 1382 * Be warned that if you want a TextView with a key listener or movement 1383 * method not to be focusable, or if you want a TextView without a 1384 * key listener or movement method to be focusable, you must call 1385 * {@link #setFocusable} again after calling this to get the focusability 1386 * back the way you want it. 1387 */ 1388 public final void setMovementMethod(MovementMethod movement) { 1389 if (mMovement != movement) { 1390 mMovement = movement; 1391 1392 if (movement != null && !(mText instanceof Spannable)) { 1393 setText(mText); 1394 } 1395 1396 fixFocusableAndClickableSettings(); 1397 1398 // SelectionModifierCursorController depends on textCanBeSelected, which depends on 1399 // mMovement 1400 if (mEditor != null) mEditor.prepareCursorControllers(); 1401 } 1402 } 1403 1404 private void fixFocusableAndClickableSettings() { 1405 if (mMovement != null || (mEditor != null && mEditor.mKeyListener != null)) { 1406 setFocusable(true); 1407 setClickable(true); 1408 setLongClickable(true); 1409 } else { 1410 setFocusable(false); 1411 setClickable(false); 1412 setLongClickable(false); 1413 } 1414 } 1415 1416 /** 1417 * @return the current transformation method for this TextView. 1418 * This will frequently be null except for single-line and password 1419 * fields. 1420 * 1421 * @attr ref android.R.styleable#TextView_password 1422 * @attr ref android.R.styleable#TextView_singleLine 1423 */ 1424 public final TransformationMethod getTransformationMethod() { 1425 return mTransformation; 1426 } 1427 1428 /** 1429 * Sets the transformation that is applied to the text that this 1430 * TextView is displaying. 1431 * 1432 * @attr ref android.R.styleable#TextView_password 1433 * @attr ref android.R.styleable#TextView_singleLine 1434 */ 1435 public final void setTransformationMethod(TransformationMethod method) { 1436 if (method == mTransformation) { 1437 // Avoid the setText() below if the transformation is 1438 // the same. 1439 return; 1440 } 1441 if (mTransformation != null) { 1442 if (mText instanceof Spannable) { 1443 ((Spannable) mText).removeSpan(mTransformation); 1444 } 1445 } 1446 1447 mTransformation = method; 1448 1449 if (method instanceof TransformationMethod2) { 1450 TransformationMethod2 method2 = (TransformationMethod2) method; 1451 mAllowTransformationLengthChange = !isTextSelectable() && !(mText instanceof Editable); 1452 method2.setLengthChangesAllowed(mAllowTransformationLengthChange); 1453 } else { 1454 mAllowTransformationLengthChange = false; 1455 } 1456 1457 setText(mText); 1458 } 1459 1460 /** 1461 * Returns the top padding of the view, plus space for the top 1462 * Drawable if any. 1463 */ 1464 public int getCompoundPaddingTop() { 1465 final Drawables dr = mDrawables; 1466 if (dr == null || dr.mDrawableTop == null) { 1467 return mPaddingTop; 1468 } else { 1469 return mPaddingTop + dr.mDrawablePadding + dr.mDrawableSizeTop; 1470 } 1471 } 1472 1473 /** 1474 * Returns the bottom padding of the view, plus space for the bottom 1475 * Drawable if any. 1476 */ 1477 public int getCompoundPaddingBottom() { 1478 final Drawables dr = mDrawables; 1479 if (dr == null || dr.mDrawableBottom == null) { 1480 return mPaddingBottom; 1481 } else { 1482 return mPaddingBottom + dr.mDrawablePadding + dr.mDrawableSizeBottom; 1483 } 1484 } 1485 1486 /** 1487 * Returns the left padding of the view, plus space for the left 1488 * Drawable if any. 1489 */ 1490 public int getCompoundPaddingLeft() { 1491 final Drawables dr = mDrawables; 1492 if (dr == null || dr.mDrawableLeft == null) { 1493 return mPaddingLeft; 1494 } else { 1495 return mPaddingLeft + dr.mDrawablePadding + dr.mDrawableSizeLeft; 1496 } 1497 } 1498 1499 /** 1500 * Returns the right padding of the view, plus space for the right 1501 * Drawable if any. 1502 */ 1503 public int getCompoundPaddingRight() { 1504 final Drawables dr = mDrawables; 1505 if (dr == null || dr.mDrawableRight == null) { 1506 return mPaddingRight; 1507 } else { 1508 return mPaddingRight + dr.mDrawablePadding + dr.mDrawableSizeRight; 1509 } 1510 } 1511 1512 /** 1513 * Returns the start padding of the view, plus space for the start 1514 * Drawable if any. 1515 */ 1516 public int getCompoundPaddingStart() { 1517 resolveDrawables(); 1518 switch(getResolvedLayoutDirection()) { 1519 default: 1520 case LAYOUT_DIRECTION_LTR: 1521 return getCompoundPaddingLeft(); 1522 case LAYOUT_DIRECTION_RTL: 1523 return getCompoundPaddingRight(); 1524 } 1525 } 1526 1527 /** 1528 * Returns the end padding of the view, plus space for the end 1529 * Drawable if any. 1530 */ 1531 public int getCompoundPaddingEnd() { 1532 resolveDrawables(); 1533 switch(getResolvedLayoutDirection()) { 1534 default: 1535 case LAYOUT_DIRECTION_LTR: 1536 return getCompoundPaddingRight(); 1537 case LAYOUT_DIRECTION_RTL: 1538 return getCompoundPaddingLeft(); 1539 } 1540 } 1541 1542 /** 1543 * Returns the extended top padding of the view, including both the 1544 * top Drawable if any and any extra space to keep more than maxLines 1545 * of text from showing. It is only valid to call this after measuring. 1546 */ 1547 public int getExtendedPaddingTop() { 1548 if (mMaxMode != LINES) { 1549 return getCompoundPaddingTop(); 1550 } 1551 1552 if (mLayout.getLineCount() <= mMaximum) { 1553 return getCompoundPaddingTop(); 1554 } 1555 1556 int top = getCompoundPaddingTop(); 1557 int bottom = getCompoundPaddingBottom(); 1558 int viewht = getHeight() - top - bottom; 1559 int layoutht = mLayout.getLineTop(mMaximum); 1560 1561 if (layoutht >= viewht) { 1562 return top; 1563 } 1564 1565 final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; 1566 if (gravity == Gravity.TOP) { 1567 return top; 1568 } else if (gravity == Gravity.BOTTOM) { 1569 return top + viewht - layoutht; 1570 } else { // (gravity == Gravity.CENTER_VERTICAL) 1571 return top + (viewht - layoutht) / 2; 1572 } 1573 } 1574 1575 /** 1576 * Returns the extended bottom padding of the view, including both the 1577 * bottom Drawable if any and any extra space to keep more than maxLines 1578 * of text from showing. It is only valid to call this after measuring. 1579 */ 1580 public int getExtendedPaddingBottom() { 1581 if (mMaxMode != LINES) { 1582 return getCompoundPaddingBottom(); 1583 } 1584 1585 if (mLayout.getLineCount() <= mMaximum) { 1586 return getCompoundPaddingBottom(); 1587 } 1588 1589 int top = getCompoundPaddingTop(); 1590 int bottom = getCompoundPaddingBottom(); 1591 int viewht = getHeight() - top - bottom; 1592 int layoutht = mLayout.getLineTop(mMaximum); 1593 1594 if (layoutht >= viewht) { 1595 return bottom; 1596 } 1597 1598 final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; 1599 if (gravity == Gravity.TOP) { 1600 return bottom + viewht - layoutht; 1601 } else if (gravity == Gravity.BOTTOM) { 1602 return bottom; 1603 } else { // (gravity == Gravity.CENTER_VERTICAL) 1604 return bottom + (viewht - layoutht) / 2; 1605 } 1606 } 1607 1608 /** 1609 * Returns the total left padding of the view, including the left 1610 * Drawable if any. 1611 */ 1612 public int getTotalPaddingLeft() { 1613 return getCompoundPaddingLeft(); 1614 } 1615 1616 /** 1617 * Returns the total right padding of the view, including the right 1618 * Drawable if any. 1619 */ 1620 public int getTotalPaddingRight() { 1621 return getCompoundPaddingRight(); 1622 } 1623 1624 /** 1625 * Returns the total start padding of the view, including the start 1626 * Drawable if any. 1627 */ 1628 public int getTotalPaddingStart() { 1629 return getCompoundPaddingStart(); 1630 } 1631 1632 /** 1633 * Returns the total end padding of the view, including the end 1634 * Drawable if any. 1635 */ 1636 public int getTotalPaddingEnd() { 1637 return getCompoundPaddingEnd(); 1638 } 1639 1640 /** 1641 * Returns the total top padding of the view, including the top 1642 * Drawable if any, the extra space to keep more than maxLines 1643 * from showing, and the vertical offset for gravity, if any. 1644 */ 1645 public int getTotalPaddingTop() { 1646 return getExtendedPaddingTop() + getVerticalOffset(true); 1647 } 1648 1649 /** 1650 * Returns the total bottom padding of the view, including the bottom 1651 * Drawable if any, the extra space to keep more than maxLines 1652 * from showing, and the vertical offset for gravity, if any. 1653 */ 1654 public int getTotalPaddingBottom() { 1655 return getExtendedPaddingBottom() + getBottomVerticalOffset(true); 1656 } 1657 1658 /** 1659 * Sets the Drawables (if any) to appear to the left of, above, 1660 * to the right of, and below the text. Use null if you do not 1661 * want a Drawable there. The Drawables must already have had 1662 * {@link Drawable#setBounds} called. 1663 * 1664 * @attr ref android.R.styleable#TextView_drawableLeft 1665 * @attr ref android.R.styleable#TextView_drawableTop 1666 * @attr ref android.R.styleable#TextView_drawableRight 1667 * @attr ref android.R.styleable#TextView_drawableBottom 1668 */ 1669 public void setCompoundDrawables(Drawable left, Drawable top, 1670 Drawable right, Drawable bottom) { 1671 Drawables dr = mDrawables; 1672 1673 final boolean drawables = left != null || top != null 1674 || right != null || bottom != null; 1675 1676 if (!drawables) { 1677 // Clearing drawables... can we free the data structure? 1678 if (dr != null) { 1679 if (dr.mDrawablePadding == 0) { 1680 mDrawables = null; 1681 } else { 1682 // We need to retain the last set padding, so just clear 1683 // out all of the fields in the existing structure. 1684 if (dr.mDrawableLeft != null) dr.mDrawableLeft.setCallback(null); 1685 dr.mDrawableLeft = null; 1686 if (dr.mDrawableTop != null) dr.mDrawableTop.setCallback(null); 1687 dr.mDrawableTop = null; 1688 if (dr.mDrawableRight != null) dr.mDrawableRight.setCallback(null); 1689 dr.mDrawableRight = null; 1690 if (dr.mDrawableBottom != null) dr.mDrawableBottom.setCallback(null); 1691 dr.mDrawableBottom = null; 1692 dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0; 1693 dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0; 1694 dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0; 1695 dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0; 1696 } 1697 } 1698 } else { 1699 if (dr == null) { 1700 mDrawables = dr = new Drawables(); 1701 } 1702 1703 if (dr.mDrawableLeft != left && dr.mDrawableLeft != null) { 1704 dr.mDrawableLeft.setCallback(null); 1705 } 1706 dr.mDrawableLeft = left; 1707 1708 if (dr.mDrawableTop != top && dr.mDrawableTop != null) { 1709 dr.mDrawableTop.setCallback(null); 1710 } 1711 dr.mDrawableTop = top; 1712 1713 if (dr.mDrawableRight != right && dr.mDrawableRight != null) { 1714 dr.mDrawableRight.setCallback(null); 1715 } 1716 dr.mDrawableRight = right; 1717 1718 if (dr.mDrawableBottom != bottom && dr.mDrawableBottom != null) { 1719 dr.mDrawableBottom.setCallback(null); 1720 } 1721 dr.mDrawableBottom = bottom; 1722 1723 final Rect compoundRect = dr.mCompoundRect; 1724 int[] state; 1725 1726 state = getDrawableState(); 1727 1728 if (left != null) { 1729 left.setState(state); 1730 left.copyBounds(compoundRect); 1731 left.setCallback(this); 1732 dr.mDrawableSizeLeft = compoundRect.width(); 1733 dr.mDrawableHeightLeft = compoundRect.height(); 1734 } else { 1735 dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0; 1736 } 1737 1738 if (right != null) { 1739 right.setState(state); 1740 right.copyBounds(compoundRect); 1741 right.setCallback(this); 1742 dr.mDrawableSizeRight = compoundRect.width(); 1743 dr.mDrawableHeightRight = compoundRect.height(); 1744 } else { 1745 dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0; 1746 } 1747 1748 if (top != null) { 1749 top.setState(state); 1750 top.copyBounds(compoundRect); 1751 top.setCallback(this); 1752 dr.mDrawableSizeTop = compoundRect.height(); 1753 dr.mDrawableWidthTop = compoundRect.width(); 1754 } else { 1755 dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0; 1756 } 1757 1758 if (bottom != null) { 1759 bottom.setState(state); 1760 bottom.copyBounds(compoundRect); 1761 bottom.setCallback(this); 1762 dr.mDrawableSizeBottom = compoundRect.height(); 1763 dr.mDrawableWidthBottom = compoundRect.width(); 1764 } else { 1765 dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0; 1766 } 1767 } 1768 1769 invalidate(); 1770 requestLayout(); 1771 } 1772 1773 /** 1774 * Sets the Drawables (if any) to appear to the left of, above, 1775 * to the right of, and below the text. Use 0 if you do not 1776 * want a Drawable there. The Drawables' bounds will be set to 1777 * their intrinsic bounds. 1778 * 1779 * @param left Resource identifier of the left Drawable. 1780 * @param top Resource identifier of the top Drawable. 1781 * @param right Resource identifier of the right Drawable. 1782 * @param bottom Resource identifier of the bottom Drawable. 1783 * 1784 * @attr ref android.R.styleable#TextView_drawableLeft 1785 * @attr ref android.R.styleable#TextView_drawableTop 1786 * @attr ref android.R.styleable#TextView_drawableRight 1787 * @attr ref android.R.styleable#TextView_drawableBottom 1788 */ 1789 @android.view.RemotableViewMethod 1790 public void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) { 1791 final Resources resources = getContext().getResources(); 1792 setCompoundDrawablesWithIntrinsicBounds(left != 0 ? resources.getDrawable(left) : null, 1793 top != 0 ? resources.getDrawable(top) : null, 1794 right != 0 ? resources.getDrawable(right) : null, 1795 bottom != 0 ? resources.getDrawable(bottom) : null); 1796 } 1797 1798 /** 1799 * Sets the Drawables (if any) to appear to the left of, above, 1800 * to the right of, and below the text. Use null if you do not 1801 * want a Drawable there. The Drawables' bounds will be set to 1802 * their intrinsic bounds. 1803 * 1804 * @attr ref android.R.styleable#TextView_drawableLeft 1805 * @attr ref android.R.styleable#TextView_drawableTop 1806 * @attr ref android.R.styleable#TextView_drawableRight 1807 * @attr ref android.R.styleable#TextView_drawableBottom 1808 */ 1809 public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, 1810 Drawable right, Drawable bottom) { 1811 1812 if (left != null) { 1813 left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight()); 1814 } 1815 if (right != null) { 1816 right.setBounds(0, 0, right.getIntrinsicWidth(), right.getIntrinsicHeight()); 1817 } 1818 if (top != null) { 1819 top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight()); 1820 } 1821 if (bottom != null) { 1822 bottom.setBounds(0, 0, bottom.getIntrinsicWidth(), bottom.getIntrinsicHeight()); 1823 } 1824 setCompoundDrawables(left, top, right, bottom); 1825 } 1826 1827 /** 1828 * Sets the Drawables (if any) to appear to the start of, above, 1829 * to the end of, and below the text. Use null if you do not 1830 * want a Drawable there. The Drawables must already have had 1831 * {@link Drawable#setBounds} called. 1832 * 1833 * @attr ref android.R.styleable#TextView_drawableStart 1834 * @attr ref android.R.styleable#TextView_drawableTop 1835 * @attr ref android.R.styleable#TextView_drawableEnd 1836 * @attr ref android.R.styleable#TextView_drawableBottom 1837 */ 1838 public void setCompoundDrawablesRelative(Drawable start, Drawable top, 1839 Drawable end, Drawable bottom) { 1840 Drawables dr = mDrawables; 1841 1842 final boolean drawables = start != null || top != null 1843 || end != null || bottom != null; 1844 1845 if (!drawables) { 1846 // Clearing drawables... can we free the data structure? 1847 if (dr != null) { 1848 if (dr.mDrawablePadding == 0) { 1849 mDrawables = null; 1850 } else { 1851 // We need to retain the last set padding, so just clear 1852 // out all of the fields in the existing structure. 1853 if (dr.mDrawableStart != null) dr.mDrawableStart.setCallback(null); 1854 dr.mDrawableStart = null; 1855 if (dr.mDrawableTop != null) dr.mDrawableTop.setCallback(null); 1856 dr.mDrawableTop = null; 1857 if (dr.mDrawableEnd != null) dr.mDrawableEnd.setCallback(null); 1858 dr.mDrawableEnd = null; 1859 if (dr.mDrawableBottom != null) dr.mDrawableBottom.setCallback(null); 1860 dr.mDrawableBottom = null; 1861 dr.mDrawableSizeStart = dr.mDrawableHeightStart = 0; 1862 dr.mDrawableSizeEnd = dr.mDrawableHeightEnd = 0; 1863 dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0; 1864 dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0; 1865 } 1866 } 1867 } else { 1868 if (dr == null) { 1869 mDrawables = dr = new Drawables(); 1870 } 1871 1872 if (dr.mDrawableStart != start && dr.mDrawableStart != null) { 1873 dr.mDrawableStart.setCallback(null); 1874 } 1875 dr.mDrawableStart = start; 1876 1877 if (dr.mDrawableTop != top && dr.mDrawableTop != null) { 1878 dr.mDrawableTop.setCallback(null); 1879 } 1880 dr.mDrawableTop = top; 1881 1882 if (dr.mDrawableEnd != end && dr.mDrawableEnd != null) { 1883 dr.mDrawableEnd.setCallback(null); 1884 } 1885 dr.mDrawableEnd = end; 1886 1887 if (dr.mDrawableBottom != bottom && dr.mDrawableBottom != null) { 1888 dr.mDrawableBottom.setCallback(null); 1889 } 1890 dr.mDrawableBottom = bottom; 1891 1892 final Rect compoundRect = dr.mCompoundRect; 1893 int[] state; 1894 1895 state = getDrawableState(); 1896 1897 if (start != null) { 1898 start.setState(state); 1899 start.copyBounds(compoundRect); 1900 start.setCallback(this); 1901 dr.mDrawableSizeStart = compoundRect.width(); 1902 dr.mDrawableHeightStart = compoundRect.height(); 1903 } else { 1904 dr.mDrawableSizeStart = dr.mDrawableHeightStart = 0; 1905 } 1906 1907 if (end != null) { 1908 end.setState(state); 1909 end.copyBounds(compoundRect); 1910 end.setCallback(this); 1911 dr.mDrawableSizeEnd = compoundRect.width(); 1912 dr.mDrawableHeightEnd = compoundRect.height(); 1913 } else { 1914 dr.mDrawableSizeEnd = dr.mDrawableHeightEnd = 0; 1915 } 1916 1917 if (top != null) { 1918 top.setState(state); 1919 top.copyBounds(compoundRect); 1920 top.setCallback(this); 1921 dr.mDrawableSizeTop = compoundRect.height(); 1922 dr.mDrawableWidthTop = compoundRect.width(); 1923 } else { 1924 dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0; 1925 } 1926 1927 if (bottom != null) { 1928 bottom.setState(state); 1929 bottom.copyBounds(compoundRect); 1930 bottom.setCallback(this); 1931 dr.mDrawableSizeBottom = compoundRect.height(); 1932 dr.mDrawableWidthBottom = compoundRect.width(); 1933 } else { 1934 dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0; 1935 } 1936 } 1937 1938 resolveDrawables(); 1939 invalidate(); 1940 requestLayout(); 1941 } 1942 1943 /** 1944 * Sets the Drawables (if any) to appear to the start of, above, 1945 * to the end of, and below the text. Use 0 if you do not 1946 * want a Drawable there. The Drawables' bounds will be set to 1947 * their intrinsic bounds. 1948 * 1949 * @param start Resource identifier of the start Drawable. 1950 * @param top Resource identifier of the top Drawable. 1951 * @param end Resource identifier of the end Drawable. 1952 * @param bottom Resource identifier of the bottom Drawable. 1953 * 1954 * @attr ref android.R.styleable#TextView_drawableStart 1955 * @attr ref android.R.styleable#TextView_drawableTop 1956 * @attr ref android.R.styleable#TextView_drawableEnd 1957 * @attr ref android.R.styleable#TextView_drawableBottom 1958 */ 1959 @android.view.RemotableViewMethod 1960 public void setCompoundDrawablesRelativeWithIntrinsicBounds(int start, int top, int end, 1961 int bottom) { 1962 resetResolvedDrawables(); 1963 final Resources resources = getContext().getResources(); 1964 setCompoundDrawablesRelativeWithIntrinsicBounds( 1965 start != 0 ? resources.getDrawable(start) : null, 1966 top != 0 ? resources.getDrawable(top) : null, 1967 end != 0 ? resources.getDrawable(end) : null, 1968 bottom != 0 ? resources.getDrawable(bottom) : null); 1969 } 1970 1971 /** 1972 * Sets the Drawables (if any) to appear to the start of, above, 1973 * to the end of, and below the text. Use null if you do not 1974 * want a Drawable there. The Drawables' bounds will be set to 1975 * their intrinsic bounds. 1976 * 1977 * @attr ref android.R.styleable#TextView_drawableStart 1978 * @attr ref android.R.styleable#TextView_drawableTop 1979 * @attr ref android.R.styleable#TextView_drawableEnd 1980 * @attr ref android.R.styleable#TextView_drawableBottom 1981 */ 1982 public void setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable start, Drawable top, 1983 Drawable end, Drawable bottom) { 1984 1985 resetResolvedDrawables(); 1986 if (start != null) { 1987 start.setBounds(0, 0, start.getIntrinsicWidth(), start.getIntrinsicHeight()); 1988 } 1989 if (end != null) { 1990 end.setBounds(0, 0, end.getIntrinsicWidth(), end.getIntrinsicHeight()); 1991 } 1992 if (top != null) { 1993 top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight()); 1994 } 1995 if (bottom != null) { 1996 bottom.setBounds(0, 0, bottom.getIntrinsicWidth(), bottom.getIntrinsicHeight()); 1997 } 1998 setCompoundDrawablesRelative(start, top, end, bottom); 1999 } 2000 2001 /** 2002 * Returns drawables for the left, top, right, and bottom borders. 2003 * 2004 * @attr ref android.R.styleable#TextView_drawableLeft 2005 * @attr ref android.R.styleable#TextView_drawableTop 2006 * @attr ref android.R.styleable#TextView_drawableRight 2007 * @attr ref android.R.styleable#TextView_drawableBottom 2008 */ 2009 public Drawable[] getCompoundDrawables() { 2010 final Drawables dr = mDrawables; 2011 if (dr != null) { 2012 return new Drawable[] { 2013 dr.mDrawableLeft, dr.mDrawableTop, dr.mDrawableRight, dr.mDrawableBottom 2014 }; 2015 } else { 2016 return new Drawable[] { null, null, null, null }; 2017 } 2018 } 2019 2020 /** 2021 * Returns drawables for the start, top, end, and bottom borders. 2022 * 2023 * @attr ref android.R.styleable#TextView_drawableStart 2024 * @attr ref android.R.styleable#TextView_drawableTop 2025 * @attr ref android.R.styleable#TextView_drawableEnd 2026 * @attr ref android.R.styleable#TextView_drawableBottom 2027 */ 2028 public Drawable[] getCompoundDrawablesRelative() { 2029 final Drawables dr = mDrawables; 2030 if (dr != null) { 2031 return new Drawable[] { 2032 dr.mDrawableStart, dr.mDrawableTop, dr.mDrawableEnd, dr.mDrawableBottom 2033 }; 2034 } else { 2035 return new Drawable[] { null, null, null, null }; 2036 } 2037 } 2038 2039 /** 2040 * Sets the size of the padding between the compound drawables and 2041 * the text. 2042 * 2043 * @attr ref android.R.styleable#TextView_drawablePadding 2044 */ 2045 @android.view.RemotableViewMethod 2046 public void setCompoundDrawablePadding(int pad) { 2047 Drawables dr = mDrawables; 2048 if (pad == 0) { 2049 if (dr != null) { 2050 dr.mDrawablePadding = pad; 2051 } 2052 } else { 2053 if (dr == null) { 2054 mDrawables = dr = new Drawables(); 2055 } 2056 dr.mDrawablePadding = pad; 2057 } 2058 2059 invalidate(); 2060 requestLayout(); 2061 } 2062 2063 /** 2064 * Returns the padding between the compound drawables and the text. 2065 * 2066 * @attr ref android.R.styleable#TextView_drawablePadding 2067 */ 2068 public int getCompoundDrawablePadding() { 2069 final Drawables dr = mDrawables; 2070 return dr != null ? dr.mDrawablePadding : 0; 2071 } 2072 2073 @Override 2074 public void setPadding(int left, int top, int right, int bottom) { 2075 if (left != mPaddingLeft || 2076 right != mPaddingRight || 2077 top != mPaddingTop || 2078 bottom != mPaddingBottom) { 2079 nullLayouts(); 2080 } 2081 2082 // the super call will requestLayout() 2083 super.setPadding(left, top, right, bottom); 2084 invalidate(); 2085 } 2086 2087 @Override 2088 public void setPaddingRelative(int start, int top, int end, int bottom) { 2089 if (start != getPaddingStart() || 2090 end != getPaddingEnd() || 2091 top != mPaddingTop || 2092 bottom != mPaddingBottom) { 2093 nullLayouts(); 2094 } 2095 2096 // the super call will requestLayout() 2097 super.setPaddingRelative(start, top, end, bottom); 2098 invalidate(); 2099 } 2100 2101 /** 2102 * Gets the autolink mask of the text. See {@link 2103 * android.text.util.Linkify#ALL Linkify.ALL} and peers for 2104 * possible values. 2105 * 2106 * @attr ref android.R.styleable#TextView_autoLink 2107 */ 2108 public final int getAutoLinkMask() { 2109 return mAutoLinkMask; 2110 } 2111 2112 /** 2113 * Sets the text color, size, style, hint color, and highlight color 2114 * from the specified TextAppearance resource. 2115 */ 2116 public void setTextAppearance(Context context, int resid) { 2117 TypedArray appearance = 2118 context.obtainStyledAttributes(resid, 2119 com.android.internal.R.styleable.TextAppearance); 2120 2121 int color; 2122 ColorStateList colors; 2123 int ts; 2124 2125 color = appearance.getColor( 2126 com.android.internal.R.styleable.TextAppearance_textColorHighlight, 0); 2127 if (color != 0) { 2128 setHighlightColor(color); 2129 } 2130 2131 colors = appearance.getColorStateList(com.android.internal.R.styleable. 2132 TextAppearance_textColor); 2133 if (colors != null) { 2134 setTextColor(colors); 2135 } 2136 2137 ts = appearance.getDimensionPixelSize(com.android.internal.R.styleable. 2138 TextAppearance_textSize, 0); 2139 if (ts != 0) { 2140 setRawTextSize(ts); 2141 } 2142 2143 colors = appearance.getColorStateList(com.android.internal.R.styleable. 2144 TextAppearance_textColorHint); 2145 if (colors != null) { 2146 setHintTextColor(colors); 2147 } 2148 2149 colors = appearance.getColorStateList(com.android.internal.R.styleable. 2150 TextAppearance_textColorLink); 2151 if (colors != null) { 2152 setLinkTextColor(colors); 2153 } 2154 2155 int typefaceIndex, styleIndex; 2156 2157 typefaceIndex = appearance.getInt(com.android.internal.R.styleable. 2158 TextAppearance_typeface, -1); 2159 styleIndex = appearance.getInt(com.android.internal.R.styleable. 2160 TextAppearance_textStyle, -1); 2161 2162 setTypefaceByIndex(typefaceIndex, styleIndex); 2163 2164 if (appearance.getBoolean(com.android.internal.R.styleable.TextAppearance_textAllCaps, 2165 false)) { 2166 setTransformationMethod(new AllCapsTransformationMethod(getContext())); 2167 } 2168 2169 appearance.recycle(); 2170 } 2171 2172 /** 2173 * @return the size (in pixels) of the default text size in this TextView. 2174 */ 2175 @ViewDebug.ExportedProperty(category = "text") 2176 public float getTextSize() { 2177 return mTextPaint.getTextSize(); 2178 } 2179 2180 /** 2181 * Set the default text size to the given value, interpreted as "scaled 2182 * pixel" units. This size is adjusted based on the current density and 2183 * user font size preference. 2184 * 2185 * @param size The scaled pixel size. 2186 * 2187 * @attr ref android.R.styleable#TextView_textSize 2188 */ 2189 @android.view.RemotableViewMethod 2190 public void setTextSize(float size) { 2191 setTextSize(TypedValue.COMPLEX_UNIT_SP, size); 2192 } 2193 2194 /** 2195 * Set the default text size to a given unit and value. See {@link 2196 * TypedValue} for the possible dimension units. 2197 * 2198 * @param unit The desired dimension unit. 2199 * @param size The desired size in the given units. 2200 * 2201 * @attr ref android.R.styleable#TextView_textSize 2202 */ 2203 public void setTextSize(int unit, float size) { 2204 Context c = getContext(); 2205 Resources r; 2206 2207 if (c == null) 2208 r = Resources.getSystem(); 2209 else 2210 r = c.getResources(); 2211 2212 setRawTextSize(TypedValue.applyDimension( 2213 unit, size, r.getDisplayMetrics())); 2214 } 2215 2216 private void setRawTextSize(float size) { 2217 if (size != mTextPaint.getTextSize()) { 2218 mTextPaint.setTextSize(size); 2219 2220 if (mLayout != null) { 2221 nullLayouts(); 2222 requestLayout(); 2223 invalidate(); 2224 } 2225 } 2226 } 2227 2228 /** 2229 * @return the extent by which text is currently being stretched 2230 * horizontally. This will usually be 1. 2231 */ 2232 public float getTextScaleX() { 2233 return mTextPaint.getTextScaleX(); 2234 } 2235 2236 /** 2237 * Sets the extent by which text should be stretched horizontally. 2238 * 2239 * @attr ref android.R.styleable#TextView_textScaleX 2240 */ 2241 @android.view.RemotableViewMethod 2242 public void setTextScaleX(float size) { 2243 if (size != mTextPaint.getTextScaleX()) { 2244 mUserSetTextScaleX = true; 2245 mTextPaint.setTextScaleX(size); 2246 2247 if (mLayout != null) { 2248 nullLayouts(); 2249 requestLayout(); 2250 invalidate(); 2251 } 2252 } 2253 } 2254 2255 /** 2256 * Sets the typeface and style in which the text should be displayed. 2257 * Note that not all Typeface families actually have bold and italic 2258 * variants, so you may need to use 2259 * {@link #setTypeface(Typeface, int)} to get the appearance 2260 * that you actually want. 2261 * 2262 * @see #getTypeface() 2263 * 2264 * @attr ref android.R.styleable#TextView_typeface 2265 * @attr ref android.R.styleable#TextView_textStyle 2266 */ 2267 public void setTypeface(Typeface tf) { 2268 if (mTextPaint.getTypeface() != tf) { 2269 mTextPaint.setTypeface(tf); 2270 2271 if (mLayout != null) { 2272 nullLayouts(); 2273 requestLayout(); 2274 invalidate(); 2275 } 2276 } 2277 } 2278 2279 /** 2280 * @return the current typeface and style in which the text is being 2281 * displayed. 2282 * 2283 * @see #setTypeface(Typeface) 2284 * 2285 * @attr ref android.R.styleable#TextView_typeface 2286 * @attr ref android.R.styleable#TextView_textStyle 2287 */ 2288 public Typeface getTypeface() { 2289 return mTextPaint.getTypeface(); 2290 } 2291 2292 /** 2293 * Sets the text color for all the states (normal, selected, 2294 * focused) to be this color. 2295 * 2296 * @see #setTextColor(ColorStateList) 2297 * @see #getTextColors() 2298 * 2299 * @attr ref android.R.styleable#TextView_textColor 2300 */ 2301 @android.view.RemotableViewMethod 2302 public void setTextColor(int color) { 2303 mTextColor = ColorStateList.valueOf(color); 2304 updateTextColors(); 2305 } 2306 2307 /** 2308 * Sets the text color. 2309 * 2310 * @see #setTextColor(int) 2311 * @see #getTextColors() 2312 * @see #setHintTextColor(ColorStateList) 2313 * @see #setLinkTextColor(ColorStateList) 2314 * 2315 * @attr ref android.R.styleable#TextView_textColor 2316 */ 2317 public void setTextColor(ColorStateList colors) { 2318 if (colors == null) { 2319 throw new NullPointerException(); 2320 } 2321 2322 mTextColor = colors; 2323 updateTextColors(); 2324 } 2325 2326 /** 2327 * Gets the text colors for the different states (normal, selected, focused) of the TextView. 2328 * 2329 * @see #setTextColor(ColorStateList) 2330 * @see #setTextColor(int) 2331 * 2332 * @attr ref android.R.styleable#TextView_textColor 2333 */ 2334 public final ColorStateList getTextColors() { 2335 return mTextColor; 2336 } 2337 2338 /** 2339 * <p>Return the current color selected for normal text.</p> 2340 * 2341 * @return Returns the current text color. 2342 */ 2343 public final int getCurrentTextColor() { 2344 return mCurTextColor; 2345 } 2346 2347 /** 2348 * Sets the color used to display the selection highlight. 2349 * 2350 * @attr ref android.R.styleable#TextView_textColorHighlight 2351 */ 2352 @android.view.RemotableViewMethod 2353 public void setHighlightColor(int color) { 2354 if (mHighlightColor != color) { 2355 mHighlightColor = color; 2356 invalidate(); 2357 } 2358 } 2359 2360 /** 2361 * @return the color used to display the selection highlight 2362 * 2363 * @see #setHighlightColor(int) 2364 * 2365 * @attr ref android.R.styleable#TextView_textColorHighlight 2366 */ 2367 public int getHighlightColor() { 2368 return mHighlightColor; 2369 } 2370 2371 /** 2372 * Sets whether the soft input method will be made visible when this 2373 * TextView gets focused. The default is true. 2374 * @hide 2375 */ 2376 @android.view.RemotableViewMethod 2377 public final void setShowSoftInputOnFocus(boolean show) { 2378 createEditorIfNeeded("setShowSoftInputOnFocus"); 2379 mEditor.mShowSoftInputOnFocus = show; 2380 } 2381 2382 /** 2383 * Returns whether the soft input method will be made visible when this 2384 * TextView gets focused. The default is true. 2385 * @hide 2386 */ 2387 public final boolean getShowSoftInputOnFocus() { 2388 // When there is no Editor, return default true value 2389 return mEditor == null || mEditor.mShowSoftInputOnFocus; 2390 } 2391 2392 /** 2393 * Gives the text a shadow of the specified radius and color, the specified 2394 * distance from its normal position. 2395 * 2396 * @attr ref android.R.styleable#TextView_shadowColor 2397 * @attr ref android.R.styleable#TextView_shadowDx 2398 * @attr ref android.R.styleable#TextView_shadowDy 2399 * @attr ref android.R.styleable#TextView_shadowRadius 2400 */ 2401 public void setShadowLayer(float radius, float dx, float dy, int color) { 2402 mTextPaint.setShadowLayer(radius, dx, dy, color); 2403 2404 mShadowRadius = radius; 2405 mShadowDx = dx; 2406 mShadowDy = dy; 2407 2408 // Will change text clip region 2409 if (mEditor != null) mEditor.invalidateTextDisplayList(); 2410 invalidate(); 2411 } 2412 2413 /** 2414 * Gets the radius of the shadow layer. 2415 * 2416 * @return the radius of the shadow layer. If 0, the shadow layer is not visible 2417 * 2418 * @see #setShadowLayer(float, float, float, int) 2419 * 2420 * @attr ref android.R.styleable#TextView_shadowRadius 2421 */ 2422 public float getShadowRadius() { 2423 return mShadowRadius; 2424 } 2425 2426 /** 2427 * @return the horizontal offset of the shadow layer 2428 * 2429 * @see #setShadowLayer(float, float, float, int) 2430 * 2431 * @attr ref android.R.styleable#TextView_shadowDx 2432 */ 2433 public float getShadowDx() { 2434 return mShadowDx; 2435 } 2436 2437 /** 2438 * @return the vertical offset of the shadow layer 2439 * 2440 * @see #setShadowLayer(float, float, float, int) 2441 * 2442 * @attr ref android.R.styleable#TextView_shadowDy 2443 */ 2444 public float getShadowDy() { 2445 return mShadowDy; 2446 } 2447 2448 /** 2449 * @return the color of the shadow layer 2450 * 2451 * @see #setShadowLayer(float, float, float, int) 2452 * 2453 * @attr ref android.R.styleable#TextView_shadowColor 2454 */ 2455 public int getShadowColor() { 2456 return mTextPaint.shadowColor; 2457 } 2458 2459 /** 2460 * @return the base paint used for the text. Please use this only to 2461 * consult the Paint's properties and not to change them. 2462 */ 2463 public TextPaint getPaint() { 2464 return mTextPaint; 2465 } 2466 2467 /** 2468 * Sets the autolink mask of the text. See {@link 2469 * android.text.util.Linkify#ALL Linkify.ALL} and peers for 2470 * possible values. 2471 * 2472 * @attr ref android.R.styleable#TextView_autoLink 2473 */ 2474 @android.view.RemotableViewMethod 2475 public final void setAutoLinkMask(int mask) { 2476 mAutoLinkMask = mask; 2477 } 2478 2479 /** 2480 * Sets whether the movement method will automatically be set to 2481 * {@link LinkMovementMethod} if {@link #setAutoLinkMask} has been 2482 * set to nonzero and links are detected in {@link #setText}. 2483 * The default is true. 2484 * 2485 * @attr ref android.R.styleable#TextView_linksClickable 2486 */ 2487 @android.view.RemotableViewMethod 2488 public final void setLinksClickable(boolean whether) { 2489 mLinksClickable = whether; 2490 } 2491 2492 /** 2493 * Returns whether the movement method will automatically be set to 2494 * {@link LinkMovementMethod} if {@link #setAutoLinkMask} has been 2495 * set to nonzero and links are detected in {@link #setText}. 2496 * The default is true. 2497 * 2498 * @attr ref android.R.styleable#TextView_linksClickable 2499 */ 2500 public final boolean getLinksClickable() { 2501 return mLinksClickable; 2502 } 2503 2504 /** 2505 * Returns the list of URLSpans attached to the text 2506 * (by {@link Linkify} or otherwise) if any. You can call 2507 * {@link URLSpan#getURL} on them to find where they link to 2508 * or use {@link Spanned#getSpanStart} and {@link Spanned#getSpanEnd} 2509 * to find the region of the text they are attached to. 2510 */ 2511 public URLSpan[] getUrls() { 2512 if (mText instanceof Spanned) { 2513 return ((Spanned) mText).getSpans(0, mText.length(), URLSpan.class); 2514 } else { 2515 return new URLSpan[0]; 2516 } 2517 } 2518 2519 /** 2520 * Sets the color of the hint text for all the states (disabled, focussed, selected...) of this 2521 * TextView. 2522 * 2523 * @see #setHintTextColor(ColorStateList) 2524 * @see #getHintTextColors() 2525 * @see #setTextColor(int) 2526 * 2527 * @attr ref android.R.styleable#TextView_textColorHint 2528 */ 2529 @android.view.RemotableViewMethod 2530 public final void setHintTextColor(int color) { 2531 mHintTextColor = ColorStateList.valueOf(color); 2532 updateTextColors(); 2533 } 2534 2535 /** 2536 * Sets the color of the hint text. 2537 * 2538 * @see #getHintTextColors() 2539 * @see #setHintTextColor(int) 2540 * @see #setTextColor(ColorStateList) 2541 * @see #setLinkTextColor(ColorStateList) 2542 * 2543 * @attr ref android.R.styleable#TextView_textColorHint 2544 */ 2545 public final void setHintTextColor(ColorStateList colors) { 2546 mHintTextColor = colors; 2547 updateTextColors(); 2548 } 2549 2550 /** 2551 * @return the color of the hint text, for the different states of this TextView. 2552 * 2553 * @see #setHintTextColor(ColorStateList) 2554 * @see #setHintTextColor(int) 2555 * @see #setTextColor(ColorStateList) 2556 * @see #setLinkTextColor(ColorStateList) 2557 * 2558 * @attr ref android.R.styleable#TextView_textColorHint 2559 */ 2560 public final ColorStateList getHintTextColors() { 2561 return mHintTextColor; 2562 } 2563 2564 /** 2565 * <p>Return the current color selected to paint the hint text.</p> 2566 * 2567 * @return Returns the current hint text color. 2568 */ 2569 public final int getCurrentHintTextColor() { 2570 return mHintTextColor != null ? mCurHintTextColor : mCurTextColor; 2571 } 2572 2573 /** 2574 * Sets the color of links in the text. 2575 * 2576 * @see #setLinkTextColor(ColorStateList) 2577 * @see #getLinkTextColors() 2578 * 2579 * @attr ref android.R.styleable#TextView_textColorLink 2580 */ 2581 @android.view.RemotableViewMethod 2582 public final void setLinkTextColor(int color) { 2583 mLinkTextColor = ColorStateList.valueOf(color); 2584 updateTextColors(); 2585 } 2586 2587 /** 2588 * Sets the color of links in the text. 2589 * 2590 * @see #setLinkTextColor(int) 2591 * @see #getLinkTextColors() 2592 * @see #setTextColor(ColorStateList) 2593 * @see #setHintTextColor(ColorStateList) 2594 * 2595 * @attr ref android.R.styleable#TextView_textColorLink 2596 */ 2597 public final void setLinkTextColor(ColorStateList colors) { 2598 mLinkTextColor = colors; 2599 updateTextColors(); 2600 } 2601 2602 /** 2603 * @return the list of colors used to paint the links in the text, for the different states of 2604 * this TextView 2605 * 2606 * @see #setLinkTextColor(ColorStateList) 2607 * @see #setLinkTextColor(int) 2608 * 2609 * @attr ref android.R.styleable#TextView_textColorLink 2610 */ 2611 public final ColorStateList getLinkTextColors() { 2612 return mLinkTextColor; 2613 } 2614 2615 /** 2616 * Sets the horizontal alignment of the text and the 2617 * vertical gravity that will be used when there is extra space 2618 * in the TextView beyond what is required for the text itself. 2619 * 2620 * @see android.view.Gravity 2621 * @attr ref android.R.styleable#TextView_gravity 2622 */ 2623 public void setGravity(int gravity) { 2624 if ((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) { 2625 gravity |= Gravity.START; 2626 } 2627 if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) { 2628 gravity |= Gravity.TOP; 2629 } 2630 2631 boolean newLayout = false; 2632 2633 if ((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) != 2634 (mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK)) { 2635 newLayout = true; 2636 } 2637 2638 if (gravity != mGravity) { 2639 invalidate(); 2640 mLayoutAlignment = null; 2641 } 2642 2643 mGravity = gravity; 2644 2645 if (mLayout != null && newLayout) { 2646 // XXX this is heavy-handed because no actual content changes. 2647 int want = mLayout.getWidth(); 2648 int hintWant = mHintLayout == null ? 0 : mHintLayout.getWidth(); 2649 2650 makeNewLayout(want, hintWant, UNKNOWN_BORING, UNKNOWN_BORING, 2651 mRight - mLeft - getCompoundPaddingLeft() - 2652 getCompoundPaddingRight(), true); 2653 } 2654 } 2655 2656 /** 2657 * Returns the horizontal and vertical alignment of this TextView. 2658 * 2659 * @see android.view.Gravity 2660 * @attr ref android.R.styleable#TextView_gravity 2661 */ 2662 public int getGravity() { 2663 return mGravity; 2664 } 2665 2666 /** 2667 * @return the flags on the Paint being used to display the text. 2668 * @see Paint#getFlags 2669 */ 2670 public int getPaintFlags() { 2671 return mTextPaint.getFlags(); 2672 } 2673 2674 /** 2675 * Sets flags on the Paint being used to display the text and 2676 * reflows the text if they are different from the old flags. 2677 * @see Paint#setFlags 2678 */ 2679 @android.view.RemotableViewMethod 2680 public void setPaintFlags(int flags) { 2681 if (mTextPaint.getFlags() != flags) { 2682 mTextPaint.setFlags(flags); 2683 2684 if (mLayout != null) { 2685 nullLayouts(); 2686 requestLayout(); 2687 invalidate(); 2688 } 2689 } 2690 } 2691 2692 /** 2693 * Sets whether the text should be allowed to be wider than the 2694 * View is. If false, it will be wrapped to the width of the View. 2695 * 2696 * @attr ref android.R.styleable#TextView_scrollHorizontally 2697 */ 2698 public void setHorizontallyScrolling(boolean whether) { 2699 if (mHorizontallyScrolling != whether) { 2700 mHorizontallyScrolling = whether; 2701 2702 if (mLayout != null) { 2703 nullLayouts(); 2704 requestLayout(); 2705 invalidate(); 2706 } 2707 } 2708 } 2709 2710 /** 2711 * Returns whether the text is allowed to be wider than the View is. 2712 * If false, the text will be wrapped to the width of the View. 2713 * 2714 * @attr ref android.R.styleable#TextView_scrollHorizontally 2715 * @hide 2716 */ 2717 public boolean getHorizontallyScrolling() { 2718 return mHorizontallyScrolling; 2719 } 2720 2721 /** 2722 * Makes the TextView at least this many lines tall. 2723 * 2724 * Setting this value overrides any other (minimum) height setting. A single line TextView will 2725 * set this value to 1. 2726 * 2727 * @see #getMinLines() 2728 * 2729 * @attr ref android.R.styleable#TextView_minLines 2730 */ 2731 @android.view.RemotableViewMethod 2732 public void setMinLines(int minlines) { 2733 mMinimum = minlines; 2734 mMinMode = LINES; 2735 2736 requestLayout(); 2737 invalidate(); 2738 } 2739 2740 /** 2741 * @return the minimum number of lines displayed in this TextView, or -1 if the minimum 2742 * height was set in pixels instead using {@link #setMinHeight(int) or #setHeight(int)}. 2743 * 2744 * @see #setMinLines(int) 2745 * 2746 * @attr ref android.R.styleable#TextView_minLines 2747 */ 2748 public int getMinLines() { 2749 return mMinMode == LINES ? mMinimum : -1; 2750 } 2751 2752 /** 2753 * Makes the TextView at least this many pixels tall. 2754 * 2755 * Setting this value overrides any other (minimum) number of lines setting. 2756 * 2757 * @attr ref android.R.styleable#TextView_minHeight 2758 */ 2759 @android.view.RemotableViewMethod 2760 public void setMinHeight(int minHeight) { 2761 mMinimum = minHeight; 2762 mMinMode = PIXELS; 2763 2764 requestLayout(); 2765 invalidate(); 2766 } 2767 2768 /** 2769 * @return the minimum height of this TextView expressed in pixels, or -1 if the minimum 2770 * height was set in number of lines instead using {@link #setMinLines(int) or #setLines(int)}. 2771 * 2772 * @see #setMinHeight(int) 2773 * 2774 * @attr ref android.R.styleable#TextView_minHeight 2775 */ 2776 public int getMinHeight() { 2777 return mMinMode == PIXELS ? mMinimum : -1; 2778 } 2779 2780 /** 2781 * Makes the TextView at most this many lines tall. 2782 * 2783 * Setting this value overrides any other (maximum) height setting. 2784 * 2785 * @attr ref android.R.styleable#TextView_maxLines 2786 */ 2787 @android.view.RemotableViewMethod 2788 public void setMaxLines(int maxlines) { 2789 mMaximum = maxlines; 2790 mMaxMode = LINES; 2791 2792 requestLayout(); 2793 invalidate(); 2794 } 2795 2796 /** 2797 * @return the maximum number of lines displayed in this TextView, or -1 if the maximum 2798 * height was set in pixels instead using {@link #setMaxHeight(int) or #setHeight(int)}. 2799 * 2800 * @see #setMaxLines(int) 2801 * 2802 * @attr ref android.R.styleable#TextView_maxLines 2803 */ 2804 public int getMaxLines() { 2805 return mMaxMode == LINES ? mMaximum : -1; 2806 } 2807 2808 /** 2809 * Makes the TextView at most this many pixels tall. This option is mutually exclusive with the 2810 * {@link #setMaxLines(int)} method. 2811 * 2812 * Setting this value overrides any other (maximum) number of lines setting. 2813 * 2814 * @attr ref android.R.styleable#TextView_maxHeight 2815 */ 2816 @android.view.RemotableViewMethod 2817 public void setMaxHeight(int maxHeight) { 2818 mMaximum = maxHeight; 2819 mMaxMode = PIXELS; 2820 2821 requestLayout(); 2822 invalidate(); 2823 } 2824 2825 /** 2826 * @return the maximum height of this TextView expressed in pixels, or -1 if the maximum 2827 * height was set in number of lines instead using {@link #setMaxLines(int) or #setLines(int)}. 2828 * 2829 * @see #setMaxHeight(int) 2830 * 2831 * @attr ref android.R.styleable#TextView_maxHeight 2832 */ 2833 public int getMaxHeight() { 2834 return mMaxMode == PIXELS ? mMaximum : -1; 2835 } 2836 2837 /** 2838 * Makes the TextView exactly this many lines tall. 2839 * 2840 * Note that setting this value overrides any other (minimum / maximum) number of lines or 2841 * height setting. A single line TextView will set this value to 1. 2842 * 2843 * @attr ref android.R.styleable#TextView_lines 2844 */ 2845 @android.view.RemotableViewMethod 2846 public void setLines(int lines) { 2847 mMaximum = mMinimum = lines; 2848 mMaxMode = mMinMode = LINES; 2849 2850 requestLayout(); 2851 invalidate(); 2852 } 2853 2854 /** 2855 * Makes the TextView exactly this many pixels tall. 2856 * You could do the same thing by specifying this number in the 2857 * LayoutParams. 2858 * 2859 * Note that setting this value overrides any other (minimum / maximum) number of lines or 2860 * height setting. 2861 * 2862 * @attr ref android.R.styleable#TextView_height 2863 */ 2864 @android.view.RemotableViewMethod 2865 public void setHeight(int pixels) { 2866 mMaximum = mMinimum = pixels; 2867 mMaxMode = mMinMode = PIXELS; 2868 2869 requestLayout(); 2870 invalidate(); 2871 } 2872 2873 /** 2874 * Makes the TextView at least this many ems wide 2875 * 2876 * @attr ref android.R.styleable#TextView_minEms 2877 */ 2878 @android.view.RemotableViewMethod 2879 public void setMinEms(int minems) { 2880 mMinWidth = minems; 2881 mMinWidthMode = EMS; 2882 2883 requestLayout(); 2884 invalidate(); 2885 } 2886 2887 /** 2888 * @return the minimum width of the TextView, expressed in ems or -1 if the minimum width 2889 * was set in pixels instead (using {@link #setMinWidth(int)} or {@link #setWidth(int)}). 2890 * 2891 * @see #setMinEms(int) 2892 * @see #setEms(int) 2893 * 2894 * @attr ref android.R.styleable#TextView_minEms 2895 */ 2896 public int getMinEms() { 2897 return mMinWidthMode == EMS ? mMinWidth : -1; 2898 } 2899 2900 /** 2901 * Makes the TextView at least this many pixels wide 2902 * 2903 * @attr ref android.R.styleable#TextView_minWidth 2904 */ 2905 @android.view.RemotableViewMethod 2906 public void setMinWidth(int minpixels) { 2907 mMinWidth = minpixels; 2908 mMinWidthMode = PIXELS; 2909 2910 requestLayout(); 2911 invalidate(); 2912 } 2913 2914 /** 2915 * @return the minimum width of the TextView, in pixels or -1 if the minimum width 2916 * was set in ems instead (using {@link #setMinEms(int)} or {@link #setEms(int)}). 2917 * 2918 * @see #setMinWidth(int) 2919 * @see #setWidth(int) 2920 * 2921 * @attr ref android.R.styleable#TextView_minWidth 2922 */ 2923 public int getMinWidth() { 2924 return mMinWidthMode == PIXELS ? mMinWidth : -1; 2925 } 2926 2927 /** 2928 * Makes the TextView at most this many ems wide 2929 * 2930 * @attr ref android.R.styleable#TextView_maxEms 2931 */ 2932 @android.view.RemotableViewMethod 2933 public void setMaxEms(int maxems) { 2934 mMaxWidth = maxems; 2935 mMaxWidthMode = EMS; 2936 2937 requestLayout(); 2938 invalidate(); 2939 } 2940 2941 /** 2942 * @return the maximum width of the TextView, expressed in ems or -1 if the maximum width 2943 * was set in pixels instead (using {@link #setMaxWidth(int)} or {@link #setWidth(int)}). 2944 * 2945 * @see #setMaxEms(int) 2946 * @see #setEms(int) 2947 * 2948 * @attr ref android.R.styleable#TextView_maxEms 2949 */ 2950 public int getMaxEms() { 2951 return mMaxWidthMode == EMS ? mMaxWidth : -1; 2952 } 2953 2954 /** 2955 * Makes the TextView at most this many pixels wide 2956 * 2957 * @attr ref android.R.styleable#TextView_maxWidth 2958 */ 2959 @android.view.RemotableViewMethod 2960 public void setMaxWidth(int maxpixels) { 2961 mMaxWidth = maxpixels; 2962 mMaxWidthMode = PIXELS; 2963 2964 requestLayout(); 2965 invalidate(); 2966 } 2967 2968 /** 2969 * @return the maximum width of the TextView, in pixels or -1 if the maximum width 2970 * was set in ems instead (using {@link #setMaxEms(int)} or {@link #setEms(int)}). 2971 * 2972 * @see #setMaxWidth(int) 2973 * @see #setWidth(int) 2974 * 2975 * @attr ref android.R.styleable#TextView_maxWidth 2976 */ 2977 public int getMaxWidth() { 2978 return mMaxWidthMode == PIXELS ? mMaxWidth : -1; 2979 } 2980 2981 /** 2982 * Makes the TextView exactly this many ems wide 2983 * 2984 * @see #setMaxEms(int) 2985 * @see #setMinEms(int) 2986 * @see #getMinEms() 2987 * @see #getMaxEms() 2988 * 2989 * @attr ref android.R.styleable#TextView_ems 2990 */ 2991 @android.view.RemotableViewMethod 2992 public void setEms(int ems) { 2993 mMaxWidth = mMinWidth = ems; 2994 mMaxWidthMode = mMinWidthMode = EMS; 2995 2996 requestLayout(); 2997 invalidate(); 2998 } 2999 3000 /** 3001 * Makes the TextView exactly this many pixels wide. 3002 * You could do the same thing by specifying this number in the 3003 * LayoutParams. 3004 * 3005 * @see #setMaxWidth(int) 3006 * @see #setMinWidth(int) 3007 * @see #getMinWidth() 3008 * @see #getMaxWidth() 3009 * 3010 * @attr ref android.R.styleable#TextView_width 3011 */ 3012 @android.view.RemotableViewMethod 3013 public void setWidth(int pixels) { 3014 mMaxWidth = mMinWidth = pixels; 3015 mMaxWidthMode = mMinWidthMode = PIXELS; 3016 3017 requestLayout(); 3018 invalidate(); 3019 } 3020 3021 /** 3022 * Sets line spacing for this TextView. Each line will have its height 3023 * multiplied by <code>mult</code> and have <code>add</code> added to it. 3024 * 3025 * @attr ref android.R.styleable#TextView_lineSpacingExtra 3026 * @attr ref android.R.styleable#TextView_lineSpacingMultiplier 3027 */ 3028 public void setLineSpacing(float add, float mult) { 3029 if (mSpacingAdd != add || mSpacingMult != mult) { 3030 mSpacingAdd = add; 3031 mSpacingMult = mult; 3032 3033 if (mLayout != null) { 3034 nullLayouts(); 3035 requestLayout(); 3036 invalidate(); 3037 } 3038 } 3039 } 3040 3041 /** 3042 * Gets the line spacing multiplier 3043 * 3044 * @return the value by which each line's height is multiplied to get its actual height. 3045 * 3046 * @see #setLineSpacing(float, float) 3047 * @see #getLineSpacingExtra() 3048 * 3049 * @attr ref android.R.styleable#TextView_lineSpacingMultiplier 3050 */ 3051 public float getLineSpacingMultiplier() { 3052 return mSpacingMult; 3053 } 3054 3055 /** 3056 * Gets the line spacing extra space 3057 * 3058 * @return the extra space that is added to the height of each lines of this TextView. 3059 * 3060 * @see #setLineSpacing(float, float) 3061 * @see #getLineSpacingMultiplier() 3062 * 3063 * @attr ref android.R.styleable#TextView_lineSpacingExtra 3064 */ 3065 public float getLineSpacingExtra() { 3066 return mSpacingAdd; 3067 } 3068 3069 /** 3070 * Convenience method: Append the specified text to the TextView's 3071 * display buffer, upgrading it to BufferType.EDITABLE if it was 3072 * not already editable. 3073 */ 3074 public final void append(CharSequence text) { 3075 append(text, 0, text.length()); 3076 } 3077 3078 /** 3079 * Convenience method: Append the specified text slice to the TextView's 3080 * display buffer, upgrading it to BufferType.EDITABLE if it was 3081 * not already editable. 3082 */ 3083 public void append(CharSequence text, int start, int end) { 3084 if (!(mText instanceof Editable)) { 3085 setText(mText, BufferType.EDITABLE); 3086 } 3087 3088 ((Editable) mText).append(text, start, end); 3089 } 3090 3091 private void updateTextColors() { 3092 boolean inval = false; 3093 int color = mTextColor.getColorForState(getDrawableState(), 0); 3094 if (color != mCurTextColor) { 3095 mCurTextColor = color; 3096 inval = true; 3097 } 3098 if (mLinkTextColor != null) { 3099 color = mLinkTextColor.getColorForState(getDrawableState(), 0); 3100 if (color != mTextPaint.linkColor) { 3101 mTextPaint.linkColor = color; 3102 inval = true; 3103 } 3104 } 3105 if (mHintTextColor != null) { 3106 color = mHintTextColor.getColorForState(getDrawableState(), 0); 3107 if (color != mCurHintTextColor && mText.length() == 0) { 3108 mCurHintTextColor = color; 3109 inval = true; 3110 } 3111 } 3112 if (inval) { 3113 // Text needs to be redrawn with the new color 3114 if (mEditor != null) mEditor.invalidateTextDisplayList(); 3115 invalidate(); 3116 } 3117 } 3118 3119 @Override 3120 protected void drawableStateChanged() { 3121 super.drawableStateChanged(); 3122 if (mTextColor != null && mTextColor.isStateful() 3123 || (mHintTextColor != null && mHintTextColor.isStateful()) 3124 || (mLinkTextColor != null && mLinkTextColor.isStateful())) { 3125 updateTextColors(); 3126 } 3127 3128 final Drawables dr = mDrawables; 3129 if (dr != null) { 3130 int[] state = getDrawableState(); 3131 if (dr.mDrawableTop != null && dr.mDrawableTop.isStateful()) { 3132 dr.mDrawableTop.setState(state); 3133 } 3134 if (dr.mDrawableBottom != null && dr.mDrawableBottom.isStateful()) { 3135 dr.mDrawableBottom.setState(state); 3136 } 3137 if (dr.mDrawableLeft != null && dr.mDrawableLeft.isStateful()) { 3138 dr.mDrawableLeft.setState(state); 3139 } 3140 if (dr.mDrawableRight != null && dr.mDrawableRight.isStateful()) { 3141 dr.mDrawableRight.setState(state); 3142 } 3143 if (dr.mDrawableStart != null && dr.mDrawableStart.isStateful()) { 3144 dr.mDrawableStart.setState(state); 3145 } 3146 if (dr.mDrawableEnd != null && dr.mDrawableEnd.isStateful()) { 3147 dr.mDrawableEnd.setState(state); 3148 } 3149 } 3150 } 3151 3152 @Override 3153 public Parcelable onSaveInstanceState() { 3154 Parcelable superState = super.onSaveInstanceState(); 3155 3156 // Save state if we are forced to 3157 boolean save = mFreezesText; 3158 int start = 0; 3159 int end = 0; 3160 3161 if (mText != null) { 3162 start = getSelectionStart(); 3163 end = getSelectionEnd(); 3164 if (start >= 0 || end >= 0) { 3165 // Or save state if there is a selection 3166 save = true; 3167 } 3168 } 3169 3170 if (save) { 3171 SavedState ss = new SavedState(superState); 3172 // XXX Should also save the current scroll position! 3173 ss.selStart = start; 3174 ss.selEnd = end; 3175 3176 if (mText instanceof Spanned) { 3177 /* 3178 * Calling setText() strips off any ChangeWatchers; 3179 * strip them now to avoid leaking references. 3180 * But do it to a copy so that if there are any 3181 * further changes to the text of this view, it 3182 * won't get into an inconsistent state. 3183 */ 3184 3185 Spannable sp = new SpannableString(mText); 3186 3187 for (ChangeWatcher cw : sp.getSpans(0, sp.length(), ChangeWatcher.class)) { 3188 sp.removeSpan(cw); 3189 } 3190 3191 if (mEditor != null) { 3192 removeMisspelledSpans(sp); 3193 sp.removeSpan(mEditor.mSuggestionRangeSpan); 3194 } 3195 3196 ss.text = sp; 3197 } else { 3198 ss.text = mText.toString(); 3199 } 3200 3201 if (isFocused() && start >= 0 && end >= 0) { 3202 ss.frozenWithFocus = true; 3203 } 3204 3205 ss.error = getError(); 3206 3207 return ss; 3208 } 3209 3210 return superState; 3211 } 3212 3213 void removeMisspelledSpans(Spannable spannable) { 3214 SuggestionSpan[] suggestionSpans = spannable.getSpans(0, spannable.length(), 3215 SuggestionSpan.class); 3216 for (int i = 0; i < suggestionSpans.length; i++) { 3217 int flags = suggestionSpans[i].getFlags(); 3218 if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0 3219 && (flags & SuggestionSpan.FLAG_MISSPELLED) != 0) { 3220 spannable.removeSpan(suggestionSpans[i]); 3221 } 3222 } 3223 } 3224 3225 @Override 3226 public void onRestoreInstanceState(Parcelable state) { 3227 if (!(state instanceof SavedState)) { 3228 super.onRestoreInstanceState(state); 3229 return; 3230 } 3231 3232 SavedState ss = (SavedState)state; 3233 super.onRestoreInstanceState(ss.getSuperState()); 3234 3235 // XXX restore buffer type too, as well as lots of other stuff 3236 if (ss.text != null) { 3237 setText(ss.text); 3238 } 3239 3240 if (ss.selStart >= 0 && ss.selEnd >= 0) { 3241 if (mText instanceof Spannable) { 3242 int len = mText.length(); 3243 3244 if (ss.selStart > len || ss.selEnd > len) { 3245 String restored = ""; 3246 3247 if (ss.text != null) { 3248 restored = "(restored) "; 3249 } 3250 3251 Log.e(LOG_TAG, "Saved cursor position " + ss.selStart + 3252 "/" + ss.selEnd + " out of range for " + restored + 3253 "text " + mText); 3254 } else { 3255 Selection.setSelection((Spannable) mText, ss.selStart, ss.selEnd); 3256 3257 if (ss.frozenWithFocus) { 3258 createEditorIfNeeded("restore instance with focus"); 3259 mEditor.mFrozenWithFocus = true; 3260 } 3261 } 3262 } 3263 } 3264 3265 if (ss.error != null) { 3266 final CharSequence error = ss.error; 3267 // Display the error later, after the first layout pass 3268 post(new Runnable() { 3269 public void run() { 3270 setError(error); 3271 } 3272 }); 3273 } 3274 } 3275 3276 /** 3277 * Control whether this text view saves its entire text contents when 3278 * freezing to an icicle, in addition to dynamic state such as cursor 3279 * position. By default this is false, not saving the text. Set to true 3280 * if the text in the text view is not being saved somewhere else in 3281 * persistent storage (such as in a content provider) so that if the 3282 * view is later thawed the user will not lose their data. 3283 * 3284 * @param freezesText Controls whether a frozen icicle should include the 3285 * entire text data: true to include it, false to not. 3286 * 3287 * @attr ref android.R.styleable#TextView_freezesText 3288 */ 3289 @android.view.RemotableViewMethod 3290 public void setFreezesText(boolean freezesText) { 3291 mFreezesText = freezesText; 3292 } 3293 3294 /** 3295 * Return whether this text view is including its entire text contents 3296 * in frozen icicles. 3297 * 3298 * @return Returns true if text is included, false if it isn't. 3299 * 3300 * @see #setFreezesText 3301 */ 3302 public boolean getFreezesText() { 3303 return mFreezesText; 3304 } 3305 3306 /////////////////////////////////////////////////////////////////////////// 3307 3308 /** 3309 * Sets the Factory used to create new Editables. 3310 */ 3311 public final void setEditableFactory(Editable.Factory factory) { 3312 mEditableFactory = factory; 3313 setText(mText); 3314 } 3315 3316 /** 3317 * Sets the Factory used to create new Spannables. 3318 */ 3319 public final void setSpannableFactory(Spannable.Factory factory) { 3320 mSpannableFactory = factory; 3321 setText(mText); 3322 } 3323 3324 /** 3325 * Sets the string value of the TextView. TextView <em>does not</em> accept 3326 * HTML-like formatting, which you can do with text strings in XML resource files. 3327 * To style your strings, attach android.text.style.* objects to a 3328 * {@link android.text.SpannableString SpannableString}, or see the 3329 * <a href="{@docRoot}guide/topics/resources/available-resources.html#stringresources"> 3330 * Available Resource Types</a> documentation for an example of setting 3331 * formatted text in the XML resource file. 3332 * 3333 * @attr ref android.R.styleable#TextView_text 3334 */ 3335 @android.view.RemotableViewMethod 3336 public final void setText(CharSequence text) { 3337 setText(text, mBufferType); 3338 } 3339 3340 /** 3341 * Like {@link #setText(CharSequence)}, 3342 * except that the cursor position (if any) is retained in the new text. 3343 * 3344 * @param text The new text to place in the text view. 3345 * 3346 * @see #setText(CharSequence) 3347 */ 3348 @android.view.RemotableViewMethod 3349 public final void setTextKeepState(CharSequence text) { 3350 setTextKeepState(text, mBufferType); 3351 } 3352 3353 /** 3354 * Sets the text that this TextView is to display (see 3355 * {@link #setText(CharSequence)}) and also sets whether it is stored 3356 * in a styleable/spannable buffer and whether it is editable. 3357 * 3358 * @attr ref android.R.styleable#TextView_text 3359 * @attr ref android.R.styleable#TextView_bufferType 3360 */ 3361 public void setText(CharSequence text, BufferType type) { 3362 setText(text, type, true, 0); 3363 3364 if (mCharWrapper != null) { 3365 mCharWrapper.mChars = null; 3366 } 3367 } 3368 3369 private void setText(CharSequence text, BufferType type, 3370 boolean notifyBefore, int oldlen) { 3371 if (text == null) { 3372 text = ""; 3373 } 3374 3375 // If suggestions are not enabled, remove the suggestion spans from the text 3376 if (!isSuggestionsEnabled()) { 3377 text = removeSuggestionSpans(text); 3378 } 3379 3380 if (!mUserSetTextScaleX) mTextPaint.setTextScaleX(1.0f); 3381 3382 if (text instanceof Spanned && 3383 ((Spanned) text).getSpanStart(TextUtils.TruncateAt.MARQUEE) >= 0) { 3384 if (ViewConfiguration.get(mContext).isFadingMarqueeEnabled()) { 3385 setHorizontalFadingEdgeEnabled(true); 3386 mMarqueeFadeMode = MARQUEE_FADE_NORMAL; 3387 } else { 3388 setHorizontalFadingEdgeEnabled(false); 3389 mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS; 3390 } 3391 setEllipsize(TextUtils.TruncateAt.MARQUEE); 3392 } 3393 3394 int n = mFilters.length; 3395 for (int i = 0; i < n; i++) { 3396 CharSequence out = mFilters[i].filter(text, 0, text.length(), EMPTY_SPANNED, 0, 0); 3397 if (out != null) { 3398 text = out; 3399 } 3400 } 3401 3402 if (notifyBefore) { 3403 if (mText != null) { 3404 oldlen = mText.length(); 3405 sendBeforeTextChanged(mText, 0, oldlen, text.length()); 3406 } else { 3407 sendBeforeTextChanged("", 0, 0, text.length()); 3408 } 3409 } 3410 3411 boolean needEditableForNotification = false; 3412 3413 if (mListeners != null && mListeners.size() != 0) { 3414 needEditableForNotification = true; 3415 } 3416 3417 if (type == BufferType.EDITABLE || getKeyListener() != null || 3418 needEditableForNotification) { 3419 createEditorIfNeeded("setText with BufferType.EDITABLE or non null mInput"); 3420 Editable t = mEditableFactory.newEditable(text); 3421 text = t; 3422 setFilters(t, mFilters); 3423 InputMethodManager imm = InputMethodManager.peekInstance(); 3424 if (imm != null) imm.restartInput(this); 3425 } else if (type == BufferType.SPANNABLE || mMovement != null) { 3426 text = mSpannableFactory.newSpannable(text); 3427 } else if (!(text instanceof CharWrapper)) { 3428 text = TextUtils.stringOrSpannedString(text); 3429 } 3430 3431 if (mAutoLinkMask != 0) { 3432 Spannable s2; 3433 3434 if (type == BufferType.EDITABLE || text instanceof Spannable) { 3435 s2 = (Spannable) text; 3436 } else { 3437 s2 = mSpannableFactory.newSpannable(text); 3438 } 3439 3440 if (Linkify.addLinks(s2, mAutoLinkMask)) { 3441 text = s2; 3442 type = (type == BufferType.EDITABLE) ? BufferType.EDITABLE : BufferType.SPANNABLE; 3443 3444 /* 3445 * We must go ahead and set the text before changing the 3446 * movement method, because setMovementMethod() may call 3447 * setText() again to try to upgrade the buffer type. 3448 */ 3449 mText = text; 3450 3451 // Do not change the movement method for text that support text selection as it 3452 // would prevent an arbitrary cursor displacement. 3453 if (mLinksClickable && !textCanBeSelected()) { 3454 setMovementMethod(LinkMovementMethod.getInstance()); 3455 } 3456 } 3457 } 3458 3459 mBufferType = type; 3460 mText = text; 3461 3462 if (mTransformation == null) { 3463 mTransformed = text; 3464 } else { 3465 mTransformed = mTransformation.getTransformation(text, this); 3466 } 3467 3468 final int textLength = text.length(); 3469 3470 if (text instanceof Spannable && !mAllowTransformationLengthChange) { 3471 Spannable sp = (Spannable) text; 3472 3473 // Remove any ChangeWatchers that might have come from other TextViews. 3474 final ChangeWatcher[] watchers = sp.getSpans(0, sp.length(), ChangeWatcher.class); 3475 final int count = watchers.length; 3476 for (int i = 0; i < count; i++) { 3477 sp.removeSpan(watchers[i]); 3478 } 3479 3480 if (mChangeWatcher == null) mChangeWatcher = new ChangeWatcher(); 3481 3482 sp.setSpan(mChangeWatcher, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE | 3483 (CHANGE_WATCHER_PRIORITY << Spanned.SPAN_PRIORITY_SHIFT)); 3484 3485 if (mEditor != null) mEditor.addSpanWatchers(sp); 3486 3487 if (mTransformation != null) { 3488 sp.setSpan(mTransformation, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE); 3489 } 3490 3491 if (mMovement != null) { 3492 mMovement.initialize(this, (Spannable) text); 3493 3494 /* 3495 * Initializing the movement method will have set the 3496 * selection, so reset mSelectionMoved to keep that from 3497 * interfering with the normal on-focus selection-setting. 3498 */ 3499 if (mEditor != null) mEditor.mSelectionMoved = false; 3500 } 3501 } 3502 3503 if (mLayout != null) { 3504 checkForRelayout(); 3505 } 3506 3507 sendOnTextChanged(text, 0, oldlen, textLength); 3508 onTextChanged(text, 0, oldlen, textLength); 3509 3510 if (needEditableForNotification) { 3511 sendAfterTextChanged((Editable) text); 3512 } 3513 3514 // SelectionModifierCursorController depends on textCanBeSelected, which depends on text 3515 if (mEditor != null) mEditor.prepareCursorControllers(); 3516 } 3517 3518 /** 3519 * Sets the TextView to display the specified slice of the specified 3520 * char array. You must promise that you will not change the contents 3521 * of the array except for right before another call to setText(), 3522 * since the TextView has no way to know that the text 3523 * has changed and that it needs to invalidate and re-layout. 3524 */ 3525 public final void setText(char[] text, int start, int len) { 3526 int oldlen = 0; 3527 3528 if (start < 0 || len < 0 || start + len > text.length) { 3529 throw new IndexOutOfBoundsException(start + ", " + len); 3530 } 3531 3532 /* 3533 * We must do the before-notification here ourselves because if 3534 * the old text is a CharWrapper we destroy it before calling 3535 * into the normal path. 3536 */ 3537 if (mText != null) { 3538 oldlen = mText.length(); 3539 sendBeforeTextChanged(mText, 0, oldlen, len); 3540 } else { 3541 sendBeforeTextChanged("", 0, 0, len); 3542 } 3543 3544 if (mCharWrapper == null) { 3545 mCharWrapper = new CharWrapper(text, start, len); 3546 } else { 3547 mCharWrapper.set(text, start, len); 3548 } 3549 3550 setText(mCharWrapper, mBufferType, false, oldlen); 3551 } 3552 3553 /** 3554 * Like {@link #setText(CharSequence, android.widget.TextView.BufferType)}, 3555 * except that the cursor position (if any) is retained in the new text. 3556 * 3557 * @see #setText(CharSequence, android.widget.TextView.BufferType) 3558 */ 3559 public final void setTextKeepState(CharSequence text, BufferType type) { 3560 int start = getSelectionStart(); 3561 int end = getSelectionEnd(); 3562 int len = text.length(); 3563 3564 setText(text, type); 3565 3566 if (start >= 0 || end >= 0) { 3567 if (mText instanceof Spannable) { 3568 Selection.setSelection((Spannable) mText, 3569 Math.max(0, Math.min(start, len)), 3570 Math.max(0, Math.min(end, len))); 3571 } 3572 } 3573 } 3574 3575 @android.view.RemotableViewMethod 3576 public final void setText(int resid) { 3577 setText(getContext().getResources().getText(resid)); 3578 } 3579 3580 public final void setText(int resid, BufferType type) { 3581 setText(getContext().getResources().getText(resid), type); 3582 } 3583 3584 /** 3585 * Sets the text to be displayed when the text of the TextView is empty. 3586 * Null means to use the normal empty text. The hint does not currently 3587 * participate in determining the size of the view. 3588 * 3589 * @attr ref android.R.styleable#TextView_hint 3590 */ 3591 @android.view.RemotableViewMethod 3592 public final void setHint(CharSequence hint) { 3593 mHint = TextUtils.stringOrSpannedString(hint); 3594 3595 if (mLayout != null) { 3596 checkForRelayout(); 3597 } 3598 3599 if (mText.length() == 0) { 3600 invalidate(); 3601 } 3602 3603 // Invalidate display list if hint is currently used 3604 if (mEditor != null && mText.length() == 0 && mHint != null) { 3605 mEditor.invalidateTextDisplayList(); 3606 } 3607 } 3608 3609 /** 3610 * Sets the text to be displayed when the text of the TextView is empty, 3611 * from a resource. 3612 * 3613 * @attr ref android.R.styleable#TextView_hint 3614 */ 3615 @android.view.RemotableViewMethod 3616 public final void setHint(int resid) { 3617 setHint(getContext().getResources().getText(resid)); 3618 } 3619 3620 /** 3621 * Returns the hint that is displayed when the text of the TextView 3622 * is empty. 3623 * 3624 * @attr ref android.R.styleable#TextView_hint 3625 */ 3626 @ViewDebug.CapturedViewProperty 3627 public CharSequence getHint() { 3628 return mHint; 3629 } 3630 3631 boolean isSingleLine() { 3632 return mSingleLine; 3633 } 3634 3635 private static boolean isMultilineInputType(int type) { 3636 return (type & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE)) == 3637 (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE); 3638 } 3639 3640 /** 3641 * Removes the suggestion spans. 3642 */ 3643 CharSequence removeSuggestionSpans(CharSequence text) { 3644 if (text instanceof Spanned) { 3645 Spannable spannable; 3646 if (text instanceof Spannable) { 3647 spannable = (Spannable) text; 3648 } else { 3649 spannable = new SpannableString(text); 3650 text = spannable; 3651 } 3652 3653 SuggestionSpan[] spans = spannable.getSpans(0, text.length(), SuggestionSpan.class); 3654 for (int i = 0; i < spans.length; i++) { 3655 spannable.removeSpan(spans[i]); 3656 } 3657 } 3658 return text; 3659 } 3660 3661 /** 3662 * Set the type of the content with a constant as defined for {@link EditorInfo#inputType}. This 3663 * will take care of changing the key listener, by calling {@link #setKeyListener(KeyListener)}, 3664 * to match the given content type. If the given content type is {@link EditorInfo#TYPE_NULL} 3665 * then a soft keyboard will not be displayed for this text view. 3666 * 3667 * Note that the maximum number of displayed lines (see {@link #setMaxLines(int)}) will be 3668 * modified if you change the {@link EditorInfo#TYPE_TEXT_FLAG_MULTI_LINE} flag of the input 3669 * type. 3670 * 3671 * @see #getInputType() 3672 * @see #setRawInputType(int) 3673 * @see android.text.InputType 3674 * @attr ref android.R.styleable#TextView_inputType 3675 */ 3676 public void setInputType(int type) { 3677 final boolean wasPassword = isPasswordInputType(getInputType()); 3678 final boolean wasVisiblePassword = isVisiblePasswordInputType(getInputType()); 3679 setInputType(type, false); 3680 final boolean isPassword = isPasswordInputType(type); 3681 final boolean isVisiblePassword = isVisiblePasswordInputType(type); 3682 boolean forceUpdate = false; 3683 if (isPassword) { 3684 setTransformationMethod(PasswordTransformationMethod.getInstance()); 3685 setTypefaceByIndex(MONOSPACE, 0); 3686 } else if (isVisiblePassword) { 3687 if (mTransformation == PasswordTransformationMethod.getInstance()) { 3688 forceUpdate = true; 3689 } 3690 setTypefaceByIndex(MONOSPACE, 0); 3691 } else if (wasPassword || wasVisiblePassword) { 3692 // not in password mode, clean up typeface and transformation 3693 setTypefaceByIndex(-1, -1); 3694 if (mTransformation == PasswordTransformationMethod.getInstance()) { 3695 forceUpdate = true; 3696 } 3697 } 3698 3699 boolean singleLine = !isMultilineInputType(type); 3700 3701 // We need to update the single line mode if it has changed or we 3702 // were previously in password mode. 3703 if (mSingleLine != singleLine || forceUpdate) { 3704 // Change single line mode, but only change the transformation if 3705 // we are not in password mode. 3706 applySingleLine(singleLine, !isPassword, true); 3707 } 3708 3709 if (!isSuggestionsEnabled()) { 3710 mText = removeSuggestionSpans(mText); 3711 } 3712 3713 InputMethodManager imm = InputMethodManager.peekInstance(); 3714 if (imm != null) imm.restartInput(this); 3715 } 3716 3717 /** 3718 * It would be better to rely on the input type for everything. A password inputType should have 3719 * a password transformation. We should hence use isPasswordInputType instead of this method. 3720 * 3721 * We should: 3722 * - Call setInputType in setKeyListener instead of changing the input type directly (which 3723 * would install the correct transformation). 3724 * - Refuse the installation of a non-password transformation in setTransformation if the input 3725 * type is password. 3726 * 3727 * However, this is like this for legacy reasons and we cannot break existing apps. This method 3728 * is useful since it matches what the user can see (obfuscated text or not). 3729 * 3730 * @return true if the current transformation method is of the password type. 3731 */ 3732 private boolean hasPasswordTransformationMethod() { 3733 return mTransformation instanceof PasswordTransformationMethod; 3734 } 3735 3736 private static boolean isPasswordInputType(int inputType) { 3737 final int variation = 3738 inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION); 3739 return variation 3740 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD) 3741 || variation 3742 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD) 3743 || variation 3744 == (EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD); 3745 } 3746 3747 private static boolean isVisiblePasswordInputType(int inputType) { 3748 final int variation = 3749 inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION); 3750 return variation 3751 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); 3752 } 3753 3754 /** 3755 * Directly change the content type integer of the text view, without 3756 * modifying any other state. 3757 * @see #setInputType(int) 3758 * @see android.text.InputType 3759 * @attr ref android.R.styleable#TextView_inputType 3760 */ 3761 public void setRawInputType(int type) { 3762 if (type == InputType.TYPE_NULL && mEditor == null) return; //TYPE_NULL is the default value 3763 createEditorIfNeeded("non null input type"); 3764 mEditor.mInputType = type; 3765 } 3766 3767 private void setInputType(int type, boolean direct) { 3768 final int cls = type & EditorInfo.TYPE_MASK_CLASS; 3769 KeyListener input; 3770 if (cls == EditorInfo.TYPE_CLASS_TEXT) { 3771 boolean autotext = (type & EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT) != 0; 3772 TextKeyListener.Capitalize cap; 3773 if ((type & EditorInfo.TYPE_TEXT_FLAG_CAP_CHARACTERS) != 0) { 3774 cap = TextKeyListener.Capitalize.CHARACTERS; 3775 } else if ((type & EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS) != 0) { 3776 cap = TextKeyListener.Capitalize.WORDS; 3777 } else if ((type & EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES) != 0) { 3778 cap = TextKeyListener.Capitalize.SENTENCES; 3779 } else { 3780 cap = TextKeyListener.Capitalize.NONE; 3781 } 3782 input = TextKeyListener.getInstance(autotext, cap); 3783 } else if (cls == EditorInfo.TYPE_CLASS_NUMBER) { 3784 input = DigitsKeyListener.getInstance( 3785 (type & EditorInfo.TYPE_NUMBER_FLAG_SIGNED) != 0, 3786 (type & EditorInfo.TYPE_NUMBER_FLAG_DECIMAL) != 0); 3787 } else if (cls == EditorInfo.TYPE_CLASS_DATETIME) { 3788 switch (type & EditorInfo.TYPE_MASK_VARIATION) { 3789 case EditorInfo.TYPE_DATETIME_VARIATION_DATE: 3790 input = DateKeyListener.getInstance(); 3791 break; 3792 case EditorInfo.TYPE_DATETIME_VARIATION_TIME: 3793 input = TimeKeyListener.getInstance(); 3794 break; 3795 default: 3796 input = DateTimeKeyListener.getInstance(); 3797 break; 3798 } 3799 } else if (cls == EditorInfo.TYPE_CLASS_PHONE) { 3800 input = DialerKeyListener.getInstance(); 3801 } else { 3802 input = TextKeyListener.getInstance(); 3803 } 3804 setRawInputType(type); 3805 if (direct) { 3806 createEditorIfNeeded("setInputType"); 3807 mEditor.mKeyListener = input; 3808 } else { 3809 setKeyListenerOnly(input); 3810 } 3811 } 3812 3813 /** 3814 * Get the type of the editable content. 3815 * 3816 * @see #setInputType(int) 3817 * @see android.text.InputType 3818 */ 3819 public int getInputType() { 3820 return mEditor == null ? EditorInfo.TYPE_NULL : mEditor.mInputType; 3821 } 3822 3823 /** 3824 * Change the editor type integer associated with the text view, which 3825 * will be reported to an IME with {@link EditorInfo#imeOptions} when it 3826 * has focus. 3827 * @see #getImeOptions 3828 * @see android.view.inputmethod.EditorInfo 3829 * @attr ref android.R.styleable#TextView_imeOptions 3830 */ 3831 public void setImeOptions(int imeOptions) { 3832 createEditorIfNeeded("IME options specified"); 3833 mEditor.createInputContentTypeIfNeeded(); 3834 mEditor.mInputContentType.imeOptions = imeOptions; 3835 } 3836 3837 /** 3838 * Get the type of the IME editor. 3839 * 3840 * @see #setImeOptions(int) 3841 * @see android.view.inputmethod.EditorInfo 3842 */ 3843 public int getImeOptions() { 3844 return mEditor != null && mEditor.mInputContentType != null 3845 ? mEditor.mInputContentType.imeOptions : EditorInfo.IME_NULL; 3846 } 3847 3848 /** 3849 * Change the custom IME action associated with the text view, which 3850 * will be reported to an IME with {@link EditorInfo#actionLabel} 3851 * and {@link EditorInfo#actionId} when it has focus. 3852 * @see #getImeActionLabel 3853 * @see #getImeActionId 3854 * @see android.view.inputmethod.EditorInfo 3855 * @attr ref android.R.styleable#TextView_imeActionLabel 3856 * @attr ref android.R.styleable#TextView_imeActionId 3857 */ 3858 public void setImeActionLabel(CharSequence label, int actionId) { 3859 createEditorIfNeeded("IME action label specified"); 3860 mEditor.createInputContentTypeIfNeeded(); 3861 mEditor.mInputContentType.imeActionLabel = label; 3862 mEditor.mInputContentType.imeActionId = actionId; 3863 } 3864 3865 /** 3866 * Get the IME action label previous set with {@link #setImeActionLabel}. 3867 * 3868 * @see #setImeActionLabel 3869 * @see android.view.inputmethod.EditorInfo 3870 */ 3871 public CharSequence getImeActionLabel() { 3872 return mEditor != null && mEditor.mInputContentType != null 3873 ? mEditor.mInputContentType.imeActionLabel : null; 3874 } 3875 3876 /** 3877 * Get the IME action ID previous set with {@link #setImeActionLabel}. 3878 * 3879 * @see #setImeActionLabel 3880 * @see android.view.inputmethod.EditorInfo 3881 */ 3882 public int getImeActionId() { 3883 return mEditor != null && mEditor.mInputContentType != null 3884 ? mEditor.mInputContentType.imeActionId : 0; 3885 } 3886 3887 /** 3888 * Set a special listener to be called when an action is performed 3889 * on the text view. This will be called when the enter key is pressed, 3890 * or when an action supplied to the IME is selected by the user. Setting 3891 * this means that the normal hard key event will not insert a newline 3892 * into the text view, even if it is multi-line; holding down the ALT 3893 * modifier will, however, allow the user to insert a newline character. 3894 */ 3895 public void setOnEditorActionListener(OnEditorActionListener l) { 3896 createEditorIfNeeded("Editor action listener set"); 3897 mEditor.createInputContentTypeIfNeeded(); 3898 mEditor.mInputContentType.onEditorActionListener = l; 3899 } 3900 3901 /** 3902 * Called when an attached input method calls 3903 * {@link InputConnection#performEditorAction(int) 3904 * InputConnection.performEditorAction()} 3905 * for this text view. The default implementation will call your action 3906 * listener supplied to {@link #setOnEditorActionListener}, or perform 3907 * a standard operation for {@link EditorInfo#IME_ACTION_NEXT 3908 * EditorInfo.IME_ACTION_NEXT}, {@link EditorInfo#IME_ACTION_PREVIOUS 3909 * EditorInfo.IME_ACTION_PREVIOUS}, or {@link EditorInfo#IME_ACTION_DONE 3910 * EditorInfo.IME_ACTION_DONE}. 3911 * 3912 * <p>For backwards compatibility, if no IME options have been set and the 3913 * text view would not normally advance focus on enter, then 3914 * the NEXT and DONE actions received here will be turned into an enter 3915 * key down/up pair to go through the normal key handling. 3916 * 3917 * @param actionCode The code of the action being performed. 3918 * 3919 * @see #setOnEditorActionListener 3920 */ 3921 public void onEditorAction(int actionCode) { 3922 final Editor.InputContentType ict = mEditor == null ? null : mEditor.mInputContentType; 3923 if (ict != null) { 3924 if (ict.onEditorActionListener != null) { 3925 if (ict.onEditorActionListener.onEditorAction(this, 3926 actionCode, null)) { 3927 return; 3928 } 3929 } 3930 3931 // This is the handling for some default action. 3932 // Note that for backwards compatibility we don't do this 3933 // default handling if explicit ime options have not been given, 3934 // instead turning this into the normal enter key codes that an 3935 // app may be expecting. 3936 if (actionCode == EditorInfo.IME_ACTION_NEXT) { 3937 View v = focusSearch(FOCUS_FORWARD); 3938 if (v != null) { 3939 if (!v.requestFocus(FOCUS_FORWARD)) { 3940 throw new IllegalStateException("focus search returned a view " + 3941 "that wasn't able to take focus!"); 3942 } 3943 } 3944 return; 3945 3946 } else if (actionCode == EditorInfo.IME_ACTION_PREVIOUS) { 3947 View v = focusSearch(FOCUS_BACKWARD); 3948 if (v != null) { 3949 if (!v.requestFocus(FOCUS_BACKWARD)) { 3950 throw new IllegalStateException("focus search returned a view " + 3951 "that wasn't able to take focus!"); 3952 } 3953 } 3954 return; 3955 3956 } else if (actionCode == EditorInfo.IME_ACTION_DONE) { 3957 InputMethodManager imm = InputMethodManager.peekInstance(); 3958 if (imm != null && imm.isActive(this)) { 3959 imm.hideSoftInputFromWindow(getWindowToken(), 0); 3960 } 3961 return; 3962 } 3963 } 3964 3965 ViewRootImpl viewRootImpl = getViewRootImpl(); 3966 if (viewRootImpl != null) { 3967 long eventTime = SystemClock.uptimeMillis(); 3968 viewRootImpl.dispatchKeyFromIme( 3969 new KeyEvent(eventTime, eventTime, 3970 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER, 0, 0, 3971 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 3972 KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE 3973 | KeyEvent.FLAG_EDITOR_ACTION)); 3974 viewRootImpl.dispatchKeyFromIme( 3975 new KeyEvent(SystemClock.uptimeMillis(), eventTime, 3976 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER, 0, 0, 3977 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 3978 KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE 3979 | KeyEvent.FLAG_EDITOR_ACTION)); 3980 } 3981 } 3982 3983 /** 3984 * Set the private content type of the text, which is the 3985 * {@link EditorInfo#privateImeOptions EditorInfo.privateImeOptions} 3986 * field that will be filled in when creating an input connection. 3987 * 3988 * @see #getPrivateImeOptions() 3989 * @see EditorInfo#privateImeOptions 3990 * @attr ref android.R.styleable#TextView_privateImeOptions 3991 */ 3992 public void setPrivateImeOptions(String type) { 3993 createEditorIfNeeded("Private IME option set"); 3994 mEditor.createInputContentTypeIfNeeded(); 3995 mEditor.mInputContentType.privateImeOptions = type; 3996 } 3997 3998 /** 3999 * Get the private type of the content. 4000 * 4001 * @see #setPrivateImeOptions(String) 4002 * @see EditorInfo#privateImeOptions 4003 */ 4004 public String getPrivateImeOptions() { 4005 return mEditor != null && mEditor.mInputContentType != null 4006 ? mEditor.mInputContentType.privateImeOptions : null; 4007 } 4008 4009 /** 4010 * Set the extra input data of the text, which is the 4011 * {@link EditorInfo#extras TextBoxAttribute.extras} 4012 * Bundle that will be filled in when creating an input connection. The 4013 * given integer is the resource ID of an XML resource holding an 4014 * {@link android.R.styleable#InputExtras <input-extras>} XML tree. 4015 * 4016 * @see #getInputExtras(boolean) 4017 * @see EditorInfo#extras 4018 * @attr ref android.R.styleable#TextView_editorExtras 4019 */ 4020 public void setInputExtras(int xmlResId) throws XmlPullParserException, IOException { 4021 createEditorIfNeeded("Input extra set"); 4022 XmlResourceParser parser = getResources().getXml(xmlResId); 4023 mEditor.createInputContentTypeIfNeeded(); 4024 mEditor.mInputContentType.extras = new Bundle(); 4025 getResources().parseBundleExtras(parser, mEditor.mInputContentType.extras); 4026 } 4027 4028 /** 4029 * Retrieve the input extras currently associated with the text view, which 4030 * can be viewed as well as modified. 4031 * 4032 * @param create If true, the extras will be created if they don't already 4033 * exist. Otherwise, null will be returned if none have been created. 4034 * @see #setInputExtras(int) 4035 * @see EditorInfo#extras 4036 * @attr ref android.R.styleable#TextView_editorExtras 4037 */ 4038 public Bundle getInputExtras(boolean create) { 4039 if (mEditor == null && !create) return null; 4040 createEditorIfNeeded("get Input extra"); 4041 if (mEditor.mInputContentType == null) { 4042 if (!create) return null; 4043 mEditor.createInputContentTypeIfNeeded(); 4044 } 4045 if (mEditor.mInputContentType.extras == null) { 4046 if (!create) return null; 4047 mEditor.mInputContentType.extras = new Bundle(); 4048 } 4049 return mEditor.mInputContentType.extras; 4050 } 4051 4052 /** 4053 * Returns the error message that was set to be displayed with 4054 * {@link #setError}, or <code>null</code> if no error was set 4055 * or if it the error was cleared by the widget after user input. 4056 */ 4057 public CharSequence getError() { 4058 return mEditor == null ? null : mEditor.mError; 4059 } 4060 4061 /** 4062 * Sets the right-hand compound drawable of the TextView to the "error" 4063 * icon and sets an error message that will be displayed in a popup when 4064 * the TextView has focus. The icon and error message will be reset to 4065 * null when any key events cause changes to the TextView's text. If the 4066 * <code>error</code> is <code>null</code>, the error message and icon 4067 * will be cleared. 4068 */ 4069 @android.view.RemotableViewMethod 4070 public void setError(CharSequence error) { 4071 if (error == null) { 4072 setError(null, null); 4073 } else { 4074 Drawable dr = getContext().getResources(). 4075 getDrawable(com.android.internal.R.drawable.indicator_input_error); 4076 4077 dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight()); 4078 setError(error, dr); 4079 } 4080 } 4081 4082 /** 4083 * Sets the right-hand compound drawable of the TextView to the specified 4084 * icon and sets an error message that will be displayed in a popup when 4085 * the TextView has focus. The icon and error message will be reset to 4086 * null when any key events cause changes to the TextView's text. The 4087 * drawable must already have had {@link Drawable#setBounds} set on it. 4088 * If the <code>error</code> is <code>null</code>, the error message will 4089 * be cleared (and you should provide a <code>null</code> icon as well). 4090 */ 4091 public void setError(CharSequence error, Drawable icon) { 4092 createEditorIfNeeded("setError"); 4093 mEditor.setError(error, icon); 4094 } 4095 4096 @Override 4097 protected boolean setFrame(int l, int t, int r, int b) { 4098 boolean result = super.setFrame(l, t, r, b); 4099 4100 if (mEditor != null) mEditor.setFrame(); 4101 4102 restartMarqueeIfNeeded(); 4103 4104 return result; 4105 } 4106 4107 private void restartMarqueeIfNeeded() { 4108 if (mRestartMarquee && mEllipsize == TextUtils.TruncateAt.MARQUEE) { 4109 mRestartMarquee = false; 4110 startMarquee(); 4111 } 4112 } 4113 4114 /** 4115 * Sets the list of input filters that will be used if the buffer is 4116 * Editable. Has no effect otherwise. 4117 * 4118 * @attr ref android.R.styleable#TextView_maxLength 4119 */ 4120 public void setFilters(InputFilter[] filters) { 4121 if (filters == null) { 4122 throw new IllegalArgumentException(); 4123 } 4124 4125 mFilters = filters; 4126 4127 if (mText instanceof Editable) { 4128 setFilters((Editable) mText, filters); 4129 } 4130 } 4131 4132 /** 4133 * Sets the list of input filters on the specified Editable, 4134 * and includes mInput in the list if it is an InputFilter. 4135 */ 4136 private void setFilters(Editable e, InputFilter[] filters) { 4137 if (mEditor != null && mEditor.mKeyListener instanceof InputFilter) { 4138 InputFilter[] nf = new InputFilter[filters.length + 1]; 4139 4140 System.arraycopy(filters, 0, nf, 0, filters.length); 4141 nf[filters.length] = (InputFilter) mEditor.mKeyListener; 4142 4143 e.setFilters(nf); 4144 } else { 4145 e.setFilters(filters); 4146 } 4147 } 4148 4149 /** 4150 * Returns the current list of input filters. 4151 * 4152 * @attr ref android.R.styleable#TextView_maxLength 4153 */ 4154 public InputFilter[] getFilters() { 4155 return mFilters; 4156 } 4157 4158 ///////////////////////////////////////////////////////////////////////// 4159 4160 int getVerticalOffset(boolean forceNormal) { 4161 int voffset = 0; 4162 final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; 4163 4164 Layout l = mLayout; 4165 if (!forceNormal && mText.length() == 0 && mHintLayout != null) { 4166 l = mHintLayout; 4167 } 4168 4169 if (gravity != Gravity.TOP) { 4170 int boxht; 4171 4172 if (l == mHintLayout) { 4173 boxht = getMeasuredHeight() - getCompoundPaddingTop() - 4174 getCompoundPaddingBottom(); 4175 } else { 4176 boxht = getMeasuredHeight() - getExtendedPaddingTop() - 4177 getExtendedPaddingBottom(); 4178 } 4179 int textht = l.getHeight(); 4180 4181 if (textht < boxht) { 4182 if (gravity == Gravity.BOTTOM) 4183 voffset = boxht - textht; 4184 else // (gravity == Gravity.CENTER_VERTICAL) 4185 voffset = (boxht - textht) >> 1; 4186 } 4187 } 4188 return voffset; 4189 } 4190 4191 private int getBottomVerticalOffset(boolean forceNormal) { 4192 int voffset = 0; 4193 final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; 4194 4195 Layout l = mLayout; 4196 if (!forceNormal && mText.length() == 0 && mHintLayout != null) { 4197 l = mHintLayout; 4198 } 4199 4200 if (gravity != Gravity.BOTTOM) { 4201 int boxht; 4202 4203 if (l == mHintLayout) { 4204 boxht = getMeasuredHeight() - getCompoundPaddingTop() - 4205 getCompoundPaddingBottom(); 4206 } else { 4207 boxht = getMeasuredHeight() - getExtendedPaddingTop() - 4208 getExtendedPaddingBottom(); 4209 } 4210 int textht = l.getHeight(); 4211 4212 if (textht < boxht) { 4213 if (gravity == Gravity.TOP) 4214 voffset = boxht - textht; 4215 else // (gravity == Gravity.CENTER_VERTICAL) 4216 voffset = (boxht - textht) >> 1; 4217 } 4218 } 4219 return voffset; 4220 } 4221 4222 void invalidateCursorPath() { 4223 if (mHighlightPathBogus) { 4224 invalidateCursor(); 4225 } else { 4226 final int horizontalPadding = getCompoundPaddingLeft(); 4227 final int verticalPadding = getExtendedPaddingTop() + getVerticalOffset(true); 4228 4229 if (mEditor.mCursorCount == 0) { 4230 synchronized (TEMP_RECTF) { 4231 /* 4232 * The reason for this concern about the thickness of the 4233 * cursor and doing the floor/ceil on the coordinates is that 4234 * some EditTexts (notably textfields in the Browser) have 4235 * anti-aliased text where not all the characters are 4236 * necessarily at integer-multiple locations. This should 4237 * make sure the entire cursor gets invalidated instead of 4238 * sometimes missing half a pixel. 4239 */ 4240 float thick = FloatMath.ceil(mTextPaint.getStrokeWidth()); 4241 if (thick < 1.0f) { 4242 thick = 1.0f; 4243 } 4244 4245 thick /= 2.0f; 4246 4247 // mHighlightPath is guaranteed to be non null at that point. 4248 mHighlightPath.computeBounds(TEMP_RECTF, false); 4249 4250 invalidate((int) FloatMath.floor(horizontalPadding + TEMP_RECTF.left - thick), 4251 (int) FloatMath.floor(verticalPadding + TEMP_RECTF.top - thick), 4252 (int) FloatMath.ceil(horizontalPadding + TEMP_RECTF.right + thick), 4253 (int) FloatMath.ceil(verticalPadding + TEMP_RECTF.bottom + thick)); 4254 } 4255 } else { 4256 for (int i = 0; i < mEditor.mCursorCount; i++) { 4257 Rect bounds = mEditor.mCursorDrawable[i].getBounds(); 4258 invalidate(bounds.left + horizontalPadding, bounds.top + verticalPadding, 4259 bounds.right + horizontalPadding, bounds.bottom + verticalPadding); 4260 } 4261 } 4262 } 4263 } 4264 4265 void invalidateCursor() { 4266 int where = getSelectionEnd(); 4267 4268 invalidateCursor(where, where, where); 4269 } 4270 4271 private void invalidateCursor(int a, int b, int c) { 4272 if (a >= 0 || b >= 0 || c >= 0) { 4273 int start = Math.min(Math.min(a, b), c); 4274 int end = Math.max(Math.max(a, b), c); 4275 invalidateRegion(start, end, true /* Also invalidates blinking cursor */); 4276 } 4277 } 4278 4279 /** 4280 * Invalidates the region of text enclosed between the start and end text offsets. 4281 */ 4282 void invalidateRegion(int start, int end, boolean invalidateCursor) { 4283 if (mLayout == null) { 4284 invalidate(); 4285 } else { 4286 int lineStart = mLayout.getLineForOffset(start); 4287 int top = mLayout.getLineTop(lineStart); 4288 4289 // This is ridiculous, but the descent from the line above 4290 // can hang down into the line we really want to redraw, 4291 // so we have to invalidate part of the line above to make 4292 // sure everything that needs to be redrawn really is. 4293 // (But not the whole line above, because that would cause 4294 // the same problem with the descenders on the line above it!) 4295 if (lineStart > 0) { 4296 top -= mLayout.getLineDescent(lineStart - 1); 4297 } 4298 4299 int lineEnd; 4300 4301 if (start == end) 4302 lineEnd = lineStart; 4303 else 4304 lineEnd = mLayout.getLineForOffset(end); 4305 4306 int bottom = mLayout.getLineBottom(lineEnd); 4307 4308 // mEditor can be null in case selection is set programmatically. 4309 if (invalidateCursor && mEditor != null) { 4310 for (int i = 0; i < mEditor.mCursorCount; i++) { 4311 Rect bounds = mEditor.mCursorDrawable[i].getBounds(); 4312 top = Math.min(top, bounds.top); 4313 bottom = Math.max(bottom, bounds.bottom); 4314 } 4315 } 4316 4317 final int compoundPaddingLeft = getCompoundPaddingLeft(); 4318 final int verticalPadding = getExtendedPaddingTop() + getVerticalOffset(true); 4319 4320 int left, right; 4321 if (lineStart == lineEnd && !invalidateCursor) { 4322 left = (int) mLayout.getPrimaryHorizontal(start); 4323 right = (int) (mLayout.getPrimaryHorizontal(end) + 1.0); 4324 left += compoundPaddingLeft; 4325 right += compoundPaddingLeft; 4326 } else { 4327 // Rectangle bounding box when the region spans several lines 4328 left = compoundPaddingLeft; 4329 right = getWidth() - getCompoundPaddingRight(); 4330 } 4331 4332 invalidate(mScrollX + left, verticalPadding + top, 4333 mScrollX + right, verticalPadding + bottom); 4334 } 4335 } 4336 4337 private void registerForPreDraw() { 4338 if (!mPreDrawRegistered) { 4339 getViewTreeObserver().addOnPreDrawListener(this); 4340 mPreDrawRegistered = true; 4341 } 4342 } 4343 4344 /** 4345 * {@inheritDoc} 4346 */ 4347 public boolean onPreDraw() { 4348 if (mLayout == null) { 4349 assumeLayout(); 4350 } 4351 4352 boolean changed = false; 4353 4354 if (mMovement != null) { 4355 /* This code also provides auto-scrolling when a cursor is moved using a 4356 * CursorController (insertion point or selection limits). 4357 * For selection, ensure start or end is visible depending on controller's state. 4358 */ 4359 int curs = getSelectionEnd(); 4360 // Do not create the controller if it is not already created. 4361 if (mEditor != null && mEditor.mSelectionModifierCursorController != null && 4362 mEditor.mSelectionModifierCursorController.isSelectionStartDragged()) { 4363 curs = getSelectionStart(); 4364 } 4365 4366 /* 4367 * TODO: This should really only keep the end in view if 4368 * it already was before the text changed. I'm not sure 4369 * of a good way to tell from here if it was. 4370 */ 4371 if (curs < 0 && (mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { 4372 curs = mText.length(); 4373 } 4374 4375 if (curs >= 0) { 4376 changed = bringPointIntoView(curs); 4377 } 4378 } else { 4379 changed = bringTextIntoView(); 4380 } 4381 4382 // This has to be checked here since: 4383 // - onFocusChanged cannot start it when focus is given to a view with selected text (after 4384 // a screen rotation) since layout is not yet initialized at that point. 4385 if (mEditor != null && mEditor.mCreatedWithASelection) { 4386 mEditor.startSelectionActionMode(); 4387 mEditor.mCreatedWithASelection = false; 4388 } 4389 4390 // Phone specific code (there is no ExtractEditText on tablets). 4391 // ExtractEditText does not call onFocus when it is displayed, and mHasSelectionOnFocus can 4392 // not be set. Do the test here instead. 4393 if (this instanceof ExtractEditText && hasSelection() && mEditor != null) { 4394 mEditor.startSelectionActionMode(); 4395 } 4396 4397 getViewTreeObserver().removeOnPreDrawListener(this); 4398 mPreDrawRegistered = false; 4399 4400 return !changed; 4401 } 4402 4403 @Override 4404 protected void onAttachedToWindow() { 4405 super.onAttachedToWindow(); 4406 4407 mTemporaryDetach = false; 4408 4409 // Resolve drawables as the layout direction has been resolved 4410 resolveDrawables(); 4411 4412 if (mEditor != null) mEditor.onAttachedToWindow(); 4413 } 4414 4415 @Override 4416 protected void onDetachedFromWindow() { 4417 super.onDetachedFromWindow(); 4418 4419 if (mPreDrawRegistered) { 4420 getViewTreeObserver().removeOnPreDrawListener(this); 4421 mPreDrawRegistered = false; 4422 } 4423 4424 resetResolvedDrawables(); 4425 4426 if (mEditor != null) mEditor.onDetachedFromWindow(); 4427 } 4428 4429 @Override 4430 public void onScreenStateChanged(int screenState) { 4431 super.onScreenStateChanged(screenState); 4432 if (mEditor != null) mEditor.onScreenStateChanged(screenState); 4433 } 4434 4435 @Override 4436 protected boolean isPaddingOffsetRequired() { 4437 return mShadowRadius != 0 || mDrawables != null; 4438 } 4439 4440 @Override 4441 protected int getLeftPaddingOffset() { 4442 return getCompoundPaddingLeft() - mPaddingLeft + 4443 (int) Math.min(0, mShadowDx - mShadowRadius); 4444 } 4445 4446 @Override 4447 protected int getTopPaddingOffset() { 4448 return (int) Math.min(0, mShadowDy - mShadowRadius); 4449 } 4450 4451 @Override 4452 protected int getBottomPaddingOffset() { 4453 return (int) Math.max(0, mShadowDy + mShadowRadius); 4454 } 4455 4456 @Override 4457 protected int getRightPaddingOffset() { 4458 return -(getCompoundPaddingRight() - mPaddingRight) + 4459 (int) Math.max(0, mShadowDx + mShadowRadius); 4460 } 4461 4462 @Override 4463 protected boolean verifyDrawable(Drawable who) { 4464 final boolean verified = super.verifyDrawable(who); 4465 if (!verified && mDrawables != null) { 4466 return who == mDrawables.mDrawableLeft || who == mDrawables.mDrawableTop || 4467 who == mDrawables.mDrawableRight || who == mDrawables.mDrawableBottom || 4468 who == mDrawables.mDrawableStart || who == mDrawables.mDrawableEnd; 4469 } 4470 return verified; 4471 } 4472 4473 @Override 4474 public void jumpDrawablesToCurrentState() { 4475 super.jumpDrawablesToCurrentState(); 4476 if (mDrawables != null) { 4477 if (mDrawables.mDrawableLeft != null) { 4478 mDrawables.mDrawableLeft.jumpToCurrentState(); 4479 } 4480 if (mDrawables.mDrawableTop != null) { 4481 mDrawables.mDrawableTop.jumpToCurrentState(); 4482 } 4483 if (mDrawables.mDrawableRight != null) { 4484 mDrawables.mDrawableRight.jumpToCurrentState(); 4485 } 4486 if (mDrawables.mDrawableBottom != null) { 4487 mDrawables.mDrawableBottom.jumpToCurrentState(); 4488 } 4489 if (mDrawables.mDrawableStart != null) { 4490 mDrawables.mDrawableStart.jumpToCurrentState(); 4491 } 4492 if (mDrawables.mDrawableEnd != null) { 4493 mDrawables.mDrawableEnd.jumpToCurrentState(); 4494 } 4495 } 4496 } 4497 4498 @Override 4499 public void invalidateDrawable(Drawable drawable) { 4500 if (verifyDrawable(drawable)) { 4501 final Rect dirty = drawable.getBounds(); 4502 int scrollX = mScrollX; 4503 int scrollY = mScrollY; 4504 4505 // IMPORTANT: The coordinates below are based on the coordinates computed 4506 // for each compound drawable in onDraw(). Make sure to update each section 4507 // accordingly. 4508 final TextView.Drawables drawables = mDrawables; 4509 if (drawables != null) { 4510 if (drawable == drawables.mDrawableLeft) { 4511 final int compoundPaddingTop = getCompoundPaddingTop(); 4512 final int compoundPaddingBottom = getCompoundPaddingBottom(); 4513 final int vspace = mBottom - mTop - compoundPaddingBottom - compoundPaddingTop; 4514 4515 scrollX += mPaddingLeft; 4516 scrollY += compoundPaddingTop + (vspace - drawables.mDrawableHeightLeft) / 2; 4517 } else if (drawable == drawables.mDrawableRight) { 4518 final int compoundPaddingTop = getCompoundPaddingTop(); 4519 final int compoundPaddingBottom = getCompoundPaddingBottom(); 4520 final int vspace = mBottom - mTop - compoundPaddingBottom - compoundPaddingTop; 4521 4522 scrollX += (mRight - mLeft - mPaddingRight - drawables.mDrawableSizeRight); 4523 scrollY += compoundPaddingTop + (vspace - drawables.mDrawableHeightRight) / 2; 4524 } else if (drawable == drawables.mDrawableTop) { 4525 final int compoundPaddingLeft = getCompoundPaddingLeft(); 4526 final int compoundPaddingRight = getCompoundPaddingRight(); 4527 final int hspace = mRight - mLeft - compoundPaddingRight - compoundPaddingLeft; 4528 4529 scrollX += compoundPaddingLeft + (hspace - drawables.mDrawableWidthTop) / 2; 4530 scrollY += mPaddingTop; 4531 } else if (drawable == drawables.mDrawableBottom) { 4532 final int compoundPaddingLeft = getCompoundPaddingLeft(); 4533 final int compoundPaddingRight = getCompoundPaddingRight(); 4534 final int hspace = mRight - mLeft - compoundPaddingRight - compoundPaddingLeft; 4535 4536 scrollX += compoundPaddingLeft + (hspace - drawables.mDrawableWidthBottom) / 2; 4537 scrollY += (mBottom - mTop - mPaddingBottom - drawables.mDrawableSizeBottom); 4538 } 4539 } 4540 4541 invalidate(dirty.left + scrollX, dirty.top + scrollY, 4542 dirty.right + scrollX, dirty.bottom + scrollY); 4543 } 4544 } 4545 4546 @Override 4547 public int getResolvedLayoutDirection(Drawable who) { 4548 if (who == null) return View.LAYOUT_DIRECTION_LTR; 4549 if (mDrawables != null) { 4550 final Drawables drawables = mDrawables; 4551 if (who == drawables.mDrawableLeft || who == drawables.mDrawableRight || 4552 who == drawables.mDrawableTop || who == drawables.mDrawableBottom || 4553 who == drawables.mDrawableStart || who == drawables.mDrawableEnd) { 4554 return getResolvedLayoutDirection(); 4555 } 4556 } 4557 return super.getResolvedLayoutDirection(who); 4558 } 4559 4560 @Override 4561 public boolean hasOverlappingRendering() { 4562 return (getBackground() != null || mText instanceof Spannable || hasSelection()); 4563 } 4564 4565 /** 4566 * When a TextView is used to display a useful piece of information to the user (such as a 4567 * contact's address), it should be made selectable, so that the user can select and copy this 4568 * content. 4569 * 4570 * Use {@link #setTextIsSelectable(boolean)} or the 4571 * {@link android.R.styleable#TextView_textIsSelectable} XML attribute to make this TextView 4572 * selectable (text is not selectable by default). 4573 * 4574 * Note that this method simply returns the state of this flag. Although this flag has to be set 4575 * in order to select text in non-editable TextView, the content of an {@link EditText} can 4576 * always be selected, independently of the value of this flag. 4577 * 4578 * @return True if the text displayed in this TextView can be selected by the user. 4579 * 4580 * @attr ref android.R.styleable#TextView_textIsSelectable 4581 */ 4582 public boolean isTextSelectable() { 4583 return mEditor == null ? false : mEditor.mTextIsSelectable; 4584 } 4585 4586 /** 4587 * Sets whether or not (default) the content of this view is selectable by the user. 4588 * 4589 * Note that this methods affect the {@link #setFocusable(boolean)}, 4590 * {@link #setFocusableInTouchMode(boolean)} {@link #setClickable(boolean)} and 4591 * {@link #setLongClickable(boolean)} states and you may want to restore these if they were 4592 * customized. 4593 * 4594 * See {@link #isTextSelectable} for details. 4595 * 4596 * @param selectable Whether or not the content of this TextView should be selectable. 4597 */ 4598 public void setTextIsSelectable(boolean selectable) { 4599 if (!selectable && mEditor == null) return; // false is default value with no edit data 4600 4601 createEditorIfNeeded("setTextIsSelectable"); 4602 if (mEditor.mTextIsSelectable == selectable) return; 4603 4604 mEditor.mTextIsSelectable = selectable; 4605 setFocusableInTouchMode(selectable); 4606 setFocusable(selectable); 4607 setClickable(selectable); 4608 setLongClickable(selectable); 4609 4610 // mInputType should already be EditorInfo.TYPE_NULL and mInput should be null 4611 4612 setMovementMethod(selectable ? ArrowKeyMovementMethod.getInstance() : null); 4613 setText(getText(), selectable ? BufferType.SPANNABLE : BufferType.NORMAL); 4614 4615 // Called by setText above, but safer in case of future code changes 4616 mEditor.prepareCursorControllers(); 4617 } 4618 4619 @Override 4620 protected int[] onCreateDrawableState(int extraSpace) { 4621 final int[] drawableState; 4622 4623 if (mSingleLine) { 4624 drawableState = super.onCreateDrawableState(extraSpace); 4625 } else { 4626 drawableState = super.onCreateDrawableState(extraSpace + 1); 4627 mergeDrawableStates(drawableState, MULTILINE_STATE_SET); 4628 } 4629 4630 if (isTextSelectable()) { 4631 // Disable pressed state, which was introduced when TextView was made clickable. 4632 // Prevents text color change. 4633 // setClickable(false) would have a similar effect, but it also disables focus changes 4634 // and long press actions, which are both needed by text selection. 4635 final int length = drawableState.length; 4636 for (int i = 0; i < length; i++) { 4637 if (drawableState[i] == R.attr.state_pressed) { 4638 final int[] nonPressedState = new int[length - 1]; 4639 System.arraycopy(drawableState, 0, nonPressedState, 0, i); 4640 System.arraycopy(drawableState, i + 1, nonPressedState, i, length - i - 1); 4641 return nonPressedState; 4642 } 4643 } 4644 } 4645 4646 return drawableState; 4647 } 4648 4649 private Path getUpdatedHighlightPath() { 4650 Path highlight = null; 4651 Paint highlightPaint = mHighlightPaint; 4652 4653 final int selStart = getSelectionStart(); 4654 final int selEnd = getSelectionEnd(); 4655 if (mMovement != null && (isFocused() || isPressed()) && selStart >= 0) { 4656 if (selStart == selEnd) { 4657 if (mEditor != null && mEditor.isCursorVisible() && 4658 (SystemClock.uptimeMillis() - mEditor.mShowCursor) % 4659 (2 * Editor.BLINK) < Editor.BLINK) { 4660 if (mHighlightPathBogus) { 4661 if (mHighlightPath == null) mHighlightPath = new Path(); 4662 mHighlightPath.reset(); 4663 mLayout.getCursorPath(selStart, mHighlightPath, mText); 4664 mEditor.updateCursorsPositions(); 4665 mHighlightPathBogus = false; 4666 } 4667 4668 // XXX should pass to skin instead of drawing directly 4669 highlightPaint.setColor(mCurTextColor); 4670 highlightPaint.setStyle(Paint.Style.STROKE); 4671 highlight = mHighlightPath; 4672 } 4673 } else { 4674 if (mHighlightPathBogus) { 4675 if (mHighlightPath == null) mHighlightPath = new Path(); 4676 mHighlightPath.reset(); 4677 mLayout.getSelectionPath(selStart, selEnd, mHighlightPath); 4678 mHighlightPathBogus = false; 4679 } 4680 4681 // XXX should pass to skin instead of drawing directly 4682 highlightPaint.setColor(mHighlightColor); 4683 highlightPaint.setStyle(Paint.Style.FILL); 4684 4685 highlight = mHighlightPath; 4686 } 4687 } 4688 return highlight; 4689 } 4690 4691 @Override 4692 protected void onDraw(Canvas canvas) { 4693 restartMarqueeIfNeeded(); 4694 4695 // Draw the background for this view 4696 super.onDraw(canvas); 4697 4698 final int compoundPaddingLeft = getCompoundPaddingLeft(); 4699 final int compoundPaddingTop = getCompoundPaddingTop(); 4700 final int compoundPaddingRight = getCompoundPaddingRight(); 4701 final int compoundPaddingBottom = getCompoundPaddingBottom(); 4702 final int scrollX = mScrollX; 4703 final int scrollY = mScrollY; 4704 final int right = mRight; 4705 final int left = mLeft; 4706 final int bottom = mBottom; 4707 final int top = mTop; 4708 4709 final Drawables dr = mDrawables; 4710 if (dr != null) { 4711 /* 4712 * Compound, not extended, because the icon is not clipped 4713 * if the text height is smaller. 4714 */ 4715 4716 int vspace = bottom - top - compoundPaddingBottom - compoundPaddingTop; 4717 int hspace = right - left - compoundPaddingRight - compoundPaddingLeft; 4718 4719 // IMPORTANT: The coordinates computed are also used in invalidateDrawable() 4720 // Make sure to update invalidateDrawable() when changing this code. 4721 if (dr.mDrawableLeft != null) { 4722 canvas.save(); 4723 canvas.translate(scrollX + mPaddingLeft, 4724 scrollY + compoundPaddingTop + 4725 (vspace - dr.mDrawableHeightLeft) / 2); 4726 dr.mDrawableLeft.draw(canvas); 4727 canvas.restore(); 4728 } 4729 4730 // IMPORTANT: The coordinates computed are also used in invalidateDrawable() 4731 // Make sure to update invalidateDrawable() when changing this code. 4732 if (dr.mDrawableRight != null) { 4733 canvas.save(); 4734 canvas.translate(scrollX + right - left - mPaddingRight - dr.mDrawableSizeRight, 4735 scrollY + compoundPaddingTop + (vspace - dr.mDrawableHeightRight) / 2); 4736 dr.mDrawableRight.draw(canvas); 4737 canvas.restore(); 4738 } 4739 4740 // IMPORTANT: The coordinates computed are also used in invalidateDrawable() 4741 // Make sure to update invalidateDrawable() when changing this code. 4742 if (dr.mDrawableTop != null) { 4743 canvas.save(); 4744 canvas.translate(scrollX + compoundPaddingLeft + 4745 (hspace - dr.mDrawableWidthTop) / 2, scrollY + mPaddingTop); 4746 dr.mDrawableTop.draw(canvas); 4747 canvas.restore(); 4748 } 4749 4750 // IMPORTANT: The coordinates computed are also used in invalidateDrawable() 4751 // Make sure to update invalidateDrawable() when changing this code. 4752 if (dr.mDrawableBottom != null) { 4753 canvas.save(); 4754 canvas.translate(scrollX + compoundPaddingLeft + 4755 (hspace - dr.mDrawableWidthBottom) / 2, 4756 scrollY + bottom - top - mPaddingBottom - dr.mDrawableSizeBottom); 4757 dr.mDrawableBottom.draw(canvas); 4758 canvas.restore(); 4759 } 4760 } 4761 4762 int color = mCurTextColor; 4763 4764 if (mLayout == null) { 4765 assumeLayout(); 4766 } 4767 4768 Layout layout = mLayout; 4769 4770 if (mHint != null && mText.length() == 0) { 4771 if (mHintTextColor != null) { 4772 color = mCurHintTextColor; 4773 } 4774 4775 layout = mHintLayout; 4776 } 4777 4778 mTextPaint.setColor(color); 4779 mTextPaint.drawableState = getDrawableState(); 4780 4781 canvas.save(); 4782 /* Would be faster if we didn't have to do this. Can we chop the 4783 (displayable) text so that we don't need to do this ever? 4784 */ 4785 4786 int extendedPaddingTop = getExtendedPaddingTop(); 4787 int extendedPaddingBottom = getExtendedPaddingBottom(); 4788 4789 final int vspace = mBottom - mTop - compoundPaddingBottom - compoundPaddingTop; 4790 final int maxScrollY = mLayout.getHeight() - vspace; 4791 4792 float clipLeft = compoundPaddingLeft + scrollX; 4793 float clipTop = (scrollY == 0) ? 0 : extendedPaddingTop + scrollY; 4794 float clipRight = right - left - compoundPaddingRight + scrollX; 4795 float clipBottom = bottom - top + scrollY - 4796 ((scrollY == maxScrollY) ? 0 : extendedPaddingBottom); 4797 4798 if (mShadowRadius != 0) { 4799 clipLeft += Math.min(0, mShadowDx - mShadowRadius); 4800 clipRight += Math.max(0, mShadowDx + mShadowRadius); 4801 4802 clipTop += Math.min(0, mShadowDy - mShadowRadius); 4803 clipBottom += Math.max(0, mShadowDy + mShadowRadius); 4804 } 4805 4806 canvas.clipRect(clipLeft, clipTop, clipRight, clipBottom); 4807 4808 int voffsetText = 0; 4809 int voffsetCursor = 0; 4810 4811 // translate in by our padding 4812 /* shortcircuit calling getVerticaOffset() */ 4813 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { 4814 voffsetText = getVerticalOffset(false); 4815 voffsetCursor = getVerticalOffset(true); 4816 } 4817 canvas.translate(compoundPaddingLeft, extendedPaddingTop + voffsetText); 4818 4819 final int layoutDirection = getResolvedLayoutDirection(); 4820 final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection); 4821 if (mEllipsize == TextUtils.TruncateAt.MARQUEE && 4822 mMarqueeFadeMode != MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { 4823 if (!mSingleLine && getLineCount() == 1 && canMarquee() && 4824 (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) != Gravity.LEFT) { 4825 canvas.translate(mLayout.getLineRight(0) - (mRight - mLeft - 4826 getCompoundPaddingLeft() - getCompoundPaddingRight()), 0.0f); 4827 } 4828 4829 if (mMarquee != null && mMarquee.isRunning()) { 4830 canvas.translate(-mMarquee.mScroll, 0.0f); 4831 } 4832 } 4833 4834 final int cursorOffsetVertical = voffsetCursor - voffsetText; 4835 4836 Path highlight = getUpdatedHighlightPath(); 4837 if (mEditor != null) { 4838 mEditor.onDraw(canvas, layout, highlight, mHighlightPaint, cursorOffsetVertical); 4839 } else { 4840 layout.draw(canvas, highlight, mHighlightPaint, cursorOffsetVertical); 4841 } 4842 4843 if (mMarquee != null && mMarquee.shouldDrawGhost()) { 4844 canvas.translate((int) mMarquee.getGhostOffset(), 0.0f); 4845 layout.draw(canvas, highlight, mHighlightPaint, cursorOffsetVertical); 4846 } 4847 4848 canvas.restore(); 4849 } 4850 4851 @Override 4852 public void getFocusedRect(Rect r) { 4853 if (mLayout == null) { 4854 super.getFocusedRect(r); 4855 return; 4856 } 4857 4858 int selEnd = getSelectionEnd(); 4859 if (selEnd < 0) { 4860 super.getFocusedRect(r); 4861 return; 4862 } 4863 4864 int selStart = getSelectionStart(); 4865 if (selStart < 0 || selStart >= selEnd) { 4866 int line = mLayout.getLineForOffset(selEnd); 4867 r.top = mLayout.getLineTop(line); 4868 r.bottom = mLayout.getLineBottom(line); 4869 r.left = (int) mLayout.getPrimaryHorizontal(selEnd) - 2; 4870 r.right = r.left + 4; 4871 } else { 4872 int lineStart = mLayout.getLineForOffset(selStart); 4873 int lineEnd = mLayout.getLineForOffset(selEnd); 4874 r.top = mLayout.getLineTop(lineStart); 4875 r.bottom = mLayout.getLineBottom(lineEnd); 4876 if (lineStart == lineEnd) { 4877 r.left = (int) mLayout.getPrimaryHorizontal(selStart); 4878 r.right = (int) mLayout.getPrimaryHorizontal(selEnd); 4879 } else { 4880 // Selection extends across multiple lines -- make the focused 4881 // rect cover the entire width. 4882 if (mHighlightPathBogus) { 4883 if (mHighlightPath == null) mHighlightPath = new Path(); 4884 mHighlightPath.reset(); 4885 mLayout.getSelectionPath(selStart, selEnd, mHighlightPath); 4886 mHighlightPathBogus = false; 4887 } 4888 synchronized (TEMP_RECTF) { 4889 mHighlightPath.computeBounds(TEMP_RECTF, true); 4890 r.left = (int)TEMP_RECTF.left-1; 4891 r.right = (int)TEMP_RECTF.right+1; 4892 } 4893 } 4894 } 4895 4896 // Adjust for padding and gravity. 4897 int paddingLeft = getCompoundPaddingLeft(); 4898 int paddingTop = getExtendedPaddingTop(); 4899 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { 4900 paddingTop += getVerticalOffset(false); 4901 } 4902 r.offset(paddingLeft, paddingTop); 4903 int paddingBottom = getExtendedPaddingBottom(); 4904 r.bottom += paddingBottom; 4905 } 4906 4907 /** 4908 * Return the number of lines of text, or 0 if the internal Layout has not 4909 * been built. 4910 */ 4911 public int getLineCount() { 4912 return mLayout != null ? mLayout.getLineCount() : 0; 4913 } 4914 4915 /** 4916 * Return the baseline for the specified line (0...getLineCount() - 1) 4917 * If bounds is not null, return the top, left, right, bottom extents 4918 * of the specified line in it. If the internal Layout has not been built, 4919 * return 0 and set bounds to (0, 0, 0, 0) 4920 * @param line which line to examine (0..getLineCount() - 1) 4921 * @param bounds Optional. If not null, it returns the extent of the line 4922 * @return the Y-coordinate of the baseline 4923 */ 4924 public int getLineBounds(int line, Rect bounds) { 4925 if (mLayout == null) { 4926 if (bounds != null) { 4927 bounds.set(0, 0, 0, 0); 4928 } 4929 return 0; 4930 } 4931 else { 4932 int baseline = mLayout.getLineBounds(line, bounds); 4933 4934 int voffset = getExtendedPaddingTop(); 4935 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { 4936 voffset += getVerticalOffset(true); 4937 } 4938 if (bounds != null) { 4939 bounds.offset(getCompoundPaddingLeft(), voffset); 4940 } 4941 return baseline + voffset; 4942 } 4943 } 4944 4945 @Override 4946 public int getBaseline() { 4947 if (mLayout == null) { 4948 return super.getBaseline(); 4949 } 4950 4951 int voffset = 0; 4952 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { 4953 voffset = getVerticalOffset(true); 4954 } 4955 4956 return getExtendedPaddingTop() + voffset + mLayout.getLineBaseline(0); 4957 } 4958 4959 /** 4960 * @hide 4961 */ 4962 @Override 4963 protected int getFadeTop(boolean offsetRequired) { 4964 if (mLayout == null) return 0; 4965 4966 int voffset = 0; 4967 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { 4968 voffset = getVerticalOffset(true); 4969 } 4970 4971 if (offsetRequired) voffset += getTopPaddingOffset(); 4972 4973 return getExtendedPaddingTop() + voffset; 4974 } 4975 4976 /** 4977 * @hide 4978 */ 4979 @Override 4980 protected int getFadeHeight(boolean offsetRequired) { 4981 return mLayout != null ? mLayout.getHeight() : 0; 4982 } 4983 4984 @Override 4985 public boolean onKeyPreIme(int keyCode, KeyEvent event) { 4986 if (keyCode == KeyEvent.KEYCODE_BACK) { 4987 boolean isInSelectionMode = mEditor != null && mEditor.mSelectionActionMode != null; 4988 4989 if (isInSelectionMode) { 4990 if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { 4991 KeyEvent.DispatcherState state = getKeyDispatcherState(); 4992 if (state != null) { 4993 state.startTracking(event, this); 4994 } 4995 return true; 4996 } else if (event.getAction() == KeyEvent.ACTION_UP) { 4997 KeyEvent.DispatcherState state = getKeyDispatcherState(); 4998 if (state != null) { 4999 state.handleUpEvent(event); 5000 } 5001 if (event.isTracking() && !event.isCanceled()) { 5002 stopSelectionActionMode(); 5003 return true; 5004 } 5005 } 5006 } 5007 } 5008 return super.onKeyPreIme(keyCode, event); 5009 } 5010 5011 @Override 5012 public boolean onKeyDown(int keyCode, KeyEvent event) { 5013 int which = doKeyDown(keyCode, event, null); 5014 if (which == 0) { 5015 // Go through default dispatching. 5016 return super.onKeyDown(keyCode, event); 5017 } 5018 5019 return true; 5020 } 5021 5022 @Override 5023 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { 5024 KeyEvent down = KeyEvent.changeAction(event, KeyEvent.ACTION_DOWN); 5025 5026 int which = doKeyDown(keyCode, down, event); 5027 if (which == 0) { 5028 // Go through default dispatching. 5029 return super.onKeyMultiple(keyCode, repeatCount, event); 5030 } 5031 if (which == -1) { 5032 // Consumed the whole thing. 5033 return true; 5034 } 5035 5036 repeatCount--; 5037 5038 // We are going to dispatch the remaining events to either the input 5039 // or movement method. To do this, we will just send a repeated stream 5040 // of down and up events until we have done the complete repeatCount. 5041 // It would be nice if those interfaces had an onKeyMultiple() method, 5042 // but adding that is a more complicated change. 5043 KeyEvent up = KeyEvent.changeAction(event, KeyEvent.ACTION_UP); 5044 if (which == 1) { 5045 // mEditor and mEditor.mInput are not null from doKeyDown 5046 mEditor.mKeyListener.onKeyUp(this, (Editable)mText, keyCode, up); 5047 while (--repeatCount > 0) { 5048 mEditor.mKeyListener.onKeyDown(this, (Editable)mText, keyCode, down); 5049 mEditor.mKeyListener.onKeyUp(this, (Editable)mText, keyCode, up); 5050 } 5051 hideErrorIfUnchanged(); 5052 5053 } else if (which == 2) { 5054 // mMovement is not null from doKeyDown 5055 mMovement.onKeyUp(this, (Spannable)mText, keyCode, up); 5056 while (--repeatCount > 0) { 5057 mMovement.onKeyDown(this, (Spannable)mText, keyCode, down); 5058 mMovement.onKeyUp(this, (Spannable)mText, keyCode, up); 5059 } 5060 } 5061 5062 return true; 5063 } 5064 5065 /** 5066 * Returns true if pressing ENTER in this field advances focus instead 5067 * of inserting the character. This is true mostly in single-line fields, 5068 * but also in mail addresses and subjects which will display on multiple 5069 * lines but where it doesn't make sense to insert newlines. 5070 */ 5071 private boolean shouldAdvanceFocusOnEnter() { 5072 if (getKeyListener() == null) { 5073 return false; 5074 } 5075 5076 if (mSingleLine) { 5077 return true; 5078 } 5079 5080 if (mEditor != null && 5081 (mEditor.mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { 5082 int variation = mEditor.mInputType & EditorInfo.TYPE_MASK_VARIATION; 5083 if (variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS 5084 || variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_SUBJECT) { 5085 return true; 5086 } 5087 } 5088 5089 return false; 5090 } 5091 5092 /** 5093 * Returns true if pressing TAB in this field advances focus instead 5094 * of inserting the character. Insert tabs only in multi-line editors. 5095 */ 5096 private boolean shouldAdvanceFocusOnTab() { 5097 if (getKeyListener() != null && !mSingleLine && mEditor != null && 5098 (mEditor.mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { 5099 int variation = mEditor.mInputType & EditorInfo.TYPE_MASK_VARIATION; 5100 if (variation == EditorInfo.TYPE_TEXT_FLAG_IME_MULTI_LINE 5101 || variation == EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE) { 5102 return false; 5103 } 5104 } 5105 return true; 5106 } 5107 5108 private int doKeyDown(int keyCode, KeyEvent event, KeyEvent otherEvent) { 5109 if (!isEnabled()) { 5110 return 0; 5111 } 5112 5113 switch (keyCode) { 5114 case KeyEvent.KEYCODE_ENTER: 5115 if (event.hasNoModifiers()) { 5116 // When mInputContentType is set, we know that we are 5117 // running in a "modern" cupcake environment, so don't need 5118 // to worry about the application trying to capture 5119 // enter key events. 5120 if (mEditor != null && mEditor.mInputContentType != null) { 5121 // If there is an action listener, given them a 5122 // chance to consume the event. 5123 if (mEditor.mInputContentType.onEditorActionListener != null && 5124 mEditor.mInputContentType.onEditorActionListener.onEditorAction( 5125 this, EditorInfo.IME_NULL, event)) { 5126 mEditor.mInputContentType.enterDown = true; 5127 // We are consuming the enter key for them. 5128 return -1; 5129 } 5130 } 5131 5132 // If our editor should move focus when enter is pressed, or 5133 // this is a generated event from an IME action button, then 5134 // don't let it be inserted into the text. 5135 if ((event.getFlags() & KeyEvent.FLAG_EDITOR_ACTION) != 0 5136 || shouldAdvanceFocusOnEnter()) { 5137 if (hasOnClickListeners()) { 5138 return 0; 5139 } 5140 return -1; 5141 } 5142 } 5143 break; 5144 5145 case KeyEvent.KEYCODE_DPAD_CENTER: 5146 if (event.hasNoModifiers()) { 5147 if (shouldAdvanceFocusOnEnter()) { 5148 return 0; 5149 } 5150 } 5151 break; 5152 5153 case KeyEvent.KEYCODE_TAB: 5154 if (event.hasNoModifiers() || event.hasModifiers(KeyEvent.META_SHIFT_ON)) { 5155 if (shouldAdvanceFocusOnTab()) { 5156 return 0; 5157 } 5158 } 5159 break; 5160 5161 // Has to be done on key down (and not on key up) to correctly be intercepted. 5162 case KeyEvent.KEYCODE_BACK: 5163 if (mEditor != null && mEditor.mSelectionActionMode != null) { 5164 stopSelectionActionMode(); 5165 return -1; 5166 } 5167 break; 5168 } 5169 5170 if (mEditor != null && mEditor.mKeyListener != null) { 5171 resetErrorChangedFlag(); 5172 5173 boolean doDown = true; 5174 if (otherEvent != null) { 5175 try { 5176 beginBatchEdit(); 5177 final boolean handled = mEditor.mKeyListener.onKeyOther(this, (Editable) mText, 5178 otherEvent); 5179 hideErrorIfUnchanged(); 5180 doDown = false; 5181 if (handled) { 5182 return -1; 5183 } 5184 } catch (AbstractMethodError e) { 5185 // onKeyOther was added after 1.0, so if it isn't 5186 // implemented we need to try to dispatch as a regular down. 5187 } finally { 5188 endBatchEdit(); 5189 } 5190 } 5191 5192 if (doDown) { 5193 beginBatchEdit(); 5194 final boolean handled = mEditor.mKeyListener.onKeyDown(this, (Editable) mText, 5195 keyCode, event); 5196 endBatchEdit(); 5197 hideErrorIfUnchanged(); 5198 if (handled) return 1; 5199 } 5200 } 5201 5202 // bug 650865: sometimes we get a key event before a layout. 5203 // don't try to move around if we don't know the layout. 5204 5205 if (mMovement != null && mLayout != null) { 5206 boolean doDown = true; 5207 if (otherEvent != null) { 5208 try { 5209 boolean handled = mMovement.onKeyOther(this, (Spannable) mText, 5210 otherEvent); 5211 doDown = false; 5212 if (handled) { 5213 return -1; 5214 } 5215 } catch (AbstractMethodError e) { 5216 // onKeyOther was added after 1.0, so if it isn't 5217 // implemented we need to try to dispatch as a regular down. 5218 } 5219 } 5220 if (doDown) { 5221 if (mMovement.onKeyDown(this, (Spannable)mText, keyCode, event)) 5222 return 2; 5223 } 5224 } 5225 5226 return 0; 5227 } 5228 5229 /** 5230 * Resets the mErrorWasChanged flag, so that future calls to {@link #setError(CharSequence)} 5231 * can be recorded. 5232 * @hide 5233 */ 5234 public void resetErrorChangedFlag() { 5235 /* 5236 * Keep track of what the error was before doing the input 5237 * so that if an input filter changed the error, we leave 5238 * that error showing. Otherwise, we take down whatever 5239 * error was showing when the user types something. 5240 */ 5241 if (mEditor != null) mEditor.mErrorWasChanged = false; 5242 } 5243 5244 /** 5245 * @hide 5246 */ 5247 public void hideErrorIfUnchanged() { 5248 if (mEditor != null && mEditor.mError != null && !mEditor.mErrorWasChanged) { 5249 setError(null, null); 5250 } 5251 } 5252 5253 @Override 5254 public boolean onKeyUp(int keyCode, KeyEvent event) { 5255 if (!isEnabled()) { 5256 return super.onKeyUp(keyCode, event); 5257 } 5258 5259 switch (keyCode) { 5260 case KeyEvent.KEYCODE_DPAD_CENTER: 5261 if (event.hasNoModifiers()) { 5262 /* 5263 * If there is a click listener, just call through to 5264 * super, which will invoke it. 5265 * 5266 * If there isn't a click listener, try to show the soft 5267 * input method. (It will also 5268 * call performClick(), but that won't do anything in 5269 * this case.) 5270 */ 5271 if (!hasOnClickListeners()) { 5272 if (mMovement != null && mText instanceof Editable 5273 && mLayout != null && onCheckIsTextEditor()) { 5274 InputMethodManager imm = InputMethodManager.peekInstance(); 5275 viewClicked(imm); 5276 if (imm != null && getShowSoftInputOnFocus()) { 5277 imm.showSoftInput(this, 0); 5278 } 5279 } 5280 } 5281 } 5282 return super.onKeyUp(keyCode, event); 5283 5284 case KeyEvent.KEYCODE_ENTER: 5285 if (event.hasNoModifiers()) { 5286 if (mEditor != null && mEditor.mInputContentType != null 5287 && mEditor.mInputContentType.onEditorActionListener != null 5288 && mEditor.mInputContentType.enterDown) { 5289 mEditor.mInputContentType.enterDown = false; 5290 if (mEditor.mInputContentType.onEditorActionListener.onEditorAction( 5291 this, EditorInfo.IME_NULL, event)) { 5292 return true; 5293 } 5294 } 5295 5296 if ((event.getFlags() & KeyEvent.FLAG_EDITOR_ACTION) != 0 5297 || shouldAdvanceFocusOnEnter()) { 5298 /* 5299 * If there is a click listener, just call through to 5300 * super, which will invoke it. 5301 * 5302 * If there isn't a click listener, try to advance focus, 5303 * but still call through to super, which will reset the 5304 * pressed state and longpress state. (It will also 5305 * call performClick(), but that won't do anything in 5306 * this case.) 5307 */ 5308 if (!hasOnClickListeners()) { 5309 View v = focusSearch(FOCUS_DOWN); 5310 5311 if (v != null) { 5312 if (!v.requestFocus(FOCUS_DOWN)) { 5313 throw new IllegalStateException( 5314 "focus search returned a view " + 5315 "that wasn't able to take focus!"); 5316 } 5317 5318 /* 5319 * Return true because we handled the key; super 5320 * will return false because there was no click 5321 * listener. 5322 */ 5323 super.onKeyUp(keyCode, event); 5324 return true; 5325 } else if ((event.getFlags() 5326 & KeyEvent.FLAG_EDITOR_ACTION) != 0) { 5327 // No target for next focus, but make sure the IME 5328 // if this came from it. 5329 InputMethodManager imm = InputMethodManager.peekInstance(); 5330 if (imm != null && imm.isActive(this)) { 5331 imm.hideSoftInputFromWindow(getWindowToken(), 0); 5332 } 5333 } 5334 } 5335 } 5336 return super.onKeyUp(keyCode, event); 5337 } 5338 break; 5339 } 5340 5341 if (mEditor != null && mEditor.mKeyListener != null) 5342 if (mEditor.mKeyListener.onKeyUp(this, (Editable) mText, keyCode, event)) 5343 return true; 5344 5345 if (mMovement != null && mLayout != null) 5346 if (mMovement.onKeyUp(this, (Spannable) mText, keyCode, event)) 5347 return true; 5348 5349 return super.onKeyUp(keyCode, event); 5350 } 5351 5352 @Override 5353 public boolean onCheckIsTextEditor() { 5354 return mEditor != null && mEditor.mInputType != EditorInfo.TYPE_NULL; 5355 } 5356 5357 @Override 5358 public InputConnection onCreateInputConnection(EditorInfo outAttrs) { 5359 if (onCheckIsTextEditor() && isEnabled()) { 5360 mEditor.createInputMethodStateIfNeeded(); 5361 outAttrs.inputType = getInputType(); 5362 if (mEditor.mInputContentType != null) { 5363 outAttrs.imeOptions = mEditor.mInputContentType.imeOptions; 5364 outAttrs.privateImeOptions = mEditor.mInputContentType.privateImeOptions; 5365 outAttrs.actionLabel = mEditor.mInputContentType.imeActionLabel; 5366 outAttrs.actionId = mEditor.mInputContentType.imeActionId; 5367 outAttrs.extras = mEditor.mInputContentType.extras; 5368 } else { 5369 outAttrs.imeOptions = EditorInfo.IME_NULL; 5370 } 5371 if (focusSearch(FOCUS_DOWN) != null) { 5372 outAttrs.imeOptions |= EditorInfo.IME_FLAG_NAVIGATE_NEXT; 5373 } 5374 if (focusSearch(FOCUS_UP) != null) { 5375 outAttrs.imeOptions |= EditorInfo.IME_FLAG_NAVIGATE_PREVIOUS; 5376 } 5377 if ((outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION) 5378 == EditorInfo.IME_ACTION_UNSPECIFIED) { 5379 if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NAVIGATE_NEXT) != 0) { 5380 // An action has not been set, but the enter key will move to 5381 // the next focus, so set the action to that. 5382 outAttrs.imeOptions |= EditorInfo.IME_ACTION_NEXT; 5383 } else { 5384 // An action has not been set, and there is no focus to move 5385 // to, so let's just supply a "done" action. 5386 outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE; 5387 } 5388 if (!shouldAdvanceFocusOnEnter()) { 5389 outAttrs.imeOptions |= EditorInfo.IME_FLAG_NO_ENTER_ACTION; 5390 } 5391 } 5392 if (isMultilineInputType(outAttrs.inputType)) { 5393 // Multi-line text editors should always show an enter key. 5394 outAttrs.imeOptions |= EditorInfo.IME_FLAG_NO_ENTER_ACTION; 5395 } 5396 outAttrs.hintText = mHint; 5397 if (mText instanceof Editable) { 5398 InputConnection ic = new EditableInputConnection(this); 5399 outAttrs.initialSelStart = getSelectionStart(); 5400 outAttrs.initialSelEnd = getSelectionEnd(); 5401 outAttrs.initialCapsMode = ic.getCursorCapsMode(getInputType()); 5402 return ic; 5403 } 5404 } 5405 return null; 5406 } 5407 5408 /** 5409 * If this TextView contains editable content, extract a portion of it 5410 * based on the information in <var>request</var> in to <var>outText</var>. 5411 * @return Returns true if the text was successfully extracted, else false. 5412 */ 5413 public boolean extractText(ExtractedTextRequest request, ExtractedText outText) { 5414 createEditorIfNeeded("extractText"); 5415 return mEditor.extractText(request, outText); 5416 } 5417 5418 /** 5419 * This is used to remove all style-impacting spans from text before new 5420 * extracted text is being replaced into it, so that we don't have any 5421 * lingering spans applied during the replace. 5422 */ 5423 static void removeParcelableSpans(Spannable spannable, int start, int end) { 5424 Object[] spans = spannable.getSpans(start, end, ParcelableSpan.class); 5425 int i = spans.length; 5426 while (i > 0) { 5427 i--; 5428 spannable.removeSpan(spans[i]); 5429 } 5430 } 5431 5432 /** 5433 * Apply to this text view the given extracted text, as previously 5434 * returned by {@link #extractText(ExtractedTextRequest, ExtractedText)}. 5435 */ 5436 public void setExtractedText(ExtractedText text) { 5437 Editable content = getEditableText(); 5438 if (text.text != null) { 5439 if (content == null) { 5440 setText(text.text, TextView.BufferType.EDITABLE); 5441 } else if (text.partialStartOffset < 0) { 5442 removeParcelableSpans(content, 0, content.length()); 5443 content.replace(0, content.length(), text.text); 5444 } else { 5445 final int N = content.length(); 5446 int start = text.partialStartOffset; 5447 if (start > N) start = N; 5448 int end = text.partialEndOffset; 5449 if (end > N) end = N; 5450 removeParcelableSpans(content, start, end); 5451 content.replace(start, end, text.text); 5452 } 5453 } 5454 5455 // Now set the selection position... make sure it is in range, to 5456 // avoid crashes. If this is a partial update, it is possible that 5457 // the underlying text may have changed, causing us problems here. 5458 // Also we just don't want to trust clients to do the right thing. 5459 Spannable sp = (Spannable)getText(); 5460 final int N = sp.length(); 5461 int start = text.selectionStart; 5462 if (start < 0) start = 0; 5463 else if (start > N) start = N; 5464 int end = text.selectionEnd; 5465 if (end < 0) end = 0; 5466 else if (end > N) end = N; 5467 Selection.setSelection(sp, start, end); 5468 5469 // Finally, update the selection mode. 5470 if ((text.flags&ExtractedText.FLAG_SELECTING) != 0) { 5471 MetaKeyKeyListener.startSelecting(this, sp); 5472 } else { 5473 MetaKeyKeyListener.stopSelecting(this, sp); 5474 } 5475 } 5476 5477 /** 5478 * @hide 5479 */ 5480 public void setExtracting(ExtractedTextRequest req) { 5481 if (mEditor.mInputMethodState != null) { 5482 mEditor.mInputMethodState.mExtractedTextRequest = req; 5483 } 5484 // This would stop a possible selection mode, but no such mode is started in case 5485 // extracted mode will start. Some text is selected though, and will trigger an action mode 5486 // in the extracted view. 5487 mEditor.hideControllers(); 5488 } 5489 5490 /** 5491 * Called by the framework in response to a text completion from 5492 * the current input method, provided by it calling 5493 * {@link InputConnection#commitCompletion 5494 * InputConnection.commitCompletion()}. The default implementation does 5495 * nothing; text views that are supporting auto-completion should override 5496 * this to do their desired behavior. 5497 * 5498 * @param text The auto complete text the user has selected. 5499 */ 5500 public void onCommitCompletion(CompletionInfo text) { 5501 // intentionally empty 5502 } 5503 5504 /** 5505 * Called by the framework in response to a text auto-correction (such as fixing a typo using a 5506 * a dictionnary) from the current input method, provided by it calling 5507 * {@link InputConnection#commitCorrection} InputConnection.commitCorrection()}. The default 5508 * implementation flashes the background of the corrected word to provide feedback to the user. 5509 * 5510 * @param info The auto correct info about the text that was corrected. 5511 */ 5512 public void onCommitCorrection(CorrectionInfo info) { 5513 if (mEditor != null) mEditor.onCommitCorrection(info); 5514 } 5515 5516 public void beginBatchEdit() { 5517 if (mEditor != null) mEditor.beginBatchEdit(); 5518 } 5519 5520 public void endBatchEdit() { 5521 if (mEditor != null) mEditor.endBatchEdit(); 5522 } 5523 5524 /** 5525 * Called by the framework in response to a request to begin a batch 5526 * of edit operations through a call to link {@link #beginBatchEdit()}. 5527 */ 5528 public void onBeginBatchEdit() { 5529 // intentionally empty 5530 } 5531 5532 /** 5533 * Called by the framework in response to a request to end a batch 5534 * of edit operations through a call to link {@link #endBatchEdit}. 5535 */ 5536 public void onEndBatchEdit() { 5537 // intentionally empty 5538 } 5539 5540 /** 5541 * Called by the framework in response to a private command from the 5542 * current method, provided by it calling 5543 * {@link InputConnection#performPrivateCommand 5544 * InputConnection.performPrivateCommand()}. 5545 * 5546 * @param action The action name of the command. 5547 * @param data Any additional data for the command. This may be null. 5548 * @return Return true if you handled the command, else false. 5549 */ 5550 public boolean onPrivateIMECommand(String action, Bundle data) { 5551 return false; 5552 } 5553 5554 private void nullLayouts() { 5555 if (mLayout instanceof BoringLayout && mSavedLayout == null) { 5556 mSavedLayout = (BoringLayout) mLayout; 5557 } 5558 if (mHintLayout instanceof BoringLayout && mSavedHintLayout == null) { 5559 mSavedHintLayout = (BoringLayout) mHintLayout; 5560 } 5561 5562 mSavedMarqueeModeLayout = mLayout = mHintLayout = null; 5563 5564 mBoring = mHintBoring = null; 5565 5566 // Since it depends on the value of mLayout 5567 if (mEditor != null) mEditor.prepareCursorControllers(); 5568 } 5569 5570 /** 5571 * Make a new Layout based on the already-measured size of the view, 5572 * on the assumption that it was measured correctly at some point. 5573 */ 5574 private void assumeLayout() { 5575 int width = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); 5576 5577 if (width < 1) { 5578 width = 0; 5579 } 5580 5581 int physicalWidth = width; 5582 5583 if (mHorizontallyScrolling) { 5584 width = VERY_WIDE; 5585 } 5586 5587 makeNewLayout(width, physicalWidth, UNKNOWN_BORING, UNKNOWN_BORING, 5588 physicalWidth, false); 5589 } 5590 5591 @Override 5592 public void onResolvedLayoutDirectionReset() { 5593 if (mLayoutAlignment != null) { 5594 int resolvedTextAlignment = getResolvedTextAlignment(); 5595 if (resolvedTextAlignment == TEXT_ALIGNMENT_VIEW_START || 5596 resolvedTextAlignment == TEXT_ALIGNMENT_VIEW_END) { 5597 mLayoutAlignment = null; 5598 } 5599 } 5600 } 5601 5602 private Layout.Alignment getLayoutAlignment() { 5603 if (mLayoutAlignment == null) { 5604 int textAlign = getResolvedTextAlignment(); 5605 switch (textAlign) { 5606 case TEXT_ALIGNMENT_GRAVITY: 5607 switch (mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) { 5608 case Gravity.START: 5609 mLayoutAlignment = Layout.Alignment.ALIGN_NORMAL; 5610 break; 5611 case Gravity.END: 5612 mLayoutAlignment = Layout.Alignment.ALIGN_OPPOSITE; 5613 break; 5614 case Gravity.LEFT: 5615 mLayoutAlignment = Layout.Alignment.ALIGN_LEFT; 5616 break; 5617 case Gravity.RIGHT: 5618 mLayoutAlignment = Layout.Alignment.ALIGN_RIGHT; 5619 break; 5620 case Gravity.CENTER_HORIZONTAL: 5621 mLayoutAlignment = Layout.Alignment.ALIGN_CENTER; 5622 break; 5623 default: 5624 mLayoutAlignment = Layout.Alignment.ALIGN_NORMAL; 5625 break; 5626 } 5627 break; 5628 case TEXT_ALIGNMENT_TEXT_START: 5629 mLayoutAlignment = Layout.Alignment.ALIGN_NORMAL; 5630 break; 5631 case TEXT_ALIGNMENT_TEXT_END: 5632 mLayoutAlignment = Layout.Alignment.ALIGN_OPPOSITE; 5633 break; 5634 case TEXT_ALIGNMENT_CENTER: 5635 mLayoutAlignment = Layout.Alignment.ALIGN_CENTER; 5636 break; 5637 case TEXT_ALIGNMENT_VIEW_START: 5638 mLayoutAlignment = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ? 5639 Layout.Alignment.ALIGN_RIGHT : Layout.Alignment.ALIGN_LEFT; 5640 break; 5641 case TEXT_ALIGNMENT_VIEW_END: 5642 mLayoutAlignment = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ? 5643 Layout.Alignment.ALIGN_LEFT : Layout.Alignment.ALIGN_RIGHT; 5644 break; 5645 case TEXT_ALIGNMENT_INHERIT: 5646 // This should never happen as we have already resolved the text alignment 5647 // but better safe than sorry so we just fall through 5648 default: 5649 mLayoutAlignment = Layout.Alignment.ALIGN_NORMAL; 5650 break; 5651 } 5652 } 5653 return mLayoutAlignment; 5654 } 5655 5656 /** 5657 * The width passed in is now the desired layout width, 5658 * not the full view width with padding. 5659 * {@hide} 5660 */ 5661 protected void makeNewLayout(int wantWidth, int hintWidth, 5662 BoringLayout.Metrics boring, 5663 BoringLayout.Metrics hintBoring, 5664 int ellipsisWidth, boolean bringIntoView) { 5665 stopMarquee(); 5666 5667 // Update "old" cached values 5668 mOldMaximum = mMaximum; 5669 mOldMaxMode = mMaxMode; 5670 5671 mHighlightPathBogus = true; 5672 5673 if (wantWidth < 0) { 5674 wantWidth = 0; 5675 } 5676 if (hintWidth < 0) { 5677 hintWidth = 0; 5678 } 5679 5680 Layout.Alignment alignment = getLayoutAlignment(); 5681 boolean shouldEllipsize = mEllipsize != null && getKeyListener() == null; 5682 final boolean switchEllipsize = mEllipsize == TruncateAt.MARQUEE && 5683 mMarqueeFadeMode != MARQUEE_FADE_NORMAL; 5684 TruncateAt effectiveEllipsize = mEllipsize; 5685 if (mEllipsize == TruncateAt.MARQUEE && 5686 mMarqueeFadeMode == MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { 5687 effectiveEllipsize = TruncateAt.END_SMALL; 5688 } 5689 5690 if (mTextDir == null) { 5691 resolveTextDirection(); 5692 } 5693 5694 mLayout = makeSingleLayout(wantWidth, boring, ellipsisWidth, alignment, shouldEllipsize, 5695 effectiveEllipsize, effectiveEllipsize == mEllipsize); 5696 if (switchEllipsize) { 5697 TruncateAt oppositeEllipsize = effectiveEllipsize == TruncateAt.MARQUEE ? 5698 TruncateAt.END : TruncateAt.MARQUEE; 5699 mSavedMarqueeModeLayout = makeSingleLayout(wantWidth, boring, ellipsisWidth, alignment, 5700 shouldEllipsize, oppositeEllipsize, effectiveEllipsize != mEllipsize); 5701 } 5702 5703 shouldEllipsize = mEllipsize != null; 5704 mHintLayout = null; 5705 5706 if (mHint != null) { 5707 if (shouldEllipsize) hintWidth = wantWidth; 5708 5709 if (hintBoring == UNKNOWN_BORING) { 5710 hintBoring = BoringLayout.isBoring(mHint, mTextPaint, mTextDir, 5711 mHintBoring); 5712 if (hintBoring != null) { 5713 mHintBoring = hintBoring; 5714 } 5715 } 5716 5717 if (hintBoring != null) { 5718 if (hintBoring.width <= hintWidth && 5719 (!shouldEllipsize || hintBoring.width <= ellipsisWidth)) { 5720 if (mSavedHintLayout != null) { 5721 mHintLayout = mSavedHintLayout. 5722 replaceOrMake(mHint, mTextPaint, 5723 hintWidth, alignment, mSpacingMult, mSpacingAdd, 5724 hintBoring, mIncludePad); 5725 } else { 5726 mHintLayout = BoringLayout.make(mHint, mTextPaint, 5727 hintWidth, alignment, mSpacingMult, mSpacingAdd, 5728 hintBoring, mIncludePad); 5729 } 5730 5731 mSavedHintLayout = (BoringLayout) mHintLayout; 5732 } else if (shouldEllipsize && hintBoring.width <= hintWidth) { 5733 if (mSavedHintLayout != null) { 5734 mHintLayout = mSavedHintLayout. 5735 replaceOrMake(mHint, mTextPaint, 5736 hintWidth, alignment, mSpacingMult, mSpacingAdd, 5737 hintBoring, mIncludePad, mEllipsize, 5738 ellipsisWidth); 5739 } else { 5740 mHintLayout = BoringLayout.make(mHint, mTextPaint, 5741 hintWidth, alignment, mSpacingMult, mSpacingAdd, 5742 hintBoring, mIncludePad, mEllipsize, 5743 ellipsisWidth); 5744 } 5745 } else if (shouldEllipsize) { 5746 mHintLayout = new StaticLayout(mHint, 5747 0, mHint.length(), 5748 mTextPaint, hintWidth, alignment, mTextDir, mSpacingMult, 5749 mSpacingAdd, mIncludePad, mEllipsize, 5750 ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE); 5751 } else { 5752 mHintLayout = new StaticLayout(mHint, mTextPaint, 5753 hintWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, 5754 mIncludePad); 5755 } 5756 } else if (shouldEllipsize) { 5757 mHintLayout = new StaticLayout(mHint, 5758 0, mHint.length(), 5759 mTextPaint, hintWidth, alignment, mTextDir, mSpacingMult, 5760 mSpacingAdd, mIncludePad, mEllipsize, 5761 ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE); 5762 } else { 5763 mHintLayout = new StaticLayout(mHint, mTextPaint, 5764 hintWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, 5765 mIncludePad); 5766 } 5767 } 5768 5769 if (bringIntoView) { 5770 registerForPreDraw(); 5771 } 5772 5773 if (mEllipsize == TextUtils.TruncateAt.MARQUEE) { 5774 if (!compressText(ellipsisWidth)) { 5775 final int height = mLayoutParams.height; 5776 // If the size of the view does not depend on the size of the text, try to 5777 // start the marquee immediately 5778 if (height != LayoutParams.WRAP_CONTENT && height != LayoutParams.MATCH_PARENT) { 5779 startMarquee(); 5780 } else { 5781 // Defer the start of the marquee until we know our width (see setFrame()) 5782 mRestartMarquee = true; 5783 } 5784 } 5785 } 5786 5787 // CursorControllers need a non-null mLayout 5788 if (mEditor != null) mEditor.prepareCursorControllers(); 5789 } 5790 5791 private Layout makeSingleLayout(int wantWidth, BoringLayout.Metrics boring, int ellipsisWidth, 5792 Layout.Alignment alignment, boolean shouldEllipsize, TruncateAt effectiveEllipsize, 5793 boolean useSaved) { 5794 Layout result = null; 5795 if (mText instanceof Spannable) { 5796 result = new DynamicLayout(mText, mTransformed, mTextPaint, wantWidth, 5797 alignment, mTextDir, mSpacingMult, 5798 mSpacingAdd, mIncludePad, getKeyListener() == null ? effectiveEllipsize : null, 5799 ellipsisWidth); 5800 } else { 5801 if (boring == UNKNOWN_BORING) { 5802 boring = BoringLayout.isBoring(mTransformed, mTextPaint, mTextDir, mBoring); 5803 if (boring != null) { 5804 mBoring = boring; 5805 } 5806 } 5807 5808 if (boring != null) { 5809 if (boring.width <= wantWidth && 5810 (effectiveEllipsize == null || boring.width <= ellipsisWidth)) { 5811 if (useSaved && mSavedLayout != null) { 5812 result = mSavedLayout.replaceOrMake(mTransformed, mTextPaint, 5813 wantWidth, alignment, mSpacingMult, mSpacingAdd, 5814 boring, mIncludePad); 5815 } else { 5816 result = BoringLayout.make(mTransformed, mTextPaint, 5817 wantWidth, alignment, mSpacingMult, mSpacingAdd, 5818 boring, mIncludePad); 5819 } 5820 5821 if (useSaved) { 5822 mSavedLayout = (BoringLayout) result; 5823 } 5824 } else if (shouldEllipsize && boring.width <= wantWidth) { 5825 if (useSaved && mSavedLayout != null) { 5826 result = mSavedLayout.replaceOrMake(mTransformed, mTextPaint, 5827 wantWidth, alignment, mSpacingMult, mSpacingAdd, 5828 boring, mIncludePad, effectiveEllipsize, 5829 ellipsisWidth); 5830 } else { 5831 result = BoringLayout.make(mTransformed, mTextPaint, 5832 wantWidth, alignment, mSpacingMult, mSpacingAdd, 5833 boring, mIncludePad, effectiveEllipsize, 5834 ellipsisWidth); 5835 } 5836 } else if (shouldEllipsize) { 5837 result = new StaticLayout(mTransformed, 5838 0, mTransformed.length(), 5839 mTextPaint, wantWidth, alignment, mTextDir, mSpacingMult, 5840 mSpacingAdd, mIncludePad, effectiveEllipsize, 5841 ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE); 5842 } else { 5843 result = new StaticLayout(mTransformed, mTextPaint, 5844 wantWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, 5845 mIncludePad); 5846 } 5847 } else if (shouldEllipsize) { 5848 result = new StaticLayout(mTransformed, 5849 0, mTransformed.length(), 5850 mTextPaint, wantWidth, alignment, mTextDir, mSpacingMult, 5851 mSpacingAdd, mIncludePad, effectiveEllipsize, 5852 ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE); 5853 } else { 5854 result = new StaticLayout(mTransformed, mTextPaint, 5855 wantWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, 5856 mIncludePad); 5857 } 5858 } 5859 return result; 5860 } 5861 5862 private boolean compressText(float width) { 5863 if (isHardwareAccelerated()) return false; 5864 5865 // Only compress the text if it hasn't been compressed by the previous pass 5866 if (width > 0.0f && mLayout != null && getLineCount() == 1 && !mUserSetTextScaleX && 5867 mTextPaint.getTextScaleX() == 1.0f) { 5868 final float textWidth = mLayout.getLineWidth(0); 5869 final float overflow = (textWidth + 1.0f - width) / width; 5870 if (overflow > 0.0f && overflow <= Marquee.MARQUEE_DELTA_MAX) { 5871 mTextPaint.setTextScaleX(1.0f - overflow - 0.005f); 5872 post(new Runnable() { 5873 public void run() { 5874 requestLayout(); 5875 } 5876 }); 5877 return true; 5878 } 5879 } 5880 5881 return false; 5882 } 5883 5884 private static int desired(Layout layout) { 5885 int n = layout.getLineCount(); 5886 CharSequence text = layout.getText(); 5887 float max = 0; 5888 5889 // if any line was wrapped, we can't use it. 5890 // but it's ok for the last line not to have a newline 5891 5892 for (int i = 0; i < n - 1; i++) { 5893 if (text.charAt(layout.getLineEnd(i) - 1) != '\n') 5894 return -1; 5895 } 5896 5897 for (int i = 0; i < n; i++) { 5898 max = Math.max(max, layout.getLineWidth(i)); 5899 } 5900 5901 return (int) FloatMath.ceil(max); 5902 } 5903 5904 /** 5905 * Set whether the TextView includes extra top and bottom padding to make 5906 * room for accents that go above the normal ascent and descent. 5907 * The default is true. 5908 * 5909 * @see #getIncludeFontPadding() 5910 * 5911 * @attr ref android.R.styleable#TextView_includeFontPadding 5912 */ 5913 public void setIncludeFontPadding(boolean includepad) { 5914 if (mIncludePad != includepad) { 5915 mIncludePad = includepad; 5916 5917 if (mLayout != null) { 5918 nullLayouts(); 5919 requestLayout(); 5920 invalidate(); 5921 } 5922 } 5923 } 5924 5925 /** 5926 * Gets whether the TextView includes extra top and bottom padding to make 5927 * room for accents that go above the normal ascent and descent. 5928 * 5929 * @see #setIncludeFontPadding(boolean) 5930 * 5931 * @attr ref android.R.styleable#TextView_includeFontPadding 5932 */ 5933 public boolean getIncludeFontPadding() { 5934 return mIncludePad; 5935 } 5936 5937 private static final BoringLayout.Metrics UNKNOWN_BORING = new BoringLayout.Metrics(); 5938 5939 @Override 5940 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 5941 int widthMode = MeasureSpec.getMode(widthMeasureSpec); 5942 int heightMode = MeasureSpec.getMode(heightMeasureSpec); 5943 int widthSize = MeasureSpec.getSize(widthMeasureSpec); 5944 int heightSize = MeasureSpec.getSize(heightMeasureSpec); 5945 5946 int width; 5947 int height; 5948 5949 BoringLayout.Metrics boring = UNKNOWN_BORING; 5950 BoringLayout.Metrics hintBoring = UNKNOWN_BORING; 5951 5952 if (mTextDir == null) { 5953 resolveTextDirection(); 5954 } 5955 5956 int des = -1; 5957 boolean fromexisting = false; 5958 5959 if (widthMode == MeasureSpec.EXACTLY) { 5960 // Parent has told us how big to be. So be it. 5961 width = widthSize; 5962 } else { 5963 if (mLayout != null && mEllipsize == null) { 5964 des = desired(mLayout); 5965 } 5966 5967 if (des < 0) { 5968 boring = BoringLayout.isBoring(mTransformed, mTextPaint, mTextDir, mBoring); 5969 if (boring != null) { 5970 mBoring = boring; 5971 } 5972 } else { 5973 fromexisting = true; 5974 } 5975 5976 if (boring == null || boring == UNKNOWN_BORING) { 5977 if (des < 0) { 5978 des = (int) FloatMath.ceil(Layout.getDesiredWidth(mTransformed, mTextPaint)); 5979 } 5980 width = des; 5981 } else { 5982 width = boring.width; 5983 } 5984 5985 final Drawables dr = mDrawables; 5986 if (dr != null) { 5987 width = Math.max(width, dr.mDrawableWidthTop); 5988 width = Math.max(width, dr.mDrawableWidthBottom); 5989 } 5990 5991 if (mHint != null) { 5992 int hintDes = -1; 5993 int hintWidth; 5994 5995 if (mHintLayout != null && mEllipsize == null) { 5996 hintDes = desired(mHintLayout); 5997 } 5998 5999 if (hintDes < 0) { 6000 hintBoring = BoringLayout.isBoring(mHint, mTextPaint, mTextDir, mHintBoring); 6001 if (hintBoring != null) { 6002 mHintBoring = hintBoring; 6003 } 6004 } 6005 6006 if (hintBoring == null || hintBoring == UNKNOWN_BORING) { 6007 if (hintDes < 0) { 6008 hintDes = (int) FloatMath.ceil(Layout.getDesiredWidth(mHint, mTextPaint)); 6009 } 6010 hintWidth = hintDes; 6011 } else { 6012 hintWidth = hintBoring.width; 6013 } 6014 6015 if (hintWidth > width) { 6016 width = hintWidth; 6017 } 6018 } 6019 6020 width += getCompoundPaddingLeft() + getCompoundPaddingRight(); 6021 6022 if (mMaxWidthMode == EMS) { 6023 width = Math.min(width, mMaxWidth * getLineHeight()); 6024 } else { 6025 width = Math.min(width, mMaxWidth); 6026 } 6027 6028 if (mMinWidthMode == EMS) { 6029 width = Math.max(width, mMinWidth * getLineHeight()); 6030 } else { 6031 width = Math.max(width, mMinWidth); 6032 } 6033 6034 // Check against our minimum width 6035 width = Math.max(width, getSuggestedMinimumWidth()); 6036 6037 if (widthMode == MeasureSpec.AT_MOST) { 6038 width = Math.min(widthSize, width); 6039 } 6040 } 6041 6042 int want = width - getCompoundPaddingLeft() - getCompoundPaddingRight(); 6043 int unpaddedWidth = want; 6044 6045 if (mHorizontallyScrolling) want = VERY_WIDE; 6046 6047 int hintWant = want; 6048 int hintWidth = (mHintLayout == null) ? hintWant : mHintLayout.getWidth(); 6049 6050 if (mLayout == null) { 6051 makeNewLayout(want, hintWant, boring, hintBoring, 6052 width - getCompoundPaddingLeft() - getCompoundPaddingRight(), false); 6053 } else { 6054 final boolean layoutChanged = (mLayout.getWidth() != want) || 6055 (hintWidth != hintWant) || 6056 (mLayout.getEllipsizedWidth() != 6057 width - getCompoundPaddingLeft() - getCompoundPaddingRight()); 6058 6059 final boolean widthChanged = (mHint == null) && 6060 (mEllipsize == null) && 6061 (want > mLayout.getWidth()) && 6062 (mLayout instanceof BoringLayout || (fromexisting && des >= 0 && des <= want)); 6063 6064 final boolean maximumChanged = (mMaxMode != mOldMaxMode) || (mMaximum != mOldMaximum); 6065 6066 if (layoutChanged || maximumChanged) { 6067 if (!maximumChanged && widthChanged) { 6068 mLayout.increaseWidthTo(want); 6069 } else { 6070 makeNewLayout(want, hintWant, boring, hintBoring, 6071 width - getCompoundPaddingLeft() - getCompoundPaddingRight(), false); 6072 } 6073 } else { 6074 // Nothing has changed 6075 } 6076 } 6077 6078 if (heightMode == MeasureSpec.EXACTLY) { 6079 // Parent has told us how big to be. So be it. 6080 height = heightSize; 6081 mDesiredHeightAtMeasure = -1; 6082 } else { 6083 int desired = getDesiredHeight(); 6084 6085 height = desired; 6086 mDesiredHeightAtMeasure = desired; 6087 6088 if (heightMode == MeasureSpec.AT_MOST) { 6089 height = Math.min(desired, heightSize); 6090 } 6091 } 6092 6093 int unpaddedHeight = height - getCompoundPaddingTop() - getCompoundPaddingBottom(); 6094 if (mMaxMode == LINES && mLayout.getLineCount() > mMaximum) { 6095 unpaddedHeight = Math.min(unpaddedHeight, mLayout.getLineTop(mMaximum)); 6096 } 6097 6098 /* 6099 * We didn't let makeNewLayout() register to bring the cursor into view, 6100 * so do it here if there is any possibility that it is needed. 6101 */ 6102 if (mMovement != null || 6103 mLayout.getWidth() > unpaddedWidth || 6104 mLayout.getHeight() > unpaddedHeight) { 6105 registerForPreDraw(); 6106 } else { 6107 scrollTo(0, 0); 6108 } 6109 6110 setMeasuredDimension(width, height); 6111 } 6112 6113 private int getDesiredHeight() { 6114 return Math.max( 6115 getDesiredHeight(mLayout, true), 6116 getDesiredHeight(mHintLayout, mEllipsize != null)); 6117 } 6118 6119 private int getDesiredHeight(Layout layout, boolean cap) { 6120 if (layout == null) { 6121 return 0; 6122 } 6123 6124 int linecount = layout.getLineCount(); 6125 int pad = getCompoundPaddingTop() + getCompoundPaddingBottom(); 6126 int desired = layout.getLineTop(linecount); 6127 6128 final Drawables dr = mDrawables; 6129 if (dr != null) { 6130 desired = Math.max(desired, dr.mDrawableHeightLeft); 6131 desired = Math.max(desired, dr.mDrawableHeightRight); 6132 } 6133 6134 desired += pad; 6135 6136 if (mMaxMode == LINES) { 6137 /* 6138 * Don't cap the hint to a certain number of lines. 6139 * (Do cap it, though, if we have a maximum pixel height.) 6140 */ 6141 if (cap) { 6142 if (linecount > mMaximum) { 6143 desired = layout.getLineTop(mMaximum); 6144 6145 if (dr != null) { 6146 desired = Math.max(desired, dr.mDrawableHeightLeft); 6147 desired = Math.max(desired, dr.mDrawableHeightRight); 6148 } 6149 6150 desired += pad; 6151 linecount = mMaximum; 6152 } 6153 } 6154 } else { 6155 desired = Math.min(desired, mMaximum); 6156 } 6157 6158 if (mMinMode == LINES) { 6159 if (linecount < mMinimum) { 6160 desired += getLineHeight() * (mMinimum - linecount); 6161 } 6162 } else { 6163 desired = Math.max(desired, mMinimum); 6164 } 6165 6166 // Check against our minimum height 6167 desired = Math.max(desired, getSuggestedMinimumHeight()); 6168 6169 return desired; 6170 } 6171 6172 /** 6173 * Check whether a change to the existing text layout requires a 6174 * new view layout. 6175 */ 6176 private void checkForResize() { 6177 boolean sizeChanged = false; 6178 6179 if (mLayout != null) { 6180 // Check if our width changed 6181 if (mLayoutParams.width == LayoutParams.WRAP_CONTENT) { 6182 sizeChanged = true; 6183 invalidate(); 6184 } 6185 6186 // Check if our height changed 6187 if (mLayoutParams.height == LayoutParams.WRAP_CONTENT) { 6188 int desiredHeight = getDesiredHeight(); 6189 6190 if (desiredHeight != this.getHeight()) { 6191 sizeChanged = true; 6192 } 6193 } else if (mLayoutParams.height == LayoutParams.MATCH_PARENT) { 6194 if (mDesiredHeightAtMeasure >= 0) { 6195 int desiredHeight = getDesiredHeight(); 6196 6197 if (desiredHeight != mDesiredHeightAtMeasure) { 6198 sizeChanged = true; 6199 } 6200 } 6201 } 6202 } 6203 6204 if (sizeChanged) { 6205 requestLayout(); 6206 // caller will have already invalidated 6207 } 6208 } 6209 6210 /** 6211 * Check whether entirely new text requires a new view layout 6212 * or merely a new text layout. 6213 */ 6214 private void checkForRelayout() { 6215 // If we have a fixed width, we can just swap in a new text layout 6216 // if the text height stays the same or if the view height is fixed. 6217 6218 if ((mLayoutParams.width != LayoutParams.WRAP_CONTENT || 6219 (mMaxWidthMode == mMinWidthMode && mMaxWidth == mMinWidth)) && 6220 (mHint == null || mHintLayout != null) && 6221 (mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight() > 0)) { 6222 // Static width, so try making a new text layout. 6223 6224 int oldht = mLayout.getHeight(); 6225 int want = mLayout.getWidth(); 6226 int hintWant = mHintLayout == null ? 0 : mHintLayout.getWidth(); 6227 6228 /* 6229 * No need to bring the text into view, since the size is not 6230 * changing (unless we do the requestLayout(), in which case it 6231 * will happen at measure). 6232 */ 6233 makeNewLayout(want, hintWant, UNKNOWN_BORING, UNKNOWN_BORING, 6234 mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(), 6235 false); 6236 6237 if (mEllipsize != TextUtils.TruncateAt.MARQUEE) { 6238 // In a fixed-height view, so use our new text layout. 6239 if (mLayoutParams.height != LayoutParams.WRAP_CONTENT && 6240 mLayoutParams.height != LayoutParams.MATCH_PARENT) { 6241 invalidate(); 6242 return; 6243 } 6244 6245 // Dynamic height, but height has stayed the same, 6246 // so use our new text layout. 6247 if (mLayout.getHeight() == oldht && 6248 (mHintLayout == null || mHintLayout.getHeight() == oldht)) { 6249 invalidate(); 6250 return; 6251 } 6252 } 6253 6254 // We lose: the height has changed and we have a dynamic height. 6255 // Request a new view layout using our new text layout. 6256 requestLayout(); 6257 invalidate(); 6258 } else { 6259 // Dynamic width, so we have no choice but to request a new 6260 // view layout with a new text layout. 6261 nullLayouts(); 6262 requestLayout(); 6263 invalidate(); 6264 } 6265 } 6266 6267 @Override 6268 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 6269 super.onLayout(changed, left, top, right, bottom); 6270 if (changed && mEditor != null) mEditor.invalidateTextDisplayList(); 6271 } 6272 6273 private boolean isShowingHint() { 6274 return TextUtils.isEmpty(mText) && !TextUtils.isEmpty(mHint); 6275 } 6276 6277 /** 6278 * Returns true if anything changed. 6279 */ 6280 private boolean bringTextIntoView() { 6281 Layout layout = isShowingHint() ? mHintLayout : mLayout; 6282 int line = 0; 6283 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { 6284 line = layout.getLineCount() - 1; 6285 } 6286 6287 Layout.Alignment a = layout.getParagraphAlignment(line); 6288 int dir = layout.getParagraphDirection(line); 6289 int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); 6290 int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom(); 6291 int ht = layout.getHeight(); 6292 6293 int scrollx, scrolly; 6294 6295 // Convert to left, center, or right alignment. 6296 if (a == Layout.Alignment.ALIGN_NORMAL) { 6297 a = dir == Layout.DIR_LEFT_TO_RIGHT ? Layout.Alignment.ALIGN_LEFT : 6298 Layout.Alignment.ALIGN_RIGHT; 6299 } else if (a == Layout.Alignment.ALIGN_OPPOSITE){ 6300 a = dir == Layout.DIR_LEFT_TO_RIGHT ? Layout.Alignment.ALIGN_RIGHT : 6301 Layout.Alignment.ALIGN_LEFT; 6302 } 6303 6304 if (a == Layout.Alignment.ALIGN_CENTER) { 6305 /* 6306 * Keep centered if possible, or, if it is too wide to fit, 6307 * keep leading edge in view. 6308 */ 6309 6310 int left = (int) FloatMath.floor(layout.getLineLeft(line)); 6311 int right = (int) FloatMath.ceil(layout.getLineRight(line)); 6312 6313 if (right - left < hspace) { 6314 scrollx = (right + left) / 2 - hspace / 2; 6315 } else { 6316 if (dir < 0) { 6317 scrollx = right - hspace; 6318 } else { 6319 scrollx = left; 6320 } 6321 } 6322 } else if (a == Layout.Alignment.ALIGN_RIGHT) { 6323 int right = (int) FloatMath.ceil(layout.getLineRight(line)); 6324 scrollx = right - hspace; 6325 } else { // a == Layout.Alignment.ALIGN_LEFT (will also be the default) 6326 scrollx = (int) FloatMath.floor(layout.getLineLeft(line)); 6327 } 6328 6329 if (ht < vspace) { 6330 scrolly = 0; 6331 } else { 6332 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { 6333 scrolly = ht - vspace; 6334 } else { 6335 scrolly = 0; 6336 } 6337 } 6338 6339 if (scrollx != mScrollX || scrolly != mScrollY) { 6340 scrollTo(scrollx, scrolly); 6341 return true; 6342 } else { 6343 return false; 6344 } 6345 } 6346 6347 /** 6348 * Move the point, specified by the offset, into the view if it is needed. 6349 * This has to be called after layout. Returns true if anything changed. 6350 */ 6351 public boolean bringPointIntoView(int offset) { 6352 boolean changed = false; 6353 6354 Layout layout = isShowingHint() ? mHintLayout: mLayout; 6355 6356 if (layout == null) return changed; 6357 6358 int line = layout.getLineForOffset(offset); 6359 6360 // FIXME: Is it okay to truncate this, or should we round? 6361 final int x = (int)layout.getPrimaryHorizontal(offset); 6362 final int top = layout.getLineTop(line); 6363 final int bottom = layout.getLineTop(line + 1); 6364 6365 int left = (int) FloatMath.floor(layout.getLineLeft(line)); 6366 int right = (int) FloatMath.ceil(layout.getLineRight(line)); 6367 int ht = layout.getHeight(); 6368 6369 int grav; 6370 6371 switch (layout.getParagraphAlignment(line)) { 6372 case ALIGN_LEFT: 6373 grav = 1; 6374 break; 6375 case ALIGN_RIGHT: 6376 grav = -1; 6377 break; 6378 case ALIGN_NORMAL: 6379 grav = layout.getParagraphDirection(line); 6380 break; 6381 case ALIGN_OPPOSITE: 6382 grav = -layout.getParagraphDirection(line); 6383 break; 6384 case ALIGN_CENTER: 6385 default: 6386 grav = 0; 6387 break; 6388 } 6389 6390 int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); 6391 int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom(); 6392 6393 int hslack = (bottom - top) / 2; 6394 int vslack = hslack; 6395 6396 if (vslack > vspace / 4) 6397 vslack = vspace / 4; 6398 if (hslack > hspace / 4) 6399 hslack = hspace / 4; 6400 6401 int hs = mScrollX; 6402 int vs = mScrollY; 6403 6404 if (top - vs < vslack) 6405 vs = top - vslack; 6406 if (bottom - vs > vspace - vslack) 6407 vs = bottom - (vspace - vslack); 6408 if (ht - vs < vspace) 6409 vs = ht - vspace; 6410 if (0 - vs > 0) 6411 vs = 0; 6412 6413 if (grav != 0) { 6414 if (x - hs < hslack) { 6415 hs = x - hslack; 6416 } 6417 if (x - hs > hspace - hslack) { 6418 hs = x - (hspace - hslack); 6419 } 6420 } 6421 6422 if (grav < 0) { 6423 if (left - hs > 0) 6424 hs = left; 6425 if (right - hs < hspace) 6426 hs = right - hspace; 6427 } else if (grav > 0) { 6428 if (right - hs < hspace) 6429 hs = right - hspace; 6430 if (left - hs > 0) 6431 hs = left; 6432 } else /* grav == 0 */ { 6433 if (right - left <= hspace) { 6434 /* 6435 * If the entire text fits, center it exactly. 6436 */ 6437 hs = left - (hspace - (right - left)) / 2; 6438 } else if (x > right - hslack) { 6439 /* 6440 * If we are near the right edge, keep the right edge 6441 * at the edge of the view. 6442 */ 6443 hs = right - hspace; 6444 } else if (x < left + hslack) { 6445 /* 6446 * If we are near the left edge, keep the left edge 6447 * at the edge of the view. 6448 */ 6449 hs = left; 6450 } else if (left > hs) { 6451 /* 6452 * Is there whitespace visible at the left? Fix it if so. 6453 */ 6454 hs = left; 6455 } else if (right < hs + hspace) { 6456 /* 6457 * Is there whitespace visible at the right? Fix it if so. 6458 */ 6459 hs = right - hspace; 6460 } else { 6461 /* 6462 * Otherwise, float as needed. 6463 */ 6464 if (x - hs < hslack) { 6465 hs = x - hslack; 6466 } 6467 if (x - hs > hspace - hslack) { 6468 hs = x - (hspace - hslack); 6469 } 6470 } 6471 } 6472 6473 if (hs != mScrollX || vs != mScrollY) { 6474 if (mScroller == null) { 6475 scrollTo(hs, vs); 6476 } else { 6477 long duration = AnimationUtils.currentAnimationTimeMillis() - mLastScroll; 6478 int dx = hs - mScrollX; 6479 int dy = vs - mScrollY; 6480 6481 if (duration > ANIMATED_SCROLL_GAP) { 6482 mScroller.startScroll(mScrollX, mScrollY, dx, dy); 6483 awakenScrollBars(mScroller.getDuration()); 6484 invalidate(); 6485 } else { 6486 if (!mScroller.isFinished()) { 6487 mScroller.abortAnimation(); 6488 } 6489 6490 scrollBy(dx, dy); 6491 } 6492 6493 mLastScroll = AnimationUtils.currentAnimationTimeMillis(); 6494 } 6495 6496 changed = true; 6497 } 6498 6499 if (isFocused()) { 6500 // This offsets because getInterestingRect() is in terms of viewport coordinates, but 6501 // requestRectangleOnScreen() is in terms of content coordinates. 6502 6503 // The offsets here are to ensure the rectangle we are using is 6504 // within our view bounds, in case the cursor is on the far left 6505 // or right. If it isn't withing the bounds, then this request 6506 // will be ignored. 6507 if (mTempRect == null) mTempRect = new Rect(); 6508 mTempRect.set(x - 2, top, x + 2, bottom); 6509 getInterestingRect(mTempRect, line); 6510 mTempRect.offset(mScrollX, mScrollY); 6511 6512 if (requestRectangleOnScreen(mTempRect)) { 6513 changed = true; 6514 } 6515 } 6516 6517 return changed; 6518 } 6519 6520 /** 6521 * Move the cursor, if needed, so that it is at an offset that is visible 6522 * to the user. This will not move the cursor if it represents more than 6523 * one character (a selection range). This will only work if the 6524 * TextView contains spannable text; otherwise it will do nothing. 6525 * 6526 * @return True if the cursor was actually moved, false otherwise. 6527 */ 6528 public boolean moveCursorToVisibleOffset() { 6529 if (!(mText instanceof Spannable)) { 6530 return false; 6531 } 6532 int start = getSelectionStart(); 6533 int end = getSelectionEnd(); 6534 if (start != end) { 6535 return false; 6536 } 6537 6538 // First: make sure the line is visible on screen: 6539 6540 int line = mLayout.getLineForOffset(start); 6541 6542 final int top = mLayout.getLineTop(line); 6543 final int bottom = mLayout.getLineTop(line + 1); 6544 final int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom(); 6545 int vslack = (bottom - top) / 2; 6546 if (vslack > vspace / 4) 6547 vslack = vspace / 4; 6548 final int vs = mScrollY; 6549 6550 if (top < (vs+vslack)) { 6551 line = mLayout.getLineForVertical(vs+vslack+(bottom-top)); 6552 } else if (bottom > (vspace+vs-vslack)) { 6553 line = mLayout.getLineForVertical(vspace+vs-vslack-(bottom-top)); 6554 } 6555 6556 // Next: make sure the character is visible on screen: 6557 6558 final int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); 6559 final int hs = mScrollX; 6560 final int leftChar = mLayout.getOffsetForHorizontal(line, hs); 6561 final int rightChar = mLayout.getOffsetForHorizontal(line, hspace+hs); 6562 6563 // line might contain bidirectional text 6564 final int lowChar = leftChar < rightChar ? leftChar : rightChar; 6565 final int highChar = leftChar > rightChar ? leftChar : rightChar; 6566 6567 int newStart = start; 6568 if (newStart < lowChar) { 6569 newStart = lowChar; 6570 } else if (newStart > highChar) { 6571 newStart = highChar; 6572 } 6573 6574 if (newStart != start) { 6575 Selection.setSelection((Spannable)mText, newStart); 6576 return true; 6577 } 6578 6579 return false; 6580 } 6581 6582 @Override 6583 public void computeScroll() { 6584 if (mScroller != null) { 6585 if (mScroller.computeScrollOffset()) { 6586 mScrollX = mScroller.getCurrX(); 6587 mScrollY = mScroller.getCurrY(); 6588 invalidateParentCaches(); 6589 postInvalidate(); // So we draw again 6590 } 6591 } 6592 } 6593 6594 private void getInterestingRect(Rect r, int line) { 6595 convertFromViewportToContentCoordinates(r); 6596 6597 // Rectangle can can be expanded on first and last line to take 6598 // padding into account. 6599 // TODO Take left/right padding into account too? 6600 if (line == 0) r.top -= getExtendedPaddingTop(); 6601 if (line == mLayout.getLineCount() - 1) r.bottom += getExtendedPaddingBottom(); 6602 } 6603 6604 private void convertFromViewportToContentCoordinates(Rect r) { 6605 final int horizontalOffset = viewportToContentHorizontalOffset(); 6606 r.left += horizontalOffset; 6607 r.right += horizontalOffset; 6608 6609 final int verticalOffset = viewportToContentVerticalOffset(); 6610 r.top += verticalOffset; 6611 r.bottom += verticalOffset; 6612 } 6613 6614 int viewportToContentHorizontalOffset() { 6615 return getCompoundPaddingLeft() - mScrollX; 6616 } 6617 6618 int viewportToContentVerticalOffset() { 6619 int offset = getExtendedPaddingTop() - mScrollY; 6620 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { 6621 offset += getVerticalOffset(false); 6622 } 6623 return offset; 6624 } 6625 6626 @Override 6627 public void debug(int depth) { 6628 super.debug(depth); 6629 6630 String output = debugIndent(depth); 6631 output += "frame={" + mLeft + ", " + mTop + ", " + mRight 6632 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY 6633 + "} "; 6634 6635 if (mText != null) { 6636 6637 output += "mText=\"" + mText + "\" "; 6638 if (mLayout != null) { 6639 output += "mLayout width=" + mLayout.getWidth() 6640 + " height=" + mLayout.getHeight(); 6641 } 6642 } else { 6643 output += "mText=NULL"; 6644 } 6645 Log.d(VIEW_LOG_TAG, output); 6646 } 6647 6648 /** 6649 * Convenience for {@link Selection#getSelectionStart}. 6650 */ 6651 @ViewDebug.ExportedProperty(category = "text") 6652 public int getSelectionStart() { 6653 return Selection.getSelectionStart(getText()); 6654 } 6655 6656 /** 6657 * Convenience for {@link Selection#getSelectionEnd}. 6658 */ 6659 @ViewDebug.ExportedProperty(category = "text") 6660 public int getSelectionEnd() { 6661 return Selection.getSelectionEnd(getText()); 6662 } 6663 6664 /** 6665 * Return true iff there is a selection inside this text view. 6666 */ 6667 public boolean hasSelection() { 6668 final int selectionStart = getSelectionStart(); 6669 final int selectionEnd = getSelectionEnd(); 6670 6671 return selectionStart >= 0 && selectionStart != selectionEnd; 6672 } 6673 6674 /** 6675 * Sets the properties of this field (lines, horizontally scrolling, 6676 * transformation method) to be for a single-line input. 6677 * 6678 * @attr ref android.R.styleable#TextView_singleLine 6679 */ 6680 public void setSingleLine() { 6681 setSingleLine(true); 6682 } 6683 6684 /** 6685 * Sets the properties of this field to transform input to ALL CAPS 6686 * display. This may use a "small caps" formatting if available. 6687 * This setting will be ignored if this field is editable or selectable. 6688 * 6689 * This call replaces the current transformation method. Disabling this 6690 * will not necessarily restore the previous behavior from before this 6691 * was enabled. 6692 * 6693 * @see #setTransformationMethod(TransformationMethod) 6694 * @attr ref android.R.styleable#TextView_textAllCaps 6695 */ 6696 public void setAllCaps(boolean allCaps) { 6697 if (allCaps) { 6698 setTransformationMethod(new AllCapsTransformationMethod(getContext())); 6699 } else { 6700 setTransformationMethod(null); 6701 } 6702 } 6703 6704 /** 6705 * If true, sets the properties of this field (number of lines, horizontally scrolling, 6706 * transformation method) to be for a single-line input; if false, restores these to the default 6707 * conditions. 6708 * 6709 * Note that the default conditions are not necessarily those that were in effect prior this 6710 * method, and you may want to reset these properties to your custom values. 6711 * 6712 * @attr ref android.R.styleable#TextView_singleLine 6713 */ 6714 @android.view.RemotableViewMethod 6715 public void setSingleLine(boolean singleLine) { 6716 // Could be used, but may break backward compatibility. 6717 // if (mSingleLine == singleLine) return; 6718 setInputTypeSingleLine(singleLine); 6719 applySingleLine(singleLine, true, true); 6720 } 6721 6722 /** 6723 * Adds or remove the EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE on the mInputType. 6724 * @param singleLine 6725 */ 6726 private void setInputTypeSingleLine(boolean singleLine) { 6727 if (mEditor != null && 6728 (mEditor.mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { 6729 if (singleLine) { 6730 mEditor.mInputType &= ~EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; 6731 } else { 6732 mEditor.mInputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; 6733 } 6734 } 6735 } 6736 6737 private void applySingleLine(boolean singleLine, boolean applyTransformation, 6738 boolean changeMaxLines) { 6739 mSingleLine = singleLine; 6740 if (singleLine) { 6741 setLines(1); 6742 setHorizontallyScrolling(true); 6743 if (applyTransformation) { 6744 setTransformationMethod(SingleLineTransformationMethod.getInstance()); 6745 } 6746 } else { 6747 if (changeMaxLines) { 6748 setMaxLines(Integer.MAX_VALUE); 6749 } 6750 setHorizontallyScrolling(false); 6751 if (applyTransformation) { 6752 setTransformationMethod(null); 6753 } 6754 } 6755 } 6756 6757 /** 6758 * Causes words in the text that are longer than the view is wide 6759 * to be ellipsized instead of broken in the middle. You may also 6760 * want to {@link #setSingleLine} or {@link #setHorizontallyScrolling} 6761 * to constrain the text to a single line. Use <code>null</code> 6762 * to turn off ellipsizing. 6763 * 6764 * If {@link #setMaxLines} has been used to set two or more lines, 6765 * {@link android.text.TextUtils.TruncateAt#END} and 6766 * {@link android.text.TextUtils.TruncateAt#MARQUEE}* are only supported 6767 * (other ellipsizing types will not do anything). 6768 * 6769 * @attr ref android.R.styleable#TextView_ellipsize 6770 */ 6771 public void setEllipsize(TextUtils.TruncateAt where) { 6772 // TruncateAt is an enum. != comparison is ok between these singleton objects. 6773 if (mEllipsize != where) { 6774 mEllipsize = where; 6775 6776 if (mLayout != null) { 6777 nullLayouts(); 6778 requestLayout(); 6779 invalidate(); 6780 } 6781 } 6782 } 6783 6784 /** 6785 * Sets how many times to repeat the marquee animation. Only applied if the 6786 * TextView has marquee enabled. Set to -1 to repeat indefinitely. 6787 * 6788 * @see #getMarqueeRepeatLimit() 6789 * 6790 * @attr ref android.R.styleable#TextView_marqueeRepeatLimit 6791 */ 6792 public void setMarqueeRepeatLimit(int marqueeLimit) { 6793 mMarqueeRepeatLimit = marqueeLimit; 6794 } 6795 6796 /** 6797 * Gets the number of times the marquee animation is repeated. Only meaningful if the 6798 * TextView has marquee enabled. 6799 * 6800 * @return the number of times the marquee animation is repeated. -1 if the animation 6801 * repeats indefinitely 6802 * 6803 * @see #setMarqueeRepeatLimit(int) 6804 * 6805 * @attr ref android.R.styleable#TextView_marqueeRepeatLimit 6806 */ 6807 public int getMarqueeRepeatLimit() { 6808 return mMarqueeRepeatLimit; 6809 } 6810 6811 /** 6812 * Returns where, if anywhere, words that are longer than the view 6813 * is wide should be ellipsized. 6814 */ 6815 @ViewDebug.ExportedProperty 6816 public TextUtils.TruncateAt getEllipsize() { 6817 return mEllipsize; 6818 } 6819 6820 /** 6821 * Set the TextView so that when it takes focus, all the text is 6822 * selected. 6823 * 6824 * @attr ref android.R.styleable#TextView_selectAllOnFocus 6825 */ 6826 @android.view.RemotableViewMethod 6827 public void setSelectAllOnFocus(boolean selectAllOnFocus) { 6828 createEditorIfNeeded("setSelectAllOnFocus"); 6829 mEditor.mSelectAllOnFocus = selectAllOnFocus; 6830 6831 if (selectAllOnFocus && !(mText instanceof Spannable)) { 6832 setText(mText, BufferType.SPANNABLE); 6833 } 6834 } 6835 6836 /** 6837 * Set whether the cursor is visible. The default is true. Note that this property only 6838 * makes sense for editable TextView. 6839 * 6840 * @see #isCursorVisible() 6841 * 6842 * @attr ref android.R.styleable#TextView_cursorVisible 6843 */ 6844 @android.view.RemotableViewMethod 6845 public void setCursorVisible(boolean visible) { 6846 if (visible && mEditor == null) return; // visible is the default value with no edit data 6847 createEditorIfNeeded("setCursorVisible"); 6848 if (mEditor.mCursorVisible != visible) { 6849 mEditor.mCursorVisible = visible; 6850 invalidate(); 6851 6852 mEditor.makeBlink(); 6853 6854 // InsertionPointCursorController depends on mCursorVisible 6855 mEditor.prepareCursorControllers(); 6856 } 6857 } 6858 6859 /** 6860 * @return whether or not the cursor is visible (assuming this TextView is editable) 6861 * 6862 * @see #setCursorVisible(boolean) 6863 * 6864 * @attr ref android.R.styleable#TextView_cursorVisible 6865 */ 6866 public boolean isCursorVisible() { 6867 // true is the default value 6868 return mEditor == null ? true : mEditor.mCursorVisible; 6869 } 6870 6871 private boolean canMarquee() { 6872 int width = (mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight()); 6873 return width > 0 && (mLayout.getLineWidth(0) > width || 6874 (mMarqueeFadeMode != MARQUEE_FADE_NORMAL && mSavedMarqueeModeLayout != null && 6875 mSavedMarqueeModeLayout.getLineWidth(0) > width)); 6876 } 6877 6878 private void startMarquee() { 6879 // Do not ellipsize EditText 6880 if (getKeyListener() != null) return; 6881 6882 if (compressText(getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight())) { 6883 return; 6884 } 6885 6886 if ((mMarquee == null || mMarquee.isStopped()) && (isFocused() || isSelected()) && 6887 getLineCount() == 1 && canMarquee()) { 6888 6889 if (mMarqueeFadeMode == MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { 6890 mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_FADE; 6891 final Layout tmp = mLayout; 6892 mLayout = mSavedMarqueeModeLayout; 6893 mSavedMarqueeModeLayout = tmp; 6894 setHorizontalFadingEdgeEnabled(true); 6895 requestLayout(); 6896 invalidate(); 6897 } 6898 6899 if (mMarquee == null) mMarquee = new Marquee(this); 6900 mMarquee.start(mMarqueeRepeatLimit); 6901 } 6902 } 6903 6904 private void stopMarquee() { 6905 if (mMarquee != null && !mMarquee.isStopped()) { 6906 mMarquee.stop(); 6907 } 6908 6909 if (mMarqueeFadeMode == MARQUEE_FADE_SWITCH_SHOW_FADE) { 6910 mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS; 6911 final Layout tmp = mSavedMarqueeModeLayout; 6912 mSavedMarqueeModeLayout = mLayout; 6913 mLayout = tmp; 6914 setHorizontalFadingEdgeEnabled(false); 6915 requestLayout(); 6916 invalidate(); 6917 } 6918 } 6919 6920 private void startStopMarquee(boolean start) { 6921 if (mEllipsize == TextUtils.TruncateAt.MARQUEE) { 6922 if (start) { 6923 startMarquee(); 6924 } else { 6925 stopMarquee(); 6926 } 6927 } 6928 } 6929 6930 /** 6931 * This method is called when the text is changed, in case any subclasses 6932 * would like to know. 6933 * 6934 * Within <code>text</code>, the <code>lengthAfter</code> characters 6935 * beginning at <code>start</code> have just replaced old text that had 6936 * length <code>lengthBefore</code>. It is an error to attempt to make 6937 * changes to <code>text</code> from this callback. 6938 * 6939 * @param text The text the TextView is displaying 6940 * @param start The offset of the start of the range of the text that was 6941 * modified 6942 * @param lengthBefore The length of the former text that has been replaced 6943 * @param lengthAfter The length of the replacement modified text 6944 */ 6945 protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { 6946 // intentionally empty, template pattern method can be overridden by subclasses 6947 } 6948 6949 /** 6950 * This method is called when the selection has changed, in case any 6951 * subclasses would like to know. 6952 * 6953 * @param selStart The new selection start location. 6954 * @param selEnd The new selection end location. 6955 */ 6956 protected void onSelectionChanged(int selStart, int selEnd) { 6957 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED); 6958 } 6959 6960 /** 6961 * Adds a TextWatcher to the list of those whose methods are called 6962 * whenever this TextView's text changes. 6963 * <p> 6964 * In 1.0, the {@link TextWatcher#afterTextChanged} method was erroneously 6965 * not called after {@link #setText} calls. Now, doing {@link #setText} 6966 * if there are any text changed listeners forces the buffer type to 6967 * Editable if it would not otherwise be and does call this method. 6968 */ 6969 public void addTextChangedListener(TextWatcher watcher) { 6970 if (mListeners == null) { 6971 mListeners = new ArrayList<TextWatcher>(); 6972 } 6973 6974 mListeners.add(watcher); 6975 } 6976 6977 /** 6978 * Removes the specified TextWatcher from the list of those whose 6979 * methods are called 6980 * whenever this TextView's text changes. 6981 */ 6982 public void removeTextChangedListener(TextWatcher watcher) { 6983 if (mListeners != null) { 6984 int i = mListeners.indexOf(watcher); 6985 6986 if (i >= 0) { 6987 mListeners.remove(i); 6988 } 6989 } 6990 } 6991 6992 private void sendBeforeTextChanged(CharSequence text, int start, int before, int after) { 6993 if (mListeners != null) { 6994 final ArrayList<TextWatcher> list = mListeners; 6995 final int count = list.size(); 6996 for (int i = 0; i < count; i++) { 6997 list.get(i).beforeTextChanged(text, start, before, after); 6998 } 6999 } 7000 7001 // The spans that are inside or intersect the modified region no longer make sense 7002 removeIntersectingSpans(start, start + before, SpellCheckSpan.class); 7003 removeIntersectingSpans(start, start + before, SuggestionSpan.class); 7004 } 7005 7006 // Removes all spans that are inside or actually overlap the start..end range 7007 private <T> void removeIntersectingSpans(int start, int end, Class<T> type) { 7008 if (!(mText instanceof Editable)) return; 7009 Editable text = (Editable) mText; 7010 7011 T[] spans = text.getSpans(start, end, type); 7012 final int length = spans.length; 7013 for (int i = 0; i < length; i++) { 7014 final int s = text.getSpanStart(spans[i]); 7015 final int e = text.getSpanEnd(spans[i]); 7016 // Spans that are adjacent to the edited region will be handled in 7017 // updateSpellCheckSpans. Result depends on what will be added (space or text) 7018 if (e == start || s == end) break; 7019 text.removeSpan(spans[i]); 7020 } 7021 } 7022 7023 /** 7024 * Not private so it can be called from an inner class without going 7025 * through a thunk. 7026 */ 7027 void sendOnTextChanged(CharSequence text, int start, int before, int after) { 7028 if (mListeners != null) { 7029 final ArrayList<TextWatcher> list = mListeners; 7030 final int count = list.size(); 7031 for (int i = 0; i < count; i++) { 7032 list.get(i).onTextChanged(text, start, before, after); 7033 } 7034 } 7035 7036 if (mEditor != null) mEditor.sendOnTextChanged(start, after); 7037 } 7038 7039 /** 7040 * Not private so it can be called from an inner class without going 7041 * through a thunk. 7042 */ 7043 void sendAfterTextChanged(Editable text) { 7044 if (mListeners != null) { 7045 final ArrayList<TextWatcher> list = mListeners; 7046 final int count = list.size(); 7047 for (int i = 0; i < count; i++) { 7048 list.get(i).afterTextChanged(text); 7049 } 7050 } 7051 } 7052 7053 void updateAfterEdit() { 7054 invalidate(); 7055 int curs = getSelectionStart(); 7056 7057 if (curs >= 0 || (mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { 7058 registerForPreDraw(); 7059 } 7060 7061 if (curs >= 0) { 7062 mHighlightPathBogus = true; 7063 if (mEditor != null) mEditor.makeBlink(); 7064 bringPointIntoView(curs); 7065 } 7066 7067 checkForResize(); 7068 } 7069 7070 /** 7071 * Not private so it can be called from an inner class without going 7072 * through a thunk. 7073 */ 7074 void handleTextChanged(CharSequence buffer, int start, int before, int after) { 7075 final Editor.InputMethodState ims = mEditor == null ? null : mEditor.mInputMethodState; 7076 if (ims == null || ims.mBatchEditNesting == 0) { 7077 updateAfterEdit(); 7078 } 7079 if (ims != null) { 7080 ims.mContentChanged = true; 7081 if (ims.mChangedStart < 0) { 7082 ims.mChangedStart = start; 7083 ims.mChangedEnd = start+before; 7084 } else { 7085 ims.mChangedStart = Math.min(ims.mChangedStart, start); 7086 ims.mChangedEnd = Math.max(ims.mChangedEnd, start + before - ims.mChangedDelta); 7087 } 7088 ims.mChangedDelta += after-before; 7089 } 7090 7091 sendOnTextChanged(buffer, start, before, after); 7092 onTextChanged(buffer, start, before, after); 7093 } 7094 7095 /** 7096 * Not private so it can be called from an inner class without going 7097 * through a thunk. 7098 */ 7099 void spanChange(Spanned buf, Object what, int oldStart, int newStart, int oldEnd, int newEnd) { 7100 // XXX Make the start and end move together if this ends up 7101 // spending too much time invalidating. 7102 7103 boolean selChanged = false; 7104 int newSelStart=-1, newSelEnd=-1; 7105 7106 final Editor.InputMethodState ims = mEditor == null ? null : mEditor.mInputMethodState; 7107 7108 if (what == Selection.SELECTION_END) { 7109 selChanged = true; 7110 newSelEnd = newStart; 7111 7112 if (oldStart >= 0 || newStart >= 0) { 7113 invalidateCursor(Selection.getSelectionStart(buf), oldStart, newStart); 7114 registerForPreDraw(); 7115 if (mEditor != null) mEditor.makeBlink(); 7116 } 7117 } 7118 7119 if (what == Selection.SELECTION_START) { 7120 selChanged = true; 7121 newSelStart = newStart; 7122 7123 if (oldStart >= 0 || newStart >= 0) { 7124 int end = Selection.getSelectionEnd(buf); 7125 invalidateCursor(end, oldStart, newStart); 7126 } 7127 } 7128 7129 if (selChanged) { 7130 mHighlightPathBogus = true; 7131 if (mEditor != null && !isFocused()) mEditor.mSelectionMoved = true; 7132 7133 if ((buf.getSpanFlags(what)&Spanned.SPAN_INTERMEDIATE) == 0) { 7134 if (newSelStart < 0) { 7135 newSelStart = Selection.getSelectionStart(buf); 7136 } 7137 if (newSelEnd < 0) { 7138 newSelEnd = Selection.getSelectionEnd(buf); 7139 } 7140 onSelectionChanged(newSelStart, newSelEnd); 7141 } 7142 } 7143 7144 if (what instanceof UpdateAppearance || what instanceof ParagraphStyle || 7145 what instanceof CharacterStyle) { 7146 if (ims == null || ims.mBatchEditNesting == 0) { 7147 invalidate(); 7148 mHighlightPathBogus = true; 7149 checkForResize(); 7150 } else { 7151 ims.mContentChanged = true; 7152 } 7153 if (mEditor != null) { 7154 if (oldStart >= 0) mEditor.invalidateTextDisplayList(mLayout, oldStart, oldEnd); 7155 if (newStart >= 0) mEditor.invalidateTextDisplayList(mLayout, newStart, newEnd); 7156 } 7157 } 7158 7159 if (MetaKeyKeyListener.isMetaTracker(buf, what)) { 7160 mHighlightPathBogus = true; 7161 if (ims != null && MetaKeyKeyListener.isSelectingMetaTracker(buf, what)) { 7162 ims.mSelectionModeChanged = true; 7163 } 7164 7165 if (Selection.getSelectionStart(buf) >= 0) { 7166 if (ims == null || ims.mBatchEditNesting == 0) { 7167 invalidateCursor(); 7168 } else { 7169 ims.mCursorChanged = true; 7170 } 7171 } 7172 } 7173 7174 if (what instanceof ParcelableSpan) { 7175 // If this is a span that can be sent to a remote process, 7176 // the current extract editor would be interested in it. 7177 if (ims != null && ims.mExtractedTextRequest != null) { 7178 if (ims.mBatchEditNesting != 0) { 7179 if (oldStart >= 0) { 7180 if (ims.mChangedStart > oldStart) { 7181 ims.mChangedStart = oldStart; 7182 } 7183 if (ims.mChangedStart > oldEnd) { 7184 ims.mChangedStart = oldEnd; 7185 } 7186 } 7187 if (newStart >= 0) { 7188 if (ims.mChangedStart > newStart) { 7189 ims.mChangedStart = newStart; 7190 } 7191 if (ims.mChangedStart > newEnd) { 7192 ims.mChangedStart = newEnd; 7193 } 7194 } 7195 } else { 7196 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "Span change outside of batch: " 7197 + oldStart + "-" + oldEnd + "," 7198 + newStart + "-" + newEnd + " " + what); 7199 ims.mContentChanged = true; 7200 } 7201 } 7202 } 7203 7204 if (mEditor != null && mEditor.mSpellChecker != null && newStart < 0 && 7205 what instanceof SpellCheckSpan) { 7206 mEditor.mSpellChecker.removeSpellCheckSpan((SpellCheckSpan) what); 7207 } 7208 } 7209 7210 /** 7211 * @hide 7212 */ 7213 @Override 7214 public void dispatchFinishTemporaryDetach() { 7215 mDispatchTemporaryDetach = true; 7216 super.dispatchFinishTemporaryDetach(); 7217 mDispatchTemporaryDetach = false; 7218 } 7219 7220 @Override 7221 public void onStartTemporaryDetach() { 7222 super.onStartTemporaryDetach(); 7223 // Only track when onStartTemporaryDetach() is called directly, 7224 // usually because this instance is an editable field in a list 7225 if (!mDispatchTemporaryDetach) mTemporaryDetach = true; 7226 7227 // Because of View recycling in ListView, there is no easy way to know when a TextView with 7228 // selection becomes visible again. Until a better solution is found, stop text selection 7229 // mode (if any) as soon as this TextView is recycled. 7230 if (mEditor != null) mEditor.hideControllers(); 7231 } 7232 7233 @Override 7234 public void onFinishTemporaryDetach() { 7235 super.onFinishTemporaryDetach(); 7236 // Only track when onStartTemporaryDetach() is called directly, 7237 // usually because this instance is an editable field in a list 7238 if (!mDispatchTemporaryDetach) mTemporaryDetach = false; 7239 } 7240 7241 @Override 7242 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { 7243 if (mTemporaryDetach) { 7244 // If we are temporarily in the detach state, then do nothing. 7245 super.onFocusChanged(focused, direction, previouslyFocusedRect); 7246 return; 7247 } 7248 7249 if (mEditor != null) mEditor.onFocusChanged(focused, direction); 7250 7251 if (focused) { 7252 if (mText instanceof Spannable) { 7253 Spannable sp = (Spannable) mText; 7254 MetaKeyKeyListener.resetMetaState(sp); 7255 } 7256 } 7257 7258 startStopMarquee(focused); 7259 7260 if (mTransformation != null) { 7261 mTransformation.onFocusChanged(this, mText, focused, direction, previouslyFocusedRect); 7262 } 7263 7264 super.onFocusChanged(focused, direction, previouslyFocusedRect); 7265 } 7266 7267 @Override 7268 public void onWindowFocusChanged(boolean hasWindowFocus) { 7269 super.onWindowFocusChanged(hasWindowFocus); 7270 7271 if (mEditor != null) mEditor.onWindowFocusChanged(hasWindowFocus); 7272 7273 startStopMarquee(hasWindowFocus); 7274 } 7275 7276 @Override 7277 protected void onVisibilityChanged(View changedView, int visibility) { 7278 super.onVisibilityChanged(changedView, visibility); 7279 if (mEditor != null && visibility != VISIBLE) { 7280 mEditor.hideControllers(); 7281 } 7282 } 7283 7284 /** 7285 * Use {@link BaseInputConnection#removeComposingSpans 7286 * BaseInputConnection.removeComposingSpans()} to remove any IME composing 7287 * state from this text view. 7288 */ 7289 public void clearComposingText() { 7290 if (mText instanceof Spannable) { 7291 BaseInputConnection.removeComposingSpans((Spannable)mText); 7292 } 7293 } 7294 7295 @Override 7296 public void setSelected(boolean selected) { 7297 boolean wasSelected = isSelected(); 7298 7299 super.setSelected(selected); 7300 7301 if (selected != wasSelected && mEllipsize == TextUtils.TruncateAt.MARQUEE) { 7302 if (selected) { 7303 startMarquee(); 7304 } else { 7305 stopMarquee(); 7306 } 7307 } 7308 } 7309 7310 @Override 7311 public boolean onTouchEvent(MotionEvent event) { 7312 final int action = event.getActionMasked(); 7313 7314 if (mEditor != null) mEditor.onTouchEvent(event); 7315 7316 final boolean superResult = super.onTouchEvent(event); 7317 7318 /* 7319 * Don't handle the release after a long press, because it will 7320 * move the selection away from whatever the menu action was 7321 * trying to affect. 7322 */ 7323 if (mEditor != null && mEditor.mDiscardNextActionUp && action == MotionEvent.ACTION_UP) { 7324 mEditor.mDiscardNextActionUp = false; 7325 return superResult; 7326 } 7327 7328 final boolean touchIsFinished = (action == MotionEvent.ACTION_UP) && 7329 (mEditor == null || !mEditor.mIgnoreActionUpEvent) && isFocused(); 7330 7331 if ((mMovement != null || onCheckIsTextEditor()) && isEnabled() 7332 && mText instanceof Spannable && mLayout != null) { 7333 boolean handled = false; 7334 7335 if (mMovement != null) { 7336 handled |= mMovement.onTouchEvent(this, (Spannable) mText, event); 7337 } 7338 7339 final boolean textIsSelectable = isTextSelectable(); 7340 if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && textIsSelectable) { 7341 // The LinkMovementMethod which should handle taps on links has not been installed 7342 // on non editable text that support text selection. 7343 // We reproduce its behavior here to open links for these. 7344 ClickableSpan[] links = ((Spannable) mText).getSpans(getSelectionStart(), 7345 getSelectionEnd(), ClickableSpan.class); 7346 7347 if (links.length > 0) { 7348 links[0].onClick(this); 7349 handled = true; 7350 } 7351 } 7352 7353 if (touchIsFinished && (isTextEditable() || textIsSelectable)) { 7354 // Show the IME, except when selecting in read-only text. 7355 final InputMethodManager imm = InputMethodManager.peekInstance(); 7356 viewClicked(imm); 7357 if (!textIsSelectable && mEditor.mShowSoftInputOnFocus) { 7358 handled |= imm != null && imm.showSoftInput(this, 0); 7359 } 7360 7361 // The above condition ensures that the mEditor is not null 7362 mEditor.onTouchUpEvent(event); 7363 7364 handled = true; 7365 } 7366 7367 if (handled) { 7368 return true; 7369 } 7370 } 7371 7372 return superResult; 7373 } 7374 7375 @Override 7376 public boolean onGenericMotionEvent(MotionEvent event) { 7377 if (mMovement != null && mText instanceof Spannable && mLayout != null) { 7378 try { 7379 if (mMovement.onGenericMotionEvent(this, (Spannable) mText, event)) { 7380 return true; 7381 } 7382 } catch (AbstractMethodError ex) { 7383 // onGenericMotionEvent was added to the MovementMethod interface in API 12. 7384 // Ignore its absence in case third party applications implemented the 7385 // interface directly. 7386 } 7387 } 7388 return super.onGenericMotionEvent(event); 7389 } 7390 7391 /** 7392 * @return True iff this TextView contains a text that can be edited, or if this is 7393 * a selectable TextView. 7394 */ 7395 boolean isTextEditable() { 7396 return mText instanceof Editable && onCheckIsTextEditor() && isEnabled(); 7397 } 7398 7399 /** 7400 * Returns true, only while processing a touch gesture, if the initial 7401 * touch down event caused focus to move to the text view and as a result 7402 * its selection changed. Only valid while processing the touch gesture 7403 * of interest, in an editable text view. 7404 */ 7405 public boolean didTouchFocusSelect() { 7406 return mEditor != null && mEditor.mTouchFocusSelected; 7407 } 7408 7409 @Override 7410 public void cancelLongPress() { 7411 super.cancelLongPress(); 7412 if (mEditor != null) mEditor.mIgnoreActionUpEvent = true; 7413 } 7414 7415 @Override 7416 public boolean onTrackballEvent(MotionEvent event) { 7417 if (mMovement != null && mText instanceof Spannable && mLayout != null) { 7418 if (mMovement.onTrackballEvent(this, (Spannable) mText, event)) { 7419 return true; 7420 } 7421 } 7422 7423 return super.onTrackballEvent(event); 7424 } 7425 7426 public void setScroller(Scroller s) { 7427 mScroller = s; 7428 } 7429 7430 @Override 7431 protected float getLeftFadingEdgeStrength() { 7432 if (mEllipsize == TextUtils.TruncateAt.MARQUEE && 7433 mMarqueeFadeMode != MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { 7434 if (mMarquee != null && !mMarquee.isStopped()) { 7435 final Marquee marquee = mMarquee; 7436 if (marquee.shouldDrawLeftFade()) { 7437 return marquee.mScroll / getHorizontalFadingEdgeLength(); 7438 } else { 7439 return 0.0f; 7440 } 7441 } else if (getLineCount() == 1) { 7442 final int layoutDirection = getResolvedLayoutDirection(); 7443 final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection); 7444 switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) { 7445 case Gravity.LEFT: 7446 return 0.0f; 7447 case Gravity.RIGHT: 7448 return (mLayout.getLineRight(0) - (mRight - mLeft) - 7449 getCompoundPaddingLeft() - getCompoundPaddingRight() - 7450 mLayout.getLineLeft(0)) / getHorizontalFadingEdgeLength(); 7451 case Gravity.CENTER_HORIZONTAL: 7452 return 0.0f; 7453 } 7454 } 7455 } 7456 return super.getLeftFadingEdgeStrength(); 7457 } 7458 7459 @Override 7460 protected float getRightFadingEdgeStrength() { 7461 if (mEllipsize == TextUtils.TruncateAt.MARQUEE && 7462 mMarqueeFadeMode != MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { 7463 if (mMarquee != null && !mMarquee.isStopped()) { 7464 final Marquee marquee = mMarquee; 7465 return (marquee.mMaxFadeScroll - marquee.mScroll) / getHorizontalFadingEdgeLength(); 7466 } else if (getLineCount() == 1) { 7467 final int layoutDirection = getResolvedLayoutDirection(); 7468 final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection); 7469 switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) { 7470 case Gravity.LEFT: 7471 final int textWidth = (mRight - mLeft) - getCompoundPaddingLeft() - 7472 getCompoundPaddingRight(); 7473 final float lineWidth = mLayout.getLineWidth(0); 7474 return (lineWidth - textWidth) / getHorizontalFadingEdgeLength(); 7475 case Gravity.RIGHT: 7476 return 0.0f; 7477 case Gravity.CENTER_HORIZONTAL: 7478 case Gravity.FILL_HORIZONTAL: 7479 return (mLayout.getLineWidth(0) - ((mRight - mLeft) - 7480 getCompoundPaddingLeft() - getCompoundPaddingRight())) / 7481 getHorizontalFadingEdgeLength(); 7482 } 7483 } 7484 } 7485 return super.getRightFadingEdgeStrength(); 7486 } 7487 7488 @Override 7489 protected int computeHorizontalScrollRange() { 7490 if (mLayout != null) { 7491 return mSingleLine && (mGravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.LEFT ? 7492 (int) mLayout.getLineWidth(0) : mLayout.getWidth(); 7493 } 7494 7495 return super.computeHorizontalScrollRange(); 7496 } 7497 7498 @Override 7499 protected int computeVerticalScrollRange() { 7500 if (mLayout != null) 7501 return mLayout.getHeight(); 7502 7503 return super.computeVerticalScrollRange(); 7504 } 7505 7506 @Override 7507 protected int computeVerticalScrollExtent() { 7508 return getHeight() - getCompoundPaddingTop() - getCompoundPaddingBottom(); 7509 } 7510 7511 @Override 7512 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) { 7513 super.findViewsWithText(outViews, searched, flags); 7514 if (!outViews.contains(this) && (flags & FIND_VIEWS_WITH_TEXT) != 0 7515 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mText)) { 7516 String searchedLowerCase = searched.toString().toLowerCase(); 7517 String textLowerCase = mText.toString().toLowerCase(); 7518 if (textLowerCase.contains(searchedLowerCase)) { 7519 outViews.add(this); 7520 } 7521 } 7522 } 7523 7524 public enum BufferType { 7525 NORMAL, SPANNABLE, EDITABLE, 7526 } 7527 7528 /** 7529 * Returns the TextView_textColor attribute from the 7530 * Resources.StyledAttributes, if set, or the TextAppearance_textColor 7531 * from the TextView_textAppearance attribute, if TextView_textColor 7532 * was not set directly. 7533 */ 7534 public static ColorStateList getTextColors(Context context, TypedArray attrs) { 7535 ColorStateList colors; 7536 colors = attrs.getColorStateList(com.android.internal.R.styleable. 7537 TextView_textColor); 7538 7539 if (colors == null) { 7540 int ap = attrs.getResourceId(com.android.internal.R.styleable. 7541 TextView_textAppearance, -1); 7542 if (ap != -1) { 7543 TypedArray appearance; 7544 appearance = context.obtainStyledAttributes(ap, 7545 com.android.internal.R.styleable.TextAppearance); 7546 colors = appearance.getColorStateList(com.android.internal.R.styleable. 7547 TextAppearance_textColor); 7548 appearance.recycle(); 7549 } 7550 } 7551 7552 return colors; 7553 } 7554 7555 /** 7556 * Returns the default color from the TextView_textColor attribute 7557 * from the AttributeSet, if set, or the default color from the 7558 * TextAppearance_textColor from the TextView_textAppearance attribute, 7559 * if TextView_textColor was not set directly. 7560 */ 7561 public static int getTextColor(Context context, 7562 TypedArray attrs, 7563 int def) { 7564 ColorStateList colors = getTextColors(context, attrs); 7565 7566 if (colors == null) { 7567 return def; 7568 } else { 7569 return colors.getDefaultColor(); 7570 } 7571 } 7572 7573 @Override 7574 public boolean onKeyShortcut(int keyCode, KeyEvent event) { 7575 final int filteredMetaState = event.getMetaState() & ~KeyEvent.META_CTRL_MASK; 7576 if (KeyEvent.metaStateHasNoModifiers(filteredMetaState)) { 7577 switch (keyCode) { 7578 case KeyEvent.KEYCODE_A: 7579 if (canSelectText()) { 7580 return onTextContextMenuItem(ID_SELECT_ALL); 7581 } 7582 break; 7583 case KeyEvent.KEYCODE_X: 7584 if (canCut()) { 7585 return onTextContextMenuItem(ID_CUT); 7586 } 7587 break; 7588 case KeyEvent.KEYCODE_C: 7589 if (canCopy()) { 7590 return onTextContextMenuItem(ID_COPY); 7591 } 7592 break; 7593 case KeyEvent.KEYCODE_V: 7594 if (canPaste()) { 7595 return onTextContextMenuItem(ID_PASTE); 7596 } 7597 break; 7598 } 7599 } 7600 return super.onKeyShortcut(keyCode, event); 7601 } 7602 7603 /** 7604 * Unlike {@link #textCanBeSelected()}, this method is based on the <i>current</i> state of the 7605 * TextView. {@link #textCanBeSelected()} has to be true (this is one of the conditions to have 7606 * a selection controller (see {@link Editor#prepareCursorControllers()}), but this is not 7607 * sufficient. 7608 */ 7609 private boolean canSelectText() { 7610 return mText.length() != 0 && mEditor != null && mEditor.hasSelectionController(); 7611 } 7612 7613 /** 7614 * Test based on the <i>intrinsic</i> charateristics of the TextView. 7615 * The text must be spannable and the movement method must allow for arbitary selection. 7616 * 7617 * See also {@link #canSelectText()}. 7618 */ 7619 boolean textCanBeSelected() { 7620 // prepareCursorController() relies on this method. 7621 // If you change this condition, make sure prepareCursorController is called anywhere 7622 // the value of this condition might be changed. 7623 if (mMovement == null || !mMovement.canSelectArbitrarily()) return false; 7624 return isTextEditable() || 7625 (isTextSelectable() && mText instanceof Spannable && isEnabled()); 7626 } 7627 7628 /** 7629 * This is a temporary method. Future versions may support multi-locale text. 7630 * 7631 * @return The locale that should be used for a word iterator and a spell checker 7632 * in this TextView, based on the current spell checker settings, 7633 * the current IME's locale, or the system default locale. 7634 * @hide 7635 */ 7636 public Locale getTextServicesLocale() { 7637 Locale locale = Locale.getDefault(); 7638 final TextServicesManager textServicesManager = (TextServicesManager) 7639 mContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE); 7640 final SpellCheckerSubtype subtype = textServicesManager.getCurrentSpellCheckerSubtype(true); 7641 if (subtype != null) { 7642 locale = new Locale(subtype.getLocale()); 7643 } 7644 return locale; 7645 } 7646 7647 void onLocaleChanged() { 7648 // Will be re-created on demand in getWordIterator with the proper new locale 7649 mEditor.mWordIterator = null; 7650 } 7651 7652 /** 7653 * This method is used by the ArrowKeyMovementMethod to jump from one word to the other. 7654 * Made available to achieve a consistent behavior. 7655 * @hide 7656 */ 7657 public WordIterator getWordIterator() { 7658 if (mEditor != null) { 7659 return mEditor.getWordIterator(); 7660 } else { 7661 return null; 7662 } 7663 } 7664 7665 @Override 7666 public void onPopulateAccessibilityEvent(AccessibilityEvent event) { 7667 super.onPopulateAccessibilityEvent(event); 7668 7669 final boolean isPassword = hasPasswordTransformationMethod(); 7670 if (!isPassword) { 7671 CharSequence text = getTextForAccessibility(); 7672 if (!TextUtils.isEmpty(text)) { 7673 event.getText().add(text); 7674 } 7675 } 7676 } 7677 7678 @Override 7679 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 7680 super.onInitializeAccessibilityEvent(event); 7681 7682 event.setClassName(TextView.class.getName()); 7683 final boolean isPassword = hasPasswordTransformationMethod(); 7684 event.setPassword(isPassword); 7685 7686 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED) { 7687 event.setFromIndex(Selection.getSelectionStart(mText)); 7688 event.setToIndex(Selection.getSelectionEnd(mText)); 7689 event.setItemCount(mText.length()); 7690 } 7691 } 7692 7693 @Override 7694 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 7695 super.onInitializeAccessibilityNodeInfo(info); 7696 7697 info.setClassName(TextView.class.getName()); 7698 final boolean isPassword = hasPasswordTransformationMethod(); 7699 info.setPassword(isPassword); 7700 7701 if (!isPassword) { 7702 info.setText(getTextForAccessibility()); 7703 } 7704 7705 if (TextUtils.isEmpty(getContentDescription()) 7706 && !TextUtils.isEmpty(mText)) { 7707 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY); 7708 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY); 7709 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER 7710 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD 7711 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_LINE 7712 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH 7713 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PAGE); 7714 } 7715 } 7716 7717 @Override 7718 public void sendAccessibilityEvent(int eventType) { 7719 // Do not send scroll events since first they are not interesting for 7720 // accessibility and second such events a generated too frequently. 7721 // For details see the implementation of bringTextIntoView(). 7722 if (eventType == AccessibilityEvent.TYPE_VIEW_SCROLLED) { 7723 return; 7724 } 7725 super.sendAccessibilityEvent(eventType); 7726 } 7727 7728 /** 7729 * Gets the text reported for accessibility purposes. 7730 * 7731 * @return The accessibility text. 7732 * 7733 * @hide 7734 */ 7735 public CharSequence getTextForAccessibility() { 7736 CharSequence text = getText(); 7737 if (TextUtils.isEmpty(text)) { 7738 text = getHint(); 7739 } 7740 return text; 7741 } 7742 7743 void sendAccessibilityEventTypeViewTextChanged(CharSequence beforeText, 7744 int fromIndex, int removedCount, int addedCount) { 7745 AccessibilityEvent event = 7746 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED); 7747 event.setFromIndex(fromIndex); 7748 event.setRemovedCount(removedCount); 7749 event.setAddedCount(addedCount); 7750 event.setBeforeText(beforeText); 7751 sendAccessibilityEventUnchecked(event); 7752 } 7753 7754 /** 7755 * Returns whether this text view is a current input method target. The 7756 * default implementation just checks with {@link InputMethodManager}. 7757 */ 7758 public boolean isInputMethodTarget() { 7759 InputMethodManager imm = InputMethodManager.peekInstance(); 7760 return imm != null && imm.isActive(this); 7761 } 7762 7763 static final int ID_SELECT_ALL = android.R.id.selectAll; 7764 static final int ID_CUT = android.R.id.cut; 7765 static final int ID_COPY = android.R.id.copy; 7766 static final int ID_PASTE = android.R.id.paste; 7767 7768 /** 7769 * Called when a context menu option for the text view is selected. Currently 7770 * this will be one of {@link android.R.id#selectAll}, {@link android.R.id#cut}, 7771 * {@link android.R.id#copy} or {@link android.R.id#paste}. 7772 * 7773 * @return true if the context menu item action was performed. 7774 */ 7775 public boolean onTextContextMenuItem(int id) { 7776 int min = 0; 7777 int max = mText.length(); 7778 7779 if (isFocused()) { 7780 final int selStart = getSelectionStart(); 7781 final int selEnd = getSelectionEnd(); 7782 7783 min = Math.max(0, Math.min(selStart, selEnd)); 7784 max = Math.max(0, Math.max(selStart, selEnd)); 7785 } 7786 7787 switch (id) { 7788 case ID_SELECT_ALL: 7789 // This does not enter text selection mode. Text is highlighted, so that it can be 7790 // bulk edited, like selectAllOnFocus does. Returns true even if text is empty. 7791 selectAllText(); 7792 return true; 7793 7794 case ID_PASTE: 7795 paste(min, max); 7796 return true; 7797 7798 case ID_CUT: 7799 setPrimaryClip(ClipData.newPlainText(null, getTransformedText(min, max))); 7800 deleteText_internal(min, max); 7801 stopSelectionActionMode(); 7802 return true; 7803 7804 case ID_COPY: 7805 setPrimaryClip(ClipData.newPlainText(null, getTransformedText(min, max))); 7806 stopSelectionActionMode(); 7807 return true; 7808 } 7809 return false; 7810 } 7811 7812 CharSequence getTransformedText(int start, int end) { 7813 return removeSuggestionSpans(mTransformed.subSequence(start, end)); 7814 } 7815 7816 @Override 7817 public boolean performLongClick() { 7818 boolean handled = false; 7819 7820 if (super.performLongClick()) { 7821 handled = true; 7822 } 7823 7824 if (mEditor != null) { 7825 handled |= mEditor.performLongClick(handled); 7826 } 7827 7828 if (handled) { 7829 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); 7830 if (mEditor != null) mEditor.mDiscardNextActionUp = true; 7831 } 7832 7833 return handled; 7834 } 7835 7836 @Override 7837 protected void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) { 7838 super.onScrollChanged(horiz, vert, oldHoriz, oldVert); 7839 if (mEditor != null) { 7840 mEditor.onScrollChanged(); 7841 } 7842 } 7843 7844 /** 7845 * Return whether or not suggestions are enabled on this TextView. The suggestions are generated 7846 * by the IME or by the spell checker as the user types. This is done by adding 7847 * {@link SuggestionSpan}s to the text. 7848 * 7849 * When suggestions are enabled (default), this list of suggestions will be displayed when the 7850 * user asks for them on these parts of the text. This value depends on the inputType of this 7851 * TextView. 7852 * 7853 * The class of the input type must be {@link InputType#TYPE_CLASS_TEXT}. 7854 * 7855 * In addition, the type variation must be one of 7856 * {@link InputType#TYPE_TEXT_VARIATION_NORMAL}, 7857 * {@link InputType#TYPE_TEXT_VARIATION_EMAIL_SUBJECT}, 7858 * {@link InputType#TYPE_TEXT_VARIATION_LONG_MESSAGE}, 7859 * {@link InputType#TYPE_TEXT_VARIATION_SHORT_MESSAGE} or 7860 * {@link InputType#TYPE_TEXT_VARIATION_WEB_EDIT_TEXT}. 7861 * 7862 * And finally, the {@link InputType#TYPE_TEXT_FLAG_NO_SUGGESTIONS} flag must <i>not</i> be set. 7863 * 7864 * @return true if the suggestions popup window is enabled, based on the inputType. 7865 */ 7866 public boolean isSuggestionsEnabled() { 7867 if (mEditor == null) return false; 7868 if ((mEditor.mInputType & InputType.TYPE_MASK_CLASS) != InputType.TYPE_CLASS_TEXT) { 7869 return false; 7870 } 7871 if ((mEditor.mInputType & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS) > 0) return false; 7872 7873 final int variation = mEditor.mInputType & EditorInfo.TYPE_MASK_VARIATION; 7874 return (variation == EditorInfo.TYPE_TEXT_VARIATION_NORMAL || 7875 variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_SUBJECT || 7876 variation == EditorInfo.TYPE_TEXT_VARIATION_LONG_MESSAGE || 7877 variation == EditorInfo.TYPE_TEXT_VARIATION_SHORT_MESSAGE || 7878 variation == EditorInfo.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT); 7879 } 7880 7881 /** 7882 * If provided, this ActionMode.Callback will be used to create the ActionMode when text 7883 * selection is initiated in this View. 7884 * 7885 * The standard implementation populates the menu with a subset of Select All, Cut, Copy and 7886 * Paste actions, depending on what this View supports. 7887 * 7888 * A custom implementation can add new entries in the default menu in its 7889 * {@link android.view.ActionMode.Callback#onPrepareActionMode(ActionMode, Menu)} method. The 7890 * default actions can also be removed from the menu using {@link Menu#removeItem(int)} and 7891 * passing {@link android.R.id#selectAll}, {@link android.R.id#cut}, {@link android.R.id#copy} 7892 * or {@link android.R.id#paste} ids as parameters. 7893 * 7894 * Returning false from 7895 * {@link android.view.ActionMode.Callback#onCreateActionMode(ActionMode, Menu)} will prevent 7896 * the action mode from being started. 7897 * 7898 * Action click events should be handled by the custom implementation of 7899 * {@link android.view.ActionMode.Callback#onActionItemClicked(ActionMode, MenuItem)}. 7900 * 7901 * Note that text selection mode is not started when a TextView receives focus and the 7902 * {@link android.R.attr#selectAllOnFocus} flag has been set. The content is highlighted in 7903 * that case, to allow for quick replacement. 7904 */ 7905 public void setCustomSelectionActionModeCallback(ActionMode.Callback actionModeCallback) { 7906 createEditorIfNeeded("custom selection action mode set"); 7907 mEditor.mCustomSelectionActionModeCallback = actionModeCallback; 7908 } 7909 7910 /** 7911 * Retrieves the value set in {@link #setCustomSelectionActionModeCallback}. Default is null. 7912 * 7913 * @return The current custom selection callback. 7914 */ 7915 public ActionMode.Callback getCustomSelectionActionModeCallback() { 7916 return mEditor == null ? null : mEditor.mCustomSelectionActionModeCallback; 7917 } 7918 7919 /** 7920 * @hide 7921 */ 7922 protected void stopSelectionActionMode() { 7923 mEditor.stopSelectionActionMode(); 7924 } 7925 7926 boolean canCut() { 7927 if (hasPasswordTransformationMethod()) { 7928 return false; 7929 } 7930 7931 if (mText.length() > 0 && hasSelection() && mText instanceof Editable && mEditor != null && 7932 mEditor.mKeyListener != null) { 7933 return true; 7934 } 7935 7936 return false; 7937 } 7938 7939 boolean canCopy() { 7940 if (hasPasswordTransformationMethod()) { 7941 return false; 7942 } 7943 7944 if (mText.length() > 0 && hasSelection()) { 7945 return true; 7946 } 7947 7948 return false; 7949 } 7950 7951 boolean canPaste() { 7952 return (mText instanceof Editable && 7953 mEditor != null && mEditor.mKeyListener != null && 7954 getSelectionStart() >= 0 && 7955 getSelectionEnd() >= 0 && 7956 ((ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE)). 7957 hasPrimaryClip()); 7958 } 7959 7960 boolean selectAllText() { 7961 final int length = mText.length(); 7962 Selection.setSelection((Spannable) mText, 0, length); 7963 return length > 0; 7964 } 7965 7966 /** 7967 * Prepare text so that there are not zero or two spaces at beginning and end of region defined 7968 * by [min, max] when replacing this region by paste. 7969 * Note that if there were two spaces (or more) at that position before, they are kept. We just 7970 * make sure we do not add an extra one from the paste content. 7971 */ 7972 long prepareSpacesAroundPaste(int min, int max, CharSequence paste) { 7973 if (paste.length() > 0) { 7974 if (min > 0) { 7975 final char charBefore = mTransformed.charAt(min - 1); 7976 final char charAfter = paste.charAt(0); 7977 7978 if (Character.isSpaceChar(charBefore) && Character.isSpaceChar(charAfter)) { 7979 // Two spaces at beginning of paste: remove one 7980 final int originalLength = mText.length(); 7981 deleteText_internal(min - 1, min); 7982 // Due to filters, there is no guarantee that exactly one character was 7983 // removed: count instead. 7984 final int delta = mText.length() - originalLength; 7985 min += delta; 7986 max += delta; 7987 } else if (!Character.isSpaceChar(charBefore) && charBefore != '\n' && 7988 !Character.isSpaceChar(charAfter) && charAfter != '\n') { 7989 // No space at beginning of paste: add one 7990 final int originalLength = mText.length(); 7991 replaceText_internal(min, min, " "); 7992 // Taking possible filters into account as above. 7993 final int delta = mText.length() - originalLength; 7994 min += delta; 7995 max += delta; 7996 } 7997 } 7998 7999 if (max < mText.length()) { 8000 final char charBefore = paste.charAt(paste.length() - 1); 8001 final char charAfter = mTransformed.charAt(max); 8002 8003 if (Character.isSpaceChar(charBefore) && Character.isSpaceChar(charAfter)) { 8004 // Two spaces at end of paste: remove one 8005 deleteText_internal(max, max + 1); 8006 } else if (!Character.isSpaceChar(charBefore) && charBefore != '\n' && 8007 !Character.isSpaceChar(charAfter) && charAfter != '\n') { 8008 // No space at end of paste: add one 8009 replaceText_internal(max, max, " "); 8010 } 8011 } 8012 } 8013 8014 return TextUtils.packRangeInLong(min, max); 8015 } 8016 8017 /** 8018 * Paste clipboard content between min and max positions. 8019 */ 8020 private void paste(int min, int max) { 8021 ClipboardManager clipboard = 8022 (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE); 8023 ClipData clip = clipboard.getPrimaryClip(); 8024 if (clip != null) { 8025 boolean didFirst = false; 8026 for (int i=0; i<clip.getItemCount(); i++) { 8027 CharSequence paste = clip.getItemAt(i).coerceToStyledText(getContext()); 8028 if (paste != null) { 8029 if (!didFirst) { 8030 long minMax = prepareSpacesAroundPaste(min, max, paste); 8031 min = TextUtils.unpackRangeStartFromLong(minMax); 8032 max = TextUtils.unpackRangeEndFromLong(minMax); 8033 Selection.setSelection((Spannable) mText, max); 8034 ((Editable) mText).replace(min, max, paste); 8035 didFirst = true; 8036 } else { 8037 ((Editable) mText).insert(getSelectionEnd(), "\n"); 8038 ((Editable) mText).insert(getSelectionEnd(), paste); 8039 } 8040 } 8041 } 8042 stopSelectionActionMode(); 8043 LAST_CUT_OR_COPY_TIME = 0; 8044 } 8045 } 8046 8047 private void setPrimaryClip(ClipData clip) { 8048 ClipboardManager clipboard = (ClipboardManager) getContext(). 8049 getSystemService(Context.CLIPBOARD_SERVICE); 8050 clipboard.setPrimaryClip(clip); 8051 LAST_CUT_OR_COPY_TIME = SystemClock.uptimeMillis(); 8052 } 8053 8054 /** 8055 * Get the character offset closest to the specified absolute position. A typical use case is to 8056 * pass the result of {@link MotionEvent#getX()} and {@link MotionEvent#getY()} to this method. 8057 * 8058 * @param x The horizontal absolute position of a point on screen 8059 * @param y The vertical absolute position of a point on screen 8060 * @return the character offset for the character whose position is closest to the specified 8061 * position. Returns -1 if there is no layout. 8062 */ 8063 public int getOffsetForPosition(float x, float y) { 8064 if (getLayout() == null) return -1; 8065 final int line = getLineAtCoordinate(y); 8066 final int offset = getOffsetAtCoordinate(line, x); 8067 return offset; 8068 } 8069 8070 float convertToLocalHorizontalCoordinate(float x) { 8071 x -= getTotalPaddingLeft(); 8072 // Clamp the position to inside of the view. 8073 x = Math.max(0.0f, x); 8074 x = Math.min(getWidth() - getTotalPaddingRight() - 1, x); 8075 x += getScrollX(); 8076 return x; 8077 } 8078 8079 int getLineAtCoordinate(float y) { 8080 y -= getTotalPaddingTop(); 8081 // Clamp the position to inside of the view. 8082 y = Math.max(0.0f, y); 8083 y = Math.min(getHeight() - getTotalPaddingBottom() - 1, y); 8084 y += getScrollY(); 8085 return getLayout().getLineForVertical((int) y); 8086 } 8087 8088 private int getOffsetAtCoordinate(int line, float x) { 8089 x = convertToLocalHorizontalCoordinate(x); 8090 return getLayout().getOffsetForHorizontal(line, x); 8091 } 8092 8093 @Override 8094 public boolean onDragEvent(DragEvent event) { 8095 switch (event.getAction()) { 8096 case DragEvent.ACTION_DRAG_STARTED: 8097 return mEditor != null && mEditor.hasInsertionController(); 8098 8099 case DragEvent.ACTION_DRAG_ENTERED: 8100 TextView.this.requestFocus(); 8101 return true; 8102 8103 case DragEvent.ACTION_DRAG_LOCATION: 8104 final int offset = getOffsetForPosition(event.getX(), event.getY()); 8105 Selection.setSelection((Spannable)mText, offset); 8106 return true; 8107 8108 case DragEvent.ACTION_DROP: 8109 if (mEditor != null) mEditor.onDrop(event); 8110 return true; 8111 8112 case DragEvent.ACTION_DRAG_ENDED: 8113 case DragEvent.ACTION_DRAG_EXITED: 8114 default: 8115 return true; 8116 } 8117 } 8118 8119 boolean isInBatchEditMode() { 8120 if (mEditor == null) return false; 8121 final Editor.InputMethodState ims = mEditor.mInputMethodState; 8122 if (ims != null) { 8123 return ims.mBatchEditNesting > 0; 8124 } 8125 return mEditor.mInBatchEditControllers; 8126 } 8127 8128 @Override 8129 public void onResolvedTextDirectionChanged() { 8130 if (hasPasswordTransformationMethod()) { 8131 // TODO: take care of the content direction to show the password text and dots justified 8132 // to the left or to the right 8133 mTextDir = TextDirectionHeuristics.LOCALE; 8134 return; 8135 } 8136 8137 // Always need to resolve layout direction first 8138 final boolean defaultIsRtl = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL); 8139 8140 // Now, we can select the heuristic 8141 int textDir = getResolvedTextDirection(); 8142 switch (textDir) { 8143 default: 8144 case TEXT_DIRECTION_FIRST_STRONG: 8145 mTextDir = (defaultIsRtl ? TextDirectionHeuristics.FIRSTSTRONG_RTL : 8146 TextDirectionHeuristics.FIRSTSTRONG_LTR); 8147 break; 8148 case TEXT_DIRECTION_ANY_RTL: 8149 mTextDir = TextDirectionHeuristics.ANYRTL_LTR; 8150 break; 8151 case TEXT_DIRECTION_LTR: 8152 mTextDir = TextDirectionHeuristics.LTR; 8153 break; 8154 case TEXT_DIRECTION_RTL: 8155 mTextDir = TextDirectionHeuristics.RTL; 8156 break; 8157 case TEXT_DIRECTION_LOCALE: 8158 mTextDir = TextDirectionHeuristics.LOCALE; 8159 break; 8160 } 8161 } 8162 8163 /** 8164 * Subclasses will need to override this method to implement their own way of resolving 8165 * drawables depending on the layout direction. 8166 * 8167 * A call to the super method will be required from the subclasses implementation. 8168 */ 8169 protected void resolveDrawables() { 8170 // No need to resolve twice 8171 if (mResolvedDrawables) { 8172 return; 8173 } 8174 // No drawable to resolve 8175 if (mDrawables == null) { 8176 return; 8177 } 8178 // No relative drawable to resolve 8179 if (mDrawables.mDrawableStart == null && mDrawables.mDrawableEnd == null) { 8180 mResolvedDrawables = true; 8181 return; 8182 } 8183 8184 Drawables dr = mDrawables; 8185 switch(getResolvedLayoutDirection()) { 8186 case LAYOUT_DIRECTION_RTL: 8187 if (dr.mDrawableStart != null) { 8188 dr.mDrawableRight = dr.mDrawableStart; 8189 8190 dr.mDrawableSizeRight = dr.mDrawableSizeStart; 8191 dr.mDrawableHeightRight = dr.mDrawableHeightStart; 8192 } 8193 if (dr.mDrawableEnd != null) { 8194 dr.mDrawableLeft = dr.mDrawableEnd; 8195 8196 dr.mDrawableSizeLeft = dr.mDrawableSizeEnd; 8197 dr.mDrawableHeightLeft = dr.mDrawableHeightEnd; 8198 } 8199 break; 8200 8201 case LAYOUT_DIRECTION_LTR: 8202 default: 8203 if (dr.mDrawableStart != null) { 8204 dr.mDrawableLeft = dr.mDrawableStart; 8205 8206 dr.mDrawableSizeLeft = dr.mDrawableSizeStart; 8207 dr.mDrawableHeightLeft = dr.mDrawableHeightStart; 8208 } 8209 if (dr.mDrawableEnd != null) { 8210 dr.mDrawableRight = dr.mDrawableEnd; 8211 8212 dr.mDrawableSizeRight = dr.mDrawableSizeEnd; 8213 dr.mDrawableHeightRight = dr.mDrawableHeightEnd; 8214 } 8215 break; 8216 } 8217 mResolvedDrawables = true; 8218 } 8219 8220 protected void resetResolvedDrawables() { 8221 mResolvedDrawables = false; 8222 } 8223 8224 /** 8225 * @hide 8226 */ 8227 protected void viewClicked(InputMethodManager imm) { 8228 if (imm != null) { 8229 imm.viewClicked(this); 8230 } 8231 } 8232 8233 /** 8234 * Deletes the range of text [start, end[. 8235 * @hide 8236 */ 8237 protected void deleteText_internal(int start, int end) { 8238 ((Editable) mText).delete(start, end); 8239 } 8240 8241 /** 8242 * Replaces the range of text [start, end[ by replacement text 8243 * @hide 8244 */ 8245 protected void replaceText_internal(int start, int end, CharSequence text) { 8246 ((Editable) mText).replace(start, end, text); 8247 } 8248 8249 /** 8250 * Sets a span on the specified range of text 8251 * @hide 8252 */ 8253 protected void setSpan_internal(Object span, int start, int end, int flags) { 8254 ((Editable) mText).setSpan(span, start, end, flags); 8255 } 8256 8257 /** 8258 * Moves the cursor to the specified offset position in text 8259 * @hide 8260 */ 8261 protected void setCursorPosition_internal(int start, int end) { 8262 Selection.setSelection(((Editable) mText), start, end); 8263 } 8264 8265 /** 8266 * An Editor should be created as soon as any of the editable-specific fields (grouped 8267 * inside the Editor object) is assigned to a non-default value. 8268 * This method will create the Editor if needed. 8269 * 8270 * A standard TextView (as well as buttons, checkboxes...) should not qualify and hence will 8271 * have a null Editor, unlike an EditText. Inconsistent in-between states will have an 8272 * Editor for backward compatibility, as soon as one of these fields is assigned. 8273 * 8274 * Also note that for performance reasons, the mEditor is created when needed, but not 8275 * reset when no more edit-specific fields are needed. 8276 */ 8277 private void createEditorIfNeeded(String reason) { 8278 if (mEditor == null) { 8279 if (!(this instanceof EditText)) { 8280 Log.e(LOG_TAG + " EDITOR", "Creating an Editor on a regular TextView. " + reason); 8281 } 8282 mEditor = new Editor(this); 8283 } else { 8284 if (!(this instanceof EditText)) { 8285 Log.d(LOG_TAG + " EDITOR", "Redundant Editor creation. " + reason); 8286 } 8287 } 8288 } 8289 8290 /** 8291 * @hide 8292 */ 8293 @Override 8294 public CharSequence getIterableTextForAccessibility() { 8295 if (getContentDescription() == null) { 8296 if (!(mText instanceof Spannable)) { 8297 setText(mText, BufferType.SPANNABLE); 8298 } 8299 return mText; 8300 } 8301 return super.getIterableTextForAccessibility(); 8302 } 8303 8304 /** 8305 * @hide 8306 */ 8307 @Override 8308 public TextSegmentIterator getIteratorForGranularity(int granularity) { 8309 switch (granularity) { 8310 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_LINE: { 8311 Spannable text = (Spannable) getIterableTextForAccessibility(); 8312 if (!TextUtils.isEmpty(text) && getLayout() != null) { 8313 AccessibilityIterators.LineTextSegmentIterator iterator = 8314 AccessibilityIterators.LineTextSegmentIterator.getInstance(); 8315 iterator.initialize(text, getLayout()); 8316 return iterator; 8317 } 8318 } break; 8319 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PAGE: { 8320 Spannable text = (Spannable) getIterableTextForAccessibility(); 8321 if (!TextUtils.isEmpty(text) && getLayout() != null) { 8322 AccessibilityIterators.PageTextSegmentIterator iterator = 8323 AccessibilityIterators.PageTextSegmentIterator.getInstance(); 8324 iterator.initialize(this); 8325 return iterator; 8326 } 8327 } break; 8328 } 8329 return super.getIteratorForGranularity(granularity); 8330 } 8331 8332 /** 8333 * @hide 8334 */ 8335 @Override 8336 public int getAccessibilityCursorPosition() { 8337 if (TextUtils.isEmpty(getContentDescription())) { 8338 return getSelectionEnd(); 8339 } else { 8340 return super.getAccessibilityCursorPosition(); 8341 } 8342 } 8343 8344 /** 8345 * @hide 8346 */ 8347 @Override 8348 public void setAccessibilityCursorPosition(int index) { 8349 if (getAccessibilityCursorPosition() == index) { 8350 return; 8351 } 8352 if (TextUtils.isEmpty(getContentDescription())) { 8353 if (index >= 0) { 8354 Selection.setSelection((Spannable) mText, index); 8355 } else { 8356 Selection.removeSelection((Spannable) mText); 8357 } 8358 } else { 8359 super.setAccessibilityCursorPosition(index); 8360 } 8361 } 8362 8363 /** 8364 * User interface state that is stored by TextView for implementing 8365 * {@link View#onSaveInstanceState}. 8366 */ 8367 public static class SavedState extends BaseSavedState { 8368 int selStart; 8369 int selEnd; 8370 CharSequence text; 8371 boolean frozenWithFocus; 8372 CharSequence error; 8373 8374 SavedState(Parcelable superState) { 8375 super(superState); 8376 } 8377 8378 @Override 8379 public void writeToParcel(Parcel out, int flags) { 8380 super.writeToParcel(out, flags); 8381 out.writeInt(selStart); 8382 out.writeInt(selEnd); 8383 out.writeInt(frozenWithFocus ? 1 : 0); 8384 TextUtils.writeToParcel(text, out, flags); 8385 8386 if (error == null) { 8387 out.writeInt(0); 8388 } else { 8389 out.writeInt(1); 8390 TextUtils.writeToParcel(error, out, flags); 8391 } 8392 } 8393 8394 @Override 8395 public String toString() { 8396 String str = "TextView.SavedState{" 8397 + Integer.toHexString(System.identityHashCode(this)) 8398 + " start=" + selStart + " end=" + selEnd; 8399 if (text != null) { 8400 str += " text=" + text; 8401 } 8402 return str + "}"; 8403 } 8404 8405 @SuppressWarnings("hiding") 8406 public static final Parcelable.Creator<SavedState> CREATOR 8407 = new Parcelable.Creator<SavedState>() { 8408 public SavedState createFromParcel(Parcel in) { 8409 return new SavedState(in); 8410 } 8411 8412 public SavedState[] newArray(int size) { 8413 return new SavedState[size]; 8414 } 8415 }; 8416 8417 private SavedState(Parcel in) { 8418 super(in); 8419 selStart = in.readInt(); 8420 selEnd = in.readInt(); 8421 frozenWithFocus = (in.readInt() != 0); 8422 text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 8423 8424 if (in.readInt() != 0) { 8425 error = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 8426 } 8427 } 8428 } 8429 8430 private static class CharWrapper implements CharSequence, GetChars, GraphicsOperations { 8431 private char[] mChars; 8432 private int mStart, mLength; 8433 8434 public CharWrapper(char[] chars, int start, int len) { 8435 mChars = chars; 8436 mStart = start; 8437 mLength = len; 8438 } 8439 8440 /* package */ void set(char[] chars, int start, int len) { 8441 mChars = chars; 8442 mStart = start; 8443 mLength = len; 8444 } 8445 8446 public int length() { 8447 return mLength; 8448 } 8449 8450 public char charAt(int off) { 8451 return mChars[off + mStart]; 8452 } 8453 8454 @Override 8455 public String toString() { 8456 return new String(mChars, mStart, mLength); 8457 } 8458 8459 public CharSequence subSequence(int start, int end) { 8460 if (start < 0 || end < 0 || start > mLength || end > mLength) { 8461 throw new IndexOutOfBoundsException(start + ", " + end); 8462 } 8463 8464 return new String(mChars, start + mStart, end - start); 8465 } 8466 8467 public void getChars(int start, int end, char[] buf, int off) { 8468 if (start < 0 || end < 0 || start > mLength || end > mLength) { 8469 throw new IndexOutOfBoundsException(start + ", " + end); 8470 } 8471 8472 System.arraycopy(mChars, start + mStart, buf, off, end - start); 8473 } 8474 8475 public void drawText(Canvas c, int start, int end, 8476 float x, float y, Paint p) { 8477 c.drawText(mChars, start + mStart, end - start, x, y, p); 8478 } 8479 8480 public void drawTextRun(Canvas c, int start, int end, 8481 int contextStart, int contextEnd, float x, float y, int flags, Paint p) { 8482 int count = end - start; 8483 int contextCount = contextEnd - contextStart; 8484 c.drawTextRun(mChars, start + mStart, count, contextStart + mStart, 8485 contextCount, x, y, flags, p); 8486 } 8487 8488 public float measureText(int start, int end, Paint p) { 8489 return p.measureText(mChars, start + mStart, end - start); 8490 } 8491 8492 public int getTextWidths(int start, int end, float[] widths, Paint p) { 8493 return p.getTextWidths(mChars, start + mStart, end - start, widths); 8494 } 8495 8496 public float getTextRunAdvances(int start, int end, int contextStart, 8497 int contextEnd, int flags, float[] advances, int advancesIndex, 8498 Paint p) { 8499 int count = end - start; 8500 int contextCount = contextEnd - contextStart; 8501 return p.getTextRunAdvances(mChars, start + mStart, count, 8502 contextStart + mStart, contextCount, flags, advances, 8503 advancesIndex); 8504 } 8505 8506 public float getTextRunAdvances(int start, int end, int contextStart, 8507 int contextEnd, int flags, float[] advances, int advancesIndex, 8508 Paint p, int reserved) { 8509 int count = end - start; 8510 int contextCount = contextEnd - contextStart; 8511 return p.getTextRunAdvances(mChars, start + mStart, count, 8512 contextStart + mStart, contextCount, flags, advances, 8513 advancesIndex, reserved); 8514 } 8515 8516 public int getTextRunCursor(int contextStart, int contextEnd, int flags, 8517 int offset, int cursorOpt, Paint p) { 8518 int contextCount = contextEnd - contextStart; 8519 return p.getTextRunCursor(mChars, contextStart + mStart, 8520 contextCount, flags, offset + mStart, cursorOpt); 8521 } 8522 } 8523 8524 private static final class Marquee extends Handler { 8525 // TODO: Add an option to configure this 8526 private static final float MARQUEE_DELTA_MAX = 0.07f; 8527 private static final int MARQUEE_DELAY = 1200; 8528 private static final int MARQUEE_RESTART_DELAY = 1200; 8529 private static final int MARQUEE_RESOLUTION = 1000 / 30; 8530 private static final int MARQUEE_PIXELS_PER_SECOND = 30; 8531 8532 private static final byte MARQUEE_STOPPED = 0x0; 8533 private static final byte MARQUEE_STARTING = 0x1; 8534 private static final byte MARQUEE_RUNNING = 0x2; 8535 8536 private static final int MESSAGE_START = 0x1; 8537 private static final int MESSAGE_TICK = 0x2; 8538 private static final int MESSAGE_RESTART = 0x3; 8539 8540 private final WeakReference<TextView> mView; 8541 8542 private byte mStatus = MARQUEE_STOPPED; 8543 private final float mScrollUnit; 8544 private float mMaxScroll; 8545 float mMaxFadeScroll; 8546 private float mGhostStart; 8547 private float mGhostOffset; 8548 private float mFadeStop; 8549 private int mRepeatLimit; 8550 8551 float mScroll; 8552 8553 Marquee(TextView v) { 8554 final float density = v.getContext().getResources().getDisplayMetrics().density; 8555 mScrollUnit = (MARQUEE_PIXELS_PER_SECOND * density) / MARQUEE_RESOLUTION; 8556 mView = new WeakReference<TextView>(v); 8557 } 8558 8559 @Override 8560 public void handleMessage(Message msg) { 8561 switch (msg.what) { 8562 case MESSAGE_START: 8563 mStatus = MARQUEE_RUNNING; 8564 tick(); 8565 break; 8566 case MESSAGE_TICK: 8567 tick(); 8568 break; 8569 case MESSAGE_RESTART: 8570 if (mStatus == MARQUEE_RUNNING) { 8571 if (mRepeatLimit >= 0) { 8572 mRepeatLimit--; 8573 } 8574 start(mRepeatLimit); 8575 } 8576 break; 8577 } 8578 } 8579 8580 void tick() { 8581 if (mStatus != MARQUEE_RUNNING) { 8582 return; 8583 } 8584 8585 removeMessages(MESSAGE_TICK); 8586 8587 final TextView textView = mView.get(); 8588 if (textView != null && (textView.isFocused() || textView.isSelected())) { 8589 mScroll += mScrollUnit; 8590 if (mScroll > mMaxScroll) { 8591 mScroll = mMaxScroll; 8592 sendEmptyMessageDelayed(MESSAGE_RESTART, MARQUEE_RESTART_DELAY); 8593 } else { 8594 sendEmptyMessageDelayed(MESSAGE_TICK, MARQUEE_RESOLUTION); 8595 } 8596 textView.invalidate(); 8597 } 8598 } 8599 8600 void stop() { 8601 mStatus = MARQUEE_STOPPED; 8602 removeMessages(MESSAGE_START); 8603 removeMessages(MESSAGE_RESTART); 8604 removeMessages(MESSAGE_TICK); 8605 resetScroll(); 8606 } 8607 8608 private void resetScroll() { 8609 mScroll = 0.0f; 8610 final TextView textView = mView.get(); 8611 if (textView != null) textView.invalidate(); 8612 } 8613 8614 void start(int repeatLimit) { 8615 if (repeatLimit == 0) { 8616 stop(); 8617 return; 8618 } 8619 mRepeatLimit = repeatLimit; 8620 final TextView textView = mView.get(); 8621 if (textView != null && textView.mLayout != null) { 8622 mStatus = MARQUEE_STARTING; 8623 mScroll = 0.0f; 8624 final int textWidth = textView.getWidth() - textView.getCompoundPaddingLeft() - 8625 textView.getCompoundPaddingRight(); 8626 final float lineWidth = textView.mLayout.getLineWidth(0); 8627 final float gap = textWidth / 3.0f; 8628 mGhostStart = lineWidth - textWidth + gap; 8629 mMaxScroll = mGhostStart + textWidth; 8630 mGhostOffset = lineWidth + gap; 8631 mFadeStop = lineWidth + textWidth / 6.0f; 8632 mMaxFadeScroll = mGhostStart + lineWidth + lineWidth; 8633 8634 textView.invalidate(); 8635 sendEmptyMessageDelayed(MESSAGE_START, MARQUEE_DELAY); 8636 } 8637 } 8638 8639 float getGhostOffset() { 8640 return mGhostOffset; 8641 } 8642 8643 boolean shouldDrawLeftFade() { 8644 return mScroll <= mFadeStop; 8645 } 8646 8647 boolean shouldDrawGhost() { 8648 return mStatus == MARQUEE_RUNNING && mScroll > mGhostStart; 8649 } 8650 8651 boolean isRunning() { 8652 return mStatus == MARQUEE_RUNNING; 8653 } 8654 8655 boolean isStopped() { 8656 return mStatus == MARQUEE_STOPPED; 8657 } 8658 } 8659 8660 private class ChangeWatcher implements TextWatcher, SpanWatcher { 8661 8662 private CharSequence mBeforeText; 8663 8664 public void beforeTextChanged(CharSequence buffer, int start, 8665 int before, int after) { 8666 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "beforeTextChanged start=" + start 8667 + " before=" + before + " after=" + after + ": " + buffer); 8668 8669 if (AccessibilityManager.getInstance(mContext).isEnabled() 8670 && !isPasswordInputType(getInputType()) 8671 && !hasPasswordTransformationMethod()) { 8672 mBeforeText = buffer.toString(); 8673 } 8674 8675 TextView.this.sendBeforeTextChanged(buffer, start, before, after); 8676 } 8677 8678 public void onTextChanged(CharSequence buffer, int start, int before, int after) { 8679 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onTextChanged start=" + start 8680 + " before=" + before + " after=" + after + ": " + buffer); 8681 TextView.this.handleTextChanged(buffer, start, before, after); 8682 8683 if (AccessibilityManager.getInstance(mContext).isEnabled() && 8684 (isFocused() || isSelected() && isShown())) { 8685 sendAccessibilityEventTypeViewTextChanged(mBeforeText, start, before, after); 8686 mBeforeText = null; 8687 } 8688 } 8689 8690 public void afterTextChanged(Editable buffer) { 8691 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "afterTextChanged: " + buffer); 8692 TextView.this.sendAfterTextChanged(buffer); 8693 8694 if (MetaKeyKeyListener.getMetaState(buffer, MetaKeyKeyListener.META_SELECTING) != 0) { 8695 MetaKeyKeyListener.stopSelecting(TextView.this, buffer); 8696 } 8697 } 8698 8699 public void onSpanChanged(Spannable buf, Object what, int s, int e, int st, int en) { 8700 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanChanged s=" + s + " e=" + e 8701 + " st=" + st + " en=" + en + " what=" + what + ": " + buf); 8702 TextView.this.spanChange(buf, what, s, st, e, en); 8703 } 8704 8705 public void onSpanAdded(Spannable buf, Object what, int s, int e) { 8706 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanAdded s=" + s + " e=" + e 8707 + " what=" + what + ": " + buf); 8708 TextView.this.spanChange(buf, what, -1, s, -1, e); 8709 } 8710 8711 public void onSpanRemoved(Spannable buf, Object what, int s, int e) { 8712 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanRemoved s=" + s + " e=" + e 8713 + " what=" + what + ": " + buf); 8714 TextView.this.spanChange(buf, what, s, -1, e, -1); 8715 } 8716 } 8717} 8718