TextView.java revision fa4e2d994bb398b89d08304bb233671ef1a5d5f8
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.ClipData.Item; 22import android.content.ClipboardManager; 23import android.content.Context; 24import android.content.Intent; 25import android.content.pm.PackageManager; 26import android.content.res.ColorStateList; 27import android.content.res.Resources; 28import android.content.res.TypedArray; 29import android.content.res.XmlResourceParser; 30import android.graphics.Canvas; 31import android.graphics.Color; 32import android.graphics.Paint; 33import android.graphics.Path; 34import android.graphics.Rect; 35import android.graphics.RectF; 36import android.graphics.Typeface; 37import android.graphics.drawable.Drawable; 38import android.inputmethodservice.ExtractEditText; 39import android.net.Uri; 40import android.os.Bundle; 41import android.os.Handler; 42import android.os.Message; 43import android.os.Parcel; 44import android.os.Parcelable; 45import android.os.SystemClock; 46import android.provider.Settings; 47import android.text.BoringLayout; 48import android.text.DynamicLayout; 49import android.text.Editable; 50import android.text.GetChars; 51import android.text.GraphicsOperations; 52import android.text.InputFilter; 53import android.text.InputType; 54import android.text.Layout; 55import android.text.ParcelableSpan; 56import android.text.Selection; 57import android.text.SpanWatcher; 58import android.text.Spannable; 59import android.text.SpannableString; 60import android.text.SpannableStringBuilder; 61import android.text.Spanned; 62import android.text.SpannedString; 63import android.text.StaticLayout; 64import android.text.TextDirectionHeuristic; 65import android.text.TextDirectionHeuristics; 66import android.text.TextPaint; 67import android.text.TextUtils; 68import android.text.TextUtils.TruncateAt; 69import android.text.TextWatcher; 70import android.text.method.AllCapsTransformationMethod; 71import android.text.method.ArrowKeyMovementMethod; 72import android.text.method.DateKeyListener; 73import android.text.method.DateTimeKeyListener; 74import android.text.method.DialerKeyListener; 75import android.text.method.DigitsKeyListener; 76import android.text.method.KeyListener; 77import android.text.method.LinkMovementMethod; 78import android.text.method.MetaKeyKeyListener; 79import android.text.method.MovementMethod; 80import android.text.method.PasswordTransformationMethod; 81import android.text.method.SingleLineTransformationMethod; 82import android.text.method.TextKeyListener; 83import android.text.method.TimeKeyListener; 84import android.text.method.TransformationMethod; 85import android.text.method.TransformationMethod2; 86import android.text.method.WordIterator; 87import android.text.style.ClickableSpan; 88import android.text.style.EasyEditSpan; 89import android.text.style.ParagraphStyle; 90import android.text.style.SpellCheckSpan; 91import android.text.style.SuggestionRangeSpan; 92import android.text.style.SuggestionSpan; 93import android.text.style.TextAppearanceSpan; 94import android.text.style.URLSpan; 95import android.text.style.UpdateAppearance; 96import android.text.util.Linkify; 97import android.util.AttributeSet; 98import android.util.DisplayMetrics; 99import android.util.FloatMath; 100import android.util.Log; 101import android.util.TypedValue; 102import android.view.ActionMode; 103import android.view.ActionMode.Callback; 104import android.view.ContextMenu; 105import android.view.DragEvent; 106import android.view.Gravity; 107import android.view.HapticFeedbackConstants; 108import android.view.KeyCharacterMap; 109import android.view.KeyEvent; 110import android.view.LayoutInflater; 111import android.view.Menu; 112import android.view.MenuItem; 113import android.view.MotionEvent; 114import android.view.View; 115import android.view.ViewConfiguration; 116import android.view.ViewDebug; 117import android.view.ViewGroup; 118import android.view.ViewGroup.LayoutParams; 119import android.view.ViewParent; 120import android.view.ViewRootImpl; 121import android.view.ViewTreeObserver; 122import android.view.WindowManager; 123import android.view.accessibility.AccessibilityEvent; 124import android.view.accessibility.AccessibilityManager; 125import android.view.accessibility.AccessibilityNodeInfo; 126import android.view.animation.AnimationUtils; 127import android.view.inputmethod.BaseInputConnection; 128import android.view.inputmethod.CompletionInfo; 129import android.view.inputmethod.CorrectionInfo; 130import android.view.inputmethod.EditorInfo; 131import android.view.inputmethod.ExtractedText; 132import android.view.inputmethod.ExtractedTextRequest; 133import android.view.inputmethod.InputConnection; 134import android.view.inputmethod.InputMethodManager; 135import android.widget.AdapterView.OnItemClickListener; 136import android.widget.RemoteViews.RemoteView; 137 138import com.android.internal.util.FastMath; 139import com.android.internal.widget.EditableInputConnection; 140 141import org.xmlpull.v1.XmlPullParserException; 142 143import java.io.IOException; 144import java.lang.ref.WeakReference; 145import java.text.BreakIterator; 146import java.util.ArrayList; 147import java.util.Arrays; 148import java.util.Comparator; 149import java.util.HashMap; 150 151/** 152 * Displays text to the user and optionally allows them to edit it. A TextView 153 * is a complete text editor, however the basic class is configured to not 154 * allow editing; see {@link EditText} for a subclass that configures the text 155 * view for editing. 156 * 157 * <p> 158 * <b>XML attributes</b> 159 * <p> 160 * See {@link android.R.styleable#TextView TextView Attributes}, 161 * {@link android.R.styleable#View View Attributes} 162 * 163 * @attr ref android.R.styleable#TextView_text 164 * @attr ref android.R.styleable#TextView_bufferType 165 * @attr ref android.R.styleable#TextView_hint 166 * @attr ref android.R.styleable#TextView_textColor 167 * @attr ref android.R.styleable#TextView_textColorHighlight 168 * @attr ref android.R.styleable#TextView_textColorHint 169 * @attr ref android.R.styleable#TextView_textAppearance 170 * @attr ref android.R.styleable#TextView_textColorLink 171 * @attr ref android.R.styleable#TextView_textSize 172 * @attr ref android.R.styleable#TextView_textScaleX 173 * @attr ref android.R.styleable#TextView_typeface 174 * @attr ref android.R.styleable#TextView_textStyle 175 * @attr ref android.R.styleable#TextView_cursorVisible 176 * @attr ref android.R.styleable#TextView_maxLines 177 * @attr ref android.R.styleable#TextView_maxHeight 178 * @attr ref android.R.styleable#TextView_lines 179 * @attr ref android.R.styleable#TextView_height 180 * @attr ref android.R.styleable#TextView_minLines 181 * @attr ref android.R.styleable#TextView_minHeight 182 * @attr ref android.R.styleable#TextView_maxEms 183 * @attr ref android.R.styleable#TextView_maxWidth 184 * @attr ref android.R.styleable#TextView_ems 185 * @attr ref android.R.styleable#TextView_width 186 * @attr ref android.R.styleable#TextView_minEms 187 * @attr ref android.R.styleable#TextView_minWidth 188 * @attr ref android.R.styleable#TextView_gravity 189 * @attr ref android.R.styleable#TextView_scrollHorizontally 190 * @attr ref android.R.styleable#TextView_password 191 * @attr ref android.R.styleable#TextView_singleLine 192 * @attr ref android.R.styleable#TextView_selectAllOnFocus 193 * @attr ref android.R.styleable#TextView_includeFontPadding 194 * @attr ref android.R.styleable#TextView_maxLength 195 * @attr ref android.R.styleable#TextView_shadowColor 196 * @attr ref android.R.styleable#TextView_shadowDx 197 * @attr ref android.R.styleable#TextView_shadowDy 198 * @attr ref android.R.styleable#TextView_shadowRadius 199 * @attr ref android.R.styleable#TextView_autoLink 200 * @attr ref android.R.styleable#TextView_linksClickable 201 * @attr ref android.R.styleable#TextView_numeric 202 * @attr ref android.R.styleable#TextView_digits 203 * @attr ref android.R.styleable#TextView_phoneNumber 204 * @attr ref android.R.styleable#TextView_inputMethod 205 * @attr ref android.R.styleable#TextView_capitalize 206 * @attr ref android.R.styleable#TextView_autoText 207 * @attr ref android.R.styleable#TextView_editable 208 * @attr ref android.R.styleable#TextView_freezesText 209 * @attr ref android.R.styleable#TextView_ellipsize 210 * @attr ref android.R.styleable#TextView_drawableTop 211 * @attr ref android.R.styleable#TextView_drawableBottom 212 * @attr ref android.R.styleable#TextView_drawableRight 213 * @attr ref android.R.styleable#TextView_drawableLeft 214 * @attr ref android.R.styleable#TextView_drawablePadding 215 * @attr ref android.R.styleable#TextView_lineSpacingExtra 216 * @attr ref android.R.styleable#TextView_lineSpacingMultiplier 217 * @attr ref android.R.styleable#TextView_marqueeRepeatLimit 218 * @attr ref android.R.styleable#TextView_inputType 219 * @attr ref android.R.styleable#TextView_imeOptions 220 * @attr ref android.R.styleable#TextView_privateImeOptions 221 * @attr ref android.R.styleable#TextView_imeActionLabel 222 * @attr ref android.R.styleable#TextView_imeActionId 223 * @attr ref android.R.styleable#TextView_editorExtras 224 */ 225@RemoteView 226public class TextView extends View implements ViewTreeObserver.OnPreDrawListener { 227 static final String LOG_TAG = "TextView"; 228 static final boolean DEBUG_EXTRACT = false; 229 230 private static final int PRIORITY = 100; 231 private int mCurrentAlpha = 255; 232 233 final int[] mTempCoords = new int[2]; 234 Rect mTempRect; 235 236 private ColorStateList mTextColor; 237 private int mCurTextColor; 238 private ColorStateList mHintTextColor; 239 private ColorStateList mLinkTextColor; 240 private int mCurHintTextColor; 241 private boolean mFreezesText; 242 private boolean mFrozenWithFocus; 243 private boolean mTemporaryDetach; 244 private boolean mDispatchTemporaryDetach; 245 246 private boolean mDiscardNextActionUp = false; 247 private boolean mIgnoreActionUpEvent = false; 248 249 private Editable.Factory mEditableFactory = Editable.Factory.getInstance(); 250 private Spannable.Factory mSpannableFactory = Spannable.Factory.getInstance(); 251 252 private float mShadowRadius, mShadowDx, mShadowDy; 253 254 private static final int PREDRAW_NOT_REGISTERED = 0; 255 private static final int PREDRAW_PENDING = 1; 256 private static final int PREDRAW_DONE = 2; 257 private int mPreDrawState = PREDRAW_NOT_REGISTERED; 258 259 private TextUtils.TruncateAt mEllipsize = null; 260 261 // Enum for the "typeface" XML parameter. 262 // TODO: How can we get this from the XML instead of hardcoding it here? 263 private static final int SANS = 1; 264 private static final int SERIF = 2; 265 private static final int MONOSPACE = 3; 266 267 // Bitfield for the "numeric" XML parameter. 268 // TODO: How can we get this from the XML instead of hardcoding it here? 269 private static final int SIGNED = 2; 270 private static final int DECIMAL = 4; 271 272 class Drawables { 273 final Rect mCompoundRect = new Rect(); 274 Drawable mDrawableTop, mDrawableBottom, mDrawableLeft, mDrawableRight, 275 mDrawableStart, mDrawableEnd; 276 int mDrawableSizeTop, mDrawableSizeBottom, mDrawableSizeLeft, mDrawableSizeRight, 277 mDrawableSizeStart, mDrawableSizeEnd; 278 int mDrawableWidthTop, mDrawableWidthBottom, mDrawableHeightLeft, mDrawableHeightRight, 279 mDrawableHeightStart, mDrawableHeightEnd; 280 int mDrawablePadding; 281 } 282 private Drawables mDrawables; 283 284 private CharSequence mError; 285 private boolean mErrorWasChanged; 286 private ErrorPopup mPopup; 287 /** 288 * This flag is set if the TextView tries to display an error before it 289 * is attached to the window (so its position is still unknown). 290 * It causes the error to be shown later, when onAttachedToWindow() 291 * is called. 292 */ 293 private boolean mShowErrorAfterAttach; 294 295 private CharWrapper mCharWrapper = null; 296 297 private boolean mSelectionMoved = false; 298 private boolean mTouchFocusSelected = false; 299 300 private Marquee mMarquee; 301 private boolean mRestartMarquee; 302 303 private int mMarqueeRepeatLimit = 3; 304 305 class InputContentType { 306 int imeOptions = EditorInfo.IME_NULL; 307 String privateImeOptions; 308 CharSequence imeActionLabel; 309 int imeActionId; 310 Bundle extras; 311 OnEditorActionListener onEditorActionListener; 312 boolean enterDown; 313 } 314 InputContentType mInputContentType; 315 316 class InputMethodState { 317 Rect mCursorRectInWindow = new Rect(); 318 RectF mTmpRectF = new RectF(); 319 float[] mTmpOffset = new float[2]; 320 ExtractedTextRequest mExtracting; 321 final ExtractedText mTmpExtracted = new ExtractedText(); 322 int mBatchEditNesting; 323 boolean mCursorChanged; 324 boolean mSelectionModeChanged; 325 boolean mContentChanged; 326 int mChangedStart, mChangedEnd, mChangedDelta; 327 } 328 InputMethodState mInputMethodState; 329 330 private int mTextSelectHandleLeftRes; 331 private int mTextSelectHandleRightRes; 332 private int mTextSelectHandleRes; 333 334 private int mTextEditSuggestionItemLayout; 335 private SuggestionsPopupWindow mSuggestionsPopupWindow; 336 private SuggestionRangeSpan mSuggestionRangeSpan; 337 338 private int mCursorDrawableRes; 339 private final Drawable[] mCursorDrawable = new Drawable[2]; 340 private int mCursorCount; // Actual current number of used mCursorDrawable: 0, 1 or 2 341 342 private Drawable mSelectHandleLeft; 343 private Drawable mSelectHandleRight; 344 private Drawable mSelectHandleCenter; 345 346 // Global listener that detects changes in the global position of the TextView 347 private PositionListener mPositionListener; 348 349 private float mLastDownPositionX, mLastDownPositionY; 350 private Callback mCustomSelectionActionModeCallback; 351 352 private final int mSquaredTouchSlopDistance; 353 // Set when this TextView gained focus with some text selected. Will start selection mode. 354 private boolean mCreatedWithASelection = false; 355 356 private WordIterator mWordIterator; 357 358 private SpellChecker mSpellChecker; 359 360 // The alignment to pass to Layout, or null if not resolved. 361 private Layout.Alignment mLayoutAlignment; 362 363 // The default value for mTextAlign. 364 private TextAlign mTextAlign = TextAlign.INHERIT; 365 366 private static enum TextAlign { 367 INHERIT, GRAVITY, TEXT_START, TEXT_END, CENTER, VIEW_START, VIEW_END; 368 } 369 370 private boolean mResolvedDrawables = false; 371 372 /** 373 * On some devices the fading edges add a performance penalty if used 374 * extensively in the same layout. This mode indicates how the marquee 375 * is currently being shown, if applicable. (mEllipsize will == MARQUEE) 376 */ 377 private int mMarqueeFadeMode = MARQUEE_FADE_NORMAL; 378 379 /** 380 * When mMarqueeFadeMode is not MARQUEE_FADE_NORMAL, this stores 381 * the layout that should be used when the mode switches. 382 */ 383 private Layout mSavedMarqueeModeLayout; 384 385 /** 386 * Draw marquee text with fading edges as usual 387 */ 388 private static final int MARQUEE_FADE_NORMAL = 0; 389 390 /** 391 * Draw marquee text as ellipsize end while inactive instead of with the fade. 392 * (Useful for devices where the fade can be expensive if overdone) 393 */ 394 private static final int MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS = 1; 395 396 /** 397 * Draw marquee text with fading edges because it is currently active/animating. 398 */ 399 private static final int MARQUEE_FADE_SWITCH_SHOW_FADE = 2; 400 401 /* 402 * Kick-start the font cache for the zygote process (to pay the cost of 403 * initializing freetype for our default font only once). 404 */ 405 static { 406 Paint p = new Paint(); 407 p.setAntiAlias(true); 408 // We don't care about the result, just the side-effect of measuring. 409 p.measureText("H"); 410 } 411 412 /** 413 * Interface definition for a callback to be invoked when an action is 414 * performed on the editor. 415 */ 416 public interface OnEditorActionListener { 417 /** 418 * Called when an action is being performed. 419 * 420 * @param v The view that was clicked. 421 * @param actionId Identifier of the action. This will be either the 422 * identifier you supplied, or {@link EditorInfo#IME_NULL 423 * EditorInfo.IME_NULL} if being called due to the enter key 424 * being pressed. 425 * @param event If triggered by an enter key, this is the event; 426 * otherwise, this is null. 427 * @return Return true if you have consumed the action, else false. 428 */ 429 boolean onEditorAction(TextView v, int actionId, KeyEvent event); 430 } 431 432 public TextView(Context context) { 433 this(context, null); 434 } 435 436 public TextView(Context context, 437 AttributeSet attrs) { 438 this(context, attrs, com.android.internal.R.attr.textViewStyle); 439 } 440 441 @SuppressWarnings("deprecation") 442 public TextView(Context context, 443 AttributeSet attrs, 444 int defStyle) { 445 super(context, attrs, defStyle); 446 mText = ""; 447 448 mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); 449 mTextPaint.density = getResources().getDisplayMetrics().density; 450 mTextPaint.setCompatibilityScaling( 451 getResources().getCompatibilityInfo().applicationScale); 452 453 // If we get the paint from the skin, we should set it to left, since 454 // the layout always wants it to be left. 455 // mTextPaint.setTextAlign(Paint.Align.LEFT); 456 457 mHighlightPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 458 mHighlightPaint.setCompatibilityScaling( 459 getResources().getCompatibilityInfo().applicationScale); 460 461 mMovement = getDefaultMovementMethod(); 462 mTransformation = null; 463 464 TypedArray a = 465 context.obtainStyledAttributes( 466 attrs, com.android.internal.R.styleable.TextView, defStyle, 0); 467 468 int textColorHighlight = 0; 469 ColorStateList textColor = null; 470 ColorStateList textColorHint = null; 471 ColorStateList textColorLink = null; 472 int textSize = 15; 473 int typefaceIndex = -1; 474 int styleIndex = -1; 475 boolean allCaps = false; 476 477 /* 478 * Look the appearance up without checking first if it exists because 479 * almost every TextView has one and it greatly simplifies the logic 480 * to be able to parse the appearance first and then let specific tags 481 * for this View override it. 482 */ 483 TypedArray appearance = null; 484 int ap = a.getResourceId(com.android.internal.R.styleable.TextView_textAppearance, -1); 485 if (ap != -1) { 486 appearance = context.obtainStyledAttributes(ap, 487 com.android.internal.R.styleable. 488 TextAppearance); 489 } 490 if (appearance != null) { 491 int n = appearance.getIndexCount(); 492 for (int i = 0; i < n; i++) { 493 int attr = appearance.getIndex(i); 494 495 switch (attr) { 496 case com.android.internal.R.styleable.TextAppearance_textColorHighlight: 497 textColorHighlight = appearance.getColor(attr, textColorHighlight); 498 break; 499 500 case com.android.internal.R.styleable.TextAppearance_textColor: 501 textColor = appearance.getColorStateList(attr); 502 break; 503 504 case com.android.internal.R.styleable.TextAppearance_textColorHint: 505 textColorHint = appearance.getColorStateList(attr); 506 break; 507 508 case com.android.internal.R.styleable.TextAppearance_textColorLink: 509 textColorLink = appearance.getColorStateList(attr); 510 break; 511 512 case com.android.internal.R.styleable.TextAppearance_textSize: 513 textSize = appearance.getDimensionPixelSize(attr, textSize); 514 break; 515 516 case com.android.internal.R.styleable.TextAppearance_typeface: 517 typefaceIndex = appearance.getInt(attr, -1); 518 break; 519 520 case com.android.internal.R.styleable.TextAppearance_textStyle: 521 styleIndex = appearance.getInt(attr, -1); 522 break; 523 524 case com.android.internal.R.styleable.TextAppearance_textAllCaps: 525 allCaps = appearance.getBoolean(attr, false); 526 break; 527 } 528 } 529 530 appearance.recycle(); 531 } 532 533 boolean editable = getDefaultEditable(); 534 CharSequence inputMethod = null; 535 int numeric = 0; 536 CharSequence digits = null; 537 boolean phone = false; 538 boolean autotext = false; 539 int autocap = -1; 540 int buffertype = 0; 541 boolean selectallonfocus = false; 542 Drawable drawableLeft = null, drawableTop = null, drawableRight = null, 543 drawableBottom = null, drawableStart = null, drawableEnd = null; 544 int drawablePadding = 0; 545 int ellipsize = -1; 546 boolean singleLine = false; 547 int maxlength = -1; 548 CharSequence text = ""; 549 CharSequence hint = null; 550 int shadowcolor = 0; 551 float dx = 0, dy = 0, r = 0; 552 boolean password = false; 553 int inputType = EditorInfo.TYPE_NULL; 554 555 int n = a.getIndexCount(); 556 for (int i = 0; i < n; i++) { 557 int attr = a.getIndex(i); 558 559 switch (attr) { 560 case com.android.internal.R.styleable.TextView_editable: 561 editable = a.getBoolean(attr, editable); 562 break; 563 564 case com.android.internal.R.styleable.TextView_inputMethod: 565 inputMethod = a.getText(attr); 566 break; 567 568 case com.android.internal.R.styleable.TextView_numeric: 569 numeric = a.getInt(attr, numeric); 570 break; 571 572 case com.android.internal.R.styleable.TextView_digits: 573 digits = a.getText(attr); 574 break; 575 576 case com.android.internal.R.styleable.TextView_phoneNumber: 577 phone = a.getBoolean(attr, phone); 578 break; 579 580 case com.android.internal.R.styleable.TextView_autoText: 581 autotext = a.getBoolean(attr, autotext); 582 break; 583 584 case com.android.internal.R.styleable.TextView_capitalize: 585 autocap = a.getInt(attr, autocap); 586 break; 587 588 case com.android.internal.R.styleable.TextView_bufferType: 589 buffertype = a.getInt(attr, buffertype); 590 break; 591 592 case com.android.internal.R.styleable.TextView_selectAllOnFocus: 593 selectallonfocus = a.getBoolean(attr, selectallonfocus); 594 break; 595 596 case com.android.internal.R.styleable.TextView_autoLink: 597 mAutoLinkMask = a.getInt(attr, 0); 598 break; 599 600 case com.android.internal.R.styleable.TextView_linksClickable: 601 mLinksClickable = a.getBoolean(attr, true); 602 break; 603 604 case com.android.internal.R.styleable.TextView_drawableLeft: 605 drawableLeft = a.getDrawable(attr); 606 break; 607 608 case com.android.internal.R.styleable.TextView_drawableTop: 609 drawableTop = a.getDrawable(attr); 610 break; 611 612 case com.android.internal.R.styleable.TextView_drawableRight: 613 drawableRight = a.getDrawable(attr); 614 break; 615 616 case com.android.internal.R.styleable.TextView_drawableBottom: 617 drawableBottom = a.getDrawable(attr); 618 break; 619 620 case com.android.internal.R.styleable.TextView_drawableStart: 621 drawableStart = a.getDrawable(attr); 622 break; 623 624 case com.android.internal.R.styleable.TextView_drawableEnd: 625 drawableEnd = a.getDrawable(attr); 626 break; 627 628 case com.android.internal.R.styleable.TextView_drawablePadding: 629 drawablePadding = a.getDimensionPixelSize(attr, drawablePadding); 630 break; 631 632 case com.android.internal.R.styleable.TextView_maxLines: 633 setMaxLines(a.getInt(attr, -1)); 634 break; 635 636 case com.android.internal.R.styleable.TextView_maxHeight: 637 setMaxHeight(a.getDimensionPixelSize(attr, -1)); 638 break; 639 640 case com.android.internal.R.styleable.TextView_lines: 641 setLines(a.getInt(attr, -1)); 642 break; 643 644 case com.android.internal.R.styleable.TextView_height: 645 setHeight(a.getDimensionPixelSize(attr, -1)); 646 break; 647 648 case com.android.internal.R.styleable.TextView_minLines: 649 setMinLines(a.getInt(attr, -1)); 650 break; 651 652 case com.android.internal.R.styleable.TextView_minHeight: 653 setMinHeight(a.getDimensionPixelSize(attr, -1)); 654 break; 655 656 case com.android.internal.R.styleable.TextView_maxEms: 657 setMaxEms(a.getInt(attr, -1)); 658 break; 659 660 case com.android.internal.R.styleable.TextView_maxWidth: 661 setMaxWidth(a.getDimensionPixelSize(attr, -1)); 662 break; 663 664 case com.android.internal.R.styleable.TextView_ems: 665 setEms(a.getInt(attr, -1)); 666 break; 667 668 case com.android.internal.R.styleable.TextView_width: 669 setWidth(a.getDimensionPixelSize(attr, -1)); 670 break; 671 672 case com.android.internal.R.styleable.TextView_minEms: 673 setMinEms(a.getInt(attr, -1)); 674 break; 675 676 case com.android.internal.R.styleable.TextView_minWidth: 677 setMinWidth(a.getDimensionPixelSize(attr, -1)); 678 break; 679 680 case com.android.internal.R.styleable.TextView_gravity: 681 setGravity(a.getInt(attr, -1)); 682 break; 683 684 case com.android.internal.R.styleable.TextView_hint: 685 hint = a.getText(attr); 686 break; 687 688 case com.android.internal.R.styleable.TextView_text: 689 text = a.getText(attr); 690 break; 691 692 case com.android.internal.R.styleable.TextView_scrollHorizontally: 693 if (a.getBoolean(attr, false)) { 694 setHorizontallyScrolling(true); 695 } 696 break; 697 698 case com.android.internal.R.styleable.TextView_singleLine: 699 singleLine = a.getBoolean(attr, singleLine); 700 break; 701 702 case com.android.internal.R.styleable.TextView_ellipsize: 703 ellipsize = a.getInt(attr, ellipsize); 704 break; 705 706 case com.android.internal.R.styleable.TextView_marqueeRepeatLimit: 707 setMarqueeRepeatLimit(a.getInt(attr, mMarqueeRepeatLimit)); 708 break; 709 710 case com.android.internal.R.styleable.TextView_includeFontPadding: 711 if (!a.getBoolean(attr, true)) { 712 setIncludeFontPadding(false); 713 } 714 break; 715 716 case com.android.internal.R.styleable.TextView_cursorVisible: 717 if (!a.getBoolean(attr, true)) { 718 setCursorVisible(false); 719 } 720 break; 721 722 case com.android.internal.R.styleable.TextView_maxLength: 723 maxlength = a.getInt(attr, -1); 724 break; 725 726 case com.android.internal.R.styleable.TextView_textScaleX: 727 setTextScaleX(a.getFloat(attr, 1.0f)); 728 break; 729 730 case com.android.internal.R.styleable.TextView_freezesText: 731 mFreezesText = a.getBoolean(attr, false); 732 break; 733 734 case com.android.internal.R.styleable.TextView_shadowColor: 735 shadowcolor = a.getInt(attr, 0); 736 break; 737 738 case com.android.internal.R.styleable.TextView_shadowDx: 739 dx = a.getFloat(attr, 0); 740 break; 741 742 case com.android.internal.R.styleable.TextView_shadowDy: 743 dy = a.getFloat(attr, 0); 744 break; 745 746 case com.android.internal.R.styleable.TextView_shadowRadius: 747 r = a.getFloat(attr, 0); 748 break; 749 750 case com.android.internal.R.styleable.TextView_enabled: 751 setEnabled(a.getBoolean(attr, isEnabled())); 752 break; 753 754 case com.android.internal.R.styleable.TextView_textColorHighlight: 755 textColorHighlight = a.getColor(attr, textColorHighlight); 756 break; 757 758 case com.android.internal.R.styleable.TextView_textColor: 759 textColor = a.getColorStateList(attr); 760 break; 761 762 case com.android.internal.R.styleable.TextView_textColorHint: 763 textColorHint = a.getColorStateList(attr); 764 break; 765 766 case com.android.internal.R.styleable.TextView_textColorLink: 767 textColorLink = a.getColorStateList(attr); 768 break; 769 770 case com.android.internal.R.styleable.TextView_textSize: 771 textSize = a.getDimensionPixelSize(attr, textSize); 772 break; 773 774 case com.android.internal.R.styleable.TextView_typeface: 775 typefaceIndex = a.getInt(attr, typefaceIndex); 776 break; 777 778 case com.android.internal.R.styleable.TextView_textStyle: 779 styleIndex = a.getInt(attr, styleIndex); 780 break; 781 782 case com.android.internal.R.styleable.TextView_password: 783 password = a.getBoolean(attr, password); 784 break; 785 786 case com.android.internal.R.styleable.TextView_lineSpacingExtra: 787 mSpacingAdd = a.getDimensionPixelSize(attr, (int) mSpacingAdd); 788 break; 789 790 case com.android.internal.R.styleable.TextView_lineSpacingMultiplier: 791 mSpacingMult = a.getFloat(attr, mSpacingMult); 792 break; 793 794 case com.android.internal.R.styleable.TextView_inputType: 795 inputType = a.getInt(attr, mInputType); 796 break; 797 798 case com.android.internal.R.styleable.TextView_imeOptions: 799 if (mInputContentType == null) { 800 mInputContentType = new InputContentType(); 801 } 802 mInputContentType.imeOptions = a.getInt(attr, 803 mInputContentType.imeOptions); 804 break; 805 806 case com.android.internal.R.styleable.TextView_imeActionLabel: 807 if (mInputContentType == null) { 808 mInputContentType = new InputContentType(); 809 } 810 mInputContentType.imeActionLabel = a.getText(attr); 811 break; 812 813 case com.android.internal.R.styleable.TextView_imeActionId: 814 if (mInputContentType == null) { 815 mInputContentType = new InputContentType(); 816 } 817 mInputContentType.imeActionId = a.getInt(attr, 818 mInputContentType.imeActionId); 819 break; 820 821 case com.android.internal.R.styleable.TextView_privateImeOptions: 822 setPrivateImeOptions(a.getString(attr)); 823 break; 824 825 case com.android.internal.R.styleable.TextView_editorExtras: 826 try { 827 setInputExtras(a.getResourceId(attr, 0)); 828 } catch (XmlPullParserException e) { 829 Log.w(LOG_TAG, "Failure reading input extras", e); 830 } catch (IOException e) { 831 Log.w(LOG_TAG, "Failure reading input extras", e); 832 } 833 break; 834 835 case com.android.internal.R.styleable.TextView_textCursorDrawable: 836 mCursorDrawableRes = a.getResourceId(attr, 0); 837 break; 838 839 case com.android.internal.R.styleable.TextView_textSelectHandleLeft: 840 mTextSelectHandleLeftRes = a.getResourceId(attr, 0); 841 break; 842 843 case com.android.internal.R.styleable.TextView_textSelectHandleRight: 844 mTextSelectHandleRightRes = a.getResourceId(attr, 0); 845 break; 846 847 case com.android.internal.R.styleable.TextView_textSelectHandle: 848 mTextSelectHandleRes = a.getResourceId(attr, 0); 849 break; 850 851 case com.android.internal.R.styleable.TextView_textEditSuggestionItemLayout: 852 mTextEditSuggestionItemLayout = a.getResourceId(attr, 0); 853 break; 854 855 case com.android.internal.R.styleable.TextView_textIsSelectable: 856 mTextIsSelectable = a.getBoolean(attr, false); 857 break; 858 859 case com.android.internal.R.styleable.TextView_textAllCaps: 860 allCaps = a.getBoolean(attr, false); 861 break; 862 } 863 } 864 a.recycle(); 865 866 BufferType bufferType = BufferType.EDITABLE; 867 868 final int variation = 869 inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION); 870 final boolean passwordInputType = variation 871 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD); 872 final boolean webPasswordInputType = variation 873 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD); 874 final boolean numberPasswordInputType = variation 875 == (EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD); 876 877 if (inputMethod != null) { 878 Class<?> c; 879 880 try { 881 c = Class.forName(inputMethod.toString()); 882 } catch (ClassNotFoundException ex) { 883 throw new RuntimeException(ex); 884 } 885 886 try { 887 mInput = (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 mInputType = inputType != EditorInfo.TYPE_NULL 895 ? inputType 896 : mInput.getInputType(); 897 } catch (IncompatibleClassChangeError e) { 898 mInputType = EditorInfo.TYPE_CLASS_TEXT; 899 } 900 } else if (digits != null) { 901 mInput = DigitsKeyListener.getInstance(digits.toString()); 902 // If no input type was specified, we will default to generic 903 // text, since we can't tell the IME about the set of digits 904 // that was selected. 905 mInputType = inputType != EditorInfo.TYPE_NULL 906 ? inputType : EditorInfo.TYPE_CLASS_TEXT; 907 } else if (inputType != EditorInfo.TYPE_NULL) { 908 setInputType(inputType, true); 909 // If set, the input type overrides what was set using the deprecated singleLine flag. 910 singleLine = !isMultilineInputType(inputType); 911 } else if (phone) { 912 mInput = DialerKeyListener.getInstance(); 913 mInputType = inputType = EditorInfo.TYPE_CLASS_PHONE; 914 } else if (numeric != 0) { 915 mInput = DigitsKeyListener.getInstance((numeric & SIGNED) != 0, 916 (numeric & DECIMAL) != 0); 917 inputType = EditorInfo.TYPE_CLASS_NUMBER; 918 if ((numeric & SIGNED) != 0) { 919 inputType |= EditorInfo.TYPE_NUMBER_FLAG_SIGNED; 920 } 921 if ((numeric & DECIMAL) != 0) { 922 inputType |= EditorInfo.TYPE_NUMBER_FLAG_DECIMAL; 923 } 924 mInputType = inputType; 925 } else if (autotext || autocap != -1) { 926 TextKeyListener.Capitalize cap; 927 928 inputType = EditorInfo.TYPE_CLASS_TEXT; 929 930 switch (autocap) { 931 case 1: 932 cap = TextKeyListener.Capitalize.SENTENCES; 933 inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES; 934 break; 935 936 case 2: 937 cap = TextKeyListener.Capitalize.WORDS; 938 inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS; 939 break; 940 941 case 3: 942 cap = TextKeyListener.Capitalize.CHARACTERS; 943 inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_CHARACTERS; 944 break; 945 946 default: 947 cap = TextKeyListener.Capitalize.NONE; 948 break; 949 } 950 951 mInput = TextKeyListener.getInstance(autotext, cap); 952 mInputType = inputType; 953 } else if (mTextIsSelectable) { 954 // Prevent text changes from keyboard. 955 mInputType = EditorInfo.TYPE_NULL; 956 mInput = null; 957 bufferType = BufferType.SPANNABLE; 958 // Required to request focus while in touch mode. 959 setFocusableInTouchMode(true); 960 // So that selection can be changed using arrow keys and touch is handled. 961 setMovementMethod(ArrowKeyMovementMethod.getInstance()); 962 } else if (editable) { 963 mInput = TextKeyListener.getInstance(); 964 mInputType = EditorInfo.TYPE_CLASS_TEXT; 965 } else { 966 mInput = null; 967 968 switch (buffertype) { 969 case 0: 970 bufferType = BufferType.NORMAL; 971 break; 972 case 1: 973 bufferType = BufferType.SPANNABLE; 974 break; 975 case 2: 976 bufferType = BufferType.EDITABLE; 977 break; 978 } 979 } 980 981 // mInputType has been set from inputType, possibly modified by mInputMethod. 982 // Specialize mInputType to [web]password if we have a text class and the original input 983 // type was a password. 984 if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { 985 if (password || passwordInputType) { 986 mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION)) 987 | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD; 988 } 989 if (webPasswordInputType) { 990 mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION)) 991 | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD; 992 } 993 } else if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_NUMBER) { 994 if (numberPasswordInputType) { 995 mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION)) 996 | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD; 997 } 998 } 999 1000 if (selectallonfocus) { 1001 mSelectAllOnFocus = true; 1002 1003 if (bufferType == BufferType.NORMAL) 1004 bufferType = BufferType.SPANNABLE; 1005 } 1006 1007 setCompoundDrawablesWithIntrinsicBounds( 1008 drawableLeft, drawableTop, drawableRight, drawableBottom); 1009 setRelativeDrawablesIfNeeded(drawableStart, drawableEnd); 1010 setCompoundDrawablePadding(drawablePadding); 1011 1012 // Same as setSingleLine(), but make sure the transformation method and the maximum number 1013 // of lines of height are unchanged for multi-line TextViews. 1014 setInputTypeSingleLine(singleLine); 1015 applySingleLine(singleLine, singleLine, singleLine); 1016 1017 if (singleLine && mInput == null && ellipsize < 0) { 1018 ellipsize = 3; // END 1019 } 1020 1021 switch (ellipsize) { 1022 case 1: 1023 setEllipsize(TextUtils.TruncateAt.START); 1024 break; 1025 case 2: 1026 setEllipsize(TextUtils.TruncateAt.MIDDLE); 1027 break; 1028 case 3: 1029 setEllipsize(TextUtils.TruncateAt.END); 1030 break; 1031 case 4: 1032 if (ViewConfiguration.get(context).isFadingMarqueeEnabled()) { 1033 setHorizontalFadingEdgeEnabled(true); 1034 mMarqueeFadeMode = MARQUEE_FADE_NORMAL; 1035 } else { 1036 setHorizontalFadingEdgeEnabled(false); 1037 mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS; 1038 } 1039 setEllipsize(TextUtils.TruncateAt.MARQUEE); 1040 break; 1041 } 1042 1043 setTextColor(textColor != null ? textColor : ColorStateList.valueOf(0xFF000000)); 1044 setHintTextColor(textColorHint); 1045 setLinkTextColor(textColorLink); 1046 if (textColorHighlight != 0) { 1047 setHighlightColor(textColorHighlight); 1048 } 1049 setRawTextSize(textSize); 1050 1051 if (allCaps) { 1052 setTransformationMethod(new AllCapsTransformationMethod(getContext())); 1053 } 1054 1055 if (password || passwordInputType || webPasswordInputType || numberPasswordInputType) { 1056 setTransformationMethod(PasswordTransformationMethod.getInstance()); 1057 typefaceIndex = MONOSPACE; 1058 } else if ((mInputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION)) 1059 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD)) { 1060 typefaceIndex = MONOSPACE; 1061 } 1062 1063 setTypefaceByIndex(typefaceIndex, styleIndex); 1064 1065 if (shadowcolor != 0) { 1066 setShadowLayer(r, dx, dy, shadowcolor); 1067 } 1068 1069 if (maxlength >= 0) { 1070 setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxlength) }); 1071 } else { 1072 setFilters(NO_FILTERS); 1073 } 1074 1075 setText(text, bufferType); 1076 if (hint != null) setHint(hint); 1077 1078 /* 1079 * Views are not normally focusable unless specified to be. 1080 * However, TextViews that have input or movement methods *are* 1081 * focusable by default. 1082 */ 1083 a = context.obtainStyledAttributes(attrs, 1084 com.android.internal.R.styleable.View, 1085 defStyle, 0); 1086 1087 boolean focusable = mMovement != null || mInput != null; 1088 boolean clickable = focusable; 1089 boolean longClickable = focusable; 1090 1091 n = a.getIndexCount(); 1092 for (int i = 0; i < n; i++) { 1093 int attr = a.getIndex(i); 1094 1095 switch (attr) { 1096 case com.android.internal.R.styleable.View_focusable: 1097 focusable = a.getBoolean(attr, focusable); 1098 break; 1099 1100 case com.android.internal.R.styleable.View_clickable: 1101 clickable = a.getBoolean(attr, clickable); 1102 break; 1103 1104 case com.android.internal.R.styleable.View_longClickable: 1105 longClickable = a.getBoolean(attr, longClickable); 1106 break; 1107 } 1108 } 1109 a.recycle(); 1110 1111 setFocusable(focusable); 1112 setClickable(clickable); 1113 setLongClickable(longClickable); 1114 1115 prepareCursorControllers(); 1116 1117 final ViewConfiguration viewConfiguration = ViewConfiguration.get(context); 1118 final int touchSlop = viewConfiguration.getScaledTouchSlop(); 1119 mSquaredTouchSlopDistance = touchSlop * touchSlop; 1120 } 1121 1122 private void setTypefaceByIndex(int typefaceIndex, int styleIndex) { 1123 Typeface tf = null; 1124 switch (typefaceIndex) { 1125 case SANS: 1126 tf = Typeface.SANS_SERIF; 1127 break; 1128 1129 case SERIF: 1130 tf = Typeface.SERIF; 1131 break; 1132 1133 case MONOSPACE: 1134 tf = Typeface.MONOSPACE; 1135 break; 1136 } 1137 1138 setTypeface(tf, styleIndex); 1139 } 1140 1141 private void setRelativeDrawablesIfNeeded(Drawable start, Drawable end) { 1142 boolean hasRelativeDrawables = (start != null) || (end != null); 1143 if (hasRelativeDrawables) { 1144 Drawables dr = mDrawables; 1145 if (dr == null) { 1146 mDrawables = dr = new Drawables(); 1147 } 1148 final Rect compoundRect = dr.mCompoundRect; 1149 int[] state = getDrawableState(); 1150 if (start != null) { 1151 start.setBounds(0, 0, start.getIntrinsicWidth(), start.getIntrinsicHeight()); 1152 start.setState(state); 1153 start.copyBounds(compoundRect); 1154 start.setCallback(this); 1155 1156 dr.mDrawableStart = start; 1157 dr.mDrawableSizeStart = compoundRect.width(); 1158 dr.mDrawableHeightStart = compoundRect.height(); 1159 } else { 1160 dr.mDrawableSizeStart = dr.mDrawableHeightStart = 0; 1161 } 1162 if (end != null) { 1163 end.setBounds(0, 0, end.getIntrinsicWidth(), end.getIntrinsicHeight()); 1164 end.setState(state); 1165 end.copyBounds(compoundRect); 1166 end.setCallback(this); 1167 1168 dr.mDrawableEnd = end; 1169 dr.mDrawableSizeEnd = compoundRect.width(); 1170 dr.mDrawableHeightEnd = compoundRect.height(); 1171 } else { 1172 dr.mDrawableSizeEnd = dr.mDrawableHeightEnd = 0; 1173 } 1174 } 1175 } 1176 1177 @Override 1178 public void setEnabled(boolean enabled) { 1179 if (enabled == isEnabled()) { 1180 return; 1181 } 1182 1183 if (!enabled) { 1184 // Hide the soft input if the currently active TextView is disabled 1185 InputMethodManager imm = InputMethodManager.peekInstance(); 1186 if (imm != null && imm.isActive(this)) { 1187 imm.hideSoftInputFromWindow(getWindowToken(), 0); 1188 } 1189 } 1190 super.setEnabled(enabled); 1191 prepareCursorControllers(); 1192 } 1193 1194 /** 1195 * Sets the typeface and style in which the text should be displayed, 1196 * and turns on the fake bold and italic bits in the Paint if the 1197 * Typeface that you provided does not have all the bits in the 1198 * style that you specified. 1199 * 1200 * @attr ref android.R.styleable#TextView_typeface 1201 * @attr ref android.R.styleable#TextView_textStyle 1202 */ 1203 public void setTypeface(Typeface tf, int style) { 1204 if (style > 0) { 1205 if (tf == null) { 1206 tf = Typeface.defaultFromStyle(style); 1207 } else { 1208 tf = Typeface.create(tf, style); 1209 } 1210 1211 setTypeface(tf); 1212 // now compute what (if any) algorithmic styling is needed 1213 int typefaceStyle = tf != null ? tf.getStyle() : 0; 1214 int need = style & ~typefaceStyle; 1215 mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0); 1216 mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0); 1217 } else { 1218 mTextPaint.setFakeBoldText(false); 1219 mTextPaint.setTextSkewX(0); 1220 setTypeface(tf); 1221 } 1222 } 1223 1224 /** 1225 * Subclasses override this to specify that they have a KeyListener 1226 * by default even if not specifically called for in the XML options. 1227 */ 1228 protected boolean getDefaultEditable() { 1229 return false; 1230 } 1231 1232 /** 1233 * Subclasses override this to specify a default movement method. 1234 */ 1235 protected MovementMethod getDefaultMovementMethod() { 1236 return null; 1237 } 1238 1239 /** 1240 * Return the text the TextView is displaying. If setText() was called with 1241 * an argument of BufferType.SPANNABLE or BufferType.EDITABLE, you can cast 1242 * the return value from this method to Spannable or Editable, respectively. 1243 * 1244 * Note: The content of the return value should not be modified. If you want 1245 * a modifiable one, you should make your own copy first. 1246 */ 1247 @ViewDebug.CapturedViewProperty 1248 public CharSequence getText() { 1249 return mText; 1250 } 1251 1252 /** 1253 * Returns the length, in characters, of the text managed by this TextView 1254 */ 1255 public int length() { 1256 return mText.length(); 1257 } 1258 1259 /** 1260 * Return the text the TextView is displaying as an Editable object. If 1261 * the text is not editable, null is returned. 1262 * 1263 * @see #getText 1264 */ 1265 public Editable getEditableText() { 1266 return (mText instanceof Editable) ? (Editable)mText : null; 1267 } 1268 1269 /** 1270 * @return the height of one standard line in pixels. Note that markup 1271 * within the text can cause individual lines to be taller or shorter 1272 * than this height, and the layout may contain additional first- 1273 * or last-line padding. 1274 */ 1275 public int getLineHeight() { 1276 return FastMath.round(mTextPaint.getFontMetricsInt(null) * mSpacingMult + mSpacingAdd); 1277 } 1278 1279 /** 1280 * @return the Layout that is currently being used to display the text. 1281 * This can be null if the text or width has recently changes. 1282 */ 1283 public final Layout getLayout() { 1284 return mLayout; 1285 } 1286 1287 /** 1288 * @return the current key listener for this TextView. 1289 * This will frequently be null for non-EditText TextViews. 1290 */ 1291 public final KeyListener getKeyListener() { 1292 return mInput; 1293 } 1294 1295 /** 1296 * Sets the key listener to be used with this TextView. This can be null 1297 * to disallow user input. Note that this method has significant and 1298 * subtle interactions with soft keyboards and other input method: 1299 * see {@link KeyListener#getInputType() KeyListener.getContentType()} 1300 * for important details. Calling this method will replace the current 1301 * content type of the text view with the content type returned by the 1302 * key listener. 1303 * <p> 1304 * Be warned that if you want a TextView with a key listener or movement 1305 * method not to be focusable, or if you want a TextView without a 1306 * key listener or movement method to be focusable, you must call 1307 * {@link #setFocusable} again after calling this to get the focusability 1308 * back the way you want it. 1309 * 1310 * @attr ref android.R.styleable#TextView_numeric 1311 * @attr ref android.R.styleable#TextView_digits 1312 * @attr ref android.R.styleable#TextView_phoneNumber 1313 * @attr ref android.R.styleable#TextView_inputMethod 1314 * @attr ref android.R.styleable#TextView_capitalize 1315 * @attr ref android.R.styleable#TextView_autoText 1316 */ 1317 public void setKeyListener(KeyListener input) { 1318 setKeyListenerOnly(input); 1319 fixFocusableAndClickableSettings(); 1320 1321 if (input != null) { 1322 try { 1323 mInputType = mInput.getInputType(); 1324 } catch (IncompatibleClassChangeError e) { 1325 mInputType = EditorInfo.TYPE_CLASS_TEXT; 1326 } 1327 // Change inputType, without affecting transformation. 1328 // No need to applySingleLine since mSingleLine is unchanged. 1329 setInputTypeSingleLine(mSingleLine); 1330 } else { 1331 mInputType = EditorInfo.TYPE_NULL; 1332 } 1333 1334 InputMethodManager imm = InputMethodManager.peekInstance(); 1335 if (imm != null) imm.restartInput(this); 1336 } 1337 1338 private void setKeyListenerOnly(KeyListener input) { 1339 mInput = input; 1340 if (mInput != null && !(mText instanceof Editable)) 1341 setText(mText); 1342 1343 setFilters((Editable) mText, mFilters); 1344 } 1345 1346 /** 1347 * @return the movement method being used for this TextView. 1348 * This will frequently be null for non-EditText TextViews. 1349 */ 1350 public final MovementMethod getMovementMethod() { 1351 return mMovement; 1352 } 1353 1354 /** 1355 * Sets the movement method (arrow key handler) to be used for 1356 * this TextView. This can be null to disallow using the arrow keys 1357 * to move the cursor or scroll the view. 1358 * <p> 1359 * Be warned that if you want a TextView with a key listener or movement 1360 * method not to be focusable, or if you want a TextView without a 1361 * key listener or movement method to be focusable, you must call 1362 * {@link #setFocusable} again after calling this to get the focusability 1363 * back the way you want it. 1364 */ 1365 public final void setMovementMethod(MovementMethod movement) { 1366 mMovement = movement; 1367 1368 if (mMovement != null && !(mText instanceof Spannable)) 1369 setText(mText); 1370 1371 fixFocusableAndClickableSettings(); 1372 1373 // SelectionModifierCursorController depends on textCanBeSelected, which depends on mMovement 1374 prepareCursorControllers(); 1375 } 1376 1377 private void fixFocusableAndClickableSettings() { 1378 if ((mMovement != null) || mInput != null) { 1379 setFocusable(true); 1380 setClickable(true); 1381 setLongClickable(true); 1382 } else { 1383 setFocusable(false); 1384 setClickable(false); 1385 setLongClickable(false); 1386 } 1387 } 1388 1389 /** 1390 * @return the current transformation method for this TextView. 1391 * This will frequently be null except for single-line and password 1392 * fields. 1393 */ 1394 public final TransformationMethod getTransformationMethod() { 1395 return mTransformation; 1396 } 1397 1398 /** 1399 * Sets the transformation that is applied to the text that this 1400 * TextView is displaying. 1401 * 1402 * @attr ref android.R.styleable#TextView_password 1403 * @attr ref android.R.styleable#TextView_singleLine 1404 */ 1405 public final void setTransformationMethod(TransformationMethod method) { 1406 if (method == mTransformation) { 1407 // Avoid the setText() below if the transformation is 1408 // the same. 1409 return; 1410 } 1411 if (mTransformation != null) { 1412 if (mText instanceof Spannable) { 1413 ((Spannable) mText).removeSpan(mTransformation); 1414 } 1415 } 1416 1417 mTransformation = method; 1418 1419 if (method instanceof TransformationMethod2) { 1420 TransformationMethod2 method2 = (TransformationMethod2) method; 1421 mAllowTransformationLengthChange = !mTextIsSelectable && !(mText instanceof Editable); 1422 method2.setLengthChangesAllowed(mAllowTransformationLengthChange); 1423 } else { 1424 mAllowTransformationLengthChange = false; 1425 } 1426 1427 setText(mText); 1428 } 1429 1430 /** 1431 * Returns the top padding of the view, plus space for the top 1432 * Drawable if any. 1433 */ 1434 public int getCompoundPaddingTop() { 1435 final Drawables dr = mDrawables; 1436 if (dr == null || dr.mDrawableTop == null) { 1437 return mPaddingTop; 1438 } else { 1439 return mPaddingTop + dr.mDrawablePadding + dr.mDrawableSizeTop; 1440 } 1441 } 1442 1443 /** 1444 * Returns the bottom padding of the view, plus space for the bottom 1445 * Drawable if any. 1446 */ 1447 public int getCompoundPaddingBottom() { 1448 final Drawables dr = mDrawables; 1449 if (dr == null || dr.mDrawableBottom == null) { 1450 return mPaddingBottom; 1451 } else { 1452 return mPaddingBottom + dr.mDrawablePadding + dr.mDrawableSizeBottom; 1453 } 1454 } 1455 1456 /** 1457 * Returns the left padding of the view, plus space for the left 1458 * Drawable if any. 1459 */ 1460 public int getCompoundPaddingLeft() { 1461 final Drawables dr = mDrawables; 1462 if (dr == null || dr.mDrawableLeft == null) { 1463 return mPaddingLeft; 1464 } else { 1465 return mPaddingLeft + dr.mDrawablePadding + dr.mDrawableSizeLeft; 1466 } 1467 } 1468 1469 /** 1470 * Returns the right padding of the view, plus space for the right 1471 * Drawable if any. 1472 */ 1473 public int getCompoundPaddingRight() { 1474 final Drawables dr = mDrawables; 1475 if (dr == null || dr.mDrawableRight == null) { 1476 return mPaddingRight; 1477 } else { 1478 return mPaddingRight + dr.mDrawablePadding + dr.mDrawableSizeRight; 1479 } 1480 } 1481 1482 /** 1483 * Returns the start padding of the view, plus space for the start 1484 * Drawable if any. 1485 * 1486 * @hide 1487 */ 1488 public int getCompoundPaddingStart() { 1489 resolveDrawables(); 1490 switch(getResolvedLayoutDirection()) { 1491 default: 1492 case LAYOUT_DIRECTION_LTR: 1493 return getCompoundPaddingLeft(); 1494 case LAYOUT_DIRECTION_RTL: 1495 return getCompoundPaddingRight(); 1496 } 1497 } 1498 1499 /** 1500 * Returns the end padding of the view, plus space for the end 1501 * Drawable if any. 1502 * 1503 * @hide 1504 */ 1505 public int getCompoundPaddingEnd() { 1506 resolveDrawables(); 1507 switch(getResolvedLayoutDirection()) { 1508 default: 1509 case LAYOUT_DIRECTION_LTR: 1510 return getCompoundPaddingRight(); 1511 case LAYOUT_DIRECTION_RTL: 1512 return getCompoundPaddingLeft(); 1513 } 1514 } 1515 1516 /** 1517 * Returns the extended top padding of the view, including both the 1518 * top Drawable if any and any extra space to keep more than maxLines 1519 * of text from showing. It is only valid to call this after measuring. 1520 */ 1521 public int getExtendedPaddingTop() { 1522 if (mMaxMode != LINES) { 1523 return getCompoundPaddingTop(); 1524 } 1525 1526 if (mLayout.getLineCount() <= mMaximum) { 1527 return getCompoundPaddingTop(); 1528 } 1529 1530 int top = getCompoundPaddingTop(); 1531 int bottom = getCompoundPaddingBottom(); 1532 int viewht = getHeight() - top - bottom; 1533 int layoutht = mLayout.getLineTop(mMaximum); 1534 1535 if (layoutht >= viewht) { 1536 return top; 1537 } 1538 1539 final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; 1540 if (gravity == Gravity.TOP) { 1541 return top; 1542 } else if (gravity == Gravity.BOTTOM) { 1543 return top + viewht - layoutht; 1544 } else { // (gravity == Gravity.CENTER_VERTICAL) 1545 return top + (viewht - layoutht) / 2; 1546 } 1547 } 1548 1549 /** 1550 * Returns the extended bottom padding of the view, including both the 1551 * bottom Drawable if any and any extra space to keep more than maxLines 1552 * of text from showing. It is only valid to call this after measuring. 1553 */ 1554 public int getExtendedPaddingBottom() { 1555 if (mMaxMode != LINES) { 1556 return getCompoundPaddingBottom(); 1557 } 1558 1559 if (mLayout.getLineCount() <= mMaximum) { 1560 return getCompoundPaddingBottom(); 1561 } 1562 1563 int top = getCompoundPaddingTop(); 1564 int bottom = getCompoundPaddingBottom(); 1565 int viewht = getHeight() - top - bottom; 1566 int layoutht = mLayout.getLineTop(mMaximum); 1567 1568 if (layoutht >= viewht) { 1569 return bottom; 1570 } 1571 1572 final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; 1573 if (gravity == Gravity.TOP) { 1574 return bottom + viewht - layoutht; 1575 } else if (gravity == Gravity.BOTTOM) { 1576 return bottom; 1577 } else { // (gravity == Gravity.CENTER_VERTICAL) 1578 return bottom + (viewht - layoutht) / 2; 1579 } 1580 } 1581 1582 /** 1583 * Returns the total left padding of the view, including the left 1584 * Drawable if any. 1585 */ 1586 public int getTotalPaddingLeft() { 1587 return getCompoundPaddingLeft(); 1588 } 1589 1590 /** 1591 * Returns the total right padding of the view, including the right 1592 * Drawable if any. 1593 */ 1594 public int getTotalPaddingRight() { 1595 return getCompoundPaddingRight(); 1596 } 1597 1598 /** 1599 * Returns the total start padding of the view, including the start 1600 * Drawable if any. 1601 * 1602 * @hide 1603 */ 1604 public int getTotalPaddingStart() { 1605 return getCompoundPaddingStart(); 1606 } 1607 1608 /** 1609 * Returns the total end padding of the view, including the end 1610 * Drawable if any. 1611 * 1612 * @hide 1613 */ 1614 public int getTotalPaddingEnd() { 1615 return getCompoundPaddingEnd(); 1616 } 1617 1618 /** 1619 * Returns the total top padding of the view, including the top 1620 * Drawable if any, the extra space to keep more than maxLines 1621 * from showing, and the vertical offset for gravity, if any. 1622 */ 1623 public int getTotalPaddingTop() { 1624 return getExtendedPaddingTop() + getVerticalOffset(true); 1625 } 1626 1627 /** 1628 * Returns the total bottom padding of the view, including the bottom 1629 * Drawable if any, the extra space to keep more than maxLines 1630 * from showing, and the vertical offset for gravity, if any. 1631 */ 1632 public int getTotalPaddingBottom() { 1633 return getExtendedPaddingBottom() + getBottomVerticalOffset(true); 1634 } 1635 1636 /** 1637 * Sets the Drawables (if any) to appear to the left of, above, 1638 * to the right of, and below the text. Use null if you do not 1639 * want a Drawable there. The Drawables must already have had 1640 * {@link Drawable#setBounds} called. 1641 * 1642 * @attr ref android.R.styleable#TextView_drawableLeft 1643 * @attr ref android.R.styleable#TextView_drawableTop 1644 * @attr ref android.R.styleable#TextView_drawableRight 1645 * @attr ref android.R.styleable#TextView_drawableBottom 1646 */ 1647 public void setCompoundDrawables(Drawable left, Drawable top, 1648 Drawable right, Drawable bottom) { 1649 Drawables dr = mDrawables; 1650 1651 final boolean drawables = left != null || top != null 1652 || right != null || bottom != null; 1653 1654 if (!drawables) { 1655 // Clearing drawables... can we free the data structure? 1656 if (dr != null) { 1657 if (dr.mDrawablePadding == 0) { 1658 mDrawables = null; 1659 } else { 1660 // We need to retain the last set padding, so just clear 1661 // out all of the fields in the existing structure. 1662 if (dr.mDrawableLeft != null) dr.mDrawableLeft.setCallback(null); 1663 dr.mDrawableLeft = null; 1664 if (dr.mDrawableTop != null) dr.mDrawableTop.setCallback(null); 1665 dr.mDrawableTop = null; 1666 if (dr.mDrawableRight != null) dr.mDrawableRight.setCallback(null); 1667 dr.mDrawableRight = null; 1668 if (dr.mDrawableBottom != null) dr.mDrawableBottom.setCallback(null); 1669 dr.mDrawableBottom = null; 1670 dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0; 1671 dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0; 1672 dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0; 1673 dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0; 1674 } 1675 } 1676 } else { 1677 if (dr == null) { 1678 mDrawables = dr = new Drawables(); 1679 } 1680 1681 if (dr.mDrawableLeft != left && dr.mDrawableLeft != null) { 1682 dr.mDrawableLeft.setCallback(null); 1683 } 1684 dr.mDrawableLeft = left; 1685 1686 if (dr.mDrawableTop != top && dr.mDrawableTop != null) { 1687 dr.mDrawableTop.setCallback(null); 1688 } 1689 dr.mDrawableTop = top; 1690 1691 if (dr.mDrawableRight != right && dr.mDrawableRight != null) { 1692 dr.mDrawableRight.setCallback(null); 1693 } 1694 dr.mDrawableRight = right; 1695 1696 if (dr.mDrawableBottom != bottom && dr.mDrawableBottom != null) { 1697 dr.mDrawableBottom.setCallback(null); 1698 } 1699 dr.mDrawableBottom = bottom; 1700 1701 final Rect compoundRect = dr.mCompoundRect; 1702 int[] state; 1703 1704 state = getDrawableState(); 1705 1706 if (left != null) { 1707 left.setState(state); 1708 left.copyBounds(compoundRect); 1709 left.setCallback(this); 1710 dr.mDrawableSizeLeft = compoundRect.width(); 1711 dr.mDrawableHeightLeft = compoundRect.height(); 1712 } else { 1713 dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0; 1714 } 1715 1716 if (right != null) { 1717 right.setState(state); 1718 right.copyBounds(compoundRect); 1719 right.setCallback(this); 1720 dr.mDrawableSizeRight = compoundRect.width(); 1721 dr.mDrawableHeightRight = compoundRect.height(); 1722 } else { 1723 dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0; 1724 } 1725 1726 if (top != null) { 1727 top.setState(state); 1728 top.copyBounds(compoundRect); 1729 top.setCallback(this); 1730 dr.mDrawableSizeTop = compoundRect.height(); 1731 dr.mDrawableWidthTop = compoundRect.width(); 1732 } else { 1733 dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0; 1734 } 1735 1736 if (bottom != null) { 1737 bottom.setState(state); 1738 bottom.copyBounds(compoundRect); 1739 bottom.setCallback(this); 1740 dr.mDrawableSizeBottom = compoundRect.height(); 1741 dr.mDrawableWidthBottom = compoundRect.width(); 1742 } else { 1743 dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0; 1744 } 1745 } 1746 1747 invalidate(); 1748 requestLayout(); 1749 } 1750 1751 /** 1752 * Sets the Drawables (if any) to appear to the left of, above, 1753 * to the right of, and below the text. Use 0 if you do not 1754 * want a Drawable there. The Drawables' bounds will be set to 1755 * their intrinsic bounds. 1756 * 1757 * @param left Resource identifier of the left Drawable. 1758 * @param top Resource identifier of the top Drawable. 1759 * @param right Resource identifier of the right Drawable. 1760 * @param bottom Resource identifier of the bottom Drawable. 1761 * 1762 * @attr ref android.R.styleable#TextView_drawableLeft 1763 * @attr ref android.R.styleable#TextView_drawableTop 1764 * @attr ref android.R.styleable#TextView_drawableRight 1765 * @attr ref android.R.styleable#TextView_drawableBottom 1766 */ 1767 public void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) { 1768 final Resources resources = getContext().getResources(); 1769 setCompoundDrawablesWithIntrinsicBounds(left != 0 ? resources.getDrawable(left) : null, 1770 top != 0 ? resources.getDrawable(top) : null, 1771 right != 0 ? resources.getDrawable(right) : null, 1772 bottom != 0 ? resources.getDrawable(bottom) : null); 1773 } 1774 1775 /** 1776 * Sets the Drawables (if any) to appear to the left of, above, 1777 * to the right of, and below the text. Use null if you do not 1778 * want a Drawable there. The Drawables' bounds will be set to 1779 * their intrinsic bounds. 1780 * 1781 * @attr ref android.R.styleable#TextView_drawableLeft 1782 * @attr ref android.R.styleable#TextView_drawableTop 1783 * @attr ref android.R.styleable#TextView_drawableRight 1784 * @attr ref android.R.styleable#TextView_drawableBottom 1785 */ 1786 public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, 1787 Drawable right, Drawable bottom) { 1788 1789 if (left != null) { 1790 left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight()); 1791 } 1792 if (right != null) { 1793 right.setBounds(0, 0, right.getIntrinsicWidth(), right.getIntrinsicHeight()); 1794 } 1795 if (top != null) { 1796 top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight()); 1797 } 1798 if (bottom != null) { 1799 bottom.setBounds(0, 0, bottom.getIntrinsicWidth(), bottom.getIntrinsicHeight()); 1800 } 1801 setCompoundDrawables(left, top, right, bottom); 1802 } 1803 1804 /** 1805 * Sets the Drawables (if any) to appear to the start of, above, 1806 * to the end of, and below the text. Use null if you do not 1807 * want a Drawable there. The Drawables must already have had 1808 * {@link Drawable#setBounds} called. 1809 * 1810 * @attr ref android.R.styleable#TextView_drawableStart 1811 * @attr ref android.R.styleable#TextView_drawableTop 1812 * @attr ref android.R.styleable#TextView_drawableEnd 1813 * @attr ref android.R.styleable#TextView_drawableBottom 1814 * 1815 * @hide 1816 */ 1817 public void setCompoundDrawablesRelative(Drawable start, Drawable top, 1818 Drawable end, Drawable bottom) { 1819 Drawables dr = mDrawables; 1820 1821 final boolean drawables = start != null || top != null 1822 || end != null || bottom != null; 1823 1824 if (!drawables) { 1825 // Clearing drawables... can we free the data structure? 1826 if (dr != null) { 1827 if (dr.mDrawablePadding == 0) { 1828 mDrawables = null; 1829 } else { 1830 // We need to retain the last set padding, so just clear 1831 // out all of the fields in the existing structure. 1832 if (dr.mDrawableStart != null) dr.mDrawableStart.setCallback(null); 1833 dr.mDrawableStart = null; 1834 if (dr.mDrawableTop != null) dr.mDrawableTop.setCallback(null); 1835 dr.mDrawableTop = null; 1836 if (dr.mDrawableEnd != null) dr.mDrawableEnd.setCallback(null); 1837 dr.mDrawableEnd = null; 1838 if (dr.mDrawableBottom != null) dr.mDrawableBottom.setCallback(null); 1839 dr.mDrawableBottom = null; 1840 dr.mDrawableSizeStart = dr.mDrawableHeightStart = 0; 1841 dr.mDrawableSizeEnd = dr.mDrawableHeightEnd = 0; 1842 dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0; 1843 dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0; 1844 } 1845 } 1846 } else { 1847 if (dr == null) { 1848 mDrawables = dr = new Drawables(); 1849 } 1850 1851 if (dr.mDrawableStart != start && dr.mDrawableStart != null) { 1852 dr.mDrawableStart.setCallback(null); 1853 } 1854 dr.mDrawableStart = start; 1855 1856 if (dr.mDrawableTop != top && dr.mDrawableTop != null) { 1857 dr.mDrawableTop.setCallback(null); 1858 } 1859 dr.mDrawableTop = top; 1860 1861 if (dr.mDrawableEnd != end && dr.mDrawableEnd != null) { 1862 dr.mDrawableEnd.setCallback(null); 1863 } 1864 dr.mDrawableEnd = end; 1865 1866 if (dr.mDrawableBottom != bottom && dr.mDrawableBottom != null) { 1867 dr.mDrawableBottom.setCallback(null); 1868 } 1869 dr.mDrawableBottom = bottom; 1870 1871 final Rect compoundRect = dr.mCompoundRect; 1872 int[] state; 1873 1874 state = getDrawableState(); 1875 1876 if (start != null) { 1877 start.setState(state); 1878 start.copyBounds(compoundRect); 1879 start.setCallback(this); 1880 dr.mDrawableSizeStart = compoundRect.width(); 1881 dr.mDrawableHeightStart = compoundRect.height(); 1882 } else { 1883 dr.mDrawableSizeStart = dr.mDrawableHeightStart = 0; 1884 } 1885 1886 if (end != null) { 1887 end.setState(state); 1888 end.copyBounds(compoundRect); 1889 end.setCallback(this); 1890 dr.mDrawableSizeEnd = compoundRect.width(); 1891 dr.mDrawableHeightEnd = compoundRect.height(); 1892 } else { 1893 dr.mDrawableSizeEnd = dr.mDrawableHeightEnd = 0; 1894 } 1895 1896 if (top != null) { 1897 top.setState(state); 1898 top.copyBounds(compoundRect); 1899 top.setCallback(this); 1900 dr.mDrawableSizeTop = compoundRect.height(); 1901 dr.mDrawableWidthTop = compoundRect.width(); 1902 } else { 1903 dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0; 1904 } 1905 1906 if (bottom != null) { 1907 bottom.setState(state); 1908 bottom.copyBounds(compoundRect); 1909 bottom.setCallback(this); 1910 dr.mDrawableSizeBottom = compoundRect.height(); 1911 dr.mDrawableWidthBottom = compoundRect.width(); 1912 } else { 1913 dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0; 1914 } 1915 } 1916 1917 resolveDrawables(); 1918 invalidate(); 1919 requestLayout(); 1920 } 1921 1922 /** 1923 * Sets the Drawables (if any) to appear to the start of, above, 1924 * to the end of, and below the text. Use 0 if you do not 1925 * want a Drawable there. The Drawables' bounds will be set to 1926 * their intrinsic bounds. 1927 * 1928 * @param start Resource identifier of the start Drawable. 1929 * @param top Resource identifier of the top Drawable. 1930 * @param end Resource identifier of the end Drawable. 1931 * @param bottom Resource identifier of the bottom Drawable. 1932 * 1933 * @attr ref android.R.styleable#TextView_drawableStart 1934 * @attr ref android.R.styleable#TextView_drawableTop 1935 * @attr ref android.R.styleable#TextView_drawableEnd 1936 * @attr ref android.R.styleable#TextView_drawableBottom 1937 * 1938 * @hide 1939 */ 1940 public void setCompoundDrawablesRelativeWithIntrinsicBounds(int start, int top, int end, 1941 int bottom) { 1942 resetResolvedDrawables(); 1943 final Resources resources = getContext().getResources(); 1944 setCompoundDrawablesRelativeWithIntrinsicBounds( 1945 start != 0 ? resources.getDrawable(start) : null, 1946 top != 0 ? resources.getDrawable(top) : null, 1947 end != 0 ? resources.getDrawable(end) : null, 1948 bottom != 0 ? resources.getDrawable(bottom) : null); 1949 } 1950 1951 /** 1952 * Sets the Drawables (if any) to appear to the start of, above, 1953 * to the end of, and below the text. Use null if you do not 1954 * want a Drawable there. The Drawables' bounds will be set to 1955 * their intrinsic bounds. 1956 * 1957 * @attr ref android.R.styleable#TextView_drawableStart 1958 * @attr ref android.R.styleable#TextView_drawableTop 1959 * @attr ref android.R.styleable#TextView_drawableEnd 1960 * @attr ref android.R.styleable#TextView_drawableBottom 1961 * 1962 * @hide 1963 */ 1964 public void setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable start, Drawable top, 1965 Drawable end, Drawable bottom) { 1966 1967 resetResolvedDrawables(); 1968 if (start != null) { 1969 start.setBounds(0, 0, start.getIntrinsicWidth(), start.getIntrinsicHeight()); 1970 } 1971 if (end != null) { 1972 end.setBounds(0, 0, end.getIntrinsicWidth(), end.getIntrinsicHeight()); 1973 } 1974 if (top != null) { 1975 top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight()); 1976 } 1977 if (bottom != null) { 1978 bottom.setBounds(0, 0, bottom.getIntrinsicWidth(), bottom.getIntrinsicHeight()); 1979 } 1980 setCompoundDrawablesRelative(start, top, end, bottom); 1981 } 1982 1983 /** 1984 * Returns drawables for the left, top, right, and bottom borders. 1985 */ 1986 public Drawable[] getCompoundDrawables() { 1987 final Drawables dr = mDrawables; 1988 if (dr != null) { 1989 return new Drawable[] { 1990 dr.mDrawableLeft, dr.mDrawableTop, dr.mDrawableRight, dr.mDrawableBottom 1991 }; 1992 } else { 1993 return new Drawable[] { null, null, null, null }; 1994 } 1995 } 1996 1997 /** 1998 * Returns drawables for the start, top, end, and bottom borders. 1999 * 2000 * @hide 2001 */ 2002 public Drawable[] getCompoundDrawablesRelative() { 2003 final Drawables dr = mDrawables; 2004 if (dr != null) { 2005 return new Drawable[] { 2006 dr.mDrawableStart, dr.mDrawableTop, dr.mDrawableEnd, dr.mDrawableBottom 2007 }; 2008 } else { 2009 return new Drawable[] { null, null, null, null }; 2010 } 2011 } 2012 2013 /** 2014 * Sets the size of the padding between the compound drawables and 2015 * the text. 2016 * 2017 * @attr ref android.R.styleable#TextView_drawablePadding 2018 */ 2019 public void setCompoundDrawablePadding(int pad) { 2020 Drawables dr = mDrawables; 2021 if (pad == 0) { 2022 if (dr != null) { 2023 dr.mDrawablePadding = pad; 2024 } 2025 } else { 2026 if (dr == null) { 2027 mDrawables = dr = new Drawables(); 2028 } 2029 dr.mDrawablePadding = pad; 2030 } 2031 2032 invalidate(); 2033 requestLayout(); 2034 } 2035 2036 /** 2037 * Returns the padding between the compound drawables and the text. 2038 */ 2039 public int getCompoundDrawablePadding() { 2040 final Drawables dr = mDrawables; 2041 return dr != null ? dr.mDrawablePadding : 0; 2042 } 2043 2044 @Override 2045 public void setPadding(int left, int top, int right, int bottom) { 2046 if (left != mPaddingLeft || 2047 right != mPaddingRight || 2048 top != mPaddingTop || 2049 bottom != mPaddingBottom) { 2050 nullLayouts(); 2051 } 2052 2053 // the super call will requestLayout() 2054 super.setPadding(left, top, right, bottom); 2055 invalidate(); 2056 } 2057 2058 /** 2059 * Gets the autolink mask of the text. See {@link 2060 * android.text.util.Linkify#ALL Linkify.ALL} and peers for 2061 * possible values. 2062 * 2063 * @attr ref android.R.styleable#TextView_autoLink 2064 */ 2065 public final int getAutoLinkMask() { 2066 return mAutoLinkMask; 2067 } 2068 2069 /** 2070 * Sets the text color, size, style, hint color, and highlight color 2071 * from the specified TextAppearance resource. 2072 */ 2073 public void setTextAppearance(Context context, int resid) { 2074 TypedArray appearance = 2075 context.obtainStyledAttributes(resid, 2076 com.android.internal.R.styleable.TextAppearance); 2077 2078 int color; 2079 ColorStateList colors; 2080 int ts; 2081 2082 color = appearance.getColor(com.android.internal.R.styleable.TextAppearance_textColorHighlight, 0); 2083 if (color != 0) { 2084 setHighlightColor(color); 2085 } 2086 2087 colors = appearance.getColorStateList(com.android.internal.R.styleable. 2088 TextAppearance_textColor); 2089 if (colors != null) { 2090 setTextColor(colors); 2091 } 2092 2093 ts = appearance.getDimensionPixelSize(com.android.internal.R.styleable. 2094 TextAppearance_textSize, 0); 2095 if (ts != 0) { 2096 setRawTextSize(ts); 2097 } 2098 2099 colors = appearance.getColorStateList(com.android.internal.R.styleable. 2100 TextAppearance_textColorHint); 2101 if (colors != null) { 2102 setHintTextColor(colors); 2103 } 2104 2105 colors = appearance.getColorStateList(com.android.internal.R.styleable. 2106 TextAppearance_textColorLink); 2107 if (colors != null) { 2108 setLinkTextColor(colors); 2109 } 2110 2111 int typefaceIndex, styleIndex; 2112 2113 typefaceIndex = appearance.getInt(com.android.internal.R.styleable. 2114 TextAppearance_typeface, -1); 2115 styleIndex = appearance.getInt(com.android.internal.R.styleable. 2116 TextAppearance_textStyle, -1); 2117 2118 setTypefaceByIndex(typefaceIndex, styleIndex); 2119 2120 if (appearance.getBoolean(com.android.internal.R.styleable.TextAppearance_textAllCaps, 2121 false)) { 2122 setTransformationMethod(new AllCapsTransformationMethod(getContext())); 2123 } 2124 2125 appearance.recycle(); 2126 } 2127 2128 /** 2129 * @return the size (in pixels) of the default text size in this TextView. 2130 */ 2131 public float getTextSize() { 2132 return mTextPaint.getTextSize(); 2133 } 2134 2135 /** 2136 * Set the default text size to the given value, interpreted as "scaled 2137 * pixel" units. This size is adjusted based on the current density and 2138 * user font size preference. 2139 * 2140 * @param size The scaled pixel size. 2141 * 2142 * @attr ref android.R.styleable#TextView_textSize 2143 */ 2144 @android.view.RemotableViewMethod 2145 public void setTextSize(float size) { 2146 setTextSize(TypedValue.COMPLEX_UNIT_SP, size); 2147 } 2148 2149 /** 2150 * Set the default text size to a given unit and value. See {@link 2151 * TypedValue} for the possible dimension units. 2152 * 2153 * @param unit The desired dimension unit. 2154 * @param size The desired size in the given units. 2155 * 2156 * @attr ref android.R.styleable#TextView_textSize 2157 */ 2158 public void setTextSize(int unit, float size) { 2159 Context c = getContext(); 2160 Resources r; 2161 2162 if (c == null) 2163 r = Resources.getSystem(); 2164 else 2165 r = c.getResources(); 2166 2167 setRawTextSize(TypedValue.applyDimension( 2168 unit, size, r.getDisplayMetrics())); 2169 } 2170 2171 private void setRawTextSize(float size) { 2172 if (size != mTextPaint.getTextSize()) { 2173 mTextPaint.setTextSize(size); 2174 2175 if (mLayout != null) { 2176 nullLayouts(); 2177 requestLayout(); 2178 invalidate(); 2179 } 2180 } 2181 } 2182 2183 /** 2184 * @return the extent by which text is currently being stretched 2185 * horizontally. This will usually be 1. 2186 */ 2187 public float getTextScaleX() { 2188 return mTextPaint.getTextScaleX(); 2189 } 2190 2191 /** 2192 * Sets the extent by which text should be stretched horizontally. 2193 * 2194 * @attr ref android.R.styleable#TextView_textScaleX 2195 */ 2196 @android.view.RemotableViewMethod 2197 public void setTextScaleX(float size) { 2198 if (size != mTextPaint.getTextScaleX()) { 2199 mUserSetTextScaleX = true; 2200 mTextPaint.setTextScaleX(size); 2201 2202 if (mLayout != null) { 2203 nullLayouts(); 2204 requestLayout(); 2205 invalidate(); 2206 } 2207 } 2208 } 2209 2210 /** 2211 * Sets the typeface and style in which the text should be displayed. 2212 * Note that not all Typeface families actually have bold and italic 2213 * variants, so you may need to use 2214 * {@link #setTypeface(Typeface, int)} to get the appearance 2215 * that you actually want. 2216 * 2217 * @attr ref android.R.styleable#TextView_typeface 2218 * @attr ref android.R.styleable#TextView_textStyle 2219 */ 2220 public void setTypeface(Typeface tf) { 2221 if (mTextPaint.getTypeface() != tf) { 2222 mTextPaint.setTypeface(tf); 2223 2224 if (mLayout != null) { 2225 nullLayouts(); 2226 requestLayout(); 2227 invalidate(); 2228 } 2229 } 2230 } 2231 2232 /** 2233 * @return the current typeface and style in which the text is being 2234 * displayed. 2235 */ 2236 public Typeface getTypeface() { 2237 return mTextPaint.getTypeface(); 2238 } 2239 2240 /** 2241 * Sets the text color for all the states (normal, selected, 2242 * focused) to be this color. 2243 * 2244 * @attr ref android.R.styleable#TextView_textColor 2245 */ 2246 @android.view.RemotableViewMethod 2247 public void setTextColor(int color) { 2248 mTextColor = ColorStateList.valueOf(color); 2249 updateTextColors(); 2250 } 2251 2252 /** 2253 * Sets the text color. 2254 * 2255 * @attr ref android.R.styleable#TextView_textColor 2256 */ 2257 public void setTextColor(ColorStateList colors) { 2258 if (colors == null) { 2259 throw new NullPointerException(); 2260 } 2261 2262 mTextColor = colors; 2263 updateTextColors(); 2264 } 2265 2266 /** 2267 * Return the set of text colors. 2268 * 2269 * @return Returns the set of text colors. 2270 */ 2271 public final ColorStateList getTextColors() { 2272 return mTextColor; 2273 } 2274 2275 /** 2276 * <p>Return the current color selected for normal text.</p> 2277 * 2278 * @return Returns the current text color. 2279 */ 2280 public final int getCurrentTextColor() { 2281 return mCurTextColor; 2282 } 2283 2284 /** 2285 * Sets the color used to display the selection highlight. 2286 * 2287 * @attr ref android.R.styleable#TextView_textColorHighlight 2288 */ 2289 @android.view.RemotableViewMethod 2290 public void setHighlightColor(int color) { 2291 if (mHighlightColor != color) { 2292 mHighlightColor = color; 2293 invalidate(); 2294 } 2295 } 2296 2297 /** 2298 * Gives the text a shadow of the specified radius and color, the specified 2299 * distance from its normal position. 2300 * 2301 * @attr ref android.R.styleable#TextView_shadowColor 2302 * @attr ref android.R.styleable#TextView_shadowDx 2303 * @attr ref android.R.styleable#TextView_shadowDy 2304 * @attr ref android.R.styleable#TextView_shadowRadius 2305 */ 2306 public void setShadowLayer(float radius, float dx, float dy, int color) { 2307 mTextPaint.setShadowLayer(radius, dx, dy, color); 2308 2309 mShadowRadius = radius; 2310 mShadowDx = dx; 2311 mShadowDy = dy; 2312 2313 invalidate(); 2314 } 2315 2316 /** 2317 * @return the base paint used for the text. Please use this only to 2318 * consult the Paint's properties and not to change them. 2319 */ 2320 public TextPaint getPaint() { 2321 return mTextPaint; 2322 } 2323 2324 /** 2325 * Sets the autolink mask of the text. See {@link 2326 * android.text.util.Linkify#ALL Linkify.ALL} and peers for 2327 * possible values. 2328 * 2329 * @attr ref android.R.styleable#TextView_autoLink 2330 */ 2331 @android.view.RemotableViewMethod 2332 public final void setAutoLinkMask(int mask) { 2333 mAutoLinkMask = mask; 2334 } 2335 2336 /** 2337 * Sets whether the movement method will automatically be set to 2338 * {@link LinkMovementMethod} if {@link #setAutoLinkMask} has been 2339 * set to nonzero and links are detected in {@link #setText}. 2340 * The default is true. 2341 * 2342 * @attr ref android.R.styleable#TextView_linksClickable 2343 */ 2344 @android.view.RemotableViewMethod 2345 public final void setLinksClickable(boolean whether) { 2346 mLinksClickable = whether; 2347 } 2348 2349 /** 2350 * Returns whether the movement method will automatically be set to 2351 * {@link LinkMovementMethod} if {@link #setAutoLinkMask} has been 2352 * set to nonzero and links are detected in {@link #setText}. 2353 * The default is true. 2354 * 2355 * @attr ref android.R.styleable#TextView_linksClickable 2356 */ 2357 public final boolean getLinksClickable() { 2358 return mLinksClickable; 2359 } 2360 2361 /** 2362 * Returns the list of URLSpans attached to the text 2363 * (by {@link Linkify} or otherwise) if any. You can call 2364 * {@link URLSpan#getURL} on them to find where they link to 2365 * or use {@link Spanned#getSpanStart} and {@link Spanned#getSpanEnd} 2366 * to find the region of the text they are attached to. 2367 */ 2368 public URLSpan[] getUrls() { 2369 if (mText instanceof Spanned) { 2370 return ((Spanned) mText).getSpans(0, mText.length(), URLSpan.class); 2371 } else { 2372 return new URLSpan[0]; 2373 } 2374 } 2375 2376 /** 2377 * Sets the color of the hint text. 2378 * 2379 * @attr ref android.R.styleable#TextView_textColorHint 2380 */ 2381 @android.view.RemotableViewMethod 2382 public final void setHintTextColor(int color) { 2383 mHintTextColor = ColorStateList.valueOf(color); 2384 updateTextColors(); 2385 } 2386 2387 /** 2388 * Sets the color of the hint text. 2389 * 2390 * @attr ref android.R.styleable#TextView_textColorHint 2391 */ 2392 public final void setHintTextColor(ColorStateList colors) { 2393 mHintTextColor = colors; 2394 updateTextColors(); 2395 } 2396 2397 /** 2398 * <p>Return the color used to paint the hint text.</p> 2399 * 2400 * @return Returns the list of hint text colors. 2401 */ 2402 public final ColorStateList getHintTextColors() { 2403 return mHintTextColor; 2404 } 2405 2406 /** 2407 * <p>Return the current color selected to paint the hint text.</p> 2408 * 2409 * @return Returns the current hint text color. 2410 */ 2411 public final int getCurrentHintTextColor() { 2412 return mHintTextColor != null ? mCurHintTextColor : mCurTextColor; 2413 } 2414 2415 /** 2416 * Sets the color of links in the text. 2417 * 2418 * @attr ref android.R.styleable#TextView_textColorLink 2419 */ 2420 @android.view.RemotableViewMethod 2421 public final void setLinkTextColor(int color) { 2422 mLinkTextColor = ColorStateList.valueOf(color); 2423 updateTextColors(); 2424 } 2425 2426 /** 2427 * Sets the color of links in the text. 2428 * 2429 * @attr ref android.R.styleable#TextView_textColorLink 2430 */ 2431 public final void setLinkTextColor(ColorStateList colors) { 2432 mLinkTextColor = colors; 2433 updateTextColors(); 2434 } 2435 2436 /** 2437 * <p>Returns the color used to paint links in the text.</p> 2438 * 2439 * @return Returns the list of link text colors. 2440 */ 2441 public final ColorStateList getLinkTextColors() { 2442 return mLinkTextColor; 2443 } 2444 2445 /** 2446 * Sets the horizontal alignment of the text and the 2447 * vertical gravity that will be used when there is extra space 2448 * in the TextView beyond what is required for the text itself. 2449 * 2450 * @see android.view.Gravity 2451 * @attr ref android.R.styleable#TextView_gravity 2452 */ 2453 public void setGravity(int gravity) { 2454 if ((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) { 2455 gravity |= Gravity.START; 2456 } 2457 if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) { 2458 gravity |= Gravity.TOP; 2459 } 2460 2461 boolean newLayout = false; 2462 2463 if ((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) != 2464 (mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK)) { 2465 newLayout = true; 2466 } 2467 2468 if (gravity != mGravity) { 2469 invalidate(); 2470 } 2471 2472 mGravity = gravity; 2473 2474 if (mLayout != null && newLayout) { 2475 // XXX this is heavy-handed because no actual content changes. 2476 int want = mLayout.getWidth(); 2477 int hintWant = mHintLayout == null ? 0 : mHintLayout.getWidth(); 2478 2479 makeNewLayout(want, hintWant, UNKNOWN_BORING, UNKNOWN_BORING, 2480 mRight - mLeft - getCompoundPaddingLeft() - 2481 getCompoundPaddingRight(), true); 2482 } 2483 } 2484 2485 /** 2486 * Returns the horizontal and vertical alignment of this TextView. 2487 * 2488 * @see android.view.Gravity 2489 * @attr ref android.R.styleable#TextView_gravity 2490 */ 2491 public int getGravity() { 2492 return mGravity; 2493 } 2494 2495 /** 2496 * @return the flags on the Paint being used to display the text. 2497 * @see Paint#getFlags 2498 */ 2499 public int getPaintFlags() { 2500 return mTextPaint.getFlags(); 2501 } 2502 2503 /** 2504 * Sets flags on the Paint being used to display the text and 2505 * reflows the text if they are different from the old flags. 2506 * @see Paint#setFlags 2507 */ 2508 @android.view.RemotableViewMethod 2509 public void setPaintFlags(int flags) { 2510 if (mTextPaint.getFlags() != flags) { 2511 mTextPaint.setFlags(flags); 2512 2513 if (mLayout != null) { 2514 nullLayouts(); 2515 requestLayout(); 2516 invalidate(); 2517 } 2518 } 2519 } 2520 2521 /** 2522 * Sets whether the text should be allowed to be wider than the 2523 * View is. If false, it will be wrapped to the width of the View. 2524 * 2525 * @attr ref android.R.styleable#TextView_scrollHorizontally 2526 */ 2527 public void setHorizontallyScrolling(boolean whether) { 2528 if (mHorizontallyScrolling != whether) { 2529 mHorizontallyScrolling = whether; 2530 2531 if (mLayout != null) { 2532 nullLayouts(); 2533 requestLayout(); 2534 invalidate(); 2535 } 2536 } 2537 } 2538 2539 /** 2540 * Makes the TextView at least this many lines tall. 2541 * 2542 * Setting this value overrides any other (minimum) height setting. A single line TextView will 2543 * set this value to 1. 2544 * 2545 * @attr ref android.R.styleable#TextView_minLines 2546 */ 2547 @android.view.RemotableViewMethod 2548 public void setMinLines(int minlines) { 2549 mMinimum = minlines; 2550 mMinMode = LINES; 2551 2552 requestLayout(); 2553 invalidate(); 2554 } 2555 2556 /** 2557 * Makes the TextView at least this many pixels tall. 2558 * 2559 * Setting this value overrides any other (minimum) number of lines setting. 2560 * 2561 * @attr ref android.R.styleable#TextView_minHeight 2562 */ 2563 @android.view.RemotableViewMethod 2564 public void setMinHeight(int minHeight) { 2565 mMinimum = minHeight; 2566 mMinMode = PIXELS; 2567 2568 requestLayout(); 2569 invalidate(); 2570 } 2571 2572 /** 2573 * Makes the TextView at most this many lines tall. 2574 * 2575 * Setting this value overrides any other (maximum) height setting. 2576 * 2577 * @attr ref android.R.styleable#TextView_maxLines 2578 */ 2579 @android.view.RemotableViewMethod 2580 public void setMaxLines(int maxlines) { 2581 mMaximum = maxlines; 2582 mMaxMode = LINES; 2583 2584 requestLayout(); 2585 invalidate(); 2586 } 2587 2588 /** 2589 * Makes the TextView at most this many pixels tall. This option is mutually exclusive with the 2590 * {@link #setMaxLines(int)} method. 2591 * 2592 * Setting this value overrides any other (maximum) number of lines setting. 2593 * 2594 * @attr ref android.R.styleable#TextView_maxHeight 2595 */ 2596 @android.view.RemotableViewMethod 2597 public void setMaxHeight(int maxHeight) { 2598 mMaximum = maxHeight; 2599 mMaxMode = PIXELS; 2600 2601 requestLayout(); 2602 invalidate(); 2603 } 2604 2605 /** 2606 * Makes the TextView exactly this many lines tall. 2607 * 2608 * Note that setting this value overrides any other (minimum / maximum) number of lines or 2609 * height setting. A single line TextView will set this value to 1. 2610 * 2611 * @attr ref android.R.styleable#TextView_lines 2612 */ 2613 @android.view.RemotableViewMethod 2614 public void setLines(int lines) { 2615 mMaximum = mMinimum = lines; 2616 mMaxMode = mMinMode = LINES; 2617 2618 requestLayout(); 2619 invalidate(); 2620 } 2621 2622 /** 2623 * Makes the TextView exactly this many pixels tall. 2624 * You could do the same thing by specifying this number in the 2625 * LayoutParams. 2626 * 2627 * Note that setting this value overrides any other (minimum / maximum) number of lines or 2628 * height setting. 2629 * 2630 * @attr ref android.R.styleable#TextView_height 2631 */ 2632 @android.view.RemotableViewMethod 2633 public void setHeight(int pixels) { 2634 mMaximum = mMinimum = pixels; 2635 mMaxMode = mMinMode = PIXELS; 2636 2637 requestLayout(); 2638 invalidate(); 2639 } 2640 2641 /** 2642 * Makes the TextView at least this many ems wide 2643 * 2644 * @attr ref android.R.styleable#TextView_minEms 2645 */ 2646 @android.view.RemotableViewMethod 2647 public void setMinEms(int minems) { 2648 mMinWidth = minems; 2649 mMinWidthMode = EMS; 2650 2651 requestLayout(); 2652 invalidate(); 2653 } 2654 2655 /** 2656 * Makes the TextView at least this many pixels wide 2657 * 2658 * @attr ref android.R.styleable#TextView_minWidth 2659 */ 2660 @android.view.RemotableViewMethod 2661 public void setMinWidth(int minpixels) { 2662 mMinWidth = minpixels; 2663 mMinWidthMode = PIXELS; 2664 2665 requestLayout(); 2666 invalidate(); 2667 } 2668 2669 /** 2670 * Makes the TextView at most this many ems wide 2671 * 2672 * @attr ref android.R.styleable#TextView_maxEms 2673 */ 2674 @android.view.RemotableViewMethod 2675 public void setMaxEms(int maxems) { 2676 mMaxWidth = maxems; 2677 mMaxWidthMode = EMS; 2678 2679 requestLayout(); 2680 invalidate(); 2681 } 2682 2683 /** 2684 * Makes the TextView at most this many pixels wide 2685 * 2686 * @attr ref android.R.styleable#TextView_maxWidth 2687 */ 2688 @android.view.RemotableViewMethod 2689 public void setMaxWidth(int maxpixels) { 2690 mMaxWidth = maxpixels; 2691 mMaxWidthMode = PIXELS; 2692 2693 requestLayout(); 2694 invalidate(); 2695 } 2696 2697 /** 2698 * Makes the TextView exactly this many ems wide 2699 * 2700 * @attr ref android.R.styleable#TextView_ems 2701 */ 2702 @android.view.RemotableViewMethod 2703 public void setEms(int ems) { 2704 mMaxWidth = mMinWidth = ems; 2705 mMaxWidthMode = mMinWidthMode = EMS; 2706 2707 requestLayout(); 2708 invalidate(); 2709 } 2710 2711 /** 2712 * Makes the TextView exactly this many pixels wide. 2713 * You could do the same thing by specifying this number in the 2714 * LayoutParams. 2715 * 2716 * @attr ref android.R.styleable#TextView_width 2717 */ 2718 @android.view.RemotableViewMethod 2719 public void setWidth(int pixels) { 2720 mMaxWidth = mMinWidth = pixels; 2721 mMaxWidthMode = mMinWidthMode = PIXELS; 2722 2723 requestLayout(); 2724 invalidate(); 2725 } 2726 2727 2728 /** 2729 * Sets line spacing for this TextView. Each line will have its height 2730 * multiplied by <code>mult</code> and have <code>add</code> added to it. 2731 * 2732 * @attr ref android.R.styleable#TextView_lineSpacingExtra 2733 * @attr ref android.R.styleable#TextView_lineSpacingMultiplier 2734 */ 2735 public void setLineSpacing(float add, float mult) { 2736 if (mSpacingAdd != add || mSpacingMult != mult) { 2737 mSpacingAdd = add; 2738 mSpacingMult = mult; 2739 2740 if (mLayout != null) { 2741 nullLayouts(); 2742 requestLayout(); 2743 invalidate(); 2744 } 2745 } 2746 } 2747 2748 /** 2749 * Convenience method: Append the specified text to the TextView's 2750 * display buffer, upgrading it to BufferType.EDITABLE if it was 2751 * not already editable. 2752 */ 2753 public final void append(CharSequence text) { 2754 append(text, 0, text.length()); 2755 } 2756 2757 /** 2758 * Convenience method: Append the specified text slice to the TextView's 2759 * display buffer, upgrading it to BufferType.EDITABLE if it was 2760 * not already editable. 2761 */ 2762 public void append(CharSequence text, int start, int end) { 2763 if (!(mText instanceof Editable)) { 2764 setText(mText, BufferType.EDITABLE); 2765 } 2766 2767 ((Editable) mText).append(text, start, end); 2768 } 2769 2770 private void updateTextColors() { 2771 boolean inval = false; 2772 int color = mTextColor.getColorForState(getDrawableState(), 0); 2773 if (color != mCurTextColor) { 2774 mCurTextColor = color; 2775 inval = true; 2776 } 2777 if (mLinkTextColor != null) { 2778 color = mLinkTextColor.getColorForState(getDrawableState(), 0); 2779 if (color != mTextPaint.linkColor) { 2780 mTextPaint.linkColor = color; 2781 inval = true; 2782 } 2783 } 2784 if (mHintTextColor != null) { 2785 color = mHintTextColor.getColorForState(getDrawableState(), 0); 2786 if (color != mCurHintTextColor && mText.length() == 0) { 2787 mCurHintTextColor = color; 2788 inval = true; 2789 } 2790 } 2791 if (inval) { 2792 invalidate(); 2793 } 2794 } 2795 2796 @Override 2797 protected void drawableStateChanged() { 2798 super.drawableStateChanged(); 2799 if (mTextColor != null && mTextColor.isStateful() 2800 || (mHintTextColor != null && mHintTextColor.isStateful()) 2801 || (mLinkTextColor != null && mLinkTextColor.isStateful())) { 2802 updateTextColors(); 2803 } 2804 2805 final Drawables dr = mDrawables; 2806 if (dr != null) { 2807 int[] state = getDrawableState(); 2808 if (dr.mDrawableTop != null && dr.mDrawableTop.isStateful()) { 2809 dr.mDrawableTop.setState(state); 2810 } 2811 if (dr.mDrawableBottom != null && dr.mDrawableBottom.isStateful()) { 2812 dr.mDrawableBottom.setState(state); 2813 } 2814 if (dr.mDrawableLeft != null && dr.mDrawableLeft.isStateful()) { 2815 dr.mDrawableLeft.setState(state); 2816 } 2817 if (dr.mDrawableRight != null && dr.mDrawableRight.isStateful()) { 2818 dr.mDrawableRight.setState(state); 2819 } 2820 if (dr.mDrawableStart != null && dr.mDrawableStart.isStateful()) { 2821 dr.mDrawableStart.setState(state); 2822 } 2823 if (dr.mDrawableEnd != null && dr.mDrawableEnd.isStateful()) { 2824 dr.mDrawableEnd.setState(state); 2825 } 2826 } 2827 } 2828 2829 /** 2830 * User interface state that is stored by TextView for implementing 2831 * {@link View#onSaveInstanceState}. 2832 */ 2833 public static class SavedState extends BaseSavedState { 2834 int selStart; 2835 int selEnd; 2836 CharSequence text; 2837 boolean frozenWithFocus; 2838 CharSequence error; 2839 2840 SavedState(Parcelable superState) { 2841 super(superState); 2842 } 2843 2844 @Override 2845 public void writeToParcel(Parcel out, int flags) { 2846 super.writeToParcel(out, flags); 2847 out.writeInt(selStart); 2848 out.writeInt(selEnd); 2849 out.writeInt(frozenWithFocus ? 1 : 0); 2850 TextUtils.writeToParcel(text, out, flags); 2851 2852 if (error == null) { 2853 out.writeInt(0); 2854 } else { 2855 out.writeInt(1); 2856 TextUtils.writeToParcel(error, out, flags); 2857 } 2858 } 2859 2860 @Override 2861 public String toString() { 2862 String str = "TextView.SavedState{" 2863 + Integer.toHexString(System.identityHashCode(this)) 2864 + " start=" + selStart + " end=" + selEnd; 2865 if (text != null) { 2866 str += " text=" + text; 2867 } 2868 return str + "}"; 2869 } 2870 2871 @SuppressWarnings("hiding") 2872 public static final Parcelable.Creator<SavedState> CREATOR 2873 = new Parcelable.Creator<SavedState>() { 2874 public SavedState createFromParcel(Parcel in) { 2875 return new SavedState(in); 2876 } 2877 2878 public SavedState[] newArray(int size) { 2879 return new SavedState[size]; 2880 } 2881 }; 2882 2883 private SavedState(Parcel in) { 2884 super(in); 2885 selStart = in.readInt(); 2886 selEnd = in.readInt(); 2887 frozenWithFocus = (in.readInt() != 0); 2888 text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 2889 2890 if (in.readInt() != 0) { 2891 error = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 2892 } 2893 } 2894 } 2895 2896 @Override 2897 public Parcelable onSaveInstanceState() { 2898 Parcelable superState = super.onSaveInstanceState(); 2899 2900 // Save state if we are forced to 2901 boolean save = mFreezesText; 2902 int start = 0; 2903 int end = 0; 2904 2905 if (mText != null) { 2906 start = getSelectionStart(); 2907 end = getSelectionEnd(); 2908 if (start >= 0 || end >= 0) { 2909 // Or save state if there is a selection 2910 save = true; 2911 } 2912 } 2913 2914 if (save) { 2915 SavedState ss = new SavedState(superState); 2916 // XXX Should also save the current scroll position! 2917 ss.selStart = start; 2918 ss.selEnd = end; 2919 2920 if (mText instanceof Spanned) { 2921 /* 2922 * Calling setText() strips off any ChangeWatchers; 2923 * strip them now to avoid leaking references. 2924 * But do it to a copy so that if there are any 2925 * further changes to the text of this view, it 2926 * won't get into an inconsistent state. 2927 */ 2928 2929 Spannable sp = new SpannableString(mText); 2930 2931 for (ChangeWatcher cw : 2932 sp.getSpans(0, sp.length(), ChangeWatcher.class)) { 2933 sp.removeSpan(cw); 2934 } 2935 2936 sp.removeSpan(mSuggestionRangeSpan); 2937 2938 ss.text = sp; 2939 } else { 2940 ss.text = mText.toString(); 2941 } 2942 2943 if (isFocused() && start >= 0 && end >= 0) { 2944 ss.frozenWithFocus = true; 2945 } 2946 2947 ss.error = mError; 2948 2949 return ss; 2950 } 2951 2952 return superState; 2953 } 2954 2955 @Override 2956 public void onRestoreInstanceState(Parcelable state) { 2957 if (!(state instanceof SavedState)) { 2958 super.onRestoreInstanceState(state); 2959 return; 2960 } 2961 2962 SavedState ss = (SavedState)state; 2963 super.onRestoreInstanceState(ss.getSuperState()); 2964 2965 // XXX restore buffer type too, as well as lots of other stuff 2966 if (ss.text != null) { 2967 setText(ss.text); 2968 } 2969 2970 if (ss.selStart >= 0 && ss.selEnd >= 0) { 2971 if (mText instanceof Spannable) { 2972 int len = mText.length(); 2973 2974 if (ss.selStart > len || ss.selEnd > len) { 2975 String restored = ""; 2976 2977 if (ss.text != null) { 2978 restored = "(restored) "; 2979 } 2980 2981 Log.e(LOG_TAG, "Saved cursor position " + ss.selStart + 2982 "/" + ss.selEnd + " out of range for " + restored + 2983 "text " + mText); 2984 } else { 2985 Selection.setSelection((Spannable) mText, ss.selStart, 2986 ss.selEnd); 2987 2988 if (ss.frozenWithFocus) { 2989 mFrozenWithFocus = true; 2990 } 2991 } 2992 } 2993 } 2994 2995 if (ss.error != null) { 2996 final CharSequence error = ss.error; 2997 // Display the error later, after the first layout pass 2998 post(new Runnable() { 2999 public void run() { 3000 setError(error); 3001 } 3002 }); 3003 } 3004 } 3005 3006 /** 3007 * Control whether this text view saves its entire text contents when 3008 * freezing to an icicle, in addition to dynamic state such as cursor 3009 * position. By default this is false, not saving the text. Set to true 3010 * if the text in the text view is not being saved somewhere else in 3011 * persistent storage (such as in a content provider) so that if the 3012 * view is later thawed the user will not lose their data. 3013 * 3014 * @param freezesText Controls whether a frozen icicle should include the 3015 * entire text data: true to include it, false to not. 3016 * 3017 * @attr ref android.R.styleable#TextView_freezesText 3018 */ 3019 @android.view.RemotableViewMethod 3020 public void setFreezesText(boolean freezesText) { 3021 mFreezesText = freezesText; 3022 } 3023 3024 /** 3025 * Return whether this text view is including its entire text contents 3026 * in frozen icicles. 3027 * 3028 * @return Returns true if text is included, false if it isn't. 3029 * 3030 * @see #setFreezesText 3031 */ 3032 public boolean getFreezesText() { 3033 return mFreezesText; 3034 } 3035 3036 /////////////////////////////////////////////////////////////////////////// 3037 3038 /** 3039 * Sets the Factory used to create new Editables. 3040 */ 3041 public final void setEditableFactory(Editable.Factory factory) { 3042 mEditableFactory = factory; 3043 setText(mText); 3044 } 3045 3046 /** 3047 * Sets the Factory used to create new Spannables. 3048 */ 3049 public final void setSpannableFactory(Spannable.Factory factory) { 3050 mSpannableFactory = factory; 3051 setText(mText); 3052 } 3053 3054 /** 3055 * Sets the string value of the TextView. TextView <em>does not</em> accept 3056 * HTML-like formatting, which you can do with text strings in XML resource files. 3057 * To style your strings, attach android.text.style.* objects to a 3058 * {@link android.text.SpannableString SpannableString}, or see the 3059 * <a href="{@docRoot}guide/topics/resources/available-resources.html#stringresources"> 3060 * Available Resource Types</a> documentation for an example of setting 3061 * formatted text in the XML resource file. 3062 * 3063 * @attr ref android.R.styleable#TextView_text 3064 */ 3065 @android.view.RemotableViewMethod 3066 public final void setText(CharSequence text) { 3067 setText(text, mBufferType); 3068 } 3069 3070 /** 3071 * Like {@link #setText(CharSequence)}, 3072 * except that the cursor position (if any) is retained in the new text. 3073 * 3074 * @param text The new text to place in the text view. 3075 * 3076 * @see #setText(CharSequence) 3077 */ 3078 @android.view.RemotableViewMethod 3079 public final void setTextKeepState(CharSequence text) { 3080 setTextKeepState(text, mBufferType); 3081 } 3082 3083 /** 3084 * Sets the text that this TextView is to display (see 3085 * {@link #setText(CharSequence)}) and also sets whether it is stored 3086 * in a styleable/spannable buffer and whether it is editable. 3087 * 3088 * @attr ref android.R.styleable#TextView_text 3089 * @attr ref android.R.styleable#TextView_bufferType 3090 */ 3091 public void setText(CharSequence text, BufferType type) { 3092 setText(text, type, true, 0); 3093 3094 if (mCharWrapper != null) { 3095 mCharWrapper.mChars = null; 3096 } 3097 } 3098 3099 private void setText(CharSequence text, BufferType type, 3100 boolean notifyBefore, int oldlen) { 3101 if (text == null) { 3102 text = ""; 3103 } 3104 3105 if (!mUserSetTextScaleX) mTextPaint.setTextScaleX(1.0f); 3106 3107 if (text instanceof Spanned && 3108 ((Spanned) text).getSpanStart(TextUtils.TruncateAt.MARQUEE) >= 0) { 3109 if (ViewConfiguration.get(mContext).isFadingMarqueeEnabled()) { 3110 setHorizontalFadingEdgeEnabled(true); 3111 mMarqueeFadeMode = MARQUEE_FADE_NORMAL; 3112 } else { 3113 setHorizontalFadingEdgeEnabled(false); 3114 mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS; 3115 } 3116 setEllipsize(TextUtils.TruncateAt.MARQUEE); 3117 } 3118 3119 int n = mFilters.length; 3120 for (int i = 0; i < n; i++) { 3121 CharSequence out = mFilters[i].filter(text, 0, text.length(), 3122 EMPTY_SPANNED, 0, 0); 3123 if (out != null) { 3124 text = out; 3125 } 3126 } 3127 3128 if (notifyBefore) { 3129 if (mText != null) { 3130 oldlen = mText.length(); 3131 sendBeforeTextChanged(mText, 0, oldlen, text.length()); 3132 } else { 3133 sendBeforeTextChanged("", 0, 0, text.length()); 3134 } 3135 } 3136 3137 boolean needEditableForNotification = false; 3138 boolean startSpellCheck = false; 3139 3140 if (mListeners != null && mListeners.size() != 0) { 3141 needEditableForNotification = true; 3142 } 3143 3144 if (type == BufferType.EDITABLE || mInput != null || needEditableForNotification) { 3145 Editable t = mEditableFactory.newEditable(text); 3146 text = t; 3147 setFilters(t, mFilters); 3148 InputMethodManager imm = InputMethodManager.peekInstance(); 3149 if (imm != null) imm.restartInput(this); 3150 startSpellCheck = true; 3151 } else if (type == BufferType.SPANNABLE || mMovement != null) { 3152 text = mSpannableFactory.newSpannable(text); 3153 } else if (!(text instanceof CharWrapper)) { 3154 text = TextUtils.stringOrSpannedString(text); 3155 } 3156 3157 if (mAutoLinkMask != 0) { 3158 Spannable s2; 3159 3160 if (type == BufferType.EDITABLE || text instanceof Spannable) { 3161 s2 = (Spannable) text; 3162 } else { 3163 s2 = mSpannableFactory.newSpannable(text); 3164 } 3165 3166 if (Linkify.addLinks(s2, mAutoLinkMask)) { 3167 text = s2; 3168 type = (type == BufferType.EDITABLE) ? BufferType.EDITABLE : BufferType.SPANNABLE; 3169 3170 /* 3171 * We must go ahead and set the text before changing the 3172 * movement method, because setMovementMethod() may call 3173 * setText() again to try to upgrade the buffer type. 3174 */ 3175 mText = text; 3176 3177 // Do not change the movement method for text that support text selection as it 3178 // would prevent an arbitrary cursor displacement. 3179 if (mLinksClickable && !textCanBeSelected()) { 3180 setMovementMethod(LinkMovementMethod.getInstance()); 3181 } 3182 } 3183 } 3184 3185 mBufferType = type; 3186 mText = text; 3187 3188 if (mTransformation == null) { 3189 mTransformed = text; 3190 } else { 3191 mTransformed = mTransformation.getTransformation(text, this); 3192 } 3193 3194 final int textLength = text.length(); 3195 3196 if (text instanceof Spannable && !mAllowTransformationLengthChange) { 3197 Spannable sp = (Spannable) text; 3198 3199 // Remove any ChangeWatchers that might have come 3200 // from other TextViews. 3201 final ChangeWatcher[] watchers = sp.getSpans(0, sp.length(), ChangeWatcher.class); 3202 final int count = watchers.length; 3203 for (int i = 0; i < count; i++) 3204 sp.removeSpan(watchers[i]); 3205 3206 if (mChangeWatcher == null) 3207 mChangeWatcher = new ChangeWatcher(); 3208 3209 sp.setSpan(mChangeWatcher, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE | 3210 (PRIORITY << Spanned.SPAN_PRIORITY_SHIFT)); 3211 3212 if (mInput != null) { 3213 sp.setSpan(mInput, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE); 3214 } 3215 3216 if (mTransformation != null) { 3217 sp.setSpan(mTransformation, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE); 3218 } 3219 3220 if (mMovement != null) { 3221 mMovement.initialize(this, (Spannable) text); 3222 3223 /* 3224 * Initializing the movement method will have set the 3225 * selection, so reset mSelectionMoved to keep that from 3226 * interfering with the normal on-focus selection-setting. 3227 */ 3228 mSelectionMoved = false; 3229 } 3230 } 3231 3232 if (mLayout != null) { 3233 checkForRelayout(); 3234 } 3235 3236 sendOnTextChanged(text, 0, oldlen, textLength); 3237 onTextChanged(text, 0, oldlen, textLength); 3238 3239 if (startSpellCheck) { 3240 updateSpellCheckSpans(0, textLength); 3241 } 3242 3243 if (needEditableForNotification) { 3244 sendAfterTextChanged((Editable) text); 3245 } 3246 3247 // SelectionModifierCursorController depends on textCanBeSelected, which depends on text 3248 prepareCursorControllers(); 3249 } 3250 3251 /** 3252 * Sets the TextView to display the specified slice of the specified 3253 * char array. You must promise that you will not change the contents 3254 * of the array except for right before another call to setText(), 3255 * since the TextView has no way to know that the text 3256 * has changed and that it needs to invalidate and re-layout. 3257 */ 3258 public final void setText(char[] text, int start, int len) { 3259 int oldlen = 0; 3260 3261 if (start < 0 || len < 0 || start + len > text.length) { 3262 throw new IndexOutOfBoundsException(start + ", " + len); 3263 } 3264 3265 /* 3266 * We must do the before-notification here ourselves because if 3267 * the old text is a CharWrapper we destroy it before calling 3268 * into the normal path. 3269 */ 3270 if (mText != null) { 3271 oldlen = mText.length(); 3272 sendBeforeTextChanged(mText, 0, oldlen, len); 3273 } else { 3274 sendBeforeTextChanged("", 0, 0, len); 3275 } 3276 3277 if (mCharWrapper == null) { 3278 mCharWrapper = new CharWrapper(text, start, len); 3279 } else { 3280 mCharWrapper.set(text, start, len); 3281 } 3282 3283 setText(mCharWrapper, mBufferType, false, oldlen); 3284 } 3285 3286 private static class CharWrapper implements CharSequence, GetChars, GraphicsOperations { 3287 private char[] mChars; 3288 private int mStart, mLength; 3289 3290 public CharWrapper(char[] chars, int start, int len) { 3291 mChars = chars; 3292 mStart = start; 3293 mLength = len; 3294 } 3295 3296 /* package */ void set(char[] chars, int start, int len) { 3297 mChars = chars; 3298 mStart = start; 3299 mLength = len; 3300 } 3301 3302 public int length() { 3303 return mLength; 3304 } 3305 3306 public char charAt(int off) { 3307 return mChars[off + mStart]; 3308 } 3309 3310 @Override 3311 public String toString() { 3312 return new String(mChars, mStart, mLength); 3313 } 3314 3315 public CharSequence subSequence(int start, int end) { 3316 if (start < 0 || end < 0 || start > mLength || end > mLength) { 3317 throw new IndexOutOfBoundsException(start + ", " + end); 3318 } 3319 3320 return new String(mChars, start + mStart, end - start); 3321 } 3322 3323 public void getChars(int start, int end, char[] buf, int off) { 3324 if (start < 0 || end < 0 || start > mLength || end > mLength) { 3325 throw new IndexOutOfBoundsException(start + ", " + end); 3326 } 3327 3328 System.arraycopy(mChars, start + mStart, buf, off, end - start); 3329 } 3330 3331 public void drawText(Canvas c, int start, int end, 3332 float x, float y, Paint p) { 3333 c.drawText(mChars, start + mStart, end - start, x, y, p); 3334 } 3335 3336 public void drawTextRun(Canvas c, int start, int end, 3337 int contextStart, int contextEnd, float x, float y, int flags, Paint p) { 3338 int count = end - start; 3339 int contextCount = contextEnd - contextStart; 3340 c.drawTextRun(mChars, start + mStart, count, contextStart + mStart, 3341 contextCount, x, y, flags, p); 3342 } 3343 3344 public float measureText(int start, int end, Paint p) { 3345 return p.measureText(mChars, start + mStart, end - start); 3346 } 3347 3348 public int getTextWidths(int start, int end, float[] widths, Paint p) { 3349 return p.getTextWidths(mChars, start + mStart, end - start, widths); 3350 } 3351 3352 public float getTextRunAdvances(int start, int end, int contextStart, 3353 int contextEnd, int flags, float[] advances, int advancesIndex, 3354 Paint p) { 3355 int count = end - start; 3356 int contextCount = contextEnd - contextStart; 3357 return p.getTextRunAdvances(mChars, start + mStart, count, 3358 contextStart + mStart, contextCount, flags, advances, 3359 advancesIndex); 3360 } 3361 3362 public float getTextRunAdvances(int start, int end, int contextStart, 3363 int contextEnd, int flags, float[] advances, int advancesIndex, 3364 Paint p, int reserved) { 3365 int count = end - start; 3366 int contextCount = contextEnd - contextStart; 3367 return p.getTextRunAdvances(mChars, start + mStart, count, 3368 contextStart + mStart, contextCount, flags, advances, 3369 advancesIndex, reserved); 3370 } 3371 3372 public int getTextRunCursor(int contextStart, int contextEnd, int flags, 3373 int offset, int cursorOpt, Paint p) { 3374 int contextCount = contextEnd - contextStart; 3375 return p.getTextRunCursor(mChars, contextStart + mStart, 3376 contextCount, flags, offset + mStart, cursorOpt); 3377 } 3378 } 3379 3380 /** 3381 * Like {@link #setText(CharSequence, android.widget.TextView.BufferType)}, 3382 * except that the cursor position (if any) is retained in the new text. 3383 * 3384 * @see #setText(CharSequence, android.widget.TextView.BufferType) 3385 */ 3386 public final void setTextKeepState(CharSequence text, BufferType type) { 3387 int start = getSelectionStart(); 3388 int end = getSelectionEnd(); 3389 int len = text.length(); 3390 3391 setText(text, type); 3392 3393 if (start >= 0 || end >= 0) { 3394 if (mText instanceof Spannable) { 3395 Selection.setSelection((Spannable) mText, 3396 Math.max(0, Math.min(start, len)), 3397 Math.max(0, Math.min(end, len))); 3398 } 3399 } 3400 } 3401 3402 @android.view.RemotableViewMethod 3403 public final void setText(int resid) { 3404 setText(getContext().getResources().getText(resid)); 3405 } 3406 3407 public final void setText(int resid, BufferType type) { 3408 setText(getContext().getResources().getText(resid), type); 3409 } 3410 3411 /** 3412 * Sets the text to be displayed when the text of the TextView is empty. 3413 * Null means to use the normal empty text. The hint does not currently 3414 * participate in determining the size of the view. 3415 * 3416 * @attr ref android.R.styleable#TextView_hint 3417 */ 3418 @android.view.RemotableViewMethod 3419 public final void setHint(CharSequence hint) { 3420 mHint = TextUtils.stringOrSpannedString(hint); 3421 3422 if (mLayout != null) { 3423 checkForRelayout(); 3424 } 3425 3426 if (mText.length() == 0) { 3427 invalidate(); 3428 } 3429 } 3430 3431 /** 3432 * Sets the text to be displayed when the text of the TextView is empty, 3433 * from a resource. 3434 * 3435 * @attr ref android.R.styleable#TextView_hint 3436 */ 3437 @android.view.RemotableViewMethod 3438 public final void setHint(int resid) { 3439 setHint(getContext().getResources().getText(resid)); 3440 } 3441 3442 /** 3443 * Returns the hint that is displayed when the text of the TextView 3444 * is empty. 3445 * 3446 * @attr ref android.R.styleable#TextView_hint 3447 */ 3448 @ViewDebug.CapturedViewProperty 3449 public CharSequence getHint() { 3450 return mHint; 3451 } 3452 3453 private static boolean isMultilineInputType(int type) { 3454 return (type & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE)) == 3455 (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE); 3456 } 3457 3458 /** 3459 * Set the type of the content with a constant as defined for {@link EditorInfo#inputType}. This 3460 * will take care of changing the key listener, by calling {@link #setKeyListener(KeyListener)}, 3461 * to match the given content type. If the given content type is {@link EditorInfo#TYPE_NULL} 3462 * then a soft keyboard will not be displayed for this text view. 3463 * 3464 * Note that the maximum number of displayed lines (see {@link #setMaxLines(int)}) will be 3465 * modified if you change the {@link EditorInfo#TYPE_TEXT_FLAG_MULTI_LINE} flag of the input 3466 * type. 3467 * 3468 * @see #getInputType() 3469 * @see #setRawInputType(int) 3470 * @see android.text.InputType 3471 * @attr ref android.R.styleable#TextView_inputType 3472 */ 3473 public void setInputType(int type) { 3474 final boolean wasPassword = isPasswordInputType(mInputType); 3475 final boolean wasVisiblePassword = isVisiblePasswordInputType(mInputType); 3476 setInputType(type, false); 3477 final boolean isPassword = isPasswordInputType(type); 3478 final boolean isVisiblePassword = isVisiblePasswordInputType(type); 3479 boolean forceUpdate = false; 3480 if (isPassword) { 3481 setTransformationMethod(PasswordTransformationMethod.getInstance()); 3482 setTypefaceByIndex(MONOSPACE, 0); 3483 } else if (isVisiblePassword) { 3484 if (mTransformation == PasswordTransformationMethod.getInstance()) { 3485 forceUpdate = true; 3486 } 3487 setTypefaceByIndex(MONOSPACE, 0); 3488 } else if (wasPassword || wasVisiblePassword) { 3489 // not in password mode, clean up typeface and transformation 3490 setTypefaceByIndex(-1, -1); 3491 if (mTransformation == PasswordTransformationMethod.getInstance()) { 3492 forceUpdate = true; 3493 } 3494 } 3495 3496 boolean singleLine = !isMultilineInputType(type); 3497 3498 // We need to update the single line mode if it has changed or we 3499 // were previously in password mode. 3500 if (mSingleLine != singleLine || forceUpdate) { 3501 // Change single line mode, but only change the transformation if 3502 // we are not in password mode. 3503 applySingleLine(singleLine, !isPassword, true); 3504 } 3505 3506 InputMethodManager imm = InputMethodManager.peekInstance(); 3507 if (imm != null) imm.restartInput(this); 3508 } 3509 3510 /** 3511 * It would be better to rely on the input type for everything. A password inputType should have 3512 * a password transformation. We should hence use isPasswordInputType instead of this method. 3513 * 3514 * We should: 3515 * - Call setInputType in setKeyListener instead of changing the input type directly (which 3516 * would install the correct transformation). 3517 * - Refuse the installation of a non-password transformation in setTransformation if the input 3518 * type is password. 3519 * 3520 * However, this is like this for legacy reasons and we cannot break existing apps. This method 3521 * is useful since it matches what the user can see (obfuscated text or not). 3522 * 3523 * @return true if the current transformation method is of the password type. 3524 */ 3525 private boolean hasPasswordTransformationMethod() { 3526 return mTransformation instanceof PasswordTransformationMethod; 3527 } 3528 3529 private static boolean isPasswordInputType(int inputType) { 3530 final int variation = 3531 inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION); 3532 return variation 3533 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD) 3534 || variation 3535 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD) 3536 || variation 3537 == (EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD); 3538 } 3539 3540 private static boolean isVisiblePasswordInputType(int inputType) { 3541 final int variation = 3542 inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION); 3543 return variation 3544 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); 3545 } 3546 3547 /** 3548 * Directly change the content type integer of the text view, without 3549 * modifying any other state. 3550 * @see #setInputType(int) 3551 * @see android.text.InputType 3552 * @attr ref android.R.styleable#TextView_inputType 3553 */ 3554 public void setRawInputType(int type) { 3555 mInputType = type; 3556 } 3557 3558 private void setInputType(int type, boolean direct) { 3559 final int cls = type & EditorInfo.TYPE_MASK_CLASS; 3560 KeyListener input; 3561 if (cls == EditorInfo.TYPE_CLASS_TEXT) { 3562 boolean autotext = (type & EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT) != 0; 3563 TextKeyListener.Capitalize cap; 3564 if ((type & EditorInfo.TYPE_TEXT_FLAG_CAP_CHARACTERS) != 0) { 3565 cap = TextKeyListener.Capitalize.CHARACTERS; 3566 } else if ((type & EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS) != 0) { 3567 cap = TextKeyListener.Capitalize.WORDS; 3568 } else if ((type & EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES) != 0) { 3569 cap = TextKeyListener.Capitalize.SENTENCES; 3570 } else { 3571 cap = TextKeyListener.Capitalize.NONE; 3572 } 3573 input = TextKeyListener.getInstance(autotext, cap); 3574 } else if (cls == EditorInfo.TYPE_CLASS_NUMBER) { 3575 input = DigitsKeyListener.getInstance( 3576 (type & EditorInfo.TYPE_NUMBER_FLAG_SIGNED) != 0, 3577 (type & EditorInfo.TYPE_NUMBER_FLAG_DECIMAL) != 0); 3578 } else if (cls == EditorInfo.TYPE_CLASS_DATETIME) { 3579 switch (type & EditorInfo.TYPE_MASK_VARIATION) { 3580 case EditorInfo.TYPE_DATETIME_VARIATION_DATE: 3581 input = DateKeyListener.getInstance(); 3582 break; 3583 case EditorInfo.TYPE_DATETIME_VARIATION_TIME: 3584 input = TimeKeyListener.getInstance(); 3585 break; 3586 default: 3587 input = DateTimeKeyListener.getInstance(); 3588 break; 3589 } 3590 } else if (cls == EditorInfo.TYPE_CLASS_PHONE) { 3591 input = DialerKeyListener.getInstance(); 3592 } else { 3593 input = TextKeyListener.getInstance(); 3594 } 3595 setRawInputType(type); 3596 if (direct) mInput = input; 3597 else { 3598 setKeyListenerOnly(input); 3599 } 3600 } 3601 3602 /** 3603 * Get the type of the content. 3604 * 3605 * @see #setInputType(int) 3606 * @see android.text.InputType 3607 */ 3608 public int getInputType() { 3609 return mInputType; 3610 } 3611 3612 /** 3613 * Change the editor type integer associated with the text view, which 3614 * will be reported to an IME with {@link EditorInfo#imeOptions} when it 3615 * has focus. 3616 * @see #getImeOptions 3617 * @see android.view.inputmethod.EditorInfo 3618 * @attr ref android.R.styleable#TextView_imeOptions 3619 */ 3620 public void setImeOptions(int imeOptions) { 3621 if (mInputContentType == null) { 3622 mInputContentType = new InputContentType(); 3623 } 3624 mInputContentType.imeOptions = imeOptions; 3625 } 3626 3627 /** 3628 * Get the type of the IME editor. 3629 * 3630 * @see #setImeOptions(int) 3631 * @see android.view.inputmethod.EditorInfo 3632 */ 3633 public int getImeOptions() { 3634 return mInputContentType != null 3635 ? mInputContentType.imeOptions : EditorInfo.IME_NULL; 3636 } 3637 3638 /** 3639 * Change the custom IME action associated with the text view, which 3640 * will be reported to an IME with {@link EditorInfo#actionLabel} 3641 * and {@link EditorInfo#actionId} when it has focus. 3642 * @see #getImeActionLabel 3643 * @see #getImeActionId 3644 * @see android.view.inputmethod.EditorInfo 3645 * @attr ref android.R.styleable#TextView_imeActionLabel 3646 * @attr ref android.R.styleable#TextView_imeActionId 3647 */ 3648 public void setImeActionLabel(CharSequence label, int actionId) { 3649 if (mInputContentType == null) { 3650 mInputContentType = new InputContentType(); 3651 } 3652 mInputContentType.imeActionLabel = label; 3653 mInputContentType.imeActionId = actionId; 3654 } 3655 3656 /** 3657 * Get the IME action label previous set with {@link #setImeActionLabel}. 3658 * 3659 * @see #setImeActionLabel 3660 * @see android.view.inputmethod.EditorInfo 3661 */ 3662 public CharSequence getImeActionLabel() { 3663 return mInputContentType != null 3664 ? mInputContentType.imeActionLabel : null; 3665 } 3666 3667 /** 3668 * Get the IME action ID previous set with {@link #setImeActionLabel}. 3669 * 3670 * @see #setImeActionLabel 3671 * @see android.view.inputmethod.EditorInfo 3672 */ 3673 public int getImeActionId() { 3674 return mInputContentType != null 3675 ? mInputContentType.imeActionId : 0; 3676 } 3677 3678 /** 3679 * Set a special listener to be called when an action is performed 3680 * on the text view. This will be called when the enter key is pressed, 3681 * or when an action supplied to the IME is selected by the user. Setting 3682 * this means that the normal hard key event will not insert a newline 3683 * into the text view, even if it is multi-line; holding down the ALT 3684 * modifier will, however, allow the user to insert a newline character. 3685 */ 3686 public void setOnEditorActionListener(OnEditorActionListener l) { 3687 if (mInputContentType == null) { 3688 mInputContentType = new InputContentType(); 3689 } 3690 mInputContentType.onEditorActionListener = l; 3691 } 3692 3693 /** 3694 * Called when an attached input method calls 3695 * {@link InputConnection#performEditorAction(int) 3696 * InputConnection.performEditorAction()} 3697 * for this text view. The default implementation will call your action 3698 * listener supplied to {@link #setOnEditorActionListener}, or perform 3699 * a standard operation for {@link EditorInfo#IME_ACTION_NEXT 3700 * EditorInfo.IME_ACTION_NEXT}, {@link EditorInfo#IME_ACTION_PREVIOUS 3701 * EditorInfo.IME_ACTION_PREVIOUS}, or {@link EditorInfo#IME_ACTION_DONE 3702 * EditorInfo.IME_ACTION_DONE}. 3703 * 3704 * <p>For backwards compatibility, if no IME options have been set and the 3705 * text view would not normally advance focus on enter, then 3706 * the NEXT and DONE actions received here will be turned into an enter 3707 * key down/up pair to go through the normal key handling. 3708 * 3709 * @param actionCode The code of the action being performed. 3710 * 3711 * @see #setOnEditorActionListener 3712 */ 3713 public void onEditorAction(int actionCode) { 3714 final InputContentType ict = mInputContentType; 3715 if (ict != null) { 3716 if (ict.onEditorActionListener != null) { 3717 if (ict.onEditorActionListener.onEditorAction(this, 3718 actionCode, null)) { 3719 return; 3720 } 3721 } 3722 3723 // This is the handling for some default action. 3724 // Note that for backwards compatibility we don't do this 3725 // default handling if explicit ime options have not been given, 3726 // instead turning this into the normal enter key codes that an 3727 // app may be expecting. 3728 if (actionCode == EditorInfo.IME_ACTION_NEXT) { 3729 View v = focusSearch(FOCUS_DOWN); 3730 if (v != null) { 3731 if (!v.requestFocus(FOCUS_DOWN)) { 3732 throw new IllegalStateException("focus search returned a view " + 3733 "that wasn't able to take focus!"); 3734 } 3735 } 3736 return; 3737 3738 } else if (actionCode == EditorInfo.IME_ACTION_PREVIOUS) { 3739 View v = focusSearch(FOCUS_UP); 3740 if (v != null) { 3741 if (!v.requestFocus(FOCUS_UP)) { 3742 throw new IllegalStateException("focus search returned a view " + 3743 "that wasn't able to take focus!"); 3744 } 3745 } 3746 return; 3747 3748 } else if (actionCode == EditorInfo.IME_ACTION_DONE) { 3749 InputMethodManager imm = InputMethodManager.peekInstance(); 3750 if (imm != null && imm.isActive(this)) { 3751 imm.hideSoftInputFromWindow(getWindowToken(), 0); 3752 } 3753 return; 3754 } 3755 } 3756 3757 Handler h = getHandler(); 3758 if (h != null) { 3759 long eventTime = SystemClock.uptimeMillis(); 3760 h.sendMessage(h.obtainMessage(ViewRootImpl.DISPATCH_KEY_FROM_IME, 3761 new KeyEvent(eventTime, eventTime, 3762 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER, 0, 0, 3763 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 3764 KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE 3765 | KeyEvent.FLAG_EDITOR_ACTION))); 3766 h.sendMessage(h.obtainMessage(ViewRootImpl.DISPATCH_KEY_FROM_IME, 3767 new KeyEvent(SystemClock.uptimeMillis(), eventTime, 3768 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER, 0, 0, 3769 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 3770 KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE 3771 | KeyEvent.FLAG_EDITOR_ACTION))); 3772 } 3773 } 3774 3775 /** 3776 * Set the private content type of the text, which is the 3777 * {@link EditorInfo#privateImeOptions EditorInfo.privateImeOptions} 3778 * field that will be filled in when creating an input connection. 3779 * 3780 * @see #getPrivateImeOptions() 3781 * @see EditorInfo#privateImeOptions 3782 * @attr ref android.R.styleable#TextView_privateImeOptions 3783 */ 3784 public void setPrivateImeOptions(String type) { 3785 if (mInputContentType == null) mInputContentType = new InputContentType(); 3786 mInputContentType.privateImeOptions = type; 3787 } 3788 3789 /** 3790 * Get the private type of the content. 3791 * 3792 * @see #setPrivateImeOptions(String) 3793 * @see EditorInfo#privateImeOptions 3794 */ 3795 public String getPrivateImeOptions() { 3796 return mInputContentType != null 3797 ? mInputContentType.privateImeOptions : null; 3798 } 3799 3800 /** 3801 * Set the extra input data of the text, which is the 3802 * {@link EditorInfo#extras TextBoxAttribute.extras} 3803 * Bundle that will be filled in when creating an input connection. The 3804 * given integer is the resource ID of an XML resource holding an 3805 * {@link android.R.styleable#InputExtras <input-extras>} XML tree. 3806 * 3807 * @see #getInputExtras(boolean) 3808 * @see EditorInfo#extras 3809 * @attr ref android.R.styleable#TextView_editorExtras 3810 */ 3811 public void setInputExtras(int xmlResId) 3812 throws XmlPullParserException, IOException { 3813 XmlResourceParser parser = getResources().getXml(xmlResId); 3814 if (mInputContentType == null) mInputContentType = new InputContentType(); 3815 mInputContentType.extras = new Bundle(); 3816 getResources().parseBundleExtras(parser, mInputContentType.extras); 3817 } 3818 3819 /** 3820 * Retrieve the input extras currently associated with the text view, which 3821 * can be viewed as well as modified. 3822 * 3823 * @param create If true, the extras will be created if they don't already 3824 * exist. Otherwise, null will be returned if none have been created. 3825 * @see #setInputExtras(int) 3826 * @see EditorInfo#extras 3827 * @attr ref android.R.styleable#TextView_editorExtras 3828 */ 3829 public Bundle getInputExtras(boolean create) { 3830 if (mInputContentType == null) { 3831 if (!create) return null; 3832 mInputContentType = new InputContentType(); 3833 } 3834 if (mInputContentType.extras == null) { 3835 if (!create) return null; 3836 mInputContentType.extras = new Bundle(); 3837 } 3838 return mInputContentType.extras; 3839 } 3840 3841 /** 3842 * Returns the error message that was set to be displayed with 3843 * {@link #setError}, or <code>null</code> if no error was set 3844 * or if it the error was cleared by the widget after user input. 3845 */ 3846 public CharSequence getError() { 3847 return mError; 3848 } 3849 3850 /** 3851 * Sets the right-hand compound drawable of the TextView to the "error" 3852 * icon and sets an error message that will be displayed in a popup when 3853 * the TextView has focus. The icon and error message will be reset to 3854 * null when any key events cause changes to the TextView's text. If the 3855 * <code>error</code> is <code>null</code>, the error message and icon 3856 * will be cleared. 3857 */ 3858 @android.view.RemotableViewMethod 3859 public void setError(CharSequence error) { 3860 if (error == null) { 3861 setError(null, null); 3862 } else { 3863 Drawable dr = getContext().getResources(). 3864 getDrawable(com.android.internal.R.drawable.indicator_input_error); 3865 3866 dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight()); 3867 setError(error, dr); 3868 } 3869 } 3870 3871 /** 3872 * Sets the right-hand compound drawable of the TextView to the specified 3873 * icon and sets an error message that will be displayed in a popup when 3874 * the TextView has focus. The icon and error message will be reset to 3875 * null when any key events cause changes to the TextView's text. The 3876 * drawable must already have had {@link Drawable#setBounds} set on it. 3877 * If the <code>error</code> is <code>null</code>, the error message will 3878 * be cleared (and you should provide a <code>null</code> icon as well). 3879 */ 3880 public void setError(CharSequence error, Drawable icon) { 3881 error = TextUtils.stringOrSpannedString(error); 3882 3883 mError = error; 3884 mErrorWasChanged = true; 3885 final Drawables dr = mDrawables; 3886 if (dr != null) { 3887 switch (getResolvedLayoutDirection()) { 3888 default: 3889 case LAYOUT_DIRECTION_LTR: 3890 setCompoundDrawables(dr.mDrawableLeft, dr.mDrawableTop, icon, 3891 dr.mDrawableBottom); 3892 break; 3893 case LAYOUT_DIRECTION_RTL: 3894 setCompoundDrawables(icon, dr.mDrawableTop, dr.mDrawableRight, 3895 dr.mDrawableBottom); 3896 break; 3897 } 3898 } else { 3899 setCompoundDrawables(null, null, icon, null); 3900 } 3901 3902 if (error == null) { 3903 if (mPopup != null) { 3904 if (mPopup.isShowing()) { 3905 mPopup.dismiss(); 3906 } 3907 3908 mPopup = null; 3909 } 3910 } else { 3911 if (isFocused()) { 3912 showError(); 3913 } 3914 } 3915 } 3916 3917 private void showError() { 3918 if (getWindowToken() == null) { 3919 mShowErrorAfterAttach = true; 3920 return; 3921 } 3922 3923 if (mPopup == null) { 3924 LayoutInflater inflater = LayoutInflater.from(getContext()); 3925 final TextView err = (TextView) inflater.inflate( 3926 com.android.internal.R.layout.textview_hint, null); 3927 3928 final float scale = getResources().getDisplayMetrics().density; 3929 mPopup = new ErrorPopup(err, (int) (200 * scale + 0.5f), (int) (50 * scale + 0.5f)); 3930 mPopup.setFocusable(false); 3931 // The user is entering text, so the input method is needed. We 3932 // don't want the popup to be displayed on top of it. 3933 mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); 3934 } 3935 3936 TextView tv = (TextView) mPopup.getContentView(); 3937 chooseSize(mPopup, mError, tv); 3938 tv.setText(mError); 3939 3940 mPopup.showAsDropDown(this, getErrorX(), getErrorY()); 3941 mPopup.fixDirection(mPopup.isAboveAnchor()); 3942 } 3943 3944 private static class ErrorPopup extends PopupWindow { 3945 private boolean mAbove = false; 3946 private final TextView mView; 3947 private int mPopupInlineErrorBackgroundId = 0; 3948 private int mPopupInlineErrorAboveBackgroundId = 0; 3949 3950 ErrorPopup(TextView v, int width, int height) { 3951 super(v, width, height); 3952 mView = v; 3953 // Make sure the TextView has a background set as it will be used the first time it is 3954 // shown and positionned. Initialized with below background, which should have 3955 // dimensions identical to the above version for this to work (and is more likely). 3956 mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId, 3957 com.android.internal.R.styleable.Theme_errorMessageBackground); 3958 mView.setBackgroundResource(mPopupInlineErrorBackgroundId); 3959 } 3960 3961 void fixDirection(boolean above) { 3962 mAbove = above; 3963 3964 if (above) { 3965 mPopupInlineErrorAboveBackgroundId = 3966 getResourceId(mPopupInlineErrorAboveBackgroundId, 3967 com.android.internal.R.styleable.Theme_errorMessageAboveBackground); 3968 } else { 3969 mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId, 3970 com.android.internal.R.styleable.Theme_errorMessageBackground); 3971 } 3972 3973 mView.setBackgroundResource(above ? mPopupInlineErrorAboveBackgroundId : 3974 mPopupInlineErrorBackgroundId); 3975 } 3976 3977 private int getResourceId(int currentId, int index) { 3978 if (currentId == 0) { 3979 TypedArray styledAttributes = mView.getContext().obtainStyledAttributes( 3980 R.styleable.Theme); 3981 currentId = styledAttributes.getResourceId(index, 0); 3982 styledAttributes.recycle(); 3983 } 3984 return currentId; 3985 } 3986 3987 @Override 3988 public void update(int x, int y, int w, int h, boolean force) { 3989 super.update(x, y, w, h, force); 3990 3991 boolean above = isAboveAnchor(); 3992 if (above != mAbove) { 3993 fixDirection(above); 3994 } 3995 } 3996 } 3997 3998 /** 3999 * Returns the Y offset to make the pointy top of the error point 4000 * at the middle of the error icon. 4001 */ 4002 private int getErrorX() { 4003 /* 4004 * The "25" is the distance between the point and the right edge 4005 * of the background 4006 */ 4007 final float scale = getResources().getDisplayMetrics().density; 4008 4009 final Drawables dr = mDrawables; 4010 return getWidth() - mPopup.getWidth() - getPaddingRight() - 4011 (dr != null ? dr.mDrawableSizeRight : 0) / 2 + (int) (25 * scale + 0.5f); 4012 } 4013 4014 /** 4015 * Returns the Y offset to make the pointy top of the error point 4016 * at the bottom of the error icon. 4017 */ 4018 private int getErrorY() { 4019 /* 4020 * Compound, not extended, because the icon is not clipped 4021 * if the text height is smaller. 4022 */ 4023 final int compoundPaddingTop = getCompoundPaddingTop(); 4024 int vspace = mBottom - mTop - getCompoundPaddingBottom() - compoundPaddingTop; 4025 4026 final Drawables dr = mDrawables; 4027 int icontop = compoundPaddingTop + 4028 (vspace - (dr != null ? dr.mDrawableHeightRight : 0)) / 2; 4029 4030 /* 4031 * The "2" is the distance between the point and the top edge 4032 * of the background. 4033 */ 4034 final float scale = getResources().getDisplayMetrics().density; 4035 return icontop + (dr != null ? dr.mDrawableHeightRight : 0) - getHeight() - 4036 (int) (2 * scale + 0.5f); 4037 } 4038 4039 private void hideError() { 4040 if (mPopup != null) { 4041 if (mPopup.isShowing()) { 4042 mPopup.dismiss(); 4043 } 4044 } 4045 4046 mShowErrorAfterAttach = false; 4047 } 4048 4049 private void chooseSize(PopupWindow pop, CharSequence text, TextView tv) { 4050 int wid = tv.getPaddingLeft() + tv.getPaddingRight(); 4051 int ht = tv.getPaddingTop() + tv.getPaddingBottom(); 4052 4053 /* 4054 * Figure out how big the text would be if we laid it out to the 4055 * full width of this view minus the border. 4056 */ 4057 int cap = getWidth() - wid; 4058 if (cap < 0) { 4059 cap = 200; // We must not be measured yet -- setFrame() will fix it. 4060 } 4061 4062 Layout l = new StaticLayout(text, tv.getPaint(), cap, 4063 Layout.Alignment.ALIGN_NORMAL, 1, 0, true); 4064 float max = 0; 4065 for (int i = 0; i < l.getLineCount(); i++) { 4066 max = Math.max(max, l.getLineWidth(i)); 4067 } 4068 4069 /* 4070 * Now set the popup size to be big enough for the text plus the border. 4071 */ 4072 pop.setWidth(wid + (int) Math.ceil(max)); 4073 pop.setHeight(ht + l.getHeight()); 4074 } 4075 4076 4077 @Override 4078 protected boolean setFrame(int l, int t, int r, int b) { 4079 boolean result = super.setFrame(l, t, r, b); 4080 4081 if (mPopup != null) { 4082 TextView tv = (TextView) mPopup.getContentView(); 4083 chooseSize(mPopup, mError, tv); 4084 mPopup.update(this, getErrorX(), getErrorY(), 4085 mPopup.getWidth(), mPopup.getHeight()); 4086 } 4087 4088 restartMarqueeIfNeeded(); 4089 4090 return result; 4091 } 4092 4093 private void restartMarqueeIfNeeded() { 4094 if (mRestartMarquee && mEllipsize == TextUtils.TruncateAt.MARQUEE) { 4095 mRestartMarquee = false; 4096 startMarquee(); 4097 } 4098 } 4099 4100 /** 4101 * Sets the list of input filters that will be used if the buffer is 4102 * Editable. Has no effect otherwise. 4103 * 4104 * @attr ref android.R.styleable#TextView_maxLength 4105 */ 4106 public void setFilters(InputFilter[] filters) { 4107 if (filters == null) { 4108 throw new IllegalArgumentException(); 4109 } 4110 4111 mFilters = filters; 4112 4113 if (mText instanceof Editable) { 4114 setFilters((Editable) mText, filters); 4115 } 4116 } 4117 4118 /** 4119 * Sets the list of input filters on the specified Editable, 4120 * and includes mInput in the list if it is an InputFilter. 4121 */ 4122 private void setFilters(Editable e, InputFilter[] filters) { 4123 if (mInput instanceof InputFilter) { 4124 InputFilter[] nf = new InputFilter[filters.length + 1]; 4125 4126 System.arraycopy(filters, 0, nf, 0, filters.length); 4127 nf[filters.length] = (InputFilter) mInput; 4128 4129 e.setFilters(nf); 4130 } else { 4131 e.setFilters(filters); 4132 } 4133 } 4134 4135 /** 4136 * Returns the current list of input filters. 4137 */ 4138 public InputFilter[] getFilters() { 4139 return mFilters; 4140 } 4141 4142 ///////////////////////////////////////////////////////////////////////// 4143 4144 private int getVerticalOffset(boolean forceNormal) { 4145 int voffset = 0; 4146 final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; 4147 4148 Layout l = mLayout; 4149 if (!forceNormal && mText.length() == 0 && mHintLayout != null) { 4150 l = mHintLayout; 4151 } 4152 4153 if (gravity != Gravity.TOP) { 4154 int boxht; 4155 4156 if (l == mHintLayout) { 4157 boxht = getMeasuredHeight() - getCompoundPaddingTop() - 4158 getCompoundPaddingBottom(); 4159 } else { 4160 boxht = getMeasuredHeight() - getExtendedPaddingTop() - 4161 getExtendedPaddingBottom(); 4162 } 4163 int textht = l.getHeight(); 4164 4165 if (textht < boxht) { 4166 if (gravity == Gravity.BOTTOM) 4167 voffset = boxht - textht; 4168 else // (gravity == Gravity.CENTER_VERTICAL) 4169 voffset = (boxht - textht) >> 1; 4170 } 4171 } 4172 return voffset; 4173 } 4174 4175 private int getBottomVerticalOffset(boolean forceNormal) { 4176 int voffset = 0; 4177 final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; 4178 4179 Layout l = mLayout; 4180 if (!forceNormal && mText.length() == 0 && mHintLayout != null) { 4181 l = mHintLayout; 4182 } 4183 4184 if (gravity != Gravity.BOTTOM) { 4185 int boxht; 4186 4187 if (l == mHintLayout) { 4188 boxht = getMeasuredHeight() - getCompoundPaddingTop() - 4189 getCompoundPaddingBottom(); 4190 } else { 4191 boxht = getMeasuredHeight() - getExtendedPaddingTop() - 4192 getExtendedPaddingBottom(); 4193 } 4194 int textht = l.getHeight(); 4195 4196 if (textht < boxht) { 4197 if (gravity == Gravity.TOP) 4198 voffset = boxht - textht; 4199 else // (gravity == Gravity.CENTER_VERTICAL) 4200 voffset = (boxht - textht) >> 1; 4201 } 4202 } 4203 return voffset; 4204 } 4205 4206 private void invalidateCursorPath() { 4207 if (mHighlightPathBogus) { 4208 invalidateCursor(); 4209 } else { 4210 final int horizontalPadding = getCompoundPaddingLeft(); 4211 final int verticalPadding = getExtendedPaddingTop() + getVerticalOffset(true); 4212 4213 if (mCursorCount == 0) { 4214 synchronized (sTempRect) { 4215 /* 4216 * The reason for this concern about the thickness of the 4217 * cursor and doing the floor/ceil on the coordinates is that 4218 * some EditTexts (notably textfields in the Browser) have 4219 * anti-aliased text where not all the characters are 4220 * necessarily at integer-multiple locations. This should 4221 * make sure the entire cursor gets invalidated instead of 4222 * sometimes missing half a pixel. 4223 */ 4224 float thick = FloatMath.ceil(mTextPaint.getStrokeWidth()); 4225 if (thick < 1.0f) { 4226 thick = 1.0f; 4227 } 4228 4229 thick /= 2.0f; 4230 4231 mHighlightPath.computeBounds(sTempRect, false); 4232 4233 invalidate((int) FloatMath.floor(horizontalPadding + sTempRect.left - thick), 4234 (int) FloatMath.floor(verticalPadding + sTempRect.top - thick), 4235 (int) FloatMath.ceil(horizontalPadding + sTempRect.right + thick), 4236 (int) FloatMath.ceil(verticalPadding + sTempRect.bottom + thick)); 4237 } 4238 } else { 4239 for (int i = 0; i < mCursorCount; i++) { 4240 Rect bounds = mCursorDrawable[i].getBounds(); 4241 invalidate(bounds.left + horizontalPadding, bounds.top + verticalPadding, 4242 bounds.right + horizontalPadding, bounds.bottom + verticalPadding); 4243 } 4244 } 4245 } 4246 } 4247 4248 private void invalidateCursor() { 4249 int where = getSelectionEnd(); 4250 4251 invalidateCursor(where, where, where); 4252 } 4253 4254 private void invalidateCursor(int a, int b, int c) { 4255 if (mLayout == null) { 4256 invalidate(); 4257 } else { 4258 if (a >= 0 || b >= 0 || c >= 0) { 4259 int first = Math.min(Math.min(a, b), c); 4260 int last = Math.max(Math.max(a, b), c); 4261 4262 int line = mLayout.getLineForOffset(first); 4263 int top = mLayout.getLineTop(line); 4264 4265 // This is ridiculous, but the descent from the line above 4266 // can hang down into the line we really want to redraw, 4267 // so we have to invalidate part of the line above to make 4268 // sure everything that needs to be redrawn really is. 4269 // (But not the whole line above, because that would cause 4270 // the same problem with the descenders on the line above it!) 4271 if (line > 0) { 4272 top -= mLayout.getLineDescent(line - 1); 4273 } 4274 4275 int line2; 4276 4277 if (first == last) 4278 line2 = line; 4279 else 4280 line2 = mLayout.getLineForOffset(last); 4281 4282 int bottom = mLayout.getLineTop(line2 + 1); 4283 4284 final int horizontalPadding = getCompoundPaddingLeft(); 4285 final int verticalPadding = getExtendedPaddingTop() + getVerticalOffset(true); 4286 4287 // If used, the cursor drawables can have an arbitrary dimension that can go beyond 4288 // the invalidated lines specified above. 4289 for (int i = 0; i < mCursorCount; i++) { 4290 Rect bounds = mCursorDrawable[i].getBounds(); 4291 top = Math.min(top, bounds.top); 4292 bottom = Math.max(bottom, bounds.bottom); 4293 // Horizontal bounds are already full width, no need to update 4294 } 4295 4296 invalidate(horizontalPadding + mScrollX, top + verticalPadding, 4297 horizontalPadding + mScrollX + getWidth() - 4298 getCompoundPaddingLeft() - getCompoundPaddingRight(), 4299 bottom + verticalPadding); 4300 } 4301 } 4302 } 4303 4304 private void registerForPreDraw() { 4305 final ViewTreeObserver observer = getViewTreeObserver(); 4306 4307 if (mPreDrawState == PREDRAW_NOT_REGISTERED) { 4308 observer.addOnPreDrawListener(this); 4309 mPreDrawState = PREDRAW_PENDING; 4310 } else if (mPreDrawState == PREDRAW_DONE) { 4311 mPreDrawState = PREDRAW_PENDING; 4312 } 4313 4314 // else state is PREDRAW_PENDING, so keep waiting. 4315 } 4316 4317 /** 4318 * {@inheritDoc} 4319 */ 4320 public boolean onPreDraw() { 4321 if (mPreDrawState != PREDRAW_PENDING) { 4322 return true; 4323 } 4324 4325 if (mLayout == null) { 4326 assumeLayout(); 4327 } 4328 4329 boolean changed = false; 4330 4331 if (mMovement != null) { 4332 /* This code also provides auto-scrolling when a cursor is moved using a 4333 * CursorController (insertion point or selection limits). 4334 * For selection, ensure start or end is visible depending on controller's state. 4335 */ 4336 int curs = getSelectionEnd(); 4337 // Do not create the controller if it is not already created. 4338 if (mSelectionModifierCursorController != null && 4339 mSelectionModifierCursorController.isSelectionStartDragged()) { 4340 curs = getSelectionStart(); 4341 } 4342 4343 /* 4344 * TODO: This should really only keep the end in view if 4345 * it already was before the text changed. I'm not sure 4346 * of a good way to tell from here if it was. 4347 */ 4348 if (curs < 0 && 4349 (mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { 4350 curs = mText.length(); 4351 } 4352 4353 if (curs >= 0) { 4354 changed = bringPointIntoView(curs); 4355 } 4356 } else { 4357 changed = bringTextIntoView(); 4358 } 4359 4360 // This has to be checked here since: 4361 // - onFocusChanged cannot start it when focus is given to a view with selected text (after 4362 // a screen rotation) since layout is not yet initialized at that point. 4363 if (mCreatedWithASelection) { 4364 startSelectionActionMode(); 4365 mCreatedWithASelection = false; 4366 } 4367 4368 // Phone specific code (there is no ExtractEditText on tablets). 4369 // ExtractEditText does not call onFocus when it is displayed, and mHasSelectionOnFocus can 4370 // not be set. Do the test here instead. 4371 if (this instanceof ExtractEditText && hasSelection()) { 4372 startSelectionActionMode(); 4373 } 4374 4375 mPreDrawState = PREDRAW_DONE; 4376 return !changed; 4377 } 4378 4379 @Override 4380 protected void onAttachedToWindow() { 4381 super.onAttachedToWindow(); 4382 4383 mTemporaryDetach = false; 4384 4385 if (mShowErrorAfterAttach) { 4386 showError(); 4387 mShowErrorAfterAttach = false; 4388 } 4389 4390 final ViewTreeObserver observer = getViewTreeObserver(); 4391 // No need to create the controller. 4392 // The get method will add the listener on controller creation. 4393 if (mInsertionPointCursorController != null) { 4394 observer.addOnTouchModeChangeListener(mInsertionPointCursorController); 4395 } 4396 if (mSelectionModifierCursorController != null) { 4397 observer.addOnTouchModeChangeListener(mSelectionModifierCursorController); 4398 } 4399 4400 // Resolve drawables as the layout direction has been resolved 4401 resolveDrawables(); 4402 } 4403 4404 @Override 4405 protected void onDetachedFromWindow() { 4406 super.onDetachedFromWindow(); 4407 4408 final ViewTreeObserver observer = getViewTreeObserver(); 4409 if (mPreDrawState != PREDRAW_NOT_REGISTERED) { 4410 observer.removeOnPreDrawListener(this); 4411 mPreDrawState = PREDRAW_NOT_REGISTERED; 4412 } 4413 4414 if (mError != null) { 4415 hideError(); 4416 } 4417 4418 if (mBlink != null) { 4419 mBlink.removeCallbacks(mBlink); 4420 } 4421 4422 if (mInsertionPointCursorController != null) { 4423 mInsertionPointCursorController.onDetached(); 4424 } 4425 4426 if (mSelectionModifierCursorController != null) { 4427 mSelectionModifierCursorController.onDetached(); 4428 } 4429 4430 hideControllers(); 4431 4432 resetResolvedDrawables(); 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 /** 4547 * @hide 4548 */ 4549 @Override 4550 public int getResolvedLayoutDirection(Drawable who) { 4551 if (who == null) return View.LAYOUT_DIRECTION_LTR; 4552 if (mDrawables != null) { 4553 final Drawables drawables = mDrawables; 4554 if (who == drawables.mDrawableLeft || who == drawables.mDrawableRight || 4555 who == drawables.mDrawableTop || who == drawables.mDrawableBottom || 4556 who == drawables.mDrawableStart || who == drawables.mDrawableEnd) { 4557 return getResolvedLayoutDirection(); 4558 } 4559 } 4560 return super.getResolvedLayoutDirection(who); 4561 } 4562 4563 @Override 4564 protected boolean onSetAlpha(int alpha) { 4565 // Alpha is supported if and only if the drawing can be done in one pass. 4566 // TODO text with spans with a background color currently do not respect this alpha. 4567 if (getBackground() == null) { 4568 mCurrentAlpha = alpha; 4569 final Drawables dr = mDrawables; 4570 if (dr != null) { 4571 if (dr.mDrawableLeft != null) dr.mDrawableLeft.mutate().setAlpha(alpha); 4572 if (dr.mDrawableTop != null) dr.mDrawableTop.mutate().setAlpha(alpha); 4573 if (dr.mDrawableRight != null) dr.mDrawableRight.mutate().setAlpha(alpha); 4574 if (dr.mDrawableBottom != null) dr.mDrawableBottom.mutate().setAlpha(alpha); 4575 if (dr.mDrawableStart != null) dr.mDrawableStart.mutate().setAlpha(alpha); 4576 if (dr.mDrawableEnd != null) dr.mDrawableEnd.mutate().setAlpha(alpha); 4577 } 4578 return true; 4579 } 4580 4581 mCurrentAlpha = 255; 4582 return false; 4583 } 4584 4585 /** 4586 * When a TextView is used to display a useful piece of information to the user (such as a 4587 * contact's address), it should be made selectable, so that the user can select and copy this 4588 * content. 4589 * 4590 * Use {@link #setTextIsSelectable(boolean)} or the 4591 * {@link android.R.styleable#TextView_textIsSelectable} XML attribute to make this TextView 4592 * selectable (text is not selectable by default). 4593 * 4594 * Note that this method simply returns the state of this flag. Although this flag has to be set 4595 * in order to select text in non-editable TextView, the content of an {@link EditText} can 4596 * always be selected, independently of the value of this flag. 4597 * 4598 * @return True if the text displayed in this TextView can be selected by the user. 4599 * 4600 * @attr ref android.R.styleable#TextView_textIsSelectable 4601 */ 4602 public boolean isTextSelectable() { 4603 return mTextIsSelectable; 4604 } 4605 4606 /** 4607 * Sets whether or not (default) the content of this view is selectable by the user. 4608 * 4609 * Note that this methods affect the {@link #setFocusable(boolean)}, 4610 * {@link #setFocusableInTouchMode(boolean)} {@link #setClickable(boolean)} and 4611 * {@link #setLongClickable(boolean)} states and you may want to restore these if they were 4612 * customized. 4613 * 4614 * See {@link #isTextSelectable} for details. 4615 * 4616 * @param selectable Whether or not the content of this TextView should be selectable. 4617 */ 4618 public void setTextIsSelectable(boolean selectable) { 4619 if (mTextIsSelectable == selectable) return; 4620 4621 mTextIsSelectable = selectable; 4622 4623 setFocusableInTouchMode(selectable); 4624 setFocusable(selectable); 4625 setClickable(selectable); 4626 setLongClickable(selectable); 4627 4628 // mInputType is already EditorInfo.TYPE_NULL and mInput is null; 4629 4630 setMovementMethod(selectable ? ArrowKeyMovementMethod.getInstance() : null); 4631 setText(getText(), selectable ? BufferType.SPANNABLE : BufferType.NORMAL); 4632 4633 // Called by setText above, but safer in case of future code changes 4634 prepareCursorControllers(); 4635 } 4636 4637 @Override 4638 protected int[] onCreateDrawableState(int extraSpace) { 4639 final int[] drawableState; 4640 4641 if (mSingleLine) { 4642 drawableState = super.onCreateDrawableState(extraSpace); 4643 } else { 4644 drawableState = super.onCreateDrawableState(extraSpace + 1); 4645 mergeDrawableStates(drawableState, MULTILINE_STATE_SET); 4646 } 4647 4648 if (mTextIsSelectable) { 4649 // Disable pressed state, which was introduced when TextView was made clickable. 4650 // Prevents text color change. 4651 // setClickable(false) would have a similar effect, but it also disables focus changes 4652 // and long press actions, which are both needed by text selection. 4653 final int length = drawableState.length; 4654 for (int i = 0; i < length; i++) { 4655 if (drawableState[i] == R.attr.state_pressed) { 4656 final int[] nonPressedState = new int[length - 1]; 4657 System.arraycopy(drawableState, 0, nonPressedState, 0, i); 4658 System.arraycopy(drawableState, i + 1, nonPressedState, i, length - i - 1); 4659 return nonPressedState; 4660 } 4661 } 4662 } 4663 4664 return drawableState; 4665 } 4666 4667 @Override 4668 protected void onDraw(Canvas canvas) { 4669 if (mPreDrawState == PREDRAW_DONE) { 4670 final ViewTreeObserver observer = getViewTreeObserver(); 4671 observer.removeOnPreDrawListener(this); 4672 mPreDrawState = PREDRAW_NOT_REGISTERED; 4673 } 4674 4675 if (mCurrentAlpha <= ViewConfiguration.ALPHA_THRESHOLD_INT) return; 4676 4677 restartMarqueeIfNeeded(); 4678 4679 // Draw the background for this view 4680 super.onDraw(canvas); 4681 4682 final int compoundPaddingLeft = getCompoundPaddingLeft(); 4683 final int compoundPaddingTop = getCompoundPaddingTop(); 4684 final int compoundPaddingRight = getCompoundPaddingRight(); 4685 final int compoundPaddingBottom = getCompoundPaddingBottom(); 4686 final int scrollX = mScrollX; 4687 final int scrollY = mScrollY; 4688 final int right = mRight; 4689 final int left = mLeft; 4690 final int bottom = mBottom; 4691 final int top = mTop; 4692 4693 final Drawables dr = mDrawables; 4694 if (dr != null) { 4695 /* 4696 * Compound, not extended, because the icon is not clipped 4697 * if the text height is smaller. 4698 */ 4699 4700 int vspace = bottom - top - compoundPaddingBottom - compoundPaddingTop; 4701 int hspace = right - left - compoundPaddingRight - compoundPaddingLeft; 4702 4703 // IMPORTANT: The coordinates computed are also used in invalidateDrawable() 4704 // Make sure to update invalidateDrawable() when changing this code. 4705 if (dr.mDrawableLeft != null) { 4706 canvas.save(); 4707 canvas.translate(scrollX + mPaddingLeft, 4708 scrollY + compoundPaddingTop + 4709 (vspace - dr.mDrawableHeightLeft) / 2); 4710 dr.mDrawableLeft.draw(canvas); 4711 canvas.restore(); 4712 } 4713 4714 // IMPORTANT: The coordinates computed are also used in invalidateDrawable() 4715 // Make sure to update invalidateDrawable() when changing this code. 4716 if (dr.mDrawableRight != null) { 4717 canvas.save(); 4718 canvas.translate(scrollX + right - left - mPaddingRight - dr.mDrawableSizeRight, 4719 scrollY + compoundPaddingTop + (vspace - dr.mDrawableHeightRight) / 2); 4720 dr.mDrawableRight.draw(canvas); 4721 canvas.restore(); 4722 } 4723 4724 // IMPORTANT: The coordinates computed are also used in invalidateDrawable() 4725 // Make sure to update invalidateDrawable() when changing this code. 4726 if (dr.mDrawableTop != null) { 4727 canvas.save(); 4728 canvas.translate(scrollX + compoundPaddingLeft + (hspace - dr.mDrawableWidthTop) / 2, 4729 scrollY + mPaddingTop); 4730 dr.mDrawableTop.draw(canvas); 4731 canvas.restore(); 4732 } 4733 4734 // IMPORTANT: The coordinates computed are also used in invalidateDrawable() 4735 // Make sure to update invalidateDrawable() when changing this code. 4736 if (dr.mDrawableBottom != null) { 4737 canvas.save(); 4738 canvas.translate(scrollX + compoundPaddingLeft + 4739 (hspace - dr.mDrawableWidthBottom) / 2, 4740 scrollY + bottom - top - mPaddingBottom - dr.mDrawableSizeBottom); 4741 dr.mDrawableBottom.draw(canvas); 4742 canvas.restore(); 4743 } 4744 } 4745 4746 int color = mCurTextColor; 4747 4748 if (mLayout == null) { 4749 assumeLayout(); 4750 } 4751 4752 Layout layout = mLayout; 4753 int cursorcolor = color; 4754 4755 if (mHint != null && mText.length() == 0) { 4756 if (mHintTextColor != null) { 4757 color = mCurHintTextColor; 4758 } 4759 4760 layout = mHintLayout; 4761 } 4762 4763 mTextPaint.setColor(color); 4764 if (mCurrentAlpha != 255) { 4765 // If set, the alpha will override the color's alpha. Multiply the alphas. 4766 mTextPaint.setAlpha((mCurrentAlpha * Color.alpha(color)) / 255); 4767 } 4768 mTextPaint.drawableState = getDrawableState(); 4769 4770 canvas.save(); 4771 /* Would be faster if we didn't have to do this. Can we chop the 4772 (displayable) text so that we don't need to do this ever? 4773 */ 4774 4775 int extendedPaddingTop = getExtendedPaddingTop(); 4776 int extendedPaddingBottom = getExtendedPaddingBottom(); 4777 4778 float clipLeft = compoundPaddingLeft + scrollX; 4779 float clipTop = extendedPaddingTop + scrollY; 4780 float clipRight = right - left - compoundPaddingRight + scrollX; 4781 float clipBottom = bottom - top - extendedPaddingBottom + scrollY; 4782 4783 if (mShadowRadius != 0) { 4784 clipLeft += Math.min(0, mShadowDx - mShadowRadius); 4785 clipRight += Math.max(0, mShadowDx + mShadowRadius); 4786 4787 clipTop += Math.min(0, mShadowDy - mShadowRadius); 4788 clipBottom += Math.max(0, mShadowDy + mShadowRadius); 4789 } 4790 4791 canvas.clipRect(clipLeft, clipTop, clipRight, clipBottom); 4792 4793 int voffsetText = 0; 4794 int voffsetCursor = 0; 4795 4796 // translate in by our padding 4797 { 4798 /* shortcircuit calling getVerticaOffset() */ 4799 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { 4800 voffsetText = getVerticalOffset(false); 4801 voffsetCursor = getVerticalOffset(true); 4802 } 4803 canvas.translate(compoundPaddingLeft, extendedPaddingTop + voffsetText); 4804 } 4805 4806 final int layoutDirection = getResolvedLayoutDirection(); 4807 final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection); 4808 if (mEllipsize == TextUtils.TruncateAt.MARQUEE && 4809 mMarqueeFadeMode != MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { 4810 if (!mSingleLine && getLineCount() == 1 && canMarquee() && 4811 (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) != Gravity.LEFT) { 4812 canvas.translate(mLayout.getLineRight(0) - (mRight - mLeft - 4813 getCompoundPaddingLeft() - getCompoundPaddingRight()), 0.0f); 4814 } 4815 4816 if (mMarquee != null && mMarquee.isRunning()) { 4817 canvas.translate(-mMarquee.mScroll, 0.0f); 4818 } 4819 } 4820 4821 Path highlight = null; 4822 int selStart = -1, selEnd = -1; 4823 boolean drawCursor = false; 4824 4825 // If there is no movement method, then there can be no selection. 4826 // Check that first and attempt to skip everything having to do with 4827 // the cursor. 4828 // XXX This is not strictly true -- a program could set the 4829 // selection manually if it really wanted to. 4830 if (mMovement != null && (isFocused() || isPressed())) { 4831 selStart = getSelectionStart(); 4832 selEnd = getSelectionEnd(); 4833 4834 if (selStart >= 0) { 4835 if (mHighlightPath == null) mHighlightPath = new Path(); 4836 4837 if (selStart == selEnd) { 4838 if (isCursorVisible() && 4839 (SystemClock.uptimeMillis() - mShowCursor) % (2 * BLINK) < BLINK) { 4840 if (mHighlightPathBogus) { 4841 mHighlightPath.reset(); 4842 mLayout.getCursorPath(selStart, mHighlightPath, mText); 4843 updateCursorsPositions(); 4844 mHighlightPathBogus = false; 4845 } 4846 4847 // XXX should pass to skin instead of drawing directly 4848 mHighlightPaint.setColor(cursorcolor); 4849 if (mCurrentAlpha != 255) { 4850 mHighlightPaint.setAlpha( 4851 (mCurrentAlpha * Color.alpha(cursorcolor)) / 255); 4852 } 4853 mHighlightPaint.setStyle(Paint.Style.STROKE); 4854 highlight = mHighlightPath; 4855 drawCursor = mCursorCount > 0; 4856 } 4857 } else if (textCanBeSelected()) { 4858 if (mHighlightPathBogus) { 4859 mHighlightPath.reset(); 4860 mLayout.getSelectionPath(selStart, selEnd, mHighlightPath); 4861 mHighlightPathBogus = false; 4862 } 4863 4864 // XXX should pass to skin instead of drawing directly 4865 mHighlightPaint.setColor(mHighlightColor); 4866 if (mCurrentAlpha != 255) { 4867 mHighlightPaint.setAlpha( 4868 (mCurrentAlpha * Color.alpha(mHighlightColor)) / 255); 4869 } 4870 mHighlightPaint.setStyle(Paint.Style.FILL); 4871 4872 highlight = mHighlightPath; 4873 } 4874 } 4875 } 4876 4877 /* Comment out until we decide what to do about animations 4878 boolean isLinearTextOn = false; 4879 if (currentTransformation != null) { 4880 isLinearTextOn = mTextPaint.isLinearTextOn(); 4881 Matrix m = currentTransformation.getMatrix(); 4882 if (!m.isIdentity()) { 4883 // mTextPaint.setLinearTextOn(true); 4884 } 4885 } 4886 */ 4887 4888 final InputMethodState ims = mInputMethodState; 4889 final int cursorOffsetVertical = voffsetCursor - voffsetText; 4890 if (ims != null && ims.mBatchEditNesting == 0) { 4891 InputMethodManager imm = InputMethodManager.peekInstance(); 4892 if (imm != null) { 4893 if (imm.isActive(this)) { 4894 boolean reported = false; 4895 if (ims.mContentChanged || ims.mSelectionModeChanged) { 4896 // We are in extract mode and the content has changed 4897 // in some way... just report complete new text to the 4898 // input method. 4899 reported = reportExtractedText(); 4900 } 4901 if (!reported && highlight != null) { 4902 int candStart = -1; 4903 int candEnd = -1; 4904 if (mText instanceof Spannable) { 4905 Spannable sp = (Spannable)mText; 4906 candStart = EditableInputConnection.getComposingSpanStart(sp); 4907 candEnd = EditableInputConnection.getComposingSpanEnd(sp); 4908 } 4909 imm.updateSelection(this, selStart, selEnd, candStart, candEnd); 4910 } 4911 } 4912 4913 if (imm.isWatchingCursor(this) && highlight != null) { 4914 highlight.computeBounds(ims.mTmpRectF, true); 4915 ims.mTmpOffset[0] = ims.mTmpOffset[1] = 0; 4916 4917 canvas.getMatrix().mapPoints(ims.mTmpOffset); 4918 ims.mTmpRectF.offset(ims.mTmpOffset[0], ims.mTmpOffset[1]); 4919 4920 ims.mTmpRectF.offset(0, cursorOffsetVertical); 4921 4922 ims.mCursorRectInWindow.set((int)(ims.mTmpRectF.left + 0.5), 4923 (int)(ims.mTmpRectF.top + 0.5), 4924 (int)(ims.mTmpRectF.right + 0.5), 4925 (int)(ims.mTmpRectF.bottom + 0.5)); 4926 4927 imm.updateCursor(this, 4928 ims.mCursorRectInWindow.left, ims.mCursorRectInWindow.top, 4929 ims.mCursorRectInWindow.right, ims.mCursorRectInWindow.bottom); 4930 } 4931 } 4932 } 4933 4934 if (mCorrectionHighlighter != null) { 4935 mCorrectionHighlighter.draw(canvas, cursorOffsetVertical); 4936 } 4937 4938 if (drawCursor) { 4939 drawCursor(canvas, cursorOffsetVertical); 4940 // Rely on the drawable entirely, do not draw the cursor line. 4941 // Has to be done after the IMM related code above which relies on the highlight. 4942 highlight = null; 4943 } 4944 4945 layout.draw(canvas, highlight, mHighlightPaint, cursorOffsetVertical); 4946 4947 if (mMarquee != null && mMarquee.shouldDrawGhost()) { 4948 canvas.translate((int) mMarquee.getGhostOffset(), 0.0f); 4949 layout.draw(canvas, highlight, mHighlightPaint, cursorOffsetVertical); 4950 } 4951 4952 /* Comment out until we decide what to do about animations 4953 if (currentTransformation != null) { 4954 mTextPaint.setLinearTextOn(isLinearTextOn); 4955 } 4956 */ 4957 4958 canvas.restore(); 4959 } 4960 4961 private void updateCursorsPositions() { 4962 if (mCursorDrawableRes == 0) { 4963 mCursorCount = 0; 4964 return; 4965 } 4966 4967 final int offset = getSelectionStart(); 4968 final int line = mLayout.getLineForOffset(offset); 4969 final int top = mLayout.getLineTop(line); 4970 final int bottom = mLayout.getLineTop(line + 1); 4971 4972 mCursorCount = mLayout.isLevelBoundary(offset) ? 2 : 1; 4973 4974 int middle = bottom; 4975 if (mCursorCount == 2) { 4976 // Similar to what is done in {@link Layout.#getCursorPath(int, Path, CharSequence)} 4977 middle = (top + bottom) >> 1; 4978 } 4979 4980 updateCursorPosition(0, top, middle, mLayout.getPrimaryHorizontal(offset)); 4981 4982 if (mCursorCount == 2) { 4983 updateCursorPosition(1, middle, bottom, mLayout.getSecondaryHorizontal(offset)); 4984 } 4985 } 4986 4987 private void updateCursorPosition(int cursorIndex, int top, int bottom, float horizontal) { 4988 if (mCursorDrawable[cursorIndex] == null) 4989 mCursorDrawable[cursorIndex] = mContext.getResources().getDrawable(mCursorDrawableRes); 4990 4991 if (mTempRect == null) mTempRect = new Rect(); 4992 4993 mCursorDrawable[cursorIndex].getPadding(mTempRect); 4994 final int width = mCursorDrawable[cursorIndex].getIntrinsicWidth(); 4995 horizontal = Math.max(0.5f, horizontal - 0.5f); 4996 final int left = (int) (horizontal) - mTempRect.left; 4997 mCursorDrawable[cursorIndex].setBounds(left, top - mTempRect.top, left + width, 4998 bottom + mTempRect.bottom); 4999 } 5000 5001 private void drawCursor(Canvas canvas, int cursorOffsetVertical) { 5002 final boolean translate = cursorOffsetVertical != 0; 5003 if (translate) canvas.translate(0, cursorOffsetVertical); 5004 for (int i = 0; i < mCursorCount; i++) { 5005 mCursorDrawable[i].draw(canvas); 5006 } 5007 if (translate) canvas.translate(0, -cursorOffsetVertical); 5008 } 5009 5010 @Override 5011 public void getFocusedRect(Rect r) { 5012 if (mLayout == null) { 5013 super.getFocusedRect(r); 5014 return; 5015 } 5016 5017 int selEnd = getSelectionEnd(); 5018 if (selEnd < 0) { 5019 super.getFocusedRect(r); 5020 return; 5021 } 5022 5023 int selStart = getSelectionStart(); 5024 if (selStart < 0 || selStart >= selEnd) { 5025 int line = mLayout.getLineForOffset(selEnd); 5026 r.top = mLayout.getLineTop(line); 5027 r.bottom = mLayout.getLineBottom(line); 5028 r.left = (int) mLayout.getPrimaryHorizontal(selEnd) - 2; 5029 r.right = r.left + 4; 5030 } else { 5031 int lineStart = mLayout.getLineForOffset(selStart); 5032 int lineEnd = mLayout.getLineForOffset(selEnd); 5033 r.top = mLayout.getLineTop(lineStart); 5034 r.bottom = mLayout.getLineBottom(lineEnd); 5035 if (lineStart == lineEnd) { 5036 r.left = (int) mLayout.getPrimaryHorizontal(selStart); 5037 r.right = (int) mLayout.getPrimaryHorizontal(selEnd); 5038 } else { 5039 // Selection extends across multiple lines -- the focused 5040 // rect covers the entire width. 5041 if (mHighlightPath == null) mHighlightPath = new Path(); 5042 if (mHighlightPathBogus) { 5043 mHighlightPath.reset(); 5044 mLayout.getSelectionPath(selStart, selEnd, mHighlightPath); 5045 mHighlightPathBogus = false; 5046 } 5047 synchronized (sTempRect) { 5048 mHighlightPath.computeBounds(sTempRect, true); 5049 r.left = (int)sTempRect.left-1; 5050 r.right = (int)sTempRect.right+1; 5051 } 5052 } 5053 } 5054 5055 // Adjust for padding and gravity. 5056 int paddingLeft = getCompoundPaddingLeft(); 5057 int paddingTop = getExtendedPaddingTop(); 5058 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { 5059 paddingTop += getVerticalOffset(false); 5060 } 5061 r.offset(paddingLeft, paddingTop); 5062 } 5063 5064 /** 5065 * Return the number of lines of text, or 0 if the internal Layout has not 5066 * been built. 5067 */ 5068 public int getLineCount() { 5069 return mLayout != null ? mLayout.getLineCount() : 0; 5070 } 5071 5072 /** 5073 * Return the baseline for the specified line (0...getLineCount() - 1) 5074 * If bounds is not null, return the top, left, right, bottom extents 5075 * of the specified line in it. If the internal Layout has not been built, 5076 * return 0 and set bounds to (0, 0, 0, 0) 5077 * @param line which line to examine (0..getLineCount() - 1) 5078 * @param bounds Optional. If not null, it returns the extent of the line 5079 * @return the Y-coordinate of the baseline 5080 */ 5081 public int getLineBounds(int line, Rect bounds) { 5082 if (mLayout == null) { 5083 if (bounds != null) { 5084 bounds.set(0, 0, 0, 0); 5085 } 5086 return 0; 5087 } 5088 else { 5089 int baseline = mLayout.getLineBounds(line, bounds); 5090 5091 int voffset = getExtendedPaddingTop(); 5092 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { 5093 voffset += getVerticalOffset(true); 5094 } 5095 if (bounds != null) { 5096 bounds.offset(getCompoundPaddingLeft(), voffset); 5097 } 5098 return baseline + voffset; 5099 } 5100 } 5101 5102 @Override 5103 public int getBaseline() { 5104 if (mLayout == null) { 5105 return super.getBaseline(); 5106 } 5107 5108 int voffset = 0; 5109 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { 5110 voffset = getVerticalOffset(true); 5111 } 5112 5113 return getExtendedPaddingTop() + voffset + mLayout.getLineBaseline(0); 5114 } 5115 5116 /** 5117 * @hide 5118 * @param offsetRequired 5119 */ 5120 @Override 5121 protected int getFadeTop(boolean offsetRequired) { 5122 if (mLayout == null) return 0; 5123 5124 int voffset = 0; 5125 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { 5126 voffset = getVerticalOffset(true); 5127 } 5128 5129 if (offsetRequired) voffset += getTopPaddingOffset(); 5130 5131 return getExtendedPaddingTop() + voffset; 5132 } 5133 5134 /** 5135 * @hide 5136 * @param offsetRequired 5137 */ 5138 @Override 5139 protected int getFadeHeight(boolean offsetRequired) { 5140 return mLayout != null ? mLayout.getHeight() : 0; 5141 } 5142 5143 @Override 5144 public boolean onKeyPreIme(int keyCode, KeyEvent event) { 5145 if (keyCode == KeyEvent.KEYCODE_BACK) { 5146 boolean isInSelectionMode = mSelectionActionMode != null; 5147 5148 if (isInSelectionMode) { 5149 if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { 5150 KeyEvent.DispatcherState state = getKeyDispatcherState(); 5151 if (state != null) { 5152 state.startTracking(event, this); 5153 } 5154 return true; 5155 } else if (event.getAction() == KeyEvent.ACTION_UP) { 5156 KeyEvent.DispatcherState state = getKeyDispatcherState(); 5157 if (state != null) { 5158 state.handleUpEvent(event); 5159 } 5160 if (event.isTracking() && !event.isCanceled()) { 5161 if (isInSelectionMode) { 5162 stopSelectionActionMode(); 5163 return true; 5164 } 5165 } 5166 } 5167 } 5168 } 5169 return super.onKeyPreIme(keyCode, event); 5170 } 5171 5172 @Override 5173 public boolean onKeyDown(int keyCode, KeyEvent event) { 5174 int which = doKeyDown(keyCode, event, null); 5175 if (which == 0) { 5176 // Go through default dispatching. 5177 return super.onKeyDown(keyCode, event); 5178 } 5179 5180 return true; 5181 } 5182 5183 @Override 5184 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { 5185 KeyEvent down = KeyEvent.changeAction(event, KeyEvent.ACTION_DOWN); 5186 5187 int which = doKeyDown(keyCode, down, event); 5188 if (which == 0) { 5189 // Go through default dispatching. 5190 return super.onKeyMultiple(keyCode, repeatCount, event); 5191 } 5192 if (which == -1) { 5193 // Consumed the whole thing. 5194 return true; 5195 } 5196 5197 repeatCount--; 5198 5199 // We are going to dispatch the remaining events to either the input 5200 // or movement method. To do this, we will just send a repeated stream 5201 // of down and up events until we have done the complete repeatCount. 5202 // It would be nice if those interfaces had an onKeyMultiple() method, 5203 // but adding that is a more complicated change. 5204 KeyEvent up = KeyEvent.changeAction(event, KeyEvent.ACTION_UP); 5205 if (which == 1) { 5206 mInput.onKeyUp(this, (Editable)mText, keyCode, up); 5207 while (--repeatCount > 0) { 5208 mInput.onKeyDown(this, (Editable)mText, keyCode, down); 5209 mInput.onKeyUp(this, (Editable)mText, keyCode, up); 5210 } 5211 hideErrorIfUnchanged(); 5212 5213 } else if (which == 2) { 5214 mMovement.onKeyUp(this, (Spannable)mText, keyCode, up); 5215 while (--repeatCount > 0) { 5216 mMovement.onKeyDown(this, (Spannable)mText, keyCode, down); 5217 mMovement.onKeyUp(this, (Spannable)mText, keyCode, up); 5218 } 5219 } 5220 5221 return true; 5222 } 5223 5224 /** 5225 * Returns true if pressing ENTER in this field advances focus instead 5226 * of inserting the character. This is true mostly in single-line fields, 5227 * but also in mail addresses and subjects which will display on multiple 5228 * lines but where it doesn't make sense to insert newlines. 5229 */ 5230 private boolean shouldAdvanceFocusOnEnter() { 5231 if (mInput == null) { 5232 return false; 5233 } 5234 5235 if (mSingleLine) { 5236 return true; 5237 } 5238 5239 if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { 5240 int variation = mInputType & EditorInfo.TYPE_MASK_VARIATION; 5241 if (variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS 5242 || variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_SUBJECT) { 5243 return true; 5244 } 5245 } 5246 5247 return false; 5248 } 5249 5250 /** 5251 * Returns true if pressing TAB in this field advances focus instead 5252 * of inserting the character. Insert tabs only in multi-line editors. 5253 */ 5254 private boolean shouldAdvanceFocusOnTab() { 5255 if (mInput != null && !mSingleLine) { 5256 if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { 5257 int variation = mInputType & EditorInfo.TYPE_MASK_VARIATION; 5258 if (variation == EditorInfo.TYPE_TEXT_FLAG_IME_MULTI_LINE 5259 || variation == EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE) { 5260 return false; 5261 } 5262 } 5263 } 5264 return true; 5265 } 5266 5267 private int doKeyDown(int keyCode, KeyEvent event, KeyEvent otherEvent) { 5268 if (!isEnabled()) { 5269 return 0; 5270 } 5271 5272 switch (keyCode) { 5273 case KeyEvent.KEYCODE_ENTER: 5274 mEnterKeyIsDown = true; 5275 if (event.hasNoModifiers()) { 5276 // When mInputContentType is set, we know that we are 5277 // running in a "modern" cupcake environment, so don't need 5278 // to worry about the application trying to capture 5279 // enter key events. 5280 if (mInputContentType != null) { 5281 // If there is an action listener, given them a 5282 // chance to consume the event. 5283 if (mInputContentType.onEditorActionListener != null && 5284 mInputContentType.onEditorActionListener.onEditorAction( 5285 this, EditorInfo.IME_NULL, event)) { 5286 mInputContentType.enterDown = true; 5287 // We are consuming the enter key for them. 5288 return -1; 5289 } 5290 } 5291 5292 // If our editor should move focus when enter is pressed, or 5293 // this is a generated event from an IME action button, then 5294 // don't let it be inserted into the text. 5295 if ((event.getFlags() & KeyEvent.FLAG_EDITOR_ACTION) != 0 5296 || shouldAdvanceFocusOnEnter()) { 5297 if (mOnClickListener != null) { 5298 return 0; 5299 } 5300 return -1; 5301 } 5302 } 5303 break; 5304 5305 case KeyEvent.KEYCODE_DPAD_CENTER: 5306 mDPadCenterIsDown = true; 5307 if (event.hasNoModifiers()) { 5308 if (shouldAdvanceFocusOnEnter()) { 5309 return 0; 5310 } 5311 } 5312 break; 5313 5314 case KeyEvent.KEYCODE_TAB: 5315 if (event.hasNoModifiers() || event.hasModifiers(KeyEvent.META_SHIFT_ON)) { 5316 if (shouldAdvanceFocusOnTab()) { 5317 return 0; 5318 } 5319 } 5320 break; 5321 5322 // Has to be done on key down (and not on key up) to correctly be intercepted. 5323 case KeyEvent.KEYCODE_BACK: 5324 if (mSelectionActionMode != null) { 5325 stopSelectionActionMode(); 5326 return -1; 5327 } 5328 break; 5329 } 5330 5331 if (mInput != null) { 5332 resetErrorChangedFlag(); 5333 5334 boolean doDown = true; 5335 if (otherEvent != null) { 5336 try { 5337 beginBatchEdit(); 5338 final boolean handled = mInput.onKeyOther(this, (Editable) mText, otherEvent); 5339 hideErrorIfUnchanged(); 5340 doDown = false; 5341 if (handled) { 5342 return -1; 5343 } 5344 } catch (AbstractMethodError e) { 5345 // onKeyOther was added after 1.0, so if it isn't 5346 // implemented we need to try to dispatch as a regular down. 5347 } finally { 5348 endBatchEdit(); 5349 } 5350 } 5351 5352 if (doDown) { 5353 beginBatchEdit(); 5354 final boolean handled = mInput.onKeyDown(this, (Editable) mText, keyCode, event); 5355 endBatchEdit(); 5356 hideErrorIfUnchanged(); 5357 if (handled) return 1; 5358 } 5359 } 5360 5361 // bug 650865: sometimes we get a key event before a layout. 5362 // don't try to move around if we don't know the layout. 5363 5364 if (mMovement != null && mLayout != null) { 5365 boolean doDown = true; 5366 if (otherEvent != null) { 5367 try { 5368 boolean handled = mMovement.onKeyOther(this, (Spannable) mText, 5369 otherEvent); 5370 doDown = false; 5371 if (handled) { 5372 return -1; 5373 } 5374 } catch (AbstractMethodError e) { 5375 // onKeyOther was added after 1.0, so if it isn't 5376 // implemented we need to try to dispatch as a regular down. 5377 } 5378 } 5379 if (doDown) { 5380 if (mMovement.onKeyDown(this, (Spannable)mText, keyCode, event)) 5381 return 2; 5382 } 5383 } 5384 5385 return 0; 5386 } 5387 5388 /** 5389 * Resets the mErrorWasChanged flag, so that future calls to {@link #setError(CharSequence)} 5390 * can be recorded. 5391 * @hide 5392 */ 5393 public void resetErrorChangedFlag() { 5394 /* 5395 * Keep track of what the error was before doing the input 5396 * so that if an input filter changed the error, we leave 5397 * that error showing. Otherwise, we take down whatever 5398 * error was showing when the user types something. 5399 */ 5400 mErrorWasChanged = false; 5401 } 5402 5403 /** 5404 * @hide 5405 */ 5406 public void hideErrorIfUnchanged() { 5407 if (mError != null && !mErrorWasChanged) { 5408 setError(null, null); 5409 } 5410 } 5411 5412 @Override 5413 public boolean onKeyUp(int keyCode, KeyEvent event) { 5414 if (!isEnabled()) { 5415 return super.onKeyUp(keyCode, event); 5416 } 5417 5418 switch (keyCode) { 5419 case KeyEvent.KEYCODE_DPAD_CENTER: 5420 mDPadCenterIsDown = false; 5421 if (event.hasNoModifiers()) { 5422 /* 5423 * If there is a click listener, just call through to 5424 * super, which will invoke it. 5425 * 5426 * If there isn't a click listener, try to show the soft 5427 * input method. (It will also 5428 * call performClick(), but that won't do anything in 5429 * this case.) 5430 */ 5431 if (mOnClickListener == null) { 5432 if (mMovement != null && mText instanceof Editable 5433 && mLayout != null && onCheckIsTextEditor()) { 5434 InputMethodManager imm = InputMethodManager.peekInstance(); 5435 viewClicked(imm); 5436 if (imm != null) { 5437 imm.showSoftInput(this, 0); 5438 } 5439 } 5440 } 5441 } 5442 return super.onKeyUp(keyCode, event); 5443 5444 case KeyEvent.KEYCODE_ENTER: 5445 mEnterKeyIsDown = false; 5446 if (event.hasNoModifiers()) { 5447 if (mInputContentType != null 5448 && mInputContentType.onEditorActionListener != null 5449 && mInputContentType.enterDown) { 5450 mInputContentType.enterDown = false; 5451 if (mInputContentType.onEditorActionListener.onEditorAction( 5452 this, EditorInfo.IME_NULL, event)) { 5453 return true; 5454 } 5455 } 5456 5457 if ((event.getFlags() & KeyEvent.FLAG_EDITOR_ACTION) != 0 5458 || shouldAdvanceFocusOnEnter()) { 5459 /* 5460 * If there is a click listener, just call through to 5461 * super, which will invoke it. 5462 * 5463 * If there isn't a click listener, try to advance focus, 5464 * but still call through to super, which will reset the 5465 * pressed state and longpress state. (It will also 5466 * call performClick(), but that won't do anything in 5467 * this case.) 5468 */ 5469 if (mOnClickListener == null) { 5470 View v = focusSearch(FOCUS_DOWN); 5471 5472 if (v != null) { 5473 if (!v.requestFocus(FOCUS_DOWN)) { 5474 throw new IllegalStateException( 5475 "focus search returned a view " + 5476 "that wasn't able to take focus!"); 5477 } 5478 5479 /* 5480 * Return true because we handled the key; super 5481 * will return false because there was no click 5482 * listener. 5483 */ 5484 super.onKeyUp(keyCode, event); 5485 return true; 5486 } else if ((event.getFlags() 5487 & KeyEvent.FLAG_EDITOR_ACTION) != 0) { 5488 // No target for next focus, but make sure the IME 5489 // if this came from it. 5490 InputMethodManager imm = InputMethodManager.peekInstance(); 5491 if (imm != null && imm.isActive(this)) { 5492 imm.hideSoftInputFromWindow(getWindowToken(), 0); 5493 } 5494 } 5495 } 5496 } 5497 return super.onKeyUp(keyCode, event); 5498 } 5499 break; 5500 } 5501 5502 if (mInput != null) 5503 if (mInput.onKeyUp(this, (Editable) mText, keyCode, event)) 5504 return true; 5505 5506 if (mMovement != null && mLayout != null) 5507 if (mMovement.onKeyUp(this, (Spannable) mText, keyCode, event)) 5508 return true; 5509 5510 return super.onKeyUp(keyCode, event); 5511 } 5512 5513 @Override public boolean onCheckIsTextEditor() { 5514 return mInputType != EditorInfo.TYPE_NULL; 5515 } 5516 5517 @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { 5518 if (onCheckIsTextEditor() && isEnabled()) { 5519 if (mInputMethodState == null) { 5520 mInputMethodState = new InputMethodState(); 5521 } 5522 outAttrs.inputType = mInputType; 5523 if (mInputContentType != null) { 5524 outAttrs.imeOptions = mInputContentType.imeOptions; 5525 outAttrs.privateImeOptions = mInputContentType.privateImeOptions; 5526 outAttrs.actionLabel = mInputContentType.imeActionLabel; 5527 outAttrs.actionId = mInputContentType.imeActionId; 5528 outAttrs.extras = mInputContentType.extras; 5529 } else { 5530 outAttrs.imeOptions = EditorInfo.IME_NULL; 5531 } 5532 if (focusSearch(FOCUS_DOWN) != null) { 5533 outAttrs.imeOptions |= EditorInfo.IME_FLAG_NAVIGATE_NEXT; 5534 } 5535 if (focusSearch(FOCUS_UP) != null) { 5536 outAttrs.imeOptions |= EditorInfo.IME_FLAG_NAVIGATE_PREVIOUS; 5537 } 5538 if ((outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION) 5539 == EditorInfo.IME_ACTION_UNSPECIFIED) { 5540 if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NAVIGATE_NEXT) != 0) { 5541 // An action has not been set, but the enter key will move to 5542 // the next focus, so set the action to that. 5543 outAttrs.imeOptions |= EditorInfo.IME_ACTION_NEXT; 5544 } else { 5545 // An action has not been set, and there is no focus to move 5546 // to, so let's just supply a "done" action. 5547 outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE; 5548 } 5549 if (!shouldAdvanceFocusOnEnter()) { 5550 outAttrs.imeOptions |= EditorInfo.IME_FLAG_NO_ENTER_ACTION; 5551 } 5552 } 5553 if (isMultilineInputType(outAttrs.inputType)) { 5554 // Multi-line text editors should always show an enter key. 5555 outAttrs.imeOptions |= EditorInfo.IME_FLAG_NO_ENTER_ACTION; 5556 } 5557 outAttrs.hintText = mHint; 5558 if (mText instanceof Editable) { 5559 InputConnection ic = new EditableInputConnection(this); 5560 outAttrs.initialSelStart = getSelectionStart(); 5561 outAttrs.initialSelEnd = getSelectionEnd(); 5562 outAttrs.initialCapsMode = ic.getCursorCapsMode(mInputType); 5563 return ic; 5564 } 5565 } 5566 return null; 5567 } 5568 5569 /** 5570 * If this TextView contains editable content, extract a portion of it 5571 * based on the information in <var>request</var> in to <var>outText</var>. 5572 * @return Returns true if the text was successfully extracted, else false. 5573 */ 5574 public boolean extractText(ExtractedTextRequest request, 5575 ExtractedText outText) { 5576 return extractTextInternal(request, EXTRACT_UNKNOWN, EXTRACT_UNKNOWN, 5577 EXTRACT_UNKNOWN, outText); 5578 } 5579 5580 static final int EXTRACT_NOTHING = -2; 5581 static final int EXTRACT_UNKNOWN = -1; 5582 5583 boolean extractTextInternal(ExtractedTextRequest request, 5584 int partialStartOffset, int partialEndOffset, int delta, 5585 ExtractedText outText) { 5586 final CharSequence content = mText; 5587 if (content != null) { 5588 if (partialStartOffset != EXTRACT_NOTHING) { 5589 final int N = content.length(); 5590 if (partialStartOffset < 0) { 5591 outText.partialStartOffset = outText.partialEndOffset = -1; 5592 partialStartOffset = 0; 5593 partialEndOffset = N; 5594 } else { 5595 // Now use the delta to determine the actual amount of text 5596 // we need. 5597 partialEndOffset += delta; 5598 // Adjust offsets to ensure we contain full spans. 5599 if (content instanceof Spanned) { 5600 Spanned spanned = (Spanned)content; 5601 Object[] spans = spanned.getSpans(partialStartOffset, 5602 partialEndOffset, ParcelableSpan.class); 5603 int i = spans.length; 5604 while (i > 0) { 5605 i--; 5606 int j = spanned.getSpanStart(spans[i]); 5607 if (j < partialStartOffset) partialStartOffset = j; 5608 j = spanned.getSpanEnd(spans[i]); 5609 if (j > partialEndOffset) partialEndOffset = j; 5610 } 5611 } 5612 outText.partialStartOffset = partialStartOffset; 5613 outText.partialEndOffset = partialEndOffset - delta; 5614 5615 if (partialStartOffset > N) { 5616 partialStartOffset = N; 5617 } else if (partialStartOffset < 0) { 5618 partialStartOffset = 0; 5619 } 5620 if (partialEndOffset > N) { 5621 partialEndOffset = N; 5622 } else if (partialEndOffset < 0) { 5623 partialEndOffset = 0; 5624 } 5625 } 5626 if ((request.flags&InputConnection.GET_TEXT_WITH_STYLES) != 0) { 5627 outText.text = content.subSequence(partialStartOffset, 5628 partialEndOffset); 5629 } else { 5630 outText.text = TextUtils.substring(content, partialStartOffset, 5631 partialEndOffset); 5632 } 5633 } else { 5634 outText.partialStartOffset = 0; 5635 outText.partialEndOffset = 0; 5636 outText.text = ""; 5637 } 5638 outText.flags = 0; 5639 if (MetaKeyKeyListener.getMetaState(mText, MetaKeyKeyListener.META_SELECTING) != 0) { 5640 outText.flags |= ExtractedText.FLAG_SELECTING; 5641 } 5642 if (mSingleLine) { 5643 outText.flags |= ExtractedText.FLAG_SINGLE_LINE; 5644 } 5645 outText.startOffset = 0; 5646 outText.selectionStart = getSelectionStart(); 5647 outText.selectionEnd = getSelectionEnd(); 5648 return true; 5649 } 5650 return false; 5651 } 5652 5653 boolean reportExtractedText() { 5654 final InputMethodState ims = mInputMethodState; 5655 if (ims != null) { 5656 final boolean contentChanged = ims.mContentChanged; 5657 if (contentChanged || ims.mSelectionModeChanged) { 5658 ims.mContentChanged = false; 5659 ims.mSelectionModeChanged = false; 5660 final ExtractedTextRequest req = mInputMethodState.mExtracting; 5661 if (req != null) { 5662 InputMethodManager imm = InputMethodManager.peekInstance(); 5663 if (imm != null) { 5664 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "Retrieving extracted start=" 5665 + ims.mChangedStart + " end=" + ims.mChangedEnd 5666 + " delta=" + ims.mChangedDelta); 5667 if (ims.mChangedStart < 0 && !contentChanged) { 5668 ims.mChangedStart = EXTRACT_NOTHING; 5669 } 5670 if (extractTextInternal(req, ims.mChangedStart, ims.mChangedEnd, 5671 ims.mChangedDelta, ims.mTmpExtracted)) { 5672 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "Reporting extracted start=" 5673 + ims.mTmpExtracted.partialStartOffset 5674 + " end=" + ims.mTmpExtracted.partialEndOffset 5675 + ": " + ims.mTmpExtracted.text); 5676 imm.updateExtractedText(this, req.token, 5677 mInputMethodState.mTmpExtracted); 5678 ims.mChangedStart = EXTRACT_UNKNOWN; 5679 ims.mChangedEnd = EXTRACT_UNKNOWN; 5680 ims.mChangedDelta = 0; 5681 ims.mContentChanged = false; 5682 return true; 5683 } 5684 } 5685 } 5686 } 5687 } 5688 return false; 5689 } 5690 5691 /** 5692 * This is used to remove all style-impacting spans from text before new 5693 * extracted text is being replaced into it, so that we don't have any 5694 * lingering spans applied during the replace. 5695 */ 5696 static void removeParcelableSpans(Spannable spannable, int start, int end) { 5697 Object[] spans = spannable.getSpans(start, end, ParcelableSpan.class); 5698 int i = spans.length; 5699 while (i > 0) { 5700 i--; 5701 spannable.removeSpan(spans[i]); 5702 } 5703 } 5704 5705 /** 5706 * Apply to this text view the given extracted text, as previously 5707 * returned by {@link #extractText(ExtractedTextRequest, ExtractedText)}. 5708 */ 5709 public void setExtractedText(ExtractedText text) { 5710 Editable content = getEditableText(); 5711 if (text.text != null) { 5712 if (content == null) { 5713 setText(text.text, TextView.BufferType.EDITABLE); 5714 } else if (text.partialStartOffset < 0) { 5715 removeParcelableSpans(content, 0, content.length()); 5716 content.replace(0, content.length(), text.text); 5717 } else { 5718 final int N = content.length(); 5719 int start = text.partialStartOffset; 5720 if (start > N) start = N; 5721 int end = text.partialEndOffset; 5722 if (end > N) end = N; 5723 removeParcelableSpans(content, start, end); 5724 content.replace(start, end, text.text); 5725 } 5726 } 5727 5728 // Now set the selection position... make sure it is in range, to 5729 // avoid crashes. If this is a partial update, it is possible that 5730 // the underlying text may have changed, causing us problems here. 5731 // Also we just don't want to trust clients to do the right thing. 5732 Spannable sp = (Spannable)getText(); 5733 final int N = sp.length(); 5734 int start = text.selectionStart; 5735 if (start < 0) start = 0; 5736 else if (start > N) start = N; 5737 int end = text.selectionEnd; 5738 if (end < 0) end = 0; 5739 else if (end > N) end = N; 5740 Selection.setSelection(sp, start, end); 5741 5742 // Finally, update the selection mode. 5743 if ((text.flags&ExtractedText.FLAG_SELECTING) != 0) { 5744 MetaKeyKeyListener.startSelecting(this, sp); 5745 } else { 5746 MetaKeyKeyListener.stopSelecting(this, sp); 5747 } 5748 } 5749 5750 /** 5751 * @hide 5752 */ 5753 public void setExtracting(ExtractedTextRequest req) { 5754 if (mInputMethodState != null) { 5755 mInputMethodState.mExtracting = req; 5756 } 5757 // This would stop a possible selection mode, but no such mode is started in case 5758 // extracted mode will start. Some text is selected though, and will trigger an action mode 5759 // in the extracted view. 5760 hideControllers(); 5761 } 5762 5763 /** 5764 * Called by the framework in response to a text completion from 5765 * the current input method, provided by it calling 5766 * {@link InputConnection#commitCompletion 5767 * InputConnection.commitCompletion()}. The default implementation does 5768 * nothing; text views that are supporting auto-completion should override 5769 * this to do their desired behavior. 5770 * 5771 * @param text The auto complete text the user has selected. 5772 */ 5773 public void onCommitCompletion(CompletionInfo text) { 5774 // intentionally empty 5775 } 5776 5777 /** 5778 * Called by the framework in response to a text auto-correction (such as fixing a typo using a 5779 * a dictionnary) from the current input method, provided by it calling 5780 * {@link InputConnection#commitCorrection} InputConnection.commitCorrection()}. The default 5781 * implementation flashes the background of the corrected word to provide feedback to the user. 5782 * 5783 * @param info The auto correct info about the text that was corrected. 5784 */ 5785 public void onCommitCorrection(CorrectionInfo info) { 5786 if (mCorrectionHighlighter == null) { 5787 mCorrectionHighlighter = new CorrectionHighlighter(); 5788 } else { 5789 mCorrectionHighlighter.invalidate(false); 5790 } 5791 5792 mCorrectionHighlighter.highlight(info); 5793 } 5794 5795 private class CorrectionHighlighter { 5796 private final Path mPath = new Path(); 5797 private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 5798 private int mStart, mEnd; 5799 private long mFadingStartTime; 5800 private final static int FADE_OUT_DURATION = 400; 5801 5802 public CorrectionHighlighter() { 5803 mPaint.setCompatibilityScaling(getResources().getCompatibilityInfo().applicationScale); 5804 mPaint.setStyle(Paint.Style.FILL); 5805 } 5806 5807 public void highlight(CorrectionInfo info) { 5808 mStart = info.getOffset(); 5809 mEnd = mStart + info.getNewText().length(); 5810 mFadingStartTime = SystemClock.uptimeMillis(); 5811 5812 if (mStart < 0 || mEnd < 0) { 5813 stopAnimation(); 5814 } 5815 } 5816 5817 public void draw(Canvas canvas, int cursorOffsetVertical) { 5818 if (updatePath() && updatePaint()) { 5819 if (cursorOffsetVertical != 0) { 5820 canvas.translate(0, cursorOffsetVertical); 5821 } 5822 5823 canvas.drawPath(mPath, mPaint); 5824 5825 if (cursorOffsetVertical != 0) { 5826 canvas.translate(0, -cursorOffsetVertical); 5827 } 5828 invalidate(true); 5829 } else { 5830 stopAnimation(); 5831 invalidate(false); 5832 } 5833 } 5834 5835 private boolean updatePaint() { 5836 final long duration = SystemClock.uptimeMillis() - mFadingStartTime; 5837 if (duration > FADE_OUT_DURATION) return false; 5838 5839 final float coef = 1.0f - (float) duration / FADE_OUT_DURATION; 5840 final int highlightColorAlpha = Color.alpha(mHighlightColor); 5841 final int color = (mHighlightColor & 0x00FFFFFF) + 5842 ((int) (highlightColorAlpha * coef) << 24); 5843 mPaint.setColor(color); 5844 return true; 5845 } 5846 5847 private boolean updatePath() { 5848 final Layout layout = TextView.this.mLayout; 5849 if (layout == null) return false; 5850 5851 // Update in case text is edited while the animation is run 5852 final int length = mText.length(); 5853 int start = Math.min(length, mStart); 5854 int end = Math.min(length, mEnd); 5855 5856 mPath.reset(); 5857 TextView.this.mLayout.getSelectionPath(start, end, mPath); 5858 return true; 5859 } 5860 5861 private void invalidate(boolean delayed) { 5862 if (TextView.this.mLayout == null) return; 5863 5864 synchronized (sTempRect) { 5865 mPath.computeBounds(sTempRect, false); 5866 5867 int left = getCompoundPaddingLeft(); 5868 int top = getExtendedPaddingTop() + getVerticalOffset(true); 5869 5870 if (delayed) { 5871 TextView.this.postInvalidateDelayed(16, // 60 Hz update 5872 left + (int) sTempRect.left, top + (int) sTempRect.top, 5873 left + (int) sTempRect.right, top + (int) sTempRect.bottom); 5874 } else { 5875 TextView.this.postInvalidate((int) sTempRect.left, (int) sTempRect.top, 5876 (int) sTempRect.right, (int) sTempRect.bottom); 5877 } 5878 } 5879 } 5880 5881 private void stopAnimation() { 5882 TextView.this.mCorrectionHighlighter = null; 5883 } 5884 } 5885 5886 public void beginBatchEdit() { 5887 mInBatchEditControllers = true; 5888 final InputMethodState ims = mInputMethodState; 5889 if (ims != null) { 5890 int nesting = ++ims.mBatchEditNesting; 5891 if (nesting == 1) { 5892 ims.mCursorChanged = false; 5893 ims.mChangedDelta = 0; 5894 if (ims.mContentChanged) { 5895 // We already have a pending change from somewhere else, 5896 // so turn this into a full update. 5897 ims.mChangedStart = 0; 5898 ims.mChangedEnd = mText.length(); 5899 } else { 5900 ims.mChangedStart = EXTRACT_UNKNOWN; 5901 ims.mChangedEnd = EXTRACT_UNKNOWN; 5902 ims.mContentChanged = false; 5903 } 5904 onBeginBatchEdit(); 5905 } 5906 } 5907 } 5908 5909 public void endBatchEdit() { 5910 mInBatchEditControllers = false; 5911 final InputMethodState ims = mInputMethodState; 5912 if (ims != null) { 5913 int nesting = --ims.mBatchEditNesting; 5914 if (nesting == 0) { 5915 finishBatchEdit(ims); 5916 } 5917 } 5918 } 5919 5920 void ensureEndedBatchEdit() { 5921 final InputMethodState ims = mInputMethodState; 5922 if (ims != null && ims.mBatchEditNesting != 0) { 5923 ims.mBatchEditNesting = 0; 5924 finishBatchEdit(ims); 5925 } 5926 } 5927 5928 void finishBatchEdit(final InputMethodState ims) { 5929 onEndBatchEdit(); 5930 5931 if (ims.mContentChanged || ims.mSelectionModeChanged) { 5932 updateAfterEdit(); 5933 reportExtractedText(); 5934 } else if (ims.mCursorChanged) { 5935 // Cheezy way to get us to report the current cursor location. 5936 invalidateCursor(); 5937 } 5938 } 5939 5940 void updateAfterEdit() { 5941 invalidate(); 5942 int curs = getSelectionStart(); 5943 5944 if (curs >= 0 || (mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { 5945 registerForPreDraw(); 5946 } 5947 5948 if (curs >= 0) { 5949 mHighlightPathBogus = true; 5950 makeBlink(); 5951 bringPointIntoView(curs); 5952 } 5953 5954 checkForResize(); 5955 } 5956 5957 /** 5958 * Called by the framework in response to a request to begin a batch 5959 * of edit operations through a call to link {@link #beginBatchEdit()}. 5960 */ 5961 public void onBeginBatchEdit() { 5962 // intentionally empty 5963 } 5964 5965 /** 5966 * Called by the framework in response to a request to end a batch 5967 * of edit operations through a call to link {@link #endBatchEdit}. 5968 */ 5969 public void onEndBatchEdit() { 5970 // intentionally empty 5971 } 5972 5973 /** 5974 * Called by the framework in response to a private command from the 5975 * current method, provided by it calling 5976 * {@link InputConnection#performPrivateCommand 5977 * InputConnection.performPrivateCommand()}. 5978 * 5979 * @param action The action name of the command. 5980 * @param data Any additional data for the command. This may be null. 5981 * @return Return true if you handled the command, else false. 5982 */ 5983 public boolean onPrivateIMECommand(String action, Bundle data) { 5984 return false; 5985 } 5986 5987 private void nullLayouts() { 5988 if (mLayout instanceof BoringLayout && mSavedLayout == null) { 5989 mSavedLayout = (BoringLayout) mLayout; 5990 } 5991 if (mHintLayout instanceof BoringLayout && mSavedHintLayout == null) { 5992 mSavedHintLayout = (BoringLayout) mHintLayout; 5993 } 5994 5995 mSavedMarqueeModeLayout = mLayout = mHintLayout = null; 5996 5997 // Since it depends on the value of mLayout 5998 prepareCursorControllers(); 5999 } 6000 6001 /** 6002 * Make a new Layout based on the already-measured size of the view, 6003 * on the assumption that it was measured correctly at some point. 6004 */ 6005 private void assumeLayout() { 6006 int width = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); 6007 6008 if (width < 1) { 6009 width = 0; 6010 } 6011 6012 int physicalWidth = width; 6013 6014 if (mHorizontallyScrolling) { 6015 width = VERY_WIDE; 6016 } 6017 6018 makeNewLayout(width, physicalWidth, UNKNOWN_BORING, UNKNOWN_BORING, 6019 physicalWidth, false); 6020 } 6021 6022 @Override 6023 protected void resetResolvedLayoutDirection() { 6024 super.resetResolvedLayoutDirection(); 6025 6026 if (mLayoutAlignment != null && 6027 (mTextAlign == TextAlign.VIEW_START || 6028 mTextAlign == TextAlign.VIEW_END)) { 6029 mLayoutAlignment = null; 6030 } 6031 } 6032 6033 private Layout.Alignment getLayoutAlignment() { 6034 if (mLayoutAlignment == null) { 6035 Layout.Alignment alignment; 6036 TextAlign textAlign = mTextAlign; 6037 switch (textAlign) { 6038 case INHERIT: 6039 // fall through to gravity temporarily 6040 // intention is to inherit value through view hierarchy. 6041 case GRAVITY: 6042 switch (mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) { 6043 case Gravity.START: 6044 alignment = Layout.Alignment.ALIGN_NORMAL; 6045 break; 6046 case Gravity.END: 6047 alignment = Layout.Alignment.ALIGN_OPPOSITE; 6048 break; 6049 case Gravity.LEFT: 6050 alignment = Layout.Alignment.ALIGN_LEFT; 6051 break; 6052 case Gravity.RIGHT: 6053 alignment = Layout.Alignment.ALIGN_RIGHT; 6054 break; 6055 case Gravity.CENTER_HORIZONTAL: 6056 alignment = Layout.Alignment.ALIGN_CENTER; 6057 break; 6058 default: 6059 alignment = Layout.Alignment.ALIGN_NORMAL; 6060 break; 6061 } 6062 break; 6063 case TEXT_START: 6064 alignment = Layout.Alignment.ALIGN_NORMAL; 6065 break; 6066 case TEXT_END: 6067 alignment = Layout.Alignment.ALIGN_OPPOSITE; 6068 break; 6069 case CENTER: 6070 alignment = Layout.Alignment.ALIGN_CENTER; 6071 break; 6072 case VIEW_START: 6073 alignment = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ? 6074 Layout.Alignment.ALIGN_RIGHT : Layout.Alignment.ALIGN_LEFT; 6075 break; 6076 case VIEW_END: 6077 alignment = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ? 6078 Layout.Alignment.ALIGN_LEFT : Layout.Alignment.ALIGN_RIGHT; 6079 break; 6080 default: 6081 alignment = Layout.Alignment.ALIGN_NORMAL; 6082 break; 6083 } 6084 mLayoutAlignment = alignment; 6085 } 6086 return mLayoutAlignment; 6087 } 6088 6089 /** 6090 * The width passed in is now the desired layout width, 6091 * not the full view width with padding. 6092 * {@hide} 6093 */ 6094 protected void makeNewLayout(int w, int hintWidth, 6095 BoringLayout.Metrics boring, 6096 BoringLayout.Metrics hintBoring, 6097 int ellipsisWidth, boolean bringIntoView) { 6098 stopMarquee(); 6099 6100 // Update "old" cached values 6101 mOldMaximum = mMaximum; 6102 mOldMaxMode = mMaxMode; 6103 6104 mHighlightPathBogus = true; 6105 6106 if (w < 0) { 6107 w = 0; 6108 } 6109 if (hintWidth < 0) { 6110 hintWidth = 0; 6111 } 6112 6113 Layout.Alignment alignment = getLayoutAlignment(); 6114 boolean shouldEllipsize = mEllipsize != null && mInput == null; 6115 final boolean switchEllipsize = mEllipsize == TruncateAt.MARQUEE && 6116 mMarqueeFadeMode != MARQUEE_FADE_NORMAL; 6117 TruncateAt effectiveEllipsize = mEllipsize; 6118 if (mEllipsize == TruncateAt.MARQUEE && 6119 mMarqueeFadeMode == MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { 6120 effectiveEllipsize = TruncateAt.END; 6121 } 6122 6123 if (mTextDir == null) { 6124 resolveTextDirection(); 6125 } 6126 6127 mLayout = makeSingleLayout(w, boring, ellipsisWidth, alignment, shouldEllipsize, 6128 effectiveEllipsize, effectiveEllipsize == mEllipsize); 6129 if (switchEllipsize) { 6130 TruncateAt oppositeEllipsize = effectiveEllipsize == TruncateAt.MARQUEE ? 6131 TruncateAt.END : TruncateAt.MARQUEE; 6132 mSavedMarqueeModeLayout = makeSingleLayout(w, boring, ellipsisWidth, alignment, 6133 shouldEllipsize, oppositeEllipsize, effectiveEllipsize != mEllipsize); 6134 } 6135 6136 shouldEllipsize = mEllipsize != null; 6137 mHintLayout = null; 6138 6139 if (mHint != null) { 6140 if (shouldEllipsize) hintWidth = w; 6141 6142 if (hintBoring == UNKNOWN_BORING) { 6143 hintBoring = BoringLayout.isBoring(mHint, mTextPaint, mTextDir, 6144 mHintBoring); 6145 if (hintBoring != null) { 6146 mHintBoring = hintBoring; 6147 } 6148 } 6149 6150 if (hintBoring != null) { 6151 if (hintBoring.width <= hintWidth && 6152 (!shouldEllipsize || hintBoring.width <= ellipsisWidth)) { 6153 if (mSavedHintLayout != null) { 6154 mHintLayout = mSavedHintLayout. 6155 replaceOrMake(mHint, mTextPaint, 6156 hintWidth, alignment, mSpacingMult, mSpacingAdd, 6157 hintBoring, mIncludePad); 6158 } else { 6159 mHintLayout = BoringLayout.make(mHint, mTextPaint, 6160 hintWidth, alignment, mSpacingMult, mSpacingAdd, 6161 hintBoring, mIncludePad); 6162 } 6163 6164 mSavedHintLayout = (BoringLayout) mHintLayout; 6165 } else if (shouldEllipsize && hintBoring.width <= hintWidth) { 6166 if (mSavedHintLayout != null) { 6167 mHintLayout = mSavedHintLayout. 6168 replaceOrMake(mHint, mTextPaint, 6169 hintWidth, alignment, mSpacingMult, mSpacingAdd, 6170 hintBoring, mIncludePad, mEllipsize, 6171 ellipsisWidth); 6172 } else { 6173 mHintLayout = BoringLayout.make(mHint, mTextPaint, 6174 hintWidth, alignment, mSpacingMult, mSpacingAdd, 6175 hintBoring, mIncludePad, mEllipsize, 6176 ellipsisWidth); 6177 } 6178 } else if (shouldEllipsize) { 6179 mHintLayout = new StaticLayout(mHint, 6180 0, mHint.length(), 6181 mTextPaint, hintWidth, alignment, mTextDir, mSpacingMult, 6182 mSpacingAdd, mIncludePad, mEllipsize, 6183 ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE); 6184 } else { 6185 mHintLayout = new StaticLayout(mHint, mTextPaint, 6186 hintWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, 6187 mIncludePad); 6188 } 6189 } else if (shouldEllipsize) { 6190 mHintLayout = new StaticLayout(mHint, 6191 0, mHint.length(), 6192 mTextPaint, hintWidth, alignment, mTextDir, mSpacingMult, 6193 mSpacingAdd, mIncludePad, mEllipsize, 6194 ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE); 6195 } else { 6196 mHintLayout = new StaticLayout(mHint, mTextPaint, 6197 hintWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, 6198 mIncludePad); 6199 } 6200 } 6201 6202 if (bringIntoView) { 6203 registerForPreDraw(); 6204 } 6205 6206 if (mEllipsize == TextUtils.TruncateAt.MARQUEE) { 6207 if (!compressText(ellipsisWidth)) { 6208 final int height = mLayoutParams.height; 6209 // If the size of the view does not depend on the size of the text, try to 6210 // start the marquee immediately 6211 if (height != LayoutParams.WRAP_CONTENT && height != LayoutParams.MATCH_PARENT) { 6212 startMarquee(); 6213 } else { 6214 // Defer the start of the marquee until we know our width (see setFrame()) 6215 mRestartMarquee = true; 6216 } 6217 } 6218 } 6219 6220 // CursorControllers need a non-null mLayout 6221 prepareCursorControllers(); 6222 } 6223 6224 private Layout makeSingleLayout(int w, BoringLayout.Metrics boring, int ellipsisWidth, 6225 Layout.Alignment alignment, boolean shouldEllipsize, TruncateAt effectiveEllipsize, 6226 boolean useSaved) { 6227 Layout result = null; 6228 if (mText instanceof Spannable) { 6229 result = new DynamicLayout(mText, mTransformed, mTextPaint, w, 6230 alignment, mTextDir, mSpacingMult, 6231 mSpacingAdd, mIncludePad, mInput == null ? effectiveEllipsize : null, 6232 ellipsisWidth); 6233 } else { 6234 if (boring == UNKNOWN_BORING) { 6235 boring = BoringLayout.isBoring(mTransformed, mTextPaint, mTextDir, mBoring); 6236 if (boring != null) { 6237 mBoring = boring; 6238 } 6239 } 6240 6241 if (boring != null) { 6242 if (boring.width <= w && 6243 (effectiveEllipsize == null || boring.width <= ellipsisWidth)) { 6244 if (useSaved && mSavedLayout != null) { 6245 result = mSavedLayout.replaceOrMake(mTransformed, mTextPaint, 6246 w, alignment, mSpacingMult, mSpacingAdd, 6247 boring, mIncludePad); 6248 } else { 6249 result = BoringLayout.make(mTransformed, mTextPaint, 6250 w, alignment, mSpacingMult, mSpacingAdd, 6251 boring, mIncludePad); 6252 } 6253 6254 if (useSaved) { 6255 mSavedLayout = (BoringLayout) result; 6256 } 6257 } else if (shouldEllipsize && boring.width <= w) { 6258 if (useSaved && mSavedLayout != null) { 6259 result = mSavedLayout.replaceOrMake(mTransformed, mTextPaint, 6260 w, alignment, mSpacingMult, mSpacingAdd, 6261 boring, mIncludePad, effectiveEllipsize, 6262 ellipsisWidth); 6263 } else { 6264 result = BoringLayout.make(mTransformed, mTextPaint, 6265 w, alignment, mSpacingMult, mSpacingAdd, 6266 boring, mIncludePad, effectiveEllipsize, 6267 ellipsisWidth); 6268 } 6269 } else if (shouldEllipsize) { 6270 result = new StaticLayout(mTransformed, 6271 0, mTransformed.length(), 6272 mTextPaint, w, alignment, mTextDir, mSpacingMult, 6273 mSpacingAdd, mIncludePad, effectiveEllipsize, 6274 ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE); 6275 } else { 6276 result = new StaticLayout(mTransformed, mTextPaint, 6277 w, alignment, mTextDir, mSpacingMult, mSpacingAdd, 6278 mIncludePad); 6279 } 6280 } else if (shouldEllipsize) { 6281 result = new StaticLayout(mTransformed, 6282 0, mTransformed.length(), 6283 mTextPaint, w, alignment, mTextDir, mSpacingMult, 6284 mSpacingAdd, mIncludePad, effectiveEllipsize, 6285 ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE); 6286 } else { 6287 result = new StaticLayout(mTransformed, mTextPaint, 6288 w, alignment, mTextDir, mSpacingMult, mSpacingAdd, 6289 mIncludePad); 6290 } 6291 } 6292 return result; 6293 } 6294 6295 private boolean compressText(float width) { 6296 if (isHardwareAccelerated()) return false; 6297 6298 // Only compress the text if it hasn't been compressed by the previous pass 6299 if (width > 0.0f && mLayout != null && getLineCount() == 1 && !mUserSetTextScaleX && 6300 mTextPaint.getTextScaleX() == 1.0f) { 6301 final float textWidth = mLayout.getLineWidth(0); 6302 final float overflow = (textWidth + 1.0f - width) / width; 6303 if (overflow > 0.0f && overflow <= Marquee.MARQUEE_DELTA_MAX) { 6304 mTextPaint.setTextScaleX(1.0f - overflow - 0.005f); 6305 post(new Runnable() { 6306 public void run() { 6307 requestLayout(); 6308 } 6309 }); 6310 return true; 6311 } 6312 } 6313 6314 return false; 6315 } 6316 6317 private static int desired(Layout layout) { 6318 int n = layout.getLineCount(); 6319 CharSequence text = layout.getText(); 6320 float max = 0; 6321 6322 // if any line was wrapped, we can't use it. 6323 // but it's ok for the last line not to have a newline 6324 6325 for (int i = 0; i < n - 1; i++) { 6326 if (text.charAt(layout.getLineEnd(i) - 1) != '\n') 6327 return -1; 6328 } 6329 6330 for (int i = 0; i < n; i++) { 6331 max = Math.max(max, layout.getLineWidth(i)); 6332 } 6333 6334 return (int) FloatMath.ceil(max); 6335 } 6336 6337 /** 6338 * Set whether the TextView includes extra top and bottom padding to make 6339 * room for accents that go above the normal ascent and descent. 6340 * The default is true. 6341 * 6342 * @attr ref android.R.styleable#TextView_includeFontPadding 6343 */ 6344 public void setIncludeFontPadding(boolean includepad) { 6345 if (mIncludePad != includepad) { 6346 mIncludePad = includepad; 6347 6348 if (mLayout != null) { 6349 nullLayouts(); 6350 requestLayout(); 6351 invalidate(); 6352 } 6353 } 6354 } 6355 6356 private static final BoringLayout.Metrics UNKNOWN_BORING = new BoringLayout.Metrics(); 6357 6358 @Override 6359 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 6360 int widthMode = MeasureSpec.getMode(widthMeasureSpec); 6361 int heightMode = MeasureSpec.getMode(heightMeasureSpec); 6362 int widthSize = MeasureSpec.getSize(widthMeasureSpec); 6363 int heightSize = MeasureSpec.getSize(heightMeasureSpec); 6364 6365 int width; 6366 int height; 6367 6368 BoringLayout.Metrics boring = UNKNOWN_BORING; 6369 BoringLayout.Metrics hintBoring = UNKNOWN_BORING; 6370 6371 if (mTextDir == null) { 6372 resolveTextDirection(); 6373 } 6374 6375 int des = -1; 6376 boolean fromexisting = false; 6377 6378 if (widthMode == MeasureSpec.EXACTLY) { 6379 // Parent has told us how big to be. So be it. 6380 width = widthSize; 6381 } else { 6382 if (mLayout != null && mEllipsize == null) { 6383 des = desired(mLayout); 6384 } 6385 6386 if (des < 0) { 6387 boring = BoringLayout.isBoring(mTransformed, mTextPaint, mTextDir, mBoring); 6388 if (boring != null) { 6389 mBoring = boring; 6390 } 6391 } else { 6392 fromexisting = true; 6393 } 6394 6395 if (boring == null || boring == UNKNOWN_BORING) { 6396 if (des < 0) { 6397 des = (int) FloatMath.ceil(Layout.getDesiredWidth(mTransformed, mTextPaint)); 6398 } 6399 6400 width = des; 6401 } else { 6402 width = boring.width; 6403 } 6404 6405 final Drawables dr = mDrawables; 6406 if (dr != null) { 6407 width = Math.max(width, dr.mDrawableWidthTop); 6408 width = Math.max(width, dr.mDrawableWidthBottom); 6409 } 6410 6411 if (mHint != null) { 6412 int hintDes = -1; 6413 int hintWidth; 6414 6415 if (mHintLayout != null && mEllipsize == null) { 6416 hintDes = desired(mHintLayout); 6417 } 6418 6419 if (hintDes < 0) { 6420 hintBoring = BoringLayout.isBoring(mHint, mTextPaint, mHintBoring); 6421 if (hintBoring != null) { 6422 mHintBoring = hintBoring; 6423 } 6424 } 6425 6426 if (hintBoring == null || hintBoring == UNKNOWN_BORING) { 6427 if (hintDes < 0) { 6428 hintDes = (int) FloatMath.ceil( 6429 Layout.getDesiredWidth(mHint, mTextPaint)); 6430 } 6431 6432 hintWidth = hintDes; 6433 } else { 6434 hintWidth = hintBoring.width; 6435 } 6436 6437 if (hintWidth > width) { 6438 width = hintWidth; 6439 } 6440 } 6441 6442 width += getCompoundPaddingLeft() + getCompoundPaddingRight(); 6443 6444 if (mMaxWidthMode == EMS) { 6445 width = Math.min(width, mMaxWidth * getLineHeight()); 6446 } else { 6447 width = Math.min(width, mMaxWidth); 6448 } 6449 6450 if (mMinWidthMode == EMS) { 6451 width = Math.max(width, mMinWidth * getLineHeight()); 6452 } else { 6453 width = Math.max(width, mMinWidth); 6454 } 6455 6456 // Check against our minimum width 6457 width = Math.max(width, getSuggestedMinimumWidth()); 6458 6459 if (widthMode == MeasureSpec.AT_MOST) { 6460 width = Math.min(widthSize, width); 6461 } 6462 } 6463 6464 int want = width - getCompoundPaddingLeft() - getCompoundPaddingRight(); 6465 int unpaddedWidth = want; 6466 6467 if (mHorizontallyScrolling) want = VERY_WIDE; 6468 6469 int hintWant = want; 6470 int hintWidth = (mHintLayout == null) ? hintWant : mHintLayout.getWidth(); 6471 6472 if (mLayout == null) { 6473 makeNewLayout(want, hintWant, boring, hintBoring, 6474 width - getCompoundPaddingLeft() - getCompoundPaddingRight(), false); 6475 } else { 6476 final boolean layoutChanged = (mLayout.getWidth() != want) || 6477 (hintWidth != hintWant) || 6478 (mLayout.getEllipsizedWidth() != 6479 width - getCompoundPaddingLeft() - getCompoundPaddingRight()); 6480 6481 final boolean widthChanged = (mHint == null) && 6482 (mEllipsize == null) && 6483 (want > mLayout.getWidth()) && 6484 (mLayout instanceof BoringLayout || (fromexisting && des >= 0 && des <= want)); 6485 6486 final boolean maximumChanged = (mMaxMode != mOldMaxMode) || (mMaximum != mOldMaximum); 6487 6488 if (layoutChanged || maximumChanged) { 6489 if (!maximumChanged && widthChanged) { 6490 mLayout.increaseWidthTo(want); 6491 } else { 6492 makeNewLayout(want, hintWant, boring, hintBoring, 6493 width - getCompoundPaddingLeft() - getCompoundPaddingRight(), false); 6494 } 6495 } else { 6496 // Nothing has changed 6497 } 6498 } 6499 6500 if (heightMode == MeasureSpec.EXACTLY) { 6501 // Parent has told us how big to be. So be it. 6502 height = heightSize; 6503 mDesiredHeightAtMeasure = -1; 6504 } else { 6505 int desired = getDesiredHeight(); 6506 6507 height = desired; 6508 mDesiredHeightAtMeasure = desired; 6509 6510 if (heightMode == MeasureSpec.AT_MOST) { 6511 height = Math.min(desired, heightSize); 6512 } 6513 } 6514 6515 int unpaddedHeight = height - getCompoundPaddingTop() - getCompoundPaddingBottom(); 6516 if (mMaxMode == LINES && mLayout.getLineCount() > mMaximum) { 6517 unpaddedHeight = Math.min(unpaddedHeight, mLayout.getLineTop(mMaximum)); 6518 } 6519 6520 /* 6521 * We didn't let makeNewLayout() register to bring the cursor into view, 6522 * so do it here if there is any possibility that it is needed. 6523 */ 6524 if (mMovement != null || 6525 mLayout.getWidth() > unpaddedWidth || 6526 mLayout.getHeight() > unpaddedHeight) { 6527 registerForPreDraw(); 6528 } else { 6529 scrollTo(0, 0); 6530 } 6531 6532 setMeasuredDimension(width, height); 6533 } 6534 6535 private int getDesiredHeight() { 6536 return Math.max( 6537 getDesiredHeight(mLayout, true), 6538 getDesiredHeight(mHintLayout, mEllipsize != null)); 6539 } 6540 6541 private int getDesiredHeight(Layout layout, boolean cap) { 6542 if (layout == null) { 6543 return 0; 6544 } 6545 6546 int linecount = layout.getLineCount(); 6547 int pad = getCompoundPaddingTop() + getCompoundPaddingBottom(); 6548 int desired = layout.getLineTop(linecount); 6549 6550 final Drawables dr = mDrawables; 6551 if (dr != null) { 6552 desired = Math.max(desired, dr.mDrawableHeightLeft); 6553 desired = Math.max(desired, dr.mDrawableHeightRight); 6554 } 6555 6556 desired += pad; 6557 6558 if (mMaxMode == LINES) { 6559 /* 6560 * Don't cap the hint to a certain number of lines. 6561 * (Do cap it, though, if we have a maximum pixel height.) 6562 */ 6563 if (cap) { 6564 if (linecount > mMaximum) { 6565 desired = layout.getLineTop(mMaximum); 6566 6567 if (dr != null) { 6568 desired = Math.max(desired, dr.mDrawableHeightLeft); 6569 desired = Math.max(desired, dr.mDrawableHeightRight); 6570 } 6571 6572 desired += pad; 6573 linecount = mMaximum; 6574 } 6575 } 6576 } else { 6577 desired = Math.min(desired, mMaximum); 6578 } 6579 6580 if (mMinMode == LINES) { 6581 if (linecount < mMinimum) { 6582 desired += getLineHeight() * (mMinimum - linecount); 6583 } 6584 } else { 6585 desired = Math.max(desired, mMinimum); 6586 } 6587 6588 // Check against our minimum height 6589 desired = Math.max(desired, getSuggestedMinimumHeight()); 6590 6591 return desired; 6592 } 6593 6594 /** 6595 * Check whether a change to the existing text layout requires a 6596 * new view layout. 6597 */ 6598 private void checkForResize() { 6599 boolean sizeChanged = false; 6600 6601 if (mLayout != null) { 6602 // Check if our width changed 6603 if (mLayoutParams.width == LayoutParams.WRAP_CONTENT) { 6604 sizeChanged = true; 6605 invalidate(); 6606 } 6607 6608 // Check if our height changed 6609 if (mLayoutParams.height == LayoutParams.WRAP_CONTENT) { 6610 int desiredHeight = getDesiredHeight(); 6611 6612 if (desiredHeight != this.getHeight()) { 6613 sizeChanged = true; 6614 } 6615 } else if (mLayoutParams.height == LayoutParams.MATCH_PARENT) { 6616 if (mDesiredHeightAtMeasure >= 0) { 6617 int desiredHeight = getDesiredHeight(); 6618 6619 if (desiredHeight != mDesiredHeightAtMeasure) { 6620 sizeChanged = true; 6621 } 6622 } 6623 } 6624 } 6625 6626 if (sizeChanged) { 6627 requestLayout(); 6628 // caller will have already invalidated 6629 } 6630 } 6631 6632 /** 6633 * Check whether entirely new text requires a new view layout 6634 * or merely a new text layout. 6635 */ 6636 private void checkForRelayout() { 6637 // If we have a fixed width, we can just swap in a new text layout 6638 // if the text height stays the same or if the view height is fixed. 6639 6640 if ((mLayoutParams.width != LayoutParams.WRAP_CONTENT || 6641 (mMaxWidthMode == mMinWidthMode && mMaxWidth == mMinWidth)) && 6642 (mHint == null || mHintLayout != null) && 6643 (mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight() > 0)) { 6644 // Static width, so try making a new text layout. 6645 6646 int oldht = mLayout.getHeight(); 6647 int want = mLayout.getWidth(); 6648 int hintWant = mHintLayout == null ? 0 : mHintLayout.getWidth(); 6649 6650 /* 6651 * No need to bring the text into view, since the size is not 6652 * changing (unless we do the requestLayout(), in which case it 6653 * will happen at measure). 6654 */ 6655 makeNewLayout(want, hintWant, UNKNOWN_BORING, UNKNOWN_BORING, 6656 mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(), 6657 false); 6658 6659 if (mEllipsize != TextUtils.TruncateAt.MARQUEE) { 6660 // In a fixed-height view, so use our new text layout. 6661 if (mLayoutParams.height != LayoutParams.WRAP_CONTENT && 6662 mLayoutParams.height != LayoutParams.MATCH_PARENT) { 6663 invalidate(); 6664 return; 6665 } 6666 6667 // Dynamic height, but height has stayed the same, 6668 // so use our new text layout. 6669 if (mLayout.getHeight() == oldht && 6670 (mHintLayout == null || mHintLayout.getHeight() == oldht)) { 6671 invalidate(); 6672 return; 6673 } 6674 } 6675 6676 // We lose: the height has changed and we have a dynamic height. 6677 // Request a new view layout using our new text layout. 6678 requestLayout(); 6679 invalidate(); 6680 } else { 6681 // Dynamic width, so we have no choice but to request a new 6682 // view layout with a new text layout. 6683 nullLayouts(); 6684 requestLayout(); 6685 invalidate(); 6686 } 6687 } 6688 6689 /** 6690 * Returns true if anything changed. 6691 */ 6692 private boolean bringTextIntoView() { 6693 int line = 0; 6694 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { 6695 line = mLayout.getLineCount() - 1; 6696 } 6697 6698 Layout.Alignment a = mLayout.getParagraphAlignment(line); 6699 int dir = mLayout.getParagraphDirection(line); 6700 int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); 6701 int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom(); 6702 int ht = mLayout.getHeight(); 6703 6704 int scrollx, scrolly; 6705 6706 // Convert to left, center, or right alignment. 6707 if (a == Layout.Alignment.ALIGN_NORMAL) { 6708 a = dir == Layout.DIR_LEFT_TO_RIGHT ? Layout.Alignment.ALIGN_LEFT : 6709 Layout.Alignment.ALIGN_RIGHT; 6710 } else if (a == Layout.Alignment.ALIGN_OPPOSITE){ 6711 a = dir == Layout.DIR_LEFT_TO_RIGHT ? Layout.Alignment.ALIGN_RIGHT : 6712 Layout.Alignment.ALIGN_LEFT; 6713 } 6714 6715 if (a == Layout.Alignment.ALIGN_CENTER) { 6716 /* 6717 * Keep centered if possible, or, if it is too wide to fit, 6718 * keep leading edge in view. 6719 */ 6720 6721 int left = (int) FloatMath.floor(mLayout.getLineLeft(line)); 6722 int right = (int) FloatMath.ceil(mLayout.getLineRight(line)); 6723 6724 if (right - left < hspace) { 6725 scrollx = (right + left) / 2 - hspace / 2; 6726 } else { 6727 if (dir < 0) { 6728 scrollx = right - hspace; 6729 } else { 6730 scrollx = left; 6731 } 6732 } 6733 } else if (a == Layout.Alignment.ALIGN_RIGHT) { 6734 int right = (int) FloatMath.ceil(mLayout.getLineRight(line)); 6735 scrollx = right - hspace; 6736 } else { // a == Layout.Alignment.ALIGN_LEFT (will also be the default) 6737 scrollx = (int) FloatMath.floor(mLayout.getLineLeft(line)); 6738 } 6739 6740 if (ht < vspace) { 6741 scrolly = 0; 6742 } else { 6743 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { 6744 scrolly = ht - vspace; 6745 } else { 6746 scrolly = 0; 6747 } 6748 } 6749 6750 if (scrollx != mScrollX || scrolly != mScrollY) { 6751 scrollTo(scrollx, scrolly); 6752 return true; 6753 } else { 6754 return false; 6755 } 6756 } 6757 6758 /** 6759 * Move the point, specified by the offset, into the view if it is needed. 6760 * This has to be called after layout. Returns true if anything changed. 6761 */ 6762 public boolean bringPointIntoView(int offset) { 6763 boolean changed = false; 6764 6765 if (mLayout == null) return changed; 6766 6767 int line = mLayout.getLineForOffset(offset); 6768 6769 // FIXME: Is it okay to truncate this, or should we round? 6770 final int x = (int)mLayout.getPrimaryHorizontal(offset); 6771 final int top = mLayout.getLineTop(line); 6772 final int bottom = mLayout.getLineTop(line + 1); 6773 6774 int left = (int) FloatMath.floor(mLayout.getLineLeft(line)); 6775 int right = (int) FloatMath.ceil(mLayout.getLineRight(line)); 6776 int ht = mLayout.getHeight(); 6777 6778 int grav; 6779 6780 switch (mLayout.getParagraphAlignment(line)) { 6781 case ALIGN_LEFT: 6782 grav = 1; 6783 break; 6784 case ALIGN_RIGHT: 6785 grav = -1; 6786 break; 6787 case ALIGN_NORMAL: 6788 grav = mLayout.getParagraphDirection(line); 6789 break; 6790 case ALIGN_OPPOSITE: 6791 grav = -mLayout.getParagraphDirection(line); 6792 break; 6793 case ALIGN_CENTER: 6794 default: 6795 grav = 0; 6796 break; 6797 } 6798 6799 int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); 6800 int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom(); 6801 6802 int hslack = (bottom - top) / 2; 6803 int vslack = hslack; 6804 6805 if (vslack > vspace / 4) 6806 vslack = vspace / 4; 6807 if (hslack > hspace / 4) 6808 hslack = hspace / 4; 6809 6810 int hs = mScrollX; 6811 int vs = mScrollY; 6812 6813 if (top - vs < vslack) 6814 vs = top - vslack; 6815 if (bottom - vs > vspace - vslack) 6816 vs = bottom - (vspace - vslack); 6817 if (ht - vs < vspace) 6818 vs = ht - vspace; 6819 if (0 - vs > 0) 6820 vs = 0; 6821 6822 if (grav != 0) { 6823 if (x - hs < hslack) { 6824 hs = x - hslack; 6825 } 6826 if (x - hs > hspace - hslack) { 6827 hs = x - (hspace - hslack); 6828 } 6829 } 6830 6831 if (grav < 0) { 6832 if (left - hs > 0) 6833 hs = left; 6834 if (right - hs < hspace) 6835 hs = right - hspace; 6836 } else if (grav > 0) { 6837 if (right - hs < hspace) 6838 hs = right - hspace; 6839 if (left - hs > 0) 6840 hs = left; 6841 } else /* grav == 0 */ { 6842 if (right - left <= hspace) { 6843 /* 6844 * If the entire text fits, center it exactly. 6845 */ 6846 hs = left - (hspace - (right - left)) / 2; 6847 } else if (x > right - hslack) { 6848 /* 6849 * If we are near the right edge, keep the right edge 6850 * at the edge of the view. 6851 */ 6852 hs = right - hspace; 6853 } else if (x < left + hslack) { 6854 /* 6855 * If we are near the left edge, keep the left edge 6856 * at the edge of the view. 6857 */ 6858 hs = left; 6859 } else if (left > hs) { 6860 /* 6861 * Is there whitespace visible at the left? Fix it if so. 6862 */ 6863 hs = left; 6864 } else if (right < hs + hspace) { 6865 /* 6866 * Is there whitespace visible at the right? Fix it if so. 6867 */ 6868 hs = right - hspace; 6869 } else { 6870 /* 6871 * Otherwise, float as needed. 6872 */ 6873 if (x - hs < hslack) { 6874 hs = x - hslack; 6875 } 6876 if (x - hs > hspace - hslack) { 6877 hs = x - (hspace - hslack); 6878 } 6879 } 6880 } 6881 6882 if (hs != mScrollX || vs != mScrollY) { 6883 if (mScroller == null) { 6884 scrollTo(hs, vs); 6885 } else { 6886 long duration = AnimationUtils.currentAnimationTimeMillis() - mLastScroll; 6887 int dx = hs - mScrollX; 6888 int dy = vs - mScrollY; 6889 6890 if (duration > ANIMATED_SCROLL_GAP) { 6891 mScroller.startScroll(mScrollX, mScrollY, dx, dy); 6892 awakenScrollBars(mScroller.getDuration()); 6893 invalidate(); 6894 } else { 6895 if (!mScroller.isFinished()) { 6896 mScroller.abortAnimation(); 6897 } 6898 6899 scrollBy(dx, dy); 6900 } 6901 6902 mLastScroll = AnimationUtils.currentAnimationTimeMillis(); 6903 } 6904 6905 changed = true; 6906 } 6907 6908 if (isFocused()) { 6909 // This offsets because getInterestingRect() is in terms of viewport coordinates, but 6910 // requestRectangleOnScreen() is in terms of content coordinates. 6911 6912 if (mTempRect == null) mTempRect = new Rect(); 6913 // The offsets here are to ensure the rectangle we are using is 6914 // within our view bounds, in case the cursor is on the far left 6915 // or right. If it isn't withing the bounds, then this request 6916 // will be ignored. 6917 mTempRect.set(x - 2, top, x + 2, bottom); 6918 getInterestingRect(mTempRect, line); 6919 mTempRect.offset(mScrollX, mScrollY); 6920 6921 if (requestRectangleOnScreen(mTempRect)) { 6922 changed = true; 6923 } 6924 } 6925 6926 return changed; 6927 } 6928 6929 /** 6930 * Move the cursor, if needed, so that it is at an offset that is visible 6931 * to the user. This will not move the cursor if it represents more than 6932 * one character (a selection range). This will only work if the 6933 * TextView contains spannable text; otherwise it will do nothing. 6934 * 6935 * @return True if the cursor was actually moved, false otherwise. 6936 */ 6937 public boolean moveCursorToVisibleOffset() { 6938 if (!(mText instanceof Spannable)) { 6939 return false; 6940 } 6941 int start = getSelectionStart(); 6942 int end = getSelectionEnd(); 6943 if (start != end) { 6944 return false; 6945 } 6946 6947 // First: make sure the line is visible on screen: 6948 6949 int line = mLayout.getLineForOffset(start); 6950 6951 final int top = mLayout.getLineTop(line); 6952 final int bottom = mLayout.getLineTop(line + 1); 6953 final int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom(); 6954 int vslack = (bottom - top) / 2; 6955 if (vslack > vspace / 4) 6956 vslack = vspace / 4; 6957 final int vs = mScrollY; 6958 6959 if (top < (vs+vslack)) { 6960 line = mLayout.getLineForVertical(vs+vslack+(bottom-top)); 6961 } else if (bottom > (vspace+vs-vslack)) { 6962 line = mLayout.getLineForVertical(vspace+vs-vslack-(bottom-top)); 6963 } 6964 6965 // Next: make sure the character is visible on screen: 6966 6967 final int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); 6968 final int hs = mScrollX; 6969 final int leftChar = mLayout.getOffsetForHorizontal(line, hs); 6970 final int rightChar = mLayout.getOffsetForHorizontal(line, hspace+hs); 6971 6972 // line might contain bidirectional text 6973 final int lowChar = leftChar < rightChar ? leftChar : rightChar; 6974 final int highChar = leftChar > rightChar ? leftChar : rightChar; 6975 6976 int newStart = start; 6977 if (newStart < lowChar) { 6978 newStart = lowChar; 6979 } else if (newStart > highChar) { 6980 newStart = highChar; 6981 } 6982 6983 if (newStart != start) { 6984 Selection.setSelection((Spannable)mText, newStart); 6985 return true; 6986 } 6987 6988 return false; 6989 } 6990 6991 @Override 6992 public void computeScroll() { 6993 if (mScroller != null) { 6994 if (mScroller.computeScrollOffset()) { 6995 mScrollX = mScroller.getCurrX(); 6996 mScrollY = mScroller.getCurrY(); 6997 invalidateParentCaches(); 6998 postInvalidate(); // So we draw again 6999 } 7000 } 7001 } 7002 7003 private void getInterestingRect(Rect r, int line) { 7004 convertFromViewportToContentCoordinates(r); 7005 7006 // Rectangle can can be expanded on first and last line to take 7007 // padding into account. 7008 // TODO Take left/right padding into account too? 7009 if (line == 0) r.top -= getExtendedPaddingTop(); 7010 if (line == mLayout.getLineCount() - 1) r.bottom += getExtendedPaddingBottom(); 7011 } 7012 7013 private void convertFromViewportToContentCoordinates(Rect r) { 7014 final int horizontalOffset = viewportToContentHorizontalOffset(); 7015 r.left += horizontalOffset; 7016 r.right += horizontalOffset; 7017 7018 final int verticalOffset = viewportToContentVerticalOffset(); 7019 r.top += verticalOffset; 7020 r.bottom += verticalOffset; 7021 } 7022 7023 private int viewportToContentHorizontalOffset() { 7024 return getCompoundPaddingLeft() - mScrollX; 7025 } 7026 7027 private int viewportToContentVerticalOffset() { 7028 int offset = getExtendedPaddingTop() - mScrollY; 7029 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { 7030 offset += getVerticalOffset(false); 7031 } 7032 return offset; 7033 } 7034 7035 @Override 7036 public void debug(int depth) { 7037 super.debug(depth); 7038 7039 String output = debugIndent(depth); 7040 output += "frame={" + mLeft + ", " + mTop + ", " + mRight 7041 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY 7042 + "} "; 7043 7044 if (mText != null) { 7045 7046 output += "mText=\"" + mText + "\" "; 7047 if (mLayout != null) { 7048 output += "mLayout width=" + mLayout.getWidth() 7049 + " height=" + mLayout.getHeight(); 7050 } 7051 } else { 7052 output += "mText=NULL"; 7053 } 7054 Log.d(VIEW_LOG_TAG, output); 7055 } 7056 7057 /** 7058 * Convenience for {@link Selection#getSelectionStart}. 7059 */ 7060 @ViewDebug.ExportedProperty(category = "text") 7061 public int getSelectionStart() { 7062 return Selection.getSelectionStart(getText()); 7063 } 7064 7065 /** 7066 * Convenience for {@link Selection#getSelectionEnd}. 7067 */ 7068 @ViewDebug.ExportedProperty(category = "text") 7069 public int getSelectionEnd() { 7070 return Selection.getSelectionEnd(getText()); 7071 } 7072 7073 /** 7074 * Return true iff there is a selection inside this text view. 7075 */ 7076 public boolean hasSelection() { 7077 final int selectionStart = getSelectionStart(); 7078 final int selectionEnd = getSelectionEnd(); 7079 7080 return selectionStart >= 0 && selectionStart != selectionEnd; 7081 } 7082 7083 /** 7084 * Sets the properties of this field (lines, horizontally scrolling, 7085 * transformation method) to be for a single-line input. 7086 * 7087 * @attr ref android.R.styleable#TextView_singleLine 7088 */ 7089 public void setSingleLine() { 7090 setSingleLine(true); 7091 } 7092 7093 /** 7094 * Sets the properties of this field to transform input to ALL CAPS 7095 * display. This may use a "small caps" formatting if available. 7096 * This setting will be ignored if this field is editable or selectable. 7097 * 7098 * This call replaces the current transformation method. Disabling this 7099 * will not necessarily restore the previous behavior from before this 7100 * was enabled. 7101 * 7102 * @see #setTransformationMethod(TransformationMethod) 7103 * @attr ref android.R.styleable#TextView_textAllCaps 7104 */ 7105 public void setAllCaps(boolean allCaps) { 7106 if (allCaps) { 7107 setTransformationMethod(new AllCapsTransformationMethod(getContext())); 7108 } else { 7109 setTransformationMethod(null); 7110 } 7111 } 7112 7113 /** 7114 * If true, sets the properties of this field (number of lines, horizontally scrolling, 7115 * transformation method) to be for a single-line input; if false, restores these to the default 7116 * conditions. 7117 * 7118 * Note that the default conditions are not necessarily those that were in effect prior this 7119 * method, and you may want to reset these properties to your custom values. 7120 * 7121 * @attr ref android.R.styleable#TextView_singleLine 7122 */ 7123 @android.view.RemotableViewMethod 7124 public void setSingleLine(boolean singleLine) { 7125 // Could be used, but may break backward compatibility. 7126 // if (mSingleLine == singleLine) return; 7127 setInputTypeSingleLine(singleLine); 7128 applySingleLine(singleLine, true, true); 7129 } 7130 7131 /** 7132 * Adds or remove the EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE on the mInputType. 7133 * @param singleLine 7134 */ 7135 private void setInputTypeSingleLine(boolean singleLine) { 7136 if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { 7137 if (singleLine) { 7138 mInputType &= ~EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; 7139 } else { 7140 mInputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; 7141 } 7142 } 7143 } 7144 7145 private void applySingleLine(boolean singleLine, boolean applyTransformation, 7146 boolean changeMaxLines) { 7147 mSingleLine = singleLine; 7148 if (singleLine) { 7149 setLines(1); 7150 setHorizontallyScrolling(true); 7151 if (applyTransformation) { 7152 setTransformationMethod(SingleLineTransformationMethod.getInstance()); 7153 } 7154 } else { 7155 if (changeMaxLines) { 7156 setMaxLines(Integer.MAX_VALUE); 7157 } 7158 setHorizontallyScrolling(false); 7159 if (applyTransformation) { 7160 setTransformationMethod(null); 7161 } 7162 } 7163 } 7164 7165 /** 7166 * Causes words in the text that are longer than the view is wide 7167 * to be ellipsized instead of broken in the middle. You may also 7168 * want to {@link #setSingleLine} or {@link #setHorizontallyScrolling} 7169 * to constrain the text to a single line. Use <code>null</code> 7170 * to turn off ellipsizing. 7171 * 7172 * If {@link #setMaxLines} has been used to set two or more lines, 7173 * {@link android.text.TextUtils.TruncateAt#END} and 7174 * {@link android.text.TextUtils.TruncateAt#MARQUEE}* are only supported 7175 * (other ellipsizing types will not do anything). 7176 * 7177 * @attr ref android.R.styleable#TextView_ellipsize 7178 */ 7179 public void setEllipsize(TextUtils.TruncateAt where) { 7180 // TruncateAt is an enum. != comparison is ok between these singleton objects. 7181 if (mEllipsize != where) { 7182 mEllipsize = where; 7183 7184 if (mLayout != null) { 7185 nullLayouts(); 7186 requestLayout(); 7187 invalidate(); 7188 } 7189 } 7190 } 7191 7192 /** 7193 * Sets how many times to repeat the marquee animation. Only applied if the 7194 * TextView has marquee enabled. Set to -1 to repeat indefinitely. 7195 * 7196 * @attr ref android.R.styleable#TextView_marqueeRepeatLimit 7197 */ 7198 public void setMarqueeRepeatLimit(int marqueeLimit) { 7199 mMarqueeRepeatLimit = marqueeLimit; 7200 } 7201 7202 /** 7203 * Returns where, if anywhere, words that are longer than the view 7204 * is wide should be ellipsized. 7205 */ 7206 @ViewDebug.ExportedProperty 7207 public TextUtils.TruncateAt getEllipsize() { 7208 return mEllipsize; 7209 } 7210 7211 /** 7212 * Set the TextView so that when it takes focus, all the text is 7213 * selected. 7214 * 7215 * @attr ref android.R.styleable#TextView_selectAllOnFocus 7216 */ 7217 @android.view.RemotableViewMethod 7218 public void setSelectAllOnFocus(boolean selectAllOnFocus) { 7219 mSelectAllOnFocus = selectAllOnFocus; 7220 7221 if (selectAllOnFocus && !(mText instanceof Spannable)) { 7222 setText(mText, BufferType.SPANNABLE); 7223 } 7224 } 7225 7226 /** 7227 * Set whether the cursor is visible. The default is true. 7228 * 7229 * @attr ref android.R.styleable#TextView_cursorVisible 7230 */ 7231 @android.view.RemotableViewMethod 7232 public void setCursorVisible(boolean visible) { 7233 if (mCursorVisible != visible) { 7234 mCursorVisible = visible; 7235 invalidate(); 7236 7237 makeBlink(); 7238 7239 // InsertionPointCursorController depends on mCursorVisible 7240 prepareCursorControllers(); 7241 } 7242 } 7243 7244 private boolean isCursorVisible() { 7245 return mCursorVisible && isTextEditable(); 7246 } 7247 7248 private boolean canMarquee() { 7249 int width = (mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight()); 7250 return width > 0 && (mLayout.getLineWidth(0) > width || 7251 (mMarqueeFadeMode != MARQUEE_FADE_NORMAL && mSavedMarqueeModeLayout != null && 7252 mSavedMarqueeModeLayout.getLineWidth(0) > width)); 7253 } 7254 7255 private void startMarquee() { 7256 // Do not ellipsize EditText 7257 if (mInput != null) return; 7258 7259 if (compressText(getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight())) { 7260 return; 7261 } 7262 7263 if ((mMarquee == null || mMarquee.isStopped()) && (isFocused() || isSelected()) && 7264 getLineCount() == 1 && canMarquee()) { 7265 7266 if (mMarqueeFadeMode == MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { 7267 mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_FADE; 7268 final Layout tmp = mLayout; 7269 mLayout = mSavedMarqueeModeLayout; 7270 mSavedMarqueeModeLayout = tmp; 7271 setHorizontalFadingEdgeEnabled(true); 7272 requestLayout(); 7273 invalidate(); 7274 } 7275 7276 if (mMarquee == null) mMarquee = new Marquee(this); 7277 mMarquee.start(mMarqueeRepeatLimit); 7278 } 7279 } 7280 7281 private void stopMarquee() { 7282 if (mMarquee != null && !mMarquee.isStopped()) { 7283 mMarquee.stop(); 7284 } 7285 7286 if (mMarqueeFadeMode == MARQUEE_FADE_SWITCH_SHOW_FADE) { 7287 mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS; 7288 final Layout tmp = mSavedMarqueeModeLayout; 7289 mSavedMarqueeModeLayout = mLayout; 7290 mLayout = tmp; 7291 setHorizontalFadingEdgeEnabled(false); 7292 requestLayout(); 7293 invalidate(); 7294 } 7295 } 7296 7297 private void startStopMarquee(boolean start) { 7298 if (mEllipsize == TextUtils.TruncateAt.MARQUEE) { 7299 if (start) { 7300 startMarquee(); 7301 } else { 7302 stopMarquee(); 7303 } 7304 } 7305 } 7306 7307 private static final class Marquee extends Handler { 7308 // TODO: Add an option to configure this 7309 private static final float MARQUEE_DELTA_MAX = 0.07f; 7310 private static final int MARQUEE_DELAY = 1200; 7311 private static final int MARQUEE_RESTART_DELAY = 1200; 7312 private static final int MARQUEE_RESOLUTION = 1000 / 30; 7313 private static final int MARQUEE_PIXELS_PER_SECOND = 30; 7314 7315 private static final byte MARQUEE_STOPPED = 0x0; 7316 private static final byte MARQUEE_STARTING = 0x1; 7317 private static final byte MARQUEE_RUNNING = 0x2; 7318 7319 private static final int MESSAGE_START = 0x1; 7320 private static final int MESSAGE_TICK = 0x2; 7321 private static final int MESSAGE_RESTART = 0x3; 7322 7323 private final WeakReference<TextView> mView; 7324 7325 private byte mStatus = MARQUEE_STOPPED; 7326 private final float mScrollUnit; 7327 private float mMaxScroll; 7328 float mMaxFadeScroll; 7329 private float mGhostStart; 7330 private float mGhostOffset; 7331 private float mFadeStop; 7332 private int mRepeatLimit; 7333 7334 float mScroll; 7335 7336 Marquee(TextView v) { 7337 final float density = v.getContext().getResources().getDisplayMetrics().density; 7338 mScrollUnit = (MARQUEE_PIXELS_PER_SECOND * density) / MARQUEE_RESOLUTION; 7339 mView = new WeakReference<TextView>(v); 7340 } 7341 7342 @Override 7343 public void handleMessage(Message msg) { 7344 switch (msg.what) { 7345 case MESSAGE_START: 7346 mStatus = MARQUEE_RUNNING; 7347 tick(); 7348 break; 7349 case MESSAGE_TICK: 7350 tick(); 7351 break; 7352 case MESSAGE_RESTART: 7353 if (mStatus == MARQUEE_RUNNING) { 7354 if (mRepeatLimit >= 0) { 7355 mRepeatLimit--; 7356 } 7357 start(mRepeatLimit); 7358 } 7359 break; 7360 } 7361 } 7362 7363 void tick() { 7364 if (mStatus != MARQUEE_RUNNING) { 7365 return; 7366 } 7367 7368 removeMessages(MESSAGE_TICK); 7369 7370 final TextView textView = mView.get(); 7371 if (textView != null && (textView.isFocused() || textView.isSelected())) { 7372 mScroll += mScrollUnit; 7373 if (mScroll > mMaxScroll) { 7374 mScroll = mMaxScroll; 7375 sendEmptyMessageDelayed(MESSAGE_RESTART, MARQUEE_RESTART_DELAY); 7376 } else { 7377 sendEmptyMessageDelayed(MESSAGE_TICK, MARQUEE_RESOLUTION); 7378 } 7379 textView.invalidate(); 7380 } 7381 } 7382 7383 void stop() { 7384 mStatus = MARQUEE_STOPPED; 7385 removeMessages(MESSAGE_START); 7386 removeMessages(MESSAGE_RESTART); 7387 removeMessages(MESSAGE_TICK); 7388 resetScroll(); 7389 } 7390 7391 private void resetScroll() { 7392 mScroll = 0.0f; 7393 final TextView textView = mView.get(); 7394 if (textView != null) textView.invalidate(); 7395 } 7396 7397 void start(int repeatLimit) { 7398 if (repeatLimit == 0) { 7399 stop(); 7400 return; 7401 } 7402 mRepeatLimit = repeatLimit; 7403 final TextView textView = mView.get(); 7404 if (textView != null && textView.mLayout != null) { 7405 mStatus = MARQUEE_STARTING; 7406 mScroll = 0.0f; 7407 final int textWidth = textView.getWidth() - textView.getCompoundPaddingLeft() - 7408 textView.getCompoundPaddingRight(); 7409 final float lineWidth = textView.mLayout.getLineWidth(0); 7410 final float gap = textWidth / 3.0f; 7411 mGhostStart = lineWidth - textWidth + gap; 7412 mMaxScroll = mGhostStart + textWidth; 7413 mGhostOffset = lineWidth + gap; 7414 mFadeStop = lineWidth + textWidth / 6.0f; 7415 mMaxFadeScroll = mGhostStart + lineWidth + lineWidth; 7416 7417 textView.invalidate(); 7418 sendEmptyMessageDelayed(MESSAGE_START, MARQUEE_DELAY); 7419 } 7420 } 7421 7422 float getGhostOffset() { 7423 return mGhostOffset; 7424 } 7425 7426 boolean shouldDrawLeftFade() { 7427 return mScroll <= mFadeStop; 7428 } 7429 7430 boolean shouldDrawGhost() { 7431 return mStatus == MARQUEE_RUNNING && mScroll > mGhostStart; 7432 } 7433 7434 boolean isRunning() { 7435 return mStatus == MARQUEE_RUNNING; 7436 } 7437 7438 boolean isStopped() { 7439 return mStatus == MARQUEE_STOPPED; 7440 } 7441 } 7442 7443 /** 7444 * This method is called when the text is changed, in case any subclasses 7445 * would like to know. 7446 * 7447 * Within <code>text</code>, the <code>lengthAfter</code> characters 7448 * beginning at <code>start</code> have just replaced old text that had 7449 * length <code>lengthBefore</code>. It is an error to attempt to make 7450 * changes to <code>text</code> from this callback. 7451 * 7452 * @param text The text the TextView is displaying 7453 * @param start The offset of the start of the range of the text that was 7454 * modified 7455 * @param lengthBefore The length of the former text that has been replaced 7456 * @param lengthAfter The length of the replacement modified text 7457 */ 7458 protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { 7459 // intentionally empty, template pattern method can be overridden by subclasses 7460 } 7461 7462 /** 7463 * This method is called when the selection has changed, in case any 7464 * subclasses would like to know. 7465 * 7466 * @param selStart The new selection start location. 7467 * @param selEnd The new selection end location. 7468 */ 7469 protected void onSelectionChanged(int selStart, int selEnd) { 7470 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED); 7471 if (mSpellChecker != null) { 7472 mSpellChecker.onSelectionChanged(); 7473 } 7474 } 7475 7476 /** 7477 * Adds a TextWatcher to the list of those whose methods are called 7478 * whenever this TextView's text changes. 7479 * <p> 7480 * In 1.0, the {@link TextWatcher#afterTextChanged} method was erroneously 7481 * not called after {@link #setText} calls. Now, doing {@link #setText} 7482 * if there are any text changed listeners forces the buffer type to 7483 * Editable if it would not otherwise be and does call this method. 7484 */ 7485 public void addTextChangedListener(TextWatcher watcher) { 7486 if (mListeners == null) { 7487 mListeners = new ArrayList<TextWatcher>(); 7488 } 7489 7490 mListeners.add(watcher); 7491 } 7492 7493 /** 7494 * Removes the specified TextWatcher from the list of those whose 7495 * methods are called 7496 * whenever this TextView's text changes. 7497 */ 7498 public void removeTextChangedListener(TextWatcher watcher) { 7499 if (mListeners != null) { 7500 int i = mListeners.indexOf(watcher); 7501 7502 if (i >= 0) { 7503 mListeners.remove(i); 7504 } 7505 } 7506 } 7507 7508 private void sendBeforeTextChanged(CharSequence text, int start, int before, int after) { 7509 if (mListeners != null) { 7510 final ArrayList<TextWatcher> list = mListeners; 7511 final int count = list.size(); 7512 for (int i = 0; i < count; i++) { 7513 list.get(i).beforeTextChanged(text, start, before, after); 7514 } 7515 } 7516 7517 // The spans that are inside or intersect the modified region no longer make sense 7518 removeIntersectingSpans(start, start + before, SpellCheckSpan.class); 7519 removeIntersectingSpans(start, start + before, SuggestionSpan.class); 7520 } 7521 7522 // Removes all spans that are inside or actually overlap the start..end range 7523 private <T> void removeIntersectingSpans(int start, int end, Class<T> type) { 7524 if (!(mText instanceof Editable)) return; 7525 Editable text = (Editable) mText; 7526 7527 T[] spans = text.getSpans(start, end, type); 7528 final int length = spans.length; 7529 for (int i = 0; i < length; i++) { 7530 final int s = text.getSpanStart(spans[i]); 7531 final int e = text.getSpanEnd(spans[i]); 7532 if (e == start || s == end) break; 7533 text.removeSpan(spans[i]); 7534 } 7535 } 7536 7537 /** 7538 * Not private so it can be called from an inner class without going 7539 * through a thunk. 7540 */ 7541 void sendOnTextChanged(CharSequence text, int start, int before, int after) { 7542 if (mListeners != null) { 7543 final ArrayList<TextWatcher> list = mListeners; 7544 final int count = list.size(); 7545 for (int i = 0; i < count; i++) { 7546 list.get(i).onTextChanged(text, start, before, after); 7547 } 7548 } 7549 } 7550 7551 /** 7552 * Not private so it can be called from an inner class without going 7553 * through a thunk. 7554 */ 7555 void sendAfterTextChanged(Editable text) { 7556 if (mListeners != null) { 7557 final ArrayList<TextWatcher> list = mListeners; 7558 final int count = list.size(); 7559 for (int i = 0; i < count; i++) { 7560 list.get(i).afterTextChanged(text); 7561 } 7562 } 7563 } 7564 7565 /** 7566 * Not private so it can be called from an inner class without going 7567 * through a thunk. 7568 */ 7569 void handleTextChanged(CharSequence buffer, int start, int before, int after) { 7570 final InputMethodState ims = mInputMethodState; 7571 if (ims == null || ims.mBatchEditNesting == 0) { 7572 updateAfterEdit(); 7573 } 7574 if (ims != null) { 7575 ims.mContentChanged = true; 7576 if (ims.mChangedStart < 0) { 7577 ims.mChangedStart = start; 7578 ims.mChangedEnd = start+before; 7579 } else { 7580 ims.mChangedStart = Math.min(ims.mChangedStart, start); 7581 ims.mChangedEnd = Math.max(ims.mChangedEnd, start + before - ims.mChangedDelta); 7582 } 7583 ims.mChangedDelta += after-before; 7584 } 7585 7586 sendOnTextChanged(buffer, start, before, after); 7587 onTextChanged(buffer, start, before, after); 7588 7589 // The WordIterator text change listener may be called after this one. 7590 // Make sure this changed text is rescanned before the iterator is used on it. 7591 getWordIterator().forceUpdate(); 7592 updateSpellCheckSpans(start, start + after); 7593 7594 // Hide the controllers if the amount of content changed 7595 if (before != after) { 7596 // We do not hide the span controllers, as they can be added when a new text is 7597 // inserted into the text view 7598 hideCursorControllers(); 7599 } 7600 } 7601 7602 /** 7603 * Not private so it can be called from an inner class without going 7604 * through a thunk. 7605 */ 7606 void spanChange(Spanned buf, Object what, int oldStart, int newStart, int oldEnd, int newEnd) { 7607 // XXX Make the start and end move together if this ends up 7608 // spending too much time invalidating. 7609 7610 boolean selChanged = false; 7611 int newSelStart=-1, newSelEnd=-1; 7612 7613 final InputMethodState ims = mInputMethodState; 7614 7615 if (what == Selection.SELECTION_END) { 7616 mHighlightPathBogus = true; 7617 selChanged = true; 7618 newSelEnd = newStart; 7619 7620 if (!isFocused()) { 7621 mSelectionMoved = true; 7622 } 7623 7624 if (oldStart >= 0 || newStart >= 0) { 7625 invalidateCursor(Selection.getSelectionStart(buf), oldStart, newStart); 7626 registerForPreDraw(); 7627 makeBlink(); 7628 } 7629 } 7630 7631 if (what == Selection.SELECTION_START) { 7632 mHighlightPathBogus = true; 7633 selChanged = true; 7634 newSelStart = newStart; 7635 7636 if (!isFocused()) { 7637 mSelectionMoved = true; 7638 } 7639 7640 if (oldStart >= 0 || newStart >= 0) { 7641 int end = Selection.getSelectionEnd(buf); 7642 invalidateCursor(end, oldStart, newStart); 7643 } 7644 } 7645 7646 if (selChanged) { 7647 if ((buf.getSpanFlags(what)&Spanned.SPAN_INTERMEDIATE) == 0) { 7648 if (newSelStart < 0) { 7649 newSelStart = Selection.getSelectionStart(buf); 7650 } 7651 if (newSelEnd < 0) { 7652 newSelEnd = Selection.getSelectionEnd(buf); 7653 } 7654 onSelectionChanged(newSelStart, newSelEnd); 7655 } 7656 } 7657 7658 if (what instanceof UpdateAppearance || 7659 what instanceof ParagraphStyle) { 7660 if (ims == null || ims.mBatchEditNesting == 0) { 7661 invalidate(); 7662 mHighlightPathBogus = true; 7663 checkForResize(); 7664 } else { 7665 ims.mContentChanged = true; 7666 } 7667 } 7668 7669 if (MetaKeyKeyListener.isMetaTracker(buf, what)) { 7670 mHighlightPathBogus = true; 7671 if (ims != null && MetaKeyKeyListener.isSelectingMetaTracker(buf, what)) { 7672 ims.mSelectionModeChanged = true; 7673 } 7674 7675 if (Selection.getSelectionStart(buf) >= 0) { 7676 if (ims == null || ims.mBatchEditNesting == 0) { 7677 invalidateCursor(); 7678 } else { 7679 ims.mCursorChanged = true; 7680 } 7681 } 7682 } 7683 7684 if (what instanceof ParcelableSpan) { 7685 // If this is a span that can be sent to a remote process, 7686 // the current extract editor would be interested in it. 7687 if (ims != null && ims.mExtracting != null) { 7688 if (ims.mBatchEditNesting != 0) { 7689 if (oldStart >= 0) { 7690 if (ims.mChangedStart > oldStart) { 7691 ims.mChangedStart = oldStart; 7692 } 7693 if (ims.mChangedStart > oldEnd) { 7694 ims.mChangedStart = oldEnd; 7695 } 7696 } 7697 if (newStart >= 0) { 7698 if (ims.mChangedStart > newStart) { 7699 ims.mChangedStart = newStart; 7700 } 7701 if (ims.mChangedStart > newEnd) { 7702 ims.mChangedStart = newEnd; 7703 } 7704 } 7705 } else { 7706 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "Span change outside of batch: " 7707 + oldStart + "-" + oldEnd + "," 7708 + newStart + "-" + newEnd + what); 7709 ims.mContentChanged = true; 7710 } 7711 } 7712 } 7713 7714 if (what instanceof SpellCheckSpan) { 7715 if (newStart < 0) { 7716 getSpellChecker().removeSpellCheckSpan((SpellCheckSpan) what); 7717 } else if (oldStart < 0) { 7718 getSpellChecker().addSpellCheckSpan((SpellCheckSpan) what); 7719 } 7720 } 7721 } 7722 7723 /** 7724 * Create new SpellCheckSpans on the modified region. 7725 */ 7726 private void updateSpellCheckSpans(int start, int end) { 7727 if (!(mText instanceof Editable) || !isSuggestionsEnabled()) return; 7728 Editable text = (Editable) mText; 7729 7730 WordIterator wordIterator = getWordIterator(); 7731 wordIterator.setCharSequence(text); 7732 7733 // Move back to the beginning of the current word, if any 7734 int wordStart = wordIterator.preceding(start); 7735 int wordEnd; 7736 if (wordStart == BreakIterator.DONE) { 7737 wordEnd = wordIterator.following(start); 7738 if (wordEnd != BreakIterator.DONE) { 7739 wordStart = wordIterator.getBeginning(wordEnd); 7740 } 7741 } else { 7742 wordEnd = wordIterator.getEnd(wordStart); 7743 } 7744 if (wordEnd == BreakIterator.DONE) { 7745 return; 7746 } 7747 7748 // Iterate over the newly added text and schedule new SpellCheckSpans 7749 while (wordStart <= end) { 7750 if (wordEnd >= start) { 7751 // A word across the interval boundaries must remove boundary edition spans 7752 if (wordStart < start && wordEnd > start) { 7753 removeEditionSpansAt(start, text); 7754 } 7755 7756 if (wordStart < end && wordEnd > end) { 7757 removeEditionSpansAt(end, text); 7758 } 7759 7760 // Do not create new boundary spans if they already exist 7761 boolean createSpellCheckSpan = true; 7762 if (wordEnd == start) { 7763 SpellCheckSpan[] spellCheckSpans = text.getSpans(start, start, 7764 SpellCheckSpan.class); 7765 if (spellCheckSpans.length > 0) createSpellCheckSpan = false; 7766 } 7767 7768 if (wordStart == end) { 7769 SpellCheckSpan[] spellCheckSpans = text.getSpans(end, end, 7770 SpellCheckSpan.class); 7771 if (spellCheckSpans.length > 0) createSpellCheckSpan = false; 7772 } 7773 7774 if (createSpellCheckSpan) { 7775 text.setSpan(new SpellCheckSpan(), wordStart, wordEnd, 7776 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 7777 } 7778 } 7779 7780 // iterate word by word 7781 wordEnd = wordIterator.following(wordEnd); 7782 if (wordEnd == BreakIterator.DONE) return; 7783 wordStart = wordIterator.getBeginning(wordEnd); 7784 if (wordStart == BreakIterator.DONE) { 7785 Log.e(LOG_TAG, "Unable to find word beginning from " + wordEnd + "in " + mText); 7786 return; 7787 } 7788 } 7789 } 7790 7791 private static void removeEditionSpansAt(int offset, Editable text) { 7792 SuggestionSpan[] suggestionSpans = text.getSpans(offset, offset, SuggestionSpan.class); 7793 for (int i = 0; i < suggestionSpans.length; i++) { 7794 text.removeSpan(suggestionSpans[i]); 7795 } 7796 SpellCheckSpan[] spellCheckSpans = text.getSpans(offset, offset, SpellCheckSpan.class); 7797 for (int i = 0; i < spellCheckSpans.length; i++) { 7798 text.removeSpan(spellCheckSpans[i]); 7799 } 7800 } 7801 7802 /** 7803 * Controls the {@link EasyEditSpan} monitoring when it is added, and when the related 7804 * pop-up should be displayed. 7805 */ 7806 private class EasyEditSpanController { 7807 7808 private static final int DISPLAY_TIMEOUT_MS = 3000; // 3 secs 7809 7810 private EasyEditPopupWindow mPopupWindow; 7811 7812 private EasyEditSpan mEasyEditSpan; 7813 7814 private Runnable mHidePopup; 7815 7816 private void hide() { 7817 if (mPopupWindow != null) { 7818 mPopupWindow.hide(); 7819 TextView.this.removeCallbacks(mHidePopup); 7820 } 7821 removeSpans(mText); 7822 mEasyEditSpan = null; 7823 } 7824 7825 /** 7826 * Monitors the changes in the text. 7827 * 7828 * <p>{@link ChangeWatcher#onSpanAdded(Spannable, Object, int, int)} cannot be used, 7829 * as the notifications are not sent when a spannable (with spans) is inserted. 7830 */ 7831 public void onTextChange(CharSequence buffer) { 7832 adjustSpans(mText); 7833 7834 if (getWindowVisibility() != View.VISIBLE) { 7835 // The window is not visible yet, ignore the text change. 7836 return; 7837 } 7838 7839 if (mLayout == null) { 7840 // The view has not been layout yet, ignore the text change 7841 return; 7842 } 7843 7844 InputMethodManager imm = InputMethodManager.peekInstance(); 7845 if (!(TextView.this instanceof ExtractEditText) 7846 && imm != null && imm.isFullscreenMode()) { 7847 // The input is in extract mode. We do not have to handle the easy edit in the 7848 // original TextView, as the ExtractEditText will do 7849 return; 7850 } 7851 7852 // Remove the current easy edit span, as the text changed, and remove the pop-up 7853 // (if any) 7854 if (mEasyEditSpan != null) { 7855 if (mText instanceof Spannable) { 7856 ((Spannable) mText).removeSpan(mEasyEditSpan); 7857 } 7858 mEasyEditSpan = null; 7859 } 7860 if (mPopupWindow != null && mPopupWindow.isShowing()) { 7861 mPopupWindow.hide(); 7862 } 7863 7864 // Display the new easy edit span (if any). 7865 if (buffer instanceof Spanned) { 7866 mEasyEditSpan = getSpan((Spanned) buffer); 7867 if (mEasyEditSpan != null) { 7868 if (mPopupWindow == null) { 7869 mPopupWindow = new EasyEditPopupWindow(); 7870 mHidePopup = new Runnable() { 7871 @Override 7872 public void run() { 7873 hide(); 7874 } 7875 }; 7876 } 7877 mPopupWindow.show(mEasyEditSpan); 7878 TextView.this.removeCallbacks(mHidePopup); 7879 TextView.this.postDelayed(mHidePopup, DISPLAY_TIMEOUT_MS); 7880 } 7881 } 7882 } 7883 7884 /** 7885 * Adjusts the spans by removing all of them except the last one. 7886 */ 7887 private void adjustSpans(CharSequence buffer) { 7888 // This method enforces that only one easy edit span is attached to the text. 7889 // A better way to enforce this would be to listen for onSpanAdded, but this method 7890 // cannot be used in this scenario as no notification is triggered when a text with 7891 // spans is inserted into a text. 7892 if (buffer instanceof Spannable) { 7893 Spannable spannable = (Spannable) buffer; 7894 EasyEditSpan[] spans = spannable.getSpans(0, spannable.length(), 7895 EasyEditSpan.class); 7896 for (int i = 0; i < spans.length - 1; i++) { 7897 spannable.removeSpan(spans[i]); 7898 } 7899 } 7900 } 7901 7902 /** 7903 * Removes all the {@link EasyEditSpan} currently attached. 7904 */ 7905 private void removeSpans(CharSequence buffer) { 7906 if (buffer instanceof Spannable) { 7907 Spannable spannable = (Spannable) buffer; 7908 EasyEditSpan[] spans = spannable.getSpans(0, spannable.length(), 7909 EasyEditSpan.class); 7910 for (int i = 0; i < spans.length; i++) { 7911 spannable.removeSpan(spans[i]); 7912 } 7913 } 7914 } 7915 7916 private EasyEditSpan getSpan(Spanned spanned) { 7917 EasyEditSpan[] easyEditSpans = spanned.getSpans(0, spanned.length(), 7918 EasyEditSpan.class); 7919 if (easyEditSpans.length == 0) { 7920 return null; 7921 } else { 7922 return easyEditSpans[0]; 7923 } 7924 } 7925 } 7926 7927 /** 7928 * Displays the actions associated to an {@link EasyEditSpan}. The pop-up is controlled 7929 * by {@link EasyEditSpanController}. 7930 */ 7931 private class EasyEditPopupWindow extends PinnedPopupWindow 7932 implements OnClickListener { 7933 private static final int POPUP_TEXT_LAYOUT = 7934 com.android.internal.R.layout.text_edit_action_popup_text; 7935 private TextView mDeleteTextView; 7936 private EasyEditSpan mEasyEditSpan; 7937 7938 @Override 7939 protected void createPopupWindow() { 7940 mPopupWindow = new PopupWindow(TextView.this.mContext, null, 7941 com.android.internal.R.attr.textSelectHandleWindowStyle); 7942 mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); 7943 mPopupWindow.setClippingEnabled(true); 7944 } 7945 7946 @Override 7947 protected void initContentView() { 7948 LinearLayout linearLayout = new LinearLayout(TextView.this.getContext()); 7949 linearLayout.setOrientation(LinearLayout.HORIZONTAL); 7950 mContentView = linearLayout; 7951 mContentView.setBackgroundResource( 7952 com.android.internal.R.drawable.text_edit_side_paste_window); 7953 7954 LayoutInflater inflater = (LayoutInflater)TextView.this.mContext. 7955 getSystemService(Context.LAYOUT_INFLATER_SERVICE); 7956 7957 LayoutParams wrapContent = new LayoutParams( 7958 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 7959 7960 mDeleteTextView = (TextView) inflater.inflate(POPUP_TEXT_LAYOUT, null); 7961 mDeleteTextView.setLayoutParams(wrapContent); 7962 mDeleteTextView.setText(com.android.internal.R.string.delete); 7963 mDeleteTextView.setOnClickListener(this); 7964 mContentView.addView(mDeleteTextView); 7965 } 7966 7967 public void show(EasyEditSpan easyEditSpan) { 7968 mEasyEditSpan = easyEditSpan; 7969 super.show(); 7970 } 7971 7972 @Override 7973 public void onClick(View view) { 7974 if (view == mDeleteTextView) { 7975 deleteText(); 7976 } 7977 } 7978 7979 private void deleteText() { 7980 Editable editable = (Editable) mText; 7981 int start = editable.getSpanStart(mEasyEditSpan); 7982 int end = editable.getSpanEnd(mEasyEditSpan); 7983 if (start >= 0 && end >= 0) { 7984 editable.delete(start, end); 7985 } 7986 } 7987 7988 @Override 7989 protected int getTextOffset() { 7990 // Place the pop-up at the end of the span 7991 Editable editable = (Editable) mText; 7992 return editable.getSpanEnd(mEasyEditSpan); 7993 } 7994 7995 @Override 7996 protected int getVerticalLocalPosition(int line) { 7997 return mLayout.getLineBottom(line); 7998 } 7999 8000 @Override 8001 protected int clipVertically(int positionY) { 8002 // As we display the pop-up below the span, no vertical clipping is required. 8003 return positionY; 8004 } 8005 } 8006 8007 private class ChangeWatcher implements TextWatcher, SpanWatcher { 8008 8009 private CharSequence mBeforeText; 8010 8011 private EasyEditSpanController mEasyEditSpanController; 8012 8013 private ChangeWatcher() { 8014 mEasyEditSpanController = new EasyEditSpanController(); 8015 } 8016 8017 public void beforeTextChanged(CharSequence buffer, int start, 8018 int before, int after) { 8019 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "beforeTextChanged start=" + start 8020 + " before=" + before + " after=" + after + ": " + buffer); 8021 8022 if (AccessibilityManager.getInstance(mContext).isEnabled() 8023 && !isPasswordInputType(mInputType) 8024 && !hasPasswordTransformationMethod()) { 8025 mBeforeText = buffer.toString(); 8026 } 8027 8028 TextView.this.sendBeforeTextChanged(buffer, start, before, after); 8029 } 8030 8031 public void onTextChanged(CharSequence buffer, int start, 8032 int before, int after) { 8033 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onTextChanged start=" + start 8034 + " before=" + before + " after=" + after + ": " + buffer); 8035 TextView.this.handleTextChanged(buffer, start, before, after); 8036 8037 mEasyEditSpanController.onTextChange(buffer); 8038 8039 if (AccessibilityManager.getInstance(mContext).isEnabled() && 8040 (isFocused() || isSelected() && isShown())) { 8041 sendAccessibilityEventTypeViewTextChanged(mBeforeText, start, before, after); 8042 mBeforeText = null; 8043 } 8044 } 8045 8046 public void afterTextChanged(Editable buffer) { 8047 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "afterTextChanged: " + buffer); 8048 TextView.this.sendAfterTextChanged(buffer); 8049 8050 if (MetaKeyKeyListener.getMetaState(buffer, MetaKeyKeyListener.META_SELECTING) != 0) { 8051 MetaKeyKeyListener.stopSelecting(TextView.this, buffer); 8052 } 8053 } 8054 8055 public void onSpanChanged(Spannable buf, 8056 Object what, int s, int e, int st, int en) { 8057 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanChanged s=" + s + " e=" + e 8058 + " st=" + st + " en=" + en + " what=" + what + ": " + buf); 8059 TextView.this.spanChange(buf, what, s, st, e, en); 8060 } 8061 8062 public void onSpanAdded(Spannable buf, Object what, int s, int e) { 8063 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanAdded s=" + s + " e=" + e 8064 + " what=" + what + ": " + buf); 8065 TextView.this.spanChange(buf, what, -1, s, -1, e); 8066 } 8067 8068 public void onSpanRemoved(Spannable buf, Object what, int s, int e) { 8069 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanRemoved s=" + s + " e=" + e 8070 + " what=" + what + ": " + buf); 8071 TextView.this.spanChange(buf, what, s, -1, e, -1); 8072 } 8073 8074 private void hideControllers() { 8075 mEasyEditSpanController.hide(); 8076 } 8077 } 8078 8079 /** 8080 * @hide 8081 */ 8082 @Override 8083 public void dispatchFinishTemporaryDetach() { 8084 mDispatchTemporaryDetach = true; 8085 super.dispatchFinishTemporaryDetach(); 8086 mDispatchTemporaryDetach = false; 8087 } 8088 8089 @Override 8090 public void onStartTemporaryDetach() { 8091 super.onStartTemporaryDetach(); 8092 // Only track when onStartTemporaryDetach() is called directly, 8093 // usually because this instance is an editable field in a list 8094 if (!mDispatchTemporaryDetach) mTemporaryDetach = true; 8095 8096 // Because of View recycling in ListView, there is no easy way to know when a TextView with 8097 // selection becomes visible again. Until a better solution is found, stop text selection 8098 // mode (if any) as soon as this TextView is recycled. 8099 hideControllers(); 8100 } 8101 8102 @Override 8103 public void onFinishTemporaryDetach() { 8104 super.onFinishTemporaryDetach(); 8105 // Only track when onStartTemporaryDetach() is called directly, 8106 // usually because this instance is an editable field in a list 8107 if (!mDispatchTemporaryDetach) mTemporaryDetach = false; 8108 } 8109 8110 @Override 8111 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { 8112 if (mTemporaryDetach) { 8113 // If we are temporarily in the detach state, then do nothing. 8114 super.onFocusChanged(focused, direction, previouslyFocusedRect); 8115 return; 8116 } 8117 8118 mShowCursor = SystemClock.uptimeMillis(); 8119 8120 ensureEndedBatchEdit(); 8121 8122 if (focused) { 8123 int selStart = getSelectionStart(); 8124 int selEnd = getSelectionEnd(); 8125 8126 // SelectAllOnFocus fields are highlighted and not selected. Do not start text selection 8127 // mode for these, unless there was a specific selection already started. 8128 final boolean isFocusHighlighted = mSelectAllOnFocus && selStart == 0 && 8129 selEnd == mText.length(); 8130 mCreatedWithASelection = mFrozenWithFocus && hasSelection() && !isFocusHighlighted; 8131 8132 if (!mFrozenWithFocus || (selStart < 0 || selEnd < 0)) { 8133 // If a tap was used to give focus to that view, move cursor at tap position. 8134 // Has to be done before onTakeFocus, which can be overloaded. 8135 final int lastTapPosition = getLastTapPosition(); 8136 if (lastTapPosition >= 0) { 8137 Selection.setSelection((Spannable) mText, lastTapPosition); 8138 } 8139 8140 if (mMovement != null) { 8141 mMovement.onTakeFocus(this, (Spannable) mText, direction); 8142 } 8143 8144 // The DecorView does not have focus when the 'Done' ExtractEditText button is 8145 // pressed. Since it is the ViewAncestor's mView, it requests focus before 8146 // ExtractEditText clears focus, which gives focus to the ExtractEditText. 8147 // This special case ensure that we keep current selection in that case. 8148 // It would be better to know why the DecorView does not have focus at that time. 8149 if (((this instanceof ExtractEditText) || mSelectionMoved) && 8150 selStart >= 0 && selEnd >= 0) { 8151 /* 8152 * Someone intentionally set the selection, so let them 8153 * do whatever it is that they wanted to do instead of 8154 * the default on-focus behavior. We reset the selection 8155 * here instead of just skipping the onTakeFocus() call 8156 * because some movement methods do something other than 8157 * just setting the selection in theirs and we still 8158 * need to go through that path. 8159 */ 8160 Selection.setSelection((Spannable) mText, selStart, selEnd); 8161 } 8162 8163 if (mSelectAllOnFocus) { 8164 selectAll(); 8165 } 8166 8167 mTouchFocusSelected = true; 8168 } 8169 8170 mFrozenWithFocus = false; 8171 mSelectionMoved = false; 8172 8173 if (mText instanceof Spannable) { 8174 Spannable sp = (Spannable) mText; 8175 MetaKeyKeyListener.resetMetaState(sp); 8176 } 8177 8178 makeBlink(); 8179 8180 if (mError != null) { 8181 showError(); 8182 } 8183 } else { 8184 if (mError != null) { 8185 hideError(); 8186 } 8187 // Don't leave us in the middle of a batch edit. 8188 onEndBatchEdit(); 8189 8190 if (this instanceof ExtractEditText) { 8191 // terminateTextSelectionMode removes selection, which we want to keep when 8192 // ExtractEditText goes out of focus. 8193 final int selStart = getSelectionStart(); 8194 final int selEnd = getSelectionEnd(); 8195 hideControllers(); 8196 Selection.setSelection((Spannable) mText, selStart, selEnd); 8197 } else { 8198 hideControllers(); 8199 downgradeEasyCorrectionSpans(); 8200 } 8201 8202 // No need to create the controller 8203 if (mSelectionModifierCursorController != null) { 8204 mSelectionModifierCursorController.resetTouchOffsets(); 8205 } 8206 } 8207 8208 startStopMarquee(focused); 8209 8210 if (mTransformation != null) { 8211 mTransformation.onFocusChanged(this, mText, focused, direction, previouslyFocusedRect); 8212 } 8213 8214 super.onFocusChanged(focused, direction, previouslyFocusedRect); 8215 } 8216 8217 private int getLastTapPosition() { 8218 // No need to create the controller at that point, no last tap position saved 8219 if (mSelectionModifierCursorController != null) { 8220 int lastTapPosition = mSelectionModifierCursorController.getMinTouchOffset(); 8221 if (lastTapPosition >= 0) { 8222 // Safety check, should not be possible. 8223 if (lastTapPosition > mText.length()) { 8224 Log.e(LOG_TAG, "Invalid tap focus position (" + lastTapPosition + " vs " 8225 + mText.length() + ")"); 8226 lastTapPosition = mText.length(); 8227 } 8228 return lastTapPosition; 8229 } 8230 } 8231 8232 return -1; 8233 } 8234 8235 @Override 8236 public void onWindowFocusChanged(boolean hasWindowFocus) { 8237 super.onWindowFocusChanged(hasWindowFocus); 8238 8239 if (hasWindowFocus) { 8240 if (mBlink != null) { 8241 mBlink.uncancel(); 8242 makeBlink(); 8243 } 8244 } else { 8245 if (mBlink != null) { 8246 mBlink.cancel(); 8247 } 8248 // Don't leave us in the middle of a batch edit. 8249 onEndBatchEdit(); 8250 if (mInputContentType != null) { 8251 mInputContentType.enterDown = false; 8252 } 8253 8254 hideControllers(); 8255 } 8256 8257 startStopMarquee(hasWindowFocus); 8258 } 8259 8260 @Override 8261 protected void onVisibilityChanged(View changedView, int visibility) { 8262 super.onVisibilityChanged(changedView, visibility); 8263 if (visibility != VISIBLE) { 8264 hideControllers(); 8265 } 8266 } 8267 8268 /** 8269 * Use {@link BaseInputConnection#removeComposingSpans 8270 * BaseInputConnection.removeComposingSpans()} to remove any IME composing 8271 * state from this text view. 8272 */ 8273 public void clearComposingText() { 8274 if (mText instanceof Spannable) { 8275 BaseInputConnection.removeComposingSpans((Spannable)mText); 8276 } 8277 } 8278 8279 @Override 8280 public void setSelected(boolean selected) { 8281 boolean wasSelected = isSelected(); 8282 8283 super.setSelected(selected); 8284 8285 if (selected != wasSelected && mEllipsize == TextUtils.TruncateAt.MARQUEE) { 8286 if (selected) { 8287 startMarquee(); 8288 } else { 8289 stopMarquee(); 8290 } 8291 } 8292 } 8293 8294 @Override 8295 public boolean onTouchEvent(MotionEvent event) { 8296 final int action = event.getActionMasked(); 8297 8298 if (hasSelectionController()) { 8299 getSelectionController().onTouchEvent(event); 8300 } 8301 8302 if (action == MotionEvent.ACTION_DOWN) { 8303 mLastDownPositionX = event.getX(); 8304 mLastDownPositionY = event.getY(); 8305 8306 // Reset this state; it will be re-set if super.onTouchEvent 8307 // causes focus to move to the view. 8308 mTouchFocusSelected = false; 8309 mIgnoreActionUpEvent = false; 8310 } 8311 8312 final boolean superResult = super.onTouchEvent(event); 8313 8314 /* 8315 * Don't handle the release after a long press, because it will 8316 * move the selection away from whatever the menu action was 8317 * trying to affect. 8318 */ 8319 if (mDiscardNextActionUp && action == MotionEvent.ACTION_UP) { 8320 mDiscardNextActionUp = false; 8321 return superResult; 8322 } 8323 8324 final boolean touchIsFinished = (action == MotionEvent.ACTION_UP) && 8325 !shouldIgnoreActionUpEvent() && isFocused(); 8326 8327 if ((mMovement != null || onCheckIsTextEditor()) && isEnabled() 8328 && mText instanceof Spannable && mLayout != null) { 8329 boolean handled = false; 8330 8331 if (mMovement != null) { 8332 handled |= mMovement.onTouchEvent(this, (Spannable) mText, event); 8333 } 8334 8335 if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && mTextIsSelectable) { 8336 // The LinkMovementMethod which should handle taps on links has not been installed 8337 // on non editable text that support text selection. 8338 // We reproduce its behavior here to open links for these. 8339 ClickableSpan[] links = ((Spannable) mText).getSpans(getSelectionStart(), 8340 getSelectionEnd(), ClickableSpan.class); 8341 8342 if (links.length != 0) { 8343 links[0].onClick(this); 8344 handled = true; 8345 } 8346 } 8347 8348 if (touchIsFinished && (isTextEditable() || mTextIsSelectable)) { 8349 // Show the IME, except when selecting in read-only text. 8350 final InputMethodManager imm = InputMethodManager.peekInstance(); 8351 viewClicked(imm); 8352 if (!mTextIsSelectable) { 8353 handled |= imm != null && imm.showSoftInput(this, 0); 8354 } 8355 8356 boolean selectAllGotFocus = mSelectAllOnFocus && didTouchFocusSelect(); 8357 hideControllers(); 8358 if (!selectAllGotFocus && mText.length() > 0) { 8359 if (isCursorInsideEasyCorrectionSpan()) { 8360 showSuggestions(); 8361 } else if (hasInsertionController()) { 8362 getInsertionController().show(); 8363 } 8364 } 8365 8366 handled = true; 8367 } 8368 8369 if (handled) { 8370 return true; 8371 } 8372 } 8373 8374 return superResult; 8375 } 8376 8377 /** 8378 * @return <code>true</code> if the cursor/current selection overlaps a {@link SuggestionSpan}. 8379 */ 8380 private boolean isCursorInsideSuggestionSpan() { 8381 if (!(mText instanceof Spannable)) return false; 8382 8383 SuggestionSpan[] suggestionSpans = ((Spannable) mText).getSpans(getSelectionStart(), 8384 getSelectionEnd(), SuggestionSpan.class); 8385 return (suggestionSpans.length > 0); 8386 } 8387 8388 /** 8389 * @return <code>true</code> if the cursor is inside an {@link SuggestionSpan} with 8390 * {@link SuggestionSpan#FLAG_EASY_CORRECT} set. 8391 */ 8392 private boolean isCursorInsideEasyCorrectionSpan() { 8393 Spannable spannable = (Spannable) mText; 8394 SuggestionSpan[] suggestionSpans = spannable.getSpans(getSelectionStart(), 8395 getSelectionEnd(), SuggestionSpan.class); 8396 for (int i = 0; i < suggestionSpans.length; i++) { 8397 if ((suggestionSpans[i].getFlags() & SuggestionSpan.FLAG_EASY_CORRECT) != 0) { 8398 return true; 8399 } 8400 } 8401 return false; 8402 } 8403 8404 /** 8405 * Downgrades to simple suggestions all the easy correction spans that are not a spell check 8406 * span. 8407 */ 8408 private void downgradeEasyCorrectionSpans() { 8409 if (mText instanceof Spannable) { 8410 Spannable spannable = (Spannable) mText; 8411 SuggestionSpan[] suggestionSpans = spannable.getSpans(0, 8412 spannable.length(), SuggestionSpan.class); 8413 for (int i = 0; i < suggestionSpans.length; i++) { 8414 int flags = suggestionSpans[i].getFlags(); 8415 if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0 8416 && (flags & SuggestionSpan.FLAG_MISSPELLED) == 0) { 8417 flags = flags & ~SuggestionSpan.FLAG_EASY_CORRECT; 8418 suggestionSpans[i].setFlags(flags); 8419 } 8420 } 8421 } 8422 } 8423 8424 @Override 8425 public boolean onGenericMotionEvent(MotionEvent event) { 8426 if (mMovement != null && mText instanceof Spannable && mLayout != null) { 8427 try { 8428 if (mMovement.onGenericMotionEvent(this, (Spannable) mText, event)) { 8429 return true; 8430 } 8431 } catch (AbstractMethodError ex) { 8432 // onGenericMotionEvent was added to the MovementMethod interface in API 12. 8433 // Ignore its absence in case third party applications implemented the 8434 // interface directly. 8435 } 8436 } 8437 return super.onGenericMotionEvent(event); 8438 } 8439 8440 private void prepareCursorControllers() { 8441 boolean windowSupportsHandles = false; 8442 8443 ViewGroup.LayoutParams params = getRootView().getLayoutParams(); 8444 if (params instanceof WindowManager.LayoutParams) { 8445 WindowManager.LayoutParams windowParams = (WindowManager.LayoutParams) params; 8446 windowSupportsHandles = windowParams.type < WindowManager.LayoutParams.FIRST_SUB_WINDOW 8447 || windowParams.type > WindowManager.LayoutParams.LAST_SUB_WINDOW; 8448 } 8449 8450 mInsertionControllerEnabled = windowSupportsHandles && isCursorVisible() && mLayout != null; 8451 mSelectionControllerEnabled = windowSupportsHandles && textCanBeSelected() && 8452 mLayout != null; 8453 8454 if (!mInsertionControllerEnabled) { 8455 hideInsertionPointCursorController(); 8456 if (mInsertionPointCursorController != null) { 8457 mInsertionPointCursorController.onDetached(); 8458 mInsertionPointCursorController = null; 8459 } 8460 } 8461 8462 if (!mSelectionControllerEnabled) { 8463 stopSelectionActionMode(); 8464 if (mSelectionModifierCursorController != null) { 8465 mSelectionModifierCursorController.onDetached(); 8466 mSelectionModifierCursorController = null; 8467 } 8468 } 8469 } 8470 8471 /** 8472 * @return True iff this TextView contains a text that can be edited, or if this is 8473 * a selectable TextView. 8474 */ 8475 private boolean isTextEditable() { 8476 return mText instanceof Editable && onCheckIsTextEditor() && isEnabled(); 8477 } 8478 8479 /** 8480 * Returns true, only while processing a touch gesture, if the initial 8481 * touch down event caused focus to move to the text view and as a result 8482 * its selection changed. Only valid while processing the touch gesture 8483 * of interest. 8484 */ 8485 public boolean didTouchFocusSelect() { 8486 return mTouchFocusSelected; 8487 } 8488 8489 @Override 8490 public void cancelLongPress() { 8491 super.cancelLongPress(); 8492 mIgnoreActionUpEvent = true; 8493 } 8494 8495 /** 8496 * This method is only valid during a touch event. 8497 * 8498 * @return true when the ACTION_UP event should be ignored, false otherwise. 8499 * 8500 * @hide 8501 */ 8502 public boolean shouldIgnoreActionUpEvent() { 8503 return mIgnoreActionUpEvent; 8504 } 8505 8506 @Override 8507 public boolean onTrackballEvent(MotionEvent event) { 8508 if (mMovement != null && mText instanceof Spannable && 8509 mLayout != null) { 8510 if (mMovement.onTrackballEvent(this, (Spannable) mText, event)) { 8511 return true; 8512 } 8513 } 8514 8515 return super.onTrackballEvent(event); 8516 } 8517 8518 public void setScroller(Scroller s) { 8519 mScroller = s; 8520 } 8521 8522 private static class Blink extends Handler implements Runnable { 8523 private final WeakReference<TextView> mView; 8524 private boolean mCancelled; 8525 8526 public Blink(TextView v) { 8527 mView = new WeakReference<TextView>(v); 8528 } 8529 8530 public void run() { 8531 if (mCancelled) { 8532 return; 8533 } 8534 8535 removeCallbacks(Blink.this); 8536 8537 TextView tv = mView.get(); 8538 8539 if (tv != null && tv.shouldBlink()) { 8540 if (tv.mLayout != null) { 8541 tv.invalidateCursorPath(); 8542 } 8543 8544 postAtTime(this, SystemClock.uptimeMillis() + BLINK); 8545 } 8546 } 8547 8548 void cancel() { 8549 if (!mCancelled) { 8550 removeCallbacks(Blink.this); 8551 mCancelled = true; 8552 } 8553 } 8554 8555 void uncancel() { 8556 mCancelled = false; 8557 } 8558 } 8559 8560 /** 8561 * @return True when the TextView isFocused and has a valid zero-length selection (cursor). 8562 */ 8563 private boolean shouldBlink() { 8564 if (!isFocused()) return false; 8565 8566 final int start = getSelectionStart(); 8567 if (start < 0) return false; 8568 8569 final int end = getSelectionEnd(); 8570 if (end < 0) return false; 8571 8572 return start == end; 8573 } 8574 8575 private void makeBlink() { 8576 if (isCursorVisible()) { 8577 if (shouldBlink()) { 8578 mShowCursor = SystemClock.uptimeMillis(); 8579 if (mBlink == null) mBlink = new Blink(this); 8580 mBlink.removeCallbacks(mBlink); 8581 mBlink.postAtTime(mBlink, mShowCursor + BLINK); 8582 } 8583 } else { 8584 if (mBlink != null) mBlink.removeCallbacks(mBlink); 8585 } 8586 } 8587 8588 @Override 8589 protected float getLeftFadingEdgeStrength() { 8590 if (mCurrentAlpha <= ViewConfiguration.ALPHA_THRESHOLD_INT) return 0.0f; 8591 if (mEllipsize == TextUtils.TruncateAt.MARQUEE && 8592 mMarqueeFadeMode != MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { 8593 if (mMarquee != null && !mMarquee.isStopped()) { 8594 final Marquee marquee = mMarquee; 8595 if (marquee.shouldDrawLeftFade()) { 8596 return marquee.mScroll / getHorizontalFadingEdgeLength(); 8597 } else { 8598 return 0.0f; 8599 } 8600 } else if (getLineCount() == 1) { 8601 final int layoutDirection = getResolvedLayoutDirection(); 8602 final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection); 8603 switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) { 8604 case Gravity.LEFT: 8605 return 0.0f; 8606 case Gravity.RIGHT: 8607 return (mLayout.getLineRight(0) - (mRight - mLeft) - 8608 getCompoundPaddingLeft() - getCompoundPaddingRight() - 8609 mLayout.getLineLeft(0)) / getHorizontalFadingEdgeLength(); 8610 case Gravity.CENTER_HORIZONTAL: 8611 return 0.0f; 8612 } 8613 } 8614 } 8615 return super.getLeftFadingEdgeStrength(); 8616 } 8617 8618 @Override 8619 protected float getRightFadingEdgeStrength() { 8620 if (mCurrentAlpha <= ViewConfiguration.ALPHA_THRESHOLD_INT) return 0.0f; 8621 if (mEllipsize == TextUtils.TruncateAt.MARQUEE && 8622 mMarqueeFadeMode != MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { 8623 if (mMarquee != null && !mMarquee.isStopped()) { 8624 final Marquee marquee = mMarquee; 8625 return (marquee.mMaxFadeScroll - marquee.mScroll) / getHorizontalFadingEdgeLength(); 8626 } else if (getLineCount() == 1) { 8627 final int layoutDirection = getResolvedLayoutDirection(); 8628 final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection); 8629 switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) { 8630 case Gravity.LEFT: 8631 final int textWidth = (mRight - mLeft) - getCompoundPaddingLeft() - 8632 getCompoundPaddingRight(); 8633 final float lineWidth = mLayout.getLineWidth(0); 8634 return (lineWidth - textWidth) / getHorizontalFadingEdgeLength(); 8635 case Gravity.RIGHT: 8636 return 0.0f; 8637 case Gravity.CENTER_HORIZONTAL: 8638 case Gravity.FILL_HORIZONTAL: 8639 return (mLayout.getLineWidth(0) - ((mRight - mLeft) - 8640 getCompoundPaddingLeft() - getCompoundPaddingRight())) / 8641 getHorizontalFadingEdgeLength(); 8642 } 8643 } 8644 } 8645 return super.getRightFadingEdgeStrength(); 8646 } 8647 8648 @Override 8649 protected int computeHorizontalScrollRange() { 8650 if (mLayout != null) { 8651 return mSingleLine && (mGravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.LEFT ? 8652 (int) mLayout.getLineWidth(0) : mLayout.getWidth(); 8653 } 8654 8655 return super.computeHorizontalScrollRange(); 8656 } 8657 8658 @Override 8659 protected int computeVerticalScrollRange() { 8660 if (mLayout != null) 8661 return mLayout.getHeight(); 8662 8663 return super.computeVerticalScrollRange(); 8664 } 8665 8666 @Override 8667 protected int computeVerticalScrollExtent() { 8668 return getHeight() - getCompoundPaddingTop() - getCompoundPaddingBottom(); 8669 } 8670 8671 @Override 8672 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched) { 8673 if (TextUtils.isEmpty(searched)) { 8674 return; 8675 } 8676 CharSequence thisText = getText(); 8677 if (TextUtils.isEmpty(thisText)) { 8678 return; 8679 } 8680 String searchedLowerCase = searched.toString().toLowerCase(); 8681 String thisTextLowerCase = thisText.toString().toLowerCase(); 8682 if (thisTextLowerCase.contains(searchedLowerCase)) { 8683 outViews.add(this); 8684 } 8685 } 8686 8687 public enum BufferType { 8688 NORMAL, SPANNABLE, EDITABLE, 8689 } 8690 8691 /** 8692 * Returns the TextView_textColor attribute from the 8693 * Resources.StyledAttributes, if set, or the TextAppearance_textColor 8694 * from the TextView_textAppearance attribute, if TextView_textColor 8695 * was not set directly. 8696 */ 8697 public static ColorStateList getTextColors(Context context, TypedArray attrs) { 8698 ColorStateList colors; 8699 colors = attrs.getColorStateList(com.android.internal.R.styleable. 8700 TextView_textColor); 8701 8702 if (colors == null) { 8703 int ap = attrs.getResourceId(com.android.internal.R.styleable. 8704 TextView_textAppearance, -1); 8705 if (ap != -1) { 8706 TypedArray appearance; 8707 appearance = context.obtainStyledAttributes(ap, 8708 com.android.internal.R.styleable.TextAppearance); 8709 colors = appearance.getColorStateList(com.android.internal.R.styleable. 8710 TextAppearance_textColor); 8711 appearance.recycle(); 8712 } 8713 } 8714 8715 return colors; 8716 } 8717 8718 /** 8719 * Returns the default color from the TextView_textColor attribute 8720 * from the AttributeSet, if set, or the default color from the 8721 * TextAppearance_textColor from the TextView_textAppearance attribute, 8722 * if TextView_textColor was not set directly. 8723 */ 8724 public static int getTextColor(Context context, 8725 TypedArray attrs, 8726 int def) { 8727 ColorStateList colors = getTextColors(context, attrs); 8728 8729 if (colors == null) { 8730 return def; 8731 } else { 8732 return colors.getDefaultColor(); 8733 } 8734 } 8735 8736 @Override 8737 public boolean onKeyShortcut(int keyCode, KeyEvent event) { 8738 final int filteredMetaState = event.getMetaState() & ~KeyEvent.META_CTRL_MASK; 8739 if (KeyEvent.metaStateHasNoModifiers(filteredMetaState)) { 8740 switch (keyCode) { 8741 case KeyEvent.KEYCODE_A: 8742 if (canSelectText()) { 8743 return onTextContextMenuItem(ID_SELECT_ALL); 8744 } 8745 break; 8746 case KeyEvent.KEYCODE_X: 8747 if (canCut()) { 8748 return onTextContextMenuItem(ID_CUT); 8749 } 8750 break; 8751 case KeyEvent.KEYCODE_C: 8752 if (canCopy()) { 8753 return onTextContextMenuItem(ID_COPY); 8754 } 8755 break; 8756 case KeyEvent.KEYCODE_V: 8757 if (canPaste()) { 8758 return onTextContextMenuItem(ID_PASTE); 8759 } 8760 break; 8761 } 8762 } 8763 return super.onKeyShortcut(keyCode, event); 8764 } 8765 8766 /** 8767 * Unlike {@link #textCanBeSelected()}, this method is based on the <i>current</i> state of the 8768 * TextView. {@link #textCanBeSelected()} has to be true (this is one of the conditions to have 8769 * a selection controller (see {@link #prepareCursorControllers()}), but this is not sufficient. 8770 */ 8771 private boolean canSelectText() { 8772 return hasSelectionController() && mText.length() != 0; 8773 } 8774 8775 /** 8776 * Test based on the <i>intrinsic</i> charateristics of the TextView. 8777 * The text must be spannable and the movement method must allow for arbitary selection. 8778 * 8779 * See also {@link #canSelectText()}. 8780 */ 8781 private boolean textCanBeSelected() { 8782 // prepareCursorController() relies on this method. 8783 // If you change this condition, make sure prepareCursorController is called anywhere 8784 // the value of this condition might be changed. 8785 if (mMovement == null || !mMovement.canSelectArbitrarily()) return false; 8786 return isTextEditable() || (mTextIsSelectable && mText instanceof Spannable && isEnabled()); 8787 } 8788 8789 private boolean canCut() { 8790 if (hasPasswordTransformationMethod()) { 8791 return false; 8792 } 8793 8794 if (mText.length() > 0 && hasSelection() && mText instanceof Editable && mInput != null) { 8795 return true; 8796 } 8797 8798 return false; 8799 } 8800 8801 private boolean canCopy() { 8802 if (hasPasswordTransformationMethod()) { 8803 return false; 8804 } 8805 8806 if (mText.length() > 0 && hasSelection()) { 8807 return true; 8808 } 8809 8810 return false; 8811 } 8812 8813 private boolean canPaste() { 8814 return (mText instanceof Editable && 8815 mInput != null && 8816 getSelectionStart() >= 0 && 8817 getSelectionEnd() >= 0 && 8818 ((ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE)). 8819 hasPrimaryClip()); 8820 } 8821 8822 private static long packRangeInLong(int start, int end) { 8823 return (((long) start) << 32) | end; 8824 } 8825 8826 private static int extractRangeStartFromLong(long range) { 8827 return (int) (range >>> 32); 8828 } 8829 8830 private static int extractRangeEndFromLong(long range) { 8831 return (int) (range & 0x00000000FFFFFFFFL); 8832 } 8833 8834 private boolean selectAll() { 8835 final int length = mText.length(); 8836 Selection.setSelection((Spannable) mText, 0, length); 8837 return length > 0; 8838 } 8839 8840 /** 8841 * Adjusts selection to the word under last touch offset. 8842 * Return true if the operation was successfully performed. 8843 */ 8844 private boolean selectCurrentWord() { 8845 if (!canSelectText()) { 8846 return false; 8847 } 8848 8849 if (hasPasswordTransformationMethod()) { 8850 // Always select all on a password field. 8851 // Cut/copy menu entries are not available for passwords, but being able to select all 8852 // is however useful to delete or paste to replace the entire content. 8853 return selectAll(); 8854 } 8855 8856 int klass = mInputType & InputType.TYPE_MASK_CLASS; 8857 int variation = mInputType & InputType.TYPE_MASK_VARIATION; 8858 8859 // Specific text field types: select the entire text for these 8860 if (klass == InputType.TYPE_CLASS_NUMBER || 8861 klass == InputType.TYPE_CLASS_PHONE || 8862 klass == InputType.TYPE_CLASS_DATETIME || 8863 variation == InputType.TYPE_TEXT_VARIATION_URI || 8864 variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS || 8865 variation == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS || 8866 variation == InputType.TYPE_TEXT_VARIATION_FILTER) { 8867 return selectAll(); 8868 } 8869 8870 long lastTouchOffsets = getLastTouchOffsets(); 8871 final int minOffset = extractRangeStartFromLong(lastTouchOffsets); 8872 final int maxOffset = extractRangeEndFromLong(lastTouchOffsets); 8873 8874 // Safety check in case standard touch event handling has been bypassed 8875 if (minOffset < 0 || minOffset >= mText.length()) return false; 8876 if (maxOffset < 0 || maxOffset >= mText.length()) return false; 8877 8878 int selectionStart, selectionEnd; 8879 8880 // If a URLSpan (web address, email, phone...) is found at that position, select it. 8881 URLSpan[] urlSpans = ((Spanned) mText).getSpans(minOffset, maxOffset, URLSpan.class); 8882 if (urlSpans.length == 1) { 8883 URLSpan url = urlSpans[0]; 8884 selectionStart = ((Spanned) mText).getSpanStart(url); 8885 selectionEnd = ((Spanned) mText).getSpanEnd(url); 8886 } else { 8887 WordIterator wordIterator = getWordIterator(); 8888 // WordIterator handles text changes, this is a no-op if text in unchanged. 8889 wordIterator.setCharSequence(mText); 8890 8891 selectionStart = wordIterator.getBeginning(minOffset); 8892 if (selectionStart == BreakIterator.DONE) return false; 8893 8894 selectionEnd = wordIterator.getEnd(maxOffset); 8895 if (selectionEnd == BreakIterator.DONE) return false; 8896 } 8897 8898 Selection.setSelection((Spannable) mText, selectionStart, selectionEnd); 8899 return true; 8900 } 8901 8902 WordIterator getWordIterator() { 8903 if (mWordIterator == null) { 8904 mWordIterator = new WordIterator(); 8905 } 8906 return mWordIterator; 8907 } 8908 8909 private SpellChecker getSpellChecker() { 8910 if (mSpellChecker == null) { 8911 mSpellChecker = new SpellChecker(this); 8912 } 8913 return mSpellChecker; 8914 } 8915 8916 private long getLastTouchOffsets() { 8917 int minOffset, maxOffset; 8918 8919 if (mContextMenuTriggeredByKey) { 8920 minOffset = getSelectionStart(); 8921 maxOffset = getSelectionEnd(); 8922 } else { 8923 SelectionModifierCursorController selectionController = getSelectionController(); 8924 minOffset = selectionController.getMinTouchOffset(); 8925 maxOffset = selectionController.getMaxTouchOffset(); 8926 } 8927 8928 return packRangeInLong(minOffset, maxOffset); 8929 } 8930 8931 @Override 8932 public void onPopulateAccessibilityEvent(AccessibilityEvent event) { 8933 super.onPopulateAccessibilityEvent(event); 8934 8935 final boolean isPassword = hasPasswordTransformationMethod(); 8936 if (!isPassword) { 8937 CharSequence text = getText(); 8938 if (TextUtils.isEmpty(text)) { 8939 text = getHint(); 8940 } 8941 if (TextUtils.isEmpty(text)) { 8942 text = getContentDescription(); 8943 } 8944 if (!TextUtils.isEmpty(text)) { 8945 event.getText().add(text); 8946 } 8947 } 8948 } 8949 8950 @Override 8951 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 8952 super.onInitializeAccessibilityEvent(event); 8953 8954 final boolean isPassword = hasPasswordTransformationMethod(); 8955 event.setPassword(isPassword); 8956 8957 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED) { 8958 event.setFromIndex(Selection.getSelectionStart(mText)); 8959 event.setToIndex(Selection.getSelectionEnd(mText)); 8960 event.setItemCount(mText.length()); 8961 } 8962 } 8963 8964 @Override 8965 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 8966 super.onInitializeAccessibilityNodeInfo(info); 8967 8968 final boolean isPassword = hasPasswordTransformationMethod(); 8969 if (!isPassword) { 8970 info.setText(getText()); 8971 } 8972 info.setPassword(isPassword); 8973 } 8974 8975 @Override 8976 public void sendAccessibilityEvent(int eventType) { 8977 // Do not send scroll events since first they are not interesting for 8978 // accessibility and second such events a generated too frequently. 8979 // For details see the implementation of bringTextIntoView(). 8980 if (eventType == AccessibilityEvent.TYPE_VIEW_SCROLLED) { 8981 return; 8982 } 8983 super.sendAccessibilityEvent(eventType); 8984 } 8985 8986 void sendAccessibilityEventTypeViewTextChanged(CharSequence beforeText, 8987 int fromIndex, int removedCount, int addedCount) { 8988 AccessibilityEvent event = 8989 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED); 8990 event.setFromIndex(fromIndex); 8991 event.setRemovedCount(removedCount); 8992 event.setAddedCount(addedCount); 8993 event.setBeforeText(beforeText); 8994 sendAccessibilityEventUnchecked(event); 8995 } 8996 8997 @Override 8998 protected void onCreateContextMenu(ContextMenu menu) { 8999 super.onCreateContextMenu(menu); 9000 boolean added = false; 9001 mContextMenuTriggeredByKey = mDPadCenterIsDown || mEnterKeyIsDown; 9002 // Problem with context menu on long press: the menu appears while the key in down and when 9003 // the key is released, the view does not receive the key_up event. 9004 // We need two layers of flags: mDPadCenterIsDown and mEnterKeyIsDown are set in key down/up 9005 // events. We cannot simply clear these flags in onTextContextMenuItem since 9006 // it may not be called (if the user/ discards the context menu with the back key). 9007 // We clear these flags here and mContextMenuTriggeredByKey saves that state so that it is 9008 // available in onTextContextMenuItem. 9009 mDPadCenterIsDown = mEnterKeyIsDown = false; 9010 9011 MenuHandler handler = new MenuHandler(); 9012 9013 if (mText instanceof Spanned && hasSelectionController()) { 9014 long lastTouchOffset = getLastTouchOffsets(); 9015 final int selStart = extractRangeStartFromLong(lastTouchOffset); 9016 final int selEnd = extractRangeEndFromLong(lastTouchOffset); 9017 9018 URLSpan[] urls = ((Spanned) mText).getSpans(selStart, selEnd, URLSpan.class); 9019 if (urls.length > 0) { 9020 menu.add(0, ID_COPY_URL, 0, com.android.internal.R.string.copyUrl). 9021 setOnMenuItemClickListener(handler); 9022 9023 added = true; 9024 } 9025 } 9026 9027 // The context menu is not empty, which will prevent the selection mode from starting. 9028 // Add a entry to start it in the context menu. 9029 // TODO Does not handle the case where a subclass does not call super.thisMethod or 9030 // populates the menu AFTER this call. 9031 if (menu.size() > 0) { 9032 menu.add(0, ID_SELECTION_MODE, 0, com.android.internal.R.string.selectTextMode). 9033 setOnMenuItemClickListener(handler); 9034 added = true; 9035 } 9036 9037 if (added) { 9038 menu.setHeaderTitle(com.android.internal.R.string.editTextMenuTitle); 9039 } 9040 } 9041 9042 /** 9043 * Returns whether this text view is a current input method target. The 9044 * default implementation just checks with {@link InputMethodManager}. 9045 */ 9046 public boolean isInputMethodTarget() { 9047 InputMethodManager imm = InputMethodManager.peekInstance(); 9048 return imm != null && imm.isActive(this); 9049 } 9050 9051 // Selection context mode 9052 private static final int ID_SELECT_ALL = android.R.id.selectAll; 9053 private static final int ID_CUT = android.R.id.cut; 9054 private static final int ID_COPY = android.R.id.copy; 9055 private static final int ID_PASTE = android.R.id.paste; 9056 // Context menu entries 9057 private static final int ID_COPY_URL = android.R.id.copyUrl; 9058 private static final int ID_SELECTION_MODE = android.R.id.selectTextMode; 9059 9060 private class MenuHandler implements MenuItem.OnMenuItemClickListener { 9061 public boolean onMenuItemClick(MenuItem item) { 9062 return onTextContextMenuItem(item.getItemId()); 9063 } 9064 } 9065 9066 /** 9067 * Called when a context menu option for the text view is selected. Currently 9068 * this will be {@link android.R.id#copyUrl}, {@link android.R.id#selectTextMode}, 9069 * {@link android.R.id#selectAll}, {@link android.R.id#paste}, {@link android.R.id#cut} 9070 * or {@link android.R.id#copy}. 9071 * 9072 * @return true if the context menu item action was performed. 9073 */ 9074 public boolean onTextContextMenuItem(int id) { 9075 int min = 0; 9076 int max = mText.length(); 9077 9078 if (isFocused()) { 9079 final int selStart = getSelectionStart(); 9080 final int selEnd = getSelectionEnd(); 9081 9082 min = Math.max(0, Math.min(selStart, selEnd)); 9083 max = Math.max(0, Math.max(selStart, selEnd)); 9084 } 9085 9086 switch (id) { 9087 case ID_COPY_URL: 9088 URLSpan[] urls = ((Spanned) mText).getSpans(min, max, URLSpan.class); 9089 if (urls.length >= 1) { 9090 ClipData clip = null; 9091 for (int i=0; i<urls.length; i++) { 9092 Uri uri = Uri.parse(urls[0].getURL()); 9093 if (clip == null) { 9094 clip = ClipData.newRawUri(null, uri); 9095 } else { 9096 clip.addItem(new ClipData.Item(uri)); 9097 } 9098 } 9099 if (clip != null) { 9100 setPrimaryClip(clip); 9101 } 9102 } 9103 stopSelectionActionMode(); 9104 return true; 9105 9106 case ID_SELECTION_MODE: 9107 if (mSelectionActionMode != null) { 9108 // Selection mode is already started, simply change selected part. 9109 selectCurrentWord(); 9110 } else { 9111 startSelectionActionMode(); 9112 } 9113 return true; 9114 9115 case ID_SELECT_ALL: 9116 // This does not enter text selection mode. Text is highlighted, so that it can be 9117 // bulk edited, like selectAllOnFocus does. Returns true even if text is empty. 9118 selectAll(); 9119 return true; 9120 9121 case ID_PASTE: 9122 paste(min, max); 9123 return true; 9124 9125 case ID_CUT: 9126 setPrimaryClip(ClipData.newPlainText(null, mTransformed.subSequence(min, max))); 9127 ((Editable) mText).delete(min, max); 9128 stopSelectionActionMode(); 9129 return true; 9130 9131 case ID_COPY: 9132 setPrimaryClip(ClipData.newPlainText(null, mTransformed.subSequence(min, max))); 9133 stopSelectionActionMode(); 9134 return true; 9135 } 9136 return false; 9137 } 9138 9139 /** 9140 * Prepare text so that there are not zero or two spaces at beginning and end of region defined 9141 * by [min, max] when replacing this region by paste. 9142 * Note that if there were two spaces (or more) at that position before, they are kept. We just 9143 * make sure we do not add an extra one from the paste content. 9144 */ 9145 private long prepareSpacesAroundPaste(int min, int max, CharSequence paste) { 9146 if (paste.length() > 0) { 9147 if (min > 0) { 9148 final char charBefore = mTransformed.charAt(min - 1); 9149 final char charAfter = paste.charAt(0); 9150 9151 if (Character.isSpaceChar(charBefore) && Character.isSpaceChar(charAfter)) { 9152 // Two spaces at beginning of paste: remove one 9153 final int originalLength = mText.length(); 9154 ((Editable) mText).delete(min - 1, min); 9155 // Due to filters, there is no guarantee that exactly one character was 9156 // removed: count instead. 9157 final int delta = mText.length() - originalLength; 9158 min += delta; 9159 max += delta; 9160 } else if (!Character.isSpaceChar(charBefore) && charBefore != '\n' && 9161 !Character.isSpaceChar(charAfter) && charAfter != '\n') { 9162 // No space at beginning of paste: add one 9163 final int originalLength = mText.length(); 9164 ((Editable) mText).replace(min, min, " "); 9165 // Taking possible filters into account as above. 9166 final int delta = mText.length() - originalLength; 9167 min += delta; 9168 max += delta; 9169 } 9170 } 9171 9172 if (max < mText.length()) { 9173 final char charBefore = paste.charAt(paste.length() - 1); 9174 final char charAfter = mTransformed.charAt(max); 9175 9176 if (Character.isSpaceChar(charBefore) && Character.isSpaceChar(charAfter)) { 9177 // Two spaces at end of paste: remove one 9178 ((Editable) mText).delete(max, max + 1); 9179 } else if (!Character.isSpaceChar(charBefore) && charBefore != '\n' && 9180 !Character.isSpaceChar(charAfter) && charAfter != '\n') { 9181 // No space at end of paste: add one 9182 ((Editable) mText).replace(max, max, " "); 9183 } 9184 } 9185 } 9186 9187 return packRangeInLong(min, max); 9188 } 9189 9190 private DragShadowBuilder getTextThumbnailBuilder(CharSequence text) { 9191 TextView shadowView = (TextView) inflate(mContext, 9192 com.android.internal.R.layout.text_drag_thumbnail, null); 9193 9194 if (shadowView == null) { 9195 throw new IllegalArgumentException("Unable to inflate text drag thumbnail"); 9196 } 9197 9198 if (text.length() > DRAG_SHADOW_MAX_TEXT_LENGTH) { 9199 text = text.subSequence(0, DRAG_SHADOW_MAX_TEXT_LENGTH); 9200 } 9201 shadowView.setText(text); 9202 shadowView.setTextColor(getTextColors()); 9203 9204 shadowView.setTextAppearance(mContext, R.styleable.Theme_textAppearanceLarge); 9205 shadowView.setGravity(Gravity.CENTER); 9206 9207 shadowView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 9208 ViewGroup.LayoutParams.WRAP_CONTENT)); 9209 9210 final int size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 9211 shadowView.measure(size, size); 9212 9213 shadowView.layout(0, 0, shadowView.getMeasuredWidth(), shadowView.getMeasuredHeight()); 9214 shadowView.invalidate(); 9215 return new DragShadowBuilder(shadowView); 9216 } 9217 9218 private static class DragLocalState { 9219 public TextView sourceTextView; 9220 public int start, end; 9221 9222 public DragLocalState(TextView sourceTextView, int start, int end) { 9223 this.sourceTextView = sourceTextView; 9224 this.start = start; 9225 this.end = end; 9226 } 9227 } 9228 9229 @Override 9230 public boolean performLongClick() { 9231 boolean handled = false; 9232 boolean vibrate = true; 9233 9234 if (super.performLongClick()) { 9235 mDiscardNextActionUp = true; 9236 handled = true; 9237 } 9238 9239 // Long press in empty space moves cursor and shows the Paste affordance if available. 9240 if (!handled && !isPositionOnText(mLastDownPositionX, mLastDownPositionY) && 9241 mInsertionControllerEnabled) { 9242 final int offset = getOffsetForPosition(mLastDownPositionX, mLastDownPositionY); 9243 stopSelectionActionMode(); 9244 Selection.setSelection((Spannable) mText, offset); 9245 getInsertionController().showWithActionPopup(); 9246 handled = true; 9247 vibrate = false; 9248 } 9249 9250 if (!handled && mSelectionActionMode != null) { 9251 if (touchPositionIsInSelection()) { 9252 // Start a drag 9253 final int start = getSelectionStart(); 9254 final int end = getSelectionEnd(); 9255 CharSequence selectedText = mTransformed.subSequence(start, end); 9256 ClipData data = ClipData.newPlainText(null, selectedText); 9257 DragLocalState localState = new DragLocalState(this, start, end); 9258 startDrag(data, getTextThumbnailBuilder(selectedText), localState, 0); 9259 stopSelectionActionMode(); 9260 } else { 9261 getSelectionController().hide(); 9262 selectCurrentWord(); 9263 getSelectionController().show(); 9264 } 9265 handled = true; 9266 } 9267 9268 // Start a new selection 9269 if (!handled) { 9270 handled = startSelectionActionMode(); 9271 } 9272 9273 if (vibrate) { 9274 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); 9275 } 9276 9277 if (handled) { 9278 mDiscardNextActionUp = true; 9279 } 9280 9281 return handled; 9282 } 9283 9284 private boolean touchPositionIsInSelection() { 9285 int selectionStart = getSelectionStart(); 9286 int selectionEnd = getSelectionEnd(); 9287 9288 if (selectionStart == selectionEnd) { 9289 return false; 9290 } 9291 9292 if (selectionStart > selectionEnd) { 9293 int tmp = selectionStart; 9294 selectionStart = selectionEnd; 9295 selectionEnd = tmp; 9296 Selection.setSelection((Spannable) mText, selectionStart, selectionEnd); 9297 } 9298 9299 SelectionModifierCursorController selectionController = getSelectionController(); 9300 int minOffset = selectionController.getMinTouchOffset(); 9301 int maxOffset = selectionController.getMaxTouchOffset(); 9302 9303 return ((minOffset >= selectionStart) && (maxOffset < selectionEnd)); 9304 } 9305 9306 private PositionListener getPositionListener() { 9307 if (mPositionListener == null) { 9308 mPositionListener = new PositionListener(); 9309 } 9310 return mPositionListener; 9311 } 9312 9313 private interface TextViewPositionListener { 9314 public void updatePosition(int parentPositionX, int parentPositionY, 9315 boolean parentPositionChanged, boolean parentScrolled); 9316 } 9317 9318 private class PositionListener implements ViewTreeObserver.OnPreDrawListener { 9319 // 3 handles 9320 // 3 ActionPopup [replace, suggestion, easyedit] (suggestionsPopup first hides the others) 9321 private final int MAXIMUM_NUMBER_OF_LISTENERS = 6; 9322 private TextViewPositionListener[] mPositionListeners = 9323 new TextViewPositionListener[MAXIMUM_NUMBER_OF_LISTENERS]; 9324 private boolean mCanMove[] = new boolean[MAXIMUM_NUMBER_OF_LISTENERS]; 9325 private boolean mPositionHasChanged = true; 9326 // Absolute position of the TextView with respect to its parent window 9327 private int mPositionX, mPositionY; 9328 private int mNumberOfListeners; 9329 private boolean mScrollHasChanged; 9330 9331 public void addSubscriber(TextViewPositionListener positionListener, boolean canMove) { 9332 if (mNumberOfListeners == 0) { 9333 updatePosition(); 9334 ViewTreeObserver vto = TextView.this.getViewTreeObserver(); 9335 vto.addOnPreDrawListener(this); 9336 } 9337 9338 int emptySlotIndex = -1; 9339 for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { 9340 TextViewPositionListener listener = mPositionListeners[i]; 9341 if (listener == positionListener) { 9342 return; 9343 } else if (emptySlotIndex < 0 && listener == null) { 9344 emptySlotIndex = i; 9345 } 9346 } 9347 9348 mPositionListeners[emptySlotIndex] = positionListener; 9349 mCanMove[emptySlotIndex] = canMove; 9350 mNumberOfListeners++; 9351 } 9352 9353 public void removeSubscriber(TextViewPositionListener positionListener) { 9354 for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { 9355 if (mPositionListeners[i] == positionListener) { 9356 mPositionListeners[i] = null; 9357 mNumberOfListeners--; 9358 break; 9359 } 9360 } 9361 9362 if (mNumberOfListeners == 0) { 9363 ViewTreeObserver vto = TextView.this.getViewTreeObserver(); 9364 vto.removeOnPreDrawListener(this); 9365 } 9366 } 9367 9368 public int getPositionX() { 9369 return mPositionX; 9370 } 9371 9372 public int getPositionY() { 9373 return mPositionY; 9374 } 9375 9376 @Override 9377 public boolean onPreDraw() { 9378 updatePosition(); 9379 9380 for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { 9381 if (mPositionHasChanged || mScrollHasChanged || mCanMove[i]) { 9382 TextViewPositionListener positionListener = mPositionListeners[i]; 9383 if (positionListener != null) { 9384 positionListener.updatePosition(mPositionX, mPositionY, 9385 mPositionHasChanged, mScrollHasChanged); 9386 } 9387 } 9388 } 9389 9390 mScrollHasChanged = false; 9391 return true; 9392 } 9393 9394 private void updatePosition() { 9395 TextView.this.getLocationInWindow(mTempCoords); 9396 9397 mPositionHasChanged = mTempCoords[0] != mPositionX || mTempCoords[1] != mPositionY; 9398 9399 mPositionX = mTempCoords[0]; 9400 mPositionY = mTempCoords[1]; 9401 } 9402 9403 public boolean isVisible(int positionX, int positionY) { 9404 final TextView textView = TextView.this; 9405 9406 if (mTempRect == null) mTempRect = new Rect(); 9407 final Rect clip = mTempRect; 9408 clip.left = getCompoundPaddingLeft(); 9409 clip.top = getExtendedPaddingTop(); 9410 clip.right = textView.getWidth() - getCompoundPaddingRight(); 9411 clip.bottom = textView.getHeight() - getExtendedPaddingBottom(); 9412 9413 final ViewParent parent = textView.getParent(); 9414 if (parent == null || !parent.getChildVisibleRect(textView, clip, null)) { 9415 return false; 9416 } 9417 9418 int posX = mPositionX + positionX; 9419 int posY = mPositionY + positionY; 9420 9421 // Offset by 1 to take into account 0.5 and int rounding around getPrimaryHorizontal. 9422 return posX >= clip.left - 1 && posX <= clip.right + 1 && 9423 posY >= clip.top && posY <= clip.bottom; 9424 } 9425 9426 public boolean isOffsetVisible(int offset) { 9427 final int line = mLayout.getLineForOffset(offset); 9428 final int lineBottom = mLayout.getLineBottom(line); 9429 final int primaryHorizontal = (int) mLayout.getPrimaryHorizontal(offset); 9430 return isVisible(primaryHorizontal, lineBottom); 9431 } 9432 9433 public void onScrollChanged() { 9434 mScrollHasChanged = true; 9435 } 9436 } 9437 9438 @Override 9439 protected void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) { 9440 super.onScrollChanged(horiz, vert, oldHoriz, oldVert); 9441 if (mPositionListener != null) { 9442 mPositionListener.onScrollChanged(); 9443 } 9444 } 9445 9446 private abstract class PinnedPopupWindow implements TextViewPositionListener { 9447 protected PopupWindow mPopupWindow; 9448 protected ViewGroup mContentView; 9449 int mPositionX, mPositionY; 9450 9451 protected abstract void createPopupWindow(); 9452 protected abstract void initContentView(); 9453 protected abstract int getTextOffset(); 9454 protected abstract int getVerticalLocalPosition(int line); 9455 protected abstract int clipVertically(int positionY); 9456 9457 public PinnedPopupWindow() { 9458 createPopupWindow(); 9459 9460 mPopupWindow.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL); 9461 mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); 9462 mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); 9463 9464 initContentView(); 9465 9466 LayoutParams wrapContent = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 9467 ViewGroup.LayoutParams.WRAP_CONTENT); 9468 mContentView.setLayoutParams(wrapContent); 9469 9470 mPopupWindow.setContentView(mContentView); 9471 } 9472 9473 public void show() { 9474 TextView.this.getPositionListener().addSubscriber(this, false /* offset is fixed */); 9475 9476 computeLocalPosition(); 9477 9478 final PositionListener positionListener = TextView.this.getPositionListener(); 9479 updatePosition(positionListener.getPositionX(), positionListener.getPositionY()); 9480 } 9481 9482 protected void measureContent() { 9483 final DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); 9484 mContentView.measure( 9485 View.MeasureSpec.makeMeasureSpec(displayMetrics.widthPixels, 9486 View.MeasureSpec.AT_MOST), 9487 View.MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels, 9488 View.MeasureSpec.AT_MOST)); 9489 } 9490 9491 /* The popup window will be horizontally centered on the getTextOffset() and vertically 9492 * positioned according to viewportToContentHorizontalOffset. 9493 * 9494 * This method assumes that mContentView has properly been measured from its content. */ 9495 private void computeLocalPosition() { 9496 measureContent(); 9497 final int width = mContentView.getMeasuredWidth(); 9498 final int offset = getTextOffset(); 9499 mPositionX = (int) (mLayout.getPrimaryHorizontal(offset) - width / 2.0f); 9500 mPositionX += viewportToContentHorizontalOffset(); 9501 9502 final int line = mLayout.getLineForOffset(offset); 9503 mPositionY = getVerticalLocalPosition(line); 9504 mPositionY += viewportToContentVerticalOffset(); 9505 } 9506 9507 private void updatePosition(int parentPositionX, int parentPositionY) { 9508 int positionX = parentPositionX + mPositionX; 9509 int positionY = parentPositionY + mPositionY; 9510 9511 positionY = clipVertically(positionY); 9512 9513 // Horizontal clipping 9514 final DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); 9515 final int width = mContentView.getMeasuredWidth(); 9516 positionX = Math.min(displayMetrics.widthPixels - width, positionX); 9517 positionX = Math.max(0, positionX); 9518 9519 if (isShowing()) { 9520 mPopupWindow.update(positionX, positionY, -1, -1); 9521 } else { 9522 mPopupWindow.showAtLocation(TextView.this, Gravity.NO_GRAVITY, 9523 positionX, positionY); 9524 } 9525 } 9526 9527 public void hide() { 9528 mPopupWindow.dismiss(); 9529 TextView.this.getPositionListener().removeSubscriber(this); 9530 } 9531 9532 @Override 9533 public void updatePosition(int parentPositionX, int parentPositionY, 9534 boolean parentPositionChanged, boolean parentScrolled) { 9535 // Either parentPositionChanged or parentScrolled is true, check if still visible 9536 if (isShowing() && getPositionListener().isOffsetVisible(getTextOffset())) { 9537 if (parentScrolled) computeLocalPosition(); 9538 updatePosition(parentPositionX, parentPositionY); 9539 } else { 9540 hide(); 9541 } 9542 } 9543 9544 public boolean isShowing() { 9545 return mPopupWindow.isShowing(); 9546 } 9547 } 9548 9549 private class SuggestionsPopupWindow extends PinnedPopupWindow implements OnItemClickListener { 9550 private static final int MAX_NUMBER_SUGGESTIONS = SuggestionSpan.SUGGESTIONS_MAX_SIZE; 9551 private SuggestionInfo[] mSuggestionInfos; 9552 private int mNumberOfSuggestions; 9553 private boolean mCursorWasVisibleBeforeSuggestions; 9554 private SuggestionAdapter mSuggestionsAdapter; 9555 9556 private class CustomPopupWindow extends PopupWindow { 9557 public CustomPopupWindow(Context context, int defStyle) { 9558 super(context, null, defStyle); 9559 } 9560 9561 @Override 9562 public void dismiss() { 9563 super.dismiss(); 9564 9565 TextView.this.getPositionListener().removeSubscriber(SuggestionsPopupWindow.this); 9566 9567 if ((mText instanceof Spannable)) { 9568 ((Spannable) mText).removeSpan(mSuggestionRangeSpan); 9569 } 9570 9571 setCursorVisible(mCursorWasVisibleBeforeSuggestions); 9572 if (hasInsertionController()) { 9573 getInsertionController().show(); 9574 } 9575 } 9576 } 9577 9578 public SuggestionsPopupWindow() { 9579 mCursorWasVisibleBeforeSuggestions = mCursorVisible; 9580 } 9581 9582 @Override 9583 protected void createPopupWindow() { 9584 mPopupWindow = new CustomPopupWindow(TextView.this.mContext, 9585 com.android.internal.R.attr.textSuggestionsWindowStyle); 9586 mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); 9587 mPopupWindow.setFocusable(true); 9588 mPopupWindow.setClippingEnabled(false); 9589 } 9590 9591 @Override 9592 protected void initContentView() { 9593 ListView listView = new ListView(TextView.this.getContext()); 9594 mSuggestionsAdapter = new SuggestionAdapter(); 9595 listView.setAdapter(mSuggestionsAdapter); 9596 listView.setOnItemClickListener(this); 9597 mContentView = listView; 9598 9599 // Inflate the suggestion items once and for all. +1 for add to dictionary 9600 mSuggestionInfos = new SuggestionInfo[MAX_NUMBER_SUGGESTIONS + 1]; 9601 for (int i = 0; i < MAX_NUMBER_SUGGESTIONS + 1; i++) { 9602 mSuggestionInfos[i] = new SuggestionInfo(); 9603 } 9604 } 9605 9606 private class SuggestionInfo { 9607 int suggestionStart, suggestionEnd; // range of suggestion item with replacement text 9608 int spanStart, spanEnd; // range in TextView where text should be inserted 9609 SuggestionSpan suggestionSpan; // the SuggestionSpan that this TextView represents 9610 int suggestionIndex; // the index of the suggestion inside suggestionSpan 9611 SpannableStringBuilder text = new SpannableStringBuilder(); 9612 TextAppearanceSpan highlightSpan = new TextAppearanceSpan(mContext, 9613 android.R.style.TextAppearance_SuggestionHighlight); 9614 9615 void removeMisspelledFlag() { 9616 int suggestionSpanFlags = suggestionSpan.getFlags(); 9617 if ((suggestionSpanFlags & SuggestionSpan.FLAG_MISSPELLED) > 0) { 9618 suggestionSpanFlags &= ~(SuggestionSpan.FLAG_MISSPELLED); 9619 suggestionSpanFlags &= ~(SuggestionSpan.FLAG_EASY_CORRECT); 9620 suggestionSpan.setFlags(suggestionSpanFlags); 9621 } 9622 } 9623 } 9624 9625 private class SuggestionAdapter extends BaseAdapter { 9626 private LayoutInflater mInflater = (LayoutInflater) TextView.this.mContext. 9627 getSystemService(Context.LAYOUT_INFLATER_SERVICE); 9628 9629 @Override 9630 public int getCount() { 9631 return mNumberOfSuggestions; 9632 } 9633 9634 @Override 9635 public Object getItem(int position) { 9636 return mSuggestionInfos[position]; 9637 } 9638 9639 @Override 9640 public long getItemId(int position) { 9641 return position; 9642 } 9643 9644 @Override 9645 public View getView(int position, View convertView, ViewGroup parent) { 9646 TextView textView = (TextView) convertView; 9647 9648 if (textView == null) { 9649 textView = (TextView) mInflater.inflate(mTextEditSuggestionItemLayout, parent, 9650 false); 9651 } 9652 9653 textView.setText(mSuggestionInfos[position].text); 9654 return textView; 9655 } 9656 } 9657 9658 /** 9659 * Returns the suggestion spans that cover the current cursor position. The suggestion 9660 * spans are sorted according to the length of text that they are attached to. 9661 */ 9662 private SuggestionSpan[] getSuggestionSpans() { 9663 int pos = TextView.this.getSelectionStart(); 9664 Spannable spannable = (Spannable) TextView.this.mText; 9665 SuggestionSpan[] suggestionSpans = spannable.getSpans(pos, pos, SuggestionSpan.class); 9666 9667 // Cache the span length for performance reason. 9668 final HashMap<SuggestionSpan, Integer> spansLengths = 9669 new HashMap<SuggestionSpan, Integer>(); 9670 9671 for (SuggestionSpan suggestionSpan : suggestionSpans) { 9672 int start = spannable.getSpanStart(suggestionSpan); 9673 int end = spannable.getSpanEnd(suggestionSpan); 9674 spansLengths.put(suggestionSpan, Integer.valueOf(end - start)); 9675 } 9676 9677 // The suggestions are sorted according to the lenght of the text that they cover 9678 // (shorter first) 9679 Arrays.sort(suggestionSpans, new Comparator<SuggestionSpan>() { 9680 public int compare(SuggestionSpan span1, SuggestionSpan span2) { 9681 return spansLengths.get(span1).intValue() - spansLengths.get(span2).intValue(); 9682 } 9683 }); 9684 9685 return suggestionSpans; 9686 } 9687 9688 @Override 9689 public void show() { 9690 if (!(mText instanceof Editable)) return; 9691 9692 if (updateSuggestions()) { 9693 mCursorWasVisibleBeforeSuggestions = mCursorVisible; 9694 setCursorVisible(false); 9695 super.show(); 9696 } 9697 } 9698 9699 @Override 9700 protected void measureContent() { 9701 final DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); 9702 final int horizontalMeasure = View.MeasureSpec.makeMeasureSpec( 9703 displayMetrics.widthPixels, View.MeasureSpec.AT_MOST); 9704 final int verticalMeasure = View.MeasureSpec.makeMeasureSpec( 9705 displayMetrics.heightPixels, View.MeasureSpec.AT_MOST); 9706 9707 int width = 0; 9708 View view = null; 9709 for (int i = 0; i < mNumberOfSuggestions; i++) { 9710 view = mSuggestionsAdapter.getView(i, view, mContentView); 9711 view.measure(horizontalMeasure, verticalMeasure); 9712 width = Math.max(width, view.getMeasuredWidth()); 9713 } 9714 9715 // Enforce the width based on actual text widths 9716 mContentView.measure( 9717 View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), 9718 verticalMeasure); 9719 9720 Drawable popupBackground = mPopupWindow.getBackground(); 9721 if (popupBackground != null) { 9722 if (mTempRect == null) mTempRect = new Rect(); 9723 popupBackground.getPadding(mTempRect); 9724 width += mTempRect.left + mTempRect.right; 9725 } 9726 mPopupWindow.setWidth(width); 9727 } 9728 9729 @Override 9730 protected int getTextOffset() { 9731 return getSelectionStart(); 9732 } 9733 9734 @Override 9735 protected int getVerticalLocalPosition(int line) { 9736 return mLayout.getLineBottom(line); 9737 } 9738 9739 @Override 9740 protected int clipVertically(int positionY) { 9741 final int height = mContentView.getMeasuredHeight(); 9742 final DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); 9743 return Math.min(positionY, displayMetrics.heightPixels - height); 9744 } 9745 9746 @Override 9747 public void hide() { 9748 super.hide(); 9749 } 9750 9751 private boolean updateSuggestions() { 9752 Spannable spannable = (Spannable) TextView.this.mText; 9753 SuggestionSpan[] suggestionSpans = getSuggestionSpans(); 9754 9755 final int nbSpans = suggestionSpans.length; 9756 9757 mNumberOfSuggestions = 0; 9758 int spanUnionStart = mText.length(); 9759 int spanUnionEnd = 0; 9760 9761 SuggestionSpan misspelledSpan = null; 9762 int underlineColor = 0; 9763 9764 for (int spanIndex = 0; spanIndex < nbSpans; spanIndex++) { 9765 SuggestionSpan suggestionSpan = suggestionSpans[spanIndex]; 9766 final int spanStart = spannable.getSpanStart(suggestionSpan); 9767 final int spanEnd = spannable.getSpanEnd(suggestionSpan); 9768 spanUnionStart = Math.min(spanStart, spanUnionStart); 9769 spanUnionEnd = Math.max(spanEnd, spanUnionEnd); 9770 9771 if ((suggestionSpan.getFlags() & SuggestionSpan.FLAG_MISSPELLED) != 0) { 9772 misspelledSpan = suggestionSpan; 9773 } 9774 9775 // The first span dictates the background color of the highlighted text 9776 if (spanIndex == 0) underlineColor = suggestionSpan.getUnderlineColor(); 9777 9778 String[] suggestions = suggestionSpan.getSuggestions(); 9779 int nbSuggestions = suggestions.length; 9780 for (int suggestionIndex = 0; suggestionIndex < nbSuggestions; suggestionIndex++) { 9781 SuggestionInfo suggestionInfo = mSuggestionInfos[mNumberOfSuggestions]; 9782 suggestionInfo.spanStart = spanStart; 9783 suggestionInfo.spanEnd = spanEnd; 9784 suggestionInfo.suggestionSpan = suggestionSpan; 9785 suggestionInfo.suggestionIndex = suggestionIndex; 9786 suggestionInfo.text.replace(0, suggestionInfo.text.length(), 9787 suggestions[suggestionIndex]); 9788 9789 mNumberOfSuggestions++; 9790 if (mNumberOfSuggestions == MAX_NUMBER_SUGGESTIONS) { 9791 // Also end outer for loop 9792 spanIndex = nbSpans; 9793 break; 9794 } 9795 } 9796 } 9797 9798 for (int i = 0; i < mNumberOfSuggestions; i++) { 9799 highlightTextDifferences(mSuggestionInfos[i], spanUnionStart, spanUnionEnd); 9800 } 9801 9802 if (misspelledSpan != null) { 9803 final int misspelledStart = spannable.getSpanStart(misspelledSpan); 9804 final int misspelledEnd = spannable.getSpanEnd(misspelledSpan); 9805 if (misspelledStart >= 0 && misspelledEnd > misspelledStart) { 9806 SuggestionInfo suggestionInfo = mSuggestionInfos[mNumberOfSuggestions]; 9807 suggestionInfo.spanStart = misspelledStart; 9808 suggestionInfo.spanEnd = misspelledEnd; 9809 suggestionInfo.suggestionSpan = misspelledSpan; 9810 suggestionInfo.suggestionIndex = -1; 9811 suggestionInfo.text.replace(0, suggestionInfo.text.length(), 9812 getContext().getString(com.android.internal.R.string.addToDictionary)); 9813 9814 mNumberOfSuggestions++; 9815 } 9816 } 9817 9818 if (mNumberOfSuggestions == 0) return false; 9819 9820 if (mSuggestionRangeSpan == null) mSuggestionRangeSpan = new SuggestionRangeSpan(); 9821 if (underlineColor == 0) { 9822 // Fallback on the default highlight color when the first span does not provide one 9823 mSuggestionRangeSpan.setBackgroundColor(mHighlightColor); 9824 } else { 9825 final float BACKGROUND_TRANSPARENCY = 0.3f; 9826 final int newAlpha = (int) (Color.alpha(underlineColor) * BACKGROUND_TRANSPARENCY); 9827 mSuggestionRangeSpan.setBackgroundColor( 9828 (underlineColor & 0x00FFFFFF) + (newAlpha << 24)); 9829 } 9830 spannable.setSpan(mSuggestionRangeSpan, spanUnionStart, spanUnionEnd, 9831 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 9832 9833 mSuggestionsAdapter.notifyDataSetChanged(); 9834 9835 return true; 9836 } 9837 9838 private void highlightTextDifferences(SuggestionInfo suggestionInfo, int unionStart, 9839 int unionEnd) { 9840 final int spanStart = suggestionInfo.spanStart; 9841 final int spanEnd = suggestionInfo.spanEnd; 9842 9843 // Adjust the start/end of the suggestion span 9844 suggestionInfo.suggestionStart = spanStart - unionStart; 9845 suggestionInfo.suggestionEnd = suggestionInfo.suggestionStart 9846 + suggestionInfo.text.length(); 9847 9848 suggestionInfo.text.clearSpans(); 9849 suggestionInfo.text.setSpan(suggestionInfo.highlightSpan, 0, 9850 suggestionInfo.text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 9851 9852 // Add the text before and after the span. 9853 suggestionInfo.text.insert(0, mText.subSequence(unionStart, spanStart).toString()); 9854 suggestionInfo.text.append(mText.subSequence(spanEnd, unionEnd).toString()); 9855 } 9856 9857 @Override 9858 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 9859 if (view instanceof TextView) { 9860 TextView textView = (TextView) view; 9861 9862 SuggestionInfo suggestionInfo = mSuggestionInfos[position]; 9863 final int spanStart = suggestionInfo.spanStart; 9864 final int spanEnd = suggestionInfo.spanEnd; 9865 final String originalText = mText.subSequence(spanStart, spanEnd).toString(); 9866 9867 if (suggestionInfo.suggestionIndex < 0) { 9868 Intent intent = new Intent(Settings.ACTION_USER_DICTIONARY_INSERT); 9869 intent.putExtra("word", originalText); 9870 intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); 9871 getContext().startActivity(intent); 9872 suggestionInfo.removeMisspelledFlag(); 9873 } else { 9874 // SuggestionSpans are removed by replace: save them before 9875 Editable editable = (Editable) mText; 9876 SuggestionSpan[] suggestionSpans = editable.getSpans(spanStart, spanEnd, 9877 SuggestionSpan.class); 9878 final int length = suggestionSpans.length; 9879 int[] suggestionSpansStarts = new int[length]; 9880 int[] suggestionSpansEnds = new int[length]; 9881 int[] suggestionSpansFlags = new int[length]; 9882 for (int i = 0; i < length; i++) { 9883 final SuggestionSpan suggestionSpan = suggestionSpans[i]; 9884 suggestionSpansStarts[i] = editable.getSpanStart(suggestionSpan); 9885 suggestionSpansEnds[i] = editable.getSpanEnd(suggestionSpan); 9886 suggestionSpansFlags[i] = editable.getSpanFlags(suggestionSpan); 9887 } 9888 9889 final int suggestionStart = suggestionInfo.suggestionStart; 9890 final int suggestionEnd = suggestionInfo.suggestionEnd; 9891 final String suggestion = textView.getText().subSequence( 9892 suggestionStart, suggestionEnd).toString(); 9893 editable.replace(spanStart, spanEnd, suggestion); 9894 9895 suggestionInfo.removeMisspelledFlag(); 9896 9897 // Notify source IME of the suggestion pick. Do this before swaping texts. 9898 if (!TextUtils.isEmpty( 9899 suggestionInfo.suggestionSpan.getNotificationTargetClassName())) { 9900 InputMethodManager imm = InputMethodManager.peekInstance(); 9901 imm.notifySuggestionPicked(suggestionInfo.suggestionSpan, originalText, 9902 suggestionInfo.suggestionIndex); 9903 } 9904 9905 // Swap text content between actual text and Suggestion span 9906 String[] suggestions = suggestionInfo.suggestionSpan.getSuggestions(); 9907 suggestions[suggestionInfo.suggestionIndex] = originalText; 9908 9909 // Restore previous SuggestionSpans 9910 final int lengthDifference = suggestion.length() - (spanEnd - spanStart); 9911 for (int i = 0; i < length; i++) { 9912 // Only spans that include the modified region make sense after replacement 9913 // Spans partially included in the replaced region are removed, there is no 9914 // way to assign them a valid range after replacement 9915 if (suggestionSpansStarts[i] <= spanStart && 9916 suggestionSpansEnds[i] >= spanEnd) { 9917 editable.setSpan(suggestionSpans[i], suggestionSpansStarts[i], 9918 suggestionSpansEnds[i] + lengthDifference, 9919 suggestionSpansFlags[i]); 9920 } 9921 } 9922 } 9923 } 9924 hide(); 9925 } 9926 } 9927 9928 void showSuggestions() { 9929 if (!isSuggestionsEnabled() || !isTextEditable()) return; 9930 9931 if (mSuggestionsPopupWindow == null) { 9932 mSuggestionsPopupWindow = new SuggestionsPopupWindow(); 9933 } 9934 hideControllers(); 9935 mSuggestionsPopupWindow.show(); 9936 } 9937 9938 boolean areSuggestionsShown() { 9939 return mSuggestionsPopupWindow != null && mSuggestionsPopupWindow.isShowing(); 9940 } 9941 9942 /** 9943 * Return whether or not suggestions are enabled on this TextView. The suggestions are generated 9944 * by the IME or by the spell checker as the user types. This is done by adding 9945 * {@link SuggestionSpan}s to the text. 9946 * 9947 * When suggestions are enabled (default), this list of suggestions will be displayed when the 9948 * user asks for them on these parts of the text. This value depends on the inputType of this 9949 * TextView. 9950 * 9951 * The class of the input type must be {@link InputType#TYPE_CLASS_TEXT}. 9952 * 9953 * In addition, the type variation must be one of 9954 * {@link InputType#TYPE_TEXT_VARIATION_NORMAL}, 9955 * {@link InputType#TYPE_TEXT_VARIATION_EMAIL_SUBJECT}, 9956 * {@link InputType#TYPE_TEXT_VARIATION_LONG_MESSAGE}, 9957 * {@link InputType#TYPE_TEXT_VARIATION_SHORT_MESSAGE} or 9958 * {@link InputType#TYPE_TEXT_VARIATION_WEB_EDIT_TEXT}. 9959 * 9960 * And finally, the {@link InputType#TYPE_TEXT_FLAG_NO_SUGGESTIONS} flag must <i>not</i> be set. 9961 * 9962 * @return true if the suggestions popup window is enabled, based on the inputType. 9963 */ 9964 public boolean isSuggestionsEnabled() { 9965 if ((mInputType & InputType.TYPE_MASK_CLASS) != InputType.TYPE_CLASS_TEXT) return false; 9966 if ((mInputType & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS) > 0) return false; 9967 9968 final int variation = mInputType & EditorInfo.TYPE_MASK_VARIATION; 9969 return (variation == EditorInfo.TYPE_TEXT_VARIATION_NORMAL || 9970 variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_SUBJECT || 9971 variation == EditorInfo.TYPE_TEXT_VARIATION_LONG_MESSAGE || 9972 variation == EditorInfo.TYPE_TEXT_VARIATION_SHORT_MESSAGE || 9973 variation == EditorInfo.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT); 9974 } 9975 9976 /** 9977 * If provided, this ActionMode.Callback will be used to create the ActionMode when text 9978 * selection is initiated in this View. 9979 * 9980 * The standard implementation populates the menu with a subset of Select All, Cut, Copy and 9981 * Paste actions, depending on what this View supports. 9982 * 9983 * A custom implementation can add new entries in the default menu in its 9984 * {@link android.view.ActionMode.Callback#onPrepareActionMode(ActionMode, Menu)} method. The 9985 * default actions can also be removed from the menu using {@link Menu#removeItem(int)} and 9986 * passing {@link android.R.id#selectAll}, {@link android.R.id#cut}, {@link android.R.id#copy} 9987 * or {@link android.R.id#paste} ids as parameters. 9988 * 9989 * Returning false from 9990 * {@link android.view.ActionMode.Callback#onCreateActionMode(ActionMode, Menu)} will prevent 9991 * the action mode from being started. 9992 * 9993 * Action click events should be handled by the custom implementation of 9994 * {@link android.view.ActionMode.Callback#onActionItemClicked(ActionMode, MenuItem)}. 9995 * 9996 * Note that text selection mode is not started when a TextView receives focus and the 9997 * {@link android.R.attr#selectAllOnFocus} flag has been set. The content is highlighted in 9998 * that case, to allow for quick replacement. 9999 */ 10000 public void setCustomSelectionActionModeCallback(ActionMode.Callback actionModeCallback) { 10001 mCustomSelectionActionModeCallback = actionModeCallback; 10002 } 10003 10004 /** 10005 * Retrieves the value set in {@link #setCustomSelectionActionModeCallback}. Default is null. 10006 * 10007 * @return The current custom selection callback. 10008 */ 10009 public ActionMode.Callback getCustomSelectionActionModeCallback() { 10010 return mCustomSelectionActionModeCallback; 10011 } 10012 10013 /** 10014 * 10015 * @return true if the selection mode was actually started. 10016 */ 10017 private boolean startSelectionActionMode() { 10018 if (mSelectionActionMode != null) { 10019 // Selection action mode is already started 10020 return false; 10021 } 10022 10023 if (!canSelectText() || !requestFocus()) { 10024 Log.w(LOG_TAG, "TextView does not support text selection. Action mode cancelled."); 10025 return false; 10026 } 10027 10028 if (!hasSelection()) { 10029 // There may already be a selection on device rotation 10030 boolean currentWordSelected = selectCurrentWord(); 10031 if (!currentWordSelected) { 10032 // No word found under cursor or text selection not permitted. 10033 return false; 10034 } 10035 } 10036 10037 final InputMethodManager imm = InputMethodManager.peekInstance(); 10038 boolean extractedTextModeWillBeStartedFullScreen = !(this instanceof ExtractEditText) && 10039 imm != null && imm.isFullscreenMode(); 10040 10041 // Do not start the action mode when extracted text will show up full screen, thus 10042 // immediately hiding the newly created action bar, which would be visually distracting. 10043 if (!extractedTextModeWillBeStartedFullScreen) { 10044 ActionMode.Callback actionModeCallback = new SelectionActionModeCallback(); 10045 mSelectionActionMode = startActionMode(actionModeCallback); 10046 } 10047 final boolean selectionStarted = mSelectionActionMode != null || 10048 extractedTextModeWillBeStartedFullScreen; 10049 10050 if (selectionStarted && !mTextIsSelectable && imm != null) { 10051 // Show the IME to be able to replace text, except when selecting non editable text. 10052 imm.showSoftInput(this, 0, null); 10053 } 10054 10055 return selectionStarted; 10056 } 10057 10058 private void stopSelectionActionMode() { 10059 if (mSelectionActionMode != null) { 10060 // This will hide the mSelectionModifierCursorController 10061 mSelectionActionMode.finish(); 10062 } 10063 } 10064 10065 /** 10066 * Paste clipboard content between min and max positions. 10067 */ 10068 private void paste(int min, int max) { 10069 ClipboardManager clipboard = 10070 (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE); 10071 ClipData clip = clipboard.getPrimaryClip(); 10072 if (clip != null) { 10073 boolean didFirst = false; 10074 for (int i=0; i<clip.getItemCount(); i++) { 10075 CharSequence paste = clip.getItemAt(i).coerceToText(getContext()); 10076 if (paste != null) { 10077 if (!didFirst) { 10078 long minMax = prepareSpacesAroundPaste(min, max, paste); 10079 min = extractRangeStartFromLong(minMax); 10080 max = extractRangeEndFromLong(minMax); 10081 Selection.setSelection((Spannable) mText, max); 10082 ((Editable) mText).replace(min, max, paste); 10083 didFirst = true; 10084 } else { 10085 ((Editable) mText).insert(getSelectionEnd(), "\n"); 10086 ((Editable) mText).insert(getSelectionEnd(), paste); 10087 } 10088 } 10089 } 10090 stopSelectionActionMode(); 10091 sLastCutOrCopyTime = 0; 10092 } 10093 } 10094 10095 private void setPrimaryClip(ClipData clip) { 10096 ClipboardManager clipboard = (ClipboardManager) getContext(). 10097 getSystemService(Context.CLIPBOARD_SERVICE); 10098 clipboard.setPrimaryClip(clip); 10099 sLastCutOrCopyTime = SystemClock.uptimeMillis(); 10100 } 10101 10102 /** 10103 * An ActionMode Callback class that is used to provide actions while in text selection mode. 10104 * 10105 * The default callback provides a subset of Select All, Cut, Copy and Paste actions, depending 10106 * on which of these this TextView supports. 10107 */ 10108 private class SelectionActionModeCallback implements ActionMode.Callback { 10109 10110 @Override 10111 public boolean onCreateActionMode(ActionMode mode, Menu menu) { 10112 TypedArray styledAttributes = mContext.obtainStyledAttributes(R.styleable.Theme); 10113 10114 boolean allowText = getContext().getResources().getBoolean( 10115 com.android.internal.R.bool.config_allowActionMenuItemTextWithIcon); 10116 10117 mode.setTitle(allowText ? 10118 mContext.getString(com.android.internal.R.string.textSelectionCABTitle) : null); 10119 mode.setSubtitle(null); 10120 10121 int selectAllIconId = 0; // No icon by default 10122 if (!allowText) { 10123 // Provide an icon, text will not be displayed on smaller screens. 10124 selectAllIconId = styledAttributes.getResourceId( 10125 R.styleable.Theme_actionModeSelectAllDrawable, 0); 10126 } 10127 10128 menu.add(0, ID_SELECT_ALL, 0, com.android.internal.R.string.selectAll). 10129 setIcon(selectAllIconId). 10130 setAlphabeticShortcut('a'). 10131 setShowAsAction( 10132 MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); 10133 10134 if (canCut()) { 10135 menu.add(0, ID_CUT, 0, com.android.internal.R.string.cut). 10136 setIcon(styledAttributes.getResourceId( 10137 R.styleable.Theme_actionModeCutDrawable, 0)). 10138 setAlphabeticShortcut('x'). 10139 setShowAsAction( 10140 MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); 10141 } 10142 10143 if (canCopy()) { 10144 menu.add(0, ID_COPY, 0, com.android.internal.R.string.copy). 10145 setIcon(styledAttributes.getResourceId( 10146 R.styleable.Theme_actionModeCopyDrawable, 0)). 10147 setAlphabeticShortcut('c'). 10148 setShowAsAction( 10149 MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); 10150 } 10151 10152 if (canPaste()) { 10153 menu.add(0, ID_PASTE, 0, com.android.internal.R.string.paste). 10154 setIcon(styledAttributes.getResourceId( 10155 R.styleable.Theme_actionModePasteDrawable, 0)). 10156 setAlphabeticShortcut('v'). 10157 setShowAsAction( 10158 MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); 10159 } 10160 10161 styledAttributes.recycle(); 10162 10163 if (mCustomSelectionActionModeCallback != null) { 10164 if (!mCustomSelectionActionModeCallback.onCreateActionMode(mode, menu)) { 10165 // The custom mode can choose to cancel the action mode 10166 return false; 10167 } 10168 } 10169 10170 if (menu.hasVisibleItems() || mode.getCustomView() != null) { 10171 getSelectionController().show(); 10172 return true; 10173 } else { 10174 return false; 10175 } 10176 } 10177 10178 @Override 10179 public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 10180 if (mCustomSelectionActionModeCallback != null) { 10181 return mCustomSelectionActionModeCallback.onPrepareActionMode(mode, menu); 10182 } 10183 return true; 10184 } 10185 10186 @Override 10187 public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 10188 if (mCustomSelectionActionModeCallback != null && 10189 mCustomSelectionActionModeCallback.onActionItemClicked(mode, item)) { 10190 return true; 10191 } 10192 return onTextContextMenuItem(item.getItemId()); 10193 } 10194 10195 @Override 10196 public void onDestroyActionMode(ActionMode mode) { 10197 if (mCustomSelectionActionModeCallback != null) { 10198 mCustomSelectionActionModeCallback.onDestroyActionMode(mode); 10199 } 10200 Selection.setSelection((Spannable) mText, getSelectionEnd()); 10201 10202 if (mSelectionModifierCursorController != null) { 10203 mSelectionModifierCursorController.hide(); 10204 } 10205 10206 mSelectionActionMode = null; 10207 } 10208 } 10209 10210 private class ActionPopupWindow extends PinnedPopupWindow implements OnClickListener { 10211 private static final int POPUP_TEXT_LAYOUT = 10212 com.android.internal.R.layout.text_edit_action_popup_text; 10213 private TextView mPasteTextView; 10214 private TextView mReplaceTextView; 10215 10216 @Override 10217 protected void createPopupWindow() { 10218 mPopupWindow = new PopupWindow(TextView.this.mContext, null, 10219 com.android.internal.R.attr.textSelectHandleWindowStyle); 10220 mPopupWindow.setClippingEnabled(true); 10221 } 10222 10223 @Override 10224 protected void initContentView() { 10225 LinearLayout linearLayout = new LinearLayout(TextView.this.getContext()); 10226 linearLayout.setOrientation(LinearLayout.HORIZONTAL); 10227 mContentView = linearLayout; 10228 mContentView.setBackgroundResource( 10229 com.android.internal.R.drawable.text_edit_paste_window); 10230 10231 LayoutInflater inflater = (LayoutInflater)TextView.this.mContext. 10232 getSystemService(Context.LAYOUT_INFLATER_SERVICE); 10233 10234 LayoutParams wrapContent = new LayoutParams( 10235 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 10236 10237 mPasteTextView = (TextView) inflater.inflate(POPUP_TEXT_LAYOUT, null); 10238 mPasteTextView.setLayoutParams(wrapContent); 10239 mContentView.addView(mPasteTextView); 10240 mPasteTextView.setText(com.android.internal.R.string.paste); 10241 mPasteTextView.setOnClickListener(this); 10242 10243 mReplaceTextView = (TextView) inflater.inflate(POPUP_TEXT_LAYOUT, null); 10244 mReplaceTextView.setLayoutParams(wrapContent); 10245 mContentView.addView(mReplaceTextView); 10246 mReplaceTextView.setText(com.android.internal.R.string.replace); 10247 mReplaceTextView.setOnClickListener(this); 10248 } 10249 10250 @Override 10251 public void show() { 10252 boolean canPaste = canPaste(); 10253 boolean canSuggest = isSuggestionsEnabled() && isCursorInsideSuggestionSpan(); 10254 mPasteTextView.setVisibility(canPaste ? View.VISIBLE : View.GONE); 10255 mReplaceTextView.setVisibility(canSuggest ? View.VISIBLE : View.GONE); 10256 10257 if (!canPaste && !canSuggest) return; 10258 10259 super.show(); 10260 } 10261 10262 @Override 10263 public void onClick(View view) { 10264 if (view == mPasteTextView && canPaste()) { 10265 onTextContextMenuItem(ID_PASTE); 10266 hide(); 10267 } else if (view == mReplaceTextView) { 10268 final int middle = (getSelectionStart() + getSelectionEnd()) / 2; 10269 stopSelectionActionMode(); 10270 Selection.setSelection((Spannable) mText, middle); 10271 showSuggestions(); 10272 } 10273 } 10274 10275 @Override 10276 protected int getTextOffset() { 10277 return (getSelectionStart() + getSelectionEnd()) / 2; 10278 } 10279 10280 @Override 10281 protected int getVerticalLocalPosition(int line) { 10282 return mLayout.getLineTop(line) - mContentView.getMeasuredHeight(); 10283 } 10284 10285 @Override 10286 protected int clipVertically(int positionY) { 10287 if (positionY < 0) { 10288 final int offset = getTextOffset(); 10289 final int line = mLayout.getLineForOffset(offset); 10290 positionY += mLayout.getLineBottom(line) - mLayout.getLineTop(line); 10291 positionY += mContentView.getMeasuredHeight(); 10292 10293 // Assumes insertion and selection handles share the same height 10294 final Drawable handle = mContext.getResources().getDrawable(mTextSelectHandleRes); 10295 positionY += handle.getIntrinsicHeight(); 10296 } 10297 10298 return positionY; 10299 } 10300 } 10301 10302 private abstract class HandleView extends View implements TextViewPositionListener { 10303 protected Drawable mDrawable; 10304 protected Drawable mDrawableLtr; 10305 protected Drawable mDrawableRtl; 10306 private final PopupWindow mContainer; 10307 // Position with respect to the parent TextView 10308 private int mPositionX, mPositionY; 10309 private boolean mIsDragging; 10310 // Offset from touch position to mPosition 10311 private float mTouchToWindowOffsetX, mTouchToWindowOffsetY; 10312 protected int mHotspotX; 10313 // Offsets the hotspot point up, so that cursor is not hidden by the finger when moving up 10314 private float mTouchOffsetY; 10315 // Where the touch position should be on the handle to ensure a maximum cursor visibility 10316 private float mIdealVerticalOffset; 10317 // Parent's (TextView) previous position in window 10318 private int mLastParentX, mLastParentY; 10319 // Transient action popup window for Paste and Replace actions 10320 protected ActionPopupWindow mActionPopupWindow; 10321 // Previous text character offset 10322 private int mPreviousOffset = -1; 10323 // Previous text character offset 10324 private boolean mPositionHasChanged = true; 10325 // Used to delay the appearance of the action popup window 10326 private Runnable mActionPopupShower; 10327 10328 public HandleView(Drawable drawableLtr, Drawable drawableRtl) { 10329 super(TextView.this.mContext); 10330 mContainer = new PopupWindow(TextView.this.mContext, null, 10331 com.android.internal.R.attr.textSelectHandleWindowStyle); 10332 mContainer.setSplitTouchEnabled(true); 10333 mContainer.setClippingEnabled(false); 10334 mContainer.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL); 10335 mContainer.setContentView(this); 10336 10337 mDrawableLtr = drawableLtr; 10338 mDrawableRtl = drawableRtl; 10339 10340 updateDrawable(); 10341 10342 final int handleHeight = mDrawable.getIntrinsicHeight(); 10343 mTouchOffsetY = -0.3f * handleHeight; 10344 mIdealVerticalOffset = 0.7f * handleHeight; 10345 } 10346 10347 protected void updateDrawable() { 10348 final int offset = getCurrentCursorOffset(); 10349 final boolean isRtlCharAtOffset = mLayout.isRtlCharAt(offset); 10350 mDrawable = isRtlCharAtOffset ? mDrawableRtl : mDrawableLtr; 10351 mHotspotX = getHotspotX(mDrawable, isRtlCharAtOffset); 10352 } 10353 10354 protected abstract int getHotspotX(Drawable drawable, boolean isRtlRun); 10355 10356 // Touch-up filter: number of previous positions remembered 10357 private static final int HISTORY_SIZE = 5; 10358 private static final int TOUCH_UP_FILTER_DELAY_AFTER = 150; 10359 private static final int TOUCH_UP_FILTER_DELAY_BEFORE = 350; 10360 private final long[] mPreviousOffsetsTimes = new long[HISTORY_SIZE]; 10361 private final int[] mPreviousOffsets = new int[HISTORY_SIZE]; 10362 private int mPreviousOffsetIndex = 0; 10363 private int mNumberPreviousOffsets = 0; 10364 10365 private void startTouchUpFilter(int offset) { 10366 mNumberPreviousOffsets = 0; 10367 addPositionToTouchUpFilter(offset); 10368 } 10369 10370 private void addPositionToTouchUpFilter(int offset) { 10371 mPreviousOffsetIndex = (mPreviousOffsetIndex + 1) % HISTORY_SIZE; 10372 mPreviousOffsets[mPreviousOffsetIndex] = offset; 10373 mPreviousOffsetsTimes[mPreviousOffsetIndex] = SystemClock.uptimeMillis(); 10374 mNumberPreviousOffsets++; 10375 } 10376 10377 private void filterOnTouchUp() { 10378 final long now = SystemClock.uptimeMillis(); 10379 int i = 0; 10380 int index = mPreviousOffsetIndex; 10381 final int iMax = Math.min(mNumberPreviousOffsets, HISTORY_SIZE); 10382 while (i < iMax && (now - mPreviousOffsetsTimes[index]) < TOUCH_UP_FILTER_DELAY_AFTER) { 10383 i++; 10384 index = (mPreviousOffsetIndex - i + HISTORY_SIZE) % HISTORY_SIZE; 10385 } 10386 10387 if (i > 0 && i < iMax && 10388 (now - mPreviousOffsetsTimes[index]) > TOUCH_UP_FILTER_DELAY_BEFORE) { 10389 positionAtCursorOffset(mPreviousOffsets[index], false); 10390 } 10391 } 10392 10393 public boolean offsetHasBeenChanged() { 10394 return mNumberPreviousOffsets > 1; 10395 } 10396 10397 @Override 10398 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 10399 setMeasuredDimension(mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight()); 10400 } 10401 10402 public void show() { 10403 if (isShowing()) return; 10404 10405 getPositionListener().addSubscriber(this, true /* local position may change */); 10406 10407 // Make sure the offset is always considered new, even when focusing at same position 10408 mPreviousOffset = -1; 10409 positionAtCursorOffset(getCurrentCursorOffset(), false); 10410 10411 hideActionPopupWindow(); 10412 } 10413 10414 protected void dismiss() { 10415 mIsDragging = false; 10416 mContainer.dismiss(); 10417 onDetached(); 10418 } 10419 10420 public void hide() { 10421 dismiss(); 10422 10423 TextView.this.getPositionListener().removeSubscriber(this); 10424 } 10425 10426 void showActionPopupWindow(int delay) { 10427 if (mActionPopupWindow == null) { 10428 mActionPopupWindow = new ActionPopupWindow(); 10429 } 10430 if (mActionPopupShower == null) { 10431 mActionPopupShower = new Runnable() { 10432 public void run() { 10433 mActionPopupWindow.show(); 10434 } 10435 }; 10436 } else { 10437 TextView.this.removeCallbacks(mActionPopupShower); 10438 } 10439 TextView.this.postDelayed(mActionPopupShower, delay); 10440 } 10441 10442 protected void hideActionPopupWindow() { 10443 if (mActionPopupShower != null) { 10444 TextView.this.removeCallbacks(mActionPopupShower); 10445 } 10446 if (mActionPopupWindow != null) { 10447 mActionPopupWindow.hide(); 10448 } 10449 } 10450 10451 public boolean isShowing() { 10452 return mContainer.isShowing(); 10453 } 10454 10455 private boolean isVisible() { 10456 // Always show a dragging handle. 10457 if (mIsDragging) { 10458 return true; 10459 } 10460 10461 if (isInBatchEditMode()) { 10462 return false; 10463 } 10464 10465 return getPositionListener().isVisible(mPositionX + mHotspotX, mPositionY); 10466 } 10467 10468 public abstract int getCurrentCursorOffset(); 10469 10470 protected void updateSelection(int offset) { 10471 updateDrawable(); 10472 } 10473 10474 public abstract void updatePosition(float x, float y); 10475 10476 protected void positionAtCursorOffset(int offset, boolean parentScrolled) { 10477 // A HandleView relies on the layout, which may be nulled by external methods 10478 if (mLayout == null) { 10479 // Will update controllers' state, hiding them and stopping selection mode if needed 10480 prepareCursorControllers(); 10481 return; 10482 } 10483 10484 if (offset != mPreviousOffset || parentScrolled) { 10485 updateSelection(offset); 10486 addPositionToTouchUpFilter(offset); 10487 final int line = mLayout.getLineForOffset(offset); 10488 10489 mPositionX = (int) (mLayout.getPrimaryHorizontal(offset) - 0.5f - mHotspotX); 10490 mPositionY = mLayout.getLineBottom(line); 10491 10492 // Take TextView's padding into account. 10493 mPositionX += viewportToContentHorizontalOffset(); 10494 mPositionY += viewportToContentVerticalOffset(); 10495 10496 mPreviousOffset = offset; 10497 mPositionHasChanged = true; 10498 } 10499 } 10500 10501 public void updatePosition(int parentPositionX, int parentPositionY, 10502 boolean parentPositionChanged, boolean parentScrolled) { 10503 positionAtCursorOffset(getCurrentCursorOffset(), parentScrolled); 10504 if (parentPositionChanged || mPositionHasChanged) { 10505 if (mIsDragging) { 10506 // Update touchToWindow offset in case of parent scrolling while dragging 10507 if (parentPositionX != mLastParentX || parentPositionY != mLastParentY) { 10508 mTouchToWindowOffsetX += parentPositionX - mLastParentX; 10509 mTouchToWindowOffsetY += parentPositionY - mLastParentY; 10510 mLastParentX = parentPositionX; 10511 mLastParentY = parentPositionY; 10512 } 10513 10514 onHandleMoved(); 10515 } 10516 10517 if (isVisible()) { 10518 final int positionX = parentPositionX + mPositionX; 10519 final int positionY = parentPositionY + mPositionY; 10520 if (isShowing()) { 10521 mContainer.update(positionX, positionY, -1, -1); 10522 } else { 10523 mContainer.showAtLocation(TextView.this, Gravity.NO_GRAVITY, 10524 positionX, positionY); 10525 } 10526 } else { 10527 if (isShowing()) { 10528 dismiss(); 10529 } 10530 } 10531 10532 mPositionHasChanged = false; 10533 } 10534 } 10535 10536 @Override 10537 protected void onDraw(Canvas c) { 10538 mDrawable.setBounds(0, 0, mRight - mLeft, mBottom - mTop); 10539 mDrawable.draw(c); 10540 } 10541 10542 @Override 10543 public boolean onTouchEvent(MotionEvent ev) { 10544 switch (ev.getActionMasked()) { 10545 case MotionEvent.ACTION_DOWN: { 10546 startTouchUpFilter(getCurrentCursorOffset()); 10547 mTouchToWindowOffsetX = ev.getRawX() - mPositionX; 10548 mTouchToWindowOffsetY = ev.getRawY() - mPositionY; 10549 10550 final PositionListener positionListener = getPositionListener(); 10551 mLastParentX = positionListener.getPositionX(); 10552 mLastParentY = positionListener.getPositionY(); 10553 mIsDragging = true; 10554 break; 10555 } 10556 10557 case MotionEvent.ACTION_MOVE: { 10558 final float rawX = ev.getRawX(); 10559 final float rawY = ev.getRawY(); 10560 10561 // Vertical hysteresis: vertical down movement tends to snap to ideal offset 10562 final float previousVerticalOffset = mTouchToWindowOffsetY - mLastParentY; 10563 final float currentVerticalOffset = rawY - mPositionY - mLastParentY; 10564 float newVerticalOffset; 10565 if (previousVerticalOffset < mIdealVerticalOffset) { 10566 newVerticalOffset = Math.min(currentVerticalOffset, mIdealVerticalOffset); 10567 newVerticalOffset = Math.max(newVerticalOffset, previousVerticalOffset); 10568 } else { 10569 newVerticalOffset = Math.max(currentVerticalOffset, mIdealVerticalOffset); 10570 newVerticalOffset = Math.min(newVerticalOffset, previousVerticalOffset); 10571 } 10572 mTouchToWindowOffsetY = newVerticalOffset + mLastParentY; 10573 10574 final float newPosX = rawX - mTouchToWindowOffsetX + mHotspotX; 10575 final float newPosY = rawY - mTouchToWindowOffsetY + mTouchOffsetY; 10576 10577 updatePosition(newPosX, newPosY); 10578 break; 10579 } 10580 10581 case MotionEvent.ACTION_UP: 10582 filterOnTouchUp(); 10583 mIsDragging = false; 10584 break; 10585 10586 case MotionEvent.ACTION_CANCEL: 10587 mIsDragging = false; 10588 break; 10589 } 10590 return true; 10591 } 10592 10593 public boolean isDragging() { 10594 return mIsDragging; 10595 } 10596 10597 void onHandleMoved() { 10598 hideActionPopupWindow(); 10599 } 10600 10601 public void onDetached() { 10602 hideActionPopupWindow(); 10603 } 10604 } 10605 10606 private class InsertionHandleView extends HandleView { 10607 private static final int DELAY_BEFORE_HANDLE_FADES_OUT = 4000; 10608 private static final int RECENT_CUT_COPY_DURATION = 15 * 1000; // seconds 10609 10610 // Used to detect taps on the insertion handle, which will affect the ActionPopupWindow 10611 private float mDownPositionX, mDownPositionY; 10612 private Runnable mHider; 10613 10614 public InsertionHandleView(Drawable drawable) { 10615 super(drawable, drawable); 10616 } 10617 10618 @Override 10619 public void show() { 10620 super.show(); 10621 10622 final long durationSinceCutOrCopy = SystemClock.uptimeMillis() - sLastCutOrCopyTime; 10623 if (durationSinceCutOrCopy < RECENT_CUT_COPY_DURATION) { 10624 showActionPopupWindow(0); 10625 } 10626 10627 hideAfterDelay(); 10628 } 10629 10630 public void showWithActionPopup() { 10631 show(); 10632 showActionPopupWindow(0); 10633 } 10634 10635 private void hideAfterDelay() { 10636 removeHiderCallback(); 10637 if (mHider == null) { 10638 mHider = new Runnable() { 10639 public void run() { 10640 hide(); 10641 } 10642 }; 10643 } 10644 TextView.this.postDelayed(mHider, DELAY_BEFORE_HANDLE_FADES_OUT); 10645 } 10646 10647 private void removeHiderCallback() { 10648 if (mHider != null) { 10649 TextView.this.removeCallbacks(mHider); 10650 } 10651 } 10652 10653 @Override 10654 protected int getHotspotX(Drawable drawable, boolean isRtlRun) { 10655 return drawable.getIntrinsicWidth() / 2; 10656 } 10657 10658 @Override 10659 public boolean onTouchEvent(MotionEvent ev) { 10660 final boolean result = super.onTouchEvent(ev); 10661 10662 switch (ev.getActionMasked()) { 10663 case MotionEvent.ACTION_DOWN: 10664 mDownPositionX = ev.getRawX(); 10665 mDownPositionY = ev.getRawY(); 10666 break; 10667 10668 case MotionEvent.ACTION_UP: 10669 if (!offsetHasBeenChanged()) { 10670 final float deltaX = mDownPositionX - ev.getRawX(); 10671 final float deltaY = mDownPositionY - ev.getRawY(); 10672 final float distanceSquared = deltaX * deltaX + deltaY * deltaY; 10673 if (distanceSquared < mSquaredTouchSlopDistance) { 10674 if (mActionPopupWindow != null && mActionPopupWindow.isShowing()) { 10675 // Tapping on the handle dismisses the displayed action popup 10676 mActionPopupWindow.hide(); 10677 } else { 10678 showWithActionPopup(); 10679 } 10680 } 10681 } 10682 hideAfterDelay(); 10683 break; 10684 10685 case MotionEvent.ACTION_CANCEL: 10686 hideAfterDelay(); 10687 break; 10688 10689 default: 10690 break; 10691 } 10692 10693 return result; 10694 } 10695 10696 @Override 10697 public int getCurrentCursorOffset() { 10698 return TextView.this.getSelectionStart(); 10699 } 10700 10701 @Override 10702 public void updateSelection(int offset) { 10703 Selection.setSelection((Spannable) mText, offset); 10704 } 10705 10706 @Override 10707 public void updatePosition(float x, float y) { 10708 positionAtCursorOffset(getOffsetForPosition(x, y), false); 10709 } 10710 10711 @Override 10712 void onHandleMoved() { 10713 super.onHandleMoved(); 10714 removeHiderCallback(); 10715 } 10716 10717 @Override 10718 public void onDetached() { 10719 super.onDetached(); 10720 removeHiderCallback(); 10721 } 10722 } 10723 10724 private class SelectionStartHandleView extends HandleView { 10725 10726 public SelectionStartHandleView(Drawable drawableLtr, Drawable drawableRtl) { 10727 super(drawableLtr, drawableRtl); 10728 } 10729 10730 @Override 10731 protected int getHotspotX(Drawable drawable, boolean isRtlRun) { 10732 if (isRtlRun) { 10733 return drawable.getIntrinsicWidth() / 4; 10734 } else { 10735 return (drawable.getIntrinsicWidth() * 3) / 4; 10736 } 10737 } 10738 10739 @Override 10740 public int getCurrentCursorOffset() { 10741 return TextView.this.getSelectionStart(); 10742 } 10743 10744 @Override 10745 public void updateSelection(int offset) { 10746 super.updateSelection(offset); 10747 Selection.setSelection((Spannable) mText, offset, getSelectionEnd()); 10748 } 10749 10750 @Override 10751 public void updatePosition(float x, float y) { 10752 int offset = getOffsetForPosition(x, y); 10753 10754 // Handles can not cross and selection is at least one character 10755 final int selectionEnd = getSelectionEnd(); 10756 if (offset >= selectionEnd) offset = selectionEnd - 1; 10757 10758 positionAtCursorOffset(offset, false); 10759 } 10760 10761 public ActionPopupWindow getActionPopupWindow() { 10762 return mActionPopupWindow; 10763 } 10764 } 10765 10766 private class SelectionEndHandleView extends HandleView { 10767 10768 public SelectionEndHandleView(Drawable drawableLtr, Drawable drawableRtl) { 10769 super(drawableLtr, drawableRtl); 10770 } 10771 10772 @Override 10773 protected int getHotspotX(Drawable drawable, boolean isRtlRun) { 10774 if (isRtlRun) { 10775 return (drawable.getIntrinsicWidth() * 3) / 4; 10776 } else { 10777 return drawable.getIntrinsicWidth() / 4; 10778 } 10779 } 10780 10781 @Override 10782 public int getCurrentCursorOffset() { 10783 return TextView.this.getSelectionEnd(); 10784 } 10785 10786 @Override 10787 public void updateSelection(int offset) { 10788 super.updateSelection(offset); 10789 Selection.setSelection((Spannable) mText, getSelectionStart(), offset); 10790 } 10791 10792 @Override 10793 public void updatePosition(float x, float y) { 10794 int offset = getOffsetForPosition(x, y); 10795 10796 // Handles can not cross and selection is at least one character 10797 final int selectionStart = getSelectionStart(); 10798 if (offset <= selectionStart) offset = selectionStart + 1; 10799 10800 positionAtCursorOffset(offset, false); 10801 } 10802 10803 public void setActionPopupWindow(ActionPopupWindow actionPopupWindow) { 10804 mActionPopupWindow = actionPopupWindow; 10805 } 10806 } 10807 10808 /** 10809 * A CursorController instance can be used to control a cursor in the text. 10810 * It is not used outside of {@link TextView}. 10811 * @hide 10812 */ 10813 private interface CursorController extends ViewTreeObserver.OnTouchModeChangeListener { 10814 /** 10815 * Makes the cursor controller visible on screen. Will be drawn by {@link #draw(Canvas)}. 10816 * See also {@link #hide()}. 10817 */ 10818 public void show(); 10819 10820 /** 10821 * Hide the cursor controller from screen. 10822 * See also {@link #show()}. 10823 */ 10824 public void hide(); 10825 10826 /** 10827 * Called when the view is detached from window. Perform house keeping task, such as 10828 * stopping Runnable thread that would otherwise keep a reference on the context, thus 10829 * preventing the activity from being recycled. 10830 */ 10831 public void onDetached(); 10832 } 10833 10834 private class InsertionPointCursorController implements CursorController { 10835 private InsertionHandleView mHandle; 10836 10837 public void show() { 10838 getHandle().show(); 10839 } 10840 10841 public void showWithActionPopup() { 10842 getHandle().showWithActionPopup(); 10843 } 10844 10845 public void hide() { 10846 if (mHandle != null) { 10847 mHandle.hide(); 10848 } 10849 } 10850 10851 public void onTouchModeChanged(boolean isInTouchMode) { 10852 if (!isInTouchMode) { 10853 hide(); 10854 } 10855 } 10856 10857 private InsertionHandleView getHandle() { 10858 if (mSelectHandleCenter == null) { 10859 mSelectHandleCenter = mContext.getResources().getDrawable( 10860 mTextSelectHandleRes); 10861 } 10862 if (mHandle == null) { 10863 mHandle = new InsertionHandleView(mSelectHandleCenter); 10864 } 10865 return mHandle; 10866 } 10867 10868 @Override 10869 public void onDetached() { 10870 final ViewTreeObserver observer = getViewTreeObserver(); 10871 observer.removeOnTouchModeChangeListener(this); 10872 10873 if (mHandle != null) mHandle.onDetached(); 10874 } 10875 } 10876 10877 private class SelectionModifierCursorController implements CursorController { 10878 private static final int DELAY_BEFORE_REPLACE_ACTION = 200; // milliseconds 10879 // The cursor controller handles, lazily created when shown. 10880 private SelectionStartHandleView mStartHandle; 10881 private SelectionEndHandleView mEndHandle; 10882 // The offsets of that last touch down event. Remembered to start selection there. 10883 private int mMinTouchOffset, mMaxTouchOffset; 10884 10885 // Double tap detection 10886 private long mPreviousTapUpTime = 0; 10887 private float mPreviousTapPositionX, mPreviousTapPositionY; 10888 10889 SelectionModifierCursorController() { 10890 resetTouchOffsets(); 10891 } 10892 10893 public void show() { 10894 if (isInBatchEditMode()) { 10895 return; 10896 } 10897 initDrawables(); 10898 initHandles(); 10899 hideInsertionPointCursorController(); 10900 } 10901 10902 private void initDrawables() { 10903 if (mSelectHandleLeft == null) { 10904 mSelectHandleLeft = mContext.getResources().getDrawable( 10905 mTextSelectHandleLeftRes); 10906 } 10907 if (mSelectHandleRight == null) { 10908 mSelectHandleRight = mContext.getResources().getDrawable( 10909 mTextSelectHandleRightRes); 10910 } 10911 } 10912 10913 private void initHandles() { 10914 // Lazy object creation has to be done before updatePosition() is called. 10915 if (mStartHandle == null) { 10916 mStartHandle = new SelectionStartHandleView(mSelectHandleLeft, mSelectHandleRight); 10917 } 10918 if (mEndHandle == null) { 10919 mEndHandle = new SelectionEndHandleView(mSelectHandleRight, mSelectHandleLeft); 10920 } 10921 10922 mStartHandle.show(); 10923 mEndHandle.show(); 10924 10925 // Make sure both left and right handles share the same ActionPopupWindow (so that 10926 // moving any of the handles hides the action popup). 10927 mStartHandle.showActionPopupWindow(DELAY_BEFORE_REPLACE_ACTION); 10928 mEndHandle.setActionPopupWindow(mStartHandle.getActionPopupWindow()); 10929 10930 hideInsertionPointCursorController(); 10931 } 10932 10933 public void hide() { 10934 if (mStartHandle != null) mStartHandle.hide(); 10935 if (mEndHandle != null) mEndHandle.hide(); 10936 } 10937 10938 public void onTouchEvent(MotionEvent event) { 10939 // This is done even when the View does not have focus, so that long presses can start 10940 // selection and tap can move cursor from this tap position. 10941 switch (event.getActionMasked()) { 10942 case MotionEvent.ACTION_DOWN: 10943 final float x = event.getX(); 10944 final float y = event.getY(); 10945 10946 // Remember finger down position, to be able to start selection from there 10947 mMinTouchOffset = mMaxTouchOffset = getOffsetForPosition(x, y); 10948 10949 // Double tap detection 10950 long duration = SystemClock.uptimeMillis() - mPreviousTapUpTime; 10951 if (duration <= ViewConfiguration.getDoubleTapTimeout() && 10952 isPositionOnText(x, y)) { 10953 final float deltaX = x - mPreviousTapPositionX; 10954 final float deltaY = y - mPreviousTapPositionY; 10955 final float distanceSquared = deltaX * deltaX + deltaY * deltaY; 10956 if (distanceSquared < mSquaredTouchSlopDistance) { 10957 startSelectionActionMode(); 10958 mDiscardNextActionUp = true; 10959 } 10960 } 10961 10962 mPreviousTapPositionX = x; 10963 mPreviousTapPositionY = y; 10964 break; 10965 10966 case MotionEvent.ACTION_POINTER_DOWN: 10967 case MotionEvent.ACTION_POINTER_UP: 10968 // Handle multi-point gestures. Keep min and max offset positions. 10969 // Only activated for devices that correctly handle multi-touch. 10970 if (mContext.getPackageManager().hasSystemFeature( 10971 PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT)) { 10972 updateMinAndMaxOffsets(event); 10973 } 10974 break; 10975 10976 case MotionEvent.ACTION_UP: 10977 mPreviousTapUpTime = SystemClock.uptimeMillis(); 10978 break; 10979 } 10980 } 10981 10982 /** 10983 * @param event 10984 */ 10985 private void updateMinAndMaxOffsets(MotionEvent event) { 10986 int pointerCount = event.getPointerCount(); 10987 for (int index = 0; index < pointerCount; index++) { 10988 int offset = getOffsetForPosition(event.getX(index), event.getY(index)); 10989 if (offset < mMinTouchOffset) mMinTouchOffset = offset; 10990 if (offset > mMaxTouchOffset) mMaxTouchOffset = offset; 10991 } 10992 } 10993 10994 public int getMinTouchOffset() { 10995 return mMinTouchOffset; 10996 } 10997 10998 public int getMaxTouchOffset() { 10999 return mMaxTouchOffset; 11000 } 11001 11002 public void resetTouchOffsets() { 11003 mMinTouchOffset = mMaxTouchOffset = -1; 11004 } 11005 11006 /** 11007 * @return true iff this controller is currently used to move the selection start. 11008 */ 11009 public boolean isSelectionStartDragged() { 11010 return mStartHandle != null && mStartHandle.isDragging(); 11011 } 11012 11013 public void onTouchModeChanged(boolean isInTouchMode) { 11014 if (!isInTouchMode) { 11015 hide(); 11016 } 11017 } 11018 11019 @Override 11020 public void onDetached() { 11021 final ViewTreeObserver observer = getViewTreeObserver(); 11022 observer.removeOnTouchModeChangeListener(this); 11023 11024 if (mStartHandle != null) mStartHandle.onDetached(); 11025 if (mEndHandle != null) mEndHandle.onDetached(); 11026 } 11027 } 11028 11029 private void hideInsertionPointCursorController() { 11030 // No need to create the controller to hide it. 11031 if (mInsertionPointCursorController != null) { 11032 mInsertionPointCursorController.hide(); 11033 } 11034 } 11035 11036 /** 11037 * Hides the insertion controller and stops text selection mode, hiding the selection controller 11038 */ 11039 private void hideControllers() { 11040 hideCursorControllers(); 11041 hideSpanControllers(); 11042 } 11043 11044 private void hideSpanControllers() { 11045 if (mChangeWatcher != null) { 11046 mChangeWatcher.hideControllers(); 11047 } 11048 } 11049 11050 private void hideCursorControllers() { 11051 hideInsertionPointCursorController(); 11052 stopSelectionActionMode(); 11053 } 11054 11055 /** 11056 * Get the character offset closest to the specified absolute position. A typical use case is to 11057 * pass the result of {@link MotionEvent#getX()} and {@link MotionEvent#getY()} to this method. 11058 * 11059 * @param x The horizontal absolute position of a point on screen 11060 * @param y The vertical absolute position of a point on screen 11061 * @return the character offset for the character whose position is closest to the specified 11062 * position. Returns -1 if there is no layout. 11063 */ 11064 public int getOffsetForPosition(float x, float y) { 11065 if (getLayout() == null) return -1; 11066 final int line = getLineAtCoordinate(y); 11067 final int offset = getOffsetAtCoordinate(line, x); 11068 return offset; 11069 } 11070 11071 private float convertToLocalHorizontalCoordinate(float x) { 11072 x -= getTotalPaddingLeft(); 11073 // Clamp the position to inside of the view. 11074 x = Math.max(0.0f, x); 11075 x = Math.min(getWidth() - getTotalPaddingRight() - 1, x); 11076 x += getScrollX(); 11077 return x; 11078 } 11079 11080 private int getLineAtCoordinate(float y) { 11081 y -= getTotalPaddingTop(); 11082 // Clamp the position to inside of the view. 11083 y = Math.max(0.0f, y); 11084 y = Math.min(getHeight() - getTotalPaddingBottom() - 1, y); 11085 y += getScrollY(); 11086 return getLayout().getLineForVertical((int) y); 11087 } 11088 11089 private int getOffsetAtCoordinate(int line, float x) { 11090 x = convertToLocalHorizontalCoordinate(x); 11091 return getLayout().getOffsetForHorizontal(line, x); 11092 } 11093 11094 /** Returns true if the screen coordinates position (x,y) corresponds to a character displayed 11095 * in the view. Returns false when the position is in the empty space of left/right of text. 11096 */ 11097 private boolean isPositionOnText(float x, float y) { 11098 if (getLayout() == null) return false; 11099 11100 final int line = getLineAtCoordinate(y); 11101 x = convertToLocalHorizontalCoordinate(x); 11102 11103 if (x < getLayout().getLineLeft(line)) return false; 11104 if (x > getLayout().getLineRight(line)) return false; 11105 return true; 11106 } 11107 11108 @Override 11109 public boolean onDragEvent(DragEvent event) { 11110 switch (event.getAction()) { 11111 case DragEvent.ACTION_DRAG_STARTED: 11112 return hasInsertionController(); 11113 11114 case DragEvent.ACTION_DRAG_ENTERED: 11115 TextView.this.requestFocus(); 11116 return true; 11117 11118 case DragEvent.ACTION_DRAG_LOCATION: 11119 final int offset = getOffsetForPosition(event.getX(), event.getY()); 11120 Selection.setSelection((Spannable)mText, offset); 11121 return true; 11122 11123 case DragEvent.ACTION_DROP: 11124 onDrop(event); 11125 return true; 11126 11127 case DragEvent.ACTION_DRAG_ENDED: 11128 case DragEvent.ACTION_DRAG_EXITED: 11129 default: 11130 return true; 11131 } 11132 } 11133 11134 private void onDrop(DragEvent event) { 11135 StringBuilder content = new StringBuilder(""); 11136 ClipData clipData = event.getClipData(); 11137 final int itemCount = clipData.getItemCount(); 11138 for (int i=0; i < itemCount; i++) { 11139 Item item = clipData.getItemAt(i); 11140 content.append(item.coerceToText(TextView.this.mContext)); 11141 } 11142 11143 final int offset = getOffsetForPosition(event.getX(), event.getY()); 11144 11145 Object localState = event.getLocalState(); 11146 DragLocalState dragLocalState = null; 11147 if (localState instanceof DragLocalState) { 11148 dragLocalState = (DragLocalState) localState; 11149 } 11150 boolean dragDropIntoItself = dragLocalState != null && 11151 dragLocalState.sourceTextView == this; 11152 11153 if (dragDropIntoItself) { 11154 if (offset >= dragLocalState.start && offset < dragLocalState.end) { 11155 // A drop inside the original selection discards the drop. 11156 return; 11157 } 11158 } 11159 11160 final int originalLength = mText.length(); 11161 long minMax = prepareSpacesAroundPaste(offset, offset, content); 11162 int min = extractRangeStartFromLong(minMax); 11163 int max = extractRangeEndFromLong(minMax); 11164 11165 Selection.setSelection((Spannable) mText, max); 11166 ((Editable) mText).replace(min, max, content); 11167 11168 if (dragDropIntoItself) { 11169 int dragSourceStart = dragLocalState.start; 11170 int dragSourceEnd = dragLocalState.end; 11171 if (max <= dragSourceStart) { 11172 // Inserting text before selection has shifted positions 11173 final int shift = mText.length() - originalLength; 11174 dragSourceStart += shift; 11175 dragSourceEnd += shift; 11176 } 11177 11178 // Delete original selection 11179 ((Editable) mText).delete(dragSourceStart, dragSourceEnd); 11180 11181 // Make sure we do not leave two adjacent spaces. 11182 if ((dragSourceStart == 0 || 11183 Character.isSpaceChar(mTransformed.charAt(dragSourceStart - 1))) && 11184 (dragSourceStart == mText.length() || 11185 Character.isSpaceChar(mTransformed.charAt(dragSourceStart)))) { 11186 final int pos = dragSourceStart == mText.length() ? 11187 dragSourceStart - 1 : dragSourceStart; 11188 ((Editable) mText).delete(pos, pos + 1); 11189 } 11190 } 11191 } 11192 11193 /** 11194 * @return True if this view supports insertion handles. 11195 */ 11196 boolean hasInsertionController() { 11197 return mInsertionControllerEnabled; 11198 } 11199 11200 /** 11201 * @return True if this view supports selection handles. 11202 */ 11203 boolean hasSelectionController() { 11204 return mSelectionControllerEnabled; 11205 } 11206 11207 InsertionPointCursorController getInsertionController() { 11208 if (!mInsertionControllerEnabled) { 11209 return null; 11210 } 11211 11212 if (mInsertionPointCursorController == null) { 11213 mInsertionPointCursorController = new InsertionPointCursorController(); 11214 11215 final ViewTreeObserver observer = getViewTreeObserver(); 11216 observer.addOnTouchModeChangeListener(mInsertionPointCursorController); 11217 } 11218 11219 return mInsertionPointCursorController; 11220 } 11221 11222 SelectionModifierCursorController getSelectionController() { 11223 if (!mSelectionControllerEnabled) { 11224 return null; 11225 } 11226 11227 if (mSelectionModifierCursorController == null) { 11228 mSelectionModifierCursorController = new SelectionModifierCursorController(); 11229 11230 final ViewTreeObserver observer = getViewTreeObserver(); 11231 observer.addOnTouchModeChangeListener(mSelectionModifierCursorController); 11232 } 11233 11234 return mSelectionModifierCursorController; 11235 } 11236 11237 boolean isInBatchEditMode() { 11238 final InputMethodState ims = mInputMethodState; 11239 if (ims != null) { 11240 return ims.mBatchEditNesting > 0; 11241 } 11242 return mInBatchEditControllers; 11243 } 11244 11245 @Override 11246 protected void resolveTextDirection() { 11247 if (hasPasswordTransformationMethod()) { 11248 mTextDir = TextDirectionHeuristics.LOCALE; 11249 return; 11250 } 11251 11252 // Always need to resolve layout direction first 11253 final boolean defaultIsRtl = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL); 11254 11255 // Then resolve text direction on the parent 11256 super.resolveTextDirection(); 11257 11258 // Now, we can select the heuristic 11259 int textDir = getResolvedTextDirection(); 11260 switch (textDir) { 11261 default: 11262 case TEXT_DIRECTION_FIRST_STRONG: 11263 mTextDir = (defaultIsRtl ? TextDirectionHeuristics.FIRSTSTRONG_RTL : 11264 TextDirectionHeuristics.FIRSTSTRONG_LTR); 11265 break; 11266 case TEXT_DIRECTION_ANY_RTL: 11267 mTextDir = TextDirectionHeuristics.ANYRTL_LTR; 11268 break; 11269 case TEXT_DIRECTION_LTR: 11270 mTextDir = TextDirectionHeuristics.LTR; 11271 break; 11272 case TEXT_DIRECTION_RTL: 11273 mTextDir = TextDirectionHeuristics.RTL; 11274 break; 11275 } 11276 } 11277 11278 /** 11279 * Subclasses will need to override this method to implement their own way of resolving 11280 * drawables depending on the layout direction. 11281 * 11282 * A call to the super method will be required from the subclasses implementation. 11283 * 11284 */ 11285 protected void resolveDrawables() { 11286 // No need to resolve twice 11287 if (mResolvedDrawables) { 11288 return; 11289 } 11290 // No drawable to resolve 11291 if (mDrawables == null) { 11292 return; 11293 } 11294 // No relative drawable to resolve 11295 if (mDrawables.mDrawableStart == null && mDrawables.mDrawableEnd == null) { 11296 mResolvedDrawables = true; 11297 return; 11298 } 11299 11300 Drawables dr = mDrawables; 11301 switch(getResolvedLayoutDirection()) { 11302 case LAYOUT_DIRECTION_RTL: 11303 if (dr.mDrawableStart != null) { 11304 dr.mDrawableRight = dr.mDrawableStart; 11305 11306 dr.mDrawableSizeRight = dr.mDrawableSizeStart; 11307 dr.mDrawableHeightRight = dr.mDrawableHeightStart; 11308 } 11309 if (dr.mDrawableEnd != null) { 11310 dr.mDrawableLeft = dr.mDrawableEnd; 11311 11312 dr.mDrawableSizeLeft = dr.mDrawableSizeEnd; 11313 dr.mDrawableHeightLeft = dr.mDrawableHeightEnd; 11314 } 11315 break; 11316 11317 case LAYOUT_DIRECTION_LTR: 11318 default: 11319 if (dr.mDrawableStart != null) { 11320 dr.mDrawableLeft = dr.mDrawableStart; 11321 11322 dr.mDrawableSizeLeft = dr.mDrawableSizeStart; 11323 dr.mDrawableHeightLeft = dr.mDrawableHeightStart; 11324 } 11325 if (dr.mDrawableEnd != null) { 11326 dr.mDrawableRight = dr.mDrawableEnd; 11327 11328 dr.mDrawableSizeRight = dr.mDrawableSizeEnd; 11329 dr.mDrawableHeightRight = dr.mDrawableHeightEnd; 11330 } 11331 break; 11332 } 11333 mResolvedDrawables = true; 11334 } 11335 11336 protected void resetResolvedDrawables() { 11337 mResolvedDrawables = false; 11338 } 11339 11340 /** 11341 * @hide 11342 */ 11343 protected void viewClicked(InputMethodManager imm) { 11344 if (imm != null) { 11345 imm.viewClicked(this); 11346 } 11347 } 11348 11349 @ViewDebug.ExportedProperty(category = "text") 11350 private CharSequence mText; 11351 private CharSequence mTransformed; 11352 private BufferType mBufferType = BufferType.NORMAL; 11353 11354 private int mInputType = EditorInfo.TYPE_NULL; 11355 private CharSequence mHint; 11356 private Layout mHintLayout; 11357 11358 private KeyListener mInput; 11359 11360 private MovementMethod mMovement; 11361 private TransformationMethod mTransformation; 11362 private boolean mAllowTransformationLengthChange; 11363 private ChangeWatcher mChangeWatcher; 11364 11365 private ArrayList<TextWatcher> mListeners = null; 11366 11367 // display attributes 11368 private final TextPaint mTextPaint; 11369 private boolean mUserSetTextScaleX; 11370 private final Paint mHighlightPaint; 11371 private int mHighlightColor = 0x6633B5E5; 11372 /** 11373 * This is temporarily visible to fix bug 3085564 in webView. Do not rely on 11374 * this field being protected. Will be restored as private when lineHeight 11375 * feature request 3215097 is implemented 11376 * @hide 11377 */ 11378 protected Layout mLayout; 11379 11380 private long mShowCursor; 11381 private Blink mBlink; 11382 private boolean mCursorVisible = true; 11383 11384 // Cursor Controllers. 11385 private InsertionPointCursorController mInsertionPointCursorController; 11386 private SelectionModifierCursorController mSelectionModifierCursorController; 11387 private ActionMode mSelectionActionMode; 11388 private boolean mInsertionControllerEnabled; 11389 private boolean mSelectionControllerEnabled; 11390 private boolean mInBatchEditControllers; 11391 11392 // These are needed to desambiguate a long click. If the long click comes from ones of these, we 11393 // select from the current cursor position. Otherwise, select from long pressed position. 11394 private boolean mDPadCenterIsDown = false; 11395 private boolean mEnterKeyIsDown = false; 11396 private boolean mContextMenuTriggeredByKey = false; 11397 11398 private boolean mSelectAllOnFocus = false; 11399 11400 private int mGravity = Gravity.TOP | Gravity.START; 11401 private boolean mHorizontallyScrolling; 11402 11403 private int mAutoLinkMask; 11404 private boolean mLinksClickable = true; 11405 11406 private float mSpacingMult = 1.0f; 11407 private float mSpacingAdd = 0.0f; 11408 private boolean mTextIsSelectable = false; 11409 11410 private static final int LINES = 1; 11411 private static final int EMS = LINES; 11412 private static final int PIXELS = 2; 11413 11414 private int mMaximum = Integer.MAX_VALUE; 11415 private int mMaxMode = LINES; 11416 private int mMinimum = 0; 11417 private int mMinMode = LINES; 11418 11419 private int mOldMaximum = mMaximum; 11420 private int mOldMaxMode = mMaxMode; 11421 11422 private int mMaxWidth = Integer.MAX_VALUE; 11423 private int mMaxWidthMode = PIXELS; 11424 private int mMinWidth = 0; 11425 private int mMinWidthMode = PIXELS; 11426 11427 private boolean mSingleLine; 11428 private int mDesiredHeightAtMeasure = -1; 11429 private boolean mIncludePad = true; 11430 11431 // tmp primitives, so we don't alloc them on each draw 11432 private Path mHighlightPath; 11433 private boolean mHighlightPathBogus = true; 11434 private static final RectF sTempRect = new RectF(); 11435 11436 // XXX should be much larger 11437 private static final int VERY_WIDE = 16384; 11438 11439 private static final int BLINK = 500; 11440 11441 private static final int ANIMATED_SCROLL_GAP = 250; 11442 private long mLastScroll; 11443 private Scroller mScroller = null; 11444 11445 private BoringLayout.Metrics mBoring; 11446 private BoringLayout.Metrics mHintBoring; 11447 11448 private BoringLayout mSavedLayout, mSavedHintLayout; 11449 11450 private TextDirectionHeuristic mTextDir = null; 11451 11452 private static final InputFilter[] NO_FILTERS = new InputFilter[0]; 11453 private InputFilter[] mFilters = NO_FILTERS; 11454 private static final Spanned EMPTY_SPANNED = new SpannedString(""); 11455 private static int DRAG_SHADOW_MAX_TEXT_LENGTH = 20; 11456 // System wide time for last cut or copy action. 11457 private static long sLastCutOrCopyTime; 11458 // Used to highlight a word when it is corrected by the IME 11459 private CorrectionHighlighter mCorrectionHighlighter; 11460 // New state used to change background based on whether this TextView is multiline. 11461 private static final int[] MULTILINE_STATE_SET = { R.attr.state_multiline }; 11462} 11463