TextView.java revision 76866208f81988fa653568513112b152e156d806
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 stops a possible text selection mode. Maybe not intended. 5758 hideControllers(); 5759 } 5760 5761 /** 5762 * Called by the framework in response to a text completion from 5763 * the current input method, provided by it calling 5764 * {@link InputConnection#commitCompletion 5765 * InputConnection.commitCompletion()}. The default implementation does 5766 * nothing; text views that are supporting auto-completion should override 5767 * this to do their desired behavior. 5768 * 5769 * @param text The auto complete text the user has selected. 5770 */ 5771 public void onCommitCompletion(CompletionInfo text) { 5772 // intentionally empty 5773 } 5774 5775 /** 5776 * Called by the framework in response to a text auto-correction (such as fixing a typo using a 5777 * a dictionnary) from the current input method, provided by it calling 5778 * {@link InputConnection#commitCorrection} InputConnection.commitCorrection()}. The default 5779 * implementation flashes the background of the corrected word to provide feedback to the user. 5780 * 5781 * @param info The auto correct info about the text that was corrected. 5782 */ 5783 public void onCommitCorrection(CorrectionInfo info) { 5784 if (mCorrectionHighlighter == null) { 5785 mCorrectionHighlighter = new CorrectionHighlighter(); 5786 } else { 5787 mCorrectionHighlighter.invalidate(false); 5788 } 5789 5790 mCorrectionHighlighter.highlight(info); 5791 } 5792 5793 private class CorrectionHighlighter { 5794 private final Path mPath = new Path(); 5795 private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 5796 private int mStart, mEnd; 5797 private long mFadingStartTime; 5798 private final static int FADE_OUT_DURATION = 400; 5799 5800 public CorrectionHighlighter() { 5801 mPaint.setCompatibilityScaling(getResources().getCompatibilityInfo().applicationScale); 5802 mPaint.setStyle(Paint.Style.FILL); 5803 } 5804 5805 public void highlight(CorrectionInfo info) { 5806 mStart = info.getOffset(); 5807 mEnd = mStart + info.getNewText().length(); 5808 mFadingStartTime = SystemClock.uptimeMillis(); 5809 5810 if (mStart < 0 || mEnd < 0) { 5811 stopAnimation(); 5812 } 5813 } 5814 5815 public void draw(Canvas canvas, int cursorOffsetVertical) { 5816 if (updatePath() && updatePaint()) { 5817 if (cursorOffsetVertical != 0) { 5818 canvas.translate(0, cursorOffsetVertical); 5819 } 5820 5821 canvas.drawPath(mPath, mPaint); 5822 5823 if (cursorOffsetVertical != 0) { 5824 canvas.translate(0, -cursorOffsetVertical); 5825 } 5826 invalidate(true); 5827 } else { 5828 stopAnimation(); 5829 invalidate(false); 5830 } 5831 } 5832 5833 private boolean updatePaint() { 5834 final long duration = SystemClock.uptimeMillis() - mFadingStartTime; 5835 if (duration > FADE_OUT_DURATION) return false; 5836 5837 final float coef = 1.0f - (float) duration / FADE_OUT_DURATION; 5838 final int highlightColorAlpha = Color.alpha(mHighlightColor); 5839 final int color = (mHighlightColor & 0x00FFFFFF) + 5840 ((int) (highlightColorAlpha * coef) << 24); 5841 mPaint.setColor(color); 5842 return true; 5843 } 5844 5845 private boolean updatePath() { 5846 final Layout layout = TextView.this.mLayout; 5847 if (layout == null) return false; 5848 5849 // Update in case text is edited while the animation is run 5850 final int length = mText.length(); 5851 int start = Math.min(length, mStart); 5852 int end = Math.min(length, mEnd); 5853 5854 mPath.reset(); 5855 TextView.this.mLayout.getSelectionPath(start, end, mPath); 5856 return true; 5857 } 5858 5859 private void invalidate(boolean delayed) { 5860 if (TextView.this.mLayout == null) return; 5861 5862 synchronized (sTempRect) { 5863 mPath.computeBounds(sTempRect, false); 5864 5865 int left = getCompoundPaddingLeft(); 5866 int top = getExtendedPaddingTop() + getVerticalOffset(true); 5867 5868 if (delayed) { 5869 TextView.this.postInvalidateDelayed(16, // 60 Hz update 5870 left + (int) sTempRect.left, top + (int) sTempRect.top, 5871 left + (int) sTempRect.right, top + (int) sTempRect.bottom); 5872 } else { 5873 TextView.this.postInvalidate((int) sTempRect.left, (int) sTempRect.top, 5874 (int) sTempRect.right, (int) sTempRect.bottom); 5875 } 5876 } 5877 } 5878 5879 private void stopAnimation() { 5880 TextView.this.mCorrectionHighlighter = null; 5881 } 5882 } 5883 5884 public void beginBatchEdit() { 5885 mInBatchEditControllers = true; 5886 final InputMethodState ims = mInputMethodState; 5887 if (ims != null) { 5888 int nesting = ++ims.mBatchEditNesting; 5889 if (nesting == 1) { 5890 ims.mCursorChanged = false; 5891 ims.mChangedDelta = 0; 5892 if (ims.mContentChanged) { 5893 // We already have a pending change from somewhere else, 5894 // so turn this into a full update. 5895 ims.mChangedStart = 0; 5896 ims.mChangedEnd = mText.length(); 5897 } else { 5898 ims.mChangedStart = EXTRACT_UNKNOWN; 5899 ims.mChangedEnd = EXTRACT_UNKNOWN; 5900 ims.mContentChanged = false; 5901 } 5902 onBeginBatchEdit(); 5903 } 5904 } 5905 } 5906 5907 public void endBatchEdit() { 5908 mInBatchEditControllers = false; 5909 final InputMethodState ims = mInputMethodState; 5910 if (ims != null) { 5911 int nesting = --ims.mBatchEditNesting; 5912 if (nesting == 0) { 5913 finishBatchEdit(ims); 5914 } 5915 } 5916 } 5917 5918 void ensureEndedBatchEdit() { 5919 final InputMethodState ims = mInputMethodState; 5920 if (ims != null && ims.mBatchEditNesting != 0) { 5921 ims.mBatchEditNesting = 0; 5922 finishBatchEdit(ims); 5923 } 5924 } 5925 5926 void finishBatchEdit(final InputMethodState ims) { 5927 onEndBatchEdit(); 5928 5929 if (ims.mContentChanged || ims.mSelectionModeChanged) { 5930 updateAfterEdit(); 5931 reportExtractedText(); 5932 } else if (ims.mCursorChanged) { 5933 // Cheezy way to get us to report the current cursor location. 5934 invalidateCursor(); 5935 } 5936 } 5937 5938 void updateAfterEdit() { 5939 invalidate(); 5940 int curs = getSelectionStart(); 5941 5942 if (curs >= 0 || (mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { 5943 registerForPreDraw(); 5944 } 5945 5946 if (curs >= 0) { 5947 mHighlightPathBogus = true; 5948 makeBlink(); 5949 bringPointIntoView(curs); 5950 } 5951 5952 checkForResize(); 5953 } 5954 5955 /** 5956 * Called by the framework in response to a request to begin a batch 5957 * of edit operations through a call to link {@link #beginBatchEdit()}. 5958 */ 5959 public void onBeginBatchEdit() { 5960 // intentionally empty 5961 } 5962 5963 /** 5964 * Called by the framework in response to a request to end a batch 5965 * of edit operations through a call to link {@link #endBatchEdit}. 5966 */ 5967 public void onEndBatchEdit() { 5968 // intentionally empty 5969 } 5970 5971 /** 5972 * Called by the framework in response to a private command from the 5973 * current method, provided by it calling 5974 * {@link InputConnection#performPrivateCommand 5975 * InputConnection.performPrivateCommand()}. 5976 * 5977 * @param action The action name of the command. 5978 * @param data Any additional data for the command. This may be null. 5979 * @return Return true if you handled the command, else false. 5980 */ 5981 public boolean onPrivateIMECommand(String action, Bundle data) { 5982 return false; 5983 } 5984 5985 private void nullLayouts() { 5986 if (mLayout instanceof BoringLayout && mSavedLayout == null) { 5987 mSavedLayout = (BoringLayout) mLayout; 5988 } 5989 if (mHintLayout instanceof BoringLayout && mSavedHintLayout == null) { 5990 mSavedHintLayout = (BoringLayout) mHintLayout; 5991 } 5992 5993 mSavedMarqueeModeLayout = mLayout = mHintLayout = null; 5994 5995 // Since it depends on the value of mLayout 5996 prepareCursorControllers(); 5997 } 5998 5999 /** 6000 * Make a new Layout based on the already-measured size of the view, 6001 * on the assumption that it was measured correctly at some point. 6002 */ 6003 private void assumeLayout() { 6004 int width = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); 6005 6006 if (width < 1) { 6007 width = 0; 6008 } 6009 6010 int physicalWidth = width; 6011 6012 if (mHorizontallyScrolling) { 6013 width = VERY_WIDE; 6014 } 6015 6016 makeNewLayout(width, physicalWidth, UNKNOWN_BORING, UNKNOWN_BORING, 6017 physicalWidth, false); 6018 } 6019 6020 @Override 6021 protected void resetResolvedLayoutDirection() { 6022 super.resetResolvedLayoutDirection(); 6023 6024 if (mLayoutAlignment != null && 6025 (mTextAlign == TextAlign.VIEW_START || 6026 mTextAlign == TextAlign.VIEW_END)) { 6027 mLayoutAlignment = null; 6028 } 6029 } 6030 6031 private Layout.Alignment getLayoutAlignment() { 6032 if (mLayoutAlignment == null) { 6033 Layout.Alignment alignment; 6034 TextAlign textAlign = mTextAlign; 6035 switch (textAlign) { 6036 case INHERIT: 6037 // fall through to gravity temporarily 6038 // intention is to inherit value through view hierarchy. 6039 case GRAVITY: 6040 switch (mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) { 6041 case Gravity.START: 6042 alignment = Layout.Alignment.ALIGN_NORMAL; 6043 break; 6044 case Gravity.END: 6045 alignment = Layout.Alignment.ALIGN_OPPOSITE; 6046 break; 6047 case Gravity.LEFT: 6048 alignment = Layout.Alignment.ALIGN_LEFT; 6049 break; 6050 case Gravity.RIGHT: 6051 alignment = Layout.Alignment.ALIGN_RIGHT; 6052 break; 6053 case Gravity.CENTER_HORIZONTAL: 6054 alignment = Layout.Alignment.ALIGN_CENTER; 6055 break; 6056 default: 6057 alignment = Layout.Alignment.ALIGN_NORMAL; 6058 break; 6059 } 6060 break; 6061 case TEXT_START: 6062 alignment = Layout.Alignment.ALIGN_NORMAL; 6063 break; 6064 case TEXT_END: 6065 alignment = Layout.Alignment.ALIGN_OPPOSITE; 6066 break; 6067 case CENTER: 6068 alignment = Layout.Alignment.ALIGN_CENTER; 6069 break; 6070 case VIEW_START: 6071 alignment = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ? 6072 Layout.Alignment.ALIGN_RIGHT : Layout.Alignment.ALIGN_LEFT; 6073 break; 6074 case VIEW_END: 6075 alignment = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ? 6076 Layout.Alignment.ALIGN_LEFT : Layout.Alignment.ALIGN_RIGHT; 6077 break; 6078 default: 6079 alignment = Layout.Alignment.ALIGN_NORMAL; 6080 break; 6081 } 6082 mLayoutAlignment = alignment; 6083 } 6084 return mLayoutAlignment; 6085 } 6086 6087 /** 6088 * The width passed in is now the desired layout width, 6089 * not the full view width with padding. 6090 * {@hide} 6091 */ 6092 protected void makeNewLayout(int w, int hintWidth, 6093 BoringLayout.Metrics boring, 6094 BoringLayout.Metrics hintBoring, 6095 int ellipsisWidth, boolean bringIntoView) { 6096 stopMarquee(); 6097 6098 // Update "old" cached values 6099 mOldMaximum = mMaximum; 6100 mOldMaxMode = mMaxMode; 6101 6102 mHighlightPathBogus = true; 6103 6104 if (w < 0) { 6105 w = 0; 6106 } 6107 if (hintWidth < 0) { 6108 hintWidth = 0; 6109 } 6110 6111 Layout.Alignment alignment = getLayoutAlignment(); 6112 boolean shouldEllipsize = mEllipsize != null && mInput == null; 6113 final boolean switchEllipsize = mEllipsize == TruncateAt.MARQUEE && 6114 mMarqueeFadeMode != MARQUEE_FADE_NORMAL; 6115 TruncateAt effectiveEllipsize = mEllipsize; 6116 if (mEllipsize == TruncateAt.MARQUEE && 6117 mMarqueeFadeMode == MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { 6118 effectiveEllipsize = TruncateAt.END; 6119 } 6120 6121 if (mTextDir == null) { 6122 resolveTextDirection(); 6123 } 6124 6125 mLayout = makeSingleLayout(w, boring, ellipsisWidth, alignment, shouldEllipsize, 6126 effectiveEllipsize, effectiveEllipsize == mEllipsize); 6127 if (switchEllipsize) { 6128 TruncateAt oppositeEllipsize = effectiveEllipsize == TruncateAt.MARQUEE ? 6129 TruncateAt.END : TruncateAt.MARQUEE; 6130 mSavedMarqueeModeLayout = makeSingleLayout(w, boring, ellipsisWidth, alignment, 6131 shouldEllipsize, oppositeEllipsize, effectiveEllipsize != mEllipsize); 6132 } 6133 6134 shouldEllipsize = mEllipsize != null; 6135 mHintLayout = null; 6136 6137 if (mHint != null) { 6138 if (shouldEllipsize) hintWidth = w; 6139 6140 if (hintBoring == UNKNOWN_BORING) { 6141 hintBoring = BoringLayout.isBoring(mHint, mTextPaint, mTextDir, 6142 mHintBoring); 6143 if (hintBoring != null) { 6144 mHintBoring = hintBoring; 6145 } 6146 } 6147 6148 if (hintBoring != null) { 6149 if (hintBoring.width <= hintWidth && 6150 (!shouldEllipsize || hintBoring.width <= ellipsisWidth)) { 6151 if (mSavedHintLayout != null) { 6152 mHintLayout = mSavedHintLayout. 6153 replaceOrMake(mHint, mTextPaint, 6154 hintWidth, alignment, mSpacingMult, mSpacingAdd, 6155 hintBoring, mIncludePad); 6156 } else { 6157 mHintLayout = BoringLayout.make(mHint, mTextPaint, 6158 hintWidth, alignment, mSpacingMult, mSpacingAdd, 6159 hintBoring, mIncludePad); 6160 } 6161 6162 mSavedHintLayout = (BoringLayout) mHintLayout; 6163 } else if (shouldEllipsize && hintBoring.width <= hintWidth) { 6164 if (mSavedHintLayout != null) { 6165 mHintLayout = mSavedHintLayout. 6166 replaceOrMake(mHint, mTextPaint, 6167 hintWidth, alignment, mSpacingMult, mSpacingAdd, 6168 hintBoring, mIncludePad, mEllipsize, 6169 ellipsisWidth); 6170 } else { 6171 mHintLayout = BoringLayout.make(mHint, mTextPaint, 6172 hintWidth, alignment, mSpacingMult, mSpacingAdd, 6173 hintBoring, mIncludePad, mEllipsize, 6174 ellipsisWidth); 6175 } 6176 } else if (shouldEllipsize) { 6177 mHintLayout = new StaticLayout(mHint, 6178 0, mHint.length(), 6179 mTextPaint, hintWidth, alignment, mTextDir, mSpacingMult, 6180 mSpacingAdd, mIncludePad, mEllipsize, 6181 ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE); 6182 } else { 6183 mHintLayout = new StaticLayout(mHint, mTextPaint, 6184 hintWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, 6185 mIncludePad); 6186 } 6187 } else if (shouldEllipsize) { 6188 mHintLayout = new StaticLayout(mHint, 6189 0, mHint.length(), 6190 mTextPaint, hintWidth, alignment, mTextDir, mSpacingMult, 6191 mSpacingAdd, mIncludePad, mEllipsize, 6192 ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE); 6193 } else { 6194 mHintLayout = new StaticLayout(mHint, mTextPaint, 6195 hintWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, 6196 mIncludePad); 6197 } 6198 } 6199 6200 if (bringIntoView) { 6201 registerForPreDraw(); 6202 } 6203 6204 if (mEllipsize == TextUtils.TruncateAt.MARQUEE) { 6205 if (!compressText(ellipsisWidth)) { 6206 final int height = mLayoutParams.height; 6207 // If the size of the view does not depend on the size of the text, try to 6208 // start the marquee immediately 6209 if (height != LayoutParams.WRAP_CONTENT && height != LayoutParams.MATCH_PARENT) { 6210 startMarquee(); 6211 } else { 6212 // Defer the start of the marquee until we know our width (see setFrame()) 6213 mRestartMarquee = true; 6214 } 6215 } 6216 } 6217 6218 // CursorControllers need a non-null mLayout 6219 prepareCursorControllers(); 6220 } 6221 6222 private Layout makeSingleLayout(int w, BoringLayout.Metrics boring, int ellipsisWidth, 6223 Layout.Alignment alignment, boolean shouldEllipsize, TruncateAt effectiveEllipsize, 6224 boolean useSaved) { 6225 Layout result = null; 6226 if (mText instanceof Spannable) { 6227 result = new DynamicLayout(mText, mTransformed, mTextPaint, w, 6228 alignment, mTextDir, mSpacingMult, 6229 mSpacingAdd, mIncludePad, mInput == null ? effectiveEllipsize : null, 6230 ellipsisWidth); 6231 } else { 6232 if (boring == UNKNOWN_BORING) { 6233 boring = BoringLayout.isBoring(mTransformed, mTextPaint, mTextDir, mBoring); 6234 if (boring != null) { 6235 mBoring = boring; 6236 } 6237 } 6238 6239 if (boring != null) { 6240 if (boring.width <= w && 6241 (effectiveEllipsize == null || boring.width <= ellipsisWidth)) { 6242 if (useSaved && mSavedLayout != null) { 6243 result = mSavedLayout.replaceOrMake(mTransformed, mTextPaint, 6244 w, alignment, mSpacingMult, mSpacingAdd, 6245 boring, mIncludePad); 6246 } else { 6247 result = BoringLayout.make(mTransformed, mTextPaint, 6248 w, alignment, mSpacingMult, mSpacingAdd, 6249 boring, mIncludePad); 6250 } 6251 6252 if (useSaved) { 6253 mSavedLayout = (BoringLayout) result; 6254 } 6255 } else if (shouldEllipsize && boring.width <= w) { 6256 if (useSaved && mSavedLayout != null) { 6257 result = mSavedLayout.replaceOrMake(mTransformed, mTextPaint, 6258 w, alignment, mSpacingMult, mSpacingAdd, 6259 boring, mIncludePad, effectiveEllipsize, 6260 ellipsisWidth); 6261 } else { 6262 result = BoringLayout.make(mTransformed, mTextPaint, 6263 w, alignment, mSpacingMult, mSpacingAdd, 6264 boring, mIncludePad, effectiveEllipsize, 6265 ellipsisWidth); 6266 } 6267 } else if (shouldEllipsize) { 6268 result = new StaticLayout(mTransformed, 6269 0, mTransformed.length(), 6270 mTextPaint, w, alignment, mTextDir, mSpacingMult, 6271 mSpacingAdd, mIncludePad, effectiveEllipsize, 6272 ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE); 6273 } else { 6274 result = new StaticLayout(mTransformed, mTextPaint, 6275 w, alignment, mTextDir, mSpacingMult, mSpacingAdd, 6276 mIncludePad); 6277 } 6278 } else if (shouldEllipsize) { 6279 result = new StaticLayout(mTransformed, 6280 0, mTransformed.length(), 6281 mTextPaint, w, alignment, mTextDir, mSpacingMult, 6282 mSpacingAdd, mIncludePad, effectiveEllipsize, 6283 ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE); 6284 } else { 6285 result = new StaticLayout(mTransformed, mTextPaint, 6286 w, alignment, mTextDir, mSpacingMult, mSpacingAdd, 6287 mIncludePad); 6288 } 6289 } 6290 return result; 6291 } 6292 6293 private boolean compressText(float width) { 6294 if (isHardwareAccelerated()) return false; 6295 6296 // Only compress the text if it hasn't been compressed by the previous pass 6297 if (width > 0.0f && mLayout != null && getLineCount() == 1 && !mUserSetTextScaleX && 6298 mTextPaint.getTextScaleX() == 1.0f) { 6299 final float textWidth = mLayout.getLineWidth(0); 6300 final float overflow = (textWidth + 1.0f - width) / width; 6301 if (overflow > 0.0f && overflow <= Marquee.MARQUEE_DELTA_MAX) { 6302 mTextPaint.setTextScaleX(1.0f - overflow - 0.005f); 6303 post(new Runnable() { 6304 public void run() { 6305 requestLayout(); 6306 } 6307 }); 6308 return true; 6309 } 6310 } 6311 6312 return false; 6313 } 6314 6315 private static int desired(Layout layout) { 6316 int n = layout.getLineCount(); 6317 CharSequence text = layout.getText(); 6318 float max = 0; 6319 6320 // if any line was wrapped, we can't use it. 6321 // but it's ok for the last line not to have a newline 6322 6323 for (int i = 0; i < n - 1; i++) { 6324 if (text.charAt(layout.getLineEnd(i) - 1) != '\n') 6325 return -1; 6326 } 6327 6328 for (int i = 0; i < n; i++) { 6329 max = Math.max(max, layout.getLineWidth(i)); 6330 } 6331 6332 return (int) FloatMath.ceil(max); 6333 } 6334 6335 /** 6336 * Set whether the TextView includes extra top and bottom padding to make 6337 * room for accents that go above the normal ascent and descent. 6338 * The default is true. 6339 * 6340 * @attr ref android.R.styleable#TextView_includeFontPadding 6341 */ 6342 public void setIncludeFontPadding(boolean includepad) { 6343 if (mIncludePad != includepad) { 6344 mIncludePad = includepad; 6345 6346 if (mLayout != null) { 6347 nullLayouts(); 6348 requestLayout(); 6349 invalidate(); 6350 } 6351 } 6352 } 6353 6354 private static final BoringLayout.Metrics UNKNOWN_BORING = new BoringLayout.Metrics(); 6355 6356 @Override 6357 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 6358 int widthMode = MeasureSpec.getMode(widthMeasureSpec); 6359 int heightMode = MeasureSpec.getMode(heightMeasureSpec); 6360 int widthSize = MeasureSpec.getSize(widthMeasureSpec); 6361 int heightSize = MeasureSpec.getSize(heightMeasureSpec); 6362 6363 int width; 6364 int height; 6365 6366 BoringLayout.Metrics boring = UNKNOWN_BORING; 6367 BoringLayout.Metrics hintBoring = UNKNOWN_BORING; 6368 6369 if (mTextDir == null) { 6370 resolveTextDirection(); 6371 } 6372 6373 int des = -1; 6374 boolean fromexisting = false; 6375 6376 if (widthMode == MeasureSpec.EXACTLY) { 6377 // Parent has told us how big to be. So be it. 6378 width = widthSize; 6379 } else { 6380 if (mLayout != null && mEllipsize == null) { 6381 des = desired(mLayout); 6382 } 6383 6384 if (des < 0) { 6385 boring = BoringLayout.isBoring(mTransformed, mTextPaint, mTextDir, mBoring); 6386 if (boring != null) { 6387 mBoring = boring; 6388 } 6389 } else { 6390 fromexisting = true; 6391 } 6392 6393 if (boring == null || boring == UNKNOWN_BORING) { 6394 if (des < 0) { 6395 des = (int) FloatMath.ceil(Layout.getDesiredWidth(mTransformed, mTextPaint)); 6396 } 6397 6398 width = des; 6399 } else { 6400 width = boring.width; 6401 } 6402 6403 final Drawables dr = mDrawables; 6404 if (dr != null) { 6405 width = Math.max(width, dr.mDrawableWidthTop); 6406 width = Math.max(width, dr.mDrawableWidthBottom); 6407 } 6408 6409 if (mHint != null) { 6410 int hintDes = -1; 6411 int hintWidth; 6412 6413 if (mHintLayout != null && mEllipsize == null) { 6414 hintDes = desired(mHintLayout); 6415 } 6416 6417 if (hintDes < 0) { 6418 hintBoring = BoringLayout.isBoring(mHint, mTextPaint, mHintBoring); 6419 if (hintBoring != null) { 6420 mHintBoring = hintBoring; 6421 } 6422 } 6423 6424 if (hintBoring == null || hintBoring == UNKNOWN_BORING) { 6425 if (hintDes < 0) { 6426 hintDes = (int) FloatMath.ceil( 6427 Layout.getDesiredWidth(mHint, mTextPaint)); 6428 } 6429 6430 hintWidth = hintDes; 6431 } else { 6432 hintWidth = hintBoring.width; 6433 } 6434 6435 if (hintWidth > width) { 6436 width = hintWidth; 6437 } 6438 } 6439 6440 width += getCompoundPaddingLeft() + getCompoundPaddingRight(); 6441 6442 if (mMaxWidthMode == EMS) { 6443 width = Math.min(width, mMaxWidth * getLineHeight()); 6444 } else { 6445 width = Math.min(width, mMaxWidth); 6446 } 6447 6448 if (mMinWidthMode == EMS) { 6449 width = Math.max(width, mMinWidth * getLineHeight()); 6450 } else { 6451 width = Math.max(width, mMinWidth); 6452 } 6453 6454 // Check against our minimum width 6455 width = Math.max(width, getSuggestedMinimumWidth()); 6456 6457 if (widthMode == MeasureSpec.AT_MOST) { 6458 width = Math.min(widthSize, width); 6459 } 6460 } 6461 6462 int want = width - getCompoundPaddingLeft() - getCompoundPaddingRight(); 6463 int unpaddedWidth = want; 6464 6465 if (mHorizontallyScrolling) want = VERY_WIDE; 6466 6467 int hintWant = want; 6468 int hintWidth = (mHintLayout == null) ? hintWant : mHintLayout.getWidth(); 6469 6470 if (mLayout == null) { 6471 makeNewLayout(want, hintWant, boring, hintBoring, 6472 width - getCompoundPaddingLeft() - getCompoundPaddingRight(), false); 6473 } else { 6474 final boolean layoutChanged = (mLayout.getWidth() != want) || 6475 (hintWidth != hintWant) || 6476 (mLayout.getEllipsizedWidth() != 6477 width - getCompoundPaddingLeft() - getCompoundPaddingRight()); 6478 6479 final boolean widthChanged = (mHint == null) && 6480 (mEllipsize == null) && 6481 (want > mLayout.getWidth()) && 6482 (mLayout instanceof BoringLayout || (fromexisting && des >= 0 && des <= want)); 6483 6484 final boolean maximumChanged = (mMaxMode != mOldMaxMode) || (mMaximum != mOldMaximum); 6485 6486 if (layoutChanged || maximumChanged) { 6487 if (!maximumChanged && widthChanged) { 6488 mLayout.increaseWidthTo(want); 6489 } else { 6490 makeNewLayout(want, hintWant, boring, hintBoring, 6491 width - getCompoundPaddingLeft() - getCompoundPaddingRight(), false); 6492 } 6493 } else { 6494 // Nothing has changed 6495 } 6496 } 6497 6498 if (heightMode == MeasureSpec.EXACTLY) { 6499 // Parent has told us how big to be. So be it. 6500 height = heightSize; 6501 mDesiredHeightAtMeasure = -1; 6502 } else { 6503 int desired = getDesiredHeight(); 6504 6505 height = desired; 6506 mDesiredHeightAtMeasure = desired; 6507 6508 if (heightMode == MeasureSpec.AT_MOST) { 6509 height = Math.min(desired, heightSize); 6510 } 6511 } 6512 6513 int unpaddedHeight = height - getCompoundPaddingTop() - getCompoundPaddingBottom(); 6514 if (mMaxMode == LINES && mLayout.getLineCount() > mMaximum) { 6515 unpaddedHeight = Math.min(unpaddedHeight, mLayout.getLineTop(mMaximum)); 6516 } 6517 6518 /* 6519 * We didn't let makeNewLayout() register to bring the cursor into view, 6520 * so do it here if there is any possibility that it is needed. 6521 */ 6522 if (mMovement != null || 6523 mLayout.getWidth() > unpaddedWidth || 6524 mLayout.getHeight() > unpaddedHeight) { 6525 registerForPreDraw(); 6526 } else { 6527 scrollTo(0, 0); 6528 } 6529 6530 setMeasuredDimension(width, height); 6531 } 6532 6533 private int getDesiredHeight() { 6534 return Math.max( 6535 getDesiredHeight(mLayout, true), 6536 getDesiredHeight(mHintLayout, mEllipsize != null)); 6537 } 6538 6539 private int getDesiredHeight(Layout layout, boolean cap) { 6540 if (layout == null) { 6541 return 0; 6542 } 6543 6544 int linecount = layout.getLineCount(); 6545 int pad = getCompoundPaddingTop() + getCompoundPaddingBottom(); 6546 int desired = layout.getLineTop(linecount); 6547 6548 final Drawables dr = mDrawables; 6549 if (dr != null) { 6550 desired = Math.max(desired, dr.mDrawableHeightLeft); 6551 desired = Math.max(desired, dr.mDrawableHeightRight); 6552 } 6553 6554 desired += pad; 6555 6556 if (mMaxMode == LINES) { 6557 /* 6558 * Don't cap the hint to a certain number of lines. 6559 * (Do cap it, though, if we have a maximum pixel height.) 6560 */ 6561 if (cap) { 6562 if (linecount > mMaximum) { 6563 desired = layout.getLineTop(mMaximum); 6564 6565 if (dr != null) { 6566 desired = Math.max(desired, dr.mDrawableHeightLeft); 6567 desired = Math.max(desired, dr.mDrawableHeightRight); 6568 } 6569 6570 desired += pad; 6571 linecount = mMaximum; 6572 } 6573 } 6574 } else { 6575 desired = Math.min(desired, mMaximum); 6576 } 6577 6578 if (mMinMode == LINES) { 6579 if (linecount < mMinimum) { 6580 desired += getLineHeight() * (mMinimum - linecount); 6581 } 6582 } else { 6583 desired = Math.max(desired, mMinimum); 6584 } 6585 6586 // Check against our minimum height 6587 desired = Math.max(desired, getSuggestedMinimumHeight()); 6588 6589 return desired; 6590 } 6591 6592 /** 6593 * Check whether a change to the existing text layout requires a 6594 * new view layout. 6595 */ 6596 private void checkForResize() { 6597 boolean sizeChanged = false; 6598 6599 if (mLayout != null) { 6600 // Check if our width changed 6601 if (mLayoutParams.width == LayoutParams.WRAP_CONTENT) { 6602 sizeChanged = true; 6603 invalidate(); 6604 } 6605 6606 // Check if our height changed 6607 if (mLayoutParams.height == LayoutParams.WRAP_CONTENT) { 6608 int desiredHeight = getDesiredHeight(); 6609 6610 if (desiredHeight != this.getHeight()) { 6611 sizeChanged = true; 6612 } 6613 } else if (mLayoutParams.height == LayoutParams.MATCH_PARENT) { 6614 if (mDesiredHeightAtMeasure >= 0) { 6615 int desiredHeight = getDesiredHeight(); 6616 6617 if (desiredHeight != mDesiredHeightAtMeasure) { 6618 sizeChanged = true; 6619 } 6620 } 6621 } 6622 } 6623 6624 if (sizeChanged) { 6625 requestLayout(); 6626 // caller will have already invalidated 6627 } 6628 } 6629 6630 /** 6631 * Check whether entirely new text requires a new view layout 6632 * or merely a new text layout. 6633 */ 6634 private void checkForRelayout() { 6635 // If we have a fixed width, we can just swap in a new text layout 6636 // if the text height stays the same or if the view height is fixed. 6637 6638 if ((mLayoutParams.width != LayoutParams.WRAP_CONTENT || 6639 (mMaxWidthMode == mMinWidthMode && mMaxWidth == mMinWidth)) && 6640 (mHint == null || mHintLayout != null) && 6641 (mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight() > 0)) { 6642 // Static width, so try making a new text layout. 6643 6644 int oldht = mLayout.getHeight(); 6645 int want = mLayout.getWidth(); 6646 int hintWant = mHintLayout == null ? 0 : mHintLayout.getWidth(); 6647 6648 /* 6649 * No need to bring the text into view, since the size is not 6650 * changing (unless we do the requestLayout(), in which case it 6651 * will happen at measure). 6652 */ 6653 makeNewLayout(want, hintWant, UNKNOWN_BORING, UNKNOWN_BORING, 6654 mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(), 6655 false); 6656 6657 if (mEllipsize != TextUtils.TruncateAt.MARQUEE) { 6658 // In a fixed-height view, so use our new text layout. 6659 if (mLayoutParams.height != LayoutParams.WRAP_CONTENT && 6660 mLayoutParams.height != LayoutParams.MATCH_PARENT) { 6661 invalidate(); 6662 return; 6663 } 6664 6665 // Dynamic height, but height has stayed the same, 6666 // so use our new text layout. 6667 if (mLayout.getHeight() == oldht && 6668 (mHintLayout == null || mHintLayout.getHeight() == oldht)) { 6669 invalidate(); 6670 return; 6671 } 6672 } 6673 6674 // We lose: the height has changed and we have a dynamic height. 6675 // Request a new view layout using our new text layout. 6676 requestLayout(); 6677 invalidate(); 6678 } else { 6679 // Dynamic width, so we have no choice but to request a new 6680 // view layout with a new text layout. 6681 nullLayouts(); 6682 requestLayout(); 6683 invalidate(); 6684 } 6685 } 6686 6687 /** 6688 * Returns true if anything changed. 6689 */ 6690 private boolean bringTextIntoView() { 6691 int line = 0; 6692 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { 6693 line = mLayout.getLineCount() - 1; 6694 } 6695 6696 Layout.Alignment a = mLayout.getParagraphAlignment(line); 6697 int dir = mLayout.getParagraphDirection(line); 6698 int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); 6699 int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom(); 6700 int ht = mLayout.getHeight(); 6701 6702 int scrollx, scrolly; 6703 6704 // Convert to left, center, or right alignment. 6705 if (a == Layout.Alignment.ALIGN_NORMAL) { 6706 a = dir == Layout.DIR_LEFT_TO_RIGHT ? Layout.Alignment.ALIGN_LEFT : 6707 Layout.Alignment.ALIGN_RIGHT; 6708 } else if (a == Layout.Alignment.ALIGN_OPPOSITE){ 6709 a = dir == Layout.DIR_LEFT_TO_RIGHT ? Layout.Alignment.ALIGN_RIGHT : 6710 Layout.Alignment.ALIGN_LEFT; 6711 } 6712 6713 if (a == Layout.Alignment.ALIGN_CENTER) { 6714 /* 6715 * Keep centered if possible, or, if it is too wide to fit, 6716 * keep leading edge in view. 6717 */ 6718 6719 int left = (int) FloatMath.floor(mLayout.getLineLeft(line)); 6720 int right = (int) FloatMath.ceil(mLayout.getLineRight(line)); 6721 6722 if (right - left < hspace) { 6723 scrollx = (right + left) / 2 - hspace / 2; 6724 } else { 6725 if (dir < 0) { 6726 scrollx = right - hspace; 6727 } else { 6728 scrollx = left; 6729 } 6730 } 6731 } else if (a == Layout.Alignment.ALIGN_RIGHT) { 6732 int right = (int) FloatMath.ceil(mLayout.getLineRight(line)); 6733 scrollx = right - hspace; 6734 } else { // a == Layout.Alignment.ALIGN_LEFT (will also be the default) 6735 scrollx = (int) FloatMath.floor(mLayout.getLineLeft(line)); 6736 } 6737 6738 if (ht < vspace) { 6739 scrolly = 0; 6740 } else { 6741 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { 6742 scrolly = ht - vspace; 6743 } else { 6744 scrolly = 0; 6745 } 6746 } 6747 6748 if (scrollx != mScrollX || scrolly != mScrollY) { 6749 scrollTo(scrollx, scrolly); 6750 return true; 6751 } else { 6752 return false; 6753 } 6754 } 6755 6756 /** 6757 * Move the point, specified by the offset, into the view if it is needed. 6758 * This has to be called after layout. Returns true if anything changed. 6759 */ 6760 public boolean bringPointIntoView(int offset) { 6761 boolean changed = false; 6762 6763 if (mLayout == null) return changed; 6764 6765 int line = mLayout.getLineForOffset(offset); 6766 6767 // FIXME: Is it okay to truncate this, or should we round? 6768 final int x = (int)mLayout.getPrimaryHorizontal(offset); 6769 final int top = mLayout.getLineTop(line); 6770 final int bottom = mLayout.getLineTop(line + 1); 6771 6772 int left = (int) FloatMath.floor(mLayout.getLineLeft(line)); 6773 int right = (int) FloatMath.ceil(mLayout.getLineRight(line)); 6774 int ht = mLayout.getHeight(); 6775 6776 int grav; 6777 6778 switch (mLayout.getParagraphAlignment(line)) { 6779 case ALIGN_LEFT: 6780 grav = 1; 6781 break; 6782 case ALIGN_RIGHT: 6783 grav = -1; 6784 break; 6785 case ALIGN_NORMAL: 6786 grav = mLayout.getParagraphDirection(line); 6787 break; 6788 case ALIGN_OPPOSITE: 6789 grav = -mLayout.getParagraphDirection(line); 6790 break; 6791 case ALIGN_CENTER: 6792 default: 6793 grav = 0; 6794 break; 6795 } 6796 6797 int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); 6798 int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom(); 6799 6800 int hslack = (bottom - top) / 2; 6801 int vslack = hslack; 6802 6803 if (vslack > vspace / 4) 6804 vslack = vspace / 4; 6805 if (hslack > hspace / 4) 6806 hslack = hspace / 4; 6807 6808 int hs = mScrollX; 6809 int vs = mScrollY; 6810 6811 if (top - vs < vslack) 6812 vs = top - vslack; 6813 if (bottom - vs > vspace - vslack) 6814 vs = bottom - (vspace - vslack); 6815 if (ht - vs < vspace) 6816 vs = ht - vspace; 6817 if (0 - vs > 0) 6818 vs = 0; 6819 6820 if (grav != 0) { 6821 if (x - hs < hslack) { 6822 hs = x - hslack; 6823 } 6824 if (x - hs > hspace - hslack) { 6825 hs = x - (hspace - hslack); 6826 } 6827 } 6828 6829 if (grav < 0) { 6830 if (left - hs > 0) 6831 hs = left; 6832 if (right - hs < hspace) 6833 hs = right - hspace; 6834 } else if (grav > 0) { 6835 if (right - hs < hspace) 6836 hs = right - hspace; 6837 if (left - hs > 0) 6838 hs = left; 6839 } else /* grav == 0 */ { 6840 if (right - left <= hspace) { 6841 /* 6842 * If the entire text fits, center it exactly. 6843 */ 6844 hs = left - (hspace - (right - left)) / 2; 6845 } else if (x > right - hslack) { 6846 /* 6847 * If we are near the right edge, keep the right edge 6848 * at the edge of the view. 6849 */ 6850 hs = right - hspace; 6851 } else if (x < left + hslack) { 6852 /* 6853 * If we are near the left edge, keep the left edge 6854 * at the edge of the view. 6855 */ 6856 hs = left; 6857 } else if (left > hs) { 6858 /* 6859 * Is there whitespace visible at the left? Fix it if so. 6860 */ 6861 hs = left; 6862 } else if (right < hs + hspace) { 6863 /* 6864 * Is there whitespace visible at the right? Fix it if so. 6865 */ 6866 hs = right - hspace; 6867 } else { 6868 /* 6869 * Otherwise, float as needed. 6870 */ 6871 if (x - hs < hslack) { 6872 hs = x - hslack; 6873 } 6874 if (x - hs > hspace - hslack) { 6875 hs = x - (hspace - hslack); 6876 } 6877 } 6878 } 6879 6880 if (hs != mScrollX || vs != mScrollY) { 6881 if (mScroller == null) { 6882 scrollTo(hs, vs); 6883 } else { 6884 long duration = AnimationUtils.currentAnimationTimeMillis() - mLastScroll; 6885 int dx = hs - mScrollX; 6886 int dy = vs - mScrollY; 6887 6888 if (duration > ANIMATED_SCROLL_GAP) { 6889 mScroller.startScroll(mScrollX, mScrollY, dx, dy); 6890 awakenScrollBars(mScroller.getDuration()); 6891 invalidate(); 6892 } else { 6893 if (!mScroller.isFinished()) { 6894 mScroller.abortAnimation(); 6895 } 6896 6897 scrollBy(dx, dy); 6898 } 6899 6900 mLastScroll = AnimationUtils.currentAnimationTimeMillis(); 6901 } 6902 6903 changed = true; 6904 } 6905 6906 if (isFocused()) { 6907 // This offsets because getInterestingRect() is in terms of viewport coordinates, but 6908 // requestRectangleOnScreen() is in terms of content coordinates. 6909 6910 if (mTempRect == null) mTempRect = new Rect(); 6911 // The offsets here are to ensure the rectangle we are using is 6912 // within our view bounds, in case the cursor is on the far left 6913 // or right. If it isn't withing the bounds, then this request 6914 // will be ignored. 6915 mTempRect.set(x - 2, top, x + 2, bottom); 6916 getInterestingRect(mTempRect, line); 6917 mTempRect.offset(mScrollX, mScrollY); 6918 6919 if (requestRectangleOnScreen(mTempRect)) { 6920 changed = true; 6921 } 6922 } 6923 6924 return changed; 6925 } 6926 6927 /** 6928 * Move the cursor, if needed, so that it is at an offset that is visible 6929 * to the user. This will not move the cursor if it represents more than 6930 * one character (a selection range). This will only work if the 6931 * TextView contains spannable text; otherwise it will do nothing. 6932 * 6933 * @return True if the cursor was actually moved, false otherwise. 6934 */ 6935 public boolean moveCursorToVisibleOffset() { 6936 if (!(mText instanceof Spannable)) { 6937 return false; 6938 } 6939 int start = getSelectionStart(); 6940 int end = getSelectionEnd(); 6941 if (start != end) { 6942 return false; 6943 } 6944 6945 // First: make sure the line is visible on screen: 6946 6947 int line = mLayout.getLineForOffset(start); 6948 6949 final int top = mLayout.getLineTop(line); 6950 final int bottom = mLayout.getLineTop(line + 1); 6951 final int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom(); 6952 int vslack = (bottom - top) / 2; 6953 if (vslack > vspace / 4) 6954 vslack = vspace / 4; 6955 final int vs = mScrollY; 6956 6957 if (top < (vs+vslack)) { 6958 line = mLayout.getLineForVertical(vs+vslack+(bottom-top)); 6959 } else if (bottom > (vspace+vs-vslack)) { 6960 line = mLayout.getLineForVertical(vspace+vs-vslack-(bottom-top)); 6961 } 6962 6963 // Next: make sure the character is visible on screen: 6964 6965 final int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); 6966 final int hs = mScrollX; 6967 final int leftChar = mLayout.getOffsetForHorizontal(line, hs); 6968 final int rightChar = mLayout.getOffsetForHorizontal(line, hspace+hs); 6969 6970 // line might contain bidirectional text 6971 final int lowChar = leftChar < rightChar ? leftChar : rightChar; 6972 final int highChar = leftChar > rightChar ? leftChar : rightChar; 6973 6974 int newStart = start; 6975 if (newStart < lowChar) { 6976 newStart = lowChar; 6977 } else if (newStart > highChar) { 6978 newStart = highChar; 6979 } 6980 6981 if (newStart != start) { 6982 Selection.setSelection((Spannable)mText, newStart); 6983 return true; 6984 } 6985 6986 return false; 6987 } 6988 6989 @Override 6990 public void computeScroll() { 6991 if (mScroller != null) { 6992 if (mScroller.computeScrollOffset()) { 6993 mScrollX = mScroller.getCurrX(); 6994 mScrollY = mScroller.getCurrY(); 6995 invalidateParentCaches(); 6996 postInvalidate(); // So we draw again 6997 } 6998 } 6999 } 7000 7001 private void getInterestingRect(Rect r, int line) { 7002 convertFromViewportToContentCoordinates(r); 7003 7004 // Rectangle can can be expanded on first and last line to take 7005 // padding into account. 7006 // TODO Take left/right padding into account too? 7007 if (line == 0) r.top -= getExtendedPaddingTop(); 7008 if (line == mLayout.getLineCount() - 1) r.bottom += getExtendedPaddingBottom(); 7009 } 7010 7011 private void convertFromViewportToContentCoordinates(Rect r) { 7012 final int horizontalOffset = viewportToContentHorizontalOffset(); 7013 r.left += horizontalOffset; 7014 r.right += horizontalOffset; 7015 7016 final int verticalOffset = viewportToContentVerticalOffset(); 7017 r.top += verticalOffset; 7018 r.bottom += verticalOffset; 7019 } 7020 7021 private int viewportToContentHorizontalOffset() { 7022 return getCompoundPaddingLeft() - mScrollX; 7023 } 7024 7025 private int viewportToContentVerticalOffset() { 7026 int offset = getExtendedPaddingTop() - mScrollY; 7027 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { 7028 offset += getVerticalOffset(false); 7029 } 7030 return offset; 7031 } 7032 7033 @Override 7034 public void debug(int depth) { 7035 super.debug(depth); 7036 7037 String output = debugIndent(depth); 7038 output += "frame={" + mLeft + ", " + mTop + ", " + mRight 7039 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY 7040 + "} "; 7041 7042 if (mText != null) { 7043 7044 output += "mText=\"" + mText + "\" "; 7045 if (mLayout != null) { 7046 output += "mLayout width=" + mLayout.getWidth() 7047 + " height=" + mLayout.getHeight(); 7048 } 7049 } else { 7050 output += "mText=NULL"; 7051 } 7052 Log.d(VIEW_LOG_TAG, output); 7053 } 7054 7055 /** 7056 * Convenience for {@link Selection#getSelectionStart}. 7057 */ 7058 @ViewDebug.ExportedProperty(category = "text") 7059 public int getSelectionStart() { 7060 return Selection.getSelectionStart(getText()); 7061 } 7062 7063 /** 7064 * Convenience for {@link Selection#getSelectionEnd}. 7065 */ 7066 @ViewDebug.ExportedProperty(category = "text") 7067 public int getSelectionEnd() { 7068 return Selection.getSelectionEnd(getText()); 7069 } 7070 7071 /** 7072 * Return true iff there is a selection inside this text view. 7073 */ 7074 public boolean hasSelection() { 7075 final int selectionStart = getSelectionStart(); 7076 final int selectionEnd = getSelectionEnd(); 7077 7078 return selectionStart >= 0 && selectionStart != selectionEnd; 7079 } 7080 7081 /** 7082 * Sets the properties of this field (lines, horizontally scrolling, 7083 * transformation method) to be for a single-line input. 7084 * 7085 * @attr ref android.R.styleable#TextView_singleLine 7086 */ 7087 public void setSingleLine() { 7088 setSingleLine(true); 7089 } 7090 7091 /** 7092 * Sets the properties of this field to transform input to ALL CAPS 7093 * display. This may use a "small caps" formatting if available. 7094 * This setting will be ignored if this field is editable or selectable. 7095 * 7096 * This call replaces the current transformation method. Disabling this 7097 * will not necessarily restore the previous behavior from before this 7098 * was enabled. 7099 * 7100 * @see #setTransformationMethod(TransformationMethod) 7101 * @attr ref android.R.styleable#TextView_textAllCaps 7102 */ 7103 public void setAllCaps(boolean allCaps) { 7104 if (allCaps) { 7105 setTransformationMethod(new AllCapsTransformationMethod(getContext())); 7106 } else { 7107 setTransformationMethod(null); 7108 } 7109 } 7110 7111 /** 7112 * If true, sets the properties of this field (number of lines, horizontally scrolling, 7113 * transformation method) to be for a single-line input; if false, restores these to the default 7114 * conditions. 7115 * 7116 * Note that the default conditions are not necessarily those that were in effect prior this 7117 * method, and you may want to reset these properties to your custom values. 7118 * 7119 * @attr ref android.R.styleable#TextView_singleLine 7120 */ 7121 @android.view.RemotableViewMethod 7122 public void setSingleLine(boolean singleLine) { 7123 // Could be used, but may break backward compatibility. 7124 // if (mSingleLine == singleLine) return; 7125 setInputTypeSingleLine(singleLine); 7126 applySingleLine(singleLine, true, true); 7127 } 7128 7129 /** 7130 * Adds or remove the EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE on the mInputType. 7131 * @param singleLine 7132 */ 7133 private void setInputTypeSingleLine(boolean singleLine) { 7134 if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { 7135 if (singleLine) { 7136 mInputType &= ~EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; 7137 } else { 7138 mInputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; 7139 } 7140 } 7141 } 7142 7143 private void applySingleLine(boolean singleLine, boolean applyTransformation, 7144 boolean changeMaxLines) { 7145 mSingleLine = singleLine; 7146 if (singleLine) { 7147 setLines(1); 7148 setHorizontallyScrolling(true); 7149 if (applyTransformation) { 7150 setTransformationMethod(SingleLineTransformationMethod.getInstance()); 7151 } 7152 } else { 7153 if (changeMaxLines) { 7154 setMaxLines(Integer.MAX_VALUE); 7155 } 7156 setHorizontallyScrolling(false); 7157 if (applyTransformation) { 7158 setTransformationMethod(null); 7159 } 7160 } 7161 } 7162 7163 /** 7164 * Causes words in the text that are longer than the view is wide 7165 * to be ellipsized instead of broken in the middle. You may also 7166 * want to {@link #setSingleLine} or {@link #setHorizontallyScrolling} 7167 * to constrain the text to a single line. Use <code>null</code> 7168 * to turn off ellipsizing. 7169 * 7170 * If {@link #setMaxLines} has been used to set two or more lines, 7171 * {@link android.text.TextUtils.TruncateAt#END} and 7172 * {@link android.text.TextUtils.TruncateAt#MARQUEE}* are only supported 7173 * (other ellipsizing types will not do anything). 7174 * 7175 * @attr ref android.R.styleable#TextView_ellipsize 7176 */ 7177 public void setEllipsize(TextUtils.TruncateAt where) { 7178 // TruncateAt is an enum. != comparison is ok between these singleton objects. 7179 if (mEllipsize != where) { 7180 mEllipsize = where; 7181 7182 if (mLayout != null) { 7183 nullLayouts(); 7184 requestLayout(); 7185 invalidate(); 7186 } 7187 } 7188 } 7189 7190 /** 7191 * Sets how many times to repeat the marquee animation. Only applied if the 7192 * TextView has marquee enabled. Set to -1 to repeat indefinitely. 7193 * 7194 * @attr ref android.R.styleable#TextView_marqueeRepeatLimit 7195 */ 7196 public void setMarqueeRepeatLimit(int marqueeLimit) { 7197 mMarqueeRepeatLimit = marqueeLimit; 7198 } 7199 7200 /** 7201 * Returns where, if anywhere, words that are longer than the view 7202 * is wide should be ellipsized. 7203 */ 7204 @ViewDebug.ExportedProperty 7205 public TextUtils.TruncateAt getEllipsize() { 7206 return mEllipsize; 7207 } 7208 7209 /** 7210 * Set the TextView so that when it takes focus, all the text is 7211 * selected. 7212 * 7213 * @attr ref android.R.styleable#TextView_selectAllOnFocus 7214 */ 7215 @android.view.RemotableViewMethod 7216 public void setSelectAllOnFocus(boolean selectAllOnFocus) { 7217 mSelectAllOnFocus = selectAllOnFocus; 7218 7219 if (selectAllOnFocus && !(mText instanceof Spannable)) { 7220 setText(mText, BufferType.SPANNABLE); 7221 } 7222 } 7223 7224 /** 7225 * Set whether the cursor is visible. The default is true. 7226 * 7227 * @attr ref android.R.styleable#TextView_cursorVisible 7228 */ 7229 @android.view.RemotableViewMethod 7230 public void setCursorVisible(boolean visible) { 7231 if (mCursorVisible != visible) { 7232 mCursorVisible = visible; 7233 invalidate(); 7234 7235 makeBlink(); 7236 7237 // InsertionPointCursorController depends on mCursorVisible 7238 prepareCursorControllers(); 7239 } 7240 } 7241 7242 private boolean isCursorVisible() { 7243 return mCursorVisible && isTextEditable(); 7244 } 7245 7246 private boolean canMarquee() { 7247 int width = (mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight()); 7248 return width > 0 && (mLayout.getLineWidth(0) > width || 7249 (mMarqueeFadeMode != MARQUEE_FADE_NORMAL && mSavedMarqueeModeLayout != null && 7250 mSavedMarqueeModeLayout.getLineWidth(0) > width)); 7251 } 7252 7253 private void startMarquee() { 7254 // Do not ellipsize EditText 7255 if (mInput != null) return; 7256 7257 if (compressText(getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight())) { 7258 return; 7259 } 7260 7261 if ((mMarquee == null || mMarquee.isStopped()) && (isFocused() || isSelected()) && 7262 getLineCount() == 1 && canMarquee()) { 7263 7264 if (mMarqueeFadeMode == MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { 7265 mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_FADE; 7266 final Layout tmp = mLayout; 7267 mLayout = mSavedMarqueeModeLayout; 7268 mSavedMarqueeModeLayout = tmp; 7269 setHorizontalFadingEdgeEnabled(true); 7270 requestLayout(); 7271 invalidate(); 7272 } 7273 7274 if (mMarquee == null) mMarquee = new Marquee(this); 7275 mMarquee.start(mMarqueeRepeatLimit); 7276 } 7277 } 7278 7279 private void stopMarquee() { 7280 if (mMarquee != null && !mMarquee.isStopped()) { 7281 mMarquee.stop(); 7282 } 7283 7284 if (mMarqueeFadeMode == MARQUEE_FADE_SWITCH_SHOW_FADE) { 7285 mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS; 7286 final Layout tmp = mSavedMarqueeModeLayout; 7287 mSavedMarqueeModeLayout = mLayout; 7288 mLayout = tmp; 7289 setHorizontalFadingEdgeEnabled(false); 7290 requestLayout(); 7291 invalidate(); 7292 } 7293 } 7294 7295 private void startStopMarquee(boolean start) { 7296 if (mEllipsize == TextUtils.TruncateAt.MARQUEE) { 7297 if (start) { 7298 startMarquee(); 7299 } else { 7300 stopMarquee(); 7301 } 7302 } 7303 } 7304 7305 private static final class Marquee extends Handler { 7306 // TODO: Add an option to configure this 7307 private static final float MARQUEE_DELTA_MAX = 0.07f; 7308 private static final int MARQUEE_DELAY = 1200; 7309 private static final int MARQUEE_RESTART_DELAY = 1200; 7310 private static final int MARQUEE_RESOLUTION = 1000 / 30; 7311 private static final int MARQUEE_PIXELS_PER_SECOND = 30; 7312 7313 private static final byte MARQUEE_STOPPED = 0x0; 7314 private static final byte MARQUEE_STARTING = 0x1; 7315 private static final byte MARQUEE_RUNNING = 0x2; 7316 7317 private static final int MESSAGE_START = 0x1; 7318 private static final int MESSAGE_TICK = 0x2; 7319 private static final int MESSAGE_RESTART = 0x3; 7320 7321 private final WeakReference<TextView> mView; 7322 7323 private byte mStatus = MARQUEE_STOPPED; 7324 private final float mScrollUnit; 7325 private float mMaxScroll; 7326 float mMaxFadeScroll; 7327 private float mGhostStart; 7328 private float mGhostOffset; 7329 private float mFadeStop; 7330 private int mRepeatLimit; 7331 7332 float mScroll; 7333 7334 Marquee(TextView v) { 7335 final float density = v.getContext().getResources().getDisplayMetrics().density; 7336 mScrollUnit = (MARQUEE_PIXELS_PER_SECOND * density) / MARQUEE_RESOLUTION; 7337 mView = new WeakReference<TextView>(v); 7338 } 7339 7340 @Override 7341 public void handleMessage(Message msg) { 7342 switch (msg.what) { 7343 case MESSAGE_START: 7344 mStatus = MARQUEE_RUNNING; 7345 tick(); 7346 break; 7347 case MESSAGE_TICK: 7348 tick(); 7349 break; 7350 case MESSAGE_RESTART: 7351 if (mStatus == MARQUEE_RUNNING) { 7352 if (mRepeatLimit >= 0) { 7353 mRepeatLimit--; 7354 } 7355 start(mRepeatLimit); 7356 } 7357 break; 7358 } 7359 } 7360 7361 void tick() { 7362 if (mStatus != MARQUEE_RUNNING) { 7363 return; 7364 } 7365 7366 removeMessages(MESSAGE_TICK); 7367 7368 final TextView textView = mView.get(); 7369 if (textView != null && (textView.isFocused() || textView.isSelected())) { 7370 mScroll += mScrollUnit; 7371 if (mScroll > mMaxScroll) { 7372 mScroll = mMaxScroll; 7373 sendEmptyMessageDelayed(MESSAGE_RESTART, MARQUEE_RESTART_DELAY); 7374 } else { 7375 sendEmptyMessageDelayed(MESSAGE_TICK, MARQUEE_RESOLUTION); 7376 } 7377 textView.invalidate(); 7378 } 7379 } 7380 7381 void stop() { 7382 mStatus = MARQUEE_STOPPED; 7383 removeMessages(MESSAGE_START); 7384 removeMessages(MESSAGE_RESTART); 7385 removeMessages(MESSAGE_TICK); 7386 resetScroll(); 7387 } 7388 7389 private void resetScroll() { 7390 mScroll = 0.0f; 7391 final TextView textView = mView.get(); 7392 if (textView != null) textView.invalidate(); 7393 } 7394 7395 void start(int repeatLimit) { 7396 if (repeatLimit == 0) { 7397 stop(); 7398 return; 7399 } 7400 mRepeatLimit = repeatLimit; 7401 final TextView textView = mView.get(); 7402 if (textView != null && textView.mLayout != null) { 7403 mStatus = MARQUEE_STARTING; 7404 mScroll = 0.0f; 7405 final int textWidth = textView.getWidth() - textView.getCompoundPaddingLeft() - 7406 textView.getCompoundPaddingRight(); 7407 final float lineWidth = textView.mLayout.getLineWidth(0); 7408 final float gap = textWidth / 3.0f; 7409 mGhostStart = lineWidth - textWidth + gap; 7410 mMaxScroll = mGhostStart + textWidth; 7411 mGhostOffset = lineWidth + gap; 7412 mFadeStop = lineWidth + textWidth / 6.0f; 7413 mMaxFadeScroll = mGhostStart + lineWidth + lineWidth; 7414 7415 textView.invalidate(); 7416 sendEmptyMessageDelayed(MESSAGE_START, MARQUEE_DELAY); 7417 } 7418 } 7419 7420 float getGhostOffset() { 7421 return mGhostOffset; 7422 } 7423 7424 boolean shouldDrawLeftFade() { 7425 return mScroll <= mFadeStop; 7426 } 7427 7428 boolean shouldDrawGhost() { 7429 return mStatus == MARQUEE_RUNNING && mScroll > mGhostStart; 7430 } 7431 7432 boolean isRunning() { 7433 return mStatus == MARQUEE_RUNNING; 7434 } 7435 7436 boolean isStopped() { 7437 return mStatus == MARQUEE_STOPPED; 7438 } 7439 } 7440 7441 /** 7442 * This method is called when the text is changed, in case any subclasses 7443 * would like to know. 7444 * 7445 * Within <code>text</code>, the <code>lengthAfter</code> characters 7446 * beginning at <code>start</code> have just replaced old text that had 7447 * length <code>lengthBefore</code>. It is an error to attempt to make 7448 * changes to <code>text</code> from this callback. 7449 * 7450 * @param text The text the TextView is displaying 7451 * @param start The offset of the start of the range of the text that was 7452 * modified 7453 * @param lengthBefore The length of the former text that has been replaced 7454 * @param lengthAfter The length of the replacement modified text 7455 */ 7456 protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { 7457 // intentionally empty, template pattern method can be overridden by subclasses 7458 } 7459 7460 /** 7461 * This method is called when the selection has changed, in case any 7462 * subclasses would like to know. 7463 * 7464 * @param selStart The new selection start location. 7465 * @param selEnd The new selection end location. 7466 */ 7467 protected void onSelectionChanged(int selStart, int selEnd) { 7468 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED); 7469 if (mSpellChecker != null) { 7470 mSpellChecker.onSelectionChanged(); 7471 } 7472 } 7473 7474 /** 7475 * Adds a TextWatcher to the list of those whose methods are called 7476 * whenever this TextView's text changes. 7477 * <p> 7478 * In 1.0, the {@link TextWatcher#afterTextChanged} method was erroneously 7479 * not called after {@link #setText} calls. Now, doing {@link #setText} 7480 * if there are any text changed listeners forces the buffer type to 7481 * Editable if it would not otherwise be and does call this method. 7482 */ 7483 public void addTextChangedListener(TextWatcher watcher) { 7484 if (mListeners == null) { 7485 mListeners = new ArrayList<TextWatcher>(); 7486 } 7487 7488 mListeners.add(watcher); 7489 } 7490 7491 /** 7492 * Removes the specified TextWatcher from the list of those whose 7493 * methods are called 7494 * whenever this TextView's text changes. 7495 */ 7496 public void removeTextChangedListener(TextWatcher watcher) { 7497 if (mListeners != null) { 7498 int i = mListeners.indexOf(watcher); 7499 7500 if (i >= 0) { 7501 mListeners.remove(i); 7502 } 7503 } 7504 } 7505 7506 private void sendBeforeTextChanged(CharSequence text, int start, int before, int after) { 7507 if (mListeners != null) { 7508 final ArrayList<TextWatcher> list = mListeners; 7509 final int count = list.size(); 7510 for (int i = 0; i < count; i++) { 7511 list.get(i).beforeTextChanged(text, start, before, after); 7512 } 7513 } 7514 7515 // The spans that are inside or intersect the modified region no longer make sense 7516 removeIntersectingSpans(start, start + before, SpellCheckSpan.class); 7517 removeIntersectingSpans(start, start + before, SuggestionSpan.class); 7518 } 7519 7520 // Removes all spans that are inside or actually overlap the start..end range 7521 private <T> void removeIntersectingSpans(int start, int end, Class<T> type) { 7522 if (!(mText instanceof Editable)) return; 7523 Editable text = (Editable) mText; 7524 7525 T[] spans = text.getSpans(start, end, type); 7526 final int length = spans.length; 7527 for (int i = 0; i < length; i++) { 7528 final int s = text.getSpanStart(spans[i]); 7529 final int e = text.getSpanEnd(spans[i]); 7530 if (e == start || s == end) break; 7531 text.removeSpan(spans[i]); 7532 } 7533 } 7534 7535 /** 7536 * Not private so it can be called from an inner class without going 7537 * through a thunk. 7538 */ 7539 void sendOnTextChanged(CharSequence text, int start, int before, int after) { 7540 if (mListeners != null) { 7541 final ArrayList<TextWatcher> list = mListeners; 7542 final int count = list.size(); 7543 for (int i = 0; i < count; i++) { 7544 list.get(i).onTextChanged(text, start, before, after); 7545 } 7546 } 7547 } 7548 7549 /** 7550 * Not private so it can be called from an inner class without going 7551 * through a thunk. 7552 */ 7553 void sendAfterTextChanged(Editable text) { 7554 if (mListeners != null) { 7555 final ArrayList<TextWatcher> list = mListeners; 7556 final int count = list.size(); 7557 for (int i = 0; i < count; i++) { 7558 list.get(i).afterTextChanged(text); 7559 } 7560 } 7561 } 7562 7563 /** 7564 * Not private so it can be called from an inner class without going 7565 * through a thunk. 7566 */ 7567 void handleTextChanged(CharSequence buffer, int start, int before, int after) { 7568 final InputMethodState ims = mInputMethodState; 7569 if (ims == null || ims.mBatchEditNesting == 0) { 7570 updateAfterEdit(); 7571 } 7572 if (ims != null) { 7573 ims.mContentChanged = true; 7574 if (ims.mChangedStart < 0) { 7575 ims.mChangedStart = start; 7576 ims.mChangedEnd = start+before; 7577 } else { 7578 ims.mChangedStart = Math.min(ims.mChangedStart, start); 7579 ims.mChangedEnd = Math.max(ims.mChangedEnd, start + before - ims.mChangedDelta); 7580 } 7581 ims.mChangedDelta += after-before; 7582 } 7583 7584 sendOnTextChanged(buffer, start, before, after); 7585 onTextChanged(buffer, start, before, after); 7586 7587 // The WordIterator text change listener may be called after this one. 7588 // Make sure this changed text is rescanned before the iterator is used on it. 7589 getWordIterator().forceUpdate(); 7590 updateSpellCheckSpans(start, start + after); 7591 7592 // Hide the controllers if the amount of content changed 7593 if (before != after) { 7594 // We do not hide the span controllers, as they can be added when a new text is 7595 // inserted into the text view 7596 hideCursorControllers(); 7597 } 7598 } 7599 7600 /** 7601 * Not private so it can be called from an inner class without going 7602 * through a thunk. 7603 */ 7604 void spanChange(Spanned buf, Object what, int oldStart, int newStart, int oldEnd, int newEnd) { 7605 // XXX Make the start and end move together if this ends up 7606 // spending too much time invalidating. 7607 7608 boolean selChanged = false; 7609 int newSelStart=-1, newSelEnd=-1; 7610 7611 final InputMethodState ims = mInputMethodState; 7612 7613 if (what == Selection.SELECTION_END) { 7614 mHighlightPathBogus = true; 7615 selChanged = true; 7616 newSelEnd = newStart; 7617 7618 if (!isFocused()) { 7619 mSelectionMoved = true; 7620 } 7621 7622 if (oldStart >= 0 || newStart >= 0) { 7623 invalidateCursor(Selection.getSelectionStart(buf), oldStart, newStart); 7624 registerForPreDraw(); 7625 makeBlink(); 7626 } 7627 } 7628 7629 if (what == Selection.SELECTION_START) { 7630 mHighlightPathBogus = true; 7631 selChanged = true; 7632 newSelStart = newStart; 7633 7634 if (!isFocused()) { 7635 mSelectionMoved = true; 7636 } 7637 7638 if (oldStart >= 0 || newStart >= 0) { 7639 int end = Selection.getSelectionEnd(buf); 7640 invalidateCursor(end, oldStart, newStart); 7641 } 7642 } 7643 7644 if (selChanged) { 7645 if ((buf.getSpanFlags(what)&Spanned.SPAN_INTERMEDIATE) == 0) { 7646 if (newSelStart < 0) { 7647 newSelStart = Selection.getSelectionStart(buf); 7648 } 7649 if (newSelEnd < 0) { 7650 newSelEnd = Selection.getSelectionEnd(buf); 7651 } 7652 onSelectionChanged(newSelStart, newSelEnd); 7653 } 7654 } 7655 7656 if (what instanceof UpdateAppearance || 7657 what instanceof ParagraphStyle) { 7658 if (ims == null || ims.mBatchEditNesting == 0) { 7659 invalidate(); 7660 mHighlightPathBogus = true; 7661 checkForResize(); 7662 } else { 7663 ims.mContentChanged = true; 7664 } 7665 } 7666 7667 if (MetaKeyKeyListener.isMetaTracker(buf, what)) { 7668 mHighlightPathBogus = true; 7669 if (ims != null && MetaKeyKeyListener.isSelectingMetaTracker(buf, what)) { 7670 ims.mSelectionModeChanged = true; 7671 } 7672 7673 if (Selection.getSelectionStart(buf) >= 0) { 7674 if (ims == null || ims.mBatchEditNesting == 0) { 7675 invalidateCursor(); 7676 } else { 7677 ims.mCursorChanged = true; 7678 } 7679 } 7680 } 7681 7682 if (what instanceof ParcelableSpan) { 7683 // If this is a span that can be sent to a remote process, 7684 // the current extract editor would be interested in it. 7685 if (ims != null && ims.mExtracting != null) { 7686 if (ims.mBatchEditNesting != 0) { 7687 if (oldStart >= 0) { 7688 if (ims.mChangedStart > oldStart) { 7689 ims.mChangedStart = oldStart; 7690 } 7691 if (ims.mChangedStart > oldEnd) { 7692 ims.mChangedStart = oldEnd; 7693 } 7694 } 7695 if (newStart >= 0) { 7696 if (ims.mChangedStart > newStart) { 7697 ims.mChangedStart = newStart; 7698 } 7699 if (ims.mChangedStart > newEnd) { 7700 ims.mChangedStart = newEnd; 7701 } 7702 } 7703 } else { 7704 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "Span change outside of batch: " 7705 + oldStart + "-" + oldEnd + "," 7706 + newStart + "-" + newEnd + what); 7707 ims.mContentChanged = true; 7708 } 7709 } 7710 } 7711 7712 if (what instanceof SpellCheckSpan) { 7713 if (newStart < 0) { 7714 getSpellChecker().removeSpellCheckSpan((SpellCheckSpan) what); 7715 } else if (oldStart < 0) { 7716 getSpellChecker().addSpellCheckSpan((SpellCheckSpan) what); 7717 } 7718 } 7719 } 7720 7721 /** 7722 * Create new SpellCheckSpans on the modified region. 7723 */ 7724 private void updateSpellCheckSpans(int start, int end) { 7725 if (!(mText instanceof Editable) || !isSuggestionsEnabled()) return; 7726 Editable text = (Editable) mText; 7727 7728 WordIterator wordIterator = getWordIterator(); 7729 wordIterator.setCharSequence(text); 7730 7731 // Move back to the beginning of the current word, if any 7732 int wordStart = wordIterator.preceding(start); 7733 int wordEnd; 7734 if (wordStart == BreakIterator.DONE) { 7735 wordEnd = wordIterator.following(start); 7736 if (wordEnd != BreakIterator.DONE) { 7737 wordStart = wordIterator.getBeginning(wordEnd); 7738 } 7739 } else { 7740 wordEnd = wordIterator.getEnd(wordStart); 7741 } 7742 if (wordEnd == BreakIterator.DONE) { 7743 return; 7744 } 7745 7746 // Iterate over the newly added text and schedule new SpellCheckSpans 7747 while (wordStart <= end) { 7748 if (wordEnd >= start) { 7749 // A word across the interval boundaries must remove boundary edition spans 7750 if (wordStart < start && wordEnd > start) { 7751 removeEditionSpansAt(start, text); 7752 } 7753 7754 if (wordStart < end && wordEnd > end) { 7755 removeEditionSpansAt(end, text); 7756 } 7757 7758 // Do not create new boundary spans if they already exist 7759 boolean createSpellCheckSpan = true; 7760 if (wordEnd == start) { 7761 SpellCheckSpan[] spellCheckSpans = text.getSpans(start, start, 7762 SpellCheckSpan.class); 7763 if (spellCheckSpans.length > 0) createSpellCheckSpan = false; 7764 } 7765 7766 if (wordStart == end) { 7767 SpellCheckSpan[] spellCheckSpans = text.getSpans(end, end, 7768 SpellCheckSpan.class); 7769 if (spellCheckSpans.length > 0) createSpellCheckSpan = false; 7770 } 7771 7772 if (createSpellCheckSpan) { 7773 text.setSpan(new SpellCheckSpan(), wordStart, wordEnd, 7774 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 7775 } 7776 } 7777 7778 // iterate word by word 7779 wordEnd = wordIterator.following(wordEnd); 7780 if (wordEnd == BreakIterator.DONE) return; 7781 wordStart = wordIterator.getBeginning(wordEnd); 7782 if (wordStart == BreakIterator.DONE) { 7783 Log.e(LOG_TAG, "Unable to find word beginning from " + wordEnd + "in " + mText); 7784 return; 7785 } 7786 } 7787 } 7788 7789 private static void removeEditionSpansAt(int offset, Editable text) { 7790 SuggestionSpan[] suggestionSpans = text.getSpans(offset, offset, SuggestionSpan.class); 7791 for (int i = 0; i < suggestionSpans.length; i++) { 7792 text.removeSpan(suggestionSpans[i]); 7793 } 7794 SpellCheckSpan[] spellCheckSpans = text.getSpans(offset, offset, SpellCheckSpan.class); 7795 for (int i = 0; i < spellCheckSpans.length; i++) { 7796 text.removeSpan(spellCheckSpans[i]); 7797 } 7798 } 7799 7800 /** 7801 * Controls the {@link EasyEditSpan} monitoring when it is added, and when the related 7802 * pop-up should be displayed. 7803 */ 7804 private class EasyEditSpanController { 7805 7806 private static final int DISPLAY_TIMEOUT_MS = 3000; // 3 secs 7807 7808 private EasyEditPopupWindow mPopupWindow; 7809 7810 private EasyEditSpan mEasyEditSpan; 7811 7812 private Runnable mHidePopup; 7813 7814 private void hide() { 7815 if (mPopupWindow != null) { 7816 mPopupWindow.hide(); 7817 TextView.this.removeCallbacks(mHidePopup); 7818 } 7819 removeSpans(mText); 7820 mEasyEditSpan = null; 7821 } 7822 7823 /** 7824 * Monitors the changes in the text. 7825 * 7826 * <p>{@link ChangeWatcher#onSpanAdded(Spannable, Object, int, int)} cannot be used, 7827 * as the notifications are not sent when a spannable (with spans) is inserted. 7828 */ 7829 public void onTextChange(CharSequence buffer) { 7830 adjustSpans(mText); 7831 7832 if (getWindowVisibility() != View.VISIBLE) { 7833 // The window is not visible yet, ignore the text change. 7834 return; 7835 } 7836 7837 if (mLayout == null) { 7838 // The view has not been layout yet, ignore the text change 7839 return; 7840 } 7841 7842 InputMethodManager imm = InputMethodManager.peekInstance(); 7843 if (!(TextView.this instanceof ExtractEditText) 7844 && imm != null && imm.isFullscreenMode()) { 7845 // The input is in extract mode. We do not have to handle the easy edit in the 7846 // original TextView, as the ExtractEditText will do 7847 return; 7848 } 7849 7850 // Remove the current easy edit span, as the text changed, and remove the pop-up 7851 // (if any) 7852 if (mEasyEditSpan != null) { 7853 if (mText instanceof Spannable) { 7854 ((Spannable) mText).removeSpan(mEasyEditSpan); 7855 } 7856 mEasyEditSpan = null; 7857 } 7858 if (mPopupWindow != null && mPopupWindow.isShowing()) { 7859 mPopupWindow.hide(); 7860 } 7861 7862 // Display the new easy edit span (if any). 7863 if (buffer instanceof Spanned) { 7864 mEasyEditSpan = getSpan((Spanned) buffer); 7865 if (mEasyEditSpan != null) { 7866 if (mPopupWindow == null) { 7867 mPopupWindow = new EasyEditPopupWindow(); 7868 mHidePopup = new Runnable() { 7869 @Override 7870 public void run() { 7871 hide(); 7872 } 7873 }; 7874 } 7875 mPopupWindow.show(mEasyEditSpan); 7876 TextView.this.removeCallbacks(mHidePopup); 7877 TextView.this.postDelayed(mHidePopup, DISPLAY_TIMEOUT_MS); 7878 } 7879 } 7880 } 7881 7882 /** 7883 * Adjusts the spans by removing all of them except the last one. 7884 */ 7885 private void adjustSpans(CharSequence buffer) { 7886 // This method enforces that only one easy edit span is attached to the text. 7887 // A better way to enforce this would be to listen for onSpanAdded, but this method 7888 // cannot be used in this scenario as no notification is triggered when a text with 7889 // spans is inserted into a text. 7890 if (buffer instanceof Spannable) { 7891 Spannable spannable = (Spannable) buffer; 7892 EasyEditSpan[] spans = spannable.getSpans(0, spannable.length(), 7893 EasyEditSpan.class); 7894 for (int i = 0; i < spans.length - 1; i++) { 7895 spannable.removeSpan(spans[i]); 7896 } 7897 } 7898 } 7899 7900 /** 7901 * Removes all the {@link EasyEditSpan} currently attached. 7902 */ 7903 private void removeSpans(CharSequence buffer) { 7904 if (buffer instanceof Spannable) { 7905 Spannable spannable = (Spannable) buffer; 7906 EasyEditSpan[] spans = spannable.getSpans(0, spannable.length(), 7907 EasyEditSpan.class); 7908 for (int i = 0; i < spans.length; i++) { 7909 spannable.removeSpan(spans[i]); 7910 } 7911 } 7912 } 7913 7914 private EasyEditSpan getSpan(Spanned spanned) { 7915 EasyEditSpan[] easyEditSpans = spanned.getSpans(0, spanned.length(), 7916 EasyEditSpan.class); 7917 if (easyEditSpans.length == 0) { 7918 return null; 7919 } else { 7920 return easyEditSpans[0]; 7921 } 7922 } 7923 } 7924 7925 /** 7926 * Displays the actions associated to an {@link EasyEditSpan}. The pop-up is controlled 7927 * by {@link EasyEditSpanController}. 7928 */ 7929 private class EasyEditPopupWindow extends PinnedPopupWindow 7930 implements OnClickListener { 7931 private static final int POPUP_TEXT_LAYOUT = 7932 com.android.internal.R.layout.text_edit_action_popup_text; 7933 private TextView mDeleteTextView; 7934 private EasyEditSpan mEasyEditSpan; 7935 7936 @Override 7937 protected void createPopupWindow() { 7938 mPopupWindow = new PopupWindow(TextView.this.mContext, null, 7939 com.android.internal.R.attr.textSelectHandleWindowStyle); 7940 mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); 7941 mPopupWindow.setClippingEnabled(true); 7942 } 7943 7944 @Override 7945 protected void initContentView() { 7946 LinearLayout linearLayout = new LinearLayout(TextView.this.getContext()); 7947 linearLayout.setOrientation(LinearLayout.HORIZONTAL); 7948 mContentView = linearLayout; 7949 mContentView.setBackgroundResource( 7950 com.android.internal.R.drawable.text_edit_side_paste_window); 7951 7952 LayoutInflater inflater = (LayoutInflater)TextView.this.mContext. 7953 getSystemService(Context.LAYOUT_INFLATER_SERVICE); 7954 7955 LayoutParams wrapContent = new LayoutParams( 7956 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 7957 7958 mDeleteTextView = (TextView) inflater.inflate(POPUP_TEXT_LAYOUT, null); 7959 mDeleteTextView.setLayoutParams(wrapContent); 7960 mDeleteTextView.setText(com.android.internal.R.string.delete); 7961 mDeleteTextView.setOnClickListener(this); 7962 mContentView.addView(mDeleteTextView); 7963 } 7964 7965 public void show(EasyEditSpan easyEditSpan) { 7966 mEasyEditSpan = easyEditSpan; 7967 super.show(); 7968 } 7969 7970 @Override 7971 public void onClick(View view) { 7972 if (view == mDeleteTextView) { 7973 deleteText(); 7974 } 7975 } 7976 7977 private void deleteText() { 7978 Editable editable = (Editable) mText; 7979 int start = editable.getSpanStart(mEasyEditSpan); 7980 int end = editable.getSpanEnd(mEasyEditSpan); 7981 if (start >= 0 && end >= 0) { 7982 editable.delete(start, end); 7983 } 7984 } 7985 7986 @Override 7987 protected int getTextOffset() { 7988 // Place the pop-up at the end of the span 7989 Editable editable = (Editable) mText; 7990 return editable.getSpanEnd(mEasyEditSpan); 7991 } 7992 7993 @Override 7994 protected int getVerticalLocalPosition(int line) { 7995 return mLayout.getLineBottom(line); 7996 } 7997 7998 @Override 7999 protected int clipVertically(int positionY) { 8000 // As we display the pop-up below the span, no vertical clipping is required. 8001 return positionY; 8002 } 8003 } 8004 8005 private class ChangeWatcher implements TextWatcher, SpanWatcher { 8006 8007 private CharSequence mBeforeText; 8008 8009 private EasyEditSpanController mEasyEditSpanController; 8010 8011 private ChangeWatcher() { 8012 mEasyEditSpanController = new EasyEditSpanController(); 8013 } 8014 8015 public void beforeTextChanged(CharSequence buffer, int start, 8016 int before, int after) { 8017 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "beforeTextChanged start=" + start 8018 + " before=" + before + " after=" + after + ": " + buffer); 8019 8020 if (AccessibilityManager.getInstance(mContext).isEnabled() 8021 && !isPasswordInputType(mInputType) 8022 && !hasPasswordTransformationMethod()) { 8023 mBeforeText = buffer.toString(); 8024 } 8025 8026 TextView.this.sendBeforeTextChanged(buffer, start, before, after); 8027 } 8028 8029 public void onTextChanged(CharSequence buffer, int start, 8030 int before, int after) { 8031 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onTextChanged start=" + start 8032 + " before=" + before + " after=" + after + ": " + buffer); 8033 TextView.this.handleTextChanged(buffer, start, before, after); 8034 8035 mEasyEditSpanController.onTextChange(buffer); 8036 8037 if (AccessibilityManager.getInstance(mContext).isEnabled() && 8038 (isFocused() || isSelected() && isShown())) { 8039 sendAccessibilityEventTypeViewTextChanged(mBeforeText, start, before, after); 8040 mBeforeText = null; 8041 } 8042 } 8043 8044 public void afterTextChanged(Editable buffer) { 8045 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "afterTextChanged: " + buffer); 8046 TextView.this.sendAfterTextChanged(buffer); 8047 8048 if (MetaKeyKeyListener.getMetaState(buffer, MetaKeyKeyListener.META_SELECTING) != 0) { 8049 MetaKeyKeyListener.stopSelecting(TextView.this, buffer); 8050 } 8051 } 8052 8053 public void onSpanChanged(Spannable buf, 8054 Object what, int s, int e, int st, int en) { 8055 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanChanged s=" + s + " e=" + e 8056 + " st=" + st + " en=" + en + " what=" + what + ": " + buf); 8057 TextView.this.spanChange(buf, what, s, st, e, en); 8058 } 8059 8060 public void onSpanAdded(Spannable buf, Object what, int s, int e) { 8061 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanAdded s=" + s + " e=" + e 8062 + " what=" + what + ": " + buf); 8063 TextView.this.spanChange(buf, what, -1, s, -1, e); 8064 } 8065 8066 public void onSpanRemoved(Spannable buf, Object what, int s, int e) { 8067 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanRemoved s=" + s + " e=" + e 8068 + " what=" + what + ": " + buf); 8069 TextView.this.spanChange(buf, what, s, -1, e, -1); 8070 } 8071 8072 private void hideControllers() { 8073 mEasyEditSpanController.hide(); 8074 } 8075 } 8076 8077 /** 8078 * @hide 8079 */ 8080 @Override 8081 public void dispatchFinishTemporaryDetach() { 8082 mDispatchTemporaryDetach = true; 8083 super.dispatchFinishTemporaryDetach(); 8084 mDispatchTemporaryDetach = false; 8085 } 8086 8087 @Override 8088 public void onStartTemporaryDetach() { 8089 super.onStartTemporaryDetach(); 8090 // Only track when onStartTemporaryDetach() is called directly, 8091 // usually because this instance is an editable field in a list 8092 if (!mDispatchTemporaryDetach) mTemporaryDetach = true; 8093 8094 // Because of View recycling in ListView, there is no easy way to know when a TextView with 8095 // selection becomes visible again. Until a better solution is found, stop text selection 8096 // mode (if any) as soon as this TextView is recycled. 8097 hideControllers(); 8098 } 8099 8100 @Override 8101 public void onFinishTemporaryDetach() { 8102 super.onFinishTemporaryDetach(); 8103 // Only track when onStartTemporaryDetach() is called directly, 8104 // usually because this instance is an editable field in a list 8105 if (!mDispatchTemporaryDetach) mTemporaryDetach = false; 8106 } 8107 8108 @Override 8109 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { 8110 if (mTemporaryDetach) { 8111 // If we are temporarily in the detach state, then do nothing. 8112 super.onFocusChanged(focused, direction, previouslyFocusedRect); 8113 return; 8114 } 8115 8116 mShowCursor = SystemClock.uptimeMillis(); 8117 8118 ensureEndedBatchEdit(); 8119 8120 if (focused) { 8121 int selStart = getSelectionStart(); 8122 int selEnd = getSelectionEnd(); 8123 8124 // SelectAllOnFocus fields are highlighted and not selected. Do not start text selection 8125 // mode for these, unless there was a specific selection already started. 8126 final boolean isFocusHighlighted = mSelectAllOnFocus && selStart == 0 && 8127 selEnd == mText.length(); 8128 mCreatedWithASelection = mFrozenWithFocus && hasSelection() && !isFocusHighlighted; 8129 8130 if (!mFrozenWithFocus || (selStart < 0 || selEnd < 0)) { 8131 // If a tap was used to give focus to that view, move cursor at tap position. 8132 // Has to be done before onTakeFocus, which can be overloaded. 8133 final int lastTapPosition = getLastTapPosition(); 8134 if (lastTapPosition >= 0) { 8135 Selection.setSelection((Spannable) mText, lastTapPosition); 8136 } 8137 8138 if (mMovement != null) { 8139 mMovement.onTakeFocus(this, (Spannable) mText, direction); 8140 } 8141 8142 // The DecorView does not have focus when the 'Done' ExtractEditText button is 8143 // pressed. Since it is the ViewAncestor's mView, it requests focus before 8144 // ExtractEditText clears focus, which gives focus to the ExtractEditText. 8145 // This special case ensure that we keep current selection in that case. 8146 // It would be better to know why the DecorView does not have focus at that time. 8147 if (((this instanceof ExtractEditText) || mSelectionMoved) && 8148 selStart >= 0 && selEnd >= 0) { 8149 /* 8150 * Someone intentionally set the selection, so let them 8151 * do whatever it is that they wanted to do instead of 8152 * the default on-focus behavior. We reset the selection 8153 * here instead of just skipping the onTakeFocus() call 8154 * because some movement methods do something other than 8155 * just setting the selection in theirs and we still 8156 * need to go through that path. 8157 */ 8158 Selection.setSelection((Spannable) mText, selStart, selEnd); 8159 } 8160 8161 if (mSelectAllOnFocus) { 8162 selectAll(); 8163 } 8164 8165 mTouchFocusSelected = true; 8166 } 8167 8168 mFrozenWithFocus = false; 8169 mSelectionMoved = false; 8170 8171 if (mText instanceof Spannable) { 8172 Spannable sp = (Spannable) mText; 8173 MetaKeyKeyListener.resetMetaState(sp); 8174 } 8175 8176 makeBlink(); 8177 8178 if (mError != null) { 8179 showError(); 8180 } 8181 } else { 8182 if (mError != null) { 8183 hideError(); 8184 } 8185 // Don't leave us in the middle of a batch edit. 8186 onEndBatchEdit(); 8187 8188 if (this instanceof ExtractEditText) { 8189 // terminateTextSelectionMode removes selection, which we want to keep when 8190 // ExtractEditText goes out of focus. 8191 final int selStart = getSelectionStart(); 8192 final int selEnd = getSelectionEnd(); 8193 hideControllers(); 8194 Selection.setSelection((Spannable) mText, selStart, selEnd); 8195 } else { 8196 hideControllers(); 8197 downgradeEasyCorrectionSpans(); 8198 } 8199 8200 // No need to create the controller 8201 if (mSelectionModifierCursorController != null) { 8202 mSelectionModifierCursorController.resetTouchOffsets(); 8203 } 8204 } 8205 8206 startStopMarquee(focused); 8207 8208 if (mTransformation != null) { 8209 mTransformation.onFocusChanged(this, mText, focused, direction, previouslyFocusedRect); 8210 } 8211 8212 super.onFocusChanged(focused, direction, previouslyFocusedRect); 8213 } 8214 8215 private int getLastTapPosition() { 8216 // No need to create the controller at that point, no last tap position saved 8217 if (mSelectionModifierCursorController != null) { 8218 int lastTapPosition = mSelectionModifierCursorController.getMinTouchOffset(); 8219 if (lastTapPosition >= 0) { 8220 // Safety check, should not be possible. 8221 if (lastTapPosition > mText.length()) { 8222 Log.e(LOG_TAG, "Invalid tap focus position (" + lastTapPosition + " vs " 8223 + mText.length() + ")"); 8224 lastTapPosition = mText.length(); 8225 } 8226 return lastTapPosition; 8227 } 8228 } 8229 8230 return -1; 8231 } 8232 8233 @Override 8234 public void onWindowFocusChanged(boolean hasWindowFocus) { 8235 super.onWindowFocusChanged(hasWindowFocus); 8236 8237 if (hasWindowFocus) { 8238 if (mBlink != null) { 8239 mBlink.uncancel(); 8240 makeBlink(); 8241 } 8242 } else { 8243 if (mBlink != null) { 8244 mBlink.cancel(); 8245 } 8246 // Don't leave us in the middle of a batch edit. 8247 onEndBatchEdit(); 8248 if (mInputContentType != null) { 8249 mInputContentType.enterDown = false; 8250 } 8251 8252 hideControllers(); 8253 } 8254 8255 startStopMarquee(hasWindowFocus); 8256 } 8257 8258 @Override 8259 protected void onVisibilityChanged(View changedView, int visibility) { 8260 super.onVisibilityChanged(changedView, visibility); 8261 if (visibility != VISIBLE) { 8262 hideControllers(); 8263 } 8264 } 8265 8266 /** 8267 * Use {@link BaseInputConnection#removeComposingSpans 8268 * BaseInputConnection.removeComposingSpans()} to remove any IME composing 8269 * state from this text view. 8270 */ 8271 public void clearComposingText() { 8272 if (mText instanceof Spannable) { 8273 BaseInputConnection.removeComposingSpans((Spannable)mText); 8274 } 8275 } 8276 8277 @Override 8278 public void setSelected(boolean selected) { 8279 boolean wasSelected = isSelected(); 8280 8281 super.setSelected(selected); 8282 8283 if (selected != wasSelected && mEllipsize == TextUtils.TruncateAt.MARQUEE) { 8284 if (selected) { 8285 startMarquee(); 8286 } else { 8287 stopMarquee(); 8288 } 8289 } 8290 } 8291 8292 @Override 8293 public boolean onTouchEvent(MotionEvent event) { 8294 final int action = event.getActionMasked(); 8295 8296 if (hasSelectionController()) { 8297 getSelectionController().onTouchEvent(event); 8298 } 8299 8300 if (action == MotionEvent.ACTION_DOWN) { 8301 mLastDownPositionX = event.getX(); 8302 mLastDownPositionY = event.getY(); 8303 8304 // Reset this state; it will be re-set if super.onTouchEvent 8305 // causes focus to move to the view. 8306 mTouchFocusSelected = false; 8307 mIgnoreActionUpEvent = false; 8308 } 8309 8310 final boolean superResult = super.onTouchEvent(event); 8311 8312 /* 8313 * Don't handle the release after a long press, because it will 8314 * move the selection away from whatever the menu action was 8315 * trying to affect. 8316 */ 8317 if (mDiscardNextActionUp && action == MotionEvent.ACTION_UP) { 8318 mDiscardNextActionUp = false; 8319 return superResult; 8320 } 8321 8322 final boolean touchIsFinished = (action == MotionEvent.ACTION_UP) && 8323 !shouldIgnoreActionUpEvent() && isFocused(); 8324 8325 if ((mMovement != null || onCheckIsTextEditor()) && isEnabled() 8326 && mText instanceof Spannable && mLayout != null) { 8327 boolean handled = false; 8328 8329 if (mMovement != null) { 8330 handled |= mMovement.onTouchEvent(this, (Spannable) mText, event); 8331 } 8332 8333 if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && mTextIsSelectable) { 8334 // The LinkMovementMethod which should handle taps on links has not been installed 8335 // on non editable text that support text selection. 8336 // We reproduce its behavior here to open links for these. 8337 ClickableSpan[] links = ((Spannable) mText).getSpans(getSelectionStart(), 8338 getSelectionEnd(), ClickableSpan.class); 8339 8340 if (links.length != 0) { 8341 links[0].onClick(this); 8342 handled = true; 8343 } 8344 } 8345 8346 if (touchIsFinished && (isTextEditable() || mTextIsSelectable)) { 8347 // Show the IME, except when selecting in read-only text. 8348 final InputMethodManager imm = InputMethodManager.peekInstance(); 8349 viewClicked(imm); 8350 if (!mTextIsSelectable) { 8351 handled |= imm != null && imm.showSoftInput(this, 0); 8352 } 8353 8354 boolean selectAllGotFocus = mSelectAllOnFocus && didTouchFocusSelect(); 8355 hideControllers(); 8356 if (!selectAllGotFocus && mText.length() > 0) { 8357 if (isCursorInsideEasyCorrectionSpan()) { 8358 showSuggestions(); 8359 } else if (hasInsertionController()) { 8360 getInsertionController().show(); 8361 } 8362 } 8363 8364 handled = true; 8365 } 8366 8367 if (handled) { 8368 return true; 8369 } 8370 } 8371 8372 return superResult; 8373 } 8374 8375 /** 8376 * @return <code>true</code> if the cursor/current selection overlaps a {@link SuggestionSpan}. 8377 */ 8378 private boolean isCursorInsideSuggestionSpan() { 8379 if (!(mText instanceof Spannable)) return false; 8380 8381 SuggestionSpan[] suggestionSpans = ((Spannable) mText).getSpans(getSelectionStart(), 8382 getSelectionEnd(), SuggestionSpan.class); 8383 return (suggestionSpans.length > 0); 8384 } 8385 8386 /** 8387 * @return <code>true</code> if the cursor is inside an {@link SuggestionSpan} with 8388 * {@link SuggestionSpan#FLAG_EASY_CORRECT} set. 8389 */ 8390 private boolean isCursorInsideEasyCorrectionSpan() { 8391 Spannable spannable = (Spannable) mText; 8392 SuggestionSpan[] suggestionSpans = spannable.getSpans(getSelectionStart(), 8393 getSelectionEnd(), SuggestionSpan.class); 8394 for (int i = 0; i < suggestionSpans.length; i++) { 8395 if ((suggestionSpans[i].getFlags() & SuggestionSpan.FLAG_EASY_CORRECT) != 0) { 8396 return true; 8397 } 8398 } 8399 return false; 8400 } 8401 8402 /** 8403 * Downgrades to simple suggestions all the easy correction spans that are not a spell check 8404 * span. 8405 */ 8406 private void downgradeEasyCorrectionSpans() { 8407 if (mText instanceof Spannable) { 8408 Spannable spannable = (Spannable) mText; 8409 SuggestionSpan[] suggestionSpans = spannable.getSpans(0, 8410 spannable.length(), SuggestionSpan.class); 8411 for (int i = 0; i < suggestionSpans.length; i++) { 8412 int flags = suggestionSpans[i].getFlags(); 8413 if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0 8414 && (flags & SuggestionSpan.FLAG_MISSPELLED) == 0) { 8415 flags = flags & ~SuggestionSpan.FLAG_EASY_CORRECT; 8416 suggestionSpans[i].setFlags(flags); 8417 } 8418 } 8419 } 8420 } 8421 8422 @Override 8423 public boolean onGenericMotionEvent(MotionEvent event) { 8424 if (mMovement != null && mText instanceof Spannable && mLayout != null) { 8425 try { 8426 if (mMovement.onGenericMotionEvent(this, (Spannable) mText, event)) { 8427 return true; 8428 } 8429 } catch (AbstractMethodError ex) { 8430 // onGenericMotionEvent was added to the MovementMethod interface in API 12. 8431 // Ignore its absence in case third party applications implemented the 8432 // interface directly. 8433 } 8434 } 8435 return super.onGenericMotionEvent(event); 8436 } 8437 8438 private void prepareCursorControllers() { 8439 boolean windowSupportsHandles = false; 8440 8441 ViewGroup.LayoutParams params = getRootView().getLayoutParams(); 8442 if (params instanceof WindowManager.LayoutParams) { 8443 WindowManager.LayoutParams windowParams = (WindowManager.LayoutParams) params; 8444 windowSupportsHandles = windowParams.type < WindowManager.LayoutParams.FIRST_SUB_WINDOW 8445 || windowParams.type > WindowManager.LayoutParams.LAST_SUB_WINDOW; 8446 } 8447 8448 mInsertionControllerEnabled = windowSupportsHandles && isCursorVisible() && mLayout != null; 8449 mSelectionControllerEnabled = windowSupportsHandles && textCanBeSelected() && 8450 mLayout != null; 8451 8452 if (!mInsertionControllerEnabled) { 8453 hideInsertionPointCursorController(); 8454 if (mInsertionPointCursorController != null) { 8455 mInsertionPointCursorController.onDetached(); 8456 mInsertionPointCursorController = null; 8457 } 8458 } 8459 8460 if (!mSelectionControllerEnabled) { 8461 stopSelectionActionMode(); 8462 if (mSelectionModifierCursorController != null) { 8463 mSelectionModifierCursorController.onDetached(); 8464 mSelectionModifierCursorController = null; 8465 } 8466 } 8467 } 8468 8469 /** 8470 * @return True iff this TextView contains a text that can be edited, or if this is 8471 * a selectable TextView. 8472 */ 8473 private boolean isTextEditable() { 8474 return mText instanceof Editable && onCheckIsTextEditor() && isEnabled(); 8475 } 8476 8477 /** 8478 * Returns true, only while processing a touch gesture, if the initial 8479 * touch down event caused focus to move to the text view and as a result 8480 * its selection changed. Only valid while processing the touch gesture 8481 * of interest. 8482 */ 8483 public boolean didTouchFocusSelect() { 8484 return mTouchFocusSelected; 8485 } 8486 8487 @Override 8488 public void cancelLongPress() { 8489 super.cancelLongPress(); 8490 mIgnoreActionUpEvent = true; 8491 } 8492 8493 /** 8494 * This method is only valid during a touch event. 8495 * 8496 * @return true when the ACTION_UP event should be ignored, false otherwise. 8497 * 8498 * @hide 8499 */ 8500 public boolean shouldIgnoreActionUpEvent() { 8501 return mIgnoreActionUpEvent; 8502 } 8503 8504 @Override 8505 public boolean onTrackballEvent(MotionEvent event) { 8506 if (mMovement != null && mText instanceof Spannable && 8507 mLayout != null) { 8508 if (mMovement.onTrackballEvent(this, (Spannable) mText, event)) { 8509 return true; 8510 } 8511 } 8512 8513 return super.onTrackballEvent(event); 8514 } 8515 8516 public void setScroller(Scroller s) { 8517 mScroller = s; 8518 } 8519 8520 private static class Blink extends Handler implements Runnable { 8521 private final WeakReference<TextView> mView; 8522 private boolean mCancelled; 8523 8524 public Blink(TextView v) { 8525 mView = new WeakReference<TextView>(v); 8526 } 8527 8528 public void run() { 8529 if (mCancelled) { 8530 return; 8531 } 8532 8533 removeCallbacks(Blink.this); 8534 8535 TextView tv = mView.get(); 8536 8537 if (tv != null && tv.shouldBlink()) { 8538 if (tv.mLayout != null) { 8539 tv.invalidateCursorPath(); 8540 } 8541 8542 postAtTime(this, SystemClock.uptimeMillis() + BLINK); 8543 } 8544 } 8545 8546 void cancel() { 8547 if (!mCancelled) { 8548 removeCallbacks(Blink.this); 8549 mCancelled = true; 8550 } 8551 } 8552 8553 void uncancel() { 8554 mCancelled = false; 8555 } 8556 } 8557 8558 /** 8559 * @return True when the TextView isFocused and has a valid zero-length selection (cursor). 8560 */ 8561 private boolean shouldBlink() { 8562 if (!isFocused()) return false; 8563 8564 final int start = getSelectionStart(); 8565 if (start < 0) return false; 8566 8567 final int end = getSelectionEnd(); 8568 if (end < 0) return false; 8569 8570 return start == end; 8571 } 8572 8573 private void makeBlink() { 8574 if (isCursorVisible()) { 8575 if (shouldBlink()) { 8576 mShowCursor = SystemClock.uptimeMillis(); 8577 if (mBlink == null) mBlink = new Blink(this); 8578 mBlink.removeCallbacks(mBlink); 8579 mBlink.postAtTime(mBlink, mShowCursor + BLINK); 8580 } 8581 } else { 8582 if (mBlink != null) mBlink.removeCallbacks(mBlink); 8583 } 8584 } 8585 8586 @Override 8587 protected float getLeftFadingEdgeStrength() { 8588 if (mCurrentAlpha <= ViewConfiguration.ALPHA_THRESHOLD_INT) return 0.0f; 8589 if (mEllipsize == TextUtils.TruncateAt.MARQUEE && 8590 mMarqueeFadeMode != MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { 8591 if (mMarquee != null && !mMarquee.isStopped()) { 8592 final Marquee marquee = mMarquee; 8593 if (marquee.shouldDrawLeftFade()) { 8594 return marquee.mScroll / getHorizontalFadingEdgeLength(); 8595 } else { 8596 return 0.0f; 8597 } 8598 } else if (getLineCount() == 1) { 8599 final int layoutDirection = getResolvedLayoutDirection(); 8600 final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection); 8601 switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) { 8602 case Gravity.LEFT: 8603 return 0.0f; 8604 case Gravity.RIGHT: 8605 return (mLayout.getLineRight(0) - (mRight - mLeft) - 8606 getCompoundPaddingLeft() - getCompoundPaddingRight() - 8607 mLayout.getLineLeft(0)) / getHorizontalFadingEdgeLength(); 8608 case Gravity.CENTER_HORIZONTAL: 8609 return 0.0f; 8610 } 8611 } 8612 } 8613 return super.getLeftFadingEdgeStrength(); 8614 } 8615 8616 @Override 8617 protected float getRightFadingEdgeStrength() { 8618 if (mCurrentAlpha <= ViewConfiguration.ALPHA_THRESHOLD_INT) return 0.0f; 8619 if (mEllipsize == TextUtils.TruncateAt.MARQUEE && 8620 mMarqueeFadeMode != MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { 8621 if (mMarquee != null && !mMarquee.isStopped()) { 8622 final Marquee marquee = mMarquee; 8623 return (marquee.mMaxFadeScroll - marquee.mScroll) / getHorizontalFadingEdgeLength(); 8624 } else if (getLineCount() == 1) { 8625 final int layoutDirection = getResolvedLayoutDirection(); 8626 final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection); 8627 switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) { 8628 case Gravity.LEFT: 8629 final int textWidth = (mRight - mLeft) - getCompoundPaddingLeft() - 8630 getCompoundPaddingRight(); 8631 final float lineWidth = mLayout.getLineWidth(0); 8632 return (lineWidth - textWidth) / getHorizontalFadingEdgeLength(); 8633 case Gravity.RIGHT: 8634 return 0.0f; 8635 case Gravity.CENTER_HORIZONTAL: 8636 case Gravity.FILL_HORIZONTAL: 8637 return (mLayout.getLineWidth(0) - ((mRight - mLeft) - 8638 getCompoundPaddingLeft() - getCompoundPaddingRight())) / 8639 getHorizontalFadingEdgeLength(); 8640 } 8641 } 8642 } 8643 return super.getRightFadingEdgeStrength(); 8644 } 8645 8646 @Override 8647 protected int computeHorizontalScrollRange() { 8648 if (mLayout != null) { 8649 return mSingleLine && (mGravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.LEFT ? 8650 (int) mLayout.getLineWidth(0) : mLayout.getWidth(); 8651 } 8652 8653 return super.computeHorizontalScrollRange(); 8654 } 8655 8656 @Override 8657 protected int computeVerticalScrollRange() { 8658 if (mLayout != null) 8659 return mLayout.getHeight(); 8660 8661 return super.computeVerticalScrollRange(); 8662 } 8663 8664 @Override 8665 protected int computeVerticalScrollExtent() { 8666 return getHeight() - getCompoundPaddingTop() - getCompoundPaddingBottom(); 8667 } 8668 8669 @Override 8670 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched) { 8671 if (TextUtils.isEmpty(searched)) { 8672 return; 8673 } 8674 CharSequence thisText = getText(); 8675 if (TextUtils.isEmpty(thisText)) { 8676 return; 8677 } 8678 String searchedLowerCase = searched.toString().toLowerCase(); 8679 String thisTextLowerCase = thisText.toString().toLowerCase(); 8680 if (thisTextLowerCase.contains(searchedLowerCase)) { 8681 outViews.add(this); 8682 } 8683 } 8684 8685 public enum BufferType { 8686 NORMAL, SPANNABLE, EDITABLE, 8687 } 8688 8689 /** 8690 * Returns the TextView_textColor attribute from the 8691 * Resources.StyledAttributes, if set, or the TextAppearance_textColor 8692 * from the TextView_textAppearance attribute, if TextView_textColor 8693 * was not set directly. 8694 */ 8695 public static ColorStateList getTextColors(Context context, TypedArray attrs) { 8696 ColorStateList colors; 8697 colors = attrs.getColorStateList(com.android.internal.R.styleable. 8698 TextView_textColor); 8699 8700 if (colors == null) { 8701 int ap = attrs.getResourceId(com.android.internal.R.styleable. 8702 TextView_textAppearance, -1); 8703 if (ap != -1) { 8704 TypedArray appearance; 8705 appearance = context.obtainStyledAttributes(ap, 8706 com.android.internal.R.styleable.TextAppearance); 8707 colors = appearance.getColorStateList(com.android.internal.R.styleable. 8708 TextAppearance_textColor); 8709 appearance.recycle(); 8710 } 8711 } 8712 8713 return colors; 8714 } 8715 8716 /** 8717 * Returns the default color from the TextView_textColor attribute 8718 * from the AttributeSet, if set, or the default color from the 8719 * TextAppearance_textColor from the TextView_textAppearance attribute, 8720 * if TextView_textColor was not set directly. 8721 */ 8722 public static int getTextColor(Context context, 8723 TypedArray attrs, 8724 int def) { 8725 ColorStateList colors = getTextColors(context, attrs); 8726 8727 if (colors == null) { 8728 return def; 8729 } else { 8730 return colors.getDefaultColor(); 8731 } 8732 } 8733 8734 @Override 8735 public boolean onKeyShortcut(int keyCode, KeyEvent event) { 8736 final int filteredMetaState = event.getMetaState() & ~KeyEvent.META_CTRL_MASK; 8737 if (KeyEvent.metaStateHasNoModifiers(filteredMetaState)) { 8738 switch (keyCode) { 8739 case KeyEvent.KEYCODE_A: 8740 if (canSelectText()) { 8741 return onTextContextMenuItem(ID_SELECT_ALL); 8742 } 8743 break; 8744 case KeyEvent.KEYCODE_X: 8745 if (canCut()) { 8746 return onTextContextMenuItem(ID_CUT); 8747 } 8748 break; 8749 case KeyEvent.KEYCODE_C: 8750 if (canCopy()) { 8751 return onTextContextMenuItem(ID_COPY); 8752 } 8753 break; 8754 case KeyEvent.KEYCODE_V: 8755 if (canPaste()) { 8756 return onTextContextMenuItem(ID_PASTE); 8757 } 8758 break; 8759 } 8760 } 8761 return super.onKeyShortcut(keyCode, event); 8762 } 8763 8764 /** 8765 * Unlike {@link #textCanBeSelected()}, this method is based on the <i>current</i> state of the 8766 * TextView. {@link #textCanBeSelected()} has to be true (this is one of the conditions to have 8767 * a selection controller (see {@link #prepareCursorControllers()}), but this is not sufficient. 8768 */ 8769 private boolean canSelectText() { 8770 return hasSelectionController() && mText.length() != 0; 8771 } 8772 8773 /** 8774 * Test based on the <i>intrinsic</i> charateristics of the TextView. 8775 * The text must be spannable and the movement method must allow for arbitary selection. 8776 * 8777 * See also {@link #canSelectText()}. 8778 */ 8779 private boolean textCanBeSelected() { 8780 // prepareCursorController() relies on this method. 8781 // If you change this condition, make sure prepareCursorController is called anywhere 8782 // the value of this condition might be changed. 8783 if (mMovement == null || !mMovement.canSelectArbitrarily()) return false; 8784 return isTextEditable() || (mTextIsSelectable && mText instanceof Spannable && isEnabled()); 8785 } 8786 8787 private boolean canCut() { 8788 if (hasPasswordTransformationMethod()) { 8789 return false; 8790 } 8791 8792 if (mText.length() > 0 && hasSelection() && mText instanceof Editable && mInput != null) { 8793 return true; 8794 } 8795 8796 return false; 8797 } 8798 8799 private boolean canCopy() { 8800 if (hasPasswordTransformationMethod()) { 8801 return false; 8802 } 8803 8804 if (mText.length() > 0 && hasSelection()) { 8805 return true; 8806 } 8807 8808 return false; 8809 } 8810 8811 private boolean canPaste() { 8812 return (mText instanceof Editable && 8813 mInput != null && 8814 getSelectionStart() >= 0 && 8815 getSelectionEnd() >= 0 && 8816 ((ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE)). 8817 hasPrimaryClip()); 8818 } 8819 8820 private static long packRangeInLong(int start, int end) { 8821 return (((long) start) << 32) | end; 8822 } 8823 8824 private static int extractRangeStartFromLong(long range) { 8825 return (int) (range >>> 32); 8826 } 8827 8828 private static int extractRangeEndFromLong(long range) { 8829 return (int) (range & 0x00000000FFFFFFFFL); 8830 } 8831 8832 private boolean selectAll() { 8833 final int length = mText.length(); 8834 Selection.setSelection((Spannable) mText, 0, length); 8835 return length > 0; 8836 } 8837 8838 /** 8839 * Adjusts selection to the word under last touch offset. 8840 * Return true if the operation was successfully performed. 8841 */ 8842 private boolean selectCurrentWord() { 8843 if (!canSelectText()) { 8844 return false; 8845 } 8846 8847 if (hasPasswordTransformationMethod()) { 8848 // Always select all on a password field. 8849 // Cut/copy menu entries are not available for passwords, but being able to select all 8850 // is however useful to delete or paste to replace the entire content. 8851 return selectAll(); 8852 } 8853 8854 int klass = mInputType & InputType.TYPE_MASK_CLASS; 8855 int variation = mInputType & InputType.TYPE_MASK_VARIATION; 8856 8857 // Specific text field types: select the entire text for these 8858 if (klass == InputType.TYPE_CLASS_NUMBER || 8859 klass == InputType.TYPE_CLASS_PHONE || 8860 klass == InputType.TYPE_CLASS_DATETIME || 8861 variation == InputType.TYPE_TEXT_VARIATION_URI || 8862 variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS || 8863 variation == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS || 8864 variation == InputType.TYPE_TEXT_VARIATION_FILTER) { 8865 return selectAll(); 8866 } 8867 8868 long lastTouchOffsets = getLastTouchOffsets(); 8869 final int minOffset = extractRangeStartFromLong(lastTouchOffsets); 8870 final int maxOffset = extractRangeEndFromLong(lastTouchOffsets); 8871 8872 // Safety check in case standard touch event handling has been bypassed 8873 if (minOffset < 0 || minOffset >= mText.length()) return false; 8874 if (maxOffset < 0 || maxOffset >= mText.length()) return false; 8875 8876 int selectionStart, selectionEnd; 8877 8878 // If a URLSpan (web address, email, phone...) is found at that position, select it. 8879 URLSpan[] urlSpans = ((Spanned) mText).getSpans(minOffset, maxOffset, URLSpan.class); 8880 if (urlSpans.length == 1) { 8881 URLSpan url = urlSpans[0]; 8882 selectionStart = ((Spanned) mText).getSpanStart(url); 8883 selectionEnd = ((Spanned) mText).getSpanEnd(url); 8884 } else { 8885 WordIterator wordIterator = getWordIterator(); 8886 // WordIterator handles text changes, this is a no-op if text in unchanged. 8887 wordIterator.setCharSequence(mText); 8888 8889 selectionStart = wordIterator.getBeginning(minOffset); 8890 if (selectionStart == BreakIterator.DONE) return false; 8891 8892 selectionEnd = wordIterator.getEnd(maxOffset); 8893 if (selectionEnd == BreakIterator.DONE) return false; 8894 } 8895 8896 Selection.setSelection((Spannable) mText, selectionStart, selectionEnd); 8897 return true; 8898 } 8899 8900 WordIterator getWordIterator() { 8901 if (mWordIterator == null) { 8902 mWordIterator = new WordIterator(); 8903 } 8904 return mWordIterator; 8905 } 8906 8907 private SpellChecker getSpellChecker() { 8908 if (mSpellChecker == null) { 8909 mSpellChecker = new SpellChecker(this); 8910 } 8911 return mSpellChecker; 8912 } 8913 8914 private long getLastTouchOffsets() { 8915 int minOffset, maxOffset; 8916 8917 if (mContextMenuTriggeredByKey) { 8918 minOffset = getSelectionStart(); 8919 maxOffset = getSelectionEnd(); 8920 } else { 8921 SelectionModifierCursorController selectionController = getSelectionController(); 8922 minOffset = selectionController.getMinTouchOffset(); 8923 maxOffset = selectionController.getMaxTouchOffset(); 8924 } 8925 8926 return packRangeInLong(minOffset, maxOffset); 8927 } 8928 8929 @Override 8930 public void onPopulateAccessibilityEvent(AccessibilityEvent event) { 8931 super.onPopulateAccessibilityEvent(event); 8932 8933 final boolean isPassword = hasPasswordTransformationMethod(); 8934 if (!isPassword) { 8935 CharSequence text = getText(); 8936 if (TextUtils.isEmpty(text)) { 8937 text = getHint(); 8938 } 8939 if (TextUtils.isEmpty(text)) { 8940 text = getContentDescription(); 8941 } 8942 if (!TextUtils.isEmpty(text)) { 8943 event.getText().add(text); 8944 } 8945 } 8946 } 8947 8948 @Override 8949 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 8950 super.onInitializeAccessibilityEvent(event); 8951 8952 final boolean isPassword = hasPasswordTransformationMethod(); 8953 event.setPassword(isPassword); 8954 8955 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED) { 8956 event.setFromIndex(Selection.getSelectionStart(mText)); 8957 event.setToIndex(Selection.getSelectionEnd(mText)); 8958 event.setItemCount(mText.length()); 8959 } 8960 } 8961 8962 @Override 8963 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 8964 super.onInitializeAccessibilityNodeInfo(info); 8965 8966 final boolean isPassword = hasPasswordTransformationMethod(); 8967 if (!isPassword) { 8968 info.setText(getText()); 8969 } 8970 info.setPassword(isPassword); 8971 } 8972 8973 @Override 8974 public void sendAccessibilityEvent(int eventType) { 8975 // Do not send scroll events since first they are not interesting for 8976 // accessibility and second such events a generated too frequently. 8977 // For details see the implementation of bringTextIntoView(). 8978 if (eventType == AccessibilityEvent.TYPE_VIEW_SCROLLED) { 8979 return; 8980 } 8981 super.sendAccessibilityEvent(eventType); 8982 } 8983 8984 void sendAccessibilityEventTypeViewTextChanged(CharSequence beforeText, 8985 int fromIndex, int removedCount, int addedCount) { 8986 AccessibilityEvent event = 8987 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED); 8988 event.setFromIndex(fromIndex); 8989 event.setRemovedCount(removedCount); 8990 event.setAddedCount(addedCount); 8991 event.setBeforeText(beforeText); 8992 sendAccessibilityEventUnchecked(event); 8993 } 8994 8995 @Override 8996 protected void onCreateContextMenu(ContextMenu menu) { 8997 super.onCreateContextMenu(menu); 8998 boolean added = false; 8999 mContextMenuTriggeredByKey = mDPadCenterIsDown || mEnterKeyIsDown; 9000 // Problem with context menu on long press: the menu appears while the key in down and when 9001 // the key is released, the view does not receive the key_up event. 9002 // We need two layers of flags: mDPadCenterIsDown and mEnterKeyIsDown are set in key down/up 9003 // events. We cannot simply clear these flags in onTextContextMenuItem since 9004 // it may not be called (if the user/ discards the context menu with the back key). 9005 // We clear these flags here and mContextMenuTriggeredByKey saves that state so that it is 9006 // available in onTextContextMenuItem. 9007 mDPadCenterIsDown = mEnterKeyIsDown = false; 9008 9009 MenuHandler handler = new MenuHandler(); 9010 9011 if (mText instanceof Spanned && hasSelectionController()) { 9012 long lastTouchOffset = getLastTouchOffsets(); 9013 final int selStart = extractRangeStartFromLong(lastTouchOffset); 9014 final int selEnd = extractRangeEndFromLong(lastTouchOffset); 9015 9016 URLSpan[] urls = ((Spanned) mText).getSpans(selStart, selEnd, URLSpan.class); 9017 if (urls.length > 0) { 9018 menu.add(0, ID_COPY_URL, 0, com.android.internal.R.string.copyUrl). 9019 setOnMenuItemClickListener(handler); 9020 9021 added = true; 9022 } 9023 } 9024 9025 // The context menu is not empty, which will prevent the selection mode from starting. 9026 // Add a entry to start it in the context menu. 9027 // TODO Does not handle the case where a subclass does not call super.thisMethod or 9028 // populates the menu AFTER this call. 9029 if (menu.size() > 0) { 9030 menu.add(0, ID_SELECTION_MODE, 0, com.android.internal.R.string.selectTextMode). 9031 setOnMenuItemClickListener(handler); 9032 added = true; 9033 } 9034 9035 if (added) { 9036 menu.setHeaderTitle(com.android.internal.R.string.editTextMenuTitle); 9037 } 9038 } 9039 9040 /** 9041 * Returns whether this text view is a current input method target. The 9042 * default implementation just checks with {@link InputMethodManager}. 9043 */ 9044 public boolean isInputMethodTarget() { 9045 InputMethodManager imm = InputMethodManager.peekInstance(); 9046 return imm != null && imm.isActive(this); 9047 } 9048 9049 // Selection context mode 9050 private static final int ID_SELECT_ALL = android.R.id.selectAll; 9051 private static final int ID_CUT = android.R.id.cut; 9052 private static final int ID_COPY = android.R.id.copy; 9053 private static final int ID_PASTE = android.R.id.paste; 9054 // Context menu entries 9055 private static final int ID_COPY_URL = android.R.id.copyUrl; 9056 private static final int ID_SELECTION_MODE = android.R.id.selectTextMode; 9057 9058 private class MenuHandler implements MenuItem.OnMenuItemClickListener { 9059 public boolean onMenuItemClick(MenuItem item) { 9060 return onTextContextMenuItem(item.getItemId()); 9061 } 9062 } 9063 9064 /** 9065 * Called when a context menu option for the text view is selected. Currently 9066 * this will be {@link android.R.id#copyUrl}, {@link android.R.id#selectTextMode}, 9067 * {@link android.R.id#selectAll}, {@link android.R.id#paste}, {@link android.R.id#cut} 9068 * or {@link android.R.id#copy}. 9069 * 9070 * @return true if the context menu item action was performed. 9071 */ 9072 public boolean onTextContextMenuItem(int id) { 9073 int min = 0; 9074 int max = mText.length(); 9075 9076 if (isFocused()) { 9077 final int selStart = getSelectionStart(); 9078 final int selEnd = getSelectionEnd(); 9079 9080 min = Math.max(0, Math.min(selStart, selEnd)); 9081 max = Math.max(0, Math.max(selStart, selEnd)); 9082 } 9083 9084 switch (id) { 9085 case ID_COPY_URL: 9086 URLSpan[] urls = ((Spanned) mText).getSpans(min, max, URLSpan.class); 9087 if (urls.length >= 1) { 9088 ClipData clip = null; 9089 for (int i=0; i<urls.length; i++) { 9090 Uri uri = Uri.parse(urls[0].getURL()); 9091 if (clip == null) { 9092 clip = ClipData.newRawUri(null, uri); 9093 } else { 9094 clip.addItem(new ClipData.Item(uri)); 9095 } 9096 } 9097 if (clip != null) { 9098 setPrimaryClip(clip); 9099 } 9100 } 9101 stopSelectionActionMode(); 9102 return true; 9103 9104 case ID_SELECTION_MODE: 9105 if (mSelectionActionMode != null) { 9106 // Selection mode is already started, simply change selected part. 9107 selectCurrentWord(); 9108 } else { 9109 startSelectionActionMode(); 9110 } 9111 return true; 9112 9113 case ID_SELECT_ALL: 9114 // This does not enter text selection mode. Text is highlighted, so that it can be 9115 // bulk edited, like selectAllOnFocus does. Returns true even if text is empty. 9116 selectAll(); 9117 return true; 9118 9119 case ID_PASTE: 9120 paste(min, max); 9121 return true; 9122 9123 case ID_CUT: 9124 setPrimaryClip(ClipData.newPlainText(null, mTransformed.subSequence(min, max))); 9125 ((Editable) mText).delete(min, max); 9126 stopSelectionActionMode(); 9127 return true; 9128 9129 case ID_COPY: 9130 setPrimaryClip(ClipData.newPlainText(null, mTransformed.subSequence(min, max))); 9131 stopSelectionActionMode(); 9132 return true; 9133 } 9134 return false; 9135 } 9136 9137 /** 9138 * Prepare text so that there are not zero or two spaces at beginning and end of region defined 9139 * by [min, max] when replacing this region by paste. 9140 * Note that if there were two spaces (or more) at that position before, they are kept. We just 9141 * make sure we do not add an extra one from the paste content. 9142 */ 9143 private long prepareSpacesAroundPaste(int min, int max, CharSequence paste) { 9144 if (paste.length() > 0) { 9145 if (min > 0) { 9146 final char charBefore = mTransformed.charAt(min - 1); 9147 final char charAfter = paste.charAt(0); 9148 9149 if (Character.isSpaceChar(charBefore) && Character.isSpaceChar(charAfter)) { 9150 // Two spaces at beginning of paste: remove one 9151 final int originalLength = mText.length(); 9152 ((Editable) mText).delete(min - 1, min); 9153 // Due to filters, there is no guarantee that exactly one character was 9154 // removed: count instead. 9155 final int delta = mText.length() - originalLength; 9156 min += delta; 9157 max += delta; 9158 } else if (!Character.isSpaceChar(charBefore) && charBefore != '\n' && 9159 !Character.isSpaceChar(charAfter) && charAfter != '\n') { 9160 // No space at beginning of paste: add one 9161 final int originalLength = mText.length(); 9162 ((Editable) mText).replace(min, min, " "); 9163 // Taking possible filters into account as above. 9164 final int delta = mText.length() - originalLength; 9165 min += delta; 9166 max += delta; 9167 } 9168 } 9169 9170 if (max < mText.length()) { 9171 final char charBefore = paste.charAt(paste.length() - 1); 9172 final char charAfter = mTransformed.charAt(max); 9173 9174 if (Character.isSpaceChar(charBefore) && Character.isSpaceChar(charAfter)) { 9175 // Two spaces at end of paste: remove one 9176 ((Editable) mText).delete(max, max + 1); 9177 } else if (!Character.isSpaceChar(charBefore) && charBefore != '\n' && 9178 !Character.isSpaceChar(charAfter) && charAfter != '\n') { 9179 // No space at end of paste: add one 9180 ((Editable) mText).replace(max, max, " "); 9181 } 9182 } 9183 } 9184 9185 return packRangeInLong(min, max); 9186 } 9187 9188 private DragShadowBuilder getTextThumbnailBuilder(CharSequence text) { 9189 TextView shadowView = (TextView) inflate(mContext, 9190 com.android.internal.R.layout.text_drag_thumbnail, null); 9191 9192 if (shadowView == null) { 9193 throw new IllegalArgumentException("Unable to inflate text drag thumbnail"); 9194 } 9195 9196 if (text.length() > DRAG_SHADOW_MAX_TEXT_LENGTH) { 9197 text = text.subSequence(0, DRAG_SHADOW_MAX_TEXT_LENGTH); 9198 } 9199 shadowView.setText(text); 9200 shadowView.setTextColor(getTextColors()); 9201 9202 shadowView.setTextAppearance(mContext, R.styleable.Theme_textAppearanceLarge); 9203 shadowView.setGravity(Gravity.CENTER); 9204 9205 shadowView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 9206 ViewGroup.LayoutParams.WRAP_CONTENT)); 9207 9208 final int size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 9209 shadowView.measure(size, size); 9210 9211 shadowView.layout(0, 0, shadowView.getMeasuredWidth(), shadowView.getMeasuredHeight()); 9212 shadowView.invalidate(); 9213 return new DragShadowBuilder(shadowView); 9214 } 9215 9216 private static class DragLocalState { 9217 public TextView sourceTextView; 9218 public int start, end; 9219 9220 public DragLocalState(TextView sourceTextView, int start, int end) { 9221 this.sourceTextView = sourceTextView; 9222 this.start = start; 9223 this.end = end; 9224 } 9225 } 9226 9227 @Override 9228 public boolean performLongClick() { 9229 if (super.performLongClick()) { 9230 mDiscardNextActionUp = true; 9231 return true; 9232 } 9233 9234 boolean handled = false; 9235 9236 // Long press in empty space moves cursor and shows the Paste affordance if available. 9237 if (!isPositionOnText(mLastDownPositionX, mLastDownPositionY) && 9238 mInsertionControllerEnabled) { 9239 final int offset = getOffsetForPosition(mLastDownPositionX, mLastDownPositionY); 9240 stopSelectionActionMode(); 9241 Selection.setSelection((Spannable) mText, offset); 9242 getInsertionController().showWithActionPopup(); 9243 handled = true; 9244 } 9245 9246 if (!handled && mSelectionActionMode != null) { 9247 if (touchPositionIsInSelection()) { 9248 // Start a drag 9249 final int start = getSelectionStart(); 9250 final int end = getSelectionEnd(); 9251 CharSequence selectedText = mTransformed.subSequence(start, end); 9252 ClipData data = ClipData.newPlainText(null, selectedText); 9253 DragLocalState localState = new DragLocalState(this, start, end); 9254 startDrag(data, getTextThumbnailBuilder(selectedText), localState, 0); 9255 stopSelectionActionMode(); 9256 } else { 9257 getSelectionController().hide(); 9258 selectCurrentWord(); 9259 getSelectionController().show(); 9260 } 9261 handled = true; 9262 } 9263 9264 // Start a new selection 9265 handled |= !handled && startSelectionActionMode(); 9266 9267 if (handled) { 9268 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); 9269 mDiscardNextActionUp = true; 9270 } 9271 9272 return handled; 9273 } 9274 9275 private boolean touchPositionIsInSelection() { 9276 int selectionStart = getSelectionStart(); 9277 int selectionEnd = getSelectionEnd(); 9278 9279 if (selectionStart == selectionEnd) { 9280 return false; 9281 } 9282 9283 if (selectionStart > selectionEnd) { 9284 int tmp = selectionStart; 9285 selectionStart = selectionEnd; 9286 selectionEnd = tmp; 9287 Selection.setSelection((Spannable) mText, selectionStart, selectionEnd); 9288 } 9289 9290 SelectionModifierCursorController selectionController = getSelectionController(); 9291 int minOffset = selectionController.getMinTouchOffset(); 9292 int maxOffset = selectionController.getMaxTouchOffset(); 9293 9294 return ((minOffset >= selectionStart) && (maxOffset < selectionEnd)); 9295 } 9296 9297 private PositionListener getPositionListener() { 9298 if (mPositionListener == null) { 9299 mPositionListener = new PositionListener(); 9300 } 9301 return mPositionListener; 9302 } 9303 9304 private interface TextViewPositionListener { 9305 public void updatePosition(int parentPositionX, int parentPositionY, 9306 boolean parentPositionChanged, boolean parentScrolled); 9307 } 9308 9309 private class PositionListener implements ViewTreeObserver.OnPreDrawListener { 9310 // 3 handles 9311 // 3 ActionPopup [replace, suggestion, easyedit] (suggestionsPopup first hides the others) 9312 private final int MAXIMUM_NUMBER_OF_LISTENERS = 6; 9313 private TextViewPositionListener[] mPositionListeners = 9314 new TextViewPositionListener[MAXIMUM_NUMBER_OF_LISTENERS]; 9315 private boolean mCanMove[] = new boolean[MAXIMUM_NUMBER_OF_LISTENERS]; 9316 private boolean mPositionHasChanged = true; 9317 // Absolute position of the TextView with respect to its parent window 9318 private int mPositionX, mPositionY; 9319 private int mNumberOfListeners; 9320 private boolean mScrollHasChanged; 9321 9322 public void addSubscriber(TextViewPositionListener positionListener, boolean canMove) { 9323 if (mNumberOfListeners == 0) { 9324 updatePosition(); 9325 ViewTreeObserver vto = TextView.this.getViewTreeObserver(); 9326 vto.addOnPreDrawListener(this); 9327 } 9328 9329 int emptySlotIndex = -1; 9330 for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { 9331 TextViewPositionListener listener = mPositionListeners[i]; 9332 if (listener == positionListener) { 9333 return; 9334 } else if (emptySlotIndex < 0 && listener == null) { 9335 emptySlotIndex = i; 9336 } 9337 } 9338 9339 mPositionListeners[emptySlotIndex] = positionListener; 9340 mCanMove[emptySlotIndex] = canMove; 9341 mNumberOfListeners++; 9342 } 9343 9344 public void removeSubscriber(TextViewPositionListener positionListener) { 9345 for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { 9346 if (mPositionListeners[i] == positionListener) { 9347 mPositionListeners[i] = null; 9348 mNumberOfListeners--; 9349 break; 9350 } 9351 } 9352 9353 if (mNumberOfListeners == 0) { 9354 ViewTreeObserver vto = TextView.this.getViewTreeObserver(); 9355 vto.removeOnPreDrawListener(this); 9356 } 9357 } 9358 9359 public int getPositionX() { 9360 return mPositionX; 9361 } 9362 9363 public int getPositionY() { 9364 return mPositionY; 9365 } 9366 9367 @Override 9368 public boolean onPreDraw() { 9369 updatePosition(); 9370 9371 for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { 9372 if (mPositionHasChanged || mScrollHasChanged || mCanMove[i]) { 9373 TextViewPositionListener positionListener = mPositionListeners[i]; 9374 if (positionListener != null) { 9375 positionListener.updatePosition(mPositionX, mPositionY, 9376 mPositionHasChanged, mScrollHasChanged); 9377 } 9378 } 9379 } 9380 9381 mScrollHasChanged = false; 9382 return true; 9383 } 9384 9385 private void updatePosition() { 9386 TextView.this.getLocationInWindow(mTempCoords); 9387 9388 mPositionHasChanged = mTempCoords[0] != mPositionX || mTempCoords[1] != mPositionY; 9389 9390 mPositionX = mTempCoords[0]; 9391 mPositionY = mTempCoords[1]; 9392 } 9393 9394 public boolean isVisible(int positionX, int positionY) { 9395 final TextView textView = TextView.this; 9396 9397 if (mTempRect == null) mTempRect = new Rect(); 9398 final Rect clip = mTempRect; 9399 clip.left = getCompoundPaddingLeft(); 9400 clip.top = getExtendedPaddingTop(); 9401 clip.right = textView.getWidth() - getCompoundPaddingRight(); 9402 clip.bottom = textView.getHeight() - getExtendedPaddingBottom(); 9403 9404 final ViewParent parent = textView.getParent(); 9405 if (parent == null || !parent.getChildVisibleRect(textView, clip, null)) { 9406 return false; 9407 } 9408 9409 int posX = mPositionX + positionX; 9410 int posY = mPositionY + positionY; 9411 9412 // Offset by 1 to take into account 0.5 and int rounding around getPrimaryHorizontal. 9413 return posX >= clip.left - 1 && posX <= clip.right + 1 && 9414 posY >= clip.top && posY <= clip.bottom; 9415 } 9416 9417 public boolean isOffsetVisible(int offset) { 9418 final int line = mLayout.getLineForOffset(offset); 9419 final int lineBottom = mLayout.getLineBottom(line); 9420 final int primaryHorizontal = (int) mLayout.getPrimaryHorizontal(offset); 9421 return isVisible(primaryHorizontal, lineBottom); 9422 } 9423 9424 public void onScrollChanged() { 9425 mScrollHasChanged = true; 9426 } 9427 } 9428 9429 @Override 9430 protected void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) { 9431 super.onScrollChanged(horiz, vert, oldHoriz, oldVert); 9432 if (mPositionListener != null) { 9433 mPositionListener.onScrollChanged(); 9434 } 9435 } 9436 9437 private abstract class PinnedPopupWindow implements TextViewPositionListener { 9438 protected PopupWindow mPopupWindow; 9439 protected ViewGroup mContentView; 9440 int mPositionX, mPositionY; 9441 9442 protected abstract void createPopupWindow(); 9443 protected abstract void initContentView(); 9444 protected abstract int getTextOffset(); 9445 protected abstract int getVerticalLocalPosition(int line); 9446 protected abstract int clipVertically(int positionY); 9447 9448 public PinnedPopupWindow() { 9449 createPopupWindow(); 9450 9451 mPopupWindow.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL); 9452 mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); 9453 mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); 9454 9455 initContentView(); 9456 9457 LayoutParams wrapContent = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 9458 ViewGroup.LayoutParams.WRAP_CONTENT); 9459 mContentView.setLayoutParams(wrapContent); 9460 9461 mPopupWindow.setContentView(mContentView); 9462 } 9463 9464 public void show() { 9465 TextView.this.getPositionListener().addSubscriber(this, false /* offset is fixed */); 9466 9467 computeLocalPosition(); 9468 9469 final PositionListener positionListener = TextView.this.getPositionListener(); 9470 updatePosition(positionListener.getPositionX(), positionListener.getPositionY()); 9471 } 9472 9473 protected void measureContent() { 9474 final DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); 9475 mContentView.measure( 9476 View.MeasureSpec.makeMeasureSpec(displayMetrics.widthPixels, 9477 View.MeasureSpec.AT_MOST), 9478 View.MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels, 9479 View.MeasureSpec.AT_MOST)); 9480 } 9481 9482 /* The popup window will be horizontally centered on the getTextOffset() and vertically 9483 * positioned according to viewportToContentHorizontalOffset. 9484 * 9485 * This method assumes that mContentView has properly been measured from its content. */ 9486 private void computeLocalPosition() { 9487 measureContent(); 9488 final int width = mContentView.getMeasuredWidth(); 9489 final int offset = getTextOffset(); 9490 mPositionX = (int) (mLayout.getPrimaryHorizontal(offset) - width / 2.0f); 9491 mPositionX += viewportToContentHorizontalOffset(); 9492 9493 final int line = mLayout.getLineForOffset(offset); 9494 mPositionY = getVerticalLocalPosition(line); 9495 mPositionY += viewportToContentVerticalOffset(); 9496 } 9497 9498 private void updatePosition(int parentPositionX, int parentPositionY) { 9499 int positionX = parentPositionX + mPositionX; 9500 int positionY = parentPositionY + mPositionY; 9501 9502 positionY = clipVertically(positionY); 9503 9504 // Horizontal clipping 9505 final DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); 9506 final int width = mContentView.getMeasuredWidth(); 9507 positionX = Math.min(displayMetrics.widthPixels - width, positionX); 9508 positionX = Math.max(0, positionX); 9509 9510 if (isShowing()) { 9511 mPopupWindow.update(positionX, positionY, -1, -1); 9512 } else { 9513 mPopupWindow.showAtLocation(TextView.this, Gravity.NO_GRAVITY, 9514 positionX, positionY); 9515 } 9516 } 9517 9518 public void hide() { 9519 mPopupWindow.dismiss(); 9520 TextView.this.getPositionListener().removeSubscriber(this); 9521 } 9522 9523 @Override 9524 public void updatePosition(int parentPositionX, int parentPositionY, 9525 boolean parentPositionChanged, boolean parentScrolled) { 9526 // Either parentPositionChanged or parentScrolled is true, check if still visible 9527 if (isShowing() && getPositionListener().isOffsetVisible(getTextOffset())) { 9528 if (parentScrolled) computeLocalPosition(); 9529 updatePosition(parentPositionX, parentPositionY); 9530 } else { 9531 hide(); 9532 } 9533 } 9534 9535 public boolean isShowing() { 9536 return mPopupWindow.isShowing(); 9537 } 9538 } 9539 9540 private class SuggestionsPopupWindow extends PinnedPopupWindow implements OnItemClickListener { 9541 private static final int MAX_NUMBER_SUGGESTIONS = SuggestionSpan.SUGGESTIONS_MAX_SIZE; 9542 private SuggestionInfo[] mSuggestionInfos; 9543 private int mNumberOfSuggestions; 9544 private boolean mCursorWasVisibleBeforeSuggestions; 9545 private SuggestionAdapter mSuggestionsAdapter; 9546 9547 private class CustomPopupWindow extends PopupWindow { 9548 public CustomPopupWindow(Context context, int defStyle) { 9549 super(context, null, defStyle); 9550 } 9551 9552 @Override 9553 public void dismiss() { 9554 super.dismiss(); 9555 9556 TextView.this.getPositionListener().removeSubscriber(SuggestionsPopupWindow.this); 9557 9558 if ((mText instanceof Editable) && mSuggestionRangeSpan != null) { 9559 ((Editable) mText).removeSpan(mSuggestionRangeSpan); 9560 } 9561 9562 setCursorVisible(mCursorWasVisibleBeforeSuggestions); 9563 if (hasInsertionController()) { 9564 getInsertionController().show(); 9565 } 9566 } 9567 } 9568 9569 public SuggestionsPopupWindow() { 9570 mCursorWasVisibleBeforeSuggestions = mCursorVisible; 9571 } 9572 9573 @Override 9574 protected void createPopupWindow() { 9575 mPopupWindow = new CustomPopupWindow(TextView.this.mContext, 9576 com.android.internal.R.attr.textSuggestionsWindowStyle); 9577 mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); 9578 mPopupWindow.setFocusable(true); 9579 mPopupWindow.setClippingEnabled(false); 9580 } 9581 9582 @Override 9583 protected void initContentView() { 9584 ListView listView = new ListView(TextView.this.getContext()); 9585 mSuggestionsAdapter = new SuggestionAdapter(); 9586 listView.setAdapter(mSuggestionsAdapter); 9587 listView.setOnItemClickListener(this); 9588 mContentView = listView; 9589 9590 // Inflate the suggestion items once and for all. +1 for add to dictionary 9591 mSuggestionInfos = new SuggestionInfo[MAX_NUMBER_SUGGESTIONS + 1]; 9592 for (int i = 0; i < MAX_NUMBER_SUGGESTIONS + 1; i++) { 9593 mSuggestionInfos[i] = new SuggestionInfo(); 9594 } 9595 } 9596 9597 private class SuggestionInfo { 9598 int suggestionStart, suggestionEnd; // range of suggestion item with replacement text 9599 int spanStart, spanEnd; // range in TextView where text should be inserted 9600 SuggestionSpan suggestionSpan; // the SuggestionSpan that this TextView represents 9601 int suggestionIndex; // the index of the suggestion inside suggestionSpan 9602 SpannableStringBuilder text = new SpannableStringBuilder(); 9603 TextAppearanceSpan highlightSpan = new TextAppearanceSpan(mContext, 9604 android.R.style.TextAppearance_SuggestionHighlight); 9605 9606 void removeMisspelledFlag() { 9607 int suggestionSpanFlags = suggestionSpan.getFlags(); 9608 if ((suggestionSpanFlags & SuggestionSpan.FLAG_MISSPELLED) > 0) { 9609 suggestionSpanFlags &= ~(SuggestionSpan.FLAG_MISSPELLED); 9610 suggestionSpanFlags &= ~(SuggestionSpan.FLAG_EASY_CORRECT); 9611 suggestionSpan.setFlags(suggestionSpanFlags); 9612 } 9613 } 9614 } 9615 9616 private class SuggestionAdapter extends BaseAdapter { 9617 private LayoutInflater mInflater = (LayoutInflater) TextView.this.mContext. 9618 getSystemService(Context.LAYOUT_INFLATER_SERVICE); 9619 9620 @Override 9621 public int getCount() { 9622 return mNumberOfSuggestions; 9623 } 9624 9625 @Override 9626 public Object getItem(int position) { 9627 return mSuggestionInfos[position]; 9628 } 9629 9630 @Override 9631 public long getItemId(int position) { 9632 return position; 9633 } 9634 9635 @Override 9636 public View getView(int position, View convertView, ViewGroup parent) { 9637 TextView textView = (TextView) convertView; 9638 9639 if (textView == null) { 9640 textView = (TextView) mInflater.inflate(mTextEditSuggestionItemLayout, parent, 9641 false); 9642 } 9643 9644 textView.setText(mSuggestionInfos[position].text); 9645 return textView; 9646 } 9647 } 9648 9649 /** 9650 * Returns the suggestion spans that cover the current cursor position. The suggestion 9651 * spans are sorted according to the length of text that they are attached to. 9652 */ 9653 private SuggestionSpan[] getSuggestionSpans() { 9654 int pos = TextView.this.getSelectionStart(); 9655 Spannable spannable = (Spannable) TextView.this.mText; 9656 SuggestionSpan[] suggestionSpans = spannable.getSpans(pos, pos, SuggestionSpan.class); 9657 9658 // Cache the span length for performance reason. 9659 final HashMap<SuggestionSpan, Integer> spansLengths = 9660 new HashMap<SuggestionSpan, Integer>(); 9661 9662 for (SuggestionSpan suggestionSpan : suggestionSpans) { 9663 int start = spannable.getSpanStart(suggestionSpan); 9664 int end = spannable.getSpanEnd(suggestionSpan); 9665 spansLengths.put(suggestionSpan, Integer.valueOf(end - start)); 9666 } 9667 9668 // The suggestions are sorted according to the lenght of the text that they cover 9669 // (shorter first) 9670 Arrays.sort(suggestionSpans, new Comparator<SuggestionSpan>() { 9671 public int compare(SuggestionSpan span1, SuggestionSpan span2) { 9672 return spansLengths.get(span1).intValue() - spansLengths.get(span2).intValue(); 9673 } 9674 }); 9675 9676 return suggestionSpans; 9677 } 9678 9679 @Override 9680 public void show() { 9681 if (!(mText instanceof Editable)) return; 9682 9683 if (updateSuggestions()) { 9684 mCursorWasVisibleBeforeSuggestions = mCursorVisible; 9685 setCursorVisible(false); 9686 super.show(); 9687 } 9688 } 9689 9690 @Override 9691 protected void measureContent() { 9692 final DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); 9693 final int horizontalMeasure = View.MeasureSpec.makeMeasureSpec( 9694 displayMetrics.widthPixels, View.MeasureSpec.AT_MOST); 9695 final int verticalMeasure = View.MeasureSpec.makeMeasureSpec( 9696 displayMetrics.heightPixels, View.MeasureSpec.AT_MOST); 9697 9698 int width = 0; 9699 View view = null; 9700 for (int i = 0; i < mNumberOfSuggestions; i++) { 9701 view = mSuggestionsAdapter.getView(i, view, mContentView); 9702 view.measure(horizontalMeasure, verticalMeasure); 9703 width = Math.max(width, view.getMeasuredWidth()); 9704 } 9705 9706 // Enforce the width based on actual text widths 9707 mContentView.measure( 9708 View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), 9709 verticalMeasure); 9710 9711 Drawable popupBackground = mPopupWindow.getBackground(); 9712 if (popupBackground != null) { 9713 if (mTempRect == null) mTempRect = new Rect(); 9714 popupBackground.getPadding(mTempRect); 9715 width += mTempRect.left + mTempRect.right; 9716 } 9717 mPopupWindow.setWidth(width); 9718 } 9719 9720 @Override 9721 protected int getTextOffset() { 9722 return getSelectionStart(); 9723 } 9724 9725 @Override 9726 protected int getVerticalLocalPosition(int line) { 9727 return mLayout.getLineBottom(line); 9728 } 9729 9730 @Override 9731 protected int clipVertically(int positionY) { 9732 final int height = mContentView.getMeasuredHeight(); 9733 final DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); 9734 return Math.min(positionY, displayMetrics.heightPixels - height); 9735 } 9736 9737 @Override 9738 public void hide() { 9739 super.hide(); 9740 } 9741 9742 private boolean updateSuggestions() { 9743 Spannable spannable = (Spannable)TextView.this.mText; 9744 SuggestionSpan[] suggestionSpans = getSuggestionSpans(); 9745 9746 final int nbSpans = suggestionSpans.length; 9747 9748 mNumberOfSuggestions = 0; 9749 int spanUnionStart = mText.length(); 9750 int spanUnionEnd = 0; 9751 9752 SuggestionSpan misspelledSpan = null; 9753 9754 for (int spanIndex = 0; spanIndex < nbSpans; spanIndex++) { 9755 SuggestionSpan suggestionSpan = suggestionSpans[spanIndex]; 9756 final int spanStart = spannable.getSpanStart(suggestionSpan); 9757 final int spanEnd = spannable.getSpanEnd(suggestionSpan); 9758 spanUnionStart = Math.min(spanStart, spanUnionStart); 9759 spanUnionEnd = Math.max(spanEnd, spanUnionEnd); 9760 9761 if ((suggestionSpan.getFlags() & SuggestionSpan.FLAG_MISSPELLED) != 0) { 9762 misspelledSpan = suggestionSpan; 9763 } 9764 9765 String[] suggestions = suggestionSpan.getSuggestions(); 9766 int nbSuggestions = suggestions.length; 9767 for (int suggestionIndex = 0; suggestionIndex < nbSuggestions; suggestionIndex++) { 9768 SuggestionInfo suggestionInfo = mSuggestionInfos[mNumberOfSuggestions]; 9769 suggestionInfo.spanStart = spanStart; 9770 suggestionInfo.spanEnd = spanEnd; 9771 suggestionInfo.suggestionSpan = suggestionSpan; 9772 suggestionInfo.suggestionIndex = suggestionIndex; 9773 suggestionInfo.text.replace(0, suggestionInfo.text.length(), 9774 suggestions[suggestionIndex]); 9775 9776 mNumberOfSuggestions++; 9777 if (mNumberOfSuggestions == MAX_NUMBER_SUGGESTIONS) { 9778 // Also end outer for loop 9779 spanIndex = nbSpans; 9780 break; 9781 } 9782 } 9783 } 9784 9785 for (int i = 0; i < mNumberOfSuggestions; i++) { 9786 highlightTextDifferences(mSuggestionInfos[i], spanUnionStart, spanUnionEnd); 9787 } 9788 9789 if (misspelledSpan != null) { 9790 final int misspelledStart = spannable.getSpanStart(misspelledSpan); 9791 final int misspelledEnd = spannable.getSpanEnd(misspelledSpan); 9792 if (misspelledStart >= 0 && misspelledEnd > misspelledStart) { 9793 SuggestionInfo suggestionInfo = mSuggestionInfos[mNumberOfSuggestions]; 9794 suggestionInfo.spanStart = misspelledStart; 9795 suggestionInfo.spanEnd = misspelledEnd; 9796 suggestionInfo.suggestionSpan = misspelledSpan; 9797 suggestionInfo.suggestionIndex = -1; 9798 suggestionInfo.text.replace(0, suggestionInfo.text.length(), 9799 getContext().getString(com.android.internal.R.string.addToDictionary)); 9800 9801 mNumberOfSuggestions++; 9802 } 9803 } 9804 9805 if (mNumberOfSuggestions == 0) return false; 9806 9807 if (mSuggestionRangeSpan == null) mSuggestionRangeSpan = 9808 new SuggestionRangeSpan(mHighlightColor); 9809 ((Editable) mText).setSpan(mSuggestionRangeSpan, spanUnionStart, spanUnionEnd, 9810 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 9811 9812 mSuggestionsAdapter.notifyDataSetChanged(); 9813 9814 return true; 9815 } 9816 9817 private void highlightTextDifferences(SuggestionInfo suggestionInfo, int unionStart, 9818 int unionEnd) { 9819 final int spanStart = suggestionInfo.spanStart; 9820 final int spanEnd = suggestionInfo.spanEnd; 9821 9822 // Adjust the start/end of the suggestion span 9823 suggestionInfo.suggestionStart = spanStart - unionStart; 9824 suggestionInfo.suggestionEnd = suggestionInfo.suggestionStart 9825 + suggestionInfo.text.length(); 9826 9827 suggestionInfo.text.clearSpans(); 9828 suggestionInfo.text.setSpan(suggestionInfo.highlightSpan, 0, 9829 suggestionInfo.text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 9830 9831 // Add the text before and after the span. 9832 suggestionInfo.text.insert(0, mText.subSequence(unionStart, spanStart).toString()); 9833 suggestionInfo.text.append(mText.subSequence(spanEnd, unionEnd).toString()); 9834 } 9835 9836 @Override 9837 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 9838 if (view instanceof TextView) { 9839 TextView textView = (TextView) view; 9840 9841 SuggestionInfo suggestionInfo = mSuggestionInfos[position]; 9842 final int spanStart = suggestionInfo.spanStart; 9843 final int spanEnd = suggestionInfo.spanEnd; 9844 final String originalText = mText.subSequence(spanStart, spanEnd).toString(); 9845 9846 if (suggestionInfo.suggestionIndex < 0) { 9847 Intent intent = new Intent(Settings.ACTION_USER_DICTIONARY_INSERT); 9848 intent.putExtra("word", originalText); 9849 intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); 9850 getContext().startActivity(intent); 9851 suggestionInfo.removeMisspelledFlag(); 9852 } else { 9853 // SuggestionSpans are removed by replace: save them before 9854 Editable editable = (Editable) mText; 9855 SuggestionSpan[] suggestionSpans = editable.getSpans(spanStart, spanEnd, 9856 SuggestionSpan.class); 9857 final int length = suggestionSpans.length; 9858 int[] suggestionSpansStarts = new int[length]; 9859 int[] suggestionSpansEnds = new int[length]; 9860 int[] suggestionSpansFlags = new int[length]; 9861 for (int i = 0; i < length; i++) { 9862 final SuggestionSpan suggestionSpan = suggestionSpans[i]; 9863 suggestionSpansStarts[i] = editable.getSpanStart(suggestionSpan); 9864 suggestionSpansEnds[i] = editable.getSpanEnd(suggestionSpan); 9865 suggestionSpansFlags[i] = editable.getSpanFlags(suggestionSpan); 9866 } 9867 9868 final int suggestionStart = suggestionInfo.suggestionStart; 9869 final int suggestionEnd = suggestionInfo.suggestionEnd; 9870 final String suggestion = textView.getText().subSequence( 9871 suggestionStart, suggestionEnd).toString(); 9872 editable.replace(spanStart, spanEnd, suggestion); 9873 9874 suggestionInfo.removeMisspelledFlag(); 9875 9876 // Notify source IME of the suggestion pick. Do this before swaping texts. 9877 if (!TextUtils.isEmpty( 9878 suggestionInfo.suggestionSpan.getNotificationTargetClassName())) { 9879 InputMethodManager imm = InputMethodManager.peekInstance(); 9880 imm.notifySuggestionPicked(suggestionInfo.suggestionSpan, originalText, 9881 suggestionInfo.suggestionIndex); 9882 } 9883 9884 // Swap text content between actual text and Suggestion span 9885 String[] suggestions = suggestionInfo.suggestionSpan.getSuggestions(); 9886 suggestions[suggestionInfo.suggestionIndex] = originalText; 9887 9888 // Restore previous SuggestionSpans 9889 final int lengthDifference = suggestion.length() - (spanEnd - spanStart); 9890 for (int i = 0; i < length; i++) { 9891 // Only spans that include the modified region make sense after replacement 9892 // Spans partially included in the replaced region are removed, there is no 9893 // way to assign them a valid range after replacement 9894 if (suggestionSpansStarts[i] <= spanStart && 9895 suggestionSpansEnds[i] >= spanEnd) { 9896 editable.setSpan(suggestionSpans[i], suggestionSpansStarts[i], 9897 suggestionSpansEnds[i] + lengthDifference, 9898 suggestionSpansFlags[i]); 9899 } 9900 } 9901 } 9902 } 9903 hide(); 9904 } 9905 } 9906 9907 void showSuggestions() { 9908 if (!isSuggestionsEnabled() || !isTextEditable()) return; 9909 9910 if (mSuggestionsPopupWindow == null) { 9911 mSuggestionsPopupWindow = new SuggestionsPopupWindow(); 9912 } 9913 hideControllers(); 9914 mSuggestionsPopupWindow.show(); 9915 } 9916 9917 boolean areSuggestionsShown() { 9918 return mSuggestionsPopupWindow != null && mSuggestionsPopupWindow.isShowing(); 9919 } 9920 9921 /** 9922 * Return whether or not suggestions are enabled on this TextView. The suggestions are generated 9923 * by the IME or by the spell checker as the user types. This is done by adding 9924 * {@link SuggestionSpan}s to the text. 9925 * 9926 * When suggestions are enabled (default), this list of suggestions will be displayed when the 9927 * user asks for them on these parts of the text. This value depends on the inputType of this 9928 * TextView. 9929 * 9930 * The class of the input type must be {@link InputType#TYPE_CLASS_TEXT}. 9931 * 9932 * In addition, the type variation must be one of 9933 * {@link InputType#TYPE_TEXT_VARIATION_NORMAL}, 9934 * {@link InputType#TYPE_TEXT_VARIATION_EMAIL_SUBJECT}, 9935 * {@link InputType#TYPE_TEXT_VARIATION_LONG_MESSAGE}, 9936 * {@link InputType#TYPE_TEXT_VARIATION_SHORT_MESSAGE} or 9937 * {@link InputType#TYPE_TEXT_VARIATION_WEB_EDIT_TEXT}. 9938 * 9939 * And finally, the {@link InputType#TYPE_TEXT_FLAG_NO_SUGGESTIONS} flag must <i>not</i> be set. 9940 * 9941 * @return true if the suggestions popup window is enabled, based on the inputType. 9942 */ 9943 public boolean isSuggestionsEnabled() { 9944 if ((mInputType & InputType.TYPE_MASK_CLASS) != InputType.TYPE_CLASS_TEXT) return false; 9945 if ((mInputType & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS) > 0) return false; 9946 9947 final int variation = mInputType & EditorInfo.TYPE_MASK_VARIATION; 9948 return (variation == EditorInfo.TYPE_TEXT_VARIATION_NORMAL || 9949 variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_SUBJECT || 9950 variation == EditorInfo.TYPE_TEXT_VARIATION_LONG_MESSAGE || 9951 variation == EditorInfo.TYPE_TEXT_VARIATION_SHORT_MESSAGE || 9952 variation == EditorInfo.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT); 9953 } 9954 9955 /** 9956 * If provided, this ActionMode.Callback will be used to create the ActionMode when text 9957 * selection is initiated in this View. 9958 * 9959 * The standard implementation populates the menu with a subset of Select All, Cut, Copy and 9960 * Paste actions, depending on what this View supports. 9961 * 9962 * A custom implementation can add new entries in the default menu in its 9963 * {@link android.view.ActionMode.Callback#onPrepareActionMode(ActionMode, Menu)} method. The 9964 * default actions can also be removed from the menu using {@link Menu#removeItem(int)} and 9965 * passing {@link android.R.id#selectAll}, {@link android.R.id#cut}, {@link android.R.id#copy} 9966 * or {@link android.R.id#paste} ids as parameters. 9967 * 9968 * Returning false from 9969 * {@link android.view.ActionMode.Callback#onCreateActionMode(ActionMode, Menu)} will prevent 9970 * the action mode from being started. 9971 * 9972 * Action click events should be handled by the custom implementation of 9973 * {@link android.view.ActionMode.Callback#onActionItemClicked(ActionMode, MenuItem)}. 9974 * 9975 * Note that text selection mode is not started when a TextView receives focus and the 9976 * {@link android.R.attr#selectAllOnFocus} flag has been set. The content is highlighted in 9977 * that case, to allow for quick replacement. 9978 */ 9979 public void setCustomSelectionActionModeCallback(ActionMode.Callback actionModeCallback) { 9980 mCustomSelectionActionModeCallback = actionModeCallback; 9981 } 9982 9983 /** 9984 * Retrieves the value set in {@link #setCustomSelectionActionModeCallback}. Default is null. 9985 * 9986 * @return The current custom selection callback. 9987 */ 9988 public ActionMode.Callback getCustomSelectionActionModeCallback() { 9989 return mCustomSelectionActionModeCallback; 9990 } 9991 9992 /** 9993 * 9994 * @return true if the selection mode was actually started. 9995 */ 9996 private boolean startSelectionActionMode() { 9997 if (mSelectionActionMode != null) { 9998 // Selection action mode is already started 9999 return false; 10000 } 10001 10002 if (!canSelectText() || !requestFocus()) { 10003 Log.w(LOG_TAG, "TextView does not support text selection. Action mode cancelled."); 10004 return false; 10005 } 10006 10007 if (!hasSelection()) { 10008 // There may already be a selection on device rotation 10009 boolean currentWordSelected = selectCurrentWord(); 10010 if (!currentWordSelected) { 10011 // No word found under cursor or text selection not permitted. 10012 return false; 10013 } 10014 } 10015 10016 ActionMode.Callback actionModeCallback = new SelectionActionModeCallback(); 10017 mSelectionActionMode = startActionMode(actionModeCallback); 10018 final boolean selectionStarted = mSelectionActionMode != null; 10019 10020 if (selectionStarted && !mTextIsSelectable) { 10021 // Show the IME to be able to replace text, except when selecting non editable text. 10022 final InputMethodManager imm = InputMethodManager.peekInstance(); 10023 if (imm != null) imm.showSoftInput(this, 0, null); 10024 } 10025 10026 return selectionStarted; 10027 } 10028 10029 private void stopSelectionActionMode() { 10030 if (mSelectionActionMode != null) { 10031 // This will hide the mSelectionModifierCursorController 10032 mSelectionActionMode.finish(); 10033 } 10034 } 10035 10036 /** 10037 * Paste clipboard content between min and max positions. 10038 */ 10039 private void paste(int min, int max) { 10040 ClipboardManager clipboard = 10041 (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE); 10042 ClipData clip = clipboard.getPrimaryClip(); 10043 if (clip != null) { 10044 boolean didFirst = false; 10045 for (int i=0; i<clip.getItemCount(); i++) { 10046 CharSequence paste = clip.getItemAt(i).coerceToText(getContext()); 10047 if (paste != null) { 10048 if (!didFirst) { 10049 long minMax = prepareSpacesAroundPaste(min, max, paste); 10050 min = extractRangeStartFromLong(minMax); 10051 max = extractRangeEndFromLong(minMax); 10052 Selection.setSelection((Spannable) mText, max); 10053 ((Editable) mText).replace(min, max, paste); 10054 didFirst = true; 10055 } else { 10056 ((Editable) mText).insert(getSelectionEnd(), "\n"); 10057 ((Editable) mText).insert(getSelectionEnd(), paste); 10058 } 10059 } 10060 } 10061 stopSelectionActionMode(); 10062 sLastCutOrCopyTime = 0; 10063 } 10064 } 10065 10066 private void setPrimaryClip(ClipData clip) { 10067 ClipboardManager clipboard = (ClipboardManager) getContext(). 10068 getSystemService(Context.CLIPBOARD_SERVICE); 10069 clipboard.setPrimaryClip(clip); 10070 sLastCutOrCopyTime = SystemClock.uptimeMillis(); 10071 } 10072 10073 /** 10074 * An ActionMode Callback class that is used to provide actions while in text selection mode. 10075 * 10076 * The default callback provides a subset of Select All, Cut, Copy and Paste actions, depending 10077 * on which of these this TextView supports. 10078 */ 10079 private class SelectionActionModeCallback implements ActionMode.Callback { 10080 10081 @Override 10082 public boolean onCreateActionMode(ActionMode mode, Menu menu) { 10083 TypedArray styledAttributes = mContext.obtainStyledAttributes(R.styleable.Theme); 10084 10085 boolean allowText = getContext().getResources().getBoolean( 10086 com.android.internal.R.bool.config_allowActionMenuItemTextWithIcon); 10087 10088 mode.setTitle(allowText ? 10089 mContext.getString(com.android.internal.R.string.textSelectionCABTitle) : null); 10090 mode.setSubtitle(null); 10091 10092 int selectAllIconId = 0; // No icon by default 10093 if (!allowText) { 10094 // Provide an icon, text will not be displayed on smaller screens. 10095 selectAllIconId = styledAttributes.getResourceId( 10096 R.styleable.Theme_actionModeSelectAllDrawable, 0); 10097 } 10098 10099 menu.add(0, ID_SELECT_ALL, 0, com.android.internal.R.string.selectAll). 10100 setIcon(selectAllIconId). 10101 setAlphabeticShortcut('a'). 10102 setShowAsAction( 10103 MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); 10104 10105 if (canCut()) { 10106 menu.add(0, ID_CUT, 0, com.android.internal.R.string.cut). 10107 setIcon(styledAttributes.getResourceId( 10108 R.styleable.Theme_actionModeCutDrawable, 0)). 10109 setAlphabeticShortcut('x'). 10110 setShowAsAction( 10111 MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); 10112 } 10113 10114 if (canCopy()) { 10115 menu.add(0, ID_COPY, 0, com.android.internal.R.string.copy). 10116 setIcon(styledAttributes.getResourceId( 10117 R.styleable.Theme_actionModeCopyDrawable, 0)). 10118 setAlphabeticShortcut('c'). 10119 setShowAsAction( 10120 MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); 10121 } 10122 10123 if (canPaste()) { 10124 menu.add(0, ID_PASTE, 0, com.android.internal.R.string.paste). 10125 setIcon(styledAttributes.getResourceId( 10126 R.styleable.Theme_actionModePasteDrawable, 0)). 10127 setAlphabeticShortcut('v'). 10128 setShowAsAction( 10129 MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); 10130 } 10131 10132 styledAttributes.recycle(); 10133 10134 if (mCustomSelectionActionModeCallback != null) { 10135 if (!mCustomSelectionActionModeCallback.onCreateActionMode(mode, menu)) { 10136 // The custom mode can choose to cancel the action mode 10137 return false; 10138 } 10139 } 10140 10141 if (menu.hasVisibleItems() || mode.getCustomView() != null) { 10142 getSelectionController().show(); 10143 return true; 10144 } else { 10145 return false; 10146 } 10147 } 10148 10149 @Override 10150 public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 10151 if (mCustomSelectionActionModeCallback != null) { 10152 return mCustomSelectionActionModeCallback.onPrepareActionMode(mode, menu); 10153 } 10154 return true; 10155 } 10156 10157 @Override 10158 public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 10159 if (mCustomSelectionActionModeCallback != null && 10160 mCustomSelectionActionModeCallback.onActionItemClicked(mode, item)) { 10161 return true; 10162 } 10163 return onTextContextMenuItem(item.getItemId()); 10164 } 10165 10166 @Override 10167 public void onDestroyActionMode(ActionMode mode) { 10168 if (mCustomSelectionActionModeCallback != null) { 10169 mCustomSelectionActionModeCallback.onDestroyActionMode(mode); 10170 } 10171 Selection.setSelection((Spannable) mText, getSelectionEnd()); 10172 10173 if (mSelectionModifierCursorController != null) { 10174 mSelectionModifierCursorController.hide(); 10175 } 10176 10177 mSelectionActionMode = null; 10178 } 10179 } 10180 10181 private class ActionPopupWindow extends PinnedPopupWindow implements OnClickListener { 10182 private static final int POPUP_TEXT_LAYOUT = 10183 com.android.internal.R.layout.text_edit_action_popup_text; 10184 private TextView mPasteTextView; 10185 private TextView mReplaceTextView; 10186 10187 @Override 10188 protected void createPopupWindow() { 10189 mPopupWindow = new PopupWindow(TextView.this.mContext, null, 10190 com.android.internal.R.attr.textSelectHandleWindowStyle); 10191 mPopupWindow.setClippingEnabled(true); 10192 } 10193 10194 @Override 10195 protected void initContentView() { 10196 LinearLayout linearLayout = new LinearLayout(TextView.this.getContext()); 10197 linearLayout.setOrientation(LinearLayout.HORIZONTAL); 10198 mContentView = linearLayout; 10199 mContentView.setBackgroundResource( 10200 com.android.internal.R.drawable.text_edit_paste_window); 10201 10202 LayoutInflater inflater = (LayoutInflater)TextView.this.mContext. 10203 getSystemService(Context.LAYOUT_INFLATER_SERVICE); 10204 10205 LayoutParams wrapContent = new LayoutParams( 10206 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 10207 10208 mPasteTextView = (TextView) inflater.inflate(POPUP_TEXT_LAYOUT, null); 10209 mPasteTextView.setLayoutParams(wrapContent); 10210 mContentView.addView(mPasteTextView); 10211 mPasteTextView.setText(com.android.internal.R.string.paste); 10212 mPasteTextView.setOnClickListener(this); 10213 10214 mReplaceTextView = (TextView) inflater.inflate(POPUP_TEXT_LAYOUT, null); 10215 mReplaceTextView.setLayoutParams(wrapContent); 10216 mContentView.addView(mReplaceTextView); 10217 mReplaceTextView.setText(com.android.internal.R.string.replace); 10218 mReplaceTextView.setOnClickListener(this); 10219 } 10220 10221 @Override 10222 public void show() { 10223 boolean canPaste = canPaste(); 10224 boolean canSuggest = isSuggestionsEnabled() && isCursorInsideSuggestionSpan(); 10225 mPasteTextView.setVisibility(canPaste ? View.VISIBLE : View.GONE); 10226 mReplaceTextView.setVisibility(canSuggest ? View.VISIBLE : View.GONE); 10227 10228 if (!canPaste && !canSuggest) return; 10229 10230 super.show(); 10231 } 10232 10233 @Override 10234 public void onClick(View view) { 10235 if (view == mPasteTextView && canPaste()) { 10236 onTextContextMenuItem(ID_PASTE); 10237 hide(); 10238 } else if (view == mReplaceTextView) { 10239 final int middle = (getSelectionStart() + getSelectionEnd()) / 2; 10240 stopSelectionActionMode(); 10241 Selection.setSelection((Spannable) mText, middle); 10242 showSuggestions(); 10243 } 10244 } 10245 10246 @Override 10247 protected int getTextOffset() { 10248 return (getSelectionStart() + getSelectionEnd()) / 2; 10249 } 10250 10251 @Override 10252 protected int getVerticalLocalPosition(int line) { 10253 return mLayout.getLineTop(line) - mContentView.getMeasuredHeight(); 10254 } 10255 10256 @Override 10257 protected int clipVertically(int positionY) { 10258 if (positionY < 0) { 10259 final int offset = getTextOffset(); 10260 final int line = mLayout.getLineForOffset(offset); 10261 positionY += mLayout.getLineBottom(line) - mLayout.getLineTop(line); 10262 positionY += mContentView.getMeasuredHeight(); 10263 10264 // Assumes insertion and selection handles share the same height 10265 final Drawable handle = mContext.getResources().getDrawable(mTextSelectHandleRes); 10266 positionY += handle.getIntrinsicHeight(); 10267 } 10268 10269 return positionY; 10270 } 10271 } 10272 10273 private abstract class HandleView extends View implements TextViewPositionListener { 10274 protected Drawable mDrawable; 10275 protected Drawable mDrawableLtr; 10276 protected Drawable mDrawableRtl; 10277 private final PopupWindow mContainer; 10278 // Position with respect to the parent TextView 10279 private int mPositionX, mPositionY; 10280 private boolean mIsDragging; 10281 // Offset from touch position to mPosition 10282 private float mTouchToWindowOffsetX, mTouchToWindowOffsetY; 10283 protected int mHotspotX; 10284 // Offsets the hotspot point up, so that cursor is not hidden by the finger when moving up 10285 private float mTouchOffsetY; 10286 // Where the touch position should be on the handle to ensure a maximum cursor visibility 10287 private float mIdealVerticalOffset; 10288 // Parent's (TextView) previous position in window 10289 private int mLastParentX, mLastParentY; 10290 // Transient action popup window for Paste and Replace actions 10291 protected ActionPopupWindow mActionPopupWindow; 10292 // Previous text character offset 10293 private int mPreviousOffset = -1; 10294 // Previous text character offset 10295 private boolean mPositionHasChanged = true; 10296 // Used to delay the appearance of the action popup window 10297 private Runnable mActionPopupShower; 10298 10299 public HandleView(Drawable drawableLtr, Drawable drawableRtl) { 10300 super(TextView.this.mContext); 10301 mContainer = new PopupWindow(TextView.this.mContext, null, 10302 com.android.internal.R.attr.textSelectHandleWindowStyle); 10303 mContainer.setSplitTouchEnabled(true); 10304 mContainer.setClippingEnabled(false); 10305 mContainer.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL); 10306 mContainer.setContentView(this); 10307 10308 mDrawableLtr = drawableLtr; 10309 mDrawableRtl = drawableRtl; 10310 10311 updateDrawable(); 10312 10313 final int handleHeight = mDrawable.getIntrinsicHeight(); 10314 mTouchOffsetY = -0.3f * handleHeight; 10315 mIdealVerticalOffset = 0.7f * handleHeight; 10316 } 10317 10318 protected void updateDrawable() { 10319 final int offset = getCurrentCursorOffset(); 10320 final boolean isRtlCharAtOffset = mLayout.isRtlCharAt(offset); 10321 mDrawable = isRtlCharAtOffset ? mDrawableRtl : mDrawableLtr; 10322 mHotspotX = getHotspotX(mDrawable, isRtlCharAtOffset); 10323 } 10324 10325 protected abstract int getHotspotX(Drawable drawable, boolean isRtlRun); 10326 10327 // Touch-up filter: number of previous positions remembered 10328 private static final int HISTORY_SIZE = 5; 10329 private static final int TOUCH_UP_FILTER_DELAY_AFTER = 150; 10330 private static final int TOUCH_UP_FILTER_DELAY_BEFORE = 350; 10331 private final long[] mPreviousOffsetsTimes = new long[HISTORY_SIZE]; 10332 private final int[] mPreviousOffsets = new int[HISTORY_SIZE]; 10333 private int mPreviousOffsetIndex = 0; 10334 private int mNumberPreviousOffsets = 0; 10335 10336 private void startTouchUpFilter(int offset) { 10337 mNumberPreviousOffsets = 0; 10338 addPositionToTouchUpFilter(offset); 10339 } 10340 10341 private void addPositionToTouchUpFilter(int offset) { 10342 mPreviousOffsetIndex = (mPreviousOffsetIndex + 1) % HISTORY_SIZE; 10343 mPreviousOffsets[mPreviousOffsetIndex] = offset; 10344 mPreviousOffsetsTimes[mPreviousOffsetIndex] = SystemClock.uptimeMillis(); 10345 mNumberPreviousOffsets++; 10346 } 10347 10348 private void filterOnTouchUp() { 10349 final long now = SystemClock.uptimeMillis(); 10350 int i = 0; 10351 int index = mPreviousOffsetIndex; 10352 final int iMax = Math.min(mNumberPreviousOffsets, HISTORY_SIZE); 10353 while (i < iMax && (now - mPreviousOffsetsTimes[index]) < TOUCH_UP_FILTER_DELAY_AFTER) { 10354 i++; 10355 index = (mPreviousOffsetIndex - i + HISTORY_SIZE) % HISTORY_SIZE; 10356 } 10357 10358 if (i > 0 && i < iMax && 10359 (now - mPreviousOffsetsTimes[index]) > TOUCH_UP_FILTER_DELAY_BEFORE) { 10360 positionAtCursorOffset(mPreviousOffsets[index], false); 10361 } 10362 } 10363 10364 public boolean offsetHasBeenChanged() { 10365 return mNumberPreviousOffsets > 1; 10366 } 10367 10368 @Override 10369 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 10370 setMeasuredDimension(mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight()); 10371 } 10372 10373 public void show() { 10374 if (isShowing()) return; 10375 10376 getPositionListener().addSubscriber(this, true /* local position may change */); 10377 10378 // Make sure the offset is always considered new, even when focusing at same position 10379 mPreviousOffset = -1; 10380 positionAtCursorOffset(getCurrentCursorOffset(), false); 10381 10382 hideActionPopupWindow(); 10383 } 10384 10385 protected void dismiss() { 10386 mIsDragging = false; 10387 mContainer.dismiss(); 10388 onDetached(); 10389 } 10390 10391 public void hide() { 10392 dismiss(); 10393 10394 TextView.this.getPositionListener().removeSubscriber(this); 10395 } 10396 10397 void showActionPopupWindow(int delay) { 10398 if (mActionPopupWindow == null) { 10399 mActionPopupWindow = new ActionPopupWindow(); 10400 } 10401 if (mActionPopupShower == null) { 10402 mActionPopupShower = new Runnable() { 10403 public void run() { 10404 mActionPopupWindow.show(); 10405 } 10406 }; 10407 } else { 10408 TextView.this.removeCallbacks(mActionPopupShower); 10409 } 10410 TextView.this.postDelayed(mActionPopupShower, delay); 10411 } 10412 10413 protected void hideActionPopupWindow() { 10414 if (mActionPopupShower != null) { 10415 TextView.this.removeCallbacks(mActionPopupShower); 10416 } 10417 if (mActionPopupWindow != null) { 10418 mActionPopupWindow.hide(); 10419 } 10420 } 10421 10422 public boolean isShowing() { 10423 return mContainer.isShowing(); 10424 } 10425 10426 private boolean isVisible() { 10427 // Always show a dragging handle. 10428 if (mIsDragging) { 10429 return true; 10430 } 10431 10432 if (isInBatchEditMode()) { 10433 return false; 10434 } 10435 10436 return getPositionListener().isVisible(mPositionX + mHotspotX, mPositionY); 10437 } 10438 10439 public abstract int getCurrentCursorOffset(); 10440 10441 protected void updateSelection(int offset) { 10442 updateDrawable(); 10443 } 10444 10445 public abstract void updatePosition(float x, float y); 10446 10447 protected void positionAtCursorOffset(int offset, boolean parentScrolled) { 10448 // A HandleView relies on the layout, which may be nulled by external methods 10449 if (mLayout == null) { 10450 // Will update controllers' state, hiding them and stopping selection mode if needed 10451 prepareCursorControllers(); 10452 return; 10453 } 10454 10455 if (offset != mPreviousOffset || parentScrolled) { 10456 updateSelection(offset); 10457 addPositionToTouchUpFilter(offset); 10458 final int line = mLayout.getLineForOffset(offset); 10459 10460 mPositionX = (int) (mLayout.getPrimaryHorizontal(offset) - 0.5f - mHotspotX); 10461 mPositionY = mLayout.getLineBottom(line); 10462 10463 // Take TextView's padding into account. 10464 mPositionX += viewportToContentHorizontalOffset(); 10465 mPositionY += viewportToContentVerticalOffset(); 10466 10467 mPreviousOffset = offset; 10468 mPositionHasChanged = true; 10469 } 10470 } 10471 10472 public void updatePosition(int parentPositionX, int parentPositionY, 10473 boolean parentPositionChanged, boolean parentScrolled) { 10474 positionAtCursorOffset(getCurrentCursorOffset(), parentScrolled); 10475 if (parentPositionChanged || mPositionHasChanged) { 10476 if (mIsDragging) { 10477 // Update touchToWindow offset in case of parent scrolling while dragging 10478 if (parentPositionX != mLastParentX || parentPositionY != mLastParentY) { 10479 mTouchToWindowOffsetX += parentPositionX - mLastParentX; 10480 mTouchToWindowOffsetY += parentPositionY - mLastParentY; 10481 mLastParentX = parentPositionX; 10482 mLastParentY = parentPositionY; 10483 } 10484 10485 onHandleMoved(); 10486 } 10487 10488 if (isVisible()) { 10489 final int positionX = parentPositionX + mPositionX; 10490 final int positionY = parentPositionY + mPositionY; 10491 if (isShowing()) { 10492 mContainer.update(positionX, positionY, -1, -1); 10493 } else { 10494 mContainer.showAtLocation(TextView.this, Gravity.NO_GRAVITY, 10495 positionX, positionY); 10496 } 10497 } else { 10498 if (isShowing()) { 10499 dismiss(); 10500 } 10501 } 10502 10503 mPositionHasChanged = false; 10504 } 10505 } 10506 10507 @Override 10508 protected void onDraw(Canvas c) { 10509 mDrawable.setBounds(0, 0, mRight - mLeft, mBottom - mTop); 10510 mDrawable.draw(c); 10511 } 10512 10513 @Override 10514 public boolean onTouchEvent(MotionEvent ev) { 10515 switch (ev.getActionMasked()) { 10516 case MotionEvent.ACTION_DOWN: { 10517 startTouchUpFilter(getCurrentCursorOffset()); 10518 mTouchToWindowOffsetX = ev.getRawX() - mPositionX; 10519 mTouchToWindowOffsetY = ev.getRawY() - mPositionY; 10520 10521 final PositionListener positionListener = getPositionListener(); 10522 mLastParentX = positionListener.getPositionX(); 10523 mLastParentY = positionListener.getPositionY(); 10524 mIsDragging = true; 10525 break; 10526 } 10527 10528 case MotionEvent.ACTION_MOVE: { 10529 final float rawX = ev.getRawX(); 10530 final float rawY = ev.getRawY(); 10531 10532 // Vertical hysteresis: vertical down movement tends to snap to ideal offset 10533 final float previousVerticalOffset = mTouchToWindowOffsetY - mLastParentY; 10534 final float currentVerticalOffset = rawY - mPositionY - mLastParentY; 10535 float newVerticalOffset; 10536 if (previousVerticalOffset < mIdealVerticalOffset) { 10537 newVerticalOffset = Math.min(currentVerticalOffset, mIdealVerticalOffset); 10538 newVerticalOffset = Math.max(newVerticalOffset, previousVerticalOffset); 10539 } else { 10540 newVerticalOffset = Math.max(currentVerticalOffset, mIdealVerticalOffset); 10541 newVerticalOffset = Math.min(newVerticalOffset, previousVerticalOffset); 10542 } 10543 mTouchToWindowOffsetY = newVerticalOffset + mLastParentY; 10544 10545 final float newPosX = rawX - mTouchToWindowOffsetX + mHotspotX; 10546 final float newPosY = rawY - mTouchToWindowOffsetY + mTouchOffsetY; 10547 10548 updatePosition(newPosX, newPosY); 10549 break; 10550 } 10551 10552 case MotionEvent.ACTION_UP: 10553 filterOnTouchUp(); 10554 mIsDragging = false; 10555 break; 10556 10557 case MotionEvent.ACTION_CANCEL: 10558 mIsDragging = false; 10559 break; 10560 } 10561 return true; 10562 } 10563 10564 public boolean isDragging() { 10565 return mIsDragging; 10566 } 10567 10568 void onHandleMoved() { 10569 hideActionPopupWindow(); 10570 } 10571 10572 public void onDetached() { 10573 hideActionPopupWindow(); 10574 } 10575 } 10576 10577 private class InsertionHandleView extends HandleView { 10578 private static final int DELAY_BEFORE_HANDLE_FADES_OUT = 4000; 10579 private static final int RECENT_CUT_COPY_DURATION = 15 * 1000; // seconds 10580 10581 // Used to detect taps on the insertion handle, which will affect the ActionPopupWindow 10582 private float mDownPositionX, mDownPositionY; 10583 private Runnable mHider; 10584 10585 public InsertionHandleView(Drawable drawable) { 10586 super(drawable, drawable); 10587 } 10588 10589 @Override 10590 public void show() { 10591 super.show(); 10592 10593 final long durationSinceCutOrCopy = SystemClock.uptimeMillis() - sLastCutOrCopyTime; 10594 if (durationSinceCutOrCopy < RECENT_CUT_COPY_DURATION) { 10595 showActionPopupWindow(0); 10596 } 10597 10598 hideAfterDelay(); 10599 } 10600 10601 public void showWithActionPopup() { 10602 show(); 10603 showActionPopupWindow(0); 10604 } 10605 10606 private void hideAfterDelay() { 10607 removeHiderCallback(); 10608 if (mHider == null) { 10609 mHider = new Runnable() { 10610 public void run() { 10611 hide(); 10612 } 10613 }; 10614 } 10615 TextView.this.postDelayed(mHider, DELAY_BEFORE_HANDLE_FADES_OUT); 10616 } 10617 10618 private void removeHiderCallback() { 10619 if (mHider != null) { 10620 TextView.this.removeCallbacks(mHider); 10621 } 10622 } 10623 10624 @Override 10625 protected int getHotspotX(Drawable drawable, boolean isRtlRun) { 10626 return drawable.getIntrinsicWidth() / 2; 10627 } 10628 10629 @Override 10630 public boolean onTouchEvent(MotionEvent ev) { 10631 final boolean result = super.onTouchEvent(ev); 10632 10633 switch (ev.getActionMasked()) { 10634 case MotionEvent.ACTION_DOWN: 10635 mDownPositionX = ev.getRawX(); 10636 mDownPositionY = ev.getRawY(); 10637 break; 10638 10639 case MotionEvent.ACTION_UP: 10640 if (!offsetHasBeenChanged()) { 10641 final float deltaX = mDownPositionX - ev.getRawX(); 10642 final float deltaY = mDownPositionY - ev.getRawY(); 10643 final float distanceSquared = deltaX * deltaX + deltaY * deltaY; 10644 if (distanceSquared < mSquaredTouchSlopDistance) { 10645 if (mActionPopupWindow != null && mActionPopupWindow.isShowing()) { 10646 // Tapping on the handle dismisses the displayed action popup 10647 mActionPopupWindow.hide(); 10648 } else { 10649 showWithActionPopup(); 10650 } 10651 } 10652 } 10653 hideAfterDelay(); 10654 break; 10655 10656 case MotionEvent.ACTION_CANCEL: 10657 hideAfterDelay(); 10658 break; 10659 10660 default: 10661 break; 10662 } 10663 10664 return result; 10665 } 10666 10667 @Override 10668 public int getCurrentCursorOffset() { 10669 return TextView.this.getSelectionStart(); 10670 } 10671 10672 @Override 10673 public void updateSelection(int offset) { 10674 Selection.setSelection((Spannable) mText, offset); 10675 } 10676 10677 @Override 10678 public void updatePosition(float x, float y) { 10679 positionAtCursorOffset(getOffsetForPosition(x, y), false); 10680 } 10681 10682 @Override 10683 void onHandleMoved() { 10684 super.onHandleMoved(); 10685 removeHiderCallback(); 10686 } 10687 10688 @Override 10689 public void onDetached() { 10690 super.onDetached(); 10691 removeHiderCallback(); 10692 } 10693 } 10694 10695 private class SelectionStartHandleView extends HandleView { 10696 10697 public SelectionStartHandleView(Drawable drawableLtr, Drawable drawableRtl) { 10698 super(drawableLtr, drawableRtl); 10699 } 10700 10701 @Override 10702 protected int getHotspotX(Drawable drawable, boolean isRtlRun) { 10703 if (isRtlRun) { 10704 return drawable.getIntrinsicWidth() / 4; 10705 } else { 10706 return (drawable.getIntrinsicWidth() * 3) / 4; 10707 } 10708 } 10709 10710 @Override 10711 public int getCurrentCursorOffset() { 10712 return TextView.this.getSelectionStart(); 10713 } 10714 10715 @Override 10716 public void updateSelection(int offset) { 10717 super.updateSelection(offset); 10718 Selection.setSelection((Spannable) mText, offset, getSelectionEnd()); 10719 } 10720 10721 @Override 10722 public void updatePosition(float x, float y) { 10723 int offset = getOffsetForPosition(x, y); 10724 10725 // Handles can not cross and selection is at least one character 10726 final int selectionEnd = getSelectionEnd(); 10727 if (offset >= selectionEnd) offset = selectionEnd - 1; 10728 10729 positionAtCursorOffset(offset, false); 10730 } 10731 10732 public ActionPopupWindow getActionPopupWindow() { 10733 return mActionPopupWindow; 10734 } 10735 } 10736 10737 private class SelectionEndHandleView extends HandleView { 10738 10739 public SelectionEndHandleView(Drawable drawableLtr, Drawable drawableRtl) { 10740 super(drawableLtr, drawableRtl); 10741 } 10742 10743 @Override 10744 protected int getHotspotX(Drawable drawable, boolean isRtlRun) { 10745 if (isRtlRun) { 10746 return (drawable.getIntrinsicWidth() * 3) / 4; 10747 } else { 10748 return drawable.getIntrinsicWidth() / 4; 10749 } 10750 } 10751 10752 @Override 10753 public int getCurrentCursorOffset() { 10754 return TextView.this.getSelectionEnd(); 10755 } 10756 10757 @Override 10758 public void updateSelection(int offset) { 10759 super.updateSelection(offset); 10760 Selection.setSelection((Spannable) mText, getSelectionStart(), offset); 10761 } 10762 10763 @Override 10764 public void updatePosition(float x, float y) { 10765 int offset = getOffsetForPosition(x, y); 10766 10767 // Handles can not cross and selection is at least one character 10768 final int selectionStart = getSelectionStart(); 10769 if (offset <= selectionStart) offset = selectionStart + 1; 10770 10771 positionAtCursorOffset(offset, false); 10772 } 10773 10774 public void setActionPopupWindow(ActionPopupWindow actionPopupWindow) { 10775 mActionPopupWindow = actionPopupWindow; 10776 } 10777 } 10778 10779 /** 10780 * A CursorController instance can be used to control a cursor in the text. 10781 * It is not used outside of {@link TextView}. 10782 * @hide 10783 */ 10784 private interface CursorController extends ViewTreeObserver.OnTouchModeChangeListener { 10785 /** 10786 * Makes the cursor controller visible on screen. Will be drawn by {@link #draw(Canvas)}. 10787 * See also {@link #hide()}. 10788 */ 10789 public void show(); 10790 10791 /** 10792 * Hide the cursor controller from screen. 10793 * See also {@link #show()}. 10794 */ 10795 public void hide(); 10796 10797 /** 10798 * Called when the view is detached from window. Perform house keeping task, such as 10799 * stopping Runnable thread that would otherwise keep a reference on the context, thus 10800 * preventing the activity from being recycled. 10801 */ 10802 public void onDetached(); 10803 } 10804 10805 private class InsertionPointCursorController implements CursorController { 10806 private InsertionHandleView mHandle; 10807 10808 public void show() { 10809 getHandle().show(); 10810 } 10811 10812 public void showWithActionPopup() { 10813 getHandle().showWithActionPopup(); 10814 } 10815 10816 public void hide() { 10817 if (mHandle != null) { 10818 mHandle.hide(); 10819 } 10820 } 10821 10822 public void onTouchModeChanged(boolean isInTouchMode) { 10823 if (!isInTouchMode) { 10824 hide(); 10825 } 10826 } 10827 10828 private InsertionHandleView getHandle() { 10829 if (mSelectHandleCenter == null) { 10830 mSelectHandleCenter = mContext.getResources().getDrawable( 10831 mTextSelectHandleRes); 10832 } 10833 if (mHandle == null) { 10834 mHandle = new InsertionHandleView(mSelectHandleCenter); 10835 } 10836 return mHandle; 10837 } 10838 10839 @Override 10840 public void onDetached() { 10841 final ViewTreeObserver observer = getViewTreeObserver(); 10842 observer.removeOnTouchModeChangeListener(this); 10843 10844 if (mHandle != null) mHandle.onDetached(); 10845 } 10846 } 10847 10848 private class SelectionModifierCursorController implements CursorController { 10849 private static final int DELAY_BEFORE_REPLACE_ACTION = 200; // milliseconds 10850 // The cursor controller handles, lazily created when shown. 10851 private SelectionStartHandleView mStartHandle; 10852 private SelectionEndHandleView mEndHandle; 10853 // The offsets of that last touch down event. Remembered to start selection there. 10854 private int mMinTouchOffset, mMaxTouchOffset; 10855 10856 // Double tap detection 10857 private long mPreviousTapUpTime = 0; 10858 private float mPreviousTapPositionX, mPreviousTapPositionY; 10859 10860 SelectionModifierCursorController() { 10861 resetTouchOffsets(); 10862 } 10863 10864 public void show() { 10865 if (isInBatchEditMode()) { 10866 return; 10867 } 10868 initDrawables(); 10869 initHandles(); 10870 hideInsertionPointCursorController(); 10871 } 10872 10873 private void initDrawables() { 10874 if (mSelectHandleLeft == null) { 10875 mSelectHandleLeft = mContext.getResources().getDrawable( 10876 mTextSelectHandleLeftRes); 10877 } 10878 if (mSelectHandleRight == null) { 10879 mSelectHandleRight = mContext.getResources().getDrawable( 10880 mTextSelectHandleRightRes); 10881 } 10882 } 10883 10884 private void initHandles() { 10885 // Lazy object creation has to be done before updatePosition() is called. 10886 if (mStartHandle == null) { 10887 mStartHandle = new SelectionStartHandleView(mSelectHandleLeft, mSelectHandleRight); 10888 } 10889 if (mEndHandle == null) { 10890 mEndHandle = new SelectionEndHandleView(mSelectHandleRight, mSelectHandleLeft); 10891 } 10892 10893 mStartHandle.show(); 10894 mEndHandle.show(); 10895 10896 // Make sure both left and right handles share the same ActionPopupWindow (so that 10897 // moving any of the handles hides the action popup). 10898 mStartHandle.showActionPopupWindow(DELAY_BEFORE_REPLACE_ACTION); 10899 mEndHandle.setActionPopupWindow(mStartHandle.getActionPopupWindow()); 10900 10901 hideInsertionPointCursorController(); 10902 } 10903 10904 public void hide() { 10905 if (mStartHandle != null) mStartHandle.hide(); 10906 if (mEndHandle != null) mEndHandle.hide(); 10907 } 10908 10909 public void onTouchEvent(MotionEvent event) { 10910 // This is done even when the View does not have focus, so that long presses can start 10911 // selection and tap can move cursor from this tap position. 10912 switch (event.getActionMasked()) { 10913 case MotionEvent.ACTION_DOWN: 10914 final float x = event.getX(); 10915 final float y = event.getY(); 10916 10917 // Remember finger down position, to be able to start selection from there 10918 mMinTouchOffset = mMaxTouchOffset = getOffsetForPosition(x, y); 10919 10920 // Double tap detection 10921 long duration = SystemClock.uptimeMillis() - mPreviousTapUpTime; 10922 if (duration <= ViewConfiguration.getDoubleTapTimeout() && 10923 isPositionOnText(x, y)) { 10924 final float deltaX = x - mPreviousTapPositionX; 10925 final float deltaY = y - mPreviousTapPositionY; 10926 final float distanceSquared = deltaX * deltaX + deltaY * deltaY; 10927 if (distanceSquared < mSquaredTouchSlopDistance) { 10928 startSelectionActionMode(); 10929 mDiscardNextActionUp = true; 10930 } 10931 } 10932 10933 mPreviousTapPositionX = x; 10934 mPreviousTapPositionY = y; 10935 break; 10936 10937 case MotionEvent.ACTION_POINTER_DOWN: 10938 case MotionEvent.ACTION_POINTER_UP: 10939 // Handle multi-point gestures. Keep min and max offset positions. 10940 // Only activated for devices that correctly handle multi-touch. 10941 if (mContext.getPackageManager().hasSystemFeature( 10942 PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT)) { 10943 updateMinAndMaxOffsets(event); 10944 } 10945 break; 10946 10947 case MotionEvent.ACTION_UP: 10948 mPreviousTapUpTime = SystemClock.uptimeMillis(); 10949 break; 10950 } 10951 } 10952 10953 /** 10954 * @param event 10955 */ 10956 private void updateMinAndMaxOffsets(MotionEvent event) { 10957 int pointerCount = event.getPointerCount(); 10958 for (int index = 0; index < pointerCount; index++) { 10959 int offset = getOffsetForPosition(event.getX(index), event.getY(index)); 10960 if (offset < mMinTouchOffset) mMinTouchOffset = offset; 10961 if (offset > mMaxTouchOffset) mMaxTouchOffset = offset; 10962 } 10963 } 10964 10965 public int getMinTouchOffset() { 10966 return mMinTouchOffset; 10967 } 10968 10969 public int getMaxTouchOffset() { 10970 return mMaxTouchOffset; 10971 } 10972 10973 public void resetTouchOffsets() { 10974 mMinTouchOffset = mMaxTouchOffset = -1; 10975 } 10976 10977 /** 10978 * @return true iff this controller is currently used to move the selection start. 10979 */ 10980 public boolean isSelectionStartDragged() { 10981 return mStartHandle != null && mStartHandle.isDragging(); 10982 } 10983 10984 public void onTouchModeChanged(boolean isInTouchMode) { 10985 if (!isInTouchMode) { 10986 hide(); 10987 } 10988 } 10989 10990 @Override 10991 public void onDetached() { 10992 final ViewTreeObserver observer = getViewTreeObserver(); 10993 observer.removeOnTouchModeChangeListener(this); 10994 10995 if (mStartHandle != null) mStartHandle.onDetached(); 10996 if (mEndHandle != null) mEndHandle.onDetached(); 10997 } 10998 } 10999 11000 private void hideInsertionPointCursorController() { 11001 // No need to create the controller to hide it. 11002 if (mInsertionPointCursorController != null) { 11003 mInsertionPointCursorController.hide(); 11004 } 11005 } 11006 11007 /** 11008 * Hides the insertion controller and stops text selection mode, hiding the selection controller 11009 */ 11010 private void hideControllers() { 11011 hideCursorControllers(); 11012 hideSpanControllers(); 11013 } 11014 11015 private void hideSpanControllers() { 11016 if (mChangeWatcher != null) { 11017 mChangeWatcher.hideControllers(); 11018 } 11019 } 11020 11021 private void hideCursorControllers() { 11022 hideInsertionPointCursorController(); 11023 stopSelectionActionMode(); 11024 } 11025 11026 /** 11027 * Get the character offset closest to the specified absolute position. A typical use case is to 11028 * pass the result of {@link MotionEvent#getX()} and {@link MotionEvent#getY()} to this method. 11029 * 11030 * @param x The horizontal absolute position of a point on screen 11031 * @param y The vertical absolute position of a point on screen 11032 * @return the character offset for the character whose position is closest to the specified 11033 * position. Returns -1 if there is no layout. 11034 */ 11035 public int getOffsetForPosition(float x, float y) { 11036 if (getLayout() == null) return -1; 11037 final int line = getLineAtCoordinate(y); 11038 final int offset = getOffsetAtCoordinate(line, x); 11039 return offset; 11040 } 11041 11042 private float convertToLocalHorizontalCoordinate(float x) { 11043 x -= getTotalPaddingLeft(); 11044 // Clamp the position to inside of the view. 11045 x = Math.max(0.0f, x); 11046 x = Math.min(getWidth() - getTotalPaddingRight() - 1, x); 11047 x += getScrollX(); 11048 return x; 11049 } 11050 11051 private int getLineAtCoordinate(float y) { 11052 y -= getTotalPaddingTop(); 11053 // Clamp the position to inside of the view. 11054 y = Math.max(0.0f, y); 11055 y = Math.min(getHeight() - getTotalPaddingBottom() - 1, y); 11056 y += getScrollY(); 11057 return getLayout().getLineForVertical((int) y); 11058 } 11059 11060 private int getOffsetAtCoordinate(int line, float x) { 11061 x = convertToLocalHorizontalCoordinate(x); 11062 return getLayout().getOffsetForHorizontal(line, x); 11063 } 11064 11065 /** Returns true if the screen coordinates position (x,y) corresponds to a character displayed 11066 * in the view. Returns false when the position is in the empty space of left/right of text. 11067 */ 11068 private boolean isPositionOnText(float x, float y) { 11069 if (getLayout() == null) return false; 11070 11071 final int line = getLineAtCoordinate(y); 11072 x = convertToLocalHorizontalCoordinate(x); 11073 11074 if (x < getLayout().getLineLeft(line)) return false; 11075 if (x > getLayout().getLineRight(line)) return false; 11076 return true; 11077 } 11078 11079 @Override 11080 public boolean onDragEvent(DragEvent event) { 11081 switch (event.getAction()) { 11082 case DragEvent.ACTION_DRAG_STARTED: 11083 return hasInsertionController(); 11084 11085 case DragEvent.ACTION_DRAG_ENTERED: 11086 TextView.this.requestFocus(); 11087 return true; 11088 11089 case DragEvent.ACTION_DRAG_LOCATION: 11090 final int offset = getOffsetForPosition(event.getX(), event.getY()); 11091 Selection.setSelection((Spannable)mText, offset); 11092 return true; 11093 11094 case DragEvent.ACTION_DROP: 11095 onDrop(event); 11096 return true; 11097 11098 case DragEvent.ACTION_DRAG_ENDED: 11099 case DragEvent.ACTION_DRAG_EXITED: 11100 default: 11101 return true; 11102 } 11103 } 11104 11105 private void onDrop(DragEvent event) { 11106 StringBuilder content = new StringBuilder(""); 11107 ClipData clipData = event.getClipData(); 11108 final int itemCount = clipData.getItemCount(); 11109 for (int i=0; i < itemCount; i++) { 11110 Item item = clipData.getItemAt(i); 11111 content.append(item.coerceToText(TextView.this.mContext)); 11112 } 11113 11114 final int offset = getOffsetForPosition(event.getX(), event.getY()); 11115 11116 Object localState = event.getLocalState(); 11117 DragLocalState dragLocalState = null; 11118 if (localState instanceof DragLocalState) { 11119 dragLocalState = (DragLocalState) localState; 11120 } 11121 boolean dragDropIntoItself = dragLocalState != null && 11122 dragLocalState.sourceTextView == this; 11123 11124 if (dragDropIntoItself) { 11125 if (offset >= dragLocalState.start && offset < dragLocalState.end) { 11126 // A drop inside the original selection discards the drop. 11127 return; 11128 } 11129 } 11130 11131 final int originalLength = mText.length(); 11132 long minMax = prepareSpacesAroundPaste(offset, offset, content); 11133 int min = extractRangeStartFromLong(minMax); 11134 int max = extractRangeEndFromLong(minMax); 11135 11136 Selection.setSelection((Spannable) mText, max); 11137 ((Editable) mText).replace(min, max, content); 11138 11139 if (dragDropIntoItself) { 11140 int dragSourceStart = dragLocalState.start; 11141 int dragSourceEnd = dragLocalState.end; 11142 if (max <= dragSourceStart) { 11143 // Inserting text before selection has shifted positions 11144 final int shift = mText.length() - originalLength; 11145 dragSourceStart += shift; 11146 dragSourceEnd += shift; 11147 } 11148 11149 // Delete original selection 11150 ((Editable) mText).delete(dragSourceStart, dragSourceEnd); 11151 11152 // Make sure we do not leave two adjacent spaces. 11153 if ((dragSourceStart == 0 || 11154 Character.isSpaceChar(mTransformed.charAt(dragSourceStart - 1))) && 11155 (dragSourceStart == mText.length() || 11156 Character.isSpaceChar(mTransformed.charAt(dragSourceStart)))) { 11157 final int pos = dragSourceStart == mText.length() ? 11158 dragSourceStart - 1 : dragSourceStart; 11159 ((Editable) mText).delete(pos, pos + 1); 11160 } 11161 } 11162 } 11163 11164 /** 11165 * @return True if this view supports insertion handles. 11166 */ 11167 boolean hasInsertionController() { 11168 return mInsertionControllerEnabled; 11169 } 11170 11171 /** 11172 * @return True if this view supports selection handles. 11173 */ 11174 boolean hasSelectionController() { 11175 return mSelectionControllerEnabled; 11176 } 11177 11178 InsertionPointCursorController getInsertionController() { 11179 if (!mInsertionControllerEnabled) { 11180 return null; 11181 } 11182 11183 if (mInsertionPointCursorController == null) { 11184 mInsertionPointCursorController = new InsertionPointCursorController(); 11185 11186 final ViewTreeObserver observer = getViewTreeObserver(); 11187 observer.addOnTouchModeChangeListener(mInsertionPointCursorController); 11188 } 11189 11190 return mInsertionPointCursorController; 11191 } 11192 11193 SelectionModifierCursorController getSelectionController() { 11194 if (!mSelectionControllerEnabled) { 11195 return null; 11196 } 11197 11198 if (mSelectionModifierCursorController == null) { 11199 mSelectionModifierCursorController = new SelectionModifierCursorController(); 11200 11201 final ViewTreeObserver observer = getViewTreeObserver(); 11202 observer.addOnTouchModeChangeListener(mSelectionModifierCursorController); 11203 } 11204 11205 return mSelectionModifierCursorController; 11206 } 11207 11208 boolean isInBatchEditMode() { 11209 final InputMethodState ims = mInputMethodState; 11210 if (ims != null) { 11211 return ims.mBatchEditNesting > 0; 11212 } 11213 return mInBatchEditControllers; 11214 } 11215 11216 @Override 11217 protected void resolveTextDirection() { 11218 if (hasPasswordTransformationMethod()) { 11219 mTextDir = TextDirectionHeuristics.LOCALE; 11220 return; 11221 } 11222 11223 // Always need to resolve layout direction first 11224 final boolean defaultIsRtl = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL); 11225 11226 // Then resolve text direction on the parent 11227 super.resolveTextDirection(); 11228 11229 // Now, we can select the heuristic 11230 int textDir = getResolvedTextDirection(); 11231 switch (textDir) { 11232 default: 11233 case TEXT_DIRECTION_FIRST_STRONG: 11234 mTextDir = (defaultIsRtl ? TextDirectionHeuristics.FIRSTSTRONG_RTL : 11235 TextDirectionHeuristics.FIRSTSTRONG_LTR); 11236 break; 11237 case TEXT_DIRECTION_ANY_RTL: 11238 mTextDir = TextDirectionHeuristics.ANYRTL_LTR; 11239 break; 11240 case TEXT_DIRECTION_LTR: 11241 mTextDir = TextDirectionHeuristics.LTR; 11242 break; 11243 case TEXT_DIRECTION_RTL: 11244 mTextDir = TextDirectionHeuristics.RTL; 11245 break; 11246 } 11247 } 11248 11249 /** 11250 * Subclasses will need to override this method to implement their own way of resolving 11251 * drawables depending on the layout direction. 11252 * 11253 * A call to the super method will be required from the subclasses implementation. 11254 * 11255 */ 11256 protected void resolveDrawables() { 11257 // No need to resolve twice 11258 if (mResolvedDrawables) { 11259 return; 11260 } 11261 // No drawable to resolve 11262 if (mDrawables == null) { 11263 return; 11264 } 11265 // No relative drawable to resolve 11266 if (mDrawables.mDrawableStart == null && mDrawables.mDrawableEnd == null) { 11267 mResolvedDrawables = true; 11268 return; 11269 } 11270 11271 Drawables dr = mDrawables; 11272 switch(getResolvedLayoutDirection()) { 11273 case LAYOUT_DIRECTION_RTL: 11274 if (dr.mDrawableStart != null) { 11275 dr.mDrawableRight = dr.mDrawableStart; 11276 11277 dr.mDrawableSizeRight = dr.mDrawableSizeStart; 11278 dr.mDrawableHeightRight = dr.mDrawableHeightStart; 11279 } 11280 if (dr.mDrawableEnd != null) { 11281 dr.mDrawableLeft = dr.mDrawableEnd; 11282 11283 dr.mDrawableSizeLeft = dr.mDrawableSizeEnd; 11284 dr.mDrawableHeightLeft = dr.mDrawableHeightEnd; 11285 } 11286 break; 11287 11288 case LAYOUT_DIRECTION_LTR: 11289 default: 11290 if (dr.mDrawableStart != null) { 11291 dr.mDrawableLeft = dr.mDrawableStart; 11292 11293 dr.mDrawableSizeLeft = dr.mDrawableSizeStart; 11294 dr.mDrawableHeightLeft = dr.mDrawableHeightStart; 11295 } 11296 if (dr.mDrawableEnd != null) { 11297 dr.mDrawableRight = dr.mDrawableEnd; 11298 11299 dr.mDrawableSizeRight = dr.mDrawableSizeEnd; 11300 dr.mDrawableHeightRight = dr.mDrawableHeightEnd; 11301 } 11302 break; 11303 } 11304 mResolvedDrawables = true; 11305 } 11306 11307 protected void resetResolvedDrawables() { 11308 mResolvedDrawables = false; 11309 } 11310 11311 /** 11312 * @hide 11313 */ 11314 protected void viewClicked(InputMethodManager imm) { 11315 if (imm != null) { 11316 imm.viewClicked(this); 11317 } 11318 } 11319 11320 @ViewDebug.ExportedProperty(category = "text") 11321 private CharSequence mText; 11322 private CharSequence mTransformed; 11323 private BufferType mBufferType = BufferType.NORMAL; 11324 11325 private int mInputType = EditorInfo.TYPE_NULL; 11326 private CharSequence mHint; 11327 private Layout mHintLayout; 11328 11329 private KeyListener mInput; 11330 11331 private MovementMethod mMovement; 11332 private TransformationMethod mTransformation; 11333 private boolean mAllowTransformationLengthChange; 11334 private ChangeWatcher mChangeWatcher; 11335 11336 private ArrayList<TextWatcher> mListeners = null; 11337 11338 // display attributes 11339 private final TextPaint mTextPaint; 11340 private boolean mUserSetTextScaleX; 11341 private final Paint mHighlightPaint; 11342 private int mHighlightColor = 0x4C33B5E5; 11343 /** 11344 * This is temporarily visible to fix bug 3085564 in webView. Do not rely on 11345 * this field being protected. Will be restored as private when lineHeight 11346 * feature request 3215097 is implemented 11347 * @hide 11348 */ 11349 protected Layout mLayout; 11350 11351 private long mShowCursor; 11352 private Blink mBlink; 11353 private boolean mCursorVisible = true; 11354 11355 // Cursor Controllers. 11356 private InsertionPointCursorController mInsertionPointCursorController; 11357 private SelectionModifierCursorController mSelectionModifierCursorController; 11358 private ActionMode mSelectionActionMode; 11359 private boolean mInsertionControllerEnabled; 11360 private boolean mSelectionControllerEnabled; 11361 private boolean mInBatchEditControllers; 11362 11363 // These are needed to desambiguate a long click. If the long click comes from ones of these, we 11364 // select from the current cursor position. Otherwise, select from long pressed position. 11365 private boolean mDPadCenterIsDown = false; 11366 private boolean mEnterKeyIsDown = false; 11367 private boolean mContextMenuTriggeredByKey = false; 11368 11369 private boolean mSelectAllOnFocus = false; 11370 11371 private int mGravity = Gravity.TOP | Gravity.START; 11372 private boolean mHorizontallyScrolling; 11373 11374 private int mAutoLinkMask; 11375 private boolean mLinksClickable = true; 11376 11377 private float mSpacingMult = 1.0f; 11378 private float mSpacingAdd = 0.0f; 11379 private boolean mTextIsSelectable = false; 11380 11381 private static final int LINES = 1; 11382 private static final int EMS = LINES; 11383 private static final int PIXELS = 2; 11384 11385 private int mMaximum = Integer.MAX_VALUE; 11386 private int mMaxMode = LINES; 11387 private int mMinimum = 0; 11388 private int mMinMode = LINES; 11389 11390 private int mOldMaximum = mMaximum; 11391 private int mOldMaxMode = mMaxMode; 11392 11393 private int mMaxWidth = Integer.MAX_VALUE; 11394 private int mMaxWidthMode = PIXELS; 11395 private int mMinWidth = 0; 11396 private int mMinWidthMode = PIXELS; 11397 11398 private boolean mSingleLine; 11399 private int mDesiredHeightAtMeasure = -1; 11400 private boolean mIncludePad = true; 11401 11402 // tmp primitives, so we don't alloc them on each draw 11403 private Path mHighlightPath; 11404 private boolean mHighlightPathBogus = true; 11405 private static final RectF sTempRect = new RectF(); 11406 11407 // XXX should be much larger 11408 private static final int VERY_WIDE = 16384; 11409 11410 private static final int BLINK = 500; 11411 11412 private static final int ANIMATED_SCROLL_GAP = 250; 11413 private long mLastScroll; 11414 private Scroller mScroller = null; 11415 11416 private BoringLayout.Metrics mBoring; 11417 private BoringLayout.Metrics mHintBoring; 11418 11419 private BoringLayout mSavedLayout, mSavedHintLayout; 11420 11421 private TextDirectionHeuristic mTextDir = null; 11422 11423 private static final InputFilter[] NO_FILTERS = new InputFilter[0]; 11424 private InputFilter[] mFilters = NO_FILTERS; 11425 private static final Spanned EMPTY_SPANNED = new SpannedString(""); 11426 private static int DRAG_SHADOW_MAX_TEXT_LENGTH = 20; 11427 // System wide time for last cut or copy action. 11428 private static long sLastCutOrCopyTime; 11429 // Used to highlight a word when it is corrected by the IME 11430 private CorrectionHighlighter mCorrectionHighlighter; 11431 // New state used to change background based on whether this TextView is multiline. 11432 private static final int[] MULTILINE_STATE_SET = { R.attr.state_multiline }; 11433} 11434