The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package android.widget; |
| 18 | |
Gilles Debunne | 78996c9 | 2010-10-12 16:01:47 -0700 | [diff] [blame] | 19 | import android.R; |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 20 | import android.content.ClipData; |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 21 | import android.content.ClipData.Item; |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 22 | import android.content.ClipboardManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.Context; |
Gilles Debunne | e90bed1 | 2011-08-30 14:28:27 -0700 | [diff] [blame] | 24 | import android.content.Intent; |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.content.res.ColorStateList; |
Christopher Tate | 1373a8e | 2011-11-10 19:59:13 -0800 | [diff] [blame] | 27 | import android.content.res.CompatibilityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.content.res.Resources; |
| 29 | import android.content.res.TypedArray; |
| 30 | import android.content.res.XmlResourceParser; |
| 31 | import android.graphics.Canvas; |
Gilles Debunne | 12d91ce | 2010-12-10 11:36:29 -0800 | [diff] [blame] | 32 | import android.graphics.Color; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.graphics.Paint; |
| 34 | import android.graphics.Path; |
| 35 | import android.graphics.Rect; |
| 36 | import android.graphics.RectF; |
| 37 | import android.graphics.Typeface; |
| 38 | import android.graphics.drawable.Drawable; |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 39 | import android.inputmethodservice.ExtractEditText; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.os.Bundle; |
| 41 | import android.os.Handler; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 42 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.os.Parcel; |
| 44 | import android.os.Parcelable; |
| 45 | import android.os.SystemClock; |
Gilles Debunne | e90bed1 | 2011-08-30 14:28:27 -0700 | [diff] [blame] | 46 | import android.provider.Settings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.text.BoringLayout; |
| 48 | import android.text.DynamicLayout; |
| 49 | import android.text.Editable; |
| 50 | import android.text.GetChars; |
| 51 | import android.text.GraphicsOperations; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.text.InputFilter; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 53 | import android.text.InputType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.text.Layout; |
| 55 | import android.text.ParcelableSpan; |
| 56 | import android.text.Selection; |
| 57 | import android.text.SpanWatcher; |
| 58 | import android.text.Spannable; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 59 | import android.text.SpannableString; |
Gilles Debunne | 214a862 | 2011-04-26 15:44:37 -0700 | [diff] [blame] | 60 | import android.text.SpannableStringBuilder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import android.text.Spanned; |
| 62 | import android.text.SpannedString; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.text.StaticLayout; |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 64 | import android.text.TextDirectionHeuristic; |
| 65 | import android.text.TextDirectionHeuristics; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import android.text.TextPaint; |
| 67 | import android.text.TextUtils; |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 68 | import android.text.TextUtils.TruncateAt; |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 69 | import android.text.TextWatcher; |
Adam Powell | 7f8f79a | 2011-07-07 18:35:54 -0700 | [diff] [blame] | 70 | import android.text.method.AllCapsTransformationMethod; |
Gilles Debunne | 86b9c78 | 2010-11-11 10:43:48 -0800 | [diff] [blame] | 71 | import android.text.method.ArrowKeyMovementMethod; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | import android.text.method.DateKeyListener; |
| 73 | import android.text.method.DateTimeKeyListener; |
| 74 | import android.text.method.DialerKeyListener; |
| 75 | import android.text.method.DigitsKeyListener; |
| 76 | import android.text.method.KeyListener; |
| 77 | import android.text.method.LinkMovementMethod; |
| 78 | import android.text.method.MetaKeyKeyListener; |
| 79 | import android.text.method.MovementMethod; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import android.text.method.PasswordTransformationMethod; |
| 81 | import android.text.method.SingleLineTransformationMethod; |
| 82 | import android.text.method.TextKeyListener; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 83 | import android.text.method.TimeKeyListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.text.method.TransformationMethod; |
Adam Powell | 7f8f79a | 2011-07-07 18:35:54 -0700 | [diff] [blame] | 85 | import android.text.method.TransformationMethod2; |
Gilles Debunne | 214a862 | 2011-04-26 15:44:37 -0700 | [diff] [blame] | 86 | import android.text.method.WordIterator; |
Gilles Debunne | f3895ed | 2010-12-21 12:53:58 -0800 | [diff] [blame] | 87 | import android.text.style.ClickableSpan; |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 88 | import android.text.style.EasyEditSpan; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | import android.text.style.ParagraphStyle; |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 90 | import android.text.style.SpellCheckSpan; |
Gilles Debunne | 28294cc | 2011-08-24 12:02:05 -0700 | [diff] [blame] | 91 | import android.text.style.SuggestionRangeSpan; |
Gilles Debunne | 2037b82 | 2011-04-22 13:07:33 -0700 | [diff] [blame] | 92 | import android.text.style.SuggestionSpan; |
Gilles Debunne | 214a862 | 2011-04-26 15:44:37 -0700 | [diff] [blame] | 93 | import android.text.style.TextAppearanceSpan; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | import android.text.style.URLSpan; |
| 95 | import android.text.style.UpdateAppearance; |
| 96 | import android.text.util.Linkify; |
| 97 | import android.util.AttributeSet; |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 98 | import android.util.DisplayMetrics; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | import android.util.FloatMath; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 100 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | import android.util.TypedValue; |
Gilles Debunne | 27113f8 | 2010-08-23 12:09:14 -0700 | [diff] [blame] | 102 | import android.view.ActionMode; |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 103 | import android.view.ActionMode.Callback; |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 104 | import android.view.DragEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | import android.view.Gravity; |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 106 | import android.view.HapticFeedbackConstants; |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 107 | import android.view.KeyCharacterMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | import android.view.KeyEvent; |
| 109 | import android.view.LayoutInflater; |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 110 | import android.view.Menu; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | import android.view.MenuItem; |
| 112 | import android.view.MotionEvent; |
| 113 | import android.view.View; |
Gilles Debunne | 65f6041 | 2010-10-15 16:18:35 -0700 | [diff] [blame] | 114 | import android.view.ViewConfiguration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | import android.view.ViewDebug; |
Adam Powell | 8c8293b | 2010-10-12 14:45:12 -0700 | [diff] [blame] | 116 | import android.view.ViewGroup; |
Gilles Debunne | 27113f8 | 2010-08-23 12:09:14 -0700 | [diff] [blame] | 117 | import android.view.ViewGroup.LayoutParams; |
Adam Powell | abcbb1a | 2010-10-04 21:12:19 -0700 | [diff] [blame] | 118 | import android.view.ViewParent; |
Gilles Debunne | 3784a7f | 2011-07-15 13:49:38 -0700 | [diff] [blame] | 119 | import android.view.ViewRootImpl; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | import android.view.ViewTreeObserver; |
Gilles Debunne | cbfbb52 | 2010-10-07 16:57:31 -0700 | [diff] [blame] | 121 | import android.view.WindowManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 122 | import android.view.accessibility.AccessibilityEvent; |
| 123 | import android.view.accessibility.AccessibilityManager; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 124 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | import android.view.animation.AnimationUtils; |
| 126 | import android.view.inputmethod.BaseInputConnection; |
| 127 | import android.view.inputmethod.CompletionInfo; |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 128 | import android.view.inputmethod.CorrectionInfo; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 129 | import android.view.inputmethod.EditorInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | import android.view.inputmethod.ExtractedText; |
| 131 | import android.view.inputmethod.ExtractedTextRequest; |
| 132 | import android.view.inputmethod.InputConnection; |
| 133 | import android.view.inputmethod.InputMethodManager; |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 134 | import android.view.textservice.SpellCheckerSubtype; |
| 135 | import android.view.textservice.TextServicesManager; |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 136 | import android.widget.AdapterView.OnItemClickListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | import android.widget.RemoteViews.RemoteView; |
| 138 | |
Gilles Debunne | 2237829 | 2011-08-12 10:38:52 -0700 | [diff] [blame] | 139 | import com.android.internal.util.FastMath; |
| 140 | import com.android.internal.widget.EditableInputConnection; |
| 141 | |
| 142 | import org.xmlpull.v1.XmlPullParserException; |
| 143 | |
Gilles Debunne | 27113f8 | 2010-08-23 12:09:14 -0700 | [diff] [blame] | 144 | import java.io.IOException; |
| 145 | import java.lang.ref.WeakReference; |
Gilles Debunne | 214a862 | 2011-04-26 15:44:37 -0700 | [diff] [blame] | 146 | import java.text.BreakIterator; |
Gilles Debunne | 27113f8 | 2010-08-23 12:09:14 -0700 | [diff] [blame] | 147 | import java.util.ArrayList; |
Luca Zanolin | e3f89c0 | 2011-08-01 09:55:17 +0100 | [diff] [blame] | 148 | import java.util.Arrays; |
| 149 | import java.util.Comparator; |
| 150 | import java.util.HashMap; |
Gilles Debunne | 9d8d3f1 | 2011-10-13 12:15:10 -0700 | [diff] [blame] | 151 | import java.util.Locale; |
Gilles Debunne | 27113f8 | 2010-08-23 12:09:14 -0700 | [diff] [blame] | 152 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | /** |
| 154 | * Displays text to the user and optionally allows them to edit it. A TextView |
| 155 | * is a complete text editor, however the basic class is configured to not |
| 156 | * allow editing; see {@link EditText} for a subclass that configures the text |
| 157 | * view for editing. |
| 158 | * |
| 159 | * <p> |
| 160 | * <b>XML attributes</b> |
| 161 | * <p> |
| 162 | * See {@link android.R.styleable#TextView TextView Attributes}, |
| 163 | * {@link android.R.styleable#View View Attributes} |
| 164 | * |
| 165 | * @attr ref android.R.styleable#TextView_text |
| 166 | * @attr ref android.R.styleable#TextView_bufferType |
| 167 | * @attr ref android.R.styleable#TextView_hint |
| 168 | * @attr ref android.R.styleable#TextView_textColor |
| 169 | * @attr ref android.R.styleable#TextView_textColorHighlight |
| 170 | * @attr ref android.R.styleable#TextView_textColorHint |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 171 | * @attr ref android.R.styleable#TextView_textAppearance |
| 172 | * @attr ref android.R.styleable#TextView_textColorLink |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | * @attr ref android.R.styleable#TextView_textSize |
| 174 | * @attr ref android.R.styleable#TextView_textScaleX |
| 175 | * @attr ref android.R.styleable#TextView_typeface |
| 176 | * @attr ref android.R.styleable#TextView_textStyle |
| 177 | * @attr ref android.R.styleable#TextView_cursorVisible |
| 178 | * @attr ref android.R.styleable#TextView_maxLines |
| 179 | * @attr ref android.R.styleable#TextView_maxHeight |
| 180 | * @attr ref android.R.styleable#TextView_lines |
| 181 | * @attr ref android.R.styleable#TextView_height |
| 182 | * @attr ref android.R.styleable#TextView_minLines |
| 183 | * @attr ref android.R.styleable#TextView_minHeight |
| 184 | * @attr ref android.R.styleable#TextView_maxEms |
| 185 | * @attr ref android.R.styleable#TextView_maxWidth |
| 186 | * @attr ref android.R.styleable#TextView_ems |
| 187 | * @attr ref android.R.styleable#TextView_width |
| 188 | * @attr ref android.R.styleable#TextView_minEms |
| 189 | * @attr ref android.R.styleable#TextView_minWidth |
| 190 | * @attr ref android.R.styleable#TextView_gravity |
| 191 | * @attr ref android.R.styleable#TextView_scrollHorizontally |
| 192 | * @attr ref android.R.styleable#TextView_password |
| 193 | * @attr ref android.R.styleable#TextView_singleLine |
| 194 | * @attr ref android.R.styleable#TextView_selectAllOnFocus |
| 195 | * @attr ref android.R.styleable#TextView_includeFontPadding |
| 196 | * @attr ref android.R.styleable#TextView_maxLength |
| 197 | * @attr ref android.R.styleable#TextView_shadowColor |
| 198 | * @attr ref android.R.styleable#TextView_shadowDx |
| 199 | * @attr ref android.R.styleable#TextView_shadowDy |
| 200 | * @attr ref android.R.styleable#TextView_shadowRadius |
| 201 | * @attr ref android.R.styleable#TextView_autoLink |
| 202 | * @attr ref android.R.styleable#TextView_linksClickable |
| 203 | * @attr ref android.R.styleable#TextView_numeric |
| 204 | * @attr ref android.R.styleable#TextView_digits |
| 205 | * @attr ref android.R.styleable#TextView_phoneNumber |
| 206 | * @attr ref android.R.styleable#TextView_inputMethod |
| 207 | * @attr ref android.R.styleable#TextView_capitalize |
| 208 | * @attr ref android.R.styleable#TextView_autoText |
| 209 | * @attr ref android.R.styleable#TextView_editable |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 210 | * @attr ref android.R.styleable#TextView_freezesText |
| 211 | * @attr ref android.R.styleable#TextView_ellipsize |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | * @attr ref android.R.styleable#TextView_drawableTop |
| 213 | * @attr ref android.R.styleable#TextView_drawableBottom |
| 214 | * @attr ref android.R.styleable#TextView_drawableRight |
| 215 | * @attr ref android.R.styleable#TextView_drawableLeft |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 216 | * @attr ref android.R.styleable#TextView_drawablePadding |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | * @attr ref android.R.styleable#TextView_lineSpacingExtra |
| 218 | * @attr ref android.R.styleable#TextView_lineSpacingMultiplier |
| 219 | * @attr ref android.R.styleable#TextView_marqueeRepeatLimit |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 220 | * @attr ref android.R.styleable#TextView_inputType |
| 221 | * @attr ref android.R.styleable#TextView_imeOptions |
| 222 | * @attr ref android.R.styleable#TextView_privateImeOptions |
| 223 | * @attr ref android.R.styleable#TextView_imeActionLabel |
| 224 | * @attr ref android.R.styleable#TextView_imeActionId |
| 225 | * @attr ref android.R.styleable#TextView_editorExtras |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | */ |
| 227 | @RemoteView |
| 228 | public class TextView extends View implements ViewTreeObserver.OnPreDrawListener { |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 229 | static final String LOG_TAG = "TextView"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | static final boolean DEBUG_EXTRACT = false; |
Gilles Debunne | b7012e84 | 2011-02-24 15:40:38 -0800 | [diff] [blame] | 231 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 232 | private static final int PRIORITY = 100; |
Gilles Debunne | 3dbf55c | 2010-12-16 10:31:51 -0800 | [diff] [blame] | 233 | private int mCurrentAlpha = 255; |
Adam Powell | 879fb6b | 2010-09-20 11:23:56 -0700 | [diff] [blame] | 234 | |
Adam Powell | abcbb1a | 2010-10-04 21:12:19 -0700 | [diff] [blame] | 235 | final int[] mTempCoords = new int[2]; |
| 236 | Rect mTempRect; |
Adam Powell | 879fb6b | 2010-09-20 11:23:56 -0700 | [diff] [blame] | 237 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | private ColorStateList mTextColor; |
| 239 | private int mCurTextColor; |
| 240 | private ColorStateList mHintTextColor; |
| 241 | private ColorStateList mLinkTextColor; |
| 242 | private int mCurHintTextColor; |
| 243 | private boolean mFreezesText; |
| 244 | private boolean mFrozenWithFocus; |
| 245 | private boolean mTemporaryDetach; |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 246 | private boolean mDispatchTemporaryDetach; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | |
Gilles Debunne | 0eb704c | 2010-11-30 12:50:54 -0800 | [diff] [blame] | 248 | private boolean mDiscardNextActionUp = false; |
| 249 | private boolean mIgnoreActionUpEvent = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | |
| 251 | private Editable.Factory mEditableFactory = Editable.Factory.getInstance(); |
| 252 | private Spannable.Factory mSpannableFactory = Spannable.Factory.getInstance(); |
| 253 | |
| 254 | private float mShadowRadius, mShadowDx, mShadowDy; |
| 255 | |
| 256 | private static final int PREDRAW_NOT_REGISTERED = 0; |
| 257 | private static final int PREDRAW_PENDING = 1; |
| 258 | private static final int PREDRAW_DONE = 2; |
| 259 | private int mPreDrawState = PREDRAW_NOT_REGISTERED; |
| 260 | |
| 261 | private TextUtils.TruncateAt mEllipsize = null; |
| 262 | |
| 263 | // Enum for the "typeface" XML parameter. |
| 264 | // TODO: How can we get this from the XML instead of hardcoding it here? |
| 265 | private static final int SANS = 1; |
| 266 | private static final int SERIF = 2; |
| 267 | private static final int MONOSPACE = 3; |
| 268 | |
| 269 | // Bitfield for the "numeric" XML parameter. |
| 270 | // TODO: How can we get this from the XML instead of hardcoding it here? |
| 271 | private static final int SIGNED = 2; |
| 272 | private static final int DECIMAL = 4; |
| 273 | |
Dianne Hackborn | 0500b3c | 2011-11-01 15:28:43 -0700 | [diff] [blame] | 274 | static class Drawables { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | final Rect mCompoundRect = new Rect(); |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 276 | Drawable mDrawableTop, mDrawableBottom, mDrawableLeft, mDrawableRight, |
| 277 | mDrawableStart, mDrawableEnd; |
| 278 | int mDrawableSizeTop, mDrawableSizeBottom, mDrawableSizeLeft, mDrawableSizeRight, |
| 279 | mDrawableSizeStart, mDrawableSizeEnd; |
| 280 | int mDrawableWidthTop, mDrawableWidthBottom, mDrawableHeightLeft, mDrawableHeightRight, |
| 281 | mDrawableHeightStart, mDrawableHeightEnd; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | int mDrawablePadding; |
| 283 | } |
| 284 | private Drawables mDrawables; |
| 285 | |
| 286 | private CharSequence mError; |
| 287 | private boolean mErrorWasChanged; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 288 | private ErrorPopup mPopup; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | /** |
| 290 | * This flag is set if the TextView tries to display an error before it |
| 291 | * is attached to the window (so its position is still unknown). |
| 292 | * It causes the error to be shown later, when onAttachedToWindow() |
| 293 | * is called. |
| 294 | */ |
| 295 | private boolean mShowErrorAfterAttach; |
| 296 | |
| 297 | private CharWrapper mCharWrapper = null; |
| 298 | |
| 299 | private boolean mSelectionMoved = false; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 300 | private boolean mTouchFocusSelected = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | |
| 302 | private Marquee mMarquee; |
| 303 | private boolean mRestartMarquee; |
| 304 | |
| 305 | private int mMarqueeRepeatLimit = 3; |
| 306 | |
Dianne Hackborn | 0500b3c | 2011-11-01 15:28:43 -0700 | [diff] [blame] | 307 | static class InputContentType { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 308 | int imeOptions = EditorInfo.IME_NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | String privateImeOptions; |
| 310 | CharSequence imeActionLabel; |
| 311 | int imeActionId; |
| 312 | Bundle extras; |
| 313 | OnEditorActionListener onEditorActionListener; |
| 314 | boolean enterDown; |
| 315 | } |
| 316 | InputContentType mInputContentType; |
| 317 | |
Dianne Hackborn | 0500b3c | 2011-11-01 15:28:43 -0700 | [diff] [blame] | 318 | static class InputMethodState { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | Rect mCursorRectInWindow = new Rect(); |
| 320 | RectF mTmpRectF = new RectF(); |
| 321 | float[] mTmpOffset = new float[2]; |
| 322 | ExtractedTextRequest mExtracting; |
| 323 | final ExtractedText mTmpExtracted = new ExtractedText(); |
| 324 | int mBatchEditNesting; |
| 325 | boolean mCursorChanged; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 326 | boolean mSelectionModeChanged; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | boolean mContentChanged; |
| 328 | int mChangedStart, mChangedEnd, mChangedDelta; |
| 329 | } |
| 330 | InputMethodState mInputMethodState; |
| 331 | |
Gilles Debunne | f75c97e | 2011-02-10 16:09:53 -0800 | [diff] [blame] | 332 | private int mTextSelectHandleLeftRes; |
| 333 | private int mTextSelectHandleRightRes; |
| 334 | private int mTextSelectHandleRes; |
Adam Powell | fbb3b47 | 2010-10-06 21:04:35 -0700 | [diff] [blame] | 335 | |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 336 | private int mTextEditSuggestionItemLayout; |
| 337 | private SuggestionsPopupWindow mSuggestionsPopupWindow; |
Gilles Debunne | 214a862 | 2011-04-26 15:44:37 -0700 | [diff] [blame] | 338 | private SuggestionRangeSpan mSuggestionRangeSpan; |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 339 | |
Gilles Debunne | f75c97e | 2011-02-10 16:09:53 -0800 | [diff] [blame] | 340 | private int mCursorDrawableRes; |
| 341 | private final Drawable[] mCursorDrawable = new Drawable[2]; |
| 342 | private int mCursorCount; // Actual current number of used mCursorDrawable: 0, 1 or 2 |
| 343 | |
| 344 | private Drawable mSelectHandleLeft; |
| 345 | private Drawable mSelectHandleRight; |
| 346 | private Drawable mSelectHandleCenter; |
Adam Powell | b08013c | 2010-09-16 16:28:11 -0700 | [diff] [blame] | 347 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 348 | // Global listener that detects changes in the global position of the TextView |
| 349 | private PositionListener mPositionListener; |
| 350 | |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 351 | private float mLastDownPositionX, mLastDownPositionY; |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 352 | private Callback mCustomSelectionActionModeCallback; |
Gilles Debunne | 9948ad7 | 2010-11-24 14:00:46 -0800 | [diff] [blame] | 353 | |
Gilles Debunne | aa85a4c | 2010-12-06 18:27:17 -0800 | [diff] [blame] | 354 | private final int mSquaredTouchSlopDistance; |
Gilles Debunne | c01f3fe | 2010-12-22 17:07:36 -0800 | [diff] [blame] | 355 | // Set when this TextView gained focus with some text selected. Will start selection mode. |
| 356 | private boolean mCreatedWithASelection = false; |
Gilles Debunne | aa85a4c | 2010-12-06 18:27:17 -0800 | [diff] [blame] | 357 | |
Gilles Debunne | c59269f | 2011-04-22 11:46:09 -0700 | [diff] [blame] | 358 | private WordIterator mWordIterator; |
| 359 | |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 360 | private SpellChecker mSpellChecker; |
| 361 | |
Gilles Debunne | 0f4109e | 2011-10-19 11:26:21 -0700 | [diff] [blame] | 362 | private boolean mSoftInputShownOnFocus = true; |
Gilles Debunne | 550efbf | 2011-10-10 16:49:02 -0700 | [diff] [blame] | 363 | |
Doug Felt | c0ccf0c | 2011-06-23 16:13:18 -0700 | [diff] [blame] | 364 | // The alignment to pass to Layout, or null if not resolved. |
| 365 | private Layout.Alignment mLayoutAlignment; |
| 366 | |
| 367 | // The default value for mTextAlign. |
| 368 | private TextAlign mTextAlign = TextAlign.INHERIT; |
| 369 | |
| 370 | private static enum TextAlign { |
| 371 | INHERIT, GRAVITY, TEXT_START, TEXT_END, CENTER, VIEW_START, VIEW_END; |
| 372 | } |
| 373 | |
Fabrice Di Meglio | f66fdad | 2011-07-19 13:08:47 -0700 | [diff] [blame] | 374 | private boolean mResolvedDrawables = false; |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 375 | |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 376 | /** |
| 377 | * On some devices the fading edges add a performance penalty if used |
| 378 | * extensively in the same layout. This mode indicates how the marquee |
| 379 | * is currently being shown, if applicable. (mEllipsize will == MARQUEE) |
| 380 | */ |
| 381 | private int mMarqueeFadeMode = MARQUEE_FADE_NORMAL; |
| 382 | |
| 383 | /** |
| 384 | * When mMarqueeFadeMode is not MARQUEE_FADE_NORMAL, this stores |
| 385 | * the layout that should be used when the mode switches. |
| 386 | */ |
| 387 | private Layout mSavedMarqueeModeLayout; |
| 388 | |
| 389 | /** |
| 390 | * Draw marquee text with fading edges as usual |
| 391 | */ |
| 392 | private static final int MARQUEE_FADE_NORMAL = 0; |
| 393 | |
| 394 | /** |
| 395 | * Draw marquee text as ellipsize end while inactive instead of with the fade. |
| 396 | * (Useful for devices where the fade can be expensive if overdone) |
| 397 | */ |
| 398 | private static final int MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS = 1; |
| 399 | |
| 400 | /** |
| 401 | * Draw marquee text with fading edges because it is currently active/animating. |
| 402 | */ |
| 403 | private static final int MARQUEE_FADE_SWITCH_SHOW_FADE = 2; |
| 404 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | /* |
| 406 | * Kick-start the font cache for the zygote process (to pay the cost of |
| 407 | * initializing freetype for our default font only once). |
| 408 | */ |
| 409 | static { |
| 410 | Paint p = new Paint(); |
| 411 | p.setAntiAlias(true); |
| 412 | // We don't care about the result, just the side-effect of measuring. |
| 413 | p.measureText("H"); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Interface definition for a callback to be invoked when an action is |
| 418 | * performed on the editor. |
| 419 | */ |
| 420 | public interface OnEditorActionListener { |
| 421 | /** |
| 422 | * Called when an action is being performed. |
| 423 | * |
| 424 | * @param v The view that was clicked. |
| 425 | * @param actionId Identifier of the action. This will be either the |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 426 | * identifier you supplied, or {@link EditorInfo#IME_NULL |
| 427 | * EditorInfo.IME_NULL} if being called due to the enter key |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | * being pressed. |
| 429 | * @param event If triggered by an enter key, this is the event; |
| 430 | * otherwise, this is null. |
| 431 | * @return Return true if you have consumed the action, else false. |
| 432 | */ |
| 433 | boolean onEditorAction(TextView v, int actionId, KeyEvent event); |
| 434 | } |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 435 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | public TextView(Context context) { |
| 437 | this(context, null); |
| 438 | } |
| 439 | |
| 440 | public TextView(Context context, |
| 441 | AttributeSet attrs) { |
| 442 | this(context, attrs, com.android.internal.R.attr.textViewStyle); |
| 443 | } |
| 444 | |
Gilles Debunne | e15b358 | 2010-06-16 15:17:21 -0700 | [diff] [blame] | 445 | @SuppressWarnings("deprecation") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | public TextView(Context context, |
| 447 | AttributeSet attrs, |
| 448 | int defStyle) { |
| 449 | super(context, attrs, defStyle); |
| 450 | mText = ""; |
| 451 | |
Christopher Tate | 1373a8e | 2011-11-10 19:59:13 -0800 | [diff] [blame] | 452 | final Resources res = getResources(); |
| 453 | final CompatibilityInfo compat = res.getCompatibilityInfo(); |
| 454 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); |
Christopher Tate | 1373a8e | 2011-11-10 19:59:13 -0800 | [diff] [blame] | 456 | mTextPaint.density = res.getDisplayMetrics().density; |
| 457 | mTextPaint.setCompatibilityScaling(compat.applicationScale); |
Gilles Debunne | 8cbb4c6 | 2011-01-24 12:33:56 -0800 | [diff] [blame] | 458 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | // If we get the paint from the skin, we should set it to left, since |
| 460 | // the layout always wants it to be left. |
| 461 | // mTextPaint.setTextAlign(Paint.Align.LEFT); |
| 462 | |
| 463 | mHighlightPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
Christopher Tate | 1373a8e | 2011-11-10 19:59:13 -0800 | [diff] [blame] | 464 | mHighlightPaint.setCompatibilityScaling(compat.applicationScale); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | |
| 466 | mMovement = getDefaultMovementMethod(); |
| 467 | mTransformation = null; |
| 468 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | int textColorHighlight = 0; |
| 470 | ColorStateList textColor = null; |
| 471 | ColorStateList textColorHint = null; |
| 472 | ColorStateList textColorLink = null; |
| 473 | int textSize = 15; |
| 474 | int typefaceIndex = -1; |
| 475 | int styleIndex = -1; |
Adam Powell | 7f8f79a | 2011-07-07 18:35:54 -0700 | [diff] [blame] | 476 | boolean allCaps = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | |
Dianne Hackborn | ab0f485 | 2011-09-12 16:59:06 -0700 | [diff] [blame] | 478 | final Resources.Theme theme = context.getTheme(); |
| 479 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | /* |
| 481 | * Look the appearance up without checking first if it exists because |
| 482 | * almost every TextView has one and it greatly simplifies the logic |
| 483 | * to be able to parse the appearance first and then let specific tags |
| 484 | * for this View override it. |
| 485 | */ |
Dianne Hackborn | ab0f485 | 2011-09-12 16:59:06 -0700 | [diff] [blame] | 486 | TypedArray a = theme.obtainStyledAttributes( |
| 487 | attrs, com.android.internal.R.styleable.TextViewAppearance, defStyle, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | TypedArray appearance = null; |
Dianne Hackborn | ab0f485 | 2011-09-12 16:59:06 -0700 | [diff] [blame] | 489 | int ap = a.getResourceId( |
| 490 | com.android.internal.R.styleable.TextViewAppearance_textAppearance, -1); |
| 491 | a.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | if (ap != -1) { |
Dianne Hackborn | ab0f485 | 2011-09-12 16:59:06 -0700 | [diff] [blame] | 493 | appearance = theme.obtainStyledAttributes( |
| 494 | ap, com.android.internal.R.styleable.TextAppearance); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | } |
| 496 | if (appearance != null) { |
| 497 | int n = appearance.getIndexCount(); |
| 498 | for (int i = 0; i < n; i++) { |
| 499 | int attr = appearance.getIndex(i); |
| 500 | |
| 501 | switch (attr) { |
| 502 | case com.android.internal.R.styleable.TextAppearance_textColorHighlight: |
| 503 | textColorHighlight = appearance.getColor(attr, textColorHighlight); |
| 504 | break; |
| 505 | |
| 506 | case com.android.internal.R.styleable.TextAppearance_textColor: |
| 507 | textColor = appearance.getColorStateList(attr); |
| 508 | break; |
| 509 | |
| 510 | case com.android.internal.R.styleable.TextAppearance_textColorHint: |
| 511 | textColorHint = appearance.getColorStateList(attr); |
| 512 | break; |
| 513 | |
| 514 | case com.android.internal.R.styleable.TextAppearance_textColorLink: |
| 515 | textColorLink = appearance.getColorStateList(attr); |
| 516 | break; |
| 517 | |
| 518 | case com.android.internal.R.styleable.TextAppearance_textSize: |
| 519 | textSize = appearance.getDimensionPixelSize(attr, textSize); |
| 520 | break; |
| 521 | |
| 522 | case com.android.internal.R.styleable.TextAppearance_typeface: |
| 523 | typefaceIndex = appearance.getInt(attr, -1); |
| 524 | break; |
| 525 | |
| 526 | case com.android.internal.R.styleable.TextAppearance_textStyle: |
| 527 | styleIndex = appearance.getInt(attr, -1); |
| 528 | break; |
Adam Powell | 7f8f79a | 2011-07-07 18:35:54 -0700 | [diff] [blame] | 529 | |
| 530 | case com.android.internal.R.styleable.TextAppearance_textAllCaps: |
| 531 | allCaps = appearance.getBoolean(attr, false); |
| 532 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
| 536 | appearance.recycle(); |
| 537 | } |
| 538 | |
| 539 | boolean editable = getDefaultEditable(); |
| 540 | CharSequence inputMethod = null; |
| 541 | int numeric = 0; |
| 542 | CharSequence digits = null; |
| 543 | boolean phone = false; |
| 544 | boolean autotext = false; |
| 545 | int autocap = -1; |
| 546 | int buffertype = 0; |
| 547 | boolean selectallonfocus = false; |
| 548 | Drawable drawableLeft = null, drawableTop = null, drawableRight = null, |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 549 | drawableBottom = null, drawableStart = null, drawableEnd = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 550 | int drawablePadding = 0; |
| 551 | int ellipsize = -1; |
Gilles Debunne | f95449d | 2010-11-05 13:54:13 -0700 | [diff] [blame] | 552 | boolean singleLine = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | int maxlength = -1; |
| 554 | CharSequence text = ""; |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 555 | CharSequence hint = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 | int shadowcolor = 0; |
| 557 | float dx = 0, dy = 0, r = 0; |
| 558 | boolean password = false; |
| 559 | int inputType = EditorInfo.TYPE_NULL; |
| 560 | |
Dianne Hackborn | ab0f485 | 2011-09-12 16:59:06 -0700 | [diff] [blame] | 561 | a = theme.obtainStyledAttributes( |
| 562 | attrs, com.android.internal.R.styleable.TextView, defStyle, 0); |
| 563 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | int n = a.getIndexCount(); |
| 565 | for (int i = 0; i < n; i++) { |
| 566 | int attr = a.getIndex(i); |
| 567 | |
| 568 | switch (attr) { |
| 569 | case com.android.internal.R.styleable.TextView_editable: |
| 570 | editable = a.getBoolean(attr, editable); |
| 571 | break; |
| 572 | |
| 573 | case com.android.internal.R.styleable.TextView_inputMethod: |
| 574 | inputMethod = a.getText(attr); |
| 575 | break; |
| 576 | |
| 577 | case com.android.internal.R.styleable.TextView_numeric: |
| 578 | numeric = a.getInt(attr, numeric); |
| 579 | break; |
| 580 | |
| 581 | case com.android.internal.R.styleable.TextView_digits: |
| 582 | digits = a.getText(attr); |
| 583 | break; |
| 584 | |
| 585 | case com.android.internal.R.styleable.TextView_phoneNumber: |
| 586 | phone = a.getBoolean(attr, phone); |
| 587 | break; |
| 588 | |
| 589 | case com.android.internal.R.styleable.TextView_autoText: |
| 590 | autotext = a.getBoolean(attr, autotext); |
| 591 | break; |
| 592 | |
| 593 | case com.android.internal.R.styleable.TextView_capitalize: |
| 594 | autocap = a.getInt(attr, autocap); |
| 595 | break; |
| 596 | |
| 597 | case com.android.internal.R.styleable.TextView_bufferType: |
| 598 | buffertype = a.getInt(attr, buffertype); |
| 599 | break; |
| 600 | |
| 601 | case com.android.internal.R.styleable.TextView_selectAllOnFocus: |
| 602 | selectallonfocus = a.getBoolean(attr, selectallonfocus); |
| 603 | break; |
| 604 | |
| 605 | case com.android.internal.R.styleable.TextView_autoLink: |
| 606 | mAutoLinkMask = a.getInt(attr, 0); |
| 607 | break; |
| 608 | |
| 609 | case com.android.internal.R.styleable.TextView_linksClickable: |
| 610 | mLinksClickable = a.getBoolean(attr, true); |
| 611 | break; |
| 612 | |
Gilles Debunne | 550efbf | 2011-10-10 16:49:02 -0700 | [diff] [blame] | 613 | // TODO uncomment when this attribute is made public in the next release |
| 614 | // also add TextView_showSoftInputOnFocus to the list of attributes above |
| 615 | // case com.android.internal.R.styleable.TextView_showSoftInputOnFocus: |
| 616 | // setShowSoftInputOnFocus(a.getBoolean(attr, true)); |
| 617 | // break; |
| 618 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | case com.android.internal.R.styleable.TextView_drawableLeft: |
| 620 | drawableLeft = a.getDrawable(attr); |
| 621 | break; |
| 622 | |
| 623 | case com.android.internal.R.styleable.TextView_drawableTop: |
| 624 | drawableTop = a.getDrawable(attr); |
| 625 | break; |
| 626 | |
| 627 | case com.android.internal.R.styleable.TextView_drawableRight: |
| 628 | drawableRight = a.getDrawable(attr); |
| 629 | break; |
| 630 | |
| 631 | case com.android.internal.R.styleable.TextView_drawableBottom: |
| 632 | drawableBottom = a.getDrawable(attr); |
| 633 | break; |
| 634 | |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 635 | case com.android.internal.R.styleable.TextView_drawableStart: |
| 636 | drawableStart = a.getDrawable(attr); |
| 637 | break; |
| 638 | |
| 639 | case com.android.internal.R.styleable.TextView_drawableEnd: |
| 640 | drawableEnd = a.getDrawable(attr); |
| 641 | break; |
| 642 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | case com.android.internal.R.styleable.TextView_drawablePadding: |
| 644 | drawablePadding = a.getDimensionPixelSize(attr, drawablePadding); |
| 645 | break; |
| 646 | |
| 647 | case com.android.internal.R.styleable.TextView_maxLines: |
| 648 | setMaxLines(a.getInt(attr, -1)); |
| 649 | break; |
| 650 | |
| 651 | case com.android.internal.R.styleable.TextView_maxHeight: |
| 652 | setMaxHeight(a.getDimensionPixelSize(attr, -1)); |
| 653 | break; |
| 654 | |
| 655 | case com.android.internal.R.styleable.TextView_lines: |
| 656 | setLines(a.getInt(attr, -1)); |
| 657 | break; |
| 658 | |
| 659 | case com.android.internal.R.styleable.TextView_height: |
| 660 | setHeight(a.getDimensionPixelSize(attr, -1)); |
| 661 | break; |
| 662 | |
| 663 | case com.android.internal.R.styleable.TextView_minLines: |
| 664 | setMinLines(a.getInt(attr, -1)); |
| 665 | break; |
| 666 | |
| 667 | case com.android.internal.R.styleable.TextView_minHeight: |
| 668 | setMinHeight(a.getDimensionPixelSize(attr, -1)); |
| 669 | break; |
| 670 | |
| 671 | case com.android.internal.R.styleable.TextView_maxEms: |
| 672 | setMaxEms(a.getInt(attr, -1)); |
| 673 | break; |
| 674 | |
| 675 | case com.android.internal.R.styleable.TextView_maxWidth: |
| 676 | setMaxWidth(a.getDimensionPixelSize(attr, -1)); |
| 677 | break; |
| 678 | |
| 679 | case com.android.internal.R.styleable.TextView_ems: |
| 680 | setEms(a.getInt(attr, -1)); |
| 681 | break; |
| 682 | |
| 683 | case com.android.internal.R.styleable.TextView_width: |
| 684 | setWidth(a.getDimensionPixelSize(attr, -1)); |
| 685 | break; |
| 686 | |
| 687 | case com.android.internal.R.styleable.TextView_minEms: |
| 688 | setMinEms(a.getInt(attr, -1)); |
| 689 | break; |
| 690 | |
| 691 | case com.android.internal.R.styleable.TextView_minWidth: |
| 692 | setMinWidth(a.getDimensionPixelSize(attr, -1)); |
| 693 | break; |
| 694 | |
| 695 | case com.android.internal.R.styleable.TextView_gravity: |
| 696 | setGravity(a.getInt(attr, -1)); |
| 697 | break; |
| 698 | |
| 699 | case com.android.internal.R.styleable.TextView_hint: |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 700 | hint = a.getText(attr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 701 | break; |
| 702 | |
| 703 | case com.android.internal.R.styleable.TextView_text: |
| 704 | text = a.getText(attr); |
| 705 | break; |
| 706 | |
| 707 | case com.android.internal.R.styleable.TextView_scrollHorizontally: |
| 708 | if (a.getBoolean(attr, false)) { |
| 709 | setHorizontallyScrolling(true); |
| 710 | } |
| 711 | break; |
| 712 | |
| 713 | case com.android.internal.R.styleable.TextView_singleLine: |
| 714 | singleLine = a.getBoolean(attr, singleLine); |
| 715 | break; |
| 716 | |
| 717 | case com.android.internal.R.styleable.TextView_ellipsize: |
| 718 | ellipsize = a.getInt(attr, ellipsize); |
| 719 | break; |
| 720 | |
| 721 | case com.android.internal.R.styleable.TextView_marqueeRepeatLimit: |
| 722 | setMarqueeRepeatLimit(a.getInt(attr, mMarqueeRepeatLimit)); |
| 723 | break; |
| 724 | |
| 725 | case com.android.internal.R.styleable.TextView_includeFontPadding: |
| 726 | if (!a.getBoolean(attr, true)) { |
| 727 | setIncludeFontPadding(false); |
| 728 | } |
| 729 | break; |
| 730 | |
| 731 | case com.android.internal.R.styleable.TextView_cursorVisible: |
| 732 | if (!a.getBoolean(attr, true)) { |
| 733 | setCursorVisible(false); |
| 734 | } |
| 735 | break; |
| 736 | |
| 737 | case com.android.internal.R.styleable.TextView_maxLength: |
| 738 | maxlength = a.getInt(attr, -1); |
| 739 | break; |
| 740 | |
| 741 | case com.android.internal.R.styleable.TextView_textScaleX: |
| 742 | setTextScaleX(a.getFloat(attr, 1.0f)); |
| 743 | break; |
| 744 | |
| 745 | case com.android.internal.R.styleable.TextView_freezesText: |
| 746 | mFreezesText = a.getBoolean(attr, false); |
| 747 | break; |
| 748 | |
| 749 | case com.android.internal.R.styleable.TextView_shadowColor: |
| 750 | shadowcolor = a.getInt(attr, 0); |
| 751 | break; |
| 752 | |
| 753 | case com.android.internal.R.styleable.TextView_shadowDx: |
| 754 | dx = a.getFloat(attr, 0); |
| 755 | break; |
| 756 | |
| 757 | case com.android.internal.R.styleable.TextView_shadowDy: |
| 758 | dy = a.getFloat(attr, 0); |
| 759 | break; |
| 760 | |
| 761 | case com.android.internal.R.styleable.TextView_shadowRadius: |
| 762 | r = a.getFloat(attr, 0); |
| 763 | break; |
| 764 | |
| 765 | case com.android.internal.R.styleable.TextView_enabled: |
| 766 | setEnabled(a.getBoolean(attr, isEnabled())); |
| 767 | break; |
| 768 | |
| 769 | case com.android.internal.R.styleable.TextView_textColorHighlight: |
| 770 | textColorHighlight = a.getColor(attr, textColorHighlight); |
| 771 | break; |
| 772 | |
| 773 | case com.android.internal.R.styleable.TextView_textColor: |
| 774 | textColor = a.getColorStateList(attr); |
| 775 | break; |
| 776 | |
| 777 | case com.android.internal.R.styleable.TextView_textColorHint: |
| 778 | textColorHint = a.getColorStateList(attr); |
| 779 | break; |
| 780 | |
| 781 | case com.android.internal.R.styleable.TextView_textColorLink: |
| 782 | textColorLink = a.getColorStateList(attr); |
| 783 | break; |
| 784 | |
| 785 | case com.android.internal.R.styleable.TextView_textSize: |
| 786 | textSize = a.getDimensionPixelSize(attr, textSize); |
| 787 | break; |
| 788 | |
| 789 | case com.android.internal.R.styleable.TextView_typeface: |
| 790 | typefaceIndex = a.getInt(attr, typefaceIndex); |
| 791 | break; |
| 792 | |
| 793 | case com.android.internal.R.styleable.TextView_textStyle: |
| 794 | styleIndex = a.getInt(attr, styleIndex); |
| 795 | break; |
| 796 | |
| 797 | case com.android.internal.R.styleable.TextView_password: |
| 798 | password = a.getBoolean(attr, password); |
| 799 | break; |
| 800 | |
| 801 | case com.android.internal.R.styleable.TextView_lineSpacingExtra: |
| 802 | mSpacingAdd = a.getDimensionPixelSize(attr, (int) mSpacingAdd); |
| 803 | break; |
| 804 | |
| 805 | case com.android.internal.R.styleable.TextView_lineSpacingMultiplier: |
| 806 | mSpacingMult = a.getFloat(attr, mSpacingMult); |
| 807 | break; |
| 808 | |
| 809 | case com.android.internal.R.styleable.TextView_inputType: |
| 810 | inputType = a.getInt(attr, mInputType); |
| 811 | break; |
| 812 | |
| 813 | case com.android.internal.R.styleable.TextView_imeOptions: |
| 814 | if (mInputContentType == null) { |
| 815 | mInputContentType = new InputContentType(); |
| 816 | } |
| 817 | mInputContentType.imeOptions = a.getInt(attr, |
| 818 | mInputContentType.imeOptions); |
| 819 | break; |
| 820 | |
| 821 | case com.android.internal.R.styleable.TextView_imeActionLabel: |
| 822 | if (mInputContentType == null) { |
| 823 | mInputContentType = new InputContentType(); |
| 824 | } |
| 825 | mInputContentType.imeActionLabel = a.getText(attr); |
| 826 | break; |
| 827 | |
| 828 | case com.android.internal.R.styleable.TextView_imeActionId: |
| 829 | if (mInputContentType == null) { |
| 830 | mInputContentType = new InputContentType(); |
| 831 | } |
| 832 | mInputContentType.imeActionId = a.getInt(attr, |
| 833 | mInputContentType.imeActionId); |
| 834 | break; |
| 835 | |
| 836 | case com.android.internal.R.styleable.TextView_privateImeOptions: |
| 837 | setPrivateImeOptions(a.getString(attr)); |
| 838 | break; |
| 839 | |
| 840 | case com.android.internal.R.styleable.TextView_editorExtras: |
| 841 | try { |
| 842 | setInputExtras(a.getResourceId(attr, 0)); |
| 843 | } catch (XmlPullParserException e) { |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 844 | Log.w(LOG_TAG, "Failure reading input extras", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 845 | } catch (IOException e) { |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 846 | Log.w(LOG_TAG, "Failure reading input extras", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | } |
| 848 | break; |
Adam Powell | b08013c | 2010-09-16 16:28:11 -0700 | [diff] [blame] | 849 | |
Gilles Debunne | f75c97e | 2011-02-10 16:09:53 -0800 | [diff] [blame] | 850 | case com.android.internal.R.styleable.TextView_textCursorDrawable: |
| 851 | mCursorDrawableRes = a.getResourceId(attr, 0); |
| 852 | break; |
| 853 | |
Adam Powell | b08013c | 2010-09-16 16:28:11 -0700 | [diff] [blame] | 854 | case com.android.internal.R.styleable.TextView_textSelectHandleLeft: |
| 855 | mTextSelectHandleLeftRes = a.getResourceId(attr, 0); |
| 856 | break; |
| 857 | |
| 858 | case com.android.internal.R.styleable.TextView_textSelectHandleRight: |
| 859 | mTextSelectHandleRightRes = a.getResourceId(attr, 0); |
| 860 | break; |
| 861 | |
| 862 | case com.android.internal.R.styleable.TextView_textSelectHandle: |
| 863 | mTextSelectHandleRes = a.getResourceId(attr, 0); |
| 864 | break; |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 865 | |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 866 | case com.android.internal.R.styleable.TextView_textEditSuggestionItemLayout: |
| 867 | mTextEditSuggestionItemLayout = a.getResourceId(attr, 0); |
| 868 | break; |
| 869 | |
Gilles Debunne | 86b9c78 | 2010-11-11 10:43:48 -0800 | [diff] [blame] | 870 | case com.android.internal.R.styleable.TextView_textIsSelectable: |
| 871 | mTextIsSelectable = a.getBoolean(attr, false); |
| 872 | break; |
Gilles Debunne | f3a135b | 2011-05-23 16:28:47 -0700 | [diff] [blame] | 873 | |
Adam Powell | 7f8f79a | 2011-07-07 18:35:54 -0700 | [diff] [blame] | 874 | case com.android.internal.R.styleable.TextView_textAllCaps: |
| 875 | allCaps = a.getBoolean(attr, false); |
| 876 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | a.recycle(); |
| 880 | |
| 881 | BufferType bufferType = BufferType.EDITABLE; |
| 882 | |
Gilles Debunne | d7483bf | 2010-11-10 10:47:45 -0800 | [diff] [blame] | 883 | final int variation = |
| 884 | inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION); |
| 885 | final boolean passwordInputType = variation |
| 886 | == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD); |
| 887 | final boolean webPasswordInputType = variation |
| 888 | == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD); |
Ken Wakasa | 82d731a | 2010-12-24 23:42:41 +0900 | [diff] [blame] | 889 | final boolean numberPasswordInputType = variation |
| 890 | == (EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD); |
Gilles Debunne | d7483bf | 2010-11-10 10:47:45 -0800 | [diff] [blame] | 891 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 892 | if (inputMethod != null) { |
Gilles Debunne | e15b358 | 2010-06-16 15:17:21 -0700 | [diff] [blame] | 893 | Class<?> c; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 894 | |
| 895 | try { |
| 896 | c = Class.forName(inputMethod.toString()); |
| 897 | } catch (ClassNotFoundException ex) { |
| 898 | throw new RuntimeException(ex); |
| 899 | } |
| 900 | |
| 901 | try { |
| 902 | mInput = (KeyListener) c.newInstance(); |
| 903 | } catch (InstantiationException ex) { |
| 904 | throw new RuntimeException(ex); |
| 905 | } catch (IllegalAccessException ex) { |
| 906 | throw new RuntimeException(ex); |
| 907 | } |
| 908 | try { |
| 909 | mInputType = inputType != EditorInfo.TYPE_NULL |
| 910 | ? inputType |
| 911 | : mInput.getInputType(); |
| 912 | } catch (IncompatibleClassChangeError e) { |
| 913 | mInputType = EditorInfo.TYPE_CLASS_TEXT; |
| 914 | } |
| 915 | } else if (digits != null) { |
| 916 | mInput = DigitsKeyListener.getInstance(digits.toString()); |
Dianne Hackborn | 7ed6ee5 | 2009-09-10 18:41:28 -0700 | [diff] [blame] | 917 | // If no input type was specified, we will default to generic |
| 918 | // text, since we can't tell the IME about the set of digits |
| 919 | // that was selected. |
| 920 | mInputType = inputType != EditorInfo.TYPE_NULL |
| 921 | ? inputType : EditorInfo.TYPE_CLASS_TEXT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | } else if (inputType != EditorInfo.TYPE_NULL) { |
| 923 | setInputType(inputType, true); |
Gilles Debunne | 91a08cf | 2010-11-08 17:34:49 -0800 | [diff] [blame] | 924 | // If set, the input type overrides what was set using the deprecated singleLine flag. |
| 925 | singleLine = !isMultilineInputType(inputType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 926 | } else if (phone) { |
| 927 | mInput = DialerKeyListener.getInstance(); |
Dianne Hackborn | 49a1a9b5 | 2009-03-24 20:06:11 -0700 | [diff] [blame] | 928 | mInputType = inputType = EditorInfo.TYPE_CLASS_PHONE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | } else if (numeric != 0) { |
| 930 | mInput = DigitsKeyListener.getInstance((numeric & SIGNED) != 0, |
| 931 | (numeric & DECIMAL) != 0); |
| 932 | inputType = EditorInfo.TYPE_CLASS_NUMBER; |
| 933 | if ((numeric & SIGNED) != 0) { |
| 934 | inputType |= EditorInfo.TYPE_NUMBER_FLAG_SIGNED; |
| 935 | } |
| 936 | if ((numeric & DECIMAL) != 0) { |
| 937 | inputType |= EditorInfo.TYPE_NUMBER_FLAG_DECIMAL; |
| 938 | } |
| 939 | mInputType = inputType; |
| 940 | } else if (autotext || autocap != -1) { |
| 941 | TextKeyListener.Capitalize cap; |
| 942 | |
| 943 | inputType = EditorInfo.TYPE_CLASS_TEXT; |
Gilles Debunne | f95449d | 2010-11-05 13:54:13 -0700 | [diff] [blame] | 944 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | switch (autocap) { |
| 946 | case 1: |
| 947 | cap = TextKeyListener.Capitalize.SENTENCES; |
| 948 | inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES; |
| 949 | break; |
| 950 | |
| 951 | case 2: |
| 952 | cap = TextKeyListener.Capitalize.WORDS; |
| 953 | inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS; |
| 954 | break; |
| 955 | |
| 956 | case 3: |
| 957 | cap = TextKeyListener.Capitalize.CHARACTERS; |
| 958 | inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_CHARACTERS; |
| 959 | break; |
| 960 | |
| 961 | default: |
| 962 | cap = TextKeyListener.Capitalize.NONE; |
| 963 | break; |
| 964 | } |
| 965 | |
| 966 | mInput = TextKeyListener.getInstance(autotext, cap); |
| 967 | mInputType = inputType; |
Gilles Debunne | 86b9c78 | 2010-11-11 10:43:48 -0800 | [diff] [blame] | 968 | } else if (mTextIsSelectable) { |
| 969 | // Prevent text changes from keyboard. |
| 970 | mInputType = EditorInfo.TYPE_NULL; |
| 971 | mInput = null; |
| 972 | bufferType = BufferType.SPANNABLE; |
Gilles Debunne | cbcb345 | 2010-12-17 15:31:02 -0800 | [diff] [blame] | 973 | // Required to request focus while in touch mode. |
| 974 | setFocusableInTouchMode(true); |
Gilles Debunne | 86b9c78 | 2010-11-11 10:43:48 -0800 | [diff] [blame] | 975 | // So that selection can be changed using arrow keys and touch is handled. |
| 976 | setMovementMethod(ArrowKeyMovementMethod.getInstance()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 977 | } else if (editable) { |
| 978 | mInput = TextKeyListener.getInstance(); |
| 979 | mInputType = EditorInfo.TYPE_CLASS_TEXT; |
| 980 | } else { |
| 981 | mInput = null; |
| 982 | |
| 983 | switch (buffertype) { |
| 984 | case 0: |
| 985 | bufferType = BufferType.NORMAL; |
| 986 | break; |
| 987 | case 1: |
| 988 | bufferType = BufferType.SPANNABLE; |
| 989 | break; |
| 990 | case 2: |
| 991 | bufferType = BufferType.EDITABLE; |
| 992 | break; |
| 993 | } |
| 994 | } |
| 995 | |
Gilles Debunne | d7483bf | 2010-11-10 10:47:45 -0800 | [diff] [blame] | 996 | // mInputType has been set from inputType, possibly modified by mInputMethod. |
| 997 | // Specialize mInputType to [web]password if we have a text class and the original input |
| 998 | // type was a password. |
| 999 | if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { |
| 1000 | if (password || passwordInputType) { |
Leon Scroggins | b5ce0e0 | 2010-11-01 13:20:24 -0400 | [diff] [blame] | 1001 | mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION)) |
Gilles Debunne | d7483bf | 2010-11-10 10:47:45 -0800 | [diff] [blame] | 1002 | | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD; |
Leon Scroggins | b5ce0e0 | 2010-11-01 13:20:24 -0400 | [diff] [blame] | 1003 | } |
Gilles Debunne | d7483bf | 2010-11-10 10:47:45 -0800 | [diff] [blame] | 1004 | if (webPasswordInputType) { |
| 1005 | mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION)) |
| 1006 | | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD; |
| 1007 | } |
Ken Wakasa | 82d731a | 2010-12-24 23:42:41 +0900 | [diff] [blame] | 1008 | } else if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_NUMBER) { |
| 1009 | if (numberPasswordInputType) { |
| 1010 | mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION)) |
| 1011 | | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD; |
| 1012 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | if (selectallonfocus) { |
| 1016 | mSelectAllOnFocus = true; |
| 1017 | |
| 1018 | if (bufferType == BufferType.NORMAL) |
| 1019 | bufferType = BufferType.SPANNABLE; |
| 1020 | } |
| 1021 | |
| 1022 | setCompoundDrawablesWithIntrinsicBounds( |
| 1023 | drawableLeft, drawableTop, drawableRight, drawableBottom); |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 1024 | setRelativeDrawablesIfNeeded(drawableStart, drawableEnd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1025 | setCompoundDrawablePadding(drawablePadding); |
| 1026 | |
Gilles Debunne | a3ae4a0 | 2010-12-13 17:22:39 -0800 | [diff] [blame] | 1027 | // Same as setSingleLine(), but make sure the transformation method and the maximum number |
Gilles Debunne | 066460f | 2010-12-15 17:31:51 -0800 | [diff] [blame] | 1028 | // of lines of height are unchanged for multi-line TextViews. |
Gilles Debunne | d7483bf | 2010-11-10 10:47:45 -0800 | [diff] [blame] | 1029 | setInputTypeSingleLine(singleLine); |
Gilles Debunne | 066460f | 2010-12-15 17:31:51 -0800 | [diff] [blame] | 1030 | applySingleLine(singleLine, singleLine, singleLine); |
Gilles Debunne | d7483bf | 2010-11-10 10:47:45 -0800 | [diff] [blame] | 1031 | |
Gilles Debunne | 91a08cf | 2010-11-08 17:34:49 -0800 | [diff] [blame] | 1032 | if (singleLine && mInput == null && ellipsize < 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1033 | ellipsize = 3; // END |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | switch (ellipsize) { |
| 1037 | case 1: |
| 1038 | setEllipsize(TextUtils.TruncateAt.START); |
| 1039 | break; |
| 1040 | case 2: |
| 1041 | setEllipsize(TextUtils.TruncateAt.MIDDLE); |
| 1042 | break; |
| 1043 | case 3: |
| 1044 | setEllipsize(TextUtils.TruncateAt.END); |
| 1045 | break; |
| 1046 | case 4: |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 1047 | if (ViewConfiguration.get(context).isFadingMarqueeEnabled()) { |
| 1048 | setHorizontalFadingEdgeEnabled(true); |
| 1049 | mMarqueeFadeMode = MARQUEE_FADE_NORMAL; |
| 1050 | } else { |
| 1051 | setHorizontalFadingEdgeEnabled(false); |
| 1052 | mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS; |
| 1053 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1054 | setEllipsize(TextUtils.TruncateAt.MARQUEE); |
| 1055 | break; |
| 1056 | } |
| 1057 | |
| 1058 | setTextColor(textColor != null ? textColor : ColorStateList.valueOf(0xFF000000)); |
| 1059 | setHintTextColor(textColorHint); |
| 1060 | setLinkTextColor(textColorLink); |
| 1061 | if (textColorHighlight != 0) { |
| 1062 | setHighlightColor(textColorHighlight); |
| 1063 | } |
| 1064 | setRawTextSize(textSize); |
| 1065 | |
Adam Powell | 7f8f79a | 2011-07-07 18:35:54 -0700 | [diff] [blame] | 1066 | if (allCaps) { |
| 1067 | setTransformationMethod(new AllCapsTransformationMethod(getContext())); |
| 1068 | } |
| 1069 | |
Ken Wakasa | 82d731a | 2010-12-24 23:42:41 +0900 | [diff] [blame] | 1070 | if (password || passwordInputType || webPasswordInputType || numberPasswordInputType) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1071 | setTransformationMethod(PasswordTransformationMethod.getInstance()); |
| 1072 | typefaceIndex = MONOSPACE; |
Gilles Debunne | d7483bf | 2010-11-10 10:47:45 -0800 | [diff] [blame] | 1073 | } else if ((mInputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION)) |
| 1074 | == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD)) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1075 | typefaceIndex = MONOSPACE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | setTypefaceByIndex(typefaceIndex, styleIndex); |
| 1079 | |
| 1080 | if (shadowcolor != 0) { |
| 1081 | setShadowLayer(r, dx, dy, shadowcolor); |
| 1082 | } |
| 1083 | |
| 1084 | if (maxlength >= 0) { |
| 1085 | setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxlength) }); |
| 1086 | } else { |
| 1087 | setFilters(NO_FILTERS); |
| 1088 | } |
| 1089 | |
| 1090 | setText(text, bufferType); |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 1091 | if (hint != null) setHint(hint); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | |
| 1093 | /* |
| 1094 | * Views are not normally focusable unless specified to be. |
| 1095 | * However, TextViews that have input or movement methods *are* |
| 1096 | * focusable by default. |
| 1097 | */ |
| 1098 | a = context.obtainStyledAttributes(attrs, |
| 1099 | com.android.internal.R.styleable.View, |
| 1100 | defStyle, 0); |
| 1101 | |
| 1102 | boolean focusable = mMovement != null || mInput != null; |
| 1103 | boolean clickable = focusable; |
| 1104 | boolean longClickable = focusable; |
| 1105 | |
| 1106 | n = a.getIndexCount(); |
| 1107 | for (int i = 0; i < n; i++) { |
| 1108 | int attr = a.getIndex(i); |
| 1109 | |
| 1110 | switch (attr) { |
| 1111 | case com.android.internal.R.styleable.View_focusable: |
| 1112 | focusable = a.getBoolean(attr, focusable); |
| 1113 | break; |
| 1114 | |
| 1115 | case com.android.internal.R.styleable.View_clickable: |
| 1116 | clickable = a.getBoolean(attr, clickable); |
| 1117 | break; |
| 1118 | |
| 1119 | case com.android.internal.R.styleable.View_longClickable: |
| 1120 | longClickable = a.getBoolean(attr, longClickable); |
| 1121 | break; |
| 1122 | } |
| 1123 | } |
| 1124 | a.recycle(); |
| 1125 | |
| 1126 | setFocusable(focusable); |
| 1127 | setClickable(clickable); |
| 1128 | setLongClickable(longClickable); |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 1129 | |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 1130 | prepareCursorControllers(); |
Gilles Debunne | aa85a4c | 2010-12-06 18:27:17 -0800 | [diff] [blame] | 1131 | |
| 1132 | final ViewConfiguration viewConfiguration = ViewConfiguration.get(context); |
| 1133 | final int touchSlop = viewConfiguration.getScaledTouchSlop(); |
| 1134 | mSquaredTouchSlopDistance = touchSlop * touchSlop; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | private void setTypefaceByIndex(int typefaceIndex, int styleIndex) { |
| 1138 | Typeface tf = null; |
| 1139 | switch (typefaceIndex) { |
| 1140 | case SANS: |
| 1141 | tf = Typeface.SANS_SERIF; |
| 1142 | break; |
| 1143 | |
| 1144 | case SERIF: |
| 1145 | tf = Typeface.SERIF; |
| 1146 | break; |
| 1147 | |
| 1148 | case MONOSPACE: |
| 1149 | tf = Typeface.MONOSPACE; |
| 1150 | break; |
| 1151 | } |
| 1152 | |
| 1153 | setTypeface(tf, styleIndex); |
| 1154 | } |
| 1155 | |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 1156 | private void setRelativeDrawablesIfNeeded(Drawable start, Drawable end) { |
| 1157 | boolean hasRelativeDrawables = (start != null) || (end != null); |
| 1158 | if (hasRelativeDrawables) { |
| 1159 | Drawables dr = mDrawables; |
| 1160 | if (dr == null) { |
| 1161 | mDrawables = dr = new Drawables(); |
| 1162 | } |
| 1163 | final Rect compoundRect = dr.mCompoundRect; |
| 1164 | int[] state = getDrawableState(); |
| 1165 | if (start != null) { |
| 1166 | start.setBounds(0, 0, start.getIntrinsicWidth(), start.getIntrinsicHeight()); |
| 1167 | start.setState(state); |
| 1168 | start.copyBounds(compoundRect); |
| 1169 | start.setCallback(this); |
| 1170 | |
| 1171 | dr.mDrawableStart = start; |
| 1172 | dr.mDrawableSizeStart = compoundRect.width(); |
| 1173 | dr.mDrawableHeightStart = compoundRect.height(); |
| 1174 | } else { |
| 1175 | dr.mDrawableSizeStart = dr.mDrawableHeightStart = 0; |
| 1176 | } |
| 1177 | if (end != null) { |
| 1178 | end.setBounds(0, 0, end.getIntrinsicWidth(), end.getIntrinsicHeight()); |
| 1179 | end.setState(state); |
| 1180 | end.copyBounds(compoundRect); |
| 1181 | end.setCallback(this); |
| 1182 | |
| 1183 | dr.mDrawableEnd = end; |
| 1184 | dr.mDrawableSizeEnd = compoundRect.width(); |
| 1185 | dr.mDrawableHeightEnd = compoundRect.height(); |
| 1186 | } else { |
| 1187 | dr.mDrawableSizeEnd = dr.mDrawableHeightEnd = 0; |
| 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | |
Janos Levai | 042856c | 2010-10-15 02:53:58 +0300 | [diff] [blame] | 1192 | @Override |
| 1193 | public void setEnabled(boolean enabled) { |
| 1194 | if (enabled == isEnabled()) { |
| 1195 | return; |
| 1196 | } |
| 1197 | |
| 1198 | if (!enabled) { |
| 1199 | // Hide the soft input if the currently active TextView is disabled |
| 1200 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 1201 | if (imm != null && imm.isActive(this)) { |
| 1202 | imm.hideSoftInputFromWindow(getWindowToken(), 0); |
| 1203 | } |
| 1204 | } |
| 1205 | super.setEnabled(enabled); |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 1206 | prepareCursorControllers(); |
Dianne Hackborn | bc82385 | 2011-09-18 17:19:50 -0700 | [diff] [blame] | 1207 | if (enabled) { |
| 1208 | // Make sure IME is updated with current editor info. |
| 1209 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 1210 | if (imm != null) imm.restartInput(this); |
| 1211 | } |
Mark Wagner | f818511 | 2011-10-25 16:33:41 -0700 | [diff] [blame] | 1212 | |
| 1213 | // start or stop the cursor blinking as appropriate |
| 1214 | makeBlink(); |
Janos Levai | 042856c | 2010-10-15 02:53:58 +0300 | [diff] [blame] | 1215 | } |
| 1216 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1217 | /** |
| 1218 | * Sets the typeface and style in which the text should be displayed, |
| 1219 | * and turns on the fake bold and italic bits in the Paint if the |
| 1220 | * Typeface that you provided does not have all the bits in the |
| 1221 | * style that you specified. |
| 1222 | * |
| 1223 | * @attr ref android.R.styleable#TextView_typeface |
| 1224 | * @attr ref android.R.styleable#TextView_textStyle |
| 1225 | */ |
| 1226 | public void setTypeface(Typeface tf, int style) { |
| 1227 | if (style > 0) { |
| 1228 | if (tf == null) { |
| 1229 | tf = Typeface.defaultFromStyle(style); |
| 1230 | } else { |
| 1231 | tf = Typeface.create(tf, style); |
| 1232 | } |
| 1233 | |
| 1234 | setTypeface(tf); |
| 1235 | // now compute what (if any) algorithmic styling is needed |
| 1236 | int typefaceStyle = tf != null ? tf.getStyle() : 0; |
| 1237 | int need = style & ~typefaceStyle; |
| 1238 | mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0); |
| 1239 | mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0); |
| 1240 | } else { |
| 1241 | mTextPaint.setFakeBoldText(false); |
| 1242 | mTextPaint.setTextSkewX(0); |
| 1243 | setTypeface(tf); |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | /** |
| 1248 | * Subclasses override this to specify that they have a KeyListener |
| 1249 | * by default even if not specifically called for in the XML options. |
| 1250 | */ |
| 1251 | protected boolean getDefaultEditable() { |
| 1252 | return false; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * Subclasses override this to specify a default movement method. |
| 1257 | */ |
| 1258 | protected MovementMethod getDefaultMovementMethod() { |
| 1259 | return null; |
| 1260 | } |
| 1261 | |
| 1262 | /** |
| 1263 | * Return the text the TextView is displaying. If setText() was called with |
| 1264 | * an argument of BufferType.SPANNABLE or BufferType.EDITABLE, you can cast |
| 1265 | * the return value from this method to Spannable or Editable, respectively. |
| 1266 | * |
| 1267 | * Note: The content of the return value should not be modified. If you want |
| 1268 | * a modifiable one, you should make your own copy first. |
| 1269 | */ |
| 1270 | @ViewDebug.CapturedViewProperty |
| 1271 | public CharSequence getText() { |
| 1272 | return mText; |
| 1273 | } |
| 1274 | |
| 1275 | /** |
| 1276 | * Returns the length, in characters, of the text managed by this TextView |
| 1277 | */ |
| 1278 | public int length() { |
| 1279 | return mText.length(); |
| 1280 | } |
| 1281 | |
| 1282 | /** |
| 1283 | * Return the text the TextView is displaying as an Editable object. If |
| 1284 | * the text is not editable, null is returned. |
| 1285 | * |
| 1286 | * @see #getText |
| 1287 | */ |
| 1288 | public Editable getEditableText() { |
| 1289 | return (mText instanceof Editable) ? (Editable)mText : null; |
| 1290 | } |
| 1291 | |
| 1292 | /** |
| 1293 | * @return the height of one standard line in pixels. Note that markup |
| 1294 | * within the text can cause individual lines to be taller or shorter |
| 1295 | * than this height, and the layout may contain additional first- |
| 1296 | * or last-line padding. |
| 1297 | */ |
| 1298 | public int getLineHeight() { |
Gilles Debunne | 96e6b8b | 2010-12-14 13:43:45 -0800 | [diff] [blame] | 1299 | return FastMath.round(mTextPaint.getFontMetricsInt(null) * mSpacingMult + mSpacingAdd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | /** |
| 1303 | * @return the Layout that is currently being used to display the text. |
| 1304 | * This can be null if the text or width has recently changes. |
| 1305 | */ |
| 1306 | public final Layout getLayout() { |
| 1307 | return mLayout; |
| 1308 | } |
| 1309 | |
| 1310 | /** |
| 1311 | * @return the current key listener for this TextView. |
| 1312 | * This will frequently be null for non-EditText TextViews. |
| 1313 | */ |
| 1314 | public final KeyListener getKeyListener() { |
| 1315 | return mInput; |
| 1316 | } |
| 1317 | |
| 1318 | /** |
| 1319 | * Sets the key listener to be used with this TextView. This can be null |
| 1320 | * to disallow user input. Note that this method has significant and |
| 1321 | * subtle interactions with soft keyboards and other input method: |
| 1322 | * see {@link KeyListener#getInputType() KeyListener.getContentType()} |
| 1323 | * for important details. Calling this method will replace the current |
| 1324 | * content type of the text view with the content type returned by the |
| 1325 | * key listener. |
| 1326 | * <p> |
| 1327 | * Be warned that if you want a TextView with a key listener or movement |
| 1328 | * method not to be focusable, or if you want a TextView without a |
| 1329 | * key listener or movement method to be focusable, you must call |
| 1330 | * {@link #setFocusable} again after calling this to get the focusability |
| 1331 | * back the way you want it. |
| 1332 | * |
| 1333 | * @attr ref android.R.styleable#TextView_numeric |
| 1334 | * @attr ref android.R.styleable#TextView_digits |
| 1335 | * @attr ref android.R.styleable#TextView_phoneNumber |
| 1336 | * @attr ref android.R.styleable#TextView_inputMethod |
| 1337 | * @attr ref android.R.styleable#TextView_capitalize |
| 1338 | * @attr ref android.R.styleable#TextView_autoText |
| 1339 | */ |
| 1340 | public void setKeyListener(KeyListener input) { |
| 1341 | setKeyListenerOnly(input); |
| 1342 | fixFocusableAndClickableSettings(); |
| 1343 | |
| 1344 | if (input != null) { |
| 1345 | try { |
| 1346 | mInputType = mInput.getInputType(); |
| 1347 | } catch (IncompatibleClassChangeError e) { |
| 1348 | mInputType = EditorInfo.TYPE_CLASS_TEXT; |
| 1349 | } |
Gilles Debunne | d7483bf | 2010-11-10 10:47:45 -0800 | [diff] [blame] | 1350 | // Change inputType, without affecting transformation. |
| 1351 | // No need to applySingleLine since mSingleLine is unchanged. |
| 1352 | setInputTypeSingleLine(mSingleLine); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | } else { |
| 1354 | mInputType = EditorInfo.TYPE_NULL; |
| 1355 | } |
| 1356 | |
| 1357 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 1358 | if (imm != null) imm.restartInput(this); |
| 1359 | } |
| 1360 | |
| 1361 | private void setKeyListenerOnly(KeyListener input) { |
| 1362 | mInput = input; |
| 1363 | if (mInput != null && !(mText instanceof Editable)) |
| 1364 | setText(mText); |
| 1365 | |
| 1366 | setFilters((Editable) mText, mFilters); |
| 1367 | } |
| 1368 | |
| 1369 | /** |
| 1370 | * @return the movement method being used for this TextView. |
| 1371 | * This will frequently be null for non-EditText TextViews. |
| 1372 | */ |
| 1373 | public final MovementMethod getMovementMethod() { |
| 1374 | return mMovement; |
| 1375 | } |
| 1376 | |
| 1377 | /** |
| 1378 | * Sets the movement method (arrow key handler) to be used for |
| 1379 | * this TextView. This can be null to disallow using the arrow keys |
| 1380 | * to move the cursor or scroll the view. |
| 1381 | * <p> |
| 1382 | * Be warned that if you want a TextView with a key listener or movement |
| 1383 | * method not to be focusable, or if you want a TextView without a |
| 1384 | * key listener or movement method to be focusable, you must call |
| 1385 | * {@link #setFocusable} again after calling this to get the focusability |
| 1386 | * back the way you want it. |
| 1387 | */ |
| 1388 | public final void setMovementMethod(MovementMethod movement) { |
| 1389 | mMovement = movement; |
| 1390 | |
| 1391 | if (mMovement != null && !(mText instanceof Spannable)) |
| 1392 | setText(mText); |
| 1393 | |
| 1394 | fixFocusableAndClickableSettings(); |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 1395 | |
Gilles Debunne | baaace5 | 2010-10-01 15:47:13 -0700 | [diff] [blame] | 1396 | // SelectionModifierCursorController depends on textCanBeSelected, which depends on mMovement |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 1397 | prepareCursorControllers(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | private void fixFocusableAndClickableSettings() { |
| 1401 | if ((mMovement != null) || mInput != null) { |
| 1402 | setFocusable(true); |
| 1403 | setClickable(true); |
| 1404 | setLongClickable(true); |
| 1405 | } else { |
| 1406 | setFocusable(false); |
| 1407 | setClickable(false); |
| 1408 | setLongClickable(false); |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | /** |
| 1413 | * @return the current transformation method for this TextView. |
| 1414 | * This will frequently be null except for single-line and password |
| 1415 | * fields. |
| 1416 | */ |
| 1417 | public final TransformationMethod getTransformationMethod() { |
| 1418 | return mTransformation; |
| 1419 | } |
| 1420 | |
| 1421 | /** |
| 1422 | * Sets the transformation that is applied to the text that this |
| 1423 | * TextView is displaying. |
| 1424 | * |
| 1425 | * @attr ref android.R.styleable#TextView_password |
| 1426 | * @attr ref android.R.styleable#TextView_singleLine |
| 1427 | */ |
| 1428 | public final void setTransformationMethod(TransformationMethod method) { |
| 1429 | if (method == mTransformation) { |
| 1430 | // Avoid the setText() below if the transformation is |
| 1431 | // the same. |
| 1432 | return; |
| 1433 | } |
| 1434 | if (mTransformation != null) { |
| 1435 | if (mText instanceof Spannable) { |
| 1436 | ((Spannable) mText).removeSpan(mTransformation); |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | mTransformation = method; |
| 1441 | |
Adam Powell | 7f8f79a | 2011-07-07 18:35:54 -0700 | [diff] [blame] | 1442 | if (method instanceof TransformationMethod2) { |
| 1443 | TransformationMethod2 method2 = (TransformationMethod2) method; |
| 1444 | mAllowTransformationLengthChange = !mTextIsSelectable && !(mText instanceof Editable); |
| 1445 | method2.setLengthChangesAllowed(mAllowTransformationLengthChange); |
| 1446 | } else { |
| 1447 | mAllowTransformationLengthChange = false; |
| 1448 | } |
| 1449 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | setText(mText); |
| 1451 | } |
| 1452 | |
| 1453 | /** |
| 1454 | * Returns the top padding of the view, plus space for the top |
| 1455 | * Drawable if any. |
| 1456 | */ |
| 1457 | public int getCompoundPaddingTop() { |
| 1458 | final Drawables dr = mDrawables; |
| 1459 | if (dr == null || dr.mDrawableTop == null) { |
| 1460 | return mPaddingTop; |
| 1461 | } else { |
| 1462 | return mPaddingTop + dr.mDrawablePadding + dr.mDrawableSizeTop; |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | /** |
| 1467 | * Returns the bottom padding of the view, plus space for the bottom |
| 1468 | * Drawable if any. |
| 1469 | */ |
| 1470 | public int getCompoundPaddingBottom() { |
| 1471 | final Drawables dr = mDrawables; |
| 1472 | if (dr == null || dr.mDrawableBottom == null) { |
| 1473 | return mPaddingBottom; |
| 1474 | } else { |
| 1475 | return mPaddingBottom + dr.mDrawablePadding + dr.mDrawableSizeBottom; |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | /** |
| 1480 | * Returns the left padding of the view, plus space for the left |
| 1481 | * Drawable if any. |
| 1482 | */ |
| 1483 | public int getCompoundPaddingLeft() { |
| 1484 | final Drawables dr = mDrawables; |
| 1485 | if (dr == null || dr.mDrawableLeft == null) { |
| 1486 | return mPaddingLeft; |
| 1487 | } else { |
| 1488 | return mPaddingLeft + dr.mDrawablePadding + dr.mDrawableSizeLeft; |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | /** |
| 1493 | * Returns the right padding of the view, plus space for the right |
| 1494 | * Drawable if any. |
| 1495 | */ |
| 1496 | public int getCompoundPaddingRight() { |
| 1497 | final Drawables dr = mDrawables; |
| 1498 | if (dr == null || dr.mDrawableRight == null) { |
| 1499 | return mPaddingRight; |
| 1500 | } else { |
| 1501 | return mPaddingRight + dr.mDrawablePadding + dr.mDrawableSizeRight; |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | /** |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 1506 | * Returns the start padding of the view, plus space for the start |
| 1507 | * Drawable if any. |
| 1508 | * |
| 1509 | * @hide |
| 1510 | */ |
| 1511 | public int getCompoundPaddingStart() { |
| 1512 | resolveDrawables(); |
| 1513 | switch(getResolvedLayoutDirection()) { |
| 1514 | default: |
| 1515 | case LAYOUT_DIRECTION_LTR: |
| 1516 | return getCompoundPaddingLeft(); |
| 1517 | case LAYOUT_DIRECTION_RTL: |
| 1518 | return getCompoundPaddingRight(); |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | /** |
| 1523 | * Returns the end padding of the view, plus space for the end |
| 1524 | * Drawable if any. |
| 1525 | * |
| 1526 | * @hide |
| 1527 | */ |
| 1528 | public int getCompoundPaddingEnd() { |
| 1529 | resolveDrawables(); |
| 1530 | switch(getResolvedLayoutDirection()) { |
| 1531 | default: |
| 1532 | case LAYOUT_DIRECTION_LTR: |
| 1533 | return getCompoundPaddingRight(); |
| 1534 | case LAYOUT_DIRECTION_RTL: |
| 1535 | return getCompoundPaddingLeft(); |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1540 | * Returns the extended top padding of the view, including both the |
| 1541 | * top Drawable if any and any extra space to keep more than maxLines |
| 1542 | * of text from showing. It is only valid to call this after measuring. |
| 1543 | */ |
| 1544 | public int getExtendedPaddingTop() { |
| 1545 | if (mMaxMode != LINES) { |
| 1546 | return getCompoundPaddingTop(); |
| 1547 | } |
| 1548 | |
| 1549 | if (mLayout.getLineCount() <= mMaximum) { |
| 1550 | return getCompoundPaddingTop(); |
| 1551 | } |
| 1552 | |
| 1553 | int top = getCompoundPaddingTop(); |
| 1554 | int bottom = getCompoundPaddingBottom(); |
| 1555 | int viewht = getHeight() - top - bottom; |
| 1556 | int layoutht = mLayout.getLineTop(mMaximum); |
| 1557 | |
| 1558 | if (layoutht >= viewht) { |
| 1559 | return top; |
| 1560 | } |
| 1561 | |
| 1562 | final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 1563 | if (gravity == Gravity.TOP) { |
| 1564 | return top; |
| 1565 | } else if (gravity == Gravity.BOTTOM) { |
| 1566 | return top + viewht - layoutht; |
| 1567 | } else { // (gravity == Gravity.CENTER_VERTICAL) |
| 1568 | return top + (viewht - layoutht) / 2; |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | /** |
| 1573 | * Returns the extended bottom padding of the view, including both the |
| 1574 | * bottom Drawable if any and any extra space to keep more than maxLines |
| 1575 | * of text from showing. It is only valid to call this after measuring. |
| 1576 | */ |
| 1577 | public int getExtendedPaddingBottom() { |
| 1578 | if (mMaxMode != LINES) { |
| 1579 | return getCompoundPaddingBottom(); |
| 1580 | } |
| 1581 | |
| 1582 | if (mLayout.getLineCount() <= mMaximum) { |
| 1583 | return getCompoundPaddingBottom(); |
| 1584 | } |
| 1585 | |
| 1586 | int top = getCompoundPaddingTop(); |
| 1587 | int bottom = getCompoundPaddingBottom(); |
| 1588 | int viewht = getHeight() - top - bottom; |
| 1589 | int layoutht = mLayout.getLineTop(mMaximum); |
| 1590 | |
| 1591 | if (layoutht >= viewht) { |
| 1592 | return bottom; |
| 1593 | } |
| 1594 | |
| 1595 | final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 1596 | if (gravity == Gravity.TOP) { |
| 1597 | return bottom + viewht - layoutht; |
| 1598 | } else if (gravity == Gravity.BOTTOM) { |
| 1599 | return bottom; |
| 1600 | } else { // (gravity == Gravity.CENTER_VERTICAL) |
| 1601 | return bottom + (viewht - layoutht) / 2; |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | /** |
| 1606 | * Returns the total left padding of the view, including the left |
| 1607 | * Drawable if any. |
| 1608 | */ |
| 1609 | public int getTotalPaddingLeft() { |
| 1610 | return getCompoundPaddingLeft(); |
| 1611 | } |
| 1612 | |
| 1613 | /** |
| 1614 | * Returns the total right padding of the view, including the right |
| 1615 | * Drawable if any. |
| 1616 | */ |
| 1617 | public int getTotalPaddingRight() { |
| 1618 | return getCompoundPaddingRight(); |
| 1619 | } |
| 1620 | |
| 1621 | /** |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 1622 | * Returns the total start padding of the view, including the start |
| 1623 | * Drawable if any. |
| 1624 | * |
| 1625 | * @hide |
| 1626 | */ |
| 1627 | public int getTotalPaddingStart() { |
| 1628 | return getCompoundPaddingStart(); |
| 1629 | } |
| 1630 | |
| 1631 | /** |
| 1632 | * Returns the total end padding of the view, including the end |
| 1633 | * Drawable if any. |
| 1634 | * |
| 1635 | * @hide |
| 1636 | */ |
| 1637 | public int getTotalPaddingEnd() { |
| 1638 | return getCompoundPaddingEnd(); |
| 1639 | } |
| 1640 | |
| 1641 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1642 | * Returns the total top padding of the view, including the top |
| 1643 | * Drawable if any, the extra space to keep more than maxLines |
| 1644 | * from showing, and the vertical offset for gravity, if any. |
| 1645 | */ |
| 1646 | public int getTotalPaddingTop() { |
| 1647 | return getExtendedPaddingTop() + getVerticalOffset(true); |
| 1648 | } |
| 1649 | |
| 1650 | /** |
| 1651 | * Returns the total bottom padding of the view, including the bottom |
| 1652 | * Drawable if any, the extra space to keep more than maxLines |
| 1653 | * from showing, and the vertical offset for gravity, if any. |
| 1654 | */ |
| 1655 | public int getTotalPaddingBottom() { |
| 1656 | return getExtendedPaddingBottom() + getBottomVerticalOffset(true); |
| 1657 | } |
| 1658 | |
| 1659 | /** |
| 1660 | * Sets the Drawables (if any) to appear to the left of, above, |
| 1661 | * to the right of, and below the text. Use null if you do not |
| 1662 | * want a Drawable there. The Drawables must already have had |
| 1663 | * {@link Drawable#setBounds} called. |
| 1664 | * |
| 1665 | * @attr ref android.R.styleable#TextView_drawableLeft |
| 1666 | * @attr ref android.R.styleable#TextView_drawableTop |
| 1667 | * @attr ref android.R.styleable#TextView_drawableRight |
| 1668 | * @attr ref android.R.styleable#TextView_drawableBottom |
| 1669 | */ |
| 1670 | public void setCompoundDrawables(Drawable left, Drawable top, |
| 1671 | Drawable right, Drawable bottom) { |
| 1672 | Drawables dr = mDrawables; |
| 1673 | |
| 1674 | final boolean drawables = left != null || top != null |
| 1675 | || right != null || bottom != null; |
| 1676 | |
| 1677 | if (!drawables) { |
| 1678 | // Clearing drawables... can we free the data structure? |
| 1679 | if (dr != null) { |
| 1680 | if (dr.mDrawablePadding == 0) { |
| 1681 | mDrawables = null; |
| 1682 | } else { |
| 1683 | // We need to retain the last set padding, so just clear |
| 1684 | // out all of the fields in the existing structure. |
Romain Guy | 48540eb | 2009-05-19 16:44:57 -0700 | [diff] [blame] | 1685 | if (dr.mDrawableLeft != null) dr.mDrawableLeft.setCallback(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1686 | dr.mDrawableLeft = null; |
Romain Guy | 48540eb | 2009-05-19 16:44:57 -0700 | [diff] [blame] | 1687 | if (dr.mDrawableTop != null) dr.mDrawableTop.setCallback(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1688 | dr.mDrawableTop = null; |
Romain Guy | 48540eb | 2009-05-19 16:44:57 -0700 | [diff] [blame] | 1689 | if (dr.mDrawableRight != null) dr.mDrawableRight.setCallback(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1690 | dr.mDrawableRight = null; |
Romain Guy | 48540eb | 2009-05-19 16:44:57 -0700 | [diff] [blame] | 1691 | if (dr.mDrawableBottom != null) dr.mDrawableBottom.setCallback(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1692 | dr.mDrawableBottom = null; |
| 1693 | dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0; |
| 1694 | dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0; |
| 1695 | dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0; |
| 1696 | dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0; |
| 1697 | } |
| 1698 | } |
| 1699 | } else { |
| 1700 | if (dr == null) { |
| 1701 | mDrawables = dr = new Drawables(); |
| 1702 | } |
| 1703 | |
Romain Guy | 48540eb | 2009-05-19 16:44:57 -0700 | [diff] [blame] | 1704 | if (dr.mDrawableLeft != left && dr.mDrawableLeft != null) { |
| 1705 | dr.mDrawableLeft.setCallback(null); |
| 1706 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1707 | dr.mDrawableLeft = left; |
Romain Guy | 8e618e5 | 2010-03-08 12:18:20 -0800 | [diff] [blame] | 1708 | |
| 1709 | if (dr.mDrawableTop != top && dr.mDrawableTop != null) { |
Romain Guy | 48540eb | 2009-05-19 16:44:57 -0700 | [diff] [blame] | 1710 | dr.mDrawableTop.setCallback(null); |
| 1711 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1712 | dr.mDrawableTop = top; |
Romain Guy | 8e618e5 | 2010-03-08 12:18:20 -0800 | [diff] [blame] | 1713 | |
| 1714 | if (dr.mDrawableRight != right && dr.mDrawableRight != null) { |
Romain Guy | 48540eb | 2009-05-19 16:44:57 -0700 | [diff] [blame] | 1715 | dr.mDrawableRight.setCallback(null); |
| 1716 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1717 | dr.mDrawableRight = right; |
Romain Guy | 8e618e5 | 2010-03-08 12:18:20 -0800 | [diff] [blame] | 1718 | |
| 1719 | if (dr.mDrawableBottom != bottom && dr.mDrawableBottom != null) { |
Romain Guy | 48540eb | 2009-05-19 16:44:57 -0700 | [diff] [blame] | 1720 | dr.mDrawableBottom.setCallback(null); |
| 1721 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1722 | dr.mDrawableBottom = bottom; |
| 1723 | |
| 1724 | final Rect compoundRect = dr.mCompoundRect; |
Romain Guy | 48540eb | 2009-05-19 16:44:57 -0700 | [diff] [blame] | 1725 | int[] state; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1726 | |
| 1727 | state = getDrawableState(); |
| 1728 | |
| 1729 | if (left != null) { |
| 1730 | left.setState(state); |
| 1731 | left.copyBounds(compoundRect); |
Romain Guy | 48540eb | 2009-05-19 16:44:57 -0700 | [diff] [blame] | 1732 | left.setCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1733 | dr.mDrawableSizeLeft = compoundRect.width(); |
| 1734 | dr.mDrawableHeightLeft = compoundRect.height(); |
| 1735 | } else { |
| 1736 | dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0; |
| 1737 | } |
| 1738 | |
| 1739 | if (right != null) { |
| 1740 | right.setState(state); |
| 1741 | right.copyBounds(compoundRect); |
Romain Guy | 48540eb | 2009-05-19 16:44:57 -0700 | [diff] [blame] | 1742 | right.setCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1743 | dr.mDrawableSizeRight = compoundRect.width(); |
| 1744 | dr.mDrawableHeightRight = compoundRect.height(); |
| 1745 | } else { |
| 1746 | dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0; |
| 1747 | } |
| 1748 | |
| 1749 | if (top != null) { |
| 1750 | top.setState(state); |
| 1751 | top.copyBounds(compoundRect); |
Romain Guy | 48540eb | 2009-05-19 16:44:57 -0700 | [diff] [blame] | 1752 | top.setCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1753 | dr.mDrawableSizeTop = compoundRect.height(); |
| 1754 | dr.mDrawableWidthTop = compoundRect.width(); |
| 1755 | } else { |
| 1756 | dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0; |
| 1757 | } |
| 1758 | |
| 1759 | if (bottom != null) { |
| 1760 | bottom.setState(state); |
| 1761 | bottom.copyBounds(compoundRect); |
Romain Guy | 48540eb | 2009-05-19 16:44:57 -0700 | [diff] [blame] | 1762 | bottom.setCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1763 | dr.mDrawableSizeBottom = compoundRect.height(); |
| 1764 | dr.mDrawableWidthBottom = compoundRect.width(); |
| 1765 | } else { |
| 1766 | dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0; |
| 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | invalidate(); |
| 1771 | requestLayout(); |
| 1772 | } |
| 1773 | |
| 1774 | /** |
| 1775 | * Sets the Drawables (if any) to appear to the left of, above, |
| 1776 | * to the right of, and below the text. Use 0 if you do not |
| 1777 | * want a Drawable there. The Drawables' bounds will be set to |
| 1778 | * their intrinsic bounds. |
| 1779 | * |
| 1780 | * @param left Resource identifier of the left Drawable. |
| 1781 | * @param top Resource identifier of the top Drawable. |
| 1782 | * @param right Resource identifier of the right Drawable. |
| 1783 | * @param bottom Resource identifier of the bottom Drawable. |
| 1784 | * |
| 1785 | * @attr ref android.R.styleable#TextView_drawableLeft |
| 1786 | * @attr ref android.R.styleable#TextView_drawableTop |
| 1787 | * @attr ref android.R.styleable#TextView_drawableRight |
| 1788 | * @attr ref android.R.styleable#TextView_drawableBottom |
| 1789 | */ |
| 1790 | public void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) { |
| 1791 | final Resources resources = getContext().getResources(); |
| 1792 | setCompoundDrawablesWithIntrinsicBounds(left != 0 ? resources.getDrawable(left) : null, |
| 1793 | top != 0 ? resources.getDrawable(top) : null, |
| 1794 | right != 0 ? resources.getDrawable(right) : null, |
| 1795 | bottom != 0 ? resources.getDrawable(bottom) : null); |
| 1796 | } |
| 1797 | |
| 1798 | /** |
| 1799 | * Sets the Drawables (if any) to appear to the left of, above, |
| 1800 | * to the right of, and below the text. Use null if you do not |
| 1801 | * want a Drawable there. The Drawables' bounds will be set to |
| 1802 | * their intrinsic bounds. |
| 1803 | * |
| 1804 | * @attr ref android.R.styleable#TextView_drawableLeft |
| 1805 | * @attr ref android.R.styleable#TextView_drawableTop |
| 1806 | * @attr ref android.R.styleable#TextView_drawableRight |
| 1807 | * @attr ref android.R.styleable#TextView_drawableBottom |
| 1808 | */ |
| 1809 | public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, |
| 1810 | Drawable right, Drawable bottom) { |
| 1811 | |
| 1812 | if (left != null) { |
| 1813 | left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight()); |
| 1814 | } |
| 1815 | if (right != null) { |
| 1816 | right.setBounds(0, 0, right.getIntrinsicWidth(), right.getIntrinsicHeight()); |
| 1817 | } |
| 1818 | if (top != null) { |
| 1819 | top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight()); |
| 1820 | } |
| 1821 | if (bottom != null) { |
| 1822 | bottom.setBounds(0, 0, bottom.getIntrinsicWidth(), bottom.getIntrinsicHeight()); |
| 1823 | } |
| 1824 | setCompoundDrawables(left, top, right, bottom); |
| 1825 | } |
| 1826 | |
| 1827 | /** |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 1828 | * Sets the Drawables (if any) to appear to the start of, above, |
| 1829 | * to the end of, and below the text. Use null if you do not |
| 1830 | * want a Drawable there. The Drawables must already have had |
| 1831 | * {@link Drawable#setBounds} called. |
| 1832 | * |
| 1833 | * @attr ref android.R.styleable#TextView_drawableStart |
| 1834 | * @attr ref android.R.styleable#TextView_drawableTop |
| 1835 | * @attr ref android.R.styleable#TextView_drawableEnd |
| 1836 | * @attr ref android.R.styleable#TextView_drawableBottom |
| 1837 | * |
| 1838 | * @hide |
| 1839 | */ |
| 1840 | public void setCompoundDrawablesRelative(Drawable start, Drawable top, |
| 1841 | Drawable end, Drawable bottom) { |
| 1842 | Drawables dr = mDrawables; |
| 1843 | |
| 1844 | final boolean drawables = start != null || top != null |
| 1845 | || end != null || bottom != null; |
| 1846 | |
| 1847 | if (!drawables) { |
| 1848 | // Clearing drawables... can we free the data structure? |
| 1849 | if (dr != null) { |
| 1850 | if (dr.mDrawablePadding == 0) { |
| 1851 | mDrawables = null; |
| 1852 | } else { |
| 1853 | // We need to retain the last set padding, so just clear |
| 1854 | // out all of the fields in the existing structure. |
| 1855 | if (dr.mDrawableStart != null) dr.mDrawableStart.setCallback(null); |
| 1856 | dr.mDrawableStart = null; |
| 1857 | if (dr.mDrawableTop != null) dr.mDrawableTop.setCallback(null); |
| 1858 | dr.mDrawableTop = null; |
| 1859 | if (dr.mDrawableEnd != null) dr.mDrawableEnd.setCallback(null); |
| 1860 | dr.mDrawableEnd = null; |
| 1861 | if (dr.mDrawableBottom != null) dr.mDrawableBottom.setCallback(null); |
| 1862 | dr.mDrawableBottom = null; |
| 1863 | dr.mDrawableSizeStart = dr.mDrawableHeightStart = 0; |
| 1864 | dr.mDrawableSizeEnd = dr.mDrawableHeightEnd = 0; |
| 1865 | dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0; |
| 1866 | dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0; |
| 1867 | } |
| 1868 | } |
| 1869 | } else { |
| 1870 | if (dr == null) { |
| 1871 | mDrawables = dr = new Drawables(); |
| 1872 | } |
| 1873 | |
| 1874 | if (dr.mDrawableStart != start && dr.mDrawableStart != null) { |
| 1875 | dr.mDrawableStart.setCallback(null); |
| 1876 | } |
| 1877 | dr.mDrawableStart = start; |
| 1878 | |
| 1879 | if (dr.mDrawableTop != top && dr.mDrawableTop != null) { |
| 1880 | dr.mDrawableTop.setCallback(null); |
| 1881 | } |
| 1882 | dr.mDrawableTop = top; |
| 1883 | |
| 1884 | if (dr.mDrawableEnd != end && dr.mDrawableEnd != null) { |
| 1885 | dr.mDrawableEnd.setCallback(null); |
| 1886 | } |
| 1887 | dr.mDrawableEnd = end; |
| 1888 | |
| 1889 | if (dr.mDrawableBottom != bottom && dr.mDrawableBottom != null) { |
| 1890 | dr.mDrawableBottom.setCallback(null); |
| 1891 | } |
| 1892 | dr.mDrawableBottom = bottom; |
| 1893 | |
| 1894 | final Rect compoundRect = dr.mCompoundRect; |
| 1895 | int[] state; |
| 1896 | |
| 1897 | state = getDrawableState(); |
| 1898 | |
| 1899 | if (start != null) { |
| 1900 | start.setState(state); |
| 1901 | start.copyBounds(compoundRect); |
| 1902 | start.setCallback(this); |
| 1903 | dr.mDrawableSizeStart = compoundRect.width(); |
| 1904 | dr.mDrawableHeightStart = compoundRect.height(); |
| 1905 | } else { |
| 1906 | dr.mDrawableSizeStart = dr.mDrawableHeightStart = 0; |
| 1907 | } |
| 1908 | |
| 1909 | if (end != null) { |
| 1910 | end.setState(state); |
| 1911 | end.copyBounds(compoundRect); |
| 1912 | end.setCallback(this); |
| 1913 | dr.mDrawableSizeEnd = compoundRect.width(); |
| 1914 | dr.mDrawableHeightEnd = compoundRect.height(); |
| 1915 | } else { |
| 1916 | dr.mDrawableSizeEnd = dr.mDrawableHeightEnd = 0; |
| 1917 | } |
| 1918 | |
| 1919 | if (top != null) { |
| 1920 | top.setState(state); |
| 1921 | top.copyBounds(compoundRect); |
| 1922 | top.setCallback(this); |
| 1923 | dr.mDrawableSizeTop = compoundRect.height(); |
| 1924 | dr.mDrawableWidthTop = compoundRect.width(); |
| 1925 | } else { |
| 1926 | dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0; |
| 1927 | } |
| 1928 | |
| 1929 | if (bottom != null) { |
| 1930 | bottom.setState(state); |
| 1931 | bottom.copyBounds(compoundRect); |
| 1932 | bottom.setCallback(this); |
| 1933 | dr.mDrawableSizeBottom = compoundRect.height(); |
| 1934 | dr.mDrawableWidthBottom = compoundRect.width(); |
| 1935 | } else { |
| 1936 | dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0; |
| 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | resolveDrawables(); |
| 1941 | invalidate(); |
| 1942 | requestLayout(); |
| 1943 | } |
| 1944 | |
| 1945 | /** |
| 1946 | * Sets the Drawables (if any) to appear to the start of, above, |
| 1947 | * to the end of, and below the text. Use 0 if you do not |
| 1948 | * want a Drawable there. The Drawables' bounds will be set to |
| 1949 | * their intrinsic bounds. |
| 1950 | * |
| 1951 | * @param start Resource identifier of the start Drawable. |
| 1952 | * @param top Resource identifier of the top Drawable. |
| 1953 | * @param end Resource identifier of the end Drawable. |
| 1954 | * @param bottom Resource identifier of the bottom Drawable. |
| 1955 | * |
| 1956 | * @attr ref android.R.styleable#TextView_drawableStart |
| 1957 | * @attr ref android.R.styleable#TextView_drawableTop |
| 1958 | * @attr ref android.R.styleable#TextView_drawableEnd |
| 1959 | * @attr ref android.R.styleable#TextView_drawableBottom |
| 1960 | * |
| 1961 | * @hide |
| 1962 | */ |
| 1963 | public void setCompoundDrawablesRelativeWithIntrinsicBounds(int start, int top, int end, |
| 1964 | int bottom) { |
| 1965 | resetResolvedDrawables(); |
| 1966 | final Resources resources = getContext().getResources(); |
| 1967 | setCompoundDrawablesRelativeWithIntrinsicBounds( |
| 1968 | start != 0 ? resources.getDrawable(start) : null, |
| 1969 | top != 0 ? resources.getDrawable(top) : null, |
| 1970 | end != 0 ? resources.getDrawable(end) : null, |
| 1971 | bottom != 0 ? resources.getDrawable(bottom) : null); |
| 1972 | } |
| 1973 | |
| 1974 | /** |
| 1975 | * Sets the Drawables (if any) to appear to the start of, above, |
| 1976 | * to the end of, and below the text. Use null if you do not |
| 1977 | * want a Drawable there. The Drawables' bounds will be set to |
| 1978 | * their intrinsic bounds. |
| 1979 | * |
| 1980 | * @attr ref android.R.styleable#TextView_drawableStart |
| 1981 | * @attr ref android.R.styleable#TextView_drawableTop |
| 1982 | * @attr ref android.R.styleable#TextView_drawableEnd |
| 1983 | * @attr ref android.R.styleable#TextView_drawableBottom |
| 1984 | * |
| 1985 | * @hide |
| 1986 | */ |
| 1987 | public void setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable start, Drawable top, |
| 1988 | Drawable end, Drawable bottom) { |
| 1989 | |
| 1990 | resetResolvedDrawables(); |
| 1991 | if (start != null) { |
| 1992 | start.setBounds(0, 0, start.getIntrinsicWidth(), start.getIntrinsicHeight()); |
| 1993 | } |
| 1994 | if (end != null) { |
| 1995 | end.setBounds(0, 0, end.getIntrinsicWidth(), end.getIntrinsicHeight()); |
| 1996 | } |
| 1997 | if (top != null) { |
| 1998 | top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight()); |
| 1999 | } |
| 2000 | if (bottom != null) { |
| 2001 | bottom.setBounds(0, 0, bottom.getIntrinsicWidth(), bottom.getIntrinsicHeight()); |
| 2002 | } |
| 2003 | setCompoundDrawablesRelative(start, top, end, bottom); |
| 2004 | } |
| 2005 | |
| 2006 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2007 | * Returns drawables for the left, top, right, and bottom borders. |
| 2008 | */ |
| 2009 | public Drawable[] getCompoundDrawables() { |
| 2010 | final Drawables dr = mDrawables; |
| 2011 | if (dr != null) { |
| 2012 | return new Drawable[] { |
| 2013 | dr.mDrawableLeft, dr.mDrawableTop, dr.mDrawableRight, dr.mDrawableBottom |
| 2014 | }; |
| 2015 | } else { |
| 2016 | return new Drawable[] { null, null, null, null }; |
| 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | /** |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 2021 | * Returns drawables for the start, top, end, and bottom borders. |
| 2022 | * |
| 2023 | * @hide |
| 2024 | */ |
| 2025 | public Drawable[] getCompoundDrawablesRelative() { |
| 2026 | final Drawables dr = mDrawables; |
| 2027 | if (dr != null) { |
| 2028 | return new Drawable[] { |
| 2029 | dr.mDrawableStart, dr.mDrawableTop, dr.mDrawableEnd, dr.mDrawableBottom |
| 2030 | }; |
| 2031 | } else { |
| 2032 | return new Drawable[] { null, null, null, null }; |
| 2033 | } |
| 2034 | } |
| 2035 | |
| 2036 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2037 | * Sets the size of the padding between the compound drawables and |
| 2038 | * the text. |
| 2039 | * |
| 2040 | * @attr ref android.R.styleable#TextView_drawablePadding |
| 2041 | */ |
| 2042 | public void setCompoundDrawablePadding(int pad) { |
| 2043 | Drawables dr = mDrawables; |
| 2044 | if (pad == 0) { |
| 2045 | if (dr != null) { |
| 2046 | dr.mDrawablePadding = pad; |
| 2047 | } |
| 2048 | } else { |
| 2049 | if (dr == null) { |
| 2050 | mDrawables = dr = new Drawables(); |
| 2051 | } |
| 2052 | dr.mDrawablePadding = pad; |
| 2053 | } |
| 2054 | |
| 2055 | invalidate(); |
| 2056 | requestLayout(); |
| 2057 | } |
| 2058 | |
| 2059 | /** |
| 2060 | * Returns the padding between the compound drawables and the text. |
| 2061 | */ |
| 2062 | public int getCompoundDrawablePadding() { |
| 2063 | final Drawables dr = mDrawables; |
| 2064 | return dr != null ? dr.mDrawablePadding : 0; |
| 2065 | } |
| 2066 | |
| 2067 | @Override |
| 2068 | public void setPadding(int left, int top, int right, int bottom) { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2069 | if (left != mPaddingLeft || |
| 2070 | right != mPaddingRight || |
| 2071 | top != mPaddingTop || |
| 2072 | bottom != mPaddingBottom) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2073 | nullLayouts(); |
| 2074 | } |
| 2075 | |
| 2076 | // the super call will requestLayout() |
| 2077 | super.setPadding(left, top, right, bottom); |
| 2078 | invalidate(); |
| 2079 | } |
| 2080 | |
| 2081 | /** |
| 2082 | * Gets the autolink mask of the text. See {@link |
| 2083 | * android.text.util.Linkify#ALL Linkify.ALL} and peers for |
| 2084 | * possible values. |
| 2085 | * |
| 2086 | * @attr ref android.R.styleable#TextView_autoLink |
| 2087 | */ |
| 2088 | public final int getAutoLinkMask() { |
| 2089 | return mAutoLinkMask; |
| 2090 | } |
| 2091 | |
| 2092 | /** |
| 2093 | * Sets the text color, size, style, hint color, and highlight color |
| 2094 | * from the specified TextAppearance resource. |
| 2095 | */ |
| 2096 | public void setTextAppearance(Context context, int resid) { |
| 2097 | TypedArray appearance = |
| 2098 | context.obtainStyledAttributes(resid, |
| 2099 | com.android.internal.R.styleable.TextAppearance); |
| 2100 | |
| 2101 | int color; |
| 2102 | ColorStateList colors; |
| 2103 | int ts; |
| 2104 | |
| 2105 | color = appearance.getColor(com.android.internal.R.styleable.TextAppearance_textColorHighlight, 0); |
| 2106 | if (color != 0) { |
| 2107 | setHighlightColor(color); |
| 2108 | } |
| 2109 | |
| 2110 | colors = appearance.getColorStateList(com.android.internal.R.styleable. |
| 2111 | TextAppearance_textColor); |
| 2112 | if (colors != null) { |
| 2113 | setTextColor(colors); |
| 2114 | } |
| 2115 | |
| 2116 | ts = appearance.getDimensionPixelSize(com.android.internal.R.styleable. |
| 2117 | TextAppearance_textSize, 0); |
| 2118 | if (ts != 0) { |
| 2119 | setRawTextSize(ts); |
| 2120 | } |
| 2121 | |
| 2122 | colors = appearance.getColorStateList(com.android.internal.R.styleable. |
| 2123 | TextAppearance_textColorHint); |
| 2124 | if (colors != null) { |
| 2125 | setHintTextColor(colors); |
| 2126 | } |
| 2127 | |
| 2128 | colors = appearance.getColorStateList(com.android.internal.R.styleable. |
| 2129 | TextAppearance_textColorLink); |
| 2130 | if (colors != null) { |
| 2131 | setLinkTextColor(colors); |
| 2132 | } |
| 2133 | |
| 2134 | int typefaceIndex, styleIndex; |
| 2135 | |
| 2136 | typefaceIndex = appearance.getInt(com.android.internal.R.styleable. |
| 2137 | TextAppearance_typeface, -1); |
| 2138 | styleIndex = appearance.getInt(com.android.internal.R.styleable. |
| 2139 | TextAppearance_textStyle, -1); |
| 2140 | |
| 2141 | setTypefaceByIndex(typefaceIndex, styleIndex); |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 2142 | |
Adam Powell | 7f8f79a | 2011-07-07 18:35:54 -0700 | [diff] [blame] | 2143 | if (appearance.getBoolean(com.android.internal.R.styleable.TextAppearance_textAllCaps, |
| 2144 | false)) { |
| 2145 | setTransformationMethod(new AllCapsTransformationMethod(getContext())); |
| 2146 | } |
| 2147 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2148 | appearance.recycle(); |
| 2149 | } |
| 2150 | |
| 2151 | /** |
| 2152 | * @return the size (in pixels) of the default text size in this TextView. |
| 2153 | */ |
| 2154 | public float getTextSize() { |
| 2155 | return mTextPaint.getTextSize(); |
| 2156 | } |
| 2157 | |
| 2158 | /** |
| 2159 | * Set the default text size to the given value, interpreted as "scaled |
| 2160 | * pixel" units. This size is adjusted based on the current density and |
| 2161 | * user font size preference. |
| 2162 | * |
| 2163 | * @param size The scaled pixel size. |
| 2164 | * |
| 2165 | * @attr ref android.R.styleable#TextView_textSize |
| 2166 | */ |
| 2167 | @android.view.RemotableViewMethod |
| 2168 | public void setTextSize(float size) { |
| 2169 | setTextSize(TypedValue.COMPLEX_UNIT_SP, size); |
| 2170 | } |
| 2171 | |
| 2172 | /** |
| 2173 | * Set the default text size to a given unit and value. See {@link |
| 2174 | * TypedValue} for the possible dimension units. |
| 2175 | * |
| 2176 | * @param unit The desired dimension unit. |
| 2177 | * @param size The desired size in the given units. |
| 2178 | * |
| 2179 | * @attr ref android.R.styleable#TextView_textSize |
| 2180 | */ |
| 2181 | public void setTextSize(int unit, float size) { |
| 2182 | Context c = getContext(); |
| 2183 | Resources r; |
| 2184 | |
| 2185 | if (c == null) |
| 2186 | r = Resources.getSystem(); |
| 2187 | else |
| 2188 | r = c.getResources(); |
| 2189 | |
| 2190 | setRawTextSize(TypedValue.applyDimension( |
| 2191 | unit, size, r.getDisplayMetrics())); |
| 2192 | } |
| 2193 | |
| 2194 | private void setRawTextSize(float size) { |
| 2195 | if (size != mTextPaint.getTextSize()) { |
| 2196 | mTextPaint.setTextSize(size); |
| 2197 | |
| 2198 | if (mLayout != null) { |
| 2199 | nullLayouts(); |
| 2200 | requestLayout(); |
| 2201 | invalidate(); |
| 2202 | } |
| 2203 | } |
| 2204 | } |
| 2205 | |
| 2206 | /** |
| 2207 | * @return the extent by which text is currently being stretched |
| 2208 | * horizontally. This will usually be 1. |
| 2209 | */ |
| 2210 | public float getTextScaleX() { |
| 2211 | return mTextPaint.getTextScaleX(); |
| 2212 | } |
| 2213 | |
| 2214 | /** |
| 2215 | * Sets the extent by which text should be stretched horizontally. |
| 2216 | * |
| 2217 | * @attr ref android.R.styleable#TextView_textScaleX |
| 2218 | */ |
| 2219 | @android.view.RemotableViewMethod |
| 2220 | public void setTextScaleX(float size) { |
| 2221 | if (size != mTextPaint.getTextScaleX()) { |
Romain Guy | 939151f | 2009-04-08 14:22:40 -0700 | [diff] [blame] | 2222 | mUserSetTextScaleX = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2223 | mTextPaint.setTextScaleX(size); |
| 2224 | |
| 2225 | if (mLayout != null) { |
| 2226 | nullLayouts(); |
| 2227 | requestLayout(); |
| 2228 | invalidate(); |
| 2229 | } |
| 2230 | } |
| 2231 | } |
| 2232 | |
| 2233 | /** |
| 2234 | * Sets the typeface and style in which the text should be displayed. |
| 2235 | * Note that not all Typeface families actually have bold and italic |
| 2236 | * variants, so you may need to use |
| 2237 | * {@link #setTypeface(Typeface, int)} to get the appearance |
| 2238 | * that you actually want. |
| 2239 | * |
| 2240 | * @attr ref android.R.styleable#TextView_typeface |
| 2241 | * @attr ref android.R.styleable#TextView_textStyle |
| 2242 | */ |
| 2243 | public void setTypeface(Typeface tf) { |
| 2244 | if (mTextPaint.getTypeface() != tf) { |
| 2245 | mTextPaint.setTypeface(tf); |
| 2246 | |
| 2247 | if (mLayout != null) { |
| 2248 | nullLayouts(); |
| 2249 | requestLayout(); |
| 2250 | invalidate(); |
| 2251 | } |
| 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | /** |
| 2256 | * @return the current typeface and style in which the text is being |
| 2257 | * displayed. |
| 2258 | */ |
| 2259 | public Typeface getTypeface() { |
| 2260 | return mTextPaint.getTypeface(); |
| 2261 | } |
| 2262 | |
| 2263 | /** |
| 2264 | * Sets the text color for all the states (normal, selected, |
| 2265 | * focused) to be this color. |
| 2266 | * |
| 2267 | * @attr ref android.R.styleable#TextView_textColor |
| 2268 | */ |
| 2269 | @android.view.RemotableViewMethod |
| 2270 | public void setTextColor(int color) { |
| 2271 | mTextColor = ColorStateList.valueOf(color); |
| 2272 | updateTextColors(); |
| 2273 | } |
| 2274 | |
| 2275 | /** |
| 2276 | * Sets the text color. |
| 2277 | * |
| 2278 | * @attr ref android.R.styleable#TextView_textColor |
| 2279 | */ |
| 2280 | public void setTextColor(ColorStateList colors) { |
| 2281 | if (colors == null) { |
| 2282 | throw new NullPointerException(); |
| 2283 | } |
| 2284 | |
| 2285 | mTextColor = colors; |
| 2286 | updateTextColors(); |
| 2287 | } |
| 2288 | |
| 2289 | /** |
| 2290 | * Return the set of text colors. |
| 2291 | * |
| 2292 | * @return Returns the set of text colors. |
| 2293 | */ |
| 2294 | public final ColorStateList getTextColors() { |
| 2295 | return mTextColor; |
| 2296 | } |
| 2297 | |
| 2298 | /** |
| 2299 | * <p>Return the current color selected for normal text.</p> |
| 2300 | * |
| 2301 | * @return Returns the current text color. |
| 2302 | */ |
| 2303 | public final int getCurrentTextColor() { |
| 2304 | return mCurTextColor; |
| 2305 | } |
| 2306 | |
| 2307 | /** |
| 2308 | * Sets the color used to display the selection highlight. |
| 2309 | * |
| 2310 | * @attr ref android.R.styleable#TextView_textColorHighlight |
| 2311 | */ |
| 2312 | @android.view.RemotableViewMethod |
| 2313 | public void setHighlightColor(int color) { |
| 2314 | if (mHighlightColor != color) { |
| 2315 | mHighlightColor = color; |
| 2316 | invalidate(); |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | /** |
| 2321 | * Gives the text a shadow of the specified radius and color, the specified |
| 2322 | * distance from its normal position. |
| 2323 | * |
| 2324 | * @attr ref android.R.styleable#TextView_shadowColor |
| 2325 | * @attr ref android.R.styleable#TextView_shadowDx |
| 2326 | * @attr ref android.R.styleable#TextView_shadowDy |
| 2327 | * @attr ref android.R.styleable#TextView_shadowRadius |
| 2328 | */ |
| 2329 | public void setShadowLayer(float radius, float dx, float dy, int color) { |
| 2330 | mTextPaint.setShadowLayer(radius, dx, dy, color); |
| 2331 | |
| 2332 | mShadowRadius = radius; |
| 2333 | mShadowDx = dx; |
| 2334 | mShadowDy = dy; |
| 2335 | |
| 2336 | invalidate(); |
| 2337 | } |
| 2338 | |
| 2339 | /** |
| 2340 | * @return the base paint used for the text. Please use this only to |
| 2341 | * consult the Paint's properties and not to change them. |
| 2342 | */ |
| 2343 | public TextPaint getPaint() { |
| 2344 | return mTextPaint; |
| 2345 | } |
| 2346 | |
| 2347 | /** |
| 2348 | * Sets the autolink mask of the text. See {@link |
| 2349 | * android.text.util.Linkify#ALL Linkify.ALL} and peers for |
| 2350 | * possible values. |
| 2351 | * |
| 2352 | * @attr ref android.R.styleable#TextView_autoLink |
| 2353 | */ |
| 2354 | @android.view.RemotableViewMethod |
| 2355 | public final void setAutoLinkMask(int mask) { |
| 2356 | mAutoLinkMask = mask; |
| 2357 | } |
| 2358 | |
| 2359 | /** |
| 2360 | * Sets whether the movement method will automatically be set to |
| 2361 | * {@link LinkMovementMethod} if {@link #setAutoLinkMask} has been |
| 2362 | * set to nonzero and links are detected in {@link #setText}. |
| 2363 | * The default is true. |
| 2364 | * |
| 2365 | * @attr ref android.R.styleable#TextView_linksClickable |
| 2366 | */ |
| 2367 | @android.view.RemotableViewMethod |
| 2368 | public final void setLinksClickable(boolean whether) { |
| 2369 | mLinksClickable = whether; |
| 2370 | } |
| 2371 | |
| 2372 | /** |
| 2373 | * Returns whether the movement method will automatically be set to |
| 2374 | * {@link LinkMovementMethod} if {@link #setAutoLinkMask} has been |
| 2375 | * set to nonzero and links are detected in {@link #setText}. |
| 2376 | * The default is true. |
| 2377 | * |
| 2378 | * @attr ref android.R.styleable#TextView_linksClickable |
| 2379 | */ |
| 2380 | public final boolean getLinksClickable() { |
| 2381 | return mLinksClickable; |
| 2382 | } |
| 2383 | |
| 2384 | /** |
Gilles Debunne | 550efbf | 2011-10-10 16:49:02 -0700 | [diff] [blame] | 2385 | * Sets whether the soft input method will be made visible when this |
| 2386 | * TextView gets focused. The default is true. |
| 2387 | * |
Gilles Debunne | 0f4109e | 2011-10-19 11:26:21 -0700 | [diff] [blame] | 2388 | * @attr ref android.R.styleable#TextView_softInputShownOnFocus |
Gilles Debunne | 550efbf | 2011-10-10 16:49:02 -0700 | [diff] [blame] | 2389 | * @hide |
| 2390 | */ |
| 2391 | @android.view.RemotableViewMethod |
Gilles Debunne | 0f4109e | 2011-10-19 11:26:21 -0700 | [diff] [blame] | 2392 | public final void setSoftInputShownOnFocus(boolean show) { |
| 2393 | mSoftInputShownOnFocus = show; |
Gilles Debunne | 550efbf | 2011-10-10 16:49:02 -0700 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | /** |
| 2397 | * Returns whether the soft input method will be made visible when this |
| 2398 | * TextView gets focused. The default is true. |
| 2399 | * |
Gilles Debunne | 0f4109e | 2011-10-19 11:26:21 -0700 | [diff] [blame] | 2400 | * @attr ref android.R.styleable#TextView_softInputShownOnFocus |
Gilles Debunne | 550efbf | 2011-10-10 16:49:02 -0700 | [diff] [blame] | 2401 | * @hide |
| 2402 | */ |
Gilles Debunne | 0f4109e | 2011-10-19 11:26:21 -0700 | [diff] [blame] | 2403 | public final boolean getSoftInputShownOnFocus() { |
| 2404 | return mSoftInputShownOnFocus; |
Gilles Debunne | 550efbf | 2011-10-10 16:49:02 -0700 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2408 | * Returns the list of URLSpans attached to the text |
| 2409 | * (by {@link Linkify} or otherwise) if any. You can call |
| 2410 | * {@link URLSpan#getURL} on them to find where they link to |
| 2411 | * or use {@link Spanned#getSpanStart} and {@link Spanned#getSpanEnd} |
| 2412 | * to find the region of the text they are attached to. |
| 2413 | */ |
| 2414 | public URLSpan[] getUrls() { |
| 2415 | if (mText instanceof Spanned) { |
| 2416 | return ((Spanned) mText).getSpans(0, mText.length(), URLSpan.class); |
| 2417 | } else { |
| 2418 | return new URLSpan[0]; |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | /** |
| 2423 | * Sets the color of the hint text. |
| 2424 | * |
| 2425 | * @attr ref android.R.styleable#TextView_textColorHint |
| 2426 | */ |
| 2427 | @android.view.RemotableViewMethod |
| 2428 | public final void setHintTextColor(int color) { |
| 2429 | mHintTextColor = ColorStateList.valueOf(color); |
| 2430 | updateTextColors(); |
| 2431 | } |
| 2432 | |
| 2433 | /** |
| 2434 | * Sets the color of the hint text. |
| 2435 | * |
| 2436 | * @attr ref android.R.styleable#TextView_textColorHint |
| 2437 | */ |
| 2438 | public final void setHintTextColor(ColorStateList colors) { |
| 2439 | mHintTextColor = colors; |
| 2440 | updateTextColors(); |
| 2441 | } |
| 2442 | |
| 2443 | /** |
| 2444 | * <p>Return the color used to paint the hint text.</p> |
| 2445 | * |
| 2446 | * @return Returns the list of hint text colors. |
| 2447 | */ |
| 2448 | public final ColorStateList getHintTextColors() { |
| 2449 | return mHintTextColor; |
| 2450 | } |
| 2451 | |
| 2452 | /** |
| 2453 | * <p>Return the current color selected to paint the hint text.</p> |
| 2454 | * |
| 2455 | * @return Returns the current hint text color. |
| 2456 | */ |
| 2457 | public final int getCurrentHintTextColor() { |
| 2458 | return mHintTextColor != null ? mCurHintTextColor : mCurTextColor; |
| 2459 | } |
| 2460 | |
| 2461 | /** |
| 2462 | * Sets the color of links in the text. |
| 2463 | * |
| 2464 | * @attr ref android.R.styleable#TextView_textColorLink |
| 2465 | */ |
| 2466 | @android.view.RemotableViewMethod |
| 2467 | public final void setLinkTextColor(int color) { |
| 2468 | mLinkTextColor = ColorStateList.valueOf(color); |
| 2469 | updateTextColors(); |
| 2470 | } |
| 2471 | |
| 2472 | /** |
| 2473 | * Sets the color of links in the text. |
| 2474 | * |
| 2475 | * @attr ref android.R.styleable#TextView_textColorLink |
| 2476 | */ |
| 2477 | public final void setLinkTextColor(ColorStateList colors) { |
| 2478 | mLinkTextColor = colors; |
| 2479 | updateTextColors(); |
| 2480 | } |
| 2481 | |
| 2482 | /** |
| 2483 | * <p>Returns the color used to paint links in the text.</p> |
| 2484 | * |
| 2485 | * @return Returns the list of link text colors. |
| 2486 | */ |
| 2487 | public final ColorStateList getLinkTextColors() { |
| 2488 | return mLinkTextColor; |
| 2489 | } |
| 2490 | |
| 2491 | /** |
| 2492 | * Sets the horizontal alignment of the text and the |
| 2493 | * vertical gravity that will be used when there is extra space |
| 2494 | * in the TextView beyond what is required for the text itself. |
| 2495 | * |
| 2496 | * @see android.view.Gravity |
| 2497 | * @attr ref android.R.styleable#TextView_gravity |
| 2498 | */ |
| 2499 | public void setGravity(int gravity) { |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 2500 | if ((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) { |
Fabrice Di Meglio | 9e3b002 | 2011-06-06 16:30:29 -0700 | [diff] [blame] | 2501 | gravity |= Gravity.START; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2502 | } |
| 2503 | if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) { |
| 2504 | gravity |= Gravity.TOP; |
| 2505 | } |
| 2506 | |
| 2507 | boolean newLayout = false; |
| 2508 | |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 2509 | if ((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) != |
| 2510 | (mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2511 | newLayout = true; |
| 2512 | } |
| 2513 | |
| 2514 | if (gravity != mGravity) { |
| 2515 | invalidate(); |
Fabrice Di Meglio | 9f51384 | 2011-10-12 11:43:27 -0700 | [diff] [blame] | 2516 | mLayoutAlignment = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | mGravity = gravity; |
| 2520 | |
| 2521 | if (mLayout != null && newLayout) { |
| 2522 | // XXX this is heavy-handed because no actual content changes. |
| 2523 | int want = mLayout.getWidth(); |
| 2524 | int hintWant = mHintLayout == null ? 0 : mHintLayout.getWidth(); |
| 2525 | |
| 2526 | makeNewLayout(want, hintWant, UNKNOWN_BORING, UNKNOWN_BORING, |
| 2527 | mRight - mLeft - getCompoundPaddingLeft() - |
| 2528 | getCompoundPaddingRight(), true); |
| 2529 | } |
| 2530 | } |
| 2531 | |
| 2532 | /** |
| 2533 | * Returns the horizontal and vertical alignment of this TextView. |
| 2534 | * |
| 2535 | * @see android.view.Gravity |
| 2536 | * @attr ref android.R.styleable#TextView_gravity |
| 2537 | */ |
| 2538 | public int getGravity() { |
| 2539 | return mGravity; |
| 2540 | } |
| 2541 | |
| 2542 | /** |
| 2543 | * @return the flags on the Paint being used to display the text. |
| 2544 | * @see Paint#getFlags |
| 2545 | */ |
| 2546 | public int getPaintFlags() { |
| 2547 | return mTextPaint.getFlags(); |
| 2548 | } |
| 2549 | |
| 2550 | /** |
| 2551 | * Sets flags on the Paint being used to display the text and |
| 2552 | * reflows the text if they are different from the old flags. |
| 2553 | * @see Paint#setFlags |
| 2554 | */ |
| 2555 | @android.view.RemotableViewMethod |
| 2556 | public void setPaintFlags(int flags) { |
| 2557 | if (mTextPaint.getFlags() != flags) { |
| 2558 | mTextPaint.setFlags(flags); |
| 2559 | |
| 2560 | if (mLayout != null) { |
| 2561 | nullLayouts(); |
| 2562 | requestLayout(); |
| 2563 | invalidate(); |
| 2564 | } |
| 2565 | } |
| 2566 | } |
| 2567 | |
| 2568 | /** |
| 2569 | * Sets whether the text should be allowed to be wider than the |
| 2570 | * View is. If false, it will be wrapped to the width of the View. |
| 2571 | * |
| 2572 | * @attr ref android.R.styleable#TextView_scrollHorizontally |
| 2573 | */ |
| 2574 | public void setHorizontallyScrolling(boolean whether) { |
Gilles Debunne | 2237829 | 2011-08-12 10:38:52 -0700 | [diff] [blame] | 2575 | if (mHorizontallyScrolling != whether) { |
| 2576 | mHorizontallyScrolling = whether; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2577 | |
Gilles Debunne | 2237829 | 2011-08-12 10:38:52 -0700 | [diff] [blame] | 2578 | if (mLayout != null) { |
| 2579 | nullLayouts(); |
| 2580 | requestLayout(); |
| 2581 | invalidate(); |
| 2582 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2583 | } |
| 2584 | } |
| 2585 | |
| 2586 | /** |
Gilles Debunne | f2a0201 | 2011-10-27 11:10:14 -0700 | [diff] [blame] | 2587 | * Returns whether the text is allowed to be wider than the View is. |
| 2588 | * If false, the text will be wrapped to the width of the View. |
| 2589 | * |
| 2590 | * @attr ref android.R.styleable#TextView_scrollHorizontally |
| 2591 | * @hide |
| 2592 | */ |
| 2593 | public boolean getHorizontallyScrolling() { |
| 2594 | return mHorizontallyScrolling; |
| 2595 | } |
| 2596 | |
| 2597 | /** |
Gilles Debunne | a3ae4a0 | 2010-12-13 17:22:39 -0800 | [diff] [blame] | 2598 | * Makes the TextView at least this many lines tall. |
| 2599 | * |
| 2600 | * Setting this value overrides any other (minimum) height setting. A single line TextView will |
| 2601 | * set this value to 1. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2602 | * |
| 2603 | * @attr ref android.R.styleable#TextView_minLines |
| 2604 | */ |
| 2605 | @android.view.RemotableViewMethod |
| 2606 | public void setMinLines(int minlines) { |
| 2607 | mMinimum = minlines; |
| 2608 | mMinMode = LINES; |
| 2609 | |
| 2610 | requestLayout(); |
| 2611 | invalidate(); |
| 2612 | } |
| 2613 | |
| 2614 | /** |
Gilles Debunne | a3ae4a0 | 2010-12-13 17:22:39 -0800 | [diff] [blame] | 2615 | * Makes the TextView at least this many pixels tall. |
| 2616 | * |
| 2617 | * Setting this value overrides any other (minimum) number of lines setting. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2618 | * |
| 2619 | * @attr ref android.R.styleable#TextView_minHeight |
| 2620 | */ |
| 2621 | @android.view.RemotableViewMethod |
| 2622 | public void setMinHeight(int minHeight) { |
| 2623 | mMinimum = minHeight; |
| 2624 | mMinMode = PIXELS; |
| 2625 | |
| 2626 | requestLayout(); |
| 2627 | invalidate(); |
| 2628 | } |
| 2629 | |
| 2630 | /** |
Gilles Debunne | a3ae4a0 | 2010-12-13 17:22:39 -0800 | [diff] [blame] | 2631 | * Makes the TextView at most this many lines tall. |
| 2632 | * |
| 2633 | * Setting this value overrides any other (maximum) height setting. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2634 | * |
| 2635 | * @attr ref android.R.styleable#TextView_maxLines |
| 2636 | */ |
| 2637 | @android.view.RemotableViewMethod |
| 2638 | public void setMaxLines(int maxlines) { |
| 2639 | mMaximum = maxlines; |
| 2640 | mMaxMode = LINES; |
| 2641 | |
| 2642 | requestLayout(); |
| 2643 | invalidate(); |
| 2644 | } |
| 2645 | |
| 2646 | /** |
Gilles Debunne | a3ae4a0 | 2010-12-13 17:22:39 -0800 | [diff] [blame] | 2647 | * Makes the TextView at most this many pixels tall. This option is mutually exclusive with the |
| 2648 | * {@link #setMaxLines(int)} method. |
| 2649 | * |
| 2650 | * Setting this value overrides any other (maximum) number of lines setting. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2651 | * |
| 2652 | * @attr ref android.R.styleable#TextView_maxHeight |
| 2653 | */ |
| 2654 | @android.view.RemotableViewMethod |
| 2655 | public void setMaxHeight(int maxHeight) { |
| 2656 | mMaximum = maxHeight; |
| 2657 | mMaxMode = PIXELS; |
| 2658 | |
| 2659 | requestLayout(); |
| 2660 | invalidate(); |
| 2661 | } |
| 2662 | |
| 2663 | /** |
Gilles Debunne | a3ae4a0 | 2010-12-13 17:22:39 -0800 | [diff] [blame] | 2664 | * Makes the TextView exactly this many lines tall. |
| 2665 | * |
| 2666 | * Note that setting this value overrides any other (minimum / maximum) number of lines or |
| 2667 | * height setting. A single line TextView will set this value to 1. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2668 | * |
| 2669 | * @attr ref android.R.styleable#TextView_lines |
| 2670 | */ |
| 2671 | @android.view.RemotableViewMethod |
| 2672 | public void setLines(int lines) { |
| 2673 | mMaximum = mMinimum = lines; |
| 2674 | mMaxMode = mMinMode = LINES; |
| 2675 | |
| 2676 | requestLayout(); |
| 2677 | invalidate(); |
| 2678 | } |
| 2679 | |
| 2680 | /** |
| 2681 | * Makes the TextView exactly this many pixels tall. |
| 2682 | * You could do the same thing by specifying this number in the |
| 2683 | * LayoutParams. |
| 2684 | * |
Gilles Debunne | a3ae4a0 | 2010-12-13 17:22:39 -0800 | [diff] [blame] | 2685 | * Note that setting this value overrides any other (minimum / maximum) number of lines or |
| 2686 | * height setting. |
| 2687 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2688 | * @attr ref android.R.styleable#TextView_height |
| 2689 | */ |
| 2690 | @android.view.RemotableViewMethod |
| 2691 | public void setHeight(int pixels) { |
| 2692 | mMaximum = mMinimum = pixels; |
| 2693 | mMaxMode = mMinMode = PIXELS; |
| 2694 | |
| 2695 | requestLayout(); |
| 2696 | invalidate(); |
| 2697 | } |
| 2698 | |
| 2699 | /** |
| 2700 | * Makes the TextView at least this many ems wide |
| 2701 | * |
| 2702 | * @attr ref android.R.styleable#TextView_minEms |
| 2703 | */ |
| 2704 | @android.view.RemotableViewMethod |
| 2705 | public void setMinEms(int minems) { |
| 2706 | mMinWidth = minems; |
| 2707 | mMinWidthMode = EMS; |
| 2708 | |
| 2709 | requestLayout(); |
| 2710 | invalidate(); |
| 2711 | } |
| 2712 | |
| 2713 | /** |
| 2714 | * Makes the TextView at least this many pixels wide |
| 2715 | * |
| 2716 | * @attr ref android.R.styleable#TextView_minWidth |
| 2717 | */ |
| 2718 | @android.view.RemotableViewMethod |
| 2719 | public void setMinWidth(int minpixels) { |
| 2720 | mMinWidth = minpixels; |
| 2721 | mMinWidthMode = PIXELS; |
| 2722 | |
| 2723 | requestLayout(); |
| 2724 | invalidate(); |
| 2725 | } |
| 2726 | |
| 2727 | /** |
| 2728 | * Makes the TextView at most this many ems wide |
| 2729 | * |
| 2730 | * @attr ref android.R.styleable#TextView_maxEms |
| 2731 | */ |
| 2732 | @android.view.RemotableViewMethod |
| 2733 | public void setMaxEms(int maxems) { |
| 2734 | mMaxWidth = maxems; |
| 2735 | mMaxWidthMode = EMS; |
| 2736 | |
| 2737 | requestLayout(); |
| 2738 | invalidate(); |
| 2739 | } |
| 2740 | |
| 2741 | /** |
| 2742 | * Makes the TextView at most this many pixels wide |
| 2743 | * |
| 2744 | * @attr ref android.R.styleable#TextView_maxWidth |
| 2745 | */ |
| 2746 | @android.view.RemotableViewMethod |
| 2747 | public void setMaxWidth(int maxpixels) { |
| 2748 | mMaxWidth = maxpixels; |
| 2749 | mMaxWidthMode = PIXELS; |
| 2750 | |
| 2751 | requestLayout(); |
| 2752 | invalidate(); |
| 2753 | } |
| 2754 | |
| 2755 | /** |
| 2756 | * Makes the TextView exactly this many ems wide |
| 2757 | * |
| 2758 | * @attr ref android.R.styleable#TextView_ems |
| 2759 | */ |
| 2760 | @android.view.RemotableViewMethod |
| 2761 | public void setEms(int ems) { |
| 2762 | mMaxWidth = mMinWidth = ems; |
| 2763 | mMaxWidthMode = mMinWidthMode = EMS; |
| 2764 | |
| 2765 | requestLayout(); |
| 2766 | invalidate(); |
| 2767 | } |
| 2768 | |
| 2769 | /** |
| 2770 | * Makes the TextView exactly this many pixels wide. |
| 2771 | * You could do the same thing by specifying this number in the |
| 2772 | * LayoutParams. |
| 2773 | * |
| 2774 | * @attr ref android.R.styleable#TextView_width |
| 2775 | */ |
| 2776 | @android.view.RemotableViewMethod |
| 2777 | public void setWidth(int pixels) { |
| 2778 | mMaxWidth = mMinWidth = pixels; |
| 2779 | mMaxWidthMode = mMinWidthMode = PIXELS; |
| 2780 | |
| 2781 | requestLayout(); |
| 2782 | invalidate(); |
| 2783 | } |
| 2784 | |
| 2785 | |
| 2786 | /** |
| 2787 | * Sets line spacing for this TextView. Each line will have its height |
| 2788 | * multiplied by <code>mult</code> and have <code>add</code> added to it. |
| 2789 | * |
| 2790 | * @attr ref android.R.styleable#TextView_lineSpacingExtra |
| 2791 | * @attr ref android.R.styleable#TextView_lineSpacingMultiplier |
| 2792 | */ |
| 2793 | public void setLineSpacing(float add, float mult) { |
Gilles Debunne | 2237829 | 2011-08-12 10:38:52 -0700 | [diff] [blame] | 2794 | if (mSpacingAdd != add || mSpacingMult != mult) { |
| 2795 | mSpacingAdd = add; |
| 2796 | mSpacingMult = mult; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2797 | |
Gilles Debunne | 2237829 | 2011-08-12 10:38:52 -0700 | [diff] [blame] | 2798 | if (mLayout != null) { |
| 2799 | nullLayouts(); |
| 2800 | requestLayout(); |
| 2801 | invalidate(); |
| 2802 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2803 | } |
| 2804 | } |
| 2805 | |
| 2806 | /** |
| 2807 | * Convenience method: Append the specified text to the TextView's |
| 2808 | * display buffer, upgrading it to BufferType.EDITABLE if it was |
| 2809 | * not already editable. |
| 2810 | */ |
| 2811 | public final void append(CharSequence text) { |
| 2812 | append(text, 0, text.length()); |
| 2813 | } |
| 2814 | |
| 2815 | /** |
| 2816 | * Convenience method: Append the specified text slice to the TextView's |
| 2817 | * display buffer, upgrading it to BufferType.EDITABLE if it was |
| 2818 | * not already editable. |
| 2819 | */ |
| 2820 | public void append(CharSequence text, int start, int end) { |
| 2821 | if (!(mText instanceof Editable)) { |
| 2822 | setText(mText, BufferType.EDITABLE); |
| 2823 | } |
| 2824 | |
| 2825 | ((Editable) mText).append(text, start, end); |
| 2826 | } |
| 2827 | |
| 2828 | private void updateTextColors() { |
| 2829 | boolean inval = false; |
| 2830 | int color = mTextColor.getColorForState(getDrawableState(), 0); |
| 2831 | if (color != mCurTextColor) { |
| 2832 | mCurTextColor = color; |
| 2833 | inval = true; |
| 2834 | } |
| 2835 | if (mLinkTextColor != null) { |
| 2836 | color = mLinkTextColor.getColorForState(getDrawableState(), 0); |
| 2837 | if (color != mTextPaint.linkColor) { |
| 2838 | mTextPaint.linkColor = color; |
| 2839 | inval = true; |
| 2840 | } |
| 2841 | } |
| 2842 | if (mHintTextColor != null) { |
| 2843 | color = mHintTextColor.getColorForState(getDrawableState(), 0); |
| 2844 | if (color != mCurHintTextColor && mText.length() == 0) { |
| 2845 | mCurHintTextColor = color; |
| 2846 | inval = true; |
| 2847 | } |
| 2848 | } |
| 2849 | if (inval) { |
| 2850 | invalidate(); |
| 2851 | } |
| 2852 | } |
| 2853 | |
| 2854 | @Override |
| 2855 | protected void drawableStateChanged() { |
| 2856 | super.drawableStateChanged(); |
| 2857 | if (mTextColor != null && mTextColor.isStateful() |
| 2858 | || (mHintTextColor != null && mHintTextColor.isStateful()) |
| 2859 | || (mLinkTextColor != null && mLinkTextColor.isStateful())) { |
| 2860 | updateTextColors(); |
| 2861 | } |
| 2862 | |
| 2863 | final Drawables dr = mDrawables; |
| 2864 | if (dr != null) { |
| 2865 | int[] state = getDrawableState(); |
| 2866 | if (dr.mDrawableTop != null && dr.mDrawableTop.isStateful()) { |
| 2867 | dr.mDrawableTop.setState(state); |
| 2868 | } |
| 2869 | if (dr.mDrawableBottom != null && dr.mDrawableBottom.isStateful()) { |
| 2870 | dr.mDrawableBottom.setState(state); |
| 2871 | } |
| 2872 | if (dr.mDrawableLeft != null && dr.mDrawableLeft.isStateful()) { |
| 2873 | dr.mDrawableLeft.setState(state); |
| 2874 | } |
| 2875 | if (dr.mDrawableRight != null && dr.mDrawableRight.isStateful()) { |
| 2876 | dr.mDrawableRight.setState(state); |
| 2877 | } |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 2878 | if (dr.mDrawableStart != null && dr.mDrawableStart.isStateful()) { |
| 2879 | dr.mDrawableStart.setState(state); |
| 2880 | } |
| 2881 | if (dr.mDrawableEnd != null && dr.mDrawableEnd.isStateful()) { |
| 2882 | dr.mDrawableEnd.setState(state); |
| 2883 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2884 | } |
| 2885 | } |
| 2886 | |
| 2887 | /** |
| 2888 | * User interface state that is stored by TextView for implementing |
| 2889 | * {@link View#onSaveInstanceState}. |
| 2890 | */ |
| 2891 | public static class SavedState extends BaseSavedState { |
| 2892 | int selStart; |
| 2893 | int selEnd; |
| 2894 | CharSequence text; |
| 2895 | boolean frozenWithFocus; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 2896 | CharSequence error; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2897 | |
| 2898 | SavedState(Parcelable superState) { |
| 2899 | super(superState); |
| 2900 | } |
| 2901 | |
| 2902 | @Override |
| 2903 | public void writeToParcel(Parcel out, int flags) { |
| 2904 | super.writeToParcel(out, flags); |
| 2905 | out.writeInt(selStart); |
| 2906 | out.writeInt(selEnd); |
| 2907 | out.writeInt(frozenWithFocus ? 1 : 0); |
| 2908 | TextUtils.writeToParcel(text, out, flags); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 2909 | |
| 2910 | if (error == null) { |
| 2911 | out.writeInt(0); |
| 2912 | } else { |
| 2913 | out.writeInt(1); |
| 2914 | TextUtils.writeToParcel(error, out, flags); |
| 2915 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | @Override |
| 2919 | public String toString() { |
| 2920 | String str = "TextView.SavedState{" |
| 2921 | + Integer.toHexString(System.identityHashCode(this)) |
| 2922 | + " start=" + selStart + " end=" + selEnd; |
| 2923 | if (text != null) { |
| 2924 | str += " text=" + text; |
| 2925 | } |
| 2926 | return str + "}"; |
| 2927 | } |
| 2928 | |
Gilles Debunne | e15b358 | 2010-06-16 15:17:21 -0700 | [diff] [blame] | 2929 | @SuppressWarnings("hiding") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2930 | public static final Parcelable.Creator<SavedState> CREATOR |
| 2931 | = new Parcelable.Creator<SavedState>() { |
| 2932 | public SavedState createFromParcel(Parcel in) { |
| 2933 | return new SavedState(in); |
| 2934 | } |
| 2935 | |
| 2936 | public SavedState[] newArray(int size) { |
| 2937 | return new SavedState[size]; |
| 2938 | } |
| 2939 | }; |
| 2940 | |
| 2941 | private SavedState(Parcel in) { |
| 2942 | super(in); |
| 2943 | selStart = in.readInt(); |
| 2944 | selEnd = in.readInt(); |
| 2945 | frozenWithFocus = (in.readInt() != 0); |
| 2946 | text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 2947 | |
| 2948 | if (in.readInt() != 0) { |
| 2949 | error = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
| 2950 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2951 | } |
| 2952 | } |
| 2953 | |
| 2954 | @Override |
| 2955 | public Parcelable onSaveInstanceState() { |
| 2956 | Parcelable superState = super.onSaveInstanceState(); |
| 2957 | |
| 2958 | // Save state if we are forced to |
| 2959 | boolean save = mFreezesText; |
| 2960 | int start = 0; |
| 2961 | int end = 0; |
| 2962 | |
| 2963 | if (mText != null) { |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 2964 | start = getSelectionStart(); |
| 2965 | end = getSelectionEnd(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2966 | if (start >= 0 || end >= 0) { |
| 2967 | // Or save state if there is a selection |
| 2968 | save = true; |
| 2969 | } |
| 2970 | } |
| 2971 | |
| 2972 | if (save) { |
| 2973 | SavedState ss = new SavedState(superState); |
| 2974 | // XXX Should also save the current scroll position! |
| 2975 | ss.selStart = start; |
| 2976 | ss.selEnd = end; |
| 2977 | |
| 2978 | if (mText instanceof Spanned) { |
| 2979 | /* |
| 2980 | * Calling setText() strips off any ChangeWatchers; |
| 2981 | * strip them now to avoid leaking references. |
| 2982 | * But do it to a copy so that if there are any |
| 2983 | * further changes to the text of this view, it |
| 2984 | * won't get into an inconsistent state. |
| 2985 | */ |
| 2986 | |
| 2987 | Spannable sp = new SpannableString(mText); |
| 2988 | |
Gilles Debunne | 176cd0d | 2011-09-29 16:37:27 -0700 | [diff] [blame] | 2989 | for (ChangeWatcher cw : sp.getSpans(0, sp.length(), ChangeWatcher.class)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2990 | sp.removeSpan(cw); |
| 2991 | } |
| 2992 | |
Gilles Debunne | 9d8d3f1 | 2011-10-13 12:15:10 -0700 | [diff] [blame] | 2993 | removeMisspelledSpans(sp); |
Gilles Debunne | aa67eef | 2011-06-01 18:03:37 -0700 | [diff] [blame] | 2994 | sp.removeSpan(mSuggestionRangeSpan); |
| 2995 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2996 | ss.text = sp; |
| 2997 | } else { |
| 2998 | ss.text = mText.toString(); |
| 2999 | } |
| 3000 | |
| 3001 | if (isFocused() && start >= 0 && end >= 0) { |
| 3002 | ss.frozenWithFocus = true; |
| 3003 | } |
| 3004 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3005 | ss.error = mError; |
| 3006 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3007 | return ss; |
| 3008 | } |
| 3009 | |
| 3010 | return superState; |
| 3011 | } |
| 3012 | |
Gilles Debunne | 9d8d3f1 | 2011-10-13 12:15:10 -0700 | [diff] [blame] | 3013 | void removeMisspelledSpans(Spannable spannable) { |
| 3014 | SuggestionSpan[] suggestionSpans = spannable.getSpans(0, spannable.length(), |
| 3015 | SuggestionSpan.class); |
| 3016 | for (int i = 0; i < suggestionSpans.length; i++) { |
| 3017 | int flags = suggestionSpans[i].getFlags(); |
| 3018 | if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0 |
| 3019 | && (flags & SuggestionSpan.FLAG_MISSPELLED) != 0) { |
| 3020 | spannable.removeSpan(suggestionSpans[i]); |
| 3021 | } |
| 3022 | } |
| 3023 | } |
| 3024 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3025 | @Override |
| 3026 | public void onRestoreInstanceState(Parcelable state) { |
| 3027 | if (!(state instanceof SavedState)) { |
| 3028 | super.onRestoreInstanceState(state); |
| 3029 | return; |
| 3030 | } |
| 3031 | |
| 3032 | SavedState ss = (SavedState)state; |
| 3033 | super.onRestoreInstanceState(ss.getSuperState()); |
| 3034 | |
| 3035 | // XXX restore buffer type too, as well as lots of other stuff |
| 3036 | if (ss.text != null) { |
| 3037 | setText(ss.text); |
| 3038 | } |
| 3039 | |
| 3040 | if (ss.selStart >= 0 && ss.selEnd >= 0) { |
| 3041 | if (mText instanceof Spannable) { |
| 3042 | int len = mText.length(); |
| 3043 | |
| 3044 | if (ss.selStart > len || ss.selEnd > len) { |
| 3045 | String restored = ""; |
| 3046 | |
| 3047 | if (ss.text != null) { |
| 3048 | restored = "(restored) "; |
| 3049 | } |
| 3050 | |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 3051 | Log.e(LOG_TAG, "Saved cursor position " + ss.selStart + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3052 | "/" + ss.selEnd + " out of range for " + restored + |
| 3053 | "text " + mText); |
| 3054 | } else { |
| 3055 | Selection.setSelection((Spannable) mText, ss.selStart, |
| 3056 | ss.selEnd); |
| 3057 | |
| 3058 | if (ss.frozenWithFocus) { |
| 3059 | mFrozenWithFocus = true; |
| 3060 | } |
| 3061 | } |
| 3062 | } |
| 3063 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3064 | |
| 3065 | if (ss.error != null) { |
Romain Guy | 9bc9fa1 | 2009-07-21 16:57:29 -0700 | [diff] [blame] | 3066 | final CharSequence error = ss.error; |
| 3067 | // Display the error later, after the first layout pass |
| 3068 | post(new Runnable() { |
| 3069 | public void run() { |
| 3070 | setError(error); |
| 3071 | } |
| 3072 | }); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3073 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | /** |
| 3077 | * Control whether this text view saves its entire text contents when |
| 3078 | * freezing to an icicle, in addition to dynamic state such as cursor |
| 3079 | * position. By default this is false, not saving the text. Set to true |
| 3080 | * if the text in the text view is not being saved somewhere else in |
| 3081 | * persistent storage (such as in a content provider) so that if the |
| 3082 | * view is later thawed the user will not lose their data. |
| 3083 | * |
| 3084 | * @param freezesText Controls whether a frozen icicle should include the |
| 3085 | * entire text data: true to include it, false to not. |
| 3086 | * |
| 3087 | * @attr ref android.R.styleable#TextView_freezesText |
| 3088 | */ |
| 3089 | @android.view.RemotableViewMethod |
| 3090 | public void setFreezesText(boolean freezesText) { |
| 3091 | mFreezesText = freezesText; |
| 3092 | } |
| 3093 | |
| 3094 | /** |
| 3095 | * Return whether this text view is including its entire text contents |
| 3096 | * in frozen icicles. |
| 3097 | * |
| 3098 | * @return Returns true if text is included, false if it isn't. |
| 3099 | * |
| 3100 | * @see #setFreezesText |
| 3101 | */ |
| 3102 | public boolean getFreezesText() { |
| 3103 | return mFreezesText; |
| 3104 | } |
| 3105 | |
| 3106 | /////////////////////////////////////////////////////////////////////////// |
| 3107 | |
| 3108 | /** |
| 3109 | * Sets the Factory used to create new Editables. |
| 3110 | */ |
| 3111 | public final void setEditableFactory(Editable.Factory factory) { |
| 3112 | mEditableFactory = factory; |
| 3113 | setText(mText); |
| 3114 | } |
| 3115 | |
| 3116 | /** |
| 3117 | * Sets the Factory used to create new Spannables. |
| 3118 | */ |
| 3119 | public final void setSpannableFactory(Spannable.Factory factory) { |
| 3120 | mSpannableFactory = factory; |
| 3121 | setText(mText); |
| 3122 | } |
| 3123 | |
| 3124 | /** |
| 3125 | * Sets the string value of the TextView. TextView <em>does not</em> accept |
| 3126 | * HTML-like formatting, which you can do with text strings in XML resource files. |
| 3127 | * To style your strings, attach android.text.style.* objects to a |
| 3128 | * {@link android.text.SpannableString SpannableString}, or see the |
| 3129 | * <a href="{@docRoot}guide/topics/resources/available-resources.html#stringresources"> |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 3130 | * Available Resource Types</a> documentation for an example of setting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3131 | * formatted text in the XML resource file. |
| 3132 | * |
| 3133 | * @attr ref android.R.styleable#TextView_text |
| 3134 | */ |
| 3135 | @android.view.RemotableViewMethod |
| 3136 | public final void setText(CharSequence text) { |
| 3137 | setText(text, mBufferType); |
| 3138 | } |
| 3139 | |
| 3140 | /** |
| 3141 | * Like {@link #setText(CharSequence)}, |
| 3142 | * except that the cursor position (if any) is retained in the new text. |
| 3143 | * |
| 3144 | * @param text The new text to place in the text view. |
| 3145 | * |
| 3146 | * @see #setText(CharSequence) |
| 3147 | */ |
| 3148 | @android.view.RemotableViewMethod |
| 3149 | public final void setTextKeepState(CharSequence text) { |
| 3150 | setTextKeepState(text, mBufferType); |
| 3151 | } |
| 3152 | |
| 3153 | /** |
| 3154 | * Sets the text that this TextView is to display (see |
| 3155 | * {@link #setText(CharSequence)}) and also sets whether it is stored |
| 3156 | * in a styleable/spannable buffer and whether it is editable. |
| 3157 | * |
| 3158 | * @attr ref android.R.styleable#TextView_text |
| 3159 | * @attr ref android.R.styleable#TextView_bufferType |
| 3160 | */ |
| 3161 | public void setText(CharSequence text, BufferType type) { |
| 3162 | setText(text, type, true, 0); |
| 3163 | |
| 3164 | if (mCharWrapper != null) { |
| 3165 | mCharWrapper.mChars = null; |
| 3166 | } |
| 3167 | } |
| 3168 | |
| 3169 | private void setText(CharSequence text, BufferType type, |
| 3170 | boolean notifyBefore, int oldlen) { |
| 3171 | if (text == null) { |
| 3172 | text = ""; |
| 3173 | } |
| 3174 | |
Luca Zanolin | e076045 | 2011-09-08 12:03:37 +0100 | [diff] [blame] | 3175 | // If suggestions are not enabled, remove the suggestion spans from the text |
| 3176 | if (!isSuggestionsEnabled()) { |
| 3177 | text = removeSuggestionSpans(text); |
| 3178 | } |
| 3179 | |
Romain Guy | 939151f | 2009-04-08 14:22:40 -0700 | [diff] [blame] | 3180 | if (!mUserSetTextScaleX) mTextPaint.setTextScaleX(1.0f); |
| 3181 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3182 | if (text instanceof Spanned && |
| 3183 | ((Spanned) text).getSpanStart(TextUtils.TruncateAt.MARQUEE) >= 0) { |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 3184 | if (ViewConfiguration.get(mContext).isFadingMarqueeEnabled()) { |
| 3185 | setHorizontalFadingEdgeEnabled(true); |
| 3186 | mMarqueeFadeMode = MARQUEE_FADE_NORMAL; |
| 3187 | } else { |
| 3188 | setHorizontalFadingEdgeEnabled(false); |
| 3189 | mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS; |
| 3190 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3191 | setEllipsize(TextUtils.TruncateAt.MARQUEE); |
| 3192 | } |
| 3193 | |
| 3194 | int n = mFilters.length; |
| 3195 | for (int i = 0; i < n; i++) { |
| 3196 | CharSequence out = mFilters[i].filter(text, 0, text.length(), |
| 3197 | EMPTY_SPANNED, 0, 0); |
| 3198 | if (out != null) { |
| 3199 | text = out; |
| 3200 | } |
| 3201 | } |
| 3202 | |
| 3203 | if (notifyBefore) { |
| 3204 | if (mText != null) { |
| 3205 | oldlen = mText.length(); |
| 3206 | sendBeforeTextChanged(mText, 0, oldlen, text.length()); |
| 3207 | } else { |
| 3208 | sendBeforeTextChanged("", 0, 0, text.length()); |
| 3209 | } |
| 3210 | } |
| 3211 | |
| 3212 | boolean needEditableForNotification = false; |
| 3213 | |
| 3214 | if (mListeners != null && mListeners.size() != 0) { |
| 3215 | needEditableForNotification = true; |
| 3216 | } |
| 3217 | |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 3218 | if (type == BufferType.EDITABLE || mInput != null || needEditableForNotification) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3219 | Editable t = mEditableFactory.newEditable(text); |
| 3220 | text = t; |
| 3221 | setFilters(t, mFilters); |
| 3222 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 3223 | if (imm != null) imm.restartInput(this); |
| 3224 | } else if (type == BufferType.SPANNABLE || mMovement != null) { |
| 3225 | text = mSpannableFactory.newSpannable(text); |
| 3226 | } else if (!(text instanceof CharWrapper)) { |
| 3227 | text = TextUtils.stringOrSpannedString(text); |
| 3228 | } |
| 3229 | |
| 3230 | if (mAutoLinkMask != 0) { |
| 3231 | Spannable s2; |
| 3232 | |
| 3233 | if (type == BufferType.EDITABLE || text instanceof Spannable) { |
| 3234 | s2 = (Spannable) text; |
| 3235 | } else { |
| 3236 | s2 = mSpannableFactory.newSpannable(text); |
| 3237 | } |
| 3238 | |
| 3239 | if (Linkify.addLinks(s2, mAutoLinkMask)) { |
| 3240 | text = s2; |
| 3241 | type = (type == BufferType.EDITABLE) ? BufferType.EDITABLE : BufferType.SPANNABLE; |
| 3242 | |
| 3243 | /* |
| 3244 | * We must go ahead and set the text before changing the |
| 3245 | * movement method, because setMovementMethod() may call |
| 3246 | * setText() again to try to upgrade the buffer type. |
| 3247 | */ |
| 3248 | mText = text; |
| 3249 | |
Gilles Debunne | cbcb345 | 2010-12-17 15:31:02 -0800 | [diff] [blame] | 3250 | // Do not change the movement method for text that support text selection as it |
| 3251 | // would prevent an arbitrary cursor displacement. |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 3252 | if (mLinksClickable && !textCanBeSelected()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3253 | setMovementMethod(LinkMovementMethod.getInstance()); |
| 3254 | } |
| 3255 | } |
| 3256 | } |
| 3257 | |
| 3258 | mBufferType = type; |
| 3259 | mText = text; |
| 3260 | |
Adam Powell | 7f8f79a | 2011-07-07 18:35:54 -0700 | [diff] [blame] | 3261 | if (mTransformation == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3262 | mTransformed = text; |
Adam Powell | 7f8f79a | 2011-07-07 18:35:54 -0700 | [diff] [blame] | 3263 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3264 | mTransformed = mTransformation.getTransformation(text, this); |
Adam Powell | 7f8f79a | 2011-07-07 18:35:54 -0700 | [diff] [blame] | 3265 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3266 | |
| 3267 | final int textLength = text.length(); |
| 3268 | |
Adam Powell | 7f8f79a | 2011-07-07 18:35:54 -0700 | [diff] [blame] | 3269 | if (text instanceof Spannable && !mAllowTransformationLengthChange) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3270 | Spannable sp = (Spannable) text; |
| 3271 | |
| 3272 | // Remove any ChangeWatchers that might have come |
| 3273 | // from other TextViews. |
| 3274 | final ChangeWatcher[] watchers = sp.getSpans(0, sp.length(), ChangeWatcher.class); |
| 3275 | final int count = watchers.length; |
| 3276 | for (int i = 0; i < count; i++) |
| 3277 | sp.removeSpan(watchers[i]); |
| 3278 | |
| 3279 | if (mChangeWatcher == null) |
| 3280 | mChangeWatcher = new ChangeWatcher(); |
| 3281 | |
| 3282 | sp.setSpan(mChangeWatcher, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE | |
| 3283 | (PRIORITY << Spanned.SPAN_PRIORITY_SHIFT)); |
| 3284 | |
| 3285 | if (mInput != null) { |
| 3286 | sp.setSpan(mInput, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE); |
| 3287 | } |
| 3288 | |
| 3289 | if (mTransformation != null) { |
| 3290 | sp.setSpan(mTransformation, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3291 | } |
| 3292 | |
| 3293 | if (mMovement != null) { |
| 3294 | mMovement.initialize(this, (Spannable) text); |
| 3295 | |
| 3296 | /* |
| 3297 | * Initializing the movement method will have set the |
| 3298 | * selection, so reset mSelectionMoved to keep that from |
| 3299 | * interfering with the normal on-focus selection-setting. |
| 3300 | */ |
| 3301 | mSelectionMoved = false; |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | if (mLayout != null) { |
| 3306 | checkForRelayout(); |
| 3307 | } |
| 3308 | |
| 3309 | sendOnTextChanged(text, 0, oldlen, textLength); |
| 3310 | onTextChanged(text, 0, oldlen, textLength); |
| 3311 | |
| 3312 | if (needEditableForNotification) { |
| 3313 | sendAfterTextChanged((Editable) text); |
| 3314 | } |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 3315 | |
Gilles Debunne | baaace5 | 2010-10-01 15:47:13 -0700 | [diff] [blame] | 3316 | // SelectionModifierCursorController depends on textCanBeSelected, which depends on text |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 3317 | prepareCursorControllers(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | /** |
| 3321 | * Sets the TextView to display the specified slice of the specified |
| 3322 | * char array. You must promise that you will not change the contents |
| 3323 | * of the array except for right before another call to setText(), |
| 3324 | * since the TextView has no way to know that the text |
| 3325 | * has changed and that it needs to invalidate and re-layout. |
| 3326 | */ |
| 3327 | public final void setText(char[] text, int start, int len) { |
| 3328 | int oldlen = 0; |
| 3329 | |
| 3330 | if (start < 0 || len < 0 || start + len > text.length) { |
| 3331 | throw new IndexOutOfBoundsException(start + ", " + len); |
| 3332 | } |
| 3333 | |
| 3334 | /* |
| 3335 | * We must do the before-notification here ourselves because if |
| 3336 | * the old text is a CharWrapper we destroy it before calling |
| 3337 | * into the normal path. |
| 3338 | */ |
| 3339 | if (mText != null) { |
| 3340 | oldlen = mText.length(); |
| 3341 | sendBeforeTextChanged(mText, 0, oldlen, len); |
| 3342 | } else { |
| 3343 | sendBeforeTextChanged("", 0, 0, len); |
| 3344 | } |
| 3345 | |
| 3346 | if (mCharWrapper == null) { |
| 3347 | mCharWrapper = new CharWrapper(text, start, len); |
| 3348 | } else { |
| 3349 | mCharWrapper.set(text, start, len); |
| 3350 | } |
| 3351 | |
| 3352 | setText(mCharWrapper, mBufferType, false, oldlen); |
| 3353 | } |
| 3354 | |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 3355 | private static class CharWrapper implements CharSequence, GetChars, GraphicsOperations { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3356 | private char[] mChars; |
| 3357 | private int mStart, mLength; |
| 3358 | |
| 3359 | public CharWrapper(char[] chars, int start, int len) { |
| 3360 | mChars = chars; |
| 3361 | mStart = start; |
| 3362 | mLength = len; |
| 3363 | } |
| 3364 | |
| 3365 | /* package */ void set(char[] chars, int start, int len) { |
| 3366 | mChars = chars; |
| 3367 | mStart = start; |
| 3368 | mLength = len; |
| 3369 | } |
| 3370 | |
| 3371 | public int length() { |
| 3372 | return mLength; |
| 3373 | } |
| 3374 | |
| 3375 | public char charAt(int off) { |
| 3376 | return mChars[off + mStart]; |
| 3377 | } |
| 3378 | |
Gilles Debunne | e15b358 | 2010-06-16 15:17:21 -0700 | [diff] [blame] | 3379 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3380 | public String toString() { |
| 3381 | return new String(mChars, mStart, mLength); |
| 3382 | } |
| 3383 | |
| 3384 | public CharSequence subSequence(int start, int end) { |
| 3385 | if (start < 0 || end < 0 || start > mLength || end > mLength) { |
| 3386 | throw new IndexOutOfBoundsException(start + ", " + end); |
| 3387 | } |
| 3388 | |
| 3389 | return new String(mChars, start + mStart, end - start); |
| 3390 | } |
| 3391 | |
| 3392 | public void getChars(int start, int end, char[] buf, int off) { |
| 3393 | if (start < 0 || end < 0 || start > mLength || end > mLength) { |
| 3394 | throw new IndexOutOfBoundsException(start + ", " + end); |
| 3395 | } |
| 3396 | |
| 3397 | System.arraycopy(mChars, start + mStart, buf, off, end - start); |
| 3398 | } |
| 3399 | |
| 3400 | public void drawText(Canvas c, int start, int end, |
| 3401 | float x, float y, Paint p) { |
| 3402 | c.drawText(mChars, start + mStart, end - start, x, y, p); |
| 3403 | } |
| 3404 | |
Doug Felt | f47d740 | 2010-04-21 16:01:52 -0700 | [diff] [blame] | 3405 | public void drawTextRun(Canvas c, int start, int end, |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 3406 | int contextStart, int contextEnd, float x, float y, int flags, Paint p) { |
| 3407 | int count = end - start; |
| 3408 | int contextCount = contextEnd - contextStart; |
| 3409 | c.drawTextRun(mChars, start + mStart, count, contextStart + mStart, |
| 3410 | contextCount, x, y, flags, p); |
Doug Felt | f47d740 | 2010-04-21 16:01:52 -0700 | [diff] [blame] | 3411 | } |
| 3412 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3413 | public float measureText(int start, int end, Paint p) { |
| 3414 | return p.measureText(mChars, start + mStart, end - start); |
| 3415 | } |
| 3416 | |
| 3417 | public int getTextWidths(int start, int end, float[] widths, Paint p) { |
| 3418 | return p.getTextWidths(mChars, start + mStart, end - start, widths); |
| 3419 | } |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 3420 | |
| 3421 | public float getTextRunAdvances(int start, int end, int contextStart, |
| 3422 | int contextEnd, int flags, float[] advances, int advancesIndex, |
| 3423 | Paint p) { |
| 3424 | int count = end - start; |
| 3425 | int contextCount = contextEnd - contextStart; |
| 3426 | return p.getTextRunAdvances(mChars, start + mStart, count, |
| 3427 | contextStart + mStart, contextCount, flags, advances, |
| 3428 | advancesIndex); |
| 3429 | } |
| 3430 | |
Fabrice Di Meglio | 0a1413e | 2011-04-21 17:36:26 -0700 | [diff] [blame] | 3431 | public float getTextRunAdvances(int start, int end, int contextStart, |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 3432 | int contextEnd, int flags, float[] advances, int advancesIndex, |
Fabrice Di Meglio | 0a1413e | 2011-04-21 17:36:26 -0700 | [diff] [blame] | 3433 | Paint p, int reserved) { |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 3434 | int count = end - start; |
| 3435 | int contextCount = contextEnd - contextStart; |
Fabrice Di Meglio | 0a1413e | 2011-04-21 17:36:26 -0700 | [diff] [blame] | 3436 | return p.getTextRunAdvances(mChars, start + mStart, count, |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 3437 | contextStart + mStart, contextCount, flags, advances, |
Fabrice Di Meglio | 0a1413e | 2011-04-21 17:36:26 -0700 | [diff] [blame] | 3438 | advancesIndex, reserved); |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 3439 | } |
| 3440 | |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 3441 | public int getTextRunCursor(int contextStart, int contextEnd, int flags, |
| 3442 | int offset, int cursorOpt, Paint p) { |
| 3443 | int contextCount = contextEnd - contextStart; |
| 3444 | return p.getTextRunCursor(mChars, contextStart + mStart, |
| 3445 | contextCount, flags, offset + mStart, cursorOpt); |
| 3446 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | /** |
| 3450 | * Like {@link #setText(CharSequence, android.widget.TextView.BufferType)}, |
| 3451 | * except that the cursor position (if any) is retained in the new text. |
| 3452 | * |
| 3453 | * @see #setText(CharSequence, android.widget.TextView.BufferType) |
| 3454 | */ |
| 3455 | public final void setTextKeepState(CharSequence text, BufferType type) { |
| 3456 | int start = getSelectionStart(); |
| 3457 | int end = getSelectionEnd(); |
| 3458 | int len = text.length(); |
| 3459 | |
| 3460 | setText(text, type); |
| 3461 | |
| 3462 | if (start >= 0 || end >= 0) { |
| 3463 | if (mText instanceof Spannable) { |
| 3464 | Selection.setSelection((Spannable) mText, |
| 3465 | Math.max(0, Math.min(start, len)), |
| 3466 | Math.max(0, Math.min(end, len))); |
| 3467 | } |
| 3468 | } |
| 3469 | } |
| 3470 | |
| 3471 | @android.view.RemotableViewMethod |
| 3472 | public final void setText(int resid) { |
| 3473 | setText(getContext().getResources().getText(resid)); |
| 3474 | } |
| 3475 | |
| 3476 | public final void setText(int resid, BufferType type) { |
| 3477 | setText(getContext().getResources().getText(resid), type); |
| 3478 | } |
| 3479 | |
| 3480 | /** |
| 3481 | * Sets the text to be displayed when the text of the TextView is empty. |
| 3482 | * Null means to use the normal empty text. The hint does not currently |
| 3483 | * participate in determining the size of the view. |
| 3484 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3485 | * @attr ref android.R.styleable#TextView_hint |
| 3486 | */ |
| 3487 | @android.view.RemotableViewMethod |
| 3488 | public final void setHint(CharSequence hint) { |
| 3489 | mHint = TextUtils.stringOrSpannedString(hint); |
| 3490 | |
| 3491 | if (mLayout != null) { |
| 3492 | checkForRelayout(); |
| 3493 | } |
| 3494 | |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 3495 | if (mText.length() == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3496 | invalidate(); |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 3497 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3498 | } |
| 3499 | |
| 3500 | /** |
| 3501 | * Sets the text to be displayed when the text of the TextView is empty, |
| 3502 | * from a resource. |
| 3503 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3504 | * @attr ref android.R.styleable#TextView_hint |
| 3505 | */ |
| 3506 | @android.view.RemotableViewMethod |
| 3507 | public final void setHint(int resid) { |
| 3508 | setHint(getContext().getResources().getText(resid)); |
| 3509 | } |
| 3510 | |
| 3511 | /** |
| 3512 | * Returns the hint that is displayed when the text of the TextView |
| 3513 | * is empty. |
| 3514 | * |
| 3515 | * @attr ref android.R.styleable#TextView_hint |
| 3516 | */ |
| 3517 | @ViewDebug.CapturedViewProperty |
| 3518 | public CharSequence getHint() { |
| 3519 | return mHint; |
| 3520 | } |
| 3521 | |
Gilles Debunne | 3784a7f | 2011-07-15 13:49:38 -0700 | [diff] [blame] | 3522 | private static boolean isMultilineInputType(int type) { |
Gilles Debunne | 91a08cf | 2010-11-08 17:34:49 -0800 | [diff] [blame] | 3523 | return (type & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE)) == |
| 3524 | (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE); |
| 3525 | } |
| 3526 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3527 | /** |
Gilles Debunne | a3ae4a0 | 2010-12-13 17:22:39 -0800 | [diff] [blame] | 3528 | * Set the type of the content with a constant as defined for {@link EditorInfo#inputType}. This |
| 3529 | * will take care of changing the key listener, by calling {@link #setKeyListener(KeyListener)}, |
| 3530 | * to match the given content type. If the given content type is {@link EditorInfo#TYPE_NULL} |
| 3531 | * then a soft keyboard will not be displayed for this text view. |
| 3532 | * |
| 3533 | * Note that the maximum number of displayed lines (see {@link #setMaxLines(int)}) will be |
| 3534 | * modified if you change the {@link EditorInfo#TYPE_TEXT_FLAG_MULTI_LINE} flag of the input |
| 3535 | * type. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3536 | * |
| 3537 | * @see #getInputType() |
| 3538 | * @see #setRawInputType(int) |
| 3539 | * @see android.text.InputType |
| 3540 | * @attr ref android.R.styleable#TextView_inputType |
| 3541 | */ |
| 3542 | public void setInputType(int type) { |
Bjorn Bringert | ad8da91 | 2009-09-17 10:47:35 +0100 | [diff] [blame] | 3543 | final boolean wasPassword = isPasswordInputType(mInputType); |
| 3544 | final boolean wasVisiblePassword = isVisiblePasswordInputType(mInputType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3545 | setInputType(type, false); |
Bjorn Bringert | ad8da91 | 2009-09-17 10:47:35 +0100 | [diff] [blame] | 3546 | final boolean isPassword = isPasswordInputType(type); |
| 3547 | final boolean isVisiblePassword = isVisiblePasswordInputType(type); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3548 | boolean forceUpdate = false; |
| 3549 | if (isPassword) { |
| 3550 | setTransformationMethod(PasswordTransformationMethod.getInstance()); |
| 3551 | setTypefaceByIndex(MONOSPACE, 0); |
Bjorn Bringert | ad8da91 | 2009-09-17 10:47:35 +0100 | [diff] [blame] | 3552 | } else if (isVisiblePassword) { |
Amith Yamasani | a8c0edb | 2009-09-27 16:51:21 -0700 | [diff] [blame] | 3553 | if (mTransformation == PasswordTransformationMethod.getInstance()) { |
| 3554 | forceUpdate = true; |
| 3555 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3556 | setTypefaceByIndex(MONOSPACE, 0); |
Bjorn Bringert | ad8da91 | 2009-09-17 10:47:35 +0100 | [diff] [blame] | 3557 | } else if (wasPassword || wasVisiblePassword) { |
| 3558 | // not in password mode, clean up typeface and transformation |
| 3559 | setTypefaceByIndex(-1, -1); |
| 3560 | if (mTransformation == PasswordTransformationMethod.getInstance()) { |
| 3561 | forceUpdate = true; |
| 3562 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3563 | } |
| 3564 | |
Gilles Debunne | 91a08cf | 2010-11-08 17:34:49 -0800 | [diff] [blame] | 3565 | boolean singleLine = !isMultilineInputType(type); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3566 | |
| 3567 | // We need to update the single line mode if it has changed or we |
| 3568 | // were previously in password mode. |
Gilles Debunne | 91a08cf | 2010-11-08 17:34:49 -0800 | [diff] [blame] | 3569 | if (mSingleLine != singleLine || forceUpdate) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3570 | // Change single line mode, but only change the transformation if |
| 3571 | // we are not in password mode. |
Gilles Debunne | a3ae4a0 | 2010-12-13 17:22:39 -0800 | [diff] [blame] | 3572 | applySingleLine(singleLine, !isPassword, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3573 | } |
| 3574 | |
Luca Zanolin | e076045 | 2011-09-08 12:03:37 +0100 | [diff] [blame] | 3575 | if (!isSuggestionsEnabled()) { |
| 3576 | mText = removeSuggestionSpans(mText); |
| 3577 | } |
| 3578 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3579 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 3580 | if (imm != null) imm.restartInput(this); |
| 3581 | } |
| 3582 | |
Gilles Debunne | 0dcad2b | 2010-10-15 16:29:25 -0700 | [diff] [blame] | 3583 | /** |
| 3584 | * It would be better to rely on the input type for everything. A password inputType should have |
| 3585 | * a password transformation. We should hence use isPasswordInputType instead of this method. |
| 3586 | * |
| 3587 | * We should: |
| 3588 | * - Call setInputType in setKeyListener instead of changing the input type directly (which |
| 3589 | * would install the correct transformation). |
| 3590 | * - Refuse the installation of a non-password transformation in setTransformation if the input |
| 3591 | * type is password. |
| 3592 | * |
| 3593 | * However, this is like this for legacy reasons and we cannot break existing apps. This method |
| 3594 | * is useful since it matches what the user can see (obfuscated text or not). |
| 3595 | * |
| 3596 | * @return true if the current transformation method is of the password type. |
| 3597 | */ |
| 3598 | private boolean hasPasswordTransformationMethod() { |
| 3599 | return mTransformation instanceof PasswordTransformationMethod; |
| 3600 | } |
| 3601 | |
Gilles Debunne | 3784a7f | 2011-07-15 13:49:38 -0700 | [diff] [blame] | 3602 | private static boolean isPasswordInputType(int inputType) { |
Gilles Debunne | d7483bf | 2010-11-10 10:47:45 -0800 | [diff] [blame] | 3603 | final int variation = |
| 3604 | inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION); |
Bjorn Bringert | ad8da91 | 2009-09-17 10:47:35 +0100 | [diff] [blame] | 3605 | return variation |
Gilles Debunne | d7483bf | 2010-11-10 10:47:45 -0800 | [diff] [blame] | 3606 | == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD) |
| 3607 | || variation |
Ken Wakasa | 82d731a | 2010-12-24 23:42:41 +0900 | [diff] [blame] | 3608 | == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD) |
| 3609 | || variation |
| 3610 | == (EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD); |
Bjorn Bringert | ad8da91 | 2009-09-17 10:47:35 +0100 | [diff] [blame] | 3611 | } |
| 3612 | |
Gilles Debunne | 3784a7f | 2011-07-15 13:49:38 -0700 | [diff] [blame] | 3613 | private static boolean isVisiblePasswordInputType(int inputType) { |
Gilles Debunne | d7483bf | 2010-11-10 10:47:45 -0800 | [diff] [blame] | 3614 | final int variation = |
| 3615 | inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION); |
Bjorn Bringert | ad8da91 | 2009-09-17 10:47:35 +0100 | [diff] [blame] | 3616 | return variation |
Gilles Debunne | d7483bf | 2010-11-10 10:47:45 -0800 | [diff] [blame] | 3617 | == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); |
Bjorn Bringert | ad8da91 | 2009-09-17 10:47:35 +0100 | [diff] [blame] | 3618 | } |
| 3619 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3620 | /** |
| 3621 | * Directly change the content type integer of the text view, without |
| 3622 | * modifying any other state. |
| 3623 | * @see #setInputType(int) |
| 3624 | * @see android.text.InputType |
| 3625 | * @attr ref android.R.styleable#TextView_inputType |
| 3626 | */ |
| 3627 | public void setRawInputType(int type) { |
| 3628 | mInputType = type; |
| 3629 | } |
| 3630 | |
| 3631 | private void setInputType(int type, boolean direct) { |
| 3632 | final int cls = type & EditorInfo.TYPE_MASK_CLASS; |
| 3633 | KeyListener input; |
| 3634 | if (cls == EditorInfo.TYPE_CLASS_TEXT) { |
Gilles Debunne | e67b58a | 2010-08-31 15:55:31 -0700 | [diff] [blame] | 3635 | boolean autotext = (type & EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3636 | TextKeyListener.Capitalize cap; |
| 3637 | if ((type & EditorInfo.TYPE_TEXT_FLAG_CAP_CHARACTERS) != 0) { |
| 3638 | cap = TextKeyListener.Capitalize.CHARACTERS; |
| 3639 | } else if ((type & EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS) != 0) { |
| 3640 | cap = TextKeyListener.Capitalize.WORDS; |
| 3641 | } else if ((type & EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES) != 0) { |
| 3642 | cap = TextKeyListener.Capitalize.SENTENCES; |
| 3643 | } else { |
| 3644 | cap = TextKeyListener.Capitalize.NONE; |
| 3645 | } |
| 3646 | input = TextKeyListener.getInstance(autotext, cap); |
| 3647 | } else if (cls == EditorInfo.TYPE_CLASS_NUMBER) { |
| 3648 | input = DigitsKeyListener.getInstance( |
| 3649 | (type & EditorInfo.TYPE_NUMBER_FLAG_SIGNED) != 0, |
| 3650 | (type & EditorInfo.TYPE_NUMBER_FLAG_DECIMAL) != 0); |
| 3651 | } else if (cls == EditorInfo.TYPE_CLASS_DATETIME) { |
| 3652 | switch (type & EditorInfo.TYPE_MASK_VARIATION) { |
| 3653 | case EditorInfo.TYPE_DATETIME_VARIATION_DATE: |
| 3654 | input = DateKeyListener.getInstance(); |
| 3655 | break; |
| 3656 | case EditorInfo.TYPE_DATETIME_VARIATION_TIME: |
| 3657 | input = TimeKeyListener.getInstance(); |
| 3658 | break; |
| 3659 | default: |
| 3660 | input = DateTimeKeyListener.getInstance(); |
| 3661 | break; |
| 3662 | } |
| 3663 | } else if (cls == EditorInfo.TYPE_CLASS_PHONE) { |
| 3664 | input = DialerKeyListener.getInstance(); |
| 3665 | } else { |
| 3666 | input = TextKeyListener.getInstance(); |
| 3667 | } |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 3668 | setRawInputType(type); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3669 | if (direct) mInput = input; |
| 3670 | else { |
| 3671 | setKeyListenerOnly(input); |
| 3672 | } |
| 3673 | } |
| 3674 | |
| 3675 | /** |
| 3676 | * Get the type of the content. |
| 3677 | * |
| 3678 | * @see #setInputType(int) |
| 3679 | * @see android.text.InputType |
| 3680 | */ |
| 3681 | public int getInputType() { |
| 3682 | return mInputType; |
| 3683 | } |
| 3684 | |
| 3685 | /** |
| 3686 | * Change the editor type integer associated with the text view, which |
| 3687 | * will be reported to an IME with {@link EditorInfo#imeOptions} when it |
| 3688 | * has focus. |
| 3689 | * @see #getImeOptions |
| 3690 | * @see android.view.inputmethod.EditorInfo |
| 3691 | * @attr ref android.R.styleable#TextView_imeOptions |
| 3692 | */ |
| 3693 | public void setImeOptions(int imeOptions) { |
| 3694 | if (mInputContentType == null) { |
| 3695 | mInputContentType = new InputContentType(); |
| 3696 | } |
| 3697 | mInputContentType.imeOptions = imeOptions; |
| 3698 | } |
| 3699 | |
| 3700 | /** |
| 3701 | * Get the type of the IME editor. |
| 3702 | * |
| 3703 | * @see #setImeOptions(int) |
| 3704 | * @see android.view.inputmethod.EditorInfo |
| 3705 | */ |
| 3706 | public int getImeOptions() { |
| 3707 | return mInputContentType != null |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3708 | ? mInputContentType.imeOptions : EditorInfo.IME_NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3709 | } |
| 3710 | |
| 3711 | /** |
| 3712 | * Change the custom IME action associated with the text view, which |
| 3713 | * will be reported to an IME with {@link EditorInfo#actionLabel} |
| 3714 | * and {@link EditorInfo#actionId} when it has focus. |
| 3715 | * @see #getImeActionLabel |
| 3716 | * @see #getImeActionId |
| 3717 | * @see android.view.inputmethod.EditorInfo |
| 3718 | * @attr ref android.R.styleable#TextView_imeActionLabel |
| 3719 | * @attr ref android.R.styleable#TextView_imeActionId |
| 3720 | */ |
| 3721 | public void setImeActionLabel(CharSequence label, int actionId) { |
| 3722 | if (mInputContentType == null) { |
| 3723 | mInputContentType = new InputContentType(); |
| 3724 | } |
| 3725 | mInputContentType.imeActionLabel = label; |
| 3726 | mInputContentType.imeActionId = actionId; |
| 3727 | } |
| 3728 | |
| 3729 | /** |
| 3730 | * Get the IME action label previous set with {@link #setImeActionLabel}. |
| 3731 | * |
| 3732 | * @see #setImeActionLabel |
| 3733 | * @see android.view.inputmethod.EditorInfo |
| 3734 | */ |
| 3735 | public CharSequence getImeActionLabel() { |
| 3736 | return mInputContentType != null |
| 3737 | ? mInputContentType.imeActionLabel : null; |
| 3738 | } |
| 3739 | |
| 3740 | /** |
| 3741 | * Get the IME action ID previous set with {@link #setImeActionLabel}. |
| 3742 | * |
| 3743 | * @see #setImeActionLabel |
| 3744 | * @see android.view.inputmethod.EditorInfo |
| 3745 | */ |
| 3746 | public int getImeActionId() { |
| 3747 | return mInputContentType != null |
| 3748 | ? mInputContentType.imeActionId : 0; |
| 3749 | } |
| 3750 | |
| 3751 | /** |
| 3752 | * Set a special listener to be called when an action is performed |
| 3753 | * on the text view. This will be called when the enter key is pressed, |
| 3754 | * or when an action supplied to the IME is selected by the user. Setting |
| 3755 | * this means that the normal hard key event will not insert a newline |
| 3756 | * into the text view, even if it is multi-line; holding down the ALT |
| 3757 | * modifier will, however, allow the user to insert a newline character. |
| 3758 | */ |
| 3759 | public void setOnEditorActionListener(OnEditorActionListener l) { |
| 3760 | if (mInputContentType == null) { |
| 3761 | mInputContentType = new InputContentType(); |
| 3762 | } |
| 3763 | mInputContentType.onEditorActionListener = l; |
| 3764 | } |
| 3765 | |
| 3766 | /** |
| 3767 | * Called when an attached input method calls |
| 3768 | * {@link InputConnection#performEditorAction(int) |
| 3769 | * InputConnection.performEditorAction()} |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3770 | * for this text view. The default implementation will call your action |
| 3771 | * listener supplied to {@link #setOnEditorActionListener}, or perform |
| 3772 | * a standard operation for {@link EditorInfo#IME_ACTION_NEXT |
Dianne Hackborn | dea3ef7 | 2010-10-28 14:24:22 -0700 | [diff] [blame] | 3773 | * EditorInfo.IME_ACTION_NEXT}, {@link EditorInfo#IME_ACTION_PREVIOUS |
| 3774 | * EditorInfo.IME_ACTION_PREVIOUS}, or {@link EditorInfo#IME_ACTION_DONE |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3775 | * EditorInfo.IME_ACTION_DONE}. |
| 3776 | * |
| 3777 | * <p>For backwards compatibility, if no IME options have been set and the |
| 3778 | * text view would not normally advance focus on enter, then |
| 3779 | * the NEXT and DONE actions received here will be turned into an enter |
| 3780 | * key down/up pair to go through the normal key handling. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3781 | * |
| 3782 | * @param actionCode The code of the action being performed. |
| 3783 | * |
| 3784 | * @see #setOnEditorActionListener |
| 3785 | */ |
| 3786 | public void onEditorAction(int actionCode) { |
| 3787 | final InputContentType ict = mInputContentType; |
| 3788 | if (ict != null) { |
| 3789 | if (ict.onEditorActionListener != null) { |
| 3790 | if (ict.onEditorActionListener.onEditorAction(this, |
| 3791 | actionCode, null)) { |
| 3792 | return; |
| 3793 | } |
| 3794 | } |
Gilles Debunne | 6479448 | 2011-11-30 15:45:28 -0800 | [diff] [blame] | 3795 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3796 | // This is the handling for some default action. |
| 3797 | // Note that for backwards compatibility we don't do this |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3798 | // default handling if explicit ime options have not been given, |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3799 | // instead turning this into the normal enter key codes that an |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3800 | // app may be expecting. |
| 3801 | if (actionCode == EditorInfo.IME_ACTION_NEXT) { |
Svetoslav Ganov | a53efe9 | 2011-09-08 18:08:36 -0700 | [diff] [blame] | 3802 | View v = focusSearch(FOCUS_FORWARD); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3803 | if (v != null) { |
Svetoslav Ganov | a53efe9 | 2011-09-08 18:08:36 -0700 | [diff] [blame] | 3804 | if (!v.requestFocus(FOCUS_FORWARD)) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3805 | throw new IllegalStateException("focus search returned a view " + |
| 3806 | "that wasn't able to take focus!"); |
| 3807 | } |
| 3808 | } |
| 3809 | return; |
Svetoslav Ganov | a53efe9 | 2011-09-08 18:08:36 -0700 | [diff] [blame] | 3810 | |
Dianne Hackborn | dea3ef7 | 2010-10-28 14:24:22 -0700 | [diff] [blame] | 3811 | } else if (actionCode == EditorInfo.IME_ACTION_PREVIOUS) { |
Svetoslav Ganov | a53efe9 | 2011-09-08 18:08:36 -0700 | [diff] [blame] | 3812 | View v = focusSearch(FOCUS_BACKWARD); |
Dianne Hackborn | dea3ef7 | 2010-10-28 14:24:22 -0700 | [diff] [blame] | 3813 | if (v != null) { |
Svetoslav Ganov | a53efe9 | 2011-09-08 18:08:36 -0700 | [diff] [blame] | 3814 | if (!v.requestFocus(FOCUS_BACKWARD)) { |
Dianne Hackborn | dea3ef7 | 2010-10-28 14:24:22 -0700 | [diff] [blame] | 3815 | throw new IllegalStateException("focus search returned a view " + |
| 3816 | "that wasn't able to take focus!"); |
| 3817 | } |
| 3818 | } |
| 3819 | return; |
| 3820 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3821 | } else if (actionCode == EditorInfo.IME_ACTION_DONE) { |
| 3822 | InputMethodManager imm = InputMethodManager.peekInstance(); |
Gilles Debunne | 17d31de | 2011-01-27 11:02:18 -0800 | [diff] [blame] | 3823 | if (imm != null && imm.isActive(this)) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3824 | imm.hideSoftInputFromWindow(getWindowToken(), 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3825 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3826 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3827 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3828 | } |
Svetoslav Ganov | a53efe9 | 2011-09-08 18:08:36 -0700 | [diff] [blame] | 3829 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3830 | Handler h = getHandler(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3831 | if (h != null) { |
| 3832 | long eventTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 3833 | h.sendMessage(h.obtainMessage(ViewRootImpl.DISPATCH_KEY_FROM_IME, |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3834 | new KeyEvent(eventTime, eventTime, |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 3835 | KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER, 0, 0, |
| 3836 | KeyCharacterMap.VIRTUAL_KEYBOARD, 0, |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3837 | KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE |
| 3838 | | KeyEvent.FLAG_EDITOR_ACTION))); |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 3839 | h.sendMessage(h.obtainMessage(ViewRootImpl.DISPATCH_KEY_FROM_IME, |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3840 | new KeyEvent(SystemClock.uptimeMillis(), eventTime, |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 3841 | KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER, 0, 0, |
| 3842 | KeyCharacterMap.VIRTUAL_KEYBOARD, 0, |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3843 | KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE |
| 3844 | | KeyEvent.FLAG_EDITOR_ACTION))); |
| 3845 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3846 | } |
Gilles Debunne | 6479448 | 2011-11-30 15:45:28 -0800 | [diff] [blame] | 3847 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3848 | /** |
| 3849 | * Set the private content type of the text, which is the |
| 3850 | * {@link EditorInfo#privateImeOptions EditorInfo.privateImeOptions} |
| 3851 | * field that will be filled in when creating an input connection. |
| 3852 | * |
| 3853 | * @see #getPrivateImeOptions() |
| 3854 | * @see EditorInfo#privateImeOptions |
| 3855 | * @attr ref android.R.styleable#TextView_privateImeOptions |
| 3856 | */ |
| 3857 | public void setPrivateImeOptions(String type) { |
| 3858 | if (mInputContentType == null) mInputContentType = new InputContentType(); |
| 3859 | mInputContentType.privateImeOptions = type; |
| 3860 | } |
| 3861 | |
| 3862 | /** |
| 3863 | * Get the private type of the content. |
| 3864 | * |
| 3865 | * @see #setPrivateImeOptions(String) |
| 3866 | * @see EditorInfo#privateImeOptions |
| 3867 | */ |
| 3868 | public String getPrivateImeOptions() { |
| 3869 | return mInputContentType != null |
| 3870 | ? mInputContentType.privateImeOptions : null; |
| 3871 | } |
| 3872 | |
| 3873 | /** |
| 3874 | * Set the extra input data of the text, which is the |
| 3875 | * {@link EditorInfo#extras TextBoxAttribute.extras} |
| 3876 | * Bundle that will be filled in when creating an input connection. The |
| 3877 | * given integer is the resource ID of an XML resource holding an |
| 3878 | * {@link android.R.styleable#InputExtras <input-extras>} XML tree. |
| 3879 | * |
| 3880 | * @see #getInputExtras(boolean) |
| 3881 | * @see EditorInfo#extras |
| 3882 | * @attr ref android.R.styleable#TextView_editorExtras |
| 3883 | */ |
| 3884 | public void setInputExtras(int xmlResId) |
| 3885 | throws XmlPullParserException, IOException { |
| 3886 | XmlResourceParser parser = getResources().getXml(xmlResId); |
| 3887 | if (mInputContentType == null) mInputContentType = new InputContentType(); |
| 3888 | mInputContentType.extras = new Bundle(); |
| 3889 | getResources().parseBundleExtras(parser, mInputContentType.extras); |
| 3890 | } |
| 3891 | |
| 3892 | /** |
| 3893 | * Retrieve the input extras currently associated with the text view, which |
| 3894 | * can be viewed as well as modified. |
| 3895 | * |
| 3896 | * @param create If true, the extras will be created if they don't already |
| 3897 | * exist. Otherwise, null will be returned if none have been created. |
Gilles Debunne | e15b358 | 2010-06-16 15:17:21 -0700 | [diff] [blame] | 3898 | * @see #setInputExtras(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3899 | * @see EditorInfo#extras |
| 3900 | * @attr ref android.R.styleable#TextView_editorExtras |
| 3901 | */ |
| 3902 | public Bundle getInputExtras(boolean create) { |
| 3903 | if (mInputContentType == null) { |
| 3904 | if (!create) return null; |
| 3905 | mInputContentType = new InputContentType(); |
| 3906 | } |
| 3907 | if (mInputContentType.extras == null) { |
| 3908 | if (!create) return null; |
| 3909 | mInputContentType.extras = new Bundle(); |
| 3910 | } |
| 3911 | return mInputContentType.extras; |
| 3912 | } |
| 3913 | |
| 3914 | /** |
| 3915 | * Returns the error message that was set to be displayed with |
| 3916 | * {@link #setError}, or <code>null</code> if no error was set |
| 3917 | * or if it the error was cleared by the widget after user input. |
| 3918 | */ |
| 3919 | public CharSequence getError() { |
| 3920 | return mError; |
| 3921 | } |
| 3922 | |
| 3923 | /** |
| 3924 | * Sets the right-hand compound drawable of the TextView to the "error" |
| 3925 | * icon and sets an error message that will be displayed in a popup when |
| 3926 | * the TextView has focus. The icon and error message will be reset to |
| 3927 | * null when any key events cause changes to the TextView's text. If the |
| 3928 | * <code>error</code> is <code>null</code>, the error message and icon |
| 3929 | * will be cleared. |
| 3930 | */ |
| 3931 | @android.view.RemotableViewMethod |
| 3932 | public void setError(CharSequence error) { |
| 3933 | if (error == null) { |
| 3934 | setError(null, null); |
| 3935 | } else { |
| 3936 | Drawable dr = getContext().getResources(). |
Gilles Debunne | a85467b | 2011-01-19 16:53:31 -0800 | [diff] [blame] | 3937 | getDrawable(com.android.internal.R.drawable.indicator_input_error); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3938 | |
| 3939 | dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight()); |
| 3940 | setError(error, dr); |
| 3941 | } |
| 3942 | } |
| 3943 | |
| 3944 | /** |
| 3945 | * Sets the right-hand compound drawable of the TextView to the specified |
| 3946 | * icon and sets an error message that will be displayed in a popup when |
| 3947 | * the TextView has focus. The icon and error message will be reset to |
| 3948 | * null when any key events cause changes to the TextView's text. The |
| 3949 | * drawable must already have had {@link Drawable#setBounds} set on it. |
| 3950 | * If the <code>error</code> is <code>null</code>, the error message will |
| 3951 | * be cleared (and you should provide a <code>null</code> icon as well). |
| 3952 | */ |
| 3953 | public void setError(CharSequence error, Drawable icon) { |
| 3954 | error = TextUtils.stringOrSpannedString(error); |
| 3955 | |
| 3956 | mError = error; |
| 3957 | mErrorWasChanged = true; |
| 3958 | final Drawables dr = mDrawables; |
| 3959 | if (dr != null) { |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 3960 | switch (getResolvedLayoutDirection()) { |
| 3961 | default: |
| 3962 | case LAYOUT_DIRECTION_LTR: |
| 3963 | setCompoundDrawables(dr.mDrawableLeft, dr.mDrawableTop, icon, |
| 3964 | dr.mDrawableBottom); |
| 3965 | break; |
| 3966 | case LAYOUT_DIRECTION_RTL: |
| 3967 | setCompoundDrawables(icon, dr.mDrawableTop, dr.mDrawableRight, |
| 3968 | dr.mDrawableBottom); |
| 3969 | break; |
| 3970 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3971 | } else { |
| 3972 | setCompoundDrawables(null, null, icon, null); |
| 3973 | } |
| 3974 | |
| 3975 | if (error == null) { |
| 3976 | if (mPopup != null) { |
| 3977 | if (mPopup.isShowing()) { |
| 3978 | mPopup.dismiss(); |
| 3979 | } |
| 3980 | |
| 3981 | mPopup = null; |
| 3982 | } |
| 3983 | } else { |
| 3984 | if (isFocused()) { |
| 3985 | showError(); |
| 3986 | } |
| 3987 | } |
| 3988 | } |
| 3989 | |
| 3990 | private void showError() { |
| 3991 | if (getWindowToken() == null) { |
| 3992 | mShowErrorAfterAttach = true; |
| 3993 | return; |
| 3994 | } |
| 3995 | |
| 3996 | if (mPopup == null) { |
| 3997 | LayoutInflater inflater = LayoutInflater.from(getContext()); |
Gilles Debunne | a85467b | 2011-01-19 16:53:31 -0800 | [diff] [blame] | 3998 | final TextView err = (TextView) inflater.inflate( |
| 3999 | com.android.internal.R.layout.textview_hint, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4000 | |
Romain Guy | 9bc9fa1 | 2009-07-21 16:57:29 -0700 | [diff] [blame] | 4001 | final float scale = getResources().getDisplayMetrics().density; |
Gilles Debunne | f1f409a | 2011-01-27 17:31:00 -0800 | [diff] [blame] | 4002 | mPopup = new ErrorPopup(err, (int) (200 * scale + 0.5f), (int) (50 * scale + 0.5f)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4003 | mPopup.setFocusable(false); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 4004 | // The user is entering text, so the input method is needed. We |
| 4005 | // don't want the popup to be displayed on top of it. |
| 4006 | mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4007 | } |
| 4008 | |
| 4009 | TextView tv = (TextView) mPopup.getContentView(); |
| 4010 | chooseSize(mPopup, mError, tv); |
| 4011 | tv.setText(mError); |
| 4012 | |
| 4013 | mPopup.showAsDropDown(this, getErrorX(), getErrorY()); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4014 | mPopup.fixDirection(mPopup.isAboveAnchor()); |
| 4015 | } |
| 4016 | |
| 4017 | private static class ErrorPopup extends PopupWindow { |
| 4018 | private boolean mAbove = false; |
Gilles Debunne | e15b358 | 2010-06-16 15:17:21 -0700 | [diff] [blame] | 4019 | private final TextView mView; |
Gilles Debunne | 5f059e4 | 2011-01-12 17:49:12 -0800 | [diff] [blame] | 4020 | private int mPopupInlineErrorBackgroundId = 0; |
| 4021 | private int mPopupInlineErrorAboveBackgroundId = 0; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4022 | |
| 4023 | ErrorPopup(TextView v, int width, int height) { |
| 4024 | super(v, width, height); |
| 4025 | mView = v; |
Gilles Debunne | f1f409a | 2011-01-27 17:31:00 -0800 | [diff] [blame] | 4026 | // Make sure the TextView has a background set as it will be used the first time it is |
| 4027 | // shown and positionned. Initialized with below background, which should have |
| 4028 | // dimensions identical to the above version for this to work (and is more likely). |
| 4029 | mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId, |
| 4030 | com.android.internal.R.styleable.Theme_errorMessageBackground); |
| 4031 | mView.setBackgroundResource(mPopupInlineErrorBackgroundId); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4032 | } |
| 4033 | |
| 4034 | void fixDirection(boolean above) { |
| 4035 | mAbove = above; |
| 4036 | |
| 4037 | if (above) { |
Gilles Debunne | 5f059e4 | 2011-01-12 17:49:12 -0800 | [diff] [blame] | 4038 | mPopupInlineErrorAboveBackgroundId = |
Gilles Debunne | f1f409a | 2011-01-27 17:31:00 -0800 | [diff] [blame] | 4039 | getResourceId(mPopupInlineErrorAboveBackgroundId, |
| 4040 | com.android.internal.R.styleable.Theme_errorMessageAboveBackground); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4041 | } else { |
Gilles Debunne | f1f409a | 2011-01-27 17:31:00 -0800 | [diff] [blame] | 4042 | mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId, |
| 4043 | com.android.internal.R.styleable.Theme_errorMessageBackground); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4044 | } |
Gilles Debunne | 5f059e4 | 2011-01-12 17:49:12 -0800 | [diff] [blame] | 4045 | |
Gilles Debunne | f1f409a | 2011-01-27 17:31:00 -0800 | [diff] [blame] | 4046 | mView.setBackgroundResource(above ? mPopupInlineErrorAboveBackgroundId : |
| 4047 | mPopupInlineErrorBackgroundId); |
Gilles Debunne | 5f059e4 | 2011-01-12 17:49:12 -0800 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | private int getResourceId(int currentId, int index) { |
| 4051 | if (currentId == 0) { |
Gilles Debunne | f1f409a | 2011-01-27 17:31:00 -0800 | [diff] [blame] | 4052 | TypedArray styledAttributes = mView.getContext().obtainStyledAttributes( |
| 4053 | R.styleable.Theme); |
Gilles Debunne | 5f059e4 | 2011-01-12 17:49:12 -0800 | [diff] [blame] | 4054 | currentId = styledAttributes.getResourceId(index, 0); |
| 4055 | styledAttributes.recycle(); |
| 4056 | } |
| 4057 | return currentId; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4058 | } |
| 4059 | |
| 4060 | @Override |
| 4061 | public void update(int x, int y, int w, int h, boolean force) { |
| 4062 | super.update(x, y, w, h, force); |
| 4063 | |
| 4064 | boolean above = isAboveAnchor(); |
| 4065 | if (above != mAbove) { |
| 4066 | fixDirection(above); |
| 4067 | } |
| 4068 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4069 | } |
| 4070 | |
| 4071 | /** |
| 4072 | * Returns the Y offset to make the pointy top of the error point |
| 4073 | * at the middle of the error icon. |
| 4074 | */ |
| 4075 | private int getErrorX() { |
| 4076 | /* |
| 4077 | * The "25" is the distance between the point and the right edge |
| 4078 | * of the background |
| 4079 | */ |
Romain Guy | 9bc9fa1 | 2009-07-21 16:57:29 -0700 | [diff] [blame] | 4080 | final float scale = getResources().getDisplayMetrics().density; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4081 | |
| 4082 | final Drawables dr = mDrawables; |
Gilles Debunne | f1f409a | 2011-01-27 17:31:00 -0800 | [diff] [blame] | 4083 | return getWidth() - mPopup.getWidth() - getPaddingRight() - |
| 4084 | (dr != null ? dr.mDrawableSizeRight : 0) / 2 + (int) (25 * scale + 0.5f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4085 | } |
| 4086 | |
| 4087 | /** |
| 4088 | * Returns the Y offset to make the pointy top of the error point |
| 4089 | * at the bottom of the error icon. |
| 4090 | */ |
| 4091 | private int getErrorY() { |
| 4092 | /* |
| 4093 | * Compound, not extended, because the icon is not clipped |
| 4094 | * if the text height is smaller. |
| 4095 | */ |
Gilles Debunne | f1f409a | 2011-01-27 17:31:00 -0800 | [diff] [blame] | 4096 | final int compoundPaddingTop = getCompoundPaddingTop(); |
| 4097 | int vspace = mBottom - mTop - getCompoundPaddingBottom() - compoundPaddingTop; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4098 | |
| 4099 | final Drawables dr = mDrawables; |
Gilles Debunne | f1f409a | 2011-01-27 17:31:00 -0800 | [diff] [blame] | 4100 | int icontop = compoundPaddingTop + |
| 4101 | (vspace - (dr != null ? dr.mDrawableHeightRight : 0)) / 2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4102 | |
| 4103 | /* |
| 4104 | * The "2" is the distance between the point and the top edge |
| 4105 | * of the background. |
| 4106 | */ |
Gilles Debunne | f1f409a | 2011-01-27 17:31:00 -0800 | [diff] [blame] | 4107 | final float scale = getResources().getDisplayMetrics().density; |
| 4108 | return icontop + (dr != null ? dr.mDrawableHeightRight : 0) - getHeight() - |
| 4109 | (int) (2 * scale + 0.5f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | private void hideError() { |
| 4113 | if (mPopup != null) { |
| 4114 | if (mPopup.isShowing()) { |
| 4115 | mPopup.dismiss(); |
| 4116 | } |
| 4117 | } |
| 4118 | |
| 4119 | mShowErrorAfterAttach = false; |
| 4120 | } |
| 4121 | |
| 4122 | private void chooseSize(PopupWindow pop, CharSequence text, TextView tv) { |
| 4123 | int wid = tv.getPaddingLeft() + tv.getPaddingRight(); |
| 4124 | int ht = tv.getPaddingTop() + tv.getPaddingBottom(); |
| 4125 | |
Fabrice Di Meglio | e423146 | 2011-09-08 18:15:50 -0700 | [diff] [blame] | 4126 | int defaultWidthInPixels = getResources().getDimensionPixelSize( |
| 4127 | com.android.internal.R.dimen.textview_error_popup_default_width); |
Fabrice Di Meglio | 33438be | 2011-09-08 15:05:23 -0700 | [diff] [blame] | 4128 | Layout l = new StaticLayout(text, tv.getPaint(), defaultWidthInPixels, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4129 | Layout.Alignment.ALIGN_NORMAL, 1, 0, true); |
| 4130 | float max = 0; |
| 4131 | for (int i = 0; i < l.getLineCount(); i++) { |
| 4132 | max = Math.max(max, l.getLineWidth(i)); |
| 4133 | } |
| 4134 | |
| 4135 | /* |
Fabrice Di Meglio | 33438be | 2011-09-08 15:05:23 -0700 | [diff] [blame] | 4136 | * Now set the popup size to be big enough for the text plus the border capped |
| 4137 | * to DEFAULT_MAX_POPUP_WIDTH |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4138 | */ |
| 4139 | pop.setWidth(wid + (int) Math.ceil(max)); |
| 4140 | pop.setHeight(ht + l.getHeight()); |
| 4141 | } |
| 4142 | |
| 4143 | |
| 4144 | @Override |
| 4145 | protected boolean setFrame(int l, int t, int r, int b) { |
| 4146 | boolean result = super.setFrame(l, t, r, b); |
| 4147 | |
| 4148 | if (mPopup != null) { |
| 4149 | TextView tv = (TextView) mPopup.getContentView(); |
| 4150 | chooseSize(mPopup, mError, tv); |
Eric Fischer | fa0d253 | 2009-09-17 17:01:59 -0700 | [diff] [blame] | 4151 | mPopup.update(this, getErrorX(), getErrorY(), |
| 4152 | mPopup.getWidth(), mPopup.getHeight()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4153 | } |
| 4154 | |
Romain Guy | 986003d | 2009-03-25 17:42:35 -0700 | [diff] [blame] | 4155 | restartMarqueeIfNeeded(); |
| 4156 | |
| 4157 | return result; |
| 4158 | } |
| 4159 | |
| 4160 | private void restartMarqueeIfNeeded() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4161 | if (mRestartMarquee && mEllipsize == TextUtils.TruncateAt.MARQUEE) { |
| 4162 | mRestartMarquee = false; |
| 4163 | startMarquee(); |
| 4164 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4165 | } |
| 4166 | |
| 4167 | /** |
| 4168 | * Sets the list of input filters that will be used if the buffer is |
| 4169 | * Editable. Has no effect otherwise. |
| 4170 | * |
| 4171 | * @attr ref android.R.styleable#TextView_maxLength |
| 4172 | */ |
| 4173 | public void setFilters(InputFilter[] filters) { |
| 4174 | if (filters == null) { |
| 4175 | throw new IllegalArgumentException(); |
| 4176 | } |
| 4177 | |
| 4178 | mFilters = filters; |
| 4179 | |
| 4180 | if (mText instanceof Editable) { |
| 4181 | setFilters((Editable) mText, filters); |
| 4182 | } |
| 4183 | } |
| 4184 | |
| 4185 | /** |
| 4186 | * Sets the list of input filters on the specified Editable, |
| 4187 | * and includes mInput in the list if it is an InputFilter. |
| 4188 | */ |
| 4189 | private void setFilters(Editable e, InputFilter[] filters) { |
| 4190 | if (mInput instanceof InputFilter) { |
| 4191 | InputFilter[] nf = new InputFilter[filters.length + 1]; |
| 4192 | |
| 4193 | System.arraycopy(filters, 0, nf, 0, filters.length); |
| 4194 | nf[filters.length] = (InputFilter) mInput; |
| 4195 | |
| 4196 | e.setFilters(nf); |
| 4197 | } else { |
| 4198 | e.setFilters(filters); |
| 4199 | } |
| 4200 | } |
| 4201 | |
| 4202 | /** |
| 4203 | * Returns the current list of input filters. |
| 4204 | */ |
| 4205 | public InputFilter[] getFilters() { |
| 4206 | return mFilters; |
| 4207 | } |
| 4208 | |
| 4209 | ///////////////////////////////////////////////////////////////////////// |
| 4210 | |
| 4211 | private int getVerticalOffset(boolean forceNormal) { |
| 4212 | int voffset = 0; |
| 4213 | final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 4214 | |
| 4215 | Layout l = mLayout; |
| 4216 | if (!forceNormal && mText.length() == 0 && mHintLayout != null) { |
| 4217 | l = mHintLayout; |
| 4218 | } |
| 4219 | |
| 4220 | if (gravity != Gravity.TOP) { |
| 4221 | int boxht; |
| 4222 | |
| 4223 | if (l == mHintLayout) { |
| 4224 | boxht = getMeasuredHeight() - getCompoundPaddingTop() - |
| 4225 | getCompoundPaddingBottom(); |
| 4226 | } else { |
| 4227 | boxht = getMeasuredHeight() - getExtendedPaddingTop() - |
| 4228 | getExtendedPaddingBottom(); |
| 4229 | } |
| 4230 | int textht = l.getHeight(); |
| 4231 | |
| 4232 | if (textht < boxht) { |
| 4233 | if (gravity == Gravity.BOTTOM) |
| 4234 | voffset = boxht - textht; |
| 4235 | else // (gravity == Gravity.CENTER_VERTICAL) |
| 4236 | voffset = (boxht - textht) >> 1; |
| 4237 | } |
| 4238 | } |
| 4239 | return voffset; |
| 4240 | } |
| 4241 | |
| 4242 | private int getBottomVerticalOffset(boolean forceNormal) { |
| 4243 | int voffset = 0; |
| 4244 | final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 4245 | |
| 4246 | Layout l = mLayout; |
| 4247 | if (!forceNormal && mText.length() == 0 && mHintLayout != null) { |
| 4248 | l = mHintLayout; |
| 4249 | } |
| 4250 | |
| 4251 | if (gravity != Gravity.BOTTOM) { |
| 4252 | int boxht; |
| 4253 | |
| 4254 | if (l == mHintLayout) { |
| 4255 | boxht = getMeasuredHeight() - getCompoundPaddingTop() - |
| 4256 | getCompoundPaddingBottom(); |
| 4257 | } else { |
| 4258 | boxht = getMeasuredHeight() - getExtendedPaddingTop() - |
| 4259 | getExtendedPaddingBottom(); |
| 4260 | } |
| 4261 | int textht = l.getHeight(); |
| 4262 | |
| 4263 | if (textht < boxht) { |
| 4264 | if (gravity == Gravity.TOP) |
| 4265 | voffset = boxht - textht; |
| 4266 | else // (gravity == Gravity.CENTER_VERTICAL) |
| 4267 | voffset = (boxht - textht) >> 1; |
| 4268 | } |
| 4269 | } |
| 4270 | return voffset; |
| 4271 | } |
| 4272 | |
| 4273 | private void invalidateCursorPath() { |
| 4274 | if (mHighlightPathBogus) { |
| 4275 | invalidateCursor(); |
| 4276 | } else { |
Gilles Debunne | f75c97e | 2011-02-10 16:09:53 -0800 | [diff] [blame] | 4277 | final int horizontalPadding = getCompoundPaddingLeft(); |
| 4278 | final int verticalPadding = getExtendedPaddingTop() + getVerticalOffset(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4279 | |
Gilles Debunne | f75c97e | 2011-02-10 16:09:53 -0800 | [diff] [blame] | 4280 | if (mCursorCount == 0) { |
| 4281 | synchronized (sTempRect) { |
| 4282 | /* |
| 4283 | * The reason for this concern about the thickness of the |
| 4284 | * cursor and doing the floor/ceil on the coordinates is that |
| 4285 | * some EditTexts (notably textfields in the Browser) have |
| 4286 | * anti-aliased text where not all the characters are |
| 4287 | * necessarily at integer-multiple locations. This should |
| 4288 | * make sure the entire cursor gets invalidated instead of |
| 4289 | * sometimes missing half a pixel. |
| 4290 | */ |
| 4291 | float thick = FloatMath.ceil(mTextPaint.getStrokeWidth()); |
| 4292 | if (thick < 1.0f) { |
| 4293 | thick = 1.0f; |
| 4294 | } |
| 4295 | |
| 4296 | thick /= 2.0f; |
| 4297 | |
| 4298 | mHighlightPath.computeBounds(sTempRect, false); |
| 4299 | |
| 4300 | invalidate((int) FloatMath.floor(horizontalPadding + sTempRect.left - thick), |
| 4301 | (int) FloatMath.floor(verticalPadding + sTempRect.top - thick), |
| 4302 | (int) FloatMath.ceil(horizontalPadding + sTempRect.right + thick), |
| 4303 | (int) FloatMath.ceil(verticalPadding + sTempRect.bottom + thick)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4304 | } |
Gilles Debunne | f75c97e | 2011-02-10 16:09:53 -0800 | [diff] [blame] | 4305 | } else { |
| 4306 | for (int i = 0; i < mCursorCount; i++) { |
| 4307 | Rect bounds = mCursorDrawable[i].getBounds(); |
| 4308 | invalidate(bounds.left + horizontalPadding, bounds.top + verticalPadding, |
| 4309 | bounds.right + horizontalPadding, bounds.bottom + verticalPadding); |
| 4310 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4311 | } |
| 4312 | } |
| 4313 | } |
| 4314 | |
| 4315 | private void invalidateCursor() { |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 4316 | int where = getSelectionEnd(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4317 | |
| 4318 | invalidateCursor(where, where, where); |
| 4319 | } |
| 4320 | |
| 4321 | private void invalidateCursor(int a, int b, int c) { |
Gilles Debunne | 8615ac9 | 2011-11-29 15:25:03 -0800 | [diff] [blame] | 4322 | if (a >= 0 || b >= 0 || c >= 0) { |
| 4323 | int start = Math.min(Math.min(a, b), c); |
| 4324 | int end = Math.max(Math.max(a, b), c); |
| 4325 | invalidateRegion(start, end); |
| 4326 | } |
| 4327 | } |
| 4328 | |
| 4329 | /** |
| 4330 | * Invalidates the region of text enclosed between the start and end text offsets. |
| 4331 | * |
| 4332 | * @hide |
| 4333 | */ |
| 4334 | void invalidateRegion(int start, int end) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4335 | if (mLayout == null) { |
| 4336 | invalidate(); |
| 4337 | } else { |
Gilles Debunne | 8615ac9 | 2011-11-29 15:25:03 -0800 | [diff] [blame] | 4338 | int lineStart = mLayout.getLineForOffset(start); |
| 4339 | int top = mLayout.getLineTop(lineStart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4340 | |
| 4341 | // This is ridiculous, but the descent from the line above |
| 4342 | // can hang down into the line we really want to redraw, |
| 4343 | // so we have to invalidate part of the line above to make |
| 4344 | // sure everything that needs to be redrawn really is. |
| 4345 | // (But not the whole line above, because that would cause |
| 4346 | // the same problem with the descenders on the line above it!) |
Gilles Debunne | 8615ac9 | 2011-11-29 15:25:03 -0800 | [diff] [blame] | 4347 | if (lineStart > 0) { |
| 4348 | top -= mLayout.getLineDescent(lineStart - 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4349 | } |
| 4350 | |
Gilles Debunne | 8615ac9 | 2011-11-29 15:25:03 -0800 | [diff] [blame] | 4351 | int lineEnd; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4352 | |
Gilles Debunne | 8615ac9 | 2011-11-29 15:25:03 -0800 | [diff] [blame] | 4353 | if (start == end) |
| 4354 | lineEnd = lineStart; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4355 | else |
Gilles Debunne | 8615ac9 | 2011-11-29 15:25:03 -0800 | [diff] [blame] | 4356 | lineEnd = mLayout.getLineForOffset(end); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4357 | |
Gilles Debunne | 8615ac9 | 2011-11-29 15:25:03 -0800 | [diff] [blame] | 4358 | int bottom = mLayout.getLineBottom(lineEnd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4359 | |
Gilles Debunne | 8615ac9 | 2011-11-29 15:25:03 -0800 | [diff] [blame] | 4360 | final int compoundPaddingLeft = getCompoundPaddingLeft(); |
Gilles Debunne | f75c97e | 2011-02-10 16:09:53 -0800 | [diff] [blame] | 4361 | final int verticalPadding = getExtendedPaddingTop() + getVerticalOffset(true); |
Gilles Debunne | 8615ac9 | 2011-11-29 15:25:03 -0800 | [diff] [blame] | 4362 | |
| 4363 | int left, right; |
| 4364 | if (lineStart == lineEnd) { |
| 4365 | left = (int) mLayout.getPrimaryHorizontal(start); |
| 4366 | right = (int) (mLayout.getPrimaryHorizontal(end) + 1.0); |
| 4367 | left += compoundPaddingLeft; |
| 4368 | right += compoundPaddingLeft; |
| 4369 | } else { |
| 4370 | // Rectangle bounding box when the region spans several lines |
| 4371 | left = compoundPaddingLeft; |
| 4372 | right = getWidth() - getCompoundPaddingRight(); |
Gilles Debunne | f75c97e | 2011-02-10 16:09:53 -0800 | [diff] [blame] | 4373 | } |
| 4374 | |
Gilles Debunne | 8615ac9 | 2011-11-29 15:25:03 -0800 | [diff] [blame] | 4375 | invalidate(mScrollX + left, verticalPadding + top, |
| 4376 | mScrollX + right, verticalPadding + bottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4377 | } |
| 4378 | } |
| 4379 | |
| 4380 | private void registerForPreDraw() { |
| 4381 | final ViewTreeObserver observer = getViewTreeObserver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4382 | |
| 4383 | if (mPreDrawState == PREDRAW_NOT_REGISTERED) { |
| 4384 | observer.addOnPreDrawListener(this); |
| 4385 | mPreDrawState = PREDRAW_PENDING; |
| 4386 | } else if (mPreDrawState == PREDRAW_DONE) { |
| 4387 | mPreDrawState = PREDRAW_PENDING; |
| 4388 | } |
| 4389 | |
| 4390 | // else state is PREDRAW_PENDING, so keep waiting. |
| 4391 | } |
| 4392 | |
| 4393 | /** |
| 4394 | * {@inheritDoc} |
| 4395 | */ |
| 4396 | public boolean onPreDraw() { |
| 4397 | if (mPreDrawState != PREDRAW_PENDING) { |
| 4398 | return true; |
| 4399 | } |
| 4400 | |
| 4401 | if (mLayout == null) { |
| 4402 | assumeLayout(); |
| 4403 | } |
| 4404 | |
| 4405 | boolean changed = false; |
| 4406 | |
| 4407 | if (mMovement != null) { |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 4408 | /* This code also provides auto-scrolling when a cursor is moved using a |
| 4409 | * CursorController (insertion point or selection limits). |
| 4410 | * For selection, ensure start or end is visible depending on controller's state. |
| 4411 | */ |
| 4412 | int curs = getSelectionEnd(); |
Gilles Debunne | e587d83 | 2010-11-23 20:20:11 -0800 | [diff] [blame] | 4413 | // Do not create the controller if it is not already created. |
| 4414 | if (mSelectionModifierCursorController != null && |
| 4415 | mSelectionModifierCursorController.isSelectionStartDragged()) { |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 4416 | curs = getSelectionStart(); |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 4417 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4418 | |
| 4419 | /* |
| 4420 | * TODO: This should really only keep the end in view if |
| 4421 | * it already was before the text changed. I'm not sure |
| 4422 | * of a good way to tell from here if it was. |
| 4423 | */ |
| 4424 | if (curs < 0 && |
| 4425 | (mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { |
| 4426 | curs = mText.length(); |
| 4427 | } |
| 4428 | |
| 4429 | if (curs >= 0) { |
| 4430 | changed = bringPointIntoView(curs); |
| 4431 | } |
| 4432 | } else { |
| 4433 | changed = bringTextIntoView(); |
| 4434 | } |
| 4435 | |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 4436 | // This has to be checked here since: |
| 4437 | // - onFocusChanged cannot start it when focus is given to a view with selected text (after |
| 4438 | // a screen rotation) since layout is not yet initialized at that point. |
Gilles Debunne | c01f3fe | 2010-12-22 17:07:36 -0800 | [diff] [blame] | 4439 | if (mCreatedWithASelection) { |
| 4440 | startSelectionActionMode(); |
| 4441 | mCreatedWithASelection = false; |
| 4442 | } |
| 4443 | |
| 4444 | // Phone specific code (there is no ExtractEditText on tablets). |
| 4445 | // ExtractEditText does not call onFocus when it is displayed, and mHasSelectionOnFocus can |
| 4446 | // not be set. Do the test here instead. |
| 4447 | if (this instanceof ExtractEditText && hasSelection()) { |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 4448 | startSelectionActionMode(); |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 4449 | } |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 4450 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4451 | mPreDrawState = PREDRAW_DONE; |
| 4452 | return !changed; |
| 4453 | } |
| 4454 | |
| 4455 | @Override |
| 4456 | protected void onAttachedToWindow() { |
| 4457 | super.onAttachedToWindow(); |
| 4458 | |
| 4459 | mTemporaryDetach = false; |
| 4460 | |
| 4461 | if (mShowErrorAfterAttach) { |
| 4462 | showError(); |
| 4463 | mShowErrorAfterAttach = false; |
| 4464 | } |
Adam Powell | 624380a | 2010-10-02 18:12:02 -0700 | [diff] [blame] | 4465 | |
| 4466 | final ViewTreeObserver observer = getViewTreeObserver(); |
Gilles Debunne | 81f0808 | 2011-02-17 14:07:19 -0800 | [diff] [blame] | 4467 | // No need to create the controller. |
| 4468 | // The get method will add the listener on controller creation. |
| 4469 | if (mInsertionPointCursorController != null) { |
| 4470 | observer.addOnTouchModeChangeListener(mInsertionPointCursorController); |
| 4471 | } |
| 4472 | if (mSelectionModifierCursorController != null) { |
| 4473 | observer.addOnTouchModeChangeListener(mSelectionModifierCursorController); |
Adam Powell | 624380a | 2010-10-02 18:12:02 -0700 | [diff] [blame] | 4474 | } |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 4475 | |
| 4476 | // Resolve drawables as the layout direction has been resolved |
| 4477 | resolveDrawables(); |
Gilles Debunne | c115fa0 | 2011-12-07 13:38:31 -0800 | [diff] [blame] | 4478 | |
| 4479 | updateSpellCheckSpans(0, mText.length(), true /* create the spell checker if needed */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4480 | } |
| 4481 | |
| 4482 | @Override |
| 4483 | protected void onDetachedFromWindow() { |
| 4484 | super.onDetachedFromWindow(); |
| 4485 | |
Adam Powell | 624380a | 2010-10-02 18:12:02 -0700 | [diff] [blame] | 4486 | final ViewTreeObserver observer = getViewTreeObserver(); |
Gilles Debunne | 81f0808 | 2011-02-17 14:07:19 -0800 | [diff] [blame] | 4487 | if (mPreDrawState != PREDRAW_NOT_REGISTERED) { |
| 4488 | observer.removeOnPreDrawListener(this); |
| 4489 | mPreDrawState = PREDRAW_NOT_REGISTERED; |
| 4490 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4491 | |
| 4492 | if (mError != null) { |
| 4493 | hideError(); |
| 4494 | } |
Adam Powell | ba0a2c3 | 2010-09-28 17:41:23 -0700 | [diff] [blame] | 4495 | |
Gilles Debunne | f48e83b | 2010-12-06 18:36:08 -0800 | [diff] [blame] | 4496 | if (mBlink != null) { |
Gilles Debunne | 3d01006 | 2011-02-18 14:16:41 -0800 | [diff] [blame] | 4497 | mBlink.removeCallbacks(mBlink); |
Gilles Debunne | f48e83b | 2010-12-06 18:36:08 -0800 | [diff] [blame] | 4498 | } |
Gilles Debunne | 7eeba5f | 2010-12-10 16:55:55 -0800 | [diff] [blame] | 4499 | |
| 4500 | if (mInsertionPointCursorController != null) { |
| 4501 | mInsertionPointCursorController.onDetached(); |
| 4502 | } |
| 4503 | |
| 4504 | if (mSelectionModifierCursorController != null) { |
| 4505 | mSelectionModifierCursorController.onDetached(); |
| 4506 | } |
| 4507 | |
Adam Powell | ba0a2c3 | 2010-09-28 17:41:23 -0700 | [diff] [blame] | 4508 | hideControllers(); |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 4509 | |
| 4510 | resetResolvedDrawables(); |
Gilles Debunne | 186aaf9 | 2011-09-16 14:26:12 -0700 | [diff] [blame] | 4511 | |
| 4512 | if (mSpellChecker != null) { |
| 4513 | mSpellChecker.closeSession(); |
Gilles Debunne | 186aaf9 | 2011-09-16 14:26:12 -0700 | [diff] [blame] | 4514 | // Forces the creation of a new SpellChecker next time this window is created. |
| 4515 | // Will handle the cases where the settings has been changed in the meantime. |
| 4516 | mSpellChecker = null; |
| 4517 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | @Override |
| 4521 | protected boolean isPaddingOffsetRequired() { |
Romain Guy | 076dc9f | 2009-06-24 17:17:51 -0700 | [diff] [blame] | 4522 | return mShadowRadius != 0 || mDrawables != null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4523 | } |
| 4524 | |
| 4525 | @Override |
| 4526 | protected int getLeftPaddingOffset() { |
Romain Guy | 076dc9f | 2009-06-24 17:17:51 -0700 | [diff] [blame] | 4527 | return getCompoundPaddingLeft() - mPaddingLeft + |
| 4528 | (int) Math.min(0, mShadowDx - mShadowRadius); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4529 | } |
| 4530 | |
| 4531 | @Override |
| 4532 | protected int getTopPaddingOffset() { |
| 4533 | return (int) Math.min(0, mShadowDy - mShadowRadius); |
| 4534 | } |
| 4535 | |
| 4536 | @Override |
| 4537 | protected int getBottomPaddingOffset() { |
| 4538 | return (int) Math.max(0, mShadowDy + mShadowRadius); |
| 4539 | } |
| 4540 | |
| 4541 | @Override |
| 4542 | protected int getRightPaddingOffset() { |
Romain Guy | 076dc9f | 2009-06-24 17:17:51 -0700 | [diff] [blame] | 4543 | return -(getCompoundPaddingRight() - mPaddingRight) + |
| 4544 | (int) Math.max(0, mShadowDx + mShadowRadius); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4545 | } |
| 4546 | |
| 4547 | @Override |
| 4548 | protected boolean verifyDrawable(Drawable who) { |
| 4549 | final boolean verified = super.verifyDrawable(who); |
| 4550 | if (!verified && mDrawables != null) { |
| 4551 | return who == mDrawables.mDrawableLeft || who == mDrawables.mDrawableTop || |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 4552 | who == mDrawables.mDrawableRight || who == mDrawables.mDrawableBottom || |
| 4553 | who == mDrawables.mDrawableStart || who == mDrawables.mDrawableEnd; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4554 | } |
| 4555 | return verified; |
| 4556 | } |
| 4557 | |
| 4558 | @Override |
Dianne Hackborn | e213677 | 2010-11-04 15:08:59 -0700 | [diff] [blame] | 4559 | public void jumpDrawablesToCurrentState() { |
| 4560 | super.jumpDrawablesToCurrentState(); |
| 4561 | if (mDrawables != null) { |
| 4562 | if (mDrawables.mDrawableLeft != null) { |
| 4563 | mDrawables.mDrawableLeft.jumpToCurrentState(); |
| 4564 | } |
| 4565 | if (mDrawables.mDrawableTop != null) { |
| 4566 | mDrawables.mDrawableTop.jumpToCurrentState(); |
| 4567 | } |
| 4568 | if (mDrawables.mDrawableRight != null) { |
| 4569 | mDrawables.mDrawableRight.jumpToCurrentState(); |
| 4570 | } |
| 4571 | if (mDrawables.mDrawableBottom != null) { |
| 4572 | mDrawables.mDrawableBottom.jumpToCurrentState(); |
| 4573 | } |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 4574 | if (mDrawables.mDrawableStart != null) { |
| 4575 | mDrawables.mDrawableStart.jumpToCurrentState(); |
| 4576 | } |
| 4577 | if (mDrawables.mDrawableEnd != null) { |
| 4578 | mDrawables.mDrawableEnd.jumpToCurrentState(); |
| 4579 | } |
Dianne Hackborn | e213677 | 2010-11-04 15:08:59 -0700 | [diff] [blame] | 4580 | } |
| 4581 | } |
| 4582 | |
| 4583 | @Override |
Romain Guy | 3c77d39 | 2009-05-20 11:26:50 -0700 | [diff] [blame] | 4584 | public void invalidateDrawable(Drawable drawable) { |
| 4585 | if (verifyDrawable(drawable)) { |
| 4586 | final Rect dirty = drawable.getBounds(); |
| 4587 | int scrollX = mScrollX; |
| 4588 | int scrollY = mScrollY; |
| 4589 | |
| 4590 | // IMPORTANT: The coordinates below are based on the coordinates computed |
| 4591 | // for each compound drawable in onDraw(). Make sure to update each section |
| 4592 | // accordingly. |
| 4593 | final TextView.Drawables drawables = mDrawables; |
Romain Guy | a6cd4e0 | 2009-05-20 15:09:21 -0700 | [diff] [blame] | 4594 | if (drawables != null) { |
| 4595 | if (drawable == drawables.mDrawableLeft) { |
| 4596 | final int compoundPaddingTop = getCompoundPaddingTop(); |
| 4597 | final int compoundPaddingBottom = getCompoundPaddingBottom(); |
| 4598 | final int vspace = mBottom - mTop - compoundPaddingBottom - compoundPaddingTop; |
Romain Guy | 3c77d39 | 2009-05-20 11:26:50 -0700 | [diff] [blame] | 4599 | |
Romain Guy | a6cd4e0 | 2009-05-20 15:09:21 -0700 | [diff] [blame] | 4600 | scrollX += mPaddingLeft; |
| 4601 | scrollY += compoundPaddingTop + (vspace - drawables.mDrawableHeightLeft) / 2; |
| 4602 | } else if (drawable == drawables.mDrawableRight) { |
| 4603 | final int compoundPaddingTop = getCompoundPaddingTop(); |
| 4604 | final int compoundPaddingBottom = getCompoundPaddingBottom(); |
| 4605 | final int vspace = mBottom - mTop - compoundPaddingBottom - compoundPaddingTop; |
Romain Guy | 3c77d39 | 2009-05-20 11:26:50 -0700 | [diff] [blame] | 4606 | |
Romain Guy | a6cd4e0 | 2009-05-20 15:09:21 -0700 | [diff] [blame] | 4607 | scrollX += (mRight - mLeft - mPaddingRight - drawables.mDrawableSizeRight); |
| 4608 | scrollY += compoundPaddingTop + (vspace - drawables.mDrawableHeightRight) / 2; |
| 4609 | } else if (drawable == drawables.mDrawableTop) { |
| 4610 | final int compoundPaddingLeft = getCompoundPaddingLeft(); |
| 4611 | final int compoundPaddingRight = getCompoundPaddingRight(); |
| 4612 | final int hspace = mRight - mLeft - compoundPaddingRight - compoundPaddingLeft; |
Romain Guy | 3c77d39 | 2009-05-20 11:26:50 -0700 | [diff] [blame] | 4613 | |
Romain Guy | a6cd4e0 | 2009-05-20 15:09:21 -0700 | [diff] [blame] | 4614 | scrollX += compoundPaddingLeft + (hspace - drawables.mDrawableWidthTop) / 2; |
| 4615 | scrollY += mPaddingTop; |
| 4616 | } else if (drawable == drawables.mDrawableBottom) { |
| 4617 | final int compoundPaddingLeft = getCompoundPaddingLeft(); |
| 4618 | final int compoundPaddingRight = getCompoundPaddingRight(); |
| 4619 | final int hspace = mRight - mLeft - compoundPaddingRight - compoundPaddingLeft; |
Romain Guy | 3c77d39 | 2009-05-20 11:26:50 -0700 | [diff] [blame] | 4620 | |
Romain Guy | a6cd4e0 | 2009-05-20 15:09:21 -0700 | [diff] [blame] | 4621 | scrollX += compoundPaddingLeft + (hspace - drawables.mDrawableWidthBottom) / 2; |
| 4622 | scrollY += (mBottom - mTop - mPaddingBottom - drawables.mDrawableSizeBottom); |
| 4623 | } |
Romain Guy | 3c77d39 | 2009-05-20 11:26:50 -0700 | [diff] [blame] | 4624 | } |
| 4625 | |
| 4626 | invalidate(dirty.left + scrollX, dirty.top + scrollY, |
| 4627 | dirty.right + scrollX, dirty.bottom + scrollY); |
| 4628 | } |
| 4629 | } |
| 4630 | |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 4631 | /** |
| 4632 | * @hide |
| 4633 | */ |
Romain Guy | 3c77d39 | 2009-05-20 11:26:50 -0700 | [diff] [blame] | 4634 | @Override |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 4635 | public int getResolvedLayoutDirection(Drawable who) { |
| 4636 | if (who == null) return View.LAYOUT_DIRECTION_LTR; |
Fabrice Di Meglio | 83fa41b | 2011-05-31 16:12:38 -0700 | [diff] [blame] | 4637 | if (mDrawables != null) { |
| 4638 | final Drawables drawables = mDrawables; |
| 4639 | if (who == drawables.mDrawableLeft || who == drawables.mDrawableRight || |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 4640 | who == drawables.mDrawableTop || who == drawables.mDrawableBottom || |
| 4641 | who == drawables.mDrawableStart || who == drawables.mDrawableEnd) { |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 4642 | return getResolvedLayoutDirection(); |
Fabrice Di Meglio | 83fa41b | 2011-05-31 16:12:38 -0700 | [diff] [blame] | 4643 | } |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 4644 | } |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 4645 | return super.getResolvedLayoutDirection(who); |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 4646 | } |
| 4647 | |
| 4648 | @Override |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 4649 | protected boolean onSetAlpha(int alpha) { |
Gilles Debunne | 3dbf55c | 2010-12-16 10:31:51 -0800 | [diff] [blame] | 4650 | // Alpha is supported if and only if the drawing can be done in one pass. |
| 4651 | // TODO text with spans with a background color currently do not respect this alpha. |
| 4652 | if (getBackground() == null) { |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 4653 | mCurrentAlpha = alpha; |
| 4654 | final Drawables dr = mDrawables; |
| 4655 | if (dr != null) { |
Michael Jurka | 406f052 | 2010-09-15 18:48:48 -0700 | [diff] [blame] | 4656 | if (dr.mDrawableLeft != null) dr.mDrawableLeft.mutate().setAlpha(alpha); |
| 4657 | if (dr.mDrawableTop != null) dr.mDrawableTop.mutate().setAlpha(alpha); |
| 4658 | if (dr.mDrawableRight != null) dr.mDrawableRight.mutate().setAlpha(alpha); |
| 4659 | if (dr.mDrawableBottom != null) dr.mDrawableBottom.mutate().setAlpha(alpha); |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 4660 | if (dr.mDrawableStart != null) dr.mDrawableStart.mutate().setAlpha(alpha); |
| 4661 | if (dr.mDrawableEnd != null) dr.mDrawableEnd.mutate().setAlpha(alpha); |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 4662 | } |
| 4663 | return true; |
| 4664 | } |
Gilles Debunne | 3dbf55c | 2010-12-16 10:31:51 -0800 | [diff] [blame] | 4665 | |
| 4666 | mCurrentAlpha = 255; |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 4667 | return false; |
| 4668 | } |
| 4669 | |
Gilles Debunne | 86b9c78 | 2010-11-11 10:43:48 -0800 | [diff] [blame] | 4670 | /** |
| 4671 | * When a TextView is used to display a useful piece of information to the user (such as a |
| 4672 | * contact's address), it should be made selectable, so that the user can select and copy this |
| 4673 | * content. |
| 4674 | * |
| 4675 | * Use {@link #setTextIsSelectable(boolean)} or the |
| 4676 | * {@link android.R.styleable#TextView_textIsSelectable} XML attribute to make this TextView |
Gilles Debunne | e12f999 | 2010-12-17 11:04:55 -0800 | [diff] [blame] | 4677 | * selectable (text is not selectable by default). |
Gilles Debunne | 6f100f3 | 2010-12-13 18:04:20 -0800 | [diff] [blame] | 4678 | * |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 4679 | * Note that this method simply returns the state of this flag. Although this flag has to be set |
| 4680 | * in order to select text in non-editable TextView, the content of an {@link EditText} can |
| 4681 | * always be selected, independently of the value of this flag. |
Gilles Debunne | 86b9c78 | 2010-11-11 10:43:48 -0800 | [diff] [blame] | 4682 | * |
| 4683 | * @return True if the text displayed in this TextView can be selected by the user. |
| 4684 | * |
| 4685 | * @attr ref android.R.styleable#TextView_textIsSelectable |
| 4686 | */ |
| 4687 | public boolean isTextSelectable() { |
| 4688 | return mTextIsSelectable; |
| 4689 | } |
| 4690 | |
| 4691 | /** |
| 4692 | * Sets whether or not (default) the content of this view is selectable by the user. |
Gilles Debunne | 6f100f3 | 2010-12-13 18:04:20 -0800 | [diff] [blame] | 4693 | * |
Gilles Debunne | e12f999 | 2010-12-17 11:04:55 -0800 | [diff] [blame] | 4694 | * Note that this methods affect the {@link #setFocusable(boolean)}, |
Gilles Debunne | cbcb345 | 2010-12-17 15:31:02 -0800 | [diff] [blame] | 4695 | * {@link #setFocusableInTouchMode(boolean)} {@link #setClickable(boolean)} and |
| 4696 | * {@link #setLongClickable(boolean)} states and you may want to restore these if they were |
| 4697 | * customized. |
Gilles Debunne | 86b9c78 | 2010-11-11 10:43:48 -0800 | [diff] [blame] | 4698 | * |
| 4699 | * See {@link #isTextSelectable} for details. |
| 4700 | * |
| 4701 | * @param selectable Whether or not the content of this TextView should be selectable. |
| 4702 | */ |
| 4703 | public void setTextIsSelectable(boolean selectable) { |
| 4704 | if (mTextIsSelectable == selectable) return; |
| 4705 | |
| 4706 | mTextIsSelectable = selectable; |
| 4707 | |
Gilles Debunne | cbcb345 | 2010-12-17 15:31:02 -0800 | [diff] [blame] | 4708 | setFocusableInTouchMode(selectable); |
Gilles Debunne | 86b9c78 | 2010-11-11 10:43:48 -0800 | [diff] [blame] | 4709 | setFocusable(selectable); |
| 4710 | setClickable(selectable); |
| 4711 | setLongClickable(selectable); |
| 4712 | |
| 4713 | // mInputType is already EditorInfo.TYPE_NULL and mInput is null; |
| 4714 | |
| 4715 | setMovementMethod(selectable ? ArrowKeyMovementMethod.getInstance() : null); |
| 4716 | setText(getText(), selectable ? BufferType.SPANNABLE : BufferType.NORMAL); |
| 4717 | |
| 4718 | // Called by setText above, but safer in case of future code changes |
| 4719 | prepareCursorControllers(); |
| 4720 | } |
| 4721 | |
| 4722 | @Override |
| 4723 | protected int[] onCreateDrawableState(int extraSpace) { |
Gilles Debunne | fb81703 | 2011-01-13 13:52:49 -0800 | [diff] [blame] | 4724 | final int[] drawableState; |
Gilles Debunne | da0a3f0 | 2010-12-22 10:07:15 -0800 | [diff] [blame] | 4725 | |
Gilles Debunne | fb81703 | 2011-01-13 13:52:49 -0800 | [diff] [blame] | 4726 | if (mSingleLine) { |
| 4727 | drawableState = super.onCreateDrawableState(extraSpace); |
| 4728 | } else { |
| 4729 | drawableState = super.onCreateDrawableState(extraSpace + 1); |
Gilles Debunne | da0a3f0 | 2010-12-22 10:07:15 -0800 | [diff] [blame] | 4730 | mergeDrawableStates(drawableState, MULTILINE_STATE_SET); |
| 4731 | } |
| 4732 | |
Gilles Debunne | 86b9c78 | 2010-11-11 10:43:48 -0800 | [diff] [blame] | 4733 | if (mTextIsSelectable) { |
| 4734 | // Disable pressed state, which was introduced when TextView was made clickable. |
| 4735 | // Prevents text color change. |
| 4736 | // setClickable(false) would have a similar effect, but it also disables focus changes |
| 4737 | // and long press actions, which are both needed by text selection. |
| 4738 | final int length = drawableState.length; |
| 4739 | for (int i = 0; i < length; i++) { |
| 4740 | if (drawableState[i] == R.attr.state_pressed) { |
| 4741 | final int[] nonPressedState = new int[length - 1]; |
| 4742 | System.arraycopy(drawableState, 0, nonPressedState, 0, i); |
| 4743 | System.arraycopy(drawableState, i + 1, nonPressedState, i, length - i - 1); |
| 4744 | return nonPressedState; |
| 4745 | } |
| 4746 | } |
| 4747 | } |
Gilles Debunne | da0a3f0 | 2010-12-22 10:07:15 -0800 | [diff] [blame] | 4748 | |
Gilles Debunne | 86b9c78 | 2010-11-11 10:43:48 -0800 | [diff] [blame] | 4749 | return drawableState; |
| 4750 | } |
| 4751 | |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 4752 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4753 | protected void onDraw(Canvas canvas) { |
Michael Jurka | 2b942d5 | 2011-03-01 13:26:11 -0800 | [diff] [blame] | 4754 | if (mPreDrawState == PREDRAW_DONE) { |
| 4755 | final ViewTreeObserver observer = getViewTreeObserver(); |
| 4756 | observer.removeOnPreDrawListener(this); |
| 4757 | mPreDrawState = PREDRAW_NOT_REGISTERED; |
| 4758 | } |
| 4759 | |
Romain Guy | 909cbaf | 2010-10-13 18:19:48 -0700 | [diff] [blame] | 4760 | if (mCurrentAlpha <= ViewConfiguration.ALPHA_THRESHOLD_INT) return; |
| 4761 | |
Romain Guy | 986003d | 2009-03-25 17:42:35 -0700 | [diff] [blame] | 4762 | restartMarqueeIfNeeded(); |
| 4763 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4764 | // Draw the background for this view |
| 4765 | super.onDraw(canvas); |
| 4766 | |
| 4767 | final int compoundPaddingLeft = getCompoundPaddingLeft(); |
| 4768 | final int compoundPaddingTop = getCompoundPaddingTop(); |
| 4769 | final int compoundPaddingRight = getCompoundPaddingRight(); |
| 4770 | final int compoundPaddingBottom = getCompoundPaddingBottom(); |
| 4771 | final int scrollX = mScrollX; |
| 4772 | final int scrollY = mScrollY; |
| 4773 | final int right = mRight; |
| 4774 | final int left = mLeft; |
| 4775 | final int bottom = mBottom; |
| 4776 | final int top = mTop; |
| 4777 | |
| 4778 | final Drawables dr = mDrawables; |
| 4779 | if (dr != null) { |
| 4780 | /* |
| 4781 | * Compound, not extended, because the icon is not clipped |
| 4782 | * if the text height is smaller. |
| 4783 | */ |
| 4784 | |
| 4785 | int vspace = bottom - top - compoundPaddingBottom - compoundPaddingTop; |
| 4786 | int hspace = right - left - compoundPaddingRight - compoundPaddingLeft; |
| 4787 | |
Romain Guy | 3c77d39 | 2009-05-20 11:26:50 -0700 | [diff] [blame] | 4788 | // IMPORTANT: The coordinates computed are also used in invalidateDrawable() |
| 4789 | // Make sure to update invalidateDrawable() when changing this code. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4790 | if (dr.mDrawableLeft != null) { |
| 4791 | canvas.save(); |
| 4792 | canvas.translate(scrollX + mPaddingLeft, |
| 4793 | scrollY + compoundPaddingTop + |
| 4794 | (vspace - dr.mDrawableHeightLeft) / 2); |
| 4795 | dr.mDrawableLeft.draw(canvas); |
| 4796 | canvas.restore(); |
| 4797 | } |
| 4798 | |
Romain Guy | 3c77d39 | 2009-05-20 11:26:50 -0700 | [diff] [blame] | 4799 | // IMPORTANT: The coordinates computed are also used in invalidateDrawable() |
| 4800 | // Make sure to update invalidateDrawable() when changing this code. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4801 | if (dr.mDrawableRight != null) { |
| 4802 | canvas.save(); |
| 4803 | canvas.translate(scrollX + right - left - mPaddingRight - dr.mDrawableSizeRight, |
| 4804 | scrollY + compoundPaddingTop + (vspace - dr.mDrawableHeightRight) / 2); |
| 4805 | dr.mDrawableRight.draw(canvas); |
| 4806 | canvas.restore(); |
| 4807 | } |
| 4808 | |
Romain Guy | 3c77d39 | 2009-05-20 11:26:50 -0700 | [diff] [blame] | 4809 | // IMPORTANT: The coordinates computed are also used in invalidateDrawable() |
| 4810 | // Make sure to update invalidateDrawable() when changing this code. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4811 | if (dr.mDrawableTop != null) { |
| 4812 | canvas.save(); |
| 4813 | canvas.translate(scrollX + compoundPaddingLeft + (hspace - dr.mDrawableWidthTop) / 2, |
| 4814 | scrollY + mPaddingTop); |
| 4815 | dr.mDrawableTop.draw(canvas); |
| 4816 | canvas.restore(); |
| 4817 | } |
| 4818 | |
Romain Guy | 3c77d39 | 2009-05-20 11:26:50 -0700 | [diff] [blame] | 4819 | // IMPORTANT: The coordinates computed are also used in invalidateDrawable() |
| 4820 | // Make sure to update invalidateDrawable() when changing this code. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4821 | if (dr.mDrawableBottom != null) { |
| 4822 | canvas.save(); |
| 4823 | canvas.translate(scrollX + compoundPaddingLeft + |
| 4824 | (hspace - dr.mDrawableWidthBottom) / 2, |
| 4825 | scrollY + bottom - top - mPaddingBottom - dr.mDrawableSizeBottom); |
| 4826 | dr.mDrawableBottom.draw(canvas); |
| 4827 | canvas.restore(); |
| 4828 | } |
| 4829 | } |
| 4830 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4831 | int color = mCurTextColor; |
| 4832 | |
| 4833 | if (mLayout == null) { |
| 4834 | assumeLayout(); |
| 4835 | } |
| 4836 | |
| 4837 | Layout layout = mLayout; |
| 4838 | int cursorcolor = color; |
| 4839 | |
| 4840 | if (mHint != null && mText.length() == 0) { |
| 4841 | if (mHintTextColor != null) { |
| 4842 | color = mCurHintTextColor; |
| 4843 | } |
| 4844 | |
| 4845 | layout = mHintLayout; |
| 4846 | } |
| 4847 | |
| 4848 | mTextPaint.setColor(color); |
Gilles Debunne | 3dbf55c | 2010-12-16 10:31:51 -0800 | [diff] [blame] | 4849 | if (mCurrentAlpha != 255) { |
| 4850 | // If set, the alpha will override the color's alpha. Multiply the alphas. |
| 4851 | mTextPaint.setAlpha((mCurrentAlpha * Color.alpha(color)) / 255); |
| 4852 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4853 | mTextPaint.drawableState = getDrawableState(); |
| 4854 | |
| 4855 | canvas.save(); |
| 4856 | /* Would be faster if we didn't have to do this. Can we chop the |
| 4857 | (displayable) text so that we don't need to do this ever? |
| 4858 | */ |
| 4859 | |
| 4860 | int extendedPaddingTop = getExtendedPaddingTop(); |
| 4861 | int extendedPaddingBottom = getExtendedPaddingBottom(); |
| 4862 | |
| 4863 | float clipLeft = compoundPaddingLeft + scrollX; |
| 4864 | float clipTop = extendedPaddingTop + scrollY; |
| 4865 | float clipRight = right - left - compoundPaddingRight + scrollX; |
| 4866 | float clipBottom = bottom - top - extendedPaddingBottom + scrollY; |
| 4867 | |
| 4868 | if (mShadowRadius != 0) { |
| 4869 | clipLeft += Math.min(0, mShadowDx - mShadowRadius); |
| 4870 | clipRight += Math.max(0, mShadowDx + mShadowRadius); |
| 4871 | |
| 4872 | clipTop += Math.min(0, mShadowDy - mShadowRadius); |
| 4873 | clipBottom += Math.max(0, mShadowDy + mShadowRadius); |
| 4874 | } |
| 4875 | |
| 4876 | canvas.clipRect(clipLeft, clipTop, clipRight, clipBottom); |
| 4877 | |
| 4878 | int voffsetText = 0; |
| 4879 | int voffsetCursor = 0; |
| 4880 | |
| 4881 | // translate in by our padding |
| 4882 | { |
| 4883 | /* shortcircuit calling getVerticaOffset() */ |
| 4884 | if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { |
| 4885 | voffsetText = getVerticalOffset(false); |
| 4886 | voffsetCursor = getVerticalOffset(true); |
| 4887 | } |
| 4888 | canvas.translate(compoundPaddingLeft, extendedPaddingTop + voffsetText); |
| 4889 | } |
| 4890 | |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 4891 | final int layoutDirection = getResolvedLayoutDirection(); |
| 4892 | final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection); |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 4893 | if (mEllipsize == TextUtils.TruncateAt.MARQUEE && |
| 4894 | mMarqueeFadeMode != MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4895 | if (!mSingleLine && getLineCount() == 1 && canMarquee() && |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 4896 | (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) != Gravity.LEFT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4897 | canvas.translate(mLayout.getLineRight(0) - (mRight - mLeft - |
| 4898 | getCompoundPaddingLeft() - getCompoundPaddingRight()), 0.0f); |
| 4899 | } |
| 4900 | |
| 4901 | if (mMarquee != null && mMarquee.isRunning()) { |
| 4902 | canvas.translate(-mMarquee.mScroll, 0.0f); |
| 4903 | } |
| 4904 | } |
| 4905 | |
| 4906 | Path highlight = null; |
| 4907 | int selStart = -1, selEnd = -1; |
Gilles Debunne | f75c97e | 2011-02-10 16:09:53 -0800 | [diff] [blame] | 4908 | boolean drawCursor = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4909 | |
| 4910 | // If there is no movement method, then there can be no selection. |
| 4911 | // Check that first and attempt to skip everything having to do with |
| 4912 | // the cursor. |
| 4913 | // XXX This is not strictly true -- a program could set the |
| 4914 | // selection manually if it really wanted to. |
| 4915 | if (mMovement != null && (isFocused() || isPressed())) { |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 4916 | selStart = getSelectionStart(); |
| 4917 | selEnd = getSelectionEnd(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4918 | |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 4919 | if (selStart >= 0) { |
Gilles Debunne | fd419b0 | 2011-08-25 11:53:26 -0700 | [diff] [blame] | 4920 | if (mHighlightPath == null) mHighlightPath = new Path(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4921 | |
| 4922 | if (selStart == selEnd) { |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 4923 | if (isCursorVisible() && |
Gilles Debunne | 86b9c78 | 2010-11-11 10:43:48 -0800 | [diff] [blame] | 4924 | (SystemClock.uptimeMillis() - mShowCursor) % (2 * BLINK) < BLINK) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4925 | if (mHighlightPathBogus) { |
| 4926 | mHighlightPath.reset(); |
| 4927 | mLayout.getCursorPath(selStart, mHighlightPath, mText); |
Gilles Debunne | f75c97e | 2011-02-10 16:09:53 -0800 | [diff] [blame] | 4928 | updateCursorsPositions(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4929 | mHighlightPathBogus = false; |
| 4930 | } |
| 4931 | |
| 4932 | // XXX should pass to skin instead of drawing directly |
| 4933 | mHighlightPaint.setColor(cursorcolor); |
Gilles Debunne | 3dbf55c | 2010-12-16 10:31:51 -0800 | [diff] [blame] | 4934 | if (mCurrentAlpha != 255) { |
| 4935 | mHighlightPaint.setAlpha( |
| 4936 | (mCurrentAlpha * Color.alpha(cursorcolor)) / 255); |
| 4937 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4938 | mHighlightPaint.setStyle(Paint.Style.STROKE); |
Gilles Debunne | 46b7d44 | 2011-02-17 16:03:10 -0800 | [diff] [blame] | 4939 | highlight = mHighlightPath; |
Gilles Debunne | eca97a3 | 2011-02-23 17:48:28 -0800 | [diff] [blame] | 4940 | drawCursor = mCursorCount > 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4941 | } |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 4942 | } else if (textCanBeSelected()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4943 | if (mHighlightPathBogus) { |
| 4944 | mHighlightPath.reset(); |
| 4945 | mLayout.getSelectionPath(selStart, selEnd, mHighlightPath); |
| 4946 | mHighlightPathBogus = false; |
| 4947 | } |
| 4948 | |
| 4949 | // XXX should pass to skin instead of drawing directly |
| 4950 | mHighlightPaint.setColor(mHighlightColor); |
Gilles Debunne | 3dbf55c | 2010-12-16 10:31:51 -0800 | [diff] [blame] | 4951 | if (mCurrentAlpha != 255) { |
| 4952 | mHighlightPaint.setAlpha( |
| 4953 | (mCurrentAlpha * Color.alpha(mHighlightColor)) / 255); |
| 4954 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4955 | mHighlightPaint.setStyle(Paint.Style.FILL); |
| 4956 | |
| 4957 | highlight = mHighlightPath; |
| 4958 | } |
| 4959 | } |
| 4960 | } |
| 4961 | |
| 4962 | /* Comment out until we decide what to do about animations |
| 4963 | boolean isLinearTextOn = false; |
| 4964 | if (currentTransformation != null) { |
| 4965 | isLinearTextOn = mTextPaint.isLinearTextOn(); |
| 4966 | Matrix m = currentTransformation.getMatrix(); |
| 4967 | if (!m.isIdentity()) { |
| 4968 | // mTextPaint.setLinearTextOn(true); |
| 4969 | } |
| 4970 | } |
| 4971 | */ |
| 4972 | |
| 4973 | final InputMethodState ims = mInputMethodState; |
Gilles Debunne | 12d91ce | 2010-12-10 11:36:29 -0800 | [diff] [blame] | 4974 | final int cursorOffsetVertical = voffsetCursor - voffsetText; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4975 | if (ims != null && ims.mBatchEditNesting == 0) { |
| 4976 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 4977 | if (imm != null) { |
| 4978 | if (imm.isActive(this)) { |
| 4979 | boolean reported = false; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4980 | if (ims.mContentChanged || ims.mSelectionModeChanged) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4981 | // We are in extract mode and the content has changed |
| 4982 | // in some way... just report complete new text to the |
| 4983 | // input method. |
| 4984 | reported = reportExtractedText(); |
| 4985 | } |
| 4986 | if (!reported && highlight != null) { |
| 4987 | int candStart = -1; |
| 4988 | int candEnd = -1; |
| 4989 | if (mText instanceof Spannable) { |
| 4990 | Spannable sp = (Spannable)mText; |
| 4991 | candStart = EditableInputConnection.getComposingSpanStart(sp); |
| 4992 | candEnd = EditableInputConnection.getComposingSpanEnd(sp); |
| 4993 | } |
| 4994 | imm.updateSelection(this, selStart, selEnd, candStart, candEnd); |
| 4995 | } |
| 4996 | } |
| 4997 | |
| 4998 | if (imm.isWatchingCursor(this) && highlight != null) { |
| 4999 | highlight.computeBounds(ims.mTmpRectF, true); |
| 5000 | ims.mTmpOffset[0] = ims.mTmpOffset[1] = 0; |
| 5001 | |
| 5002 | canvas.getMatrix().mapPoints(ims.mTmpOffset); |
| 5003 | ims.mTmpRectF.offset(ims.mTmpOffset[0], ims.mTmpOffset[1]); |
| 5004 | |
Gilles Debunne | 12d91ce | 2010-12-10 11:36:29 -0800 | [diff] [blame] | 5005 | ims.mTmpRectF.offset(0, cursorOffsetVertical); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5006 | |
| 5007 | ims.mCursorRectInWindow.set((int)(ims.mTmpRectF.left + 0.5), |
| 5008 | (int)(ims.mTmpRectF.top + 0.5), |
| 5009 | (int)(ims.mTmpRectF.right + 0.5), |
| 5010 | (int)(ims.mTmpRectF.bottom + 0.5)); |
| 5011 | |
| 5012 | imm.updateCursor(this, |
| 5013 | ims.mCursorRectInWindow.left, ims.mCursorRectInWindow.top, |
| 5014 | ims.mCursorRectInWindow.right, ims.mCursorRectInWindow.bottom); |
| 5015 | } |
| 5016 | } |
| 5017 | } |
| 5018 | |
Gilles Debunne | 12d91ce | 2010-12-10 11:36:29 -0800 | [diff] [blame] | 5019 | if (mCorrectionHighlighter != null) { |
| 5020 | mCorrectionHighlighter.draw(canvas, cursorOffsetVertical); |
| 5021 | } |
| 5022 | |
Gilles Debunne | 46b7d44 | 2011-02-17 16:03:10 -0800 | [diff] [blame] | 5023 | if (drawCursor) { |
| 5024 | drawCursor(canvas, cursorOffsetVertical); |
| 5025 | // Rely on the drawable entirely, do not draw the cursor line. |
| 5026 | // Has to be done after the IMM related code above which relies on the highlight. |
| 5027 | highlight = null; |
| 5028 | } |
Gilles Debunne | f75c97e | 2011-02-10 16:09:53 -0800 | [diff] [blame] | 5029 | |
Gilles Debunne | 12d91ce | 2010-12-10 11:36:29 -0800 | [diff] [blame] | 5030 | layout.draw(canvas, highlight, mHighlightPaint, cursorOffsetVertical); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5031 | |
Romain Guy | c230319 | 2009-04-03 17:37:18 -0700 | [diff] [blame] | 5032 | if (mMarquee != null && mMarquee.shouldDrawGhost()) { |
| 5033 | canvas.translate((int) mMarquee.getGhostOffset(), 0.0f); |
Gilles Debunne | 12d91ce | 2010-12-10 11:36:29 -0800 | [diff] [blame] | 5034 | layout.draw(canvas, highlight, mHighlightPaint, cursorOffsetVertical); |
Romain Guy | c230319 | 2009-04-03 17:37:18 -0700 | [diff] [blame] | 5035 | } |
| 5036 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5037 | /* Comment out until we decide what to do about animations |
| 5038 | if (currentTransformation != null) { |
| 5039 | mTextPaint.setLinearTextOn(isLinearTextOn); |
| 5040 | } |
| 5041 | */ |
| 5042 | |
| 5043 | canvas.restore(); |
Leon Scroggins | 5642625 | 2010-11-01 15:45:37 -0400 | [diff] [blame] | 5044 | } |
| 5045 | |
Gilles Debunne | f75c97e | 2011-02-10 16:09:53 -0800 | [diff] [blame] | 5046 | private void updateCursorsPositions() { |
Gilles Debunne | eca97a3 | 2011-02-23 17:48:28 -0800 | [diff] [blame] | 5047 | if (mCursorDrawableRes == 0) { |
| 5048 | mCursorCount = 0; |
| 5049 | return; |
| 5050 | } |
Gilles Debunne | f75c97e | 2011-02-10 16:09:53 -0800 | [diff] [blame] | 5051 | |
| 5052 | final int offset = getSelectionStart(); |
| 5053 | final int line = mLayout.getLineForOffset(offset); |
| 5054 | final int top = mLayout.getLineTop(line); |
| 5055 | final int bottom = mLayout.getLineTop(line + 1); |
| 5056 | |
| 5057 | mCursorCount = mLayout.isLevelBoundary(offset) ? 2 : 1; |
| 5058 | |
| 5059 | int middle = bottom; |
| 5060 | if (mCursorCount == 2) { |
| 5061 | // Similar to what is done in {@link Layout.#getCursorPath(int, Path, CharSequence)} |
| 5062 | middle = (top + bottom) >> 1; |
| 5063 | } |
| 5064 | |
| 5065 | updateCursorPosition(0, top, middle, mLayout.getPrimaryHorizontal(offset)); |
| 5066 | |
| 5067 | if (mCursorCount == 2) { |
| 5068 | updateCursorPosition(1, middle, bottom, mLayout.getSecondaryHorizontal(offset)); |
| 5069 | } |
| 5070 | } |
| 5071 | |
| 5072 | private void updateCursorPosition(int cursorIndex, int top, int bottom, float horizontal) { |
| 5073 | if (mCursorDrawable[cursorIndex] == null) |
| 5074 | mCursorDrawable[cursorIndex] = mContext.getResources().getDrawable(mCursorDrawableRes); |
| 5075 | |
| 5076 | if (mTempRect == null) mTempRect = new Rect(); |
| 5077 | |
| 5078 | mCursorDrawable[cursorIndex].getPadding(mTempRect); |
| 5079 | final int width = mCursorDrawable[cursorIndex].getIntrinsicWidth(); |
| 5080 | horizontal = Math.max(0.5f, horizontal - 0.5f); |
| 5081 | final int left = (int) (horizontal) - mTempRect.left; |
| 5082 | mCursorDrawable[cursorIndex].setBounds(left, top - mTempRect.top, left + width, |
| 5083 | bottom + mTempRect.bottom); |
| 5084 | } |
| 5085 | |
| 5086 | private void drawCursor(Canvas canvas, int cursorOffsetVertical) { |
| 5087 | final boolean translate = cursorOffsetVertical != 0; |
| 5088 | if (translate) canvas.translate(0, cursorOffsetVertical); |
| 5089 | for (int i = 0; i < mCursorCount; i++) { |
| 5090 | mCursorDrawable[i].draw(canvas); |
| 5091 | } |
| 5092 | if (translate) canvas.translate(0, -cursorOffsetVertical); |
| 5093 | } |
| 5094 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5095 | @Override |
| 5096 | public void getFocusedRect(Rect r) { |
| 5097 | if (mLayout == null) { |
| 5098 | super.getFocusedRect(r); |
| 5099 | return; |
| 5100 | } |
| 5101 | |
Dianne Hackborn | 70a3f67 | 2011-08-08 14:32:41 -0700 | [diff] [blame] | 5102 | int selEnd = getSelectionEnd(); |
| 5103 | if (selEnd < 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5104 | super.getFocusedRect(r); |
| 5105 | return; |
| 5106 | } |
| 5107 | |
Dianne Hackborn | 70a3f67 | 2011-08-08 14:32:41 -0700 | [diff] [blame] | 5108 | int selStart = getSelectionStart(); |
| 5109 | if (selStart < 0 || selStart >= selEnd) { |
| 5110 | int line = mLayout.getLineForOffset(selEnd); |
| 5111 | r.top = mLayout.getLineTop(line); |
| 5112 | r.bottom = mLayout.getLineBottom(line); |
| 5113 | r.left = (int) mLayout.getPrimaryHorizontal(selEnd) - 2; |
| 5114 | r.right = r.left + 4; |
| 5115 | } else { |
| 5116 | int lineStart = mLayout.getLineForOffset(selStart); |
| 5117 | int lineEnd = mLayout.getLineForOffset(selEnd); |
| 5118 | r.top = mLayout.getLineTop(lineStart); |
| 5119 | r.bottom = mLayout.getLineBottom(lineEnd); |
| 5120 | if (lineStart == lineEnd) { |
| 5121 | r.left = (int) mLayout.getPrimaryHorizontal(selStart); |
| 5122 | r.right = (int) mLayout.getPrimaryHorizontal(selEnd); |
| 5123 | } else { |
| 5124 | // Selection extends across multiple lines -- the focused |
| 5125 | // rect covers the entire width. |
Gilles Debunne | fd419b0 | 2011-08-25 11:53:26 -0700 | [diff] [blame] | 5126 | if (mHighlightPath == null) mHighlightPath = new Path(); |
Dianne Hackborn | 70a3f67 | 2011-08-08 14:32:41 -0700 | [diff] [blame] | 5127 | if (mHighlightPathBogus) { |
| 5128 | mHighlightPath.reset(); |
| 5129 | mLayout.getSelectionPath(selStart, selEnd, mHighlightPath); |
| 5130 | mHighlightPathBogus = false; |
| 5131 | } |
| 5132 | synchronized (sTempRect) { |
| 5133 | mHighlightPath.computeBounds(sTempRect, true); |
| 5134 | r.left = (int)sTempRect.left-1; |
| 5135 | r.right = (int)sTempRect.right+1; |
| 5136 | } |
| 5137 | } |
| 5138 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5139 | |
| 5140 | // Adjust for padding and gravity. |
| 5141 | int paddingLeft = getCompoundPaddingLeft(); |
| 5142 | int paddingTop = getExtendedPaddingTop(); |
| 5143 | if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { |
| 5144 | paddingTop += getVerticalOffset(false); |
| 5145 | } |
| 5146 | r.offset(paddingLeft, paddingTop); |
| 5147 | } |
| 5148 | |
| 5149 | /** |
| 5150 | * Return the number of lines of text, or 0 if the internal Layout has not |
| 5151 | * been built. |
| 5152 | */ |
| 5153 | public int getLineCount() { |
| 5154 | return mLayout != null ? mLayout.getLineCount() : 0; |
| 5155 | } |
| 5156 | |
| 5157 | /** |
| 5158 | * Return the baseline for the specified line (0...getLineCount() - 1) |
| 5159 | * If bounds is not null, return the top, left, right, bottom extents |
| 5160 | * of the specified line in it. If the internal Layout has not been built, |
| 5161 | * return 0 and set bounds to (0, 0, 0, 0) |
| 5162 | * @param line which line to examine (0..getLineCount() - 1) |
| 5163 | * @param bounds Optional. If not null, it returns the extent of the line |
| 5164 | * @return the Y-coordinate of the baseline |
| 5165 | */ |
| 5166 | public int getLineBounds(int line, Rect bounds) { |
| 5167 | if (mLayout == null) { |
| 5168 | if (bounds != null) { |
| 5169 | bounds.set(0, 0, 0, 0); |
| 5170 | } |
| 5171 | return 0; |
| 5172 | } |
| 5173 | else { |
| 5174 | int baseline = mLayout.getLineBounds(line, bounds); |
| 5175 | |
| 5176 | int voffset = getExtendedPaddingTop(); |
| 5177 | if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { |
| 5178 | voffset += getVerticalOffset(true); |
| 5179 | } |
| 5180 | if (bounds != null) { |
| 5181 | bounds.offset(getCompoundPaddingLeft(), voffset); |
| 5182 | } |
| 5183 | return baseline + voffset; |
| 5184 | } |
| 5185 | } |
| 5186 | |
| 5187 | @Override |
| 5188 | public int getBaseline() { |
| 5189 | if (mLayout == null) { |
| 5190 | return super.getBaseline(); |
| 5191 | } |
| 5192 | |
| 5193 | int voffset = 0; |
| 5194 | if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { |
| 5195 | voffset = getVerticalOffset(true); |
| 5196 | } |
| 5197 | |
| 5198 | return getExtendedPaddingTop() + voffset + mLayout.getLineBaseline(0); |
| 5199 | } |
| 5200 | |
Romain Guy | f2fc460 | 2011-07-19 15:20:03 -0700 | [diff] [blame] | 5201 | /** |
| 5202 | * @hide |
| 5203 | * @param offsetRequired |
| 5204 | */ |
| 5205 | @Override |
| 5206 | protected int getFadeTop(boolean offsetRequired) { |
Romain Guy | 59f13c7d | 2011-07-19 18:35:33 -0700 | [diff] [blame] | 5207 | if (mLayout == null) return 0; |
| 5208 | |
Romain Guy | f2fc460 | 2011-07-19 15:20:03 -0700 | [diff] [blame] | 5209 | int voffset = 0; |
| 5210 | if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { |
| 5211 | voffset = getVerticalOffset(true); |
| 5212 | } |
| 5213 | |
| 5214 | if (offsetRequired) voffset += getTopPaddingOffset(); |
| 5215 | |
| 5216 | return getExtendedPaddingTop() + voffset; |
| 5217 | } |
| 5218 | |
| 5219 | /** |
| 5220 | * @hide |
| 5221 | * @param offsetRequired |
| 5222 | */ |
Gilles Debunne | 3784a7f | 2011-07-15 13:49:38 -0700 | [diff] [blame] | 5223 | @Override |
Romain Guy | f2fc460 | 2011-07-19 15:20:03 -0700 | [diff] [blame] | 5224 | protected int getFadeHeight(boolean offsetRequired) { |
| 5225 | return mLayout != null ? mLayout.getHeight() : 0; |
| 5226 | } |
| 5227 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5228 | @Override |
Gilles Debunne | 4a7199a | 2011-07-11 14:58:27 -0700 | [diff] [blame] | 5229 | public boolean onKeyPreIme(int keyCode, KeyEvent event) { |
| 5230 | if (keyCode == KeyEvent.KEYCODE_BACK) { |
Gilles Debunne | 4a7199a | 2011-07-11 14:58:27 -0700 | [diff] [blame] | 5231 | boolean isInSelectionMode = mSelectionActionMode != null; |
| 5232 | |
Gilles Debunne | 28294cc | 2011-08-24 12:02:05 -0700 | [diff] [blame] | 5233 | if (isInSelectionMode) { |
Gilles Debunne | 4a7199a | 2011-07-11 14:58:27 -0700 | [diff] [blame] | 5234 | if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { |
| 5235 | KeyEvent.DispatcherState state = getKeyDispatcherState(); |
| 5236 | if (state != null) { |
| 5237 | state.startTracking(event, this); |
| 5238 | } |
| 5239 | return true; |
| 5240 | } else if (event.getAction() == KeyEvent.ACTION_UP) { |
| 5241 | KeyEvent.DispatcherState state = getKeyDispatcherState(); |
| 5242 | if (state != null) { |
| 5243 | state.handleUpEvent(event); |
| 5244 | } |
| 5245 | if (event.isTracking() && !event.isCanceled()) { |
Gilles Debunne | 4a7199a | 2011-07-11 14:58:27 -0700 | [diff] [blame] | 5246 | if (isInSelectionMode) { |
| 5247 | stopSelectionActionMode(); |
| 5248 | return true; |
| 5249 | } |
| 5250 | } |
| 5251 | } |
| 5252 | } |
| 5253 | } |
| 5254 | return super.onKeyPreIme(keyCode, event); |
| 5255 | } |
| 5256 | |
| 5257 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5258 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 5259 | int which = doKeyDown(keyCode, event, null); |
| 5260 | if (which == 0) { |
| 5261 | // Go through default dispatching. |
| 5262 | return super.onKeyDown(keyCode, event); |
| 5263 | } |
| 5264 | |
| 5265 | return true; |
| 5266 | } |
| 5267 | |
| 5268 | @Override |
| 5269 | public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5270 | KeyEvent down = KeyEvent.changeAction(event, KeyEvent.ACTION_DOWN); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5271 | |
| 5272 | int which = doKeyDown(keyCode, down, event); |
| 5273 | if (which == 0) { |
| 5274 | // Go through default dispatching. |
| 5275 | return super.onKeyMultiple(keyCode, repeatCount, event); |
| 5276 | } |
| 5277 | if (which == -1) { |
| 5278 | // Consumed the whole thing. |
| 5279 | return true; |
| 5280 | } |
| 5281 | |
| 5282 | repeatCount--; |
| 5283 | |
| 5284 | // We are going to dispatch the remaining events to either the input |
| 5285 | // or movement method. To do this, we will just send a repeated stream |
| 5286 | // of down and up events until we have done the complete repeatCount. |
| 5287 | // It would be nice if those interfaces had an onKeyMultiple() method, |
| 5288 | // but adding that is a more complicated change. |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5289 | KeyEvent up = KeyEvent.changeAction(event, KeyEvent.ACTION_UP); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5290 | if (which == 1) { |
| 5291 | mInput.onKeyUp(this, (Editable)mText, keyCode, up); |
| 5292 | while (--repeatCount > 0) { |
| 5293 | mInput.onKeyDown(this, (Editable)mText, keyCode, down); |
| 5294 | mInput.onKeyUp(this, (Editable)mText, keyCode, up); |
| 5295 | } |
Gilles Debunne | b7fc63f | 2011-01-27 15:55:29 -0800 | [diff] [blame] | 5296 | hideErrorIfUnchanged(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5297 | |
| 5298 | } else if (which == 2) { |
| 5299 | mMovement.onKeyUp(this, (Spannable)mText, keyCode, up); |
| 5300 | while (--repeatCount > 0) { |
| 5301 | mMovement.onKeyDown(this, (Spannable)mText, keyCode, down); |
| 5302 | mMovement.onKeyUp(this, (Spannable)mText, keyCode, up); |
| 5303 | } |
| 5304 | } |
| 5305 | |
| 5306 | return true; |
| 5307 | } |
| 5308 | |
| 5309 | /** |
| 5310 | * Returns true if pressing ENTER in this field advances focus instead |
| 5311 | * of inserting the character. This is true mostly in single-line fields, |
| 5312 | * but also in mail addresses and subjects which will display on multiple |
| 5313 | * lines but where it doesn't make sense to insert newlines. |
| 5314 | */ |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 5315 | private boolean shouldAdvanceFocusOnEnter() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5316 | if (mInput == null) { |
| 5317 | return false; |
| 5318 | } |
| 5319 | |
| 5320 | if (mSingleLine) { |
| 5321 | return true; |
| 5322 | } |
| 5323 | |
| 5324 | if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { |
| 5325 | int variation = mInputType & EditorInfo.TYPE_MASK_VARIATION; |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 5326 | if (variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS |
| 5327 | || variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_SUBJECT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5328 | return true; |
| 5329 | } |
| 5330 | } |
| 5331 | |
| 5332 | return false; |
| 5333 | } |
| 5334 | |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 5335 | /** |
| 5336 | * Returns true if pressing TAB in this field advances focus instead |
| 5337 | * of inserting the character. Insert tabs only in multi-line editors. |
| 5338 | */ |
| 5339 | private boolean shouldAdvanceFocusOnTab() { |
| 5340 | if (mInput != null && !mSingleLine) { |
| 5341 | if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { |
| 5342 | int variation = mInputType & EditorInfo.TYPE_MASK_VARIATION; |
| 5343 | if (variation == EditorInfo.TYPE_TEXT_FLAG_IME_MULTI_LINE |
| 5344 | || variation == EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE) { |
| 5345 | return false; |
| 5346 | } |
| 5347 | } |
| 5348 | } |
| 5349 | return true; |
| 5350 | } |
| 5351 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5352 | private int doKeyDown(int keyCode, KeyEvent event, KeyEvent otherEvent) { |
| 5353 | if (!isEnabled()) { |
| 5354 | return 0; |
| 5355 | } |
| 5356 | |
| 5357 | switch (keyCode) { |
| 5358 | case KeyEvent.KEYCODE_ENTER: |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 5359 | if (event.hasNoModifiers()) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5360 | // When mInputContentType is set, we know that we are |
| 5361 | // running in a "modern" cupcake environment, so don't need |
| 5362 | // to worry about the application trying to capture |
| 5363 | // enter key events. |
| 5364 | if (mInputContentType != null) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5365 | // If there is an action listener, given them a |
| 5366 | // chance to consume the event. |
| 5367 | if (mInputContentType.onEditorActionListener != null && |
| 5368 | mInputContentType.onEditorActionListener.onEditorAction( |
| 5369 | this, EditorInfo.IME_NULL, event)) { |
| 5370 | mInputContentType.enterDown = true; |
| 5371 | // We are consuming the enter key for them. |
| 5372 | return -1; |
| 5373 | } |
| 5374 | } |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 5375 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5376 | // If our editor should move focus when enter is pressed, or |
| 5377 | // this is a generated event from an IME action button, then |
| 5378 | // don't let it be inserted into the text. |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 5379 | if ((event.getFlags() & KeyEvent.FLAG_EDITOR_ACTION) != 0 |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5380 | || shouldAdvanceFocusOnEnter()) { |
Dianne Hackborn | 0500b3c | 2011-11-01 15:28:43 -0700 | [diff] [blame] | 5381 | if (hasOnClickListeners()) { |
Leon Scroggins | 7014b12 | 2011-01-11 15:17:34 -0500 | [diff] [blame] | 5382 | return 0; |
| 5383 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5384 | return -1; |
| 5385 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5386 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5387 | break; |
| 5388 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5389 | case KeyEvent.KEYCODE_DPAD_CENTER: |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 5390 | if (event.hasNoModifiers()) { |
| 5391 | if (shouldAdvanceFocusOnEnter()) { |
| 5392 | return 0; |
| 5393 | } |
| 5394 | } |
| 5395 | break; |
| 5396 | |
| 5397 | case KeyEvent.KEYCODE_TAB: |
| 5398 | if (event.hasNoModifiers() || event.hasModifiers(KeyEvent.META_SHIFT_ON)) { |
| 5399 | if (shouldAdvanceFocusOnTab()) { |
| 5400 | return 0; |
| 5401 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5402 | } |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 5403 | break; |
| 5404 | |
| 5405 | // Has to be done on key down (and not on key up) to correctly be intercepted. |
| 5406 | case KeyEvent.KEYCODE_BACK: |
| 5407 | if (mSelectionActionMode != null) { |
| 5408 | stopSelectionActionMode(); |
| 5409 | return -1; |
| 5410 | } |
| 5411 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | if (mInput != null) { |
Gilles Debunne | b7fc63f | 2011-01-27 15:55:29 -0800 | [diff] [blame] | 5415 | resetErrorChangedFlag(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5416 | |
| 5417 | boolean doDown = true; |
| 5418 | if (otherEvent != null) { |
| 5419 | try { |
| 5420 | beginBatchEdit(); |
Gilles Debunne | 12ab645 | 2011-01-30 12:08:25 -0800 | [diff] [blame] | 5421 | final boolean handled = mInput.onKeyOther(this, (Editable) mText, otherEvent); |
Gilles Debunne | b7fc63f | 2011-01-27 15:55:29 -0800 | [diff] [blame] | 5422 | hideErrorIfUnchanged(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5423 | doDown = false; |
| 5424 | if (handled) { |
| 5425 | return -1; |
| 5426 | } |
| 5427 | } catch (AbstractMethodError e) { |
| 5428 | // onKeyOther was added after 1.0, so if it isn't |
| 5429 | // implemented we need to try to dispatch as a regular down. |
| 5430 | } finally { |
| 5431 | endBatchEdit(); |
| 5432 | } |
| 5433 | } |
| 5434 | |
| 5435 | if (doDown) { |
| 5436 | beginBatchEdit(); |
Gilles Debunne | 12ab645 | 2011-01-30 12:08:25 -0800 | [diff] [blame] | 5437 | final boolean handled = mInput.onKeyDown(this, (Editable) mText, keyCode, event); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5438 | endBatchEdit(); |
Gilles Debunne | 12ab645 | 2011-01-30 12:08:25 -0800 | [diff] [blame] | 5439 | hideErrorIfUnchanged(); |
| 5440 | if (handled) return 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5441 | } |
| 5442 | } |
| 5443 | |
| 5444 | // bug 650865: sometimes we get a key event before a layout. |
| 5445 | // don't try to move around if we don't know the layout. |
| 5446 | |
| 5447 | if (mMovement != null && mLayout != null) { |
| 5448 | boolean doDown = true; |
| 5449 | if (otherEvent != null) { |
| 5450 | try { |
| 5451 | boolean handled = mMovement.onKeyOther(this, (Spannable) mText, |
| 5452 | otherEvent); |
| 5453 | doDown = false; |
| 5454 | if (handled) { |
| 5455 | return -1; |
| 5456 | } |
| 5457 | } catch (AbstractMethodError e) { |
| 5458 | // onKeyOther was added after 1.0, so if it isn't |
| 5459 | // implemented we need to try to dispatch as a regular down. |
| 5460 | } |
| 5461 | } |
| 5462 | if (doDown) { |
| 5463 | if (mMovement.onKeyDown(this, (Spannable)mText, keyCode, event)) |
| 5464 | return 2; |
| 5465 | } |
| 5466 | } |
| 5467 | |
| 5468 | return 0; |
| 5469 | } |
| 5470 | |
Gilles Debunne | b7fc63f | 2011-01-27 15:55:29 -0800 | [diff] [blame] | 5471 | /** |
| 5472 | * Resets the mErrorWasChanged flag, so that future calls to {@link #setError(CharSequence)} |
| 5473 | * can be recorded. |
| 5474 | * @hide |
| 5475 | */ |
| 5476 | public void resetErrorChangedFlag() { |
| 5477 | /* |
| 5478 | * Keep track of what the error was before doing the input |
| 5479 | * so that if an input filter changed the error, we leave |
| 5480 | * that error showing. Otherwise, we take down whatever |
| 5481 | * error was showing when the user types something. |
| 5482 | */ |
| 5483 | mErrorWasChanged = false; |
| 5484 | } |
| 5485 | |
| 5486 | /** |
| 5487 | * @hide |
| 5488 | */ |
| 5489 | public void hideErrorIfUnchanged() { |
| 5490 | if (mError != null && !mErrorWasChanged) { |
| 5491 | setError(null, null); |
| 5492 | } |
| 5493 | } |
| 5494 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5495 | @Override |
| 5496 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 5497 | if (!isEnabled()) { |
| 5498 | return super.onKeyUp(keyCode, event); |
| 5499 | } |
| 5500 | |
| 5501 | switch (keyCode) { |
| 5502 | case KeyEvent.KEYCODE_DPAD_CENTER: |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 5503 | if (event.hasNoModifiers()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5504 | /* |
| 5505 | * If there is a click listener, just call through to |
| 5506 | * super, which will invoke it. |
| 5507 | * |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 5508 | * If there isn't a click listener, try to show the soft |
| 5509 | * input method. (It will also |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5510 | * call performClick(), but that won't do anything in |
| 5511 | * this case.) |
| 5512 | */ |
Gilles Debunne | 06a8e9b | 2011-12-08 10:39:39 -0800 | [diff] [blame] | 5513 | if (!hasOnClickListeners()) { |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 5514 | if (mMovement != null && mText instanceof Editable |
| 5515 | && mLayout != null && onCheckIsTextEditor()) { |
Gilles Debunne | 17d31de | 2011-01-27 11:02:18 -0800 | [diff] [blame] | 5516 | InputMethodManager imm = InputMethodManager.peekInstance(); |
satok | a67a3cf | 2011-09-07 17:14:03 +0900 | [diff] [blame] | 5517 | viewClicked(imm); |
Gilles Debunne | 0f4109e | 2011-10-19 11:26:21 -0700 | [diff] [blame] | 5518 | if (imm != null && mSoftInputShownOnFocus) { |
satok | 863fcd6 | 2011-06-21 17:38:02 +0900 | [diff] [blame] | 5519 | imm.showSoftInput(this, 0); |
| 5520 | } |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 5521 | } |
| 5522 | } |
| 5523 | } |
| 5524 | return super.onKeyUp(keyCode, event); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5525 | |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 5526 | case KeyEvent.KEYCODE_ENTER: |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 5527 | if (event.hasNoModifiers()) { |
| 5528 | if (mInputContentType != null |
| 5529 | && mInputContentType.onEditorActionListener != null |
| 5530 | && mInputContentType.enterDown) { |
| 5531 | mInputContentType.enterDown = false; |
| 5532 | if (mInputContentType.onEditorActionListener.onEditorAction( |
| 5533 | this, EditorInfo.IME_NULL, event)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5534 | return true; |
| 5535 | } |
| 5536 | } |
| 5537 | |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 5538 | if ((event.getFlags() & KeyEvent.FLAG_EDITOR_ACTION) != 0 |
| 5539 | || shouldAdvanceFocusOnEnter()) { |
| 5540 | /* |
| 5541 | * If there is a click listener, just call through to |
| 5542 | * super, which will invoke it. |
| 5543 | * |
| 5544 | * If there isn't a click listener, try to advance focus, |
| 5545 | * but still call through to super, which will reset the |
| 5546 | * pressed state and longpress state. (It will also |
| 5547 | * call performClick(), but that won't do anything in |
| 5548 | * this case.) |
| 5549 | */ |
Gilles Debunne | 06a8e9b | 2011-12-08 10:39:39 -0800 | [diff] [blame] | 5550 | if (!hasOnClickListeners()) { |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 5551 | View v = focusSearch(FOCUS_DOWN); |
| 5552 | |
| 5553 | if (v != null) { |
| 5554 | if (!v.requestFocus(FOCUS_DOWN)) { |
| 5555 | throw new IllegalStateException( |
| 5556 | "focus search returned a view " + |
| 5557 | "that wasn't able to take focus!"); |
| 5558 | } |
| 5559 | |
| 5560 | /* |
| 5561 | * Return true because we handled the key; super |
| 5562 | * will return false because there was no click |
| 5563 | * listener. |
| 5564 | */ |
| 5565 | super.onKeyUp(keyCode, event); |
| 5566 | return true; |
| 5567 | } else if ((event.getFlags() |
| 5568 | & KeyEvent.FLAG_EDITOR_ACTION) != 0) { |
| 5569 | // No target for next focus, but make sure the IME |
| 5570 | // if this came from it. |
| 5571 | InputMethodManager imm = InputMethodManager.peekInstance(); |
Gilles Debunne | 17d31de | 2011-01-27 11:02:18 -0800 | [diff] [blame] | 5572 | if (imm != null && imm.isActive(this)) { |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 5573 | imm.hideSoftInputFromWindow(getWindowToken(), 0); |
| 5574 | } |
| 5575 | } |
| 5576 | } |
| 5577 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5578 | return super.onKeyUp(keyCode, event); |
| 5579 | } |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 5580 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5581 | } |
| 5582 | |
| 5583 | if (mInput != null) |
| 5584 | if (mInput.onKeyUp(this, (Editable) mText, keyCode, event)) |
| 5585 | return true; |
| 5586 | |
| 5587 | if (mMovement != null && mLayout != null) |
| 5588 | if (mMovement.onKeyUp(this, (Spannable) mText, keyCode, event)) |
| 5589 | return true; |
| 5590 | |
| 5591 | return super.onKeyUp(keyCode, event); |
| 5592 | } |
| 5593 | |
| 5594 | @Override public boolean onCheckIsTextEditor() { |
| 5595 | return mInputType != EditorInfo.TYPE_NULL; |
| 5596 | } |
Gilles Debunne | b062e81 | 2011-09-27 14:58:37 -0700 | [diff] [blame] | 5597 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5598 | @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { |
Janos Levai | 042856c | 2010-10-15 02:53:58 +0300 | [diff] [blame] | 5599 | if (onCheckIsTextEditor() && isEnabled()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5600 | if (mInputMethodState == null) { |
| 5601 | mInputMethodState = new InputMethodState(); |
| 5602 | } |
| 5603 | outAttrs.inputType = mInputType; |
| 5604 | if (mInputContentType != null) { |
| 5605 | outAttrs.imeOptions = mInputContentType.imeOptions; |
| 5606 | outAttrs.privateImeOptions = mInputContentType.privateImeOptions; |
| 5607 | outAttrs.actionLabel = mInputContentType.imeActionLabel; |
| 5608 | outAttrs.actionId = mInputContentType.imeActionId; |
| 5609 | outAttrs.extras = mInputContentType.extras; |
| 5610 | } else { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5611 | outAttrs.imeOptions = EditorInfo.IME_NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5612 | } |
Dianne Hackborn | dea3ef7 | 2010-10-28 14:24:22 -0700 | [diff] [blame] | 5613 | if (focusSearch(FOCUS_DOWN) != null) { |
| 5614 | outAttrs.imeOptions |= EditorInfo.IME_FLAG_NAVIGATE_NEXT; |
| 5615 | } |
| 5616 | if (focusSearch(FOCUS_UP) != null) { |
| 5617 | outAttrs.imeOptions |= EditorInfo.IME_FLAG_NAVIGATE_PREVIOUS; |
| 5618 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5619 | if ((outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION) |
| 5620 | == EditorInfo.IME_ACTION_UNSPECIFIED) { |
Dianne Hackborn | dea3ef7 | 2010-10-28 14:24:22 -0700 | [diff] [blame] | 5621 | if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NAVIGATE_NEXT) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5622 | // An action has not been set, but the enter key will move to |
| 5623 | // the next focus, so set the action to that. |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5624 | outAttrs.imeOptions |= EditorInfo.IME_ACTION_NEXT; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 5625 | } else { |
| 5626 | // An action has not been set, and there is no focus to move |
| 5627 | // to, so let's just supply a "done" action. |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5628 | outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 5629 | } |
| 5630 | if (!shouldAdvanceFocusOnEnter()) { |
| 5631 | outAttrs.imeOptions |= EditorInfo.IME_FLAG_NO_ENTER_ACTION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5632 | } |
| 5633 | } |
Gilles Debunne | 91a08cf | 2010-11-08 17:34:49 -0800 | [diff] [blame] | 5634 | if (isMultilineInputType(outAttrs.inputType)) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5635 | // Multi-line text editors should always show an enter key. |
| 5636 | outAttrs.imeOptions |= EditorInfo.IME_FLAG_NO_ENTER_ACTION; |
| 5637 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5638 | outAttrs.hintText = mHint; |
| 5639 | if (mText instanceof Editable) { |
| 5640 | InputConnection ic = new EditableInputConnection(this); |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 5641 | outAttrs.initialSelStart = getSelectionStart(); |
| 5642 | outAttrs.initialSelEnd = getSelectionEnd(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5643 | outAttrs.initialCapsMode = ic.getCursorCapsMode(mInputType); |
| 5644 | return ic; |
| 5645 | } |
| 5646 | } |
| 5647 | return null; |
| 5648 | } |
| 5649 | |
| 5650 | /** |
| 5651 | * If this TextView contains editable content, extract a portion of it |
| 5652 | * based on the information in <var>request</var> in to <var>outText</var>. |
| 5653 | * @return Returns true if the text was successfully extracted, else false. |
| 5654 | */ |
| 5655 | public boolean extractText(ExtractedTextRequest request, |
| 5656 | ExtractedText outText) { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5657 | return extractTextInternal(request, EXTRACT_UNKNOWN, EXTRACT_UNKNOWN, |
| 5658 | EXTRACT_UNKNOWN, outText); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5659 | } |
| 5660 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5661 | static final int EXTRACT_NOTHING = -2; |
| 5662 | static final int EXTRACT_UNKNOWN = -1; |
| 5663 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5664 | boolean extractTextInternal(ExtractedTextRequest request, |
| 5665 | int partialStartOffset, int partialEndOffset, int delta, |
| 5666 | ExtractedText outText) { |
| 5667 | final CharSequence content = mText; |
| 5668 | if (content != null) { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5669 | if (partialStartOffset != EXTRACT_NOTHING) { |
| 5670 | final int N = content.length(); |
| 5671 | if (partialStartOffset < 0) { |
| 5672 | outText.partialStartOffset = outText.partialEndOffset = -1; |
| 5673 | partialStartOffset = 0; |
| 5674 | partialEndOffset = N; |
| 5675 | } else { |
Viktor Yakovel | 964be41 | 2010-02-17 08:35:57 +0100 | [diff] [blame] | 5676 | // Now use the delta to determine the actual amount of text |
| 5677 | // we need. |
| 5678 | partialEndOffset += delta; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5679 | // Adjust offsets to ensure we contain full spans. |
| 5680 | if (content instanceof Spanned) { |
| 5681 | Spanned spanned = (Spanned)content; |
| 5682 | Object[] spans = spanned.getSpans(partialStartOffset, |
| 5683 | partialEndOffset, ParcelableSpan.class); |
| 5684 | int i = spans.length; |
| 5685 | while (i > 0) { |
| 5686 | i--; |
| 5687 | int j = spanned.getSpanStart(spans[i]); |
| 5688 | if (j < partialStartOffset) partialStartOffset = j; |
| 5689 | j = spanned.getSpanEnd(spans[i]); |
| 5690 | if (j > partialEndOffset) partialEndOffset = j; |
| 5691 | } |
| 5692 | } |
| 5693 | outText.partialStartOffset = partialStartOffset; |
Viktor Yakovel | 964be41 | 2010-02-17 08:35:57 +0100 | [diff] [blame] | 5694 | outText.partialEndOffset = partialEndOffset - delta; |
| 5695 | |
Eric Fischer | 3292941 | 2009-12-14 17:33:11 -0800 | [diff] [blame] | 5696 | if (partialStartOffset > N) { |
| 5697 | partialStartOffset = N; |
| 5698 | } else if (partialStartOffset < 0) { |
| 5699 | partialStartOffset = 0; |
| 5700 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5701 | if (partialEndOffset > N) { |
| 5702 | partialEndOffset = N; |
| 5703 | } else if (partialEndOffset < 0) { |
| 5704 | partialEndOffset = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5705 | } |
| 5706 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5707 | if ((request.flags&InputConnection.GET_TEXT_WITH_STYLES) != 0) { |
| 5708 | outText.text = content.subSequence(partialStartOffset, |
| 5709 | partialEndOffset); |
| 5710 | } else { |
| 5711 | outText.text = TextUtils.substring(content, partialStartOffset, |
| 5712 | partialEndOffset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5713 | } |
Viktor Yakovel | 970a138c9 | 2010-02-12 16:00:41 +0100 | [diff] [blame] | 5714 | } else { |
| 5715 | outText.partialStartOffset = 0; |
| 5716 | outText.partialEndOffset = 0; |
| 5717 | outText.text = ""; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5718 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5719 | outText.flags = 0; |
| 5720 | if (MetaKeyKeyListener.getMetaState(mText, MetaKeyKeyListener.META_SELECTING) != 0) { |
| 5721 | outText.flags |= ExtractedText.FLAG_SELECTING; |
| 5722 | } |
| 5723 | if (mSingleLine) { |
| 5724 | outText.flags |= ExtractedText.FLAG_SINGLE_LINE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5725 | } |
| 5726 | outText.startOffset = 0; |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 5727 | outText.selectionStart = getSelectionStart(); |
| 5728 | outText.selectionEnd = getSelectionEnd(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5729 | return true; |
| 5730 | } |
| 5731 | return false; |
| 5732 | } |
| 5733 | |
| 5734 | boolean reportExtractedText() { |
| 5735 | final InputMethodState ims = mInputMethodState; |
Marco Nelissen | 9ea9231 | 2009-05-14 15:11:23 -0700 | [diff] [blame] | 5736 | if (ims != null) { |
| 5737 | final boolean contentChanged = ims.mContentChanged; |
| 5738 | if (contentChanged || ims.mSelectionModeChanged) { |
| 5739 | ims.mContentChanged = false; |
| 5740 | ims.mSelectionModeChanged = false; |
| 5741 | final ExtractedTextRequest req = mInputMethodState.mExtracting; |
| 5742 | if (req != null) { |
| 5743 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 5744 | if (imm != null) { |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 5745 | if (DEBUG_EXTRACT) Log.v(LOG_TAG, "Retrieving extracted start=" |
Marco Nelissen | 9ea9231 | 2009-05-14 15:11:23 -0700 | [diff] [blame] | 5746 | + ims.mChangedStart + " end=" + ims.mChangedEnd |
| 5747 | + " delta=" + ims.mChangedDelta); |
| 5748 | if (ims.mChangedStart < 0 && !contentChanged) { |
| 5749 | ims.mChangedStart = EXTRACT_NOTHING; |
| 5750 | } |
| 5751 | if (extractTextInternal(req, ims.mChangedStart, ims.mChangedEnd, |
| 5752 | ims.mChangedDelta, ims.mTmpExtracted)) { |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 5753 | if (DEBUG_EXTRACT) Log.v(LOG_TAG, "Reporting extracted start=" |
Marco Nelissen | 9ea9231 | 2009-05-14 15:11:23 -0700 | [diff] [blame] | 5754 | + ims.mTmpExtracted.partialStartOffset |
| 5755 | + " end=" + ims.mTmpExtracted.partialEndOffset |
| 5756 | + ": " + ims.mTmpExtracted.text); |
| 5757 | imm.updateExtractedText(this, req.token, |
| 5758 | mInputMethodState.mTmpExtracted); |
Viktor Yakovel | 964be41 | 2010-02-17 08:35:57 +0100 | [diff] [blame] | 5759 | ims.mChangedStart = EXTRACT_UNKNOWN; |
| 5760 | ims.mChangedEnd = EXTRACT_UNKNOWN; |
| 5761 | ims.mChangedDelta = 0; |
| 5762 | ims.mContentChanged = false; |
Marco Nelissen | 9ea9231 | 2009-05-14 15:11:23 -0700 | [diff] [blame] | 5763 | return true; |
| 5764 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5765 | } |
| 5766 | } |
| 5767 | } |
| 5768 | } |
| 5769 | return false; |
| 5770 | } |
| 5771 | |
| 5772 | /** |
| 5773 | * This is used to remove all style-impacting spans from text before new |
| 5774 | * extracted text is being replaced into it, so that we don't have any |
| 5775 | * lingering spans applied during the replace. |
| 5776 | */ |
| 5777 | static void removeParcelableSpans(Spannable spannable, int start, int end) { |
| 5778 | Object[] spans = spannable.getSpans(start, end, ParcelableSpan.class); |
| 5779 | int i = spans.length; |
| 5780 | while (i > 0) { |
| 5781 | i--; |
| 5782 | spannable.removeSpan(spans[i]); |
| 5783 | } |
| 5784 | } |
| 5785 | |
| 5786 | /** |
| 5787 | * Apply to this text view the given extracted text, as previously |
| 5788 | * returned by {@link #extractText(ExtractedTextRequest, ExtractedText)}. |
| 5789 | */ |
| 5790 | public void setExtractedText(ExtractedText text) { |
| 5791 | Editable content = getEditableText(); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5792 | if (text.text != null) { |
| 5793 | if (content == null) { |
| 5794 | setText(text.text, TextView.BufferType.EDITABLE); |
| 5795 | } else if (text.partialStartOffset < 0) { |
| 5796 | removeParcelableSpans(content, 0, content.length()); |
| 5797 | content.replace(0, content.length(), text.text); |
| 5798 | } else { |
| 5799 | final int N = content.length(); |
| 5800 | int start = text.partialStartOffset; |
| 5801 | if (start > N) start = N; |
| 5802 | int end = text.partialEndOffset; |
| 5803 | if (end > N) end = N; |
| 5804 | removeParcelableSpans(content, start, end); |
| 5805 | content.replace(start, end, text.text); |
| 5806 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5807 | } |
| 5808 | |
| 5809 | // Now set the selection position... make sure it is in range, to |
| 5810 | // avoid crashes. If this is a partial update, it is possible that |
| 5811 | // the underlying text may have changed, causing us problems here. |
| 5812 | // Also we just don't want to trust clients to do the right thing. |
| 5813 | Spannable sp = (Spannable)getText(); |
| 5814 | final int N = sp.length(); |
| 5815 | int start = text.selectionStart; |
| 5816 | if (start < 0) start = 0; |
| 5817 | else if (start > N) start = N; |
| 5818 | int end = text.selectionEnd; |
| 5819 | if (end < 0) end = 0; |
| 5820 | else if (end > N) end = N; |
| 5821 | Selection.setSelection(sp, start, end); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5822 | |
| 5823 | // Finally, update the selection mode. |
| 5824 | if ((text.flags&ExtractedText.FLAG_SELECTING) != 0) { |
| 5825 | MetaKeyKeyListener.startSelecting(this, sp); |
| 5826 | } else { |
| 5827 | MetaKeyKeyListener.stopSelecting(this, sp); |
| 5828 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5829 | } |
Gilles Debunne | 98fb9ed | 2011-09-07 17:15:41 -0700 | [diff] [blame] | 5830 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5831 | /** |
| 5832 | * @hide |
| 5833 | */ |
| 5834 | public void setExtracting(ExtractedTextRequest req) { |
| 5835 | if (mInputMethodState != null) { |
| 5836 | mInputMethodState.mExtracting = req; |
| 5837 | } |
Gilles Debunne | 98fb9ed | 2011-09-07 17:15:41 -0700 | [diff] [blame] | 5838 | // This would stop a possible selection mode, but no such mode is started in case |
| 5839 | // extracted mode will start. Some text is selected though, and will trigger an action mode |
| 5840 | // in the extracted view. |
Adam Powell | ba0a2c3 | 2010-09-28 17:41:23 -0700 | [diff] [blame] | 5841 | hideControllers(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5842 | } |
Gilles Debunne | 98fb9ed | 2011-09-07 17:15:41 -0700 | [diff] [blame] | 5843 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5844 | /** |
| 5845 | * Called by the framework in response to a text completion from |
| 5846 | * the current input method, provided by it calling |
| 5847 | * {@link InputConnection#commitCompletion |
| 5848 | * InputConnection.commitCompletion()}. The default implementation does |
| 5849 | * nothing; text views that are supporting auto-completion should override |
| 5850 | * this to do their desired behavior. |
| 5851 | * |
| 5852 | * @param text The auto complete text the user has selected. |
| 5853 | */ |
| 5854 | public void onCommitCompletion(CompletionInfo text) { |
Gilles Debunne | d40cc3c | 2011-03-11 16:59:32 -0800 | [diff] [blame] | 5855 | // intentionally empty |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5856 | } |
| 5857 | |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 5858 | /** |
| 5859 | * Called by the framework in response to a text auto-correction (such as fixing a typo using a |
| 5860 | * a dictionnary) from the current input method, provided by it calling |
| 5861 | * {@link InputConnection#commitCorrection} InputConnection.commitCorrection()}. The default |
| 5862 | * implementation flashes the background of the corrected word to provide feedback to the user. |
| 5863 | * |
| 5864 | * @param info The auto correct info about the text that was corrected. |
| 5865 | */ |
| 5866 | public void onCommitCorrection(CorrectionInfo info) { |
Gilles Debunne | 12d91ce | 2010-12-10 11:36:29 -0800 | [diff] [blame] | 5867 | if (mCorrectionHighlighter == null) { |
| 5868 | mCorrectionHighlighter = new CorrectionHighlighter(); |
| 5869 | } else { |
| 5870 | mCorrectionHighlighter.invalidate(false); |
| 5871 | } |
| 5872 | |
| 5873 | mCorrectionHighlighter.highlight(info); |
| 5874 | } |
| 5875 | |
| 5876 | private class CorrectionHighlighter { |
| 5877 | private final Path mPath = new Path(); |
| 5878 | private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 5879 | private int mStart, mEnd; |
| 5880 | private long mFadingStartTime; |
| 5881 | private final static int FADE_OUT_DURATION = 400; |
| 5882 | |
| 5883 | public CorrectionHighlighter() { |
| 5884 | mPaint.setCompatibilityScaling(getResources().getCompatibilityInfo().applicationScale); |
| 5885 | mPaint.setStyle(Paint.Style.FILL); |
| 5886 | } |
| 5887 | |
| 5888 | public void highlight(CorrectionInfo info) { |
| 5889 | mStart = info.getOffset(); |
| 5890 | mEnd = mStart + info.getNewText().length(); |
| 5891 | mFadingStartTime = SystemClock.uptimeMillis(); |
| 5892 | |
| 5893 | if (mStart < 0 || mEnd < 0) { |
| 5894 | stopAnimation(); |
| 5895 | } |
| 5896 | } |
| 5897 | |
| 5898 | public void draw(Canvas canvas, int cursorOffsetVertical) { |
| 5899 | if (updatePath() && updatePaint()) { |
| 5900 | if (cursorOffsetVertical != 0) { |
| 5901 | canvas.translate(0, cursorOffsetVertical); |
| 5902 | } |
| 5903 | |
| 5904 | canvas.drawPath(mPath, mPaint); |
| 5905 | |
| 5906 | if (cursorOffsetVertical != 0) { |
| 5907 | canvas.translate(0, -cursorOffsetVertical); |
| 5908 | } |
Gilles Debunne | 8615ac9 | 2011-11-29 15:25:03 -0800 | [diff] [blame] | 5909 | invalidate(true); // TODO invalidate cursor region only |
Gilles Debunne | 12d91ce | 2010-12-10 11:36:29 -0800 | [diff] [blame] | 5910 | } else { |
| 5911 | stopAnimation(); |
Gilles Debunne | 8615ac9 | 2011-11-29 15:25:03 -0800 | [diff] [blame] | 5912 | invalidate(false); // TODO invalidate cursor region only |
Gilles Debunne | 12d91ce | 2010-12-10 11:36:29 -0800 | [diff] [blame] | 5913 | } |
| 5914 | } |
| 5915 | |
| 5916 | private boolean updatePaint() { |
| 5917 | final long duration = SystemClock.uptimeMillis() - mFadingStartTime; |
| 5918 | if (duration > FADE_OUT_DURATION) return false; |
| 5919 | |
| 5920 | final float coef = 1.0f - (float) duration / FADE_OUT_DURATION; |
| 5921 | final int highlightColorAlpha = Color.alpha(mHighlightColor); |
| 5922 | final int color = (mHighlightColor & 0x00FFFFFF) + |
| 5923 | ((int) (highlightColorAlpha * coef) << 24); |
| 5924 | mPaint.setColor(color); |
| 5925 | return true; |
| 5926 | } |
| 5927 | |
| 5928 | private boolean updatePath() { |
| 5929 | final Layout layout = TextView.this.mLayout; |
| 5930 | if (layout == null) return false; |
| 5931 | |
| 5932 | // Update in case text is edited while the animation is run |
| 5933 | final int length = mText.length(); |
| 5934 | int start = Math.min(length, mStart); |
| 5935 | int end = Math.min(length, mEnd); |
| 5936 | |
| 5937 | mPath.reset(); |
| 5938 | TextView.this.mLayout.getSelectionPath(start, end, mPath); |
| 5939 | return true; |
| 5940 | } |
| 5941 | |
| 5942 | private void invalidate(boolean delayed) { |
| 5943 | if (TextView.this.mLayout == null) return; |
| 5944 | |
| 5945 | synchronized (sTempRect) { |
| 5946 | mPath.computeBounds(sTempRect, false); |
| 5947 | |
| 5948 | int left = getCompoundPaddingLeft(); |
| 5949 | int top = getExtendedPaddingTop() + getVerticalOffset(true); |
| 5950 | |
| 5951 | if (delayed) { |
| 5952 | TextView.this.postInvalidateDelayed(16, // 60 Hz update |
| 5953 | left + (int) sTempRect.left, top + (int) sTempRect.top, |
| 5954 | left + (int) sTempRect.right, top + (int) sTempRect.bottom); |
| 5955 | } else { |
| 5956 | TextView.this.postInvalidate((int) sTempRect.left, (int) sTempRect.top, |
| 5957 | (int) sTempRect.right, (int) sTempRect.bottom); |
| 5958 | } |
| 5959 | } |
| 5960 | } |
| 5961 | |
| 5962 | private void stopAnimation() { |
| 5963 | TextView.this.mCorrectionHighlighter = null; |
| 5964 | } |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 5965 | } |
| 5966 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5967 | public void beginBatchEdit() { |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 5968 | mInBatchEditControllers = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5969 | final InputMethodState ims = mInputMethodState; |
| 5970 | if (ims != null) { |
| 5971 | int nesting = ++ims.mBatchEditNesting; |
| 5972 | if (nesting == 1) { |
| 5973 | ims.mCursorChanged = false; |
| 5974 | ims.mChangedDelta = 0; |
| 5975 | if (ims.mContentChanged) { |
| 5976 | // We already have a pending change from somewhere else, |
| 5977 | // so turn this into a full update. |
| 5978 | ims.mChangedStart = 0; |
| 5979 | ims.mChangedEnd = mText.length(); |
| 5980 | } else { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5981 | ims.mChangedStart = EXTRACT_UNKNOWN; |
| 5982 | ims.mChangedEnd = EXTRACT_UNKNOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5983 | ims.mContentChanged = false; |
| 5984 | } |
| 5985 | onBeginBatchEdit(); |
| 5986 | } |
| 5987 | } |
| 5988 | } |
| 5989 | |
| 5990 | public void endBatchEdit() { |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 5991 | mInBatchEditControllers = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5992 | final InputMethodState ims = mInputMethodState; |
| 5993 | if (ims != null) { |
| 5994 | int nesting = --ims.mBatchEditNesting; |
| 5995 | if (nesting == 0) { |
| 5996 | finishBatchEdit(ims); |
| 5997 | } |
| 5998 | } |
| 5999 | } |
| 6000 | |
| 6001 | void ensureEndedBatchEdit() { |
| 6002 | final InputMethodState ims = mInputMethodState; |
| 6003 | if (ims != null && ims.mBatchEditNesting != 0) { |
| 6004 | ims.mBatchEditNesting = 0; |
| 6005 | finishBatchEdit(ims); |
| 6006 | } |
| 6007 | } |
| 6008 | |
| 6009 | void finishBatchEdit(final InputMethodState ims) { |
| 6010 | onEndBatchEdit(); |
| 6011 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 6012 | if (ims.mContentChanged || ims.mSelectionModeChanged) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6013 | updateAfterEdit(); |
| 6014 | reportExtractedText(); |
| 6015 | } else if (ims.mCursorChanged) { |
| 6016 | // Cheezy way to get us to report the current cursor location. |
| 6017 | invalidateCursor(); |
| 6018 | } |
| 6019 | } |
| 6020 | |
| 6021 | void updateAfterEdit() { |
| 6022 | invalidate(); |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 6023 | int curs = getSelectionStart(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6024 | |
Gilles Debunne | 3d01006 | 2011-02-18 14:16:41 -0800 | [diff] [blame] | 6025 | if (curs >= 0 || (mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6026 | registerForPreDraw(); |
| 6027 | } |
| 6028 | |
| 6029 | if (curs >= 0) { |
| 6030 | mHighlightPathBogus = true; |
Gilles Debunne | 3d01006 | 2011-02-18 14:16:41 -0800 | [diff] [blame] | 6031 | makeBlink(); |
Gilles Debunne | 8202cd3 | 2011-07-15 09:56:30 -0700 | [diff] [blame] | 6032 | bringPointIntoView(curs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6033 | } |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 6034 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6035 | checkForResize(); |
| 6036 | } |
| 6037 | |
| 6038 | /** |
| 6039 | * Called by the framework in response to a request to begin a batch |
| 6040 | * of edit operations through a call to link {@link #beginBatchEdit()}. |
| 6041 | */ |
| 6042 | public void onBeginBatchEdit() { |
Gilles Debunne | d40cc3c | 2011-03-11 16:59:32 -0800 | [diff] [blame] | 6043 | // intentionally empty |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6044 | } |
| 6045 | |
| 6046 | /** |
| 6047 | * Called by the framework in response to a request to end a batch |
| 6048 | * of edit operations through a call to link {@link #endBatchEdit}. |
| 6049 | */ |
| 6050 | public void onEndBatchEdit() { |
Gilles Debunne | d40cc3c | 2011-03-11 16:59:32 -0800 | [diff] [blame] | 6051 | // intentionally empty |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6052 | } |
| 6053 | |
| 6054 | /** |
| 6055 | * Called by the framework in response to a private command from the |
| 6056 | * current method, provided by it calling |
| 6057 | * {@link InputConnection#performPrivateCommand |
| 6058 | * InputConnection.performPrivateCommand()}. |
| 6059 | * |
| 6060 | * @param action The action name of the command. |
| 6061 | * @param data Any additional data for the command. This may be null. |
| 6062 | * @return Return true if you handled the command, else false. |
| 6063 | */ |
| 6064 | public boolean onPrivateIMECommand(String action, Bundle data) { |
| 6065 | return false; |
| 6066 | } |
| 6067 | |
| 6068 | private void nullLayouts() { |
| 6069 | if (mLayout instanceof BoringLayout && mSavedLayout == null) { |
| 6070 | mSavedLayout = (BoringLayout) mLayout; |
| 6071 | } |
| 6072 | if (mHintLayout instanceof BoringLayout && mSavedHintLayout == null) { |
| 6073 | mSavedHintLayout = (BoringLayout) mHintLayout; |
| 6074 | } |
| 6075 | |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6076 | mSavedMarqueeModeLayout = mLayout = mHintLayout = null; |
Gilles Debunne | 77f18b0 | 2010-10-22 14:28:25 -0700 | [diff] [blame] | 6077 | |
Fabrice Di Meglio | d4c3b8e | 2011-11-09 18:04:07 -0800 | [diff] [blame] | 6078 | mBoring = mHintBoring = null; |
| 6079 | |
Gilles Debunne | 77f18b0 | 2010-10-22 14:28:25 -0700 | [diff] [blame] | 6080 | // Since it depends on the value of mLayout |
| 6081 | prepareCursorControllers(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6082 | } |
| 6083 | |
| 6084 | /** |
| 6085 | * Make a new Layout based on the already-measured size of the view, |
| 6086 | * on the assumption that it was measured correctly at some point. |
| 6087 | */ |
| 6088 | private void assumeLayout() { |
| 6089 | int width = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); |
| 6090 | |
| 6091 | if (width < 1) { |
| 6092 | width = 0; |
| 6093 | } |
| 6094 | |
| 6095 | int physicalWidth = width; |
| 6096 | |
| 6097 | if (mHorizontallyScrolling) { |
Jeff Brown | 033a001 | 2011-11-11 15:30:16 -0800 | [diff] [blame] | 6098 | width = VERY_WIDE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6099 | } |
| 6100 | |
| 6101 | makeNewLayout(width, physicalWidth, UNKNOWN_BORING, UNKNOWN_BORING, |
| 6102 | physicalWidth, false); |
| 6103 | } |
| 6104 | |
Doug Felt | c0ccf0c | 2011-06-23 16:13:18 -0700 | [diff] [blame] | 6105 | @Override |
Fabrice Di Meglio | 7f86c80 | 2011-07-01 15:09:24 -0700 | [diff] [blame] | 6106 | protected void resetResolvedLayoutDirection() { |
| 6107 | super.resetResolvedLayoutDirection(); |
Doug Felt | c0ccf0c | 2011-06-23 16:13:18 -0700 | [diff] [blame] | 6108 | |
| 6109 | if (mLayoutAlignment != null && |
| 6110 | (mTextAlign == TextAlign.VIEW_START || |
| 6111 | mTextAlign == TextAlign.VIEW_END)) { |
| 6112 | mLayoutAlignment = null; |
| 6113 | } |
| 6114 | } |
| 6115 | |
| 6116 | private Layout.Alignment getLayoutAlignment() { |
| 6117 | if (mLayoutAlignment == null) { |
| 6118 | Layout.Alignment alignment; |
| 6119 | TextAlign textAlign = mTextAlign; |
| 6120 | switch (textAlign) { |
| 6121 | case INHERIT: |
| 6122 | // fall through to gravity temporarily |
| 6123 | // intention is to inherit value through view hierarchy. |
| 6124 | case GRAVITY: |
| 6125 | switch (mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) { |
| 6126 | case Gravity.START: |
| 6127 | alignment = Layout.Alignment.ALIGN_NORMAL; |
| 6128 | break; |
| 6129 | case Gravity.END: |
| 6130 | alignment = Layout.Alignment.ALIGN_OPPOSITE; |
| 6131 | break; |
| 6132 | case Gravity.LEFT: |
| 6133 | alignment = Layout.Alignment.ALIGN_LEFT; |
| 6134 | break; |
| 6135 | case Gravity.RIGHT: |
| 6136 | alignment = Layout.Alignment.ALIGN_RIGHT; |
| 6137 | break; |
| 6138 | case Gravity.CENTER_HORIZONTAL: |
| 6139 | alignment = Layout.Alignment.ALIGN_CENTER; |
| 6140 | break; |
| 6141 | default: |
| 6142 | alignment = Layout.Alignment.ALIGN_NORMAL; |
| 6143 | break; |
| 6144 | } |
| 6145 | break; |
| 6146 | case TEXT_START: |
| 6147 | alignment = Layout.Alignment.ALIGN_NORMAL; |
| 6148 | break; |
| 6149 | case TEXT_END: |
| 6150 | alignment = Layout.Alignment.ALIGN_OPPOSITE; |
| 6151 | break; |
| 6152 | case CENTER: |
| 6153 | alignment = Layout.Alignment.ALIGN_CENTER; |
| 6154 | break; |
| 6155 | case VIEW_START: |
| 6156 | alignment = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ? |
| 6157 | Layout.Alignment.ALIGN_RIGHT : Layout.Alignment.ALIGN_LEFT; |
| 6158 | break; |
| 6159 | case VIEW_END: |
| 6160 | alignment = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ? |
| 6161 | Layout.Alignment.ALIGN_LEFT : Layout.Alignment.ALIGN_RIGHT; |
| 6162 | break; |
| 6163 | default: |
| 6164 | alignment = Layout.Alignment.ALIGN_NORMAL; |
| 6165 | break; |
| 6166 | } |
| 6167 | mLayoutAlignment = alignment; |
| 6168 | } |
| 6169 | return mLayoutAlignment; |
| 6170 | } |
| 6171 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6172 | /** |
| 6173 | * The width passed in is now the desired layout width, |
| 6174 | * not the full view width with padding. |
| 6175 | * {@hide} |
| 6176 | */ |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6177 | protected void makeNewLayout(int wantWidth, int hintWidth, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6178 | BoringLayout.Metrics boring, |
| 6179 | BoringLayout.Metrics hintBoring, |
| 6180 | int ellipsisWidth, boolean bringIntoView) { |
| 6181 | stopMarquee(); |
| 6182 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 6183 | // Update "old" cached values |
| 6184 | mOldMaximum = mMaximum; |
| 6185 | mOldMaxMode = mMaxMode; |
| 6186 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6187 | mHighlightPathBogus = true; |
| 6188 | |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6189 | if (wantWidth < 0) { |
| 6190 | wantWidth = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6191 | } |
| 6192 | if (hintWidth < 0) { |
| 6193 | hintWidth = 0; |
| 6194 | } |
| 6195 | |
Doug Felt | c0ccf0c | 2011-06-23 16:13:18 -0700 | [diff] [blame] | 6196 | Layout.Alignment alignment = getLayoutAlignment(); |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6197 | boolean shouldEllipsize = mEllipsize != null && mInput == null; |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6198 | final boolean switchEllipsize = mEllipsize == TruncateAt.MARQUEE && |
| 6199 | mMarqueeFadeMode != MARQUEE_FADE_NORMAL; |
| 6200 | TruncateAt effectiveEllipsize = mEllipsize; |
| 6201 | if (mEllipsize == TruncateAt.MARQUEE && |
| 6202 | mMarqueeFadeMode == MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { |
Fabrice Di Meglio | cb33264 | 2011-09-23 19:08:04 -0700 | [diff] [blame] | 6203 | effectiveEllipsize = TruncateAt.END_SMALL; |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6204 | } |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6205 | |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 6206 | if (mTextDir == null) { |
| 6207 | resolveTextDirection(); |
| 6208 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6209 | |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6210 | mLayout = makeSingleLayout(wantWidth, boring, ellipsisWidth, alignment, shouldEllipsize, |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6211 | effectiveEllipsize, effectiveEllipsize == mEllipsize); |
| 6212 | if (switchEllipsize) { |
| 6213 | TruncateAt oppositeEllipsize = effectiveEllipsize == TruncateAt.MARQUEE ? |
| 6214 | TruncateAt.END : TruncateAt.MARQUEE; |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6215 | mSavedMarqueeModeLayout = makeSingleLayout(wantWidth, boring, ellipsisWidth, alignment, |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6216 | shouldEllipsize, oppositeEllipsize, effectiveEllipsize != mEllipsize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6217 | } |
| 6218 | |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6219 | shouldEllipsize = mEllipsize != null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6220 | mHintLayout = null; |
| 6221 | |
| 6222 | if (mHint != null) { |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6223 | if (shouldEllipsize) hintWidth = wantWidth; |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6224 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6225 | if (hintBoring == UNKNOWN_BORING) { |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 6226 | hintBoring = BoringLayout.isBoring(mHint, mTextPaint, mTextDir, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6227 | mHintBoring); |
| 6228 | if (hintBoring != null) { |
| 6229 | mHintBoring = hintBoring; |
| 6230 | } |
| 6231 | } |
| 6232 | |
| 6233 | if (hintBoring != null) { |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6234 | if (hintBoring.width <= hintWidth && |
| 6235 | (!shouldEllipsize || hintBoring.width <= ellipsisWidth)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6236 | if (mSavedHintLayout != null) { |
| 6237 | mHintLayout = mSavedHintLayout. |
| 6238 | replaceOrMake(mHint, mTextPaint, |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6239 | hintWidth, alignment, mSpacingMult, mSpacingAdd, |
| 6240 | hintBoring, mIncludePad); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6241 | } else { |
| 6242 | mHintLayout = BoringLayout.make(mHint, mTextPaint, |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6243 | hintWidth, alignment, mSpacingMult, mSpacingAdd, |
| 6244 | hintBoring, mIncludePad); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6245 | } |
| 6246 | |
| 6247 | mSavedHintLayout = (BoringLayout) mHintLayout; |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6248 | } else if (shouldEllipsize && hintBoring.width <= hintWidth) { |
| 6249 | if (mSavedHintLayout != null) { |
| 6250 | mHintLayout = mSavedHintLayout. |
| 6251 | replaceOrMake(mHint, mTextPaint, |
| 6252 | hintWidth, alignment, mSpacingMult, mSpacingAdd, |
| 6253 | hintBoring, mIncludePad, mEllipsize, |
| 6254 | ellipsisWidth); |
| 6255 | } else { |
| 6256 | mHintLayout = BoringLayout.make(mHint, mTextPaint, |
| 6257 | hintWidth, alignment, mSpacingMult, mSpacingAdd, |
| 6258 | hintBoring, mIncludePad, mEllipsize, |
| 6259 | ellipsisWidth); |
| 6260 | } |
| 6261 | } else if (shouldEllipsize) { |
| 6262 | mHintLayout = new StaticLayout(mHint, |
| 6263 | 0, mHint.length(), |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 6264 | mTextPaint, hintWidth, alignment, mTextDir, mSpacingMult, |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6265 | mSpacingAdd, mIncludePad, mEllipsize, |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 6266 | ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6267 | } else { |
| 6268 | mHintLayout = new StaticLayout(mHint, mTextPaint, |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 6269 | hintWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6270 | mIncludePad); |
| 6271 | } |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6272 | } else if (shouldEllipsize) { |
| 6273 | mHintLayout = new StaticLayout(mHint, |
| 6274 | 0, mHint.length(), |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 6275 | mTextPaint, hintWidth, alignment, mTextDir, mSpacingMult, |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6276 | mSpacingAdd, mIncludePad, mEllipsize, |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 6277 | ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6278 | } else { |
| 6279 | mHintLayout = new StaticLayout(mHint, mTextPaint, |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 6280 | hintWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6281 | mIncludePad); |
| 6282 | } |
| 6283 | } |
| 6284 | |
| 6285 | if (bringIntoView) { |
| 6286 | registerForPreDraw(); |
| 6287 | } |
| 6288 | |
| 6289 | if (mEllipsize == TextUtils.TruncateAt.MARQUEE) { |
Romain Guy | 939151f | 2009-04-08 14:22:40 -0700 | [diff] [blame] | 6290 | if (!compressText(ellipsisWidth)) { |
| 6291 | final int height = mLayoutParams.height; |
| 6292 | // If the size of the view does not depend on the size of the text, try to |
| 6293 | // start the marquee immediately |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6294 | if (height != LayoutParams.WRAP_CONTENT && height != LayoutParams.MATCH_PARENT) { |
Romain Guy | 939151f | 2009-04-08 14:22:40 -0700 | [diff] [blame] | 6295 | startMarquee(); |
| 6296 | } else { |
| 6297 | // Defer the start of the marquee until we know our width (see setFrame()) |
| 6298 | mRestartMarquee = true; |
| 6299 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6300 | } |
| 6301 | } |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 6302 | |
| 6303 | // CursorControllers need a non-null mLayout |
| 6304 | prepareCursorControllers(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6305 | } |
| 6306 | |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6307 | private Layout makeSingleLayout(int wantWidth, BoringLayout.Metrics boring, int ellipsisWidth, |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6308 | Layout.Alignment alignment, boolean shouldEllipsize, TruncateAt effectiveEllipsize, |
| 6309 | boolean useSaved) { |
| 6310 | Layout result = null; |
| 6311 | if (mText instanceof Spannable) { |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6312 | result = new DynamicLayout(mText, mTransformed, mTextPaint, wantWidth, |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6313 | alignment, mTextDir, mSpacingMult, |
| 6314 | mSpacingAdd, mIncludePad, mInput == null ? effectiveEllipsize : null, |
Fabrice Di Meglio | ad0b051 | 2011-10-04 17:21:26 -0700 | [diff] [blame] | 6315 | ellipsisWidth); |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6316 | } else { |
| 6317 | if (boring == UNKNOWN_BORING) { |
| 6318 | boring = BoringLayout.isBoring(mTransformed, mTextPaint, mTextDir, mBoring); |
| 6319 | if (boring != null) { |
| 6320 | mBoring = boring; |
| 6321 | } |
| 6322 | } |
| 6323 | |
| 6324 | if (boring != null) { |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6325 | if (boring.width <= wantWidth && |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6326 | (effectiveEllipsize == null || boring.width <= ellipsisWidth)) { |
| 6327 | if (useSaved && mSavedLayout != null) { |
| 6328 | result = mSavedLayout.replaceOrMake(mTransformed, mTextPaint, |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6329 | wantWidth, alignment, mSpacingMult, mSpacingAdd, |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6330 | boring, mIncludePad); |
| 6331 | } else { |
| 6332 | result = BoringLayout.make(mTransformed, mTextPaint, |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6333 | wantWidth, alignment, mSpacingMult, mSpacingAdd, |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6334 | boring, mIncludePad); |
| 6335 | } |
| 6336 | |
| 6337 | if (useSaved) { |
| 6338 | mSavedLayout = (BoringLayout) result; |
| 6339 | } |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6340 | } else if (shouldEllipsize && boring.width <= wantWidth) { |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6341 | if (useSaved && mSavedLayout != null) { |
| 6342 | result = mSavedLayout.replaceOrMake(mTransformed, mTextPaint, |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6343 | wantWidth, alignment, mSpacingMult, mSpacingAdd, |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6344 | boring, mIncludePad, effectiveEllipsize, |
| 6345 | ellipsisWidth); |
| 6346 | } else { |
| 6347 | result = BoringLayout.make(mTransformed, mTextPaint, |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6348 | wantWidth, alignment, mSpacingMult, mSpacingAdd, |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6349 | boring, mIncludePad, effectiveEllipsize, |
| 6350 | ellipsisWidth); |
| 6351 | } |
| 6352 | } else if (shouldEllipsize) { |
| 6353 | result = new StaticLayout(mTransformed, |
| 6354 | 0, mTransformed.length(), |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6355 | mTextPaint, wantWidth, alignment, mTextDir, mSpacingMult, |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6356 | mSpacingAdd, mIncludePad, effectiveEllipsize, |
| 6357 | ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE); |
| 6358 | } else { |
| 6359 | result = new StaticLayout(mTransformed, mTextPaint, |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6360 | wantWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6361 | mIncludePad); |
| 6362 | } |
| 6363 | } else if (shouldEllipsize) { |
| 6364 | result = new StaticLayout(mTransformed, |
| 6365 | 0, mTransformed.length(), |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6366 | mTextPaint, wantWidth, alignment, mTextDir, mSpacingMult, |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6367 | mSpacingAdd, mIncludePad, effectiveEllipsize, |
| 6368 | ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE); |
| 6369 | } else { |
| 6370 | result = new StaticLayout(mTransformed, mTextPaint, |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 6371 | wantWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 6372 | mIncludePad); |
| 6373 | } |
| 6374 | } |
| 6375 | return result; |
| 6376 | } |
| 6377 | |
Romain Guy | 939151f | 2009-04-08 14:22:40 -0700 | [diff] [blame] | 6378 | private boolean compressText(float width) { |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 6379 | if (isHardwareAccelerated()) return false; |
| 6380 | |
Romain Guy | 3373ed6 | 2009-05-04 14:13:32 -0700 | [diff] [blame] | 6381 | // Only compress the text if it hasn't been compressed by the previous pass |
| 6382 | if (width > 0.0f && mLayout != null && getLineCount() == 1 && !mUserSetTextScaleX && |
| 6383 | mTextPaint.getTextScaleX() == 1.0f) { |
Romain Guy | 939151f | 2009-04-08 14:22:40 -0700 | [diff] [blame] | 6384 | final float textWidth = mLayout.getLineWidth(0); |
Romain Guy | 3373ed6 | 2009-05-04 14:13:32 -0700 | [diff] [blame] | 6385 | final float overflow = (textWidth + 1.0f - width) / width; |
Romain Guy | 939151f | 2009-04-08 14:22:40 -0700 | [diff] [blame] | 6386 | if (overflow > 0.0f && overflow <= Marquee.MARQUEE_DELTA_MAX) { |
| 6387 | mTextPaint.setTextScaleX(1.0f - overflow - 0.005f); |
| 6388 | post(new Runnable() { |
| 6389 | public void run() { |
| 6390 | requestLayout(); |
| 6391 | } |
| 6392 | }); |
| 6393 | return true; |
| 6394 | } |
| 6395 | } |
| 6396 | |
| 6397 | return false; |
| 6398 | } |
| 6399 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6400 | private static int desired(Layout layout) { |
| 6401 | int n = layout.getLineCount(); |
| 6402 | CharSequence text = layout.getText(); |
| 6403 | float max = 0; |
| 6404 | |
| 6405 | // if any line was wrapped, we can't use it. |
| 6406 | // but it's ok for the last line not to have a newline |
| 6407 | |
| 6408 | for (int i = 0; i < n - 1; i++) { |
| 6409 | if (text.charAt(layout.getLineEnd(i) - 1) != '\n') |
| 6410 | return -1; |
| 6411 | } |
| 6412 | |
| 6413 | for (int i = 0; i < n; i++) { |
| 6414 | max = Math.max(max, layout.getLineWidth(i)); |
| 6415 | } |
| 6416 | |
| 6417 | return (int) FloatMath.ceil(max); |
| 6418 | } |
| 6419 | |
| 6420 | /** |
| 6421 | * Set whether the TextView includes extra top and bottom padding to make |
| 6422 | * room for accents that go above the normal ascent and descent. |
| 6423 | * The default is true. |
| 6424 | * |
| 6425 | * @attr ref android.R.styleable#TextView_includeFontPadding |
| 6426 | */ |
| 6427 | public void setIncludeFontPadding(boolean includepad) { |
Gilles Debunne | 2237829 | 2011-08-12 10:38:52 -0700 | [diff] [blame] | 6428 | if (mIncludePad != includepad) { |
| 6429 | mIncludePad = includepad; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6430 | |
Gilles Debunne | 2237829 | 2011-08-12 10:38:52 -0700 | [diff] [blame] | 6431 | if (mLayout != null) { |
| 6432 | nullLayouts(); |
| 6433 | requestLayout(); |
| 6434 | invalidate(); |
| 6435 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6436 | } |
| 6437 | } |
| 6438 | |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6439 | private static final BoringLayout.Metrics UNKNOWN_BORING = new BoringLayout.Metrics(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6440 | |
| 6441 | @Override |
| 6442 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 6443 | int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 6444 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 6445 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 6446 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
| 6447 | |
| 6448 | int width; |
| 6449 | int height; |
| 6450 | |
| 6451 | BoringLayout.Metrics boring = UNKNOWN_BORING; |
| 6452 | BoringLayout.Metrics hintBoring = UNKNOWN_BORING; |
| 6453 | |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 6454 | if (mTextDir == null) { |
| 6455 | resolveTextDirection(); |
| 6456 | } |
| 6457 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6458 | int des = -1; |
| 6459 | boolean fromexisting = false; |
| 6460 | |
| 6461 | if (widthMode == MeasureSpec.EXACTLY) { |
| 6462 | // Parent has told us how big to be. So be it. |
| 6463 | width = widthSize; |
| 6464 | } else { |
| 6465 | if (mLayout != null && mEllipsize == null) { |
| 6466 | des = desired(mLayout); |
| 6467 | } |
| 6468 | |
| 6469 | if (des < 0) { |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 6470 | boring = BoringLayout.isBoring(mTransformed, mTextPaint, mTextDir, mBoring); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6471 | if (boring != null) { |
| 6472 | mBoring = boring; |
| 6473 | } |
| 6474 | } else { |
| 6475 | fromexisting = true; |
| 6476 | } |
| 6477 | |
| 6478 | if (boring == null || boring == UNKNOWN_BORING) { |
| 6479 | if (des < 0) { |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6480 | des = (int) FloatMath.ceil(Layout.getDesiredWidth(mTransformed, mTextPaint)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6481 | } |
| 6482 | |
| 6483 | width = des; |
| 6484 | } else { |
| 6485 | width = boring.width; |
| 6486 | } |
| 6487 | |
| 6488 | final Drawables dr = mDrawables; |
| 6489 | if (dr != null) { |
| 6490 | width = Math.max(width, dr.mDrawableWidthTop); |
| 6491 | width = Math.max(width, dr.mDrawableWidthBottom); |
| 6492 | } |
| 6493 | |
| 6494 | if (mHint != null) { |
| 6495 | int hintDes = -1; |
| 6496 | int hintWidth; |
| 6497 | |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6498 | if (mHintLayout != null && mEllipsize == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6499 | hintDes = desired(mHintLayout); |
| 6500 | } |
| 6501 | |
| 6502 | if (hintDes < 0) { |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6503 | hintBoring = BoringLayout.isBoring(mHint, mTextPaint, mHintBoring); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6504 | if (hintBoring != null) { |
| 6505 | mHintBoring = hintBoring; |
| 6506 | } |
| 6507 | } |
| 6508 | |
| 6509 | if (hintBoring == null || hintBoring == UNKNOWN_BORING) { |
| 6510 | if (hintDes < 0) { |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6511 | hintDes = (int) FloatMath.ceil( |
| 6512 | Layout.getDesiredWidth(mHint, mTextPaint)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6513 | } |
| 6514 | |
| 6515 | hintWidth = hintDes; |
| 6516 | } else { |
| 6517 | hintWidth = hintBoring.width; |
| 6518 | } |
| 6519 | |
| 6520 | if (hintWidth > width) { |
| 6521 | width = hintWidth; |
| 6522 | } |
| 6523 | } |
| 6524 | |
| 6525 | width += getCompoundPaddingLeft() + getCompoundPaddingRight(); |
| 6526 | |
| 6527 | if (mMaxWidthMode == EMS) { |
| 6528 | width = Math.min(width, mMaxWidth * getLineHeight()); |
| 6529 | } else { |
| 6530 | width = Math.min(width, mMaxWidth); |
| 6531 | } |
| 6532 | |
| 6533 | if (mMinWidthMode == EMS) { |
| 6534 | width = Math.max(width, mMinWidth * getLineHeight()); |
| 6535 | } else { |
| 6536 | width = Math.max(width, mMinWidth); |
| 6537 | } |
| 6538 | |
| 6539 | // Check against our minimum width |
| 6540 | width = Math.max(width, getSuggestedMinimumWidth()); |
| 6541 | |
| 6542 | if (widthMode == MeasureSpec.AT_MOST) { |
| 6543 | width = Math.min(widthSize, width); |
| 6544 | } |
| 6545 | } |
| 6546 | |
| 6547 | int want = width - getCompoundPaddingLeft() - getCompoundPaddingRight(); |
| 6548 | int unpaddedWidth = want; |
Gilles Debunne | 9a80a65 | 2011-01-31 12:56:07 -0800 | [diff] [blame] | 6549 | |
Jeff Brown | 033a001 | 2011-11-11 15:30:16 -0800 | [diff] [blame] | 6550 | if (mHorizontallyScrolling) want = VERY_WIDE; |
Gilles Debunne | 9a80a65 | 2011-01-31 12:56:07 -0800 | [diff] [blame] | 6551 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6552 | int hintWant = want; |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 6553 | int hintWidth = (mHintLayout == null) ? hintWant : mHintLayout.getWidth(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6554 | |
| 6555 | if (mLayout == null) { |
| 6556 | makeNewLayout(want, hintWant, boring, hintBoring, |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6557 | width - getCompoundPaddingLeft() - getCompoundPaddingRight(), false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6558 | } else { |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 6559 | final boolean layoutChanged = (mLayout.getWidth() != want) || |
| 6560 | (hintWidth != hintWant) || |
| 6561 | (mLayout.getEllipsizedWidth() != |
| 6562 | width - getCompoundPaddingLeft() - getCompoundPaddingRight()); |
| 6563 | |
| 6564 | final boolean widthChanged = (mHint == null) && |
| 6565 | (mEllipsize == null) && |
| 6566 | (want > mLayout.getWidth()) && |
| 6567 | (mLayout instanceof BoringLayout || (fromexisting && des >= 0 && des <= want)); |
| 6568 | |
| 6569 | final boolean maximumChanged = (mMaxMode != mOldMaxMode) || (mMaximum != mOldMaximum); |
| 6570 | |
| 6571 | if (layoutChanged || maximumChanged) { |
| 6572 | if (!maximumChanged && widthChanged) { |
| 6573 | mLayout.increaseWidthTo(want); |
| 6574 | } else { |
| 6575 | makeNewLayout(want, hintWant, boring, hintBoring, |
| 6576 | width - getCompoundPaddingLeft() - getCompoundPaddingRight(), false); |
| 6577 | } |
| 6578 | } else { |
| 6579 | // Nothing has changed |
| 6580 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6581 | } |
| 6582 | |
| 6583 | if (heightMode == MeasureSpec.EXACTLY) { |
| 6584 | // Parent has told us how big to be. So be it. |
| 6585 | height = heightSize; |
| 6586 | mDesiredHeightAtMeasure = -1; |
| 6587 | } else { |
| 6588 | int desired = getDesiredHeight(); |
| 6589 | |
| 6590 | height = desired; |
| 6591 | mDesiredHeightAtMeasure = desired; |
| 6592 | |
| 6593 | if (heightMode == MeasureSpec.AT_MOST) { |
Christoffer Gurell | 1d05c7c | 2009-10-12 15:53:39 +0200 | [diff] [blame] | 6594 | height = Math.min(desired, heightSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6595 | } |
| 6596 | } |
| 6597 | |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6598 | int unpaddedHeight = height - getCompoundPaddingTop() - getCompoundPaddingBottom(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6599 | if (mMaxMode == LINES && mLayout.getLineCount() > mMaximum) { |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6600 | unpaddedHeight = Math.min(unpaddedHeight, mLayout.getLineTop(mMaximum)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6601 | } |
| 6602 | |
| 6603 | /* |
| 6604 | * We didn't let makeNewLayout() register to bring the cursor into view, |
| 6605 | * so do it here if there is any possibility that it is needed. |
| 6606 | */ |
| 6607 | if (mMovement != null || |
| 6608 | mLayout.getWidth() > unpaddedWidth || |
| 6609 | mLayout.getHeight() > unpaddedHeight) { |
| 6610 | registerForPreDraw(); |
| 6611 | } else { |
| 6612 | scrollTo(0, 0); |
| 6613 | } |
| 6614 | |
| 6615 | setMeasuredDimension(width, height); |
| 6616 | } |
| 6617 | |
| 6618 | private int getDesiredHeight() { |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 6619 | return Math.max( |
| 6620 | getDesiredHeight(mLayout, true), |
| 6621 | getDesiredHeight(mHintLayout, mEllipsize != null)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6622 | } |
| 6623 | |
| 6624 | private int getDesiredHeight(Layout layout, boolean cap) { |
| 6625 | if (layout == null) { |
| 6626 | return 0; |
| 6627 | } |
| 6628 | |
| 6629 | int linecount = layout.getLineCount(); |
| 6630 | int pad = getCompoundPaddingTop() + getCompoundPaddingBottom(); |
| 6631 | int desired = layout.getLineTop(linecount); |
| 6632 | |
| 6633 | final Drawables dr = mDrawables; |
| 6634 | if (dr != null) { |
| 6635 | desired = Math.max(desired, dr.mDrawableHeightLeft); |
| 6636 | desired = Math.max(desired, dr.mDrawableHeightRight); |
| 6637 | } |
| 6638 | |
| 6639 | desired += pad; |
| 6640 | |
| 6641 | if (mMaxMode == LINES) { |
| 6642 | /* |
| 6643 | * Don't cap the hint to a certain number of lines. |
| 6644 | * (Do cap it, though, if we have a maximum pixel height.) |
| 6645 | */ |
| 6646 | if (cap) { |
| 6647 | if (linecount > mMaximum) { |
Gilles Debunne | 0a4db3c | 2011-01-14 12:12:04 -0800 | [diff] [blame] | 6648 | desired = layout.getLineTop(mMaximum); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6649 | |
| 6650 | if (dr != null) { |
| 6651 | desired = Math.max(desired, dr.mDrawableHeightLeft); |
| 6652 | desired = Math.max(desired, dr.mDrawableHeightRight); |
| 6653 | } |
| 6654 | |
| 6655 | desired += pad; |
| 6656 | linecount = mMaximum; |
| 6657 | } |
| 6658 | } |
| 6659 | } else { |
| 6660 | desired = Math.min(desired, mMaximum); |
| 6661 | } |
| 6662 | |
| 6663 | if (mMinMode == LINES) { |
| 6664 | if (linecount < mMinimum) { |
| 6665 | desired += getLineHeight() * (mMinimum - linecount); |
| 6666 | } |
| 6667 | } else { |
| 6668 | desired = Math.max(desired, mMinimum); |
| 6669 | } |
| 6670 | |
| 6671 | // Check against our minimum height |
| 6672 | desired = Math.max(desired, getSuggestedMinimumHeight()); |
| 6673 | |
| 6674 | return desired; |
| 6675 | } |
| 6676 | |
| 6677 | /** |
| 6678 | * Check whether a change to the existing text layout requires a |
| 6679 | * new view layout. |
| 6680 | */ |
| 6681 | private void checkForResize() { |
| 6682 | boolean sizeChanged = false; |
| 6683 | |
| 6684 | if (mLayout != null) { |
| 6685 | // Check if our width changed |
| 6686 | if (mLayoutParams.width == LayoutParams.WRAP_CONTENT) { |
| 6687 | sizeChanged = true; |
| 6688 | invalidate(); |
| 6689 | } |
| 6690 | |
| 6691 | // Check if our height changed |
| 6692 | if (mLayoutParams.height == LayoutParams.WRAP_CONTENT) { |
| 6693 | int desiredHeight = getDesiredHeight(); |
| 6694 | |
| 6695 | if (desiredHeight != this.getHeight()) { |
| 6696 | sizeChanged = true; |
| 6697 | } |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6698 | } else if (mLayoutParams.height == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6699 | if (mDesiredHeightAtMeasure >= 0) { |
| 6700 | int desiredHeight = getDesiredHeight(); |
| 6701 | |
| 6702 | if (desiredHeight != mDesiredHeightAtMeasure) { |
| 6703 | sizeChanged = true; |
| 6704 | } |
| 6705 | } |
| 6706 | } |
| 6707 | } |
| 6708 | |
| 6709 | if (sizeChanged) { |
| 6710 | requestLayout(); |
| 6711 | // caller will have already invalidated |
| 6712 | } |
| 6713 | } |
| 6714 | |
| 6715 | /** |
| 6716 | * Check whether entirely new text requires a new view layout |
| 6717 | * or merely a new text layout. |
| 6718 | */ |
| 6719 | private void checkForRelayout() { |
| 6720 | // If we have a fixed width, we can just swap in a new text layout |
| 6721 | // if the text height stays the same or if the view height is fixed. |
| 6722 | |
| 6723 | if ((mLayoutParams.width != LayoutParams.WRAP_CONTENT || |
| 6724 | (mMaxWidthMode == mMinWidthMode && mMaxWidth == mMinWidth)) && |
| 6725 | (mHint == null || mHintLayout != null) && |
| 6726 | (mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight() > 0)) { |
| 6727 | // Static width, so try making a new text layout. |
| 6728 | |
| 6729 | int oldht = mLayout.getHeight(); |
| 6730 | int want = mLayout.getWidth(); |
| 6731 | int hintWant = mHintLayout == null ? 0 : mHintLayout.getWidth(); |
| 6732 | |
| 6733 | /* |
| 6734 | * No need to bring the text into view, since the size is not |
| 6735 | * changing (unless we do the requestLayout(), in which case it |
| 6736 | * will happen at measure). |
| 6737 | */ |
| 6738 | makeNewLayout(want, hintWant, UNKNOWN_BORING, UNKNOWN_BORING, |
Romain Guy | e1e0dc8 | 2009-11-03 17:21:04 -0800 | [diff] [blame] | 6739 | mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(), |
| 6740 | false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6741 | |
Romain Guy | e1e0dc8 | 2009-11-03 17:21:04 -0800 | [diff] [blame] | 6742 | if (mEllipsize != TextUtils.TruncateAt.MARQUEE) { |
| 6743 | // In a fixed-height view, so use our new text layout. |
| 6744 | if (mLayoutParams.height != LayoutParams.WRAP_CONTENT && |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6745 | mLayoutParams.height != LayoutParams.MATCH_PARENT) { |
Romain Guy | e1e0dc8 | 2009-11-03 17:21:04 -0800 | [diff] [blame] | 6746 | invalidate(); |
| 6747 | return; |
| 6748 | } |
| 6749 | |
| 6750 | // Dynamic height, but height has stayed the same, |
| 6751 | // so use our new text layout. |
| 6752 | if (mLayout.getHeight() == oldht && |
| 6753 | (mHintLayout == null || mHintLayout.getHeight() == oldht)) { |
| 6754 | invalidate(); |
| 6755 | return; |
| 6756 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6757 | } |
| 6758 | |
| 6759 | // We lose: the height has changed and we have a dynamic height. |
| 6760 | // Request a new view layout using our new text layout. |
| 6761 | requestLayout(); |
| 6762 | invalidate(); |
| 6763 | } else { |
| 6764 | // Dynamic width, so we have no choice but to request a new |
| 6765 | // view layout with a new text layout. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6766 | nullLayouts(); |
| 6767 | requestLayout(); |
| 6768 | invalidate(); |
| 6769 | } |
| 6770 | } |
| 6771 | |
| 6772 | /** |
| 6773 | * Returns true if anything changed. |
| 6774 | */ |
| 6775 | private boolean bringTextIntoView() { |
| 6776 | int line = 0; |
| 6777 | if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { |
| 6778 | line = mLayout.getLineCount() - 1; |
| 6779 | } |
| 6780 | |
| 6781 | Layout.Alignment a = mLayout.getParagraphAlignment(line); |
| 6782 | int dir = mLayout.getParagraphDirection(line); |
| 6783 | int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); |
| 6784 | int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom(); |
| 6785 | int ht = mLayout.getHeight(); |
| 6786 | |
| 6787 | int scrollx, scrolly; |
| 6788 | |
Doug Felt | 25b9f42 | 2011-07-11 13:48:37 -0700 | [diff] [blame] | 6789 | // Convert to left, center, or right alignment. |
| 6790 | if (a == Layout.Alignment.ALIGN_NORMAL) { |
| 6791 | a = dir == Layout.DIR_LEFT_TO_RIGHT ? Layout.Alignment.ALIGN_LEFT : |
| 6792 | Layout.Alignment.ALIGN_RIGHT; |
| 6793 | } else if (a == Layout.Alignment.ALIGN_OPPOSITE){ |
| 6794 | a = dir == Layout.DIR_LEFT_TO_RIGHT ? Layout.Alignment.ALIGN_RIGHT : |
| 6795 | Layout.Alignment.ALIGN_LEFT; |
| 6796 | } |
| 6797 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6798 | if (a == Layout.Alignment.ALIGN_CENTER) { |
| 6799 | /* |
| 6800 | * Keep centered if possible, or, if it is too wide to fit, |
| 6801 | * keep leading edge in view. |
| 6802 | */ |
| 6803 | |
| 6804 | int left = (int) FloatMath.floor(mLayout.getLineLeft(line)); |
| 6805 | int right = (int) FloatMath.ceil(mLayout.getLineRight(line)); |
| 6806 | |
| 6807 | if (right - left < hspace) { |
| 6808 | scrollx = (right + left) / 2 - hspace / 2; |
| 6809 | } else { |
| 6810 | if (dir < 0) { |
| 6811 | scrollx = right - hspace; |
| 6812 | } else { |
| 6813 | scrollx = left; |
| 6814 | } |
| 6815 | } |
Fabrice Di Meglio | d2b5d1c | 2011-07-13 19:38:17 -0700 | [diff] [blame] | 6816 | } else if (a == Layout.Alignment.ALIGN_RIGHT) { |
Doug Felt | 25b9f42 | 2011-07-11 13:48:37 -0700 | [diff] [blame] | 6817 | int right = (int) FloatMath.ceil(mLayout.getLineRight(line)); |
| 6818 | scrollx = right - hspace; |
Fabrice Di Meglio | d2b5d1c | 2011-07-13 19:38:17 -0700 | [diff] [blame] | 6819 | } else { // a == Layout.Alignment.ALIGN_LEFT (will also be the default) |
| 6820 | scrollx = (int) FloatMath.floor(mLayout.getLineLeft(line)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6821 | } |
| 6822 | |
| 6823 | if (ht < vspace) { |
| 6824 | scrolly = 0; |
| 6825 | } else { |
| 6826 | if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { |
| 6827 | scrolly = ht - vspace; |
| 6828 | } else { |
| 6829 | scrolly = 0; |
| 6830 | } |
| 6831 | } |
| 6832 | |
| 6833 | if (scrollx != mScrollX || scrolly != mScrollY) { |
| 6834 | scrollTo(scrollx, scrolly); |
| 6835 | return true; |
| 6836 | } else { |
| 6837 | return false; |
| 6838 | } |
| 6839 | } |
| 6840 | |
| 6841 | /** |
| 6842 | * Move the point, specified by the offset, into the view if it is needed. |
| 6843 | * This has to be called after layout. Returns true if anything changed. |
| 6844 | */ |
| 6845 | public boolean bringPointIntoView(int offset) { |
| 6846 | boolean changed = false; |
| 6847 | |
Gilles Debunne | 176ee3d | 2011-07-16 13:28:41 -0700 | [diff] [blame] | 6848 | if (mLayout == null) return changed; |
| 6849 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6850 | int line = mLayout.getLineForOffset(offset); |
| 6851 | |
| 6852 | // FIXME: Is it okay to truncate this, or should we round? |
| 6853 | final int x = (int)mLayout.getPrimaryHorizontal(offset); |
| 6854 | final int top = mLayout.getLineTop(line); |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 6855 | final int bottom = mLayout.getLineTop(line + 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6856 | |
| 6857 | int left = (int) FloatMath.floor(mLayout.getLineLeft(line)); |
| 6858 | int right = (int) FloatMath.ceil(mLayout.getLineRight(line)); |
| 6859 | int ht = mLayout.getHeight(); |
| 6860 | |
| 6861 | int grav; |
| 6862 | |
| 6863 | switch (mLayout.getParagraphAlignment(line)) { |
Doug Felt | 25b9f42 | 2011-07-11 13:48:37 -0700 | [diff] [blame] | 6864 | case ALIGN_LEFT: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6865 | grav = 1; |
| 6866 | break; |
Doug Felt | 25b9f42 | 2011-07-11 13:48:37 -0700 | [diff] [blame] | 6867 | case ALIGN_RIGHT: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6868 | grav = -1; |
| 6869 | break; |
Doug Felt | 25b9f42 | 2011-07-11 13:48:37 -0700 | [diff] [blame] | 6870 | case ALIGN_NORMAL: |
| 6871 | grav = mLayout.getParagraphDirection(line); |
| 6872 | break; |
| 6873 | case ALIGN_OPPOSITE: |
| 6874 | grav = -mLayout.getParagraphDirection(line); |
| 6875 | break; |
| 6876 | case ALIGN_CENTER: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6877 | default: |
| 6878 | grav = 0; |
Doug Felt | 25b9f42 | 2011-07-11 13:48:37 -0700 | [diff] [blame] | 6879 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6880 | } |
| 6881 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6882 | int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); |
| 6883 | int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom(); |
| 6884 | |
| 6885 | int hslack = (bottom - top) / 2; |
| 6886 | int vslack = hslack; |
| 6887 | |
| 6888 | if (vslack > vspace / 4) |
| 6889 | vslack = vspace / 4; |
| 6890 | if (hslack > hspace / 4) |
| 6891 | hslack = hspace / 4; |
| 6892 | |
| 6893 | int hs = mScrollX; |
| 6894 | int vs = mScrollY; |
| 6895 | |
| 6896 | if (top - vs < vslack) |
| 6897 | vs = top - vslack; |
| 6898 | if (bottom - vs > vspace - vslack) |
| 6899 | vs = bottom - (vspace - vslack); |
| 6900 | if (ht - vs < vspace) |
| 6901 | vs = ht - vspace; |
| 6902 | if (0 - vs > 0) |
| 6903 | vs = 0; |
| 6904 | |
| 6905 | if (grav != 0) { |
| 6906 | if (x - hs < hslack) { |
| 6907 | hs = x - hslack; |
| 6908 | } |
| 6909 | if (x - hs > hspace - hslack) { |
| 6910 | hs = x - (hspace - hslack); |
| 6911 | } |
| 6912 | } |
| 6913 | |
| 6914 | if (grav < 0) { |
| 6915 | if (left - hs > 0) |
| 6916 | hs = left; |
| 6917 | if (right - hs < hspace) |
| 6918 | hs = right - hspace; |
| 6919 | } else if (grav > 0) { |
| 6920 | if (right - hs < hspace) |
| 6921 | hs = right - hspace; |
| 6922 | if (left - hs > 0) |
| 6923 | hs = left; |
| 6924 | } else /* grav == 0 */ { |
| 6925 | if (right - left <= hspace) { |
| 6926 | /* |
| 6927 | * If the entire text fits, center it exactly. |
| 6928 | */ |
| 6929 | hs = left - (hspace - (right - left)) / 2; |
| 6930 | } else if (x > right - hslack) { |
| 6931 | /* |
| 6932 | * If we are near the right edge, keep the right edge |
| 6933 | * at the edge of the view. |
| 6934 | */ |
| 6935 | hs = right - hspace; |
| 6936 | } else if (x < left + hslack) { |
| 6937 | /* |
| 6938 | * If we are near the left edge, keep the left edge |
| 6939 | * at the edge of the view. |
| 6940 | */ |
| 6941 | hs = left; |
| 6942 | } else if (left > hs) { |
| 6943 | /* |
| 6944 | * Is there whitespace visible at the left? Fix it if so. |
| 6945 | */ |
| 6946 | hs = left; |
| 6947 | } else if (right < hs + hspace) { |
| 6948 | /* |
| 6949 | * Is there whitespace visible at the right? Fix it if so. |
| 6950 | */ |
| 6951 | hs = right - hspace; |
| 6952 | } else { |
| 6953 | /* |
| 6954 | * Otherwise, float as needed. |
| 6955 | */ |
| 6956 | if (x - hs < hslack) { |
| 6957 | hs = x - hslack; |
| 6958 | } |
| 6959 | if (x - hs > hspace - hslack) { |
| 6960 | hs = x - (hspace - hslack); |
| 6961 | } |
| 6962 | } |
| 6963 | } |
| 6964 | |
| 6965 | if (hs != mScrollX || vs != mScrollY) { |
| 6966 | if (mScroller == null) { |
| 6967 | scrollTo(hs, vs); |
| 6968 | } else { |
| 6969 | long duration = AnimationUtils.currentAnimationTimeMillis() - mLastScroll; |
| 6970 | int dx = hs - mScrollX; |
| 6971 | int dy = vs - mScrollY; |
| 6972 | |
| 6973 | if (duration > ANIMATED_SCROLL_GAP) { |
| 6974 | mScroller.startScroll(mScrollX, mScrollY, dx, dy); |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 6975 | awakenScrollBars(mScroller.getDuration()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6976 | invalidate(); |
| 6977 | } else { |
| 6978 | if (!mScroller.isFinished()) { |
| 6979 | mScroller.abortAnimation(); |
| 6980 | } |
| 6981 | |
| 6982 | scrollBy(dx, dy); |
| 6983 | } |
| 6984 | |
| 6985 | mLastScroll = AnimationUtils.currentAnimationTimeMillis(); |
| 6986 | } |
| 6987 | |
| 6988 | changed = true; |
| 6989 | } |
| 6990 | |
| 6991 | if (isFocused()) { |
Gilles Debunne | 716dbf6 | 2011-03-07 18:12:10 -0800 | [diff] [blame] | 6992 | // This offsets because getInterestingRect() is in terms of viewport coordinates, but |
| 6993 | // requestRectangleOnScreen() is in terms of content coordinates. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6994 | |
Gilles Debunne | 716dbf6 | 2011-03-07 18:12:10 -0800 | [diff] [blame] | 6995 | if (mTempRect == null) mTempRect = new Rect(); |
Dianne Hackborn | 70a3f67 | 2011-08-08 14:32:41 -0700 | [diff] [blame] | 6996 | // The offsets here are to ensure the rectangle we are using is |
| 6997 | // within our view bounds, in case the cursor is on the far left |
| 6998 | // or right. If it isn't withing the bounds, then this request |
| 6999 | // will be ignored. |
| 7000 | mTempRect.set(x - 2, top, x + 2, bottom); |
Gilles Debunne | 716dbf6 | 2011-03-07 18:12:10 -0800 | [diff] [blame] | 7001 | getInterestingRect(mTempRect, line); |
| 7002 | mTempRect.offset(mScrollX, mScrollY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7003 | |
Gilles Debunne | 716dbf6 | 2011-03-07 18:12:10 -0800 | [diff] [blame] | 7004 | if (requestRectangleOnScreen(mTempRect)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7005 | changed = true; |
| 7006 | } |
| 7007 | } |
| 7008 | |
| 7009 | return changed; |
| 7010 | } |
| 7011 | |
Dianne Hackborn | 38e98fc | 2009-03-24 18:18:24 -0700 | [diff] [blame] | 7012 | /** |
| 7013 | * Move the cursor, if needed, so that it is at an offset that is visible |
| 7014 | * to the user. This will not move the cursor if it represents more than |
| 7015 | * one character (a selection range). This will only work if the |
| 7016 | * TextView contains spannable text; otherwise it will do nothing. |
Gilles Debunne | bbb5d6e | 2010-06-21 16:21:51 -0700 | [diff] [blame] | 7017 | * |
Gilles Debunne | 57f4e5b | 2010-06-21 16:21:51 -0700 | [diff] [blame] | 7018 | * @return True if the cursor was actually moved, false otherwise. |
Dianne Hackborn | 38e98fc | 2009-03-24 18:18:24 -0700 | [diff] [blame] | 7019 | */ |
| 7020 | public boolean moveCursorToVisibleOffset() { |
| 7021 | if (!(mText instanceof Spannable)) { |
| 7022 | return false; |
| 7023 | } |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 7024 | int start = getSelectionStart(); |
| 7025 | int end = getSelectionEnd(); |
Dianne Hackborn | 38e98fc | 2009-03-24 18:18:24 -0700 | [diff] [blame] | 7026 | if (start != end) { |
| 7027 | return false; |
| 7028 | } |
| 7029 | |
| 7030 | // First: make sure the line is visible on screen: |
| 7031 | |
| 7032 | int line = mLayout.getLineForOffset(start); |
| 7033 | |
| 7034 | final int top = mLayout.getLineTop(line); |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 7035 | final int bottom = mLayout.getLineTop(line + 1); |
Dianne Hackborn | 38e98fc | 2009-03-24 18:18:24 -0700 | [diff] [blame] | 7036 | final int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom(); |
| 7037 | int vslack = (bottom - top) / 2; |
| 7038 | if (vslack > vspace / 4) |
| 7039 | vslack = vspace / 4; |
| 7040 | final int vs = mScrollY; |
| 7041 | |
| 7042 | if (top < (vs+vslack)) { |
| 7043 | line = mLayout.getLineForVertical(vs+vslack+(bottom-top)); |
| 7044 | } else if (bottom > (vspace+vs-vslack)) { |
| 7045 | line = mLayout.getLineForVertical(vspace+vs-vslack-(bottom-top)); |
| 7046 | } |
| 7047 | |
| 7048 | // Next: make sure the character is visible on screen: |
| 7049 | |
| 7050 | final int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); |
| 7051 | final int hs = mScrollX; |
| 7052 | final int leftChar = mLayout.getOffsetForHorizontal(line, hs); |
| 7053 | final int rightChar = mLayout.getOffsetForHorizontal(line, hspace+hs); |
| 7054 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 7055 | // line might contain bidirectional text |
| 7056 | final int lowChar = leftChar < rightChar ? leftChar : rightChar; |
| 7057 | final int highChar = leftChar > rightChar ? leftChar : rightChar; |
| 7058 | |
Dianne Hackborn | 38e98fc | 2009-03-24 18:18:24 -0700 | [diff] [blame] | 7059 | int newStart = start; |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 7060 | if (newStart < lowChar) { |
| 7061 | newStart = lowChar; |
| 7062 | } else if (newStart > highChar) { |
| 7063 | newStart = highChar; |
Dianne Hackborn | 38e98fc | 2009-03-24 18:18:24 -0700 | [diff] [blame] | 7064 | } |
| 7065 | |
| 7066 | if (newStart != start) { |
| 7067 | Selection.setSelection((Spannable)mText, newStart); |
| 7068 | return true; |
| 7069 | } |
| 7070 | |
| 7071 | return false; |
| 7072 | } |
| 7073 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7074 | @Override |
| 7075 | public void computeScroll() { |
| 7076 | if (mScroller != null) { |
| 7077 | if (mScroller.computeScrollOffset()) { |
| 7078 | mScrollX = mScroller.getCurrX(); |
| 7079 | mScrollY = mScroller.getCurrY(); |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7080 | invalidateParentCaches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7081 | postInvalidate(); // So we draw again |
| 7082 | } |
| 7083 | } |
| 7084 | } |
| 7085 | |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 7086 | private void getInterestingRect(Rect r, int line) { |
| 7087 | convertFromViewportToContentCoordinates(r); |
| 7088 | |
| 7089 | // Rectangle can can be expanded on first and last line to take |
| 7090 | // padding into account. |
| 7091 | // TODO Take left/right padding into account too? |
| 7092 | if (line == 0) r.top -= getExtendedPaddingTop(); |
| 7093 | if (line == mLayout.getLineCount() - 1) r.bottom += getExtendedPaddingBottom(); |
| 7094 | } |
| 7095 | |
| 7096 | private void convertFromViewportToContentCoordinates(Rect r) { |
Gilles Debunne | 44c1e4c | 2010-09-08 19:33:20 -0700 | [diff] [blame] | 7097 | final int horizontalOffset = viewportToContentHorizontalOffset(); |
| 7098 | r.left += horizontalOffset; |
| 7099 | r.right += horizontalOffset; |
| 7100 | |
| 7101 | final int verticalOffset = viewportToContentVerticalOffset(); |
| 7102 | r.top += verticalOffset; |
| 7103 | r.bottom += verticalOffset; |
| 7104 | } |
| 7105 | |
| 7106 | private int viewportToContentHorizontalOffset() { |
| 7107 | return getCompoundPaddingLeft() - mScrollX; |
| 7108 | } |
| 7109 | |
| 7110 | private int viewportToContentVerticalOffset() { |
| 7111 | int offset = getExtendedPaddingTop() - mScrollY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7112 | if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { |
Gilles Debunne | 44c1e4c | 2010-09-08 19:33:20 -0700 | [diff] [blame] | 7113 | offset += getVerticalOffset(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7114 | } |
Gilles Debunne | 44c1e4c | 2010-09-08 19:33:20 -0700 | [diff] [blame] | 7115 | return offset; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7116 | } |
| 7117 | |
| 7118 | @Override |
| 7119 | public void debug(int depth) { |
| 7120 | super.debug(depth); |
| 7121 | |
| 7122 | String output = debugIndent(depth); |
| 7123 | output += "frame={" + mLeft + ", " + mTop + ", " + mRight |
| 7124 | + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY |
| 7125 | + "} "; |
| 7126 | |
| 7127 | if (mText != null) { |
| 7128 | |
| 7129 | output += "mText=\"" + mText + "\" "; |
| 7130 | if (mLayout != null) { |
| 7131 | output += "mLayout width=" + mLayout.getWidth() |
| 7132 | + " height=" + mLayout.getHeight(); |
| 7133 | } |
| 7134 | } else { |
| 7135 | output += "mText=NULL"; |
| 7136 | } |
| 7137 | Log.d(VIEW_LOG_TAG, output); |
| 7138 | } |
| 7139 | |
| 7140 | /** |
| 7141 | * Convenience for {@link Selection#getSelectionStart}. |
| 7142 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 7143 | @ViewDebug.ExportedProperty(category = "text") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7144 | public int getSelectionStart() { |
| 7145 | return Selection.getSelectionStart(getText()); |
| 7146 | } |
| 7147 | |
| 7148 | /** |
| 7149 | * Convenience for {@link Selection#getSelectionEnd}. |
| 7150 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 7151 | @ViewDebug.ExportedProperty(category = "text") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7152 | public int getSelectionEnd() { |
| 7153 | return Selection.getSelectionEnd(getText()); |
| 7154 | } |
| 7155 | |
| 7156 | /** |
| 7157 | * Return true iff there is a selection inside this text view. |
| 7158 | */ |
| 7159 | public boolean hasSelection() { |
Gilles Debunne | 03789e8 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 7160 | final int selectionStart = getSelectionStart(); |
| 7161 | final int selectionEnd = getSelectionEnd(); |
| 7162 | |
| 7163 | return selectionStart >= 0 && selectionStart != selectionEnd; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7164 | } |
| 7165 | |
| 7166 | /** |
| 7167 | * Sets the properties of this field (lines, horizontally scrolling, |
| 7168 | * transformation method) to be for a single-line input. |
| 7169 | * |
| 7170 | * @attr ref android.R.styleable#TextView_singleLine |
| 7171 | */ |
| 7172 | public void setSingleLine() { |
| 7173 | setSingleLine(true); |
| 7174 | } |
| 7175 | |
| 7176 | /** |
Adam Powell | 7f8f79a | 2011-07-07 18:35:54 -0700 | [diff] [blame] | 7177 | * Sets the properties of this field to transform input to ALL CAPS |
| 7178 | * display. This may use a "small caps" formatting if available. |
| 7179 | * This setting will be ignored if this field is editable or selectable. |
| 7180 | * |
| 7181 | * This call replaces the current transformation method. Disabling this |
| 7182 | * will not necessarily restore the previous behavior from before this |
| 7183 | * was enabled. |
| 7184 | * |
| 7185 | * @see #setTransformationMethod(TransformationMethod) |
| 7186 | * @attr ref android.R.styleable#TextView_textAllCaps |
| 7187 | */ |
| 7188 | public void setAllCaps(boolean allCaps) { |
| 7189 | if (allCaps) { |
| 7190 | setTransformationMethod(new AllCapsTransformationMethod(getContext())); |
| 7191 | } else { |
| 7192 | setTransformationMethod(null); |
| 7193 | } |
| 7194 | } |
| 7195 | |
| 7196 | /** |
Gilles Debunne | a3ae4a0 | 2010-12-13 17:22:39 -0800 | [diff] [blame] | 7197 | * If true, sets the properties of this field (number of lines, horizontally scrolling, |
| 7198 | * transformation method) to be for a single-line input; if false, restores these to the default |
| 7199 | * conditions. |
| 7200 | * |
| 7201 | * Note that the default conditions are not necessarily those that were in effect prior this |
| 7202 | * method, and you may want to reset these properties to your custom values. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7203 | * |
| 7204 | * @attr ref android.R.styleable#TextView_singleLine |
| 7205 | */ |
| 7206 | @android.view.RemotableViewMethod |
| 7207 | public void setSingleLine(boolean singleLine) { |
Gilles Debunne | a3ae4a0 | 2010-12-13 17:22:39 -0800 | [diff] [blame] | 7208 | // Could be used, but may break backward compatibility. |
| 7209 | // if (mSingleLine == singleLine) return; |
Gilles Debunne | d7483bf | 2010-11-10 10:47:45 -0800 | [diff] [blame] | 7210 | setInputTypeSingleLine(singleLine); |
Gilles Debunne | a3ae4a0 | 2010-12-13 17:22:39 -0800 | [diff] [blame] | 7211 | applySingleLine(singleLine, true, true); |
Gilles Debunne | d7483bf | 2010-11-10 10:47:45 -0800 | [diff] [blame] | 7212 | } |
| 7213 | |
| 7214 | /** |
| 7215 | * Adds or remove the EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE on the mInputType. |
| 7216 | * @param singleLine |
| 7217 | */ |
| 7218 | private void setInputTypeSingleLine(boolean singleLine) { |
Gilles Debunne | 91a08cf | 2010-11-08 17:34:49 -0800 | [diff] [blame] | 7219 | if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7220 | if (singleLine) { |
| 7221 | mInputType &= ~EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; |
| 7222 | } else { |
| 7223 | mInputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; |
| 7224 | } |
| 7225 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7226 | } |
| 7227 | |
Gilles Debunne | a3ae4a0 | 2010-12-13 17:22:39 -0800 | [diff] [blame] | 7228 | private void applySingleLine(boolean singleLine, boolean applyTransformation, |
| 7229 | boolean changeMaxLines) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7230 | mSingleLine = singleLine; |
| 7231 | if (singleLine) { |
| 7232 | setLines(1); |
| 7233 | setHorizontallyScrolling(true); |
| 7234 | if (applyTransformation) { |
Gilles Debunne | 91a08cf | 2010-11-08 17:34:49 -0800 | [diff] [blame] | 7235 | setTransformationMethod(SingleLineTransformationMethod.getInstance()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7236 | } |
| 7237 | } else { |
Gilles Debunne | a3ae4a0 | 2010-12-13 17:22:39 -0800 | [diff] [blame] | 7238 | if (changeMaxLines) { |
| 7239 | setMaxLines(Integer.MAX_VALUE); |
| 7240 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7241 | setHorizontallyScrolling(false); |
| 7242 | if (applyTransformation) { |
| 7243 | setTransformationMethod(null); |
| 7244 | } |
| 7245 | } |
| 7246 | } |
Gilles Debunne | b231696 | 2010-12-21 17:32:43 -0800 | [diff] [blame] | 7247 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7248 | /** |
| 7249 | * Causes words in the text that are longer than the view is wide |
| 7250 | * to be ellipsized instead of broken in the middle. You may also |
| 7251 | * want to {@link #setSingleLine} or {@link #setHorizontallyScrolling} |
Kenny Root | e855d13 | 2009-06-11 11:00:42 -0500 | [diff] [blame] | 7252 | * to constrain the text to a single line. Use <code>null</code> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7253 | * to turn off ellipsizing. |
| 7254 | * |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 7255 | * If {@link #setMaxLines} has been used to set two or more lines, |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 7256 | * {@link android.text.TextUtils.TruncateAt#END} and |
| 7257 | * {@link android.text.TextUtils.TruncateAt#MARQUEE}* are only supported |
| 7258 | * (other ellipsizing types will not do anything). |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 7259 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7260 | * @attr ref android.R.styleable#TextView_ellipsize |
| 7261 | */ |
| 7262 | public void setEllipsize(TextUtils.TruncateAt where) { |
Gilles Debunne | 2237829 | 2011-08-12 10:38:52 -0700 | [diff] [blame] | 7263 | // TruncateAt is an enum. != comparison is ok between these singleton objects. |
| 7264 | if (mEllipsize != where) { |
| 7265 | mEllipsize = where; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7266 | |
Gilles Debunne | 2237829 | 2011-08-12 10:38:52 -0700 | [diff] [blame] | 7267 | if (mLayout != null) { |
| 7268 | nullLayouts(); |
| 7269 | requestLayout(); |
| 7270 | invalidate(); |
| 7271 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7272 | } |
| 7273 | } |
| 7274 | |
| 7275 | /** |
| 7276 | * Sets how many times to repeat the marquee animation. Only applied if the |
| 7277 | * TextView has marquee enabled. Set to -1 to repeat indefinitely. |
| 7278 | * |
| 7279 | * @attr ref android.R.styleable#TextView_marqueeRepeatLimit |
| 7280 | */ |
| 7281 | public void setMarqueeRepeatLimit(int marqueeLimit) { |
| 7282 | mMarqueeRepeatLimit = marqueeLimit; |
| 7283 | } |
| 7284 | |
| 7285 | /** |
| 7286 | * Returns where, if anywhere, words that are longer than the view |
| 7287 | * is wide should be ellipsized. |
| 7288 | */ |
| 7289 | @ViewDebug.ExportedProperty |
| 7290 | public TextUtils.TruncateAt getEllipsize() { |
| 7291 | return mEllipsize; |
| 7292 | } |
| 7293 | |
| 7294 | /** |
| 7295 | * Set the TextView so that when it takes focus, all the text is |
| 7296 | * selected. |
| 7297 | * |
| 7298 | * @attr ref android.R.styleable#TextView_selectAllOnFocus |
| 7299 | */ |
| 7300 | @android.view.RemotableViewMethod |
| 7301 | public void setSelectAllOnFocus(boolean selectAllOnFocus) { |
| 7302 | mSelectAllOnFocus = selectAllOnFocus; |
| 7303 | |
| 7304 | if (selectAllOnFocus && !(mText instanceof Spannable)) { |
| 7305 | setText(mText, BufferType.SPANNABLE); |
| 7306 | } |
| 7307 | } |
| 7308 | |
| 7309 | /** |
| 7310 | * Set whether the cursor is visible. The default is true. |
| 7311 | * |
| 7312 | * @attr ref android.R.styleable#TextView_cursorVisible |
| 7313 | */ |
| 7314 | @android.view.RemotableViewMethod |
| 7315 | public void setCursorVisible(boolean visible) { |
Gilles Debunne | 3d01006 | 2011-02-18 14:16:41 -0800 | [diff] [blame] | 7316 | if (mCursorVisible != visible) { |
| 7317 | mCursorVisible = visible; |
| 7318 | invalidate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7319 | |
Gilles Debunne | 3d01006 | 2011-02-18 14:16:41 -0800 | [diff] [blame] | 7320 | makeBlink(); |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 7321 | |
Gilles Debunne | 3d01006 | 2011-02-18 14:16:41 -0800 | [diff] [blame] | 7322 | // InsertionPointCursorController depends on mCursorVisible |
| 7323 | prepareCursorControllers(); |
| 7324 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7325 | } |
| 7326 | |
Gilles Debunne | 98dbfd4 | 2011-01-24 12:54:10 -0800 | [diff] [blame] | 7327 | private boolean isCursorVisible() { |
| 7328 | return mCursorVisible && isTextEditable(); |
| 7329 | } |
| 7330 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7331 | private boolean canMarquee() { |
| 7332 | int width = (mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight()); |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 7333 | return width > 0 && (mLayout.getLineWidth(0) > width || |
| 7334 | (mMarqueeFadeMode != MARQUEE_FADE_NORMAL && mSavedMarqueeModeLayout != null && |
| 7335 | mSavedMarqueeModeLayout.getLineWidth(0) > width)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7336 | } |
| 7337 | |
| 7338 | private void startMarquee() { |
Romain Guy | 4dc4f73 | 2009-06-19 15:16:40 -0700 | [diff] [blame] | 7339 | // Do not ellipsize EditText |
| 7340 | if (mInput != null) return; |
| 7341 | |
Romain Guy | 939151f | 2009-04-08 14:22:40 -0700 | [diff] [blame] | 7342 | if (compressText(getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight())) { |
| 7343 | return; |
| 7344 | } |
| 7345 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7346 | if ((mMarquee == null || mMarquee.isStopped()) && (isFocused() || isSelected()) && |
| 7347 | getLineCount() == 1 && canMarquee()) { |
Romain Guy | 939151f | 2009-04-08 14:22:40 -0700 | [diff] [blame] | 7348 | |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 7349 | if (mMarqueeFadeMode == MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { |
| 7350 | mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_FADE; |
| 7351 | final Layout tmp = mLayout; |
| 7352 | mLayout = mSavedMarqueeModeLayout; |
| 7353 | mSavedMarqueeModeLayout = tmp; |
| 7354 | setHorizontalFadingEdgeEnabled(true); |
| 7355 | requestLayout(); |
| 7356 | invalidate(); |
| 7357 | } |
| 7358 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7359 | if (mMarquee == null) mMarquee = new Marquee(this); |
| 7360 | mMarquee.start(mMarqueeRepeatLimit); |
| 7361 | } |
| 7362 | } |
| 7363 | |
| 7364 | private void stopMarquee() { |
| 7365 | if (mMarquee != null && !mMarquee.isStopped()) { |
| 7366 | mMarquee.stop(); |
| 7367 | } |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 7368 | |
| 7369 | if (mMarqueeFadeMode == MARQUEE_FADE_SWITCH_SHOW_FADE) { |
| 7370 | mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS; |
| 7371 | final Layout tmp = mSavedMarqueeModeLayout; |
| 7372 | mSavedMarqueeModeLayout = mLayout; |
| 7373 | mLayout = tmp; |
| 7374 | setHorizontalFadingEdgeEnabled(false); |
| 7375 | requestLayout(); |
| 7376 | invalidate(); |
| 7377 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7378 | } |
| 7379 | |
| 7380 | private void startStopMarquee(boolean start) { |
| 7381 | if (mEllipsize == TextUtils.TruncateAt.MARQUEE) { |
| 7382 | if (start) { |
| 7383 | startMarquee(); |
| 7384 | } else { |
| 7385 | stopMarquee(); |
| 7386 | } |
| 7387 | } |
| 7388 | } |
| 7389 | |
| 7390 | private static final class Marquee extends Handler { |
| 7391 | // TODO: Add an option to configure this |
Romain Guy | 939151f | 2009-04-08 14:22:40 -0700 | [diff] [blame] | 7392 | private static final float MARQUEE_DELTA_MAX = 0.07f; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7393 | private static final int MARQUEE_DELAY = 1200; |
| 7394 | private static final int MARQUEE_RESTART_DELAY = 1200; |
| 7395 | private static final int MARQUEE_RESOLUTION = 1000 / 30; |
| 7396 | private static final int MARQUEE_PIXELS_PER_SECOND = 30; |
| 7397 | |
| 7398 | private static final byte MARQUEE_STOPPED = 0x0; |
| 7399 | private static final byte MARQUEE_STARTING = 0x1; |
| 7400 | private static final byte MARQUEE_RUNNING = 0x2; |
| 7401 | |
| 7402 | private static final int MESSAGE_START = 0x1; |
| 7403 | private static final int MESSAGE_TICK = 0x2; |
| 7404 | private static final int MESSAGE_RESTART = 0x3; |
| 7405 | |
| 7406 | private final WeakReference<TextView> mView; |
| 7407 | |
| 7408 | private byte mStatus = MARQUEE_STOPPED; |
Gilles Debunne | e15b358 | 2010-06-16 15:17:21 -0700 | [diff] [blame] | 7409 | private final float mScrollUnit; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7410 | private float mMaxScroll; |
Romain Guy | c230319 | 2009-04-03 17:37:18 -0700 | [diff] [blame] | 7411 | float mMaxFadeScroll; |
| 7412 | private float mGhostStart; |
| 7413 | private float mGhostOffset; |
| 7414 | private float mFadeStop; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7415 | private int mRepeatLimit; |
| 7416 | |
| 7417 | float mScroll; |
| 7418 | |
| 7419 | Marquee(TextView v) { |
| 7420 | final float density = v.getContext().getResources().getDisplayMetrics().density; |
Gilles Debunne | e15b358 | 2010-06-16 15:17:21 -0700 | [diff] [blame] | 7421 | mScrollUnit = (MARQUEE_PIXELS_PER_SECOND * density) / MARQUEE_RESOLUTION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7422 | mView = new WeakReference<TextView>(v); |
| 7423 | } |
| 7424 | |
| 7425 | @Override |
| 7426 | public void handleMessage(Message msg) { |
| 7427 | switch (msg.what) { |
| 7428 | case MESSAGE_START: |
| 7429 | mStatus = MARQUEE_RUNNING; |
| 7430 | tick(); |
| 7431 | break; |
| 7432 | case MESSAGE_TICK: |
| 7433 | tick(); |
| 7434 | break; |
| 7435 | case MESSAGE_RESTART: |
| 7436 | if (mStatus == MARQUEE_RUNNING) { |
| 7437 | if (mRepeatLimit >= 0) { |
| 7438 | mRepeatLimit--; |
| 7439 | } |
| 7440 | start(mRepeatLimit); |
| 7441 | } |
| 7442 | break; |
| 7443 | } |
| 7444 | } |
| 7445 | |
| 7446 | void tick() { |
| 7447 | if (mStatus != MARQUEE_RUNNING) { |
| 7448 | return; |
| 7449 | } |
| 7450 | |
| 7451 | removeMessages(MESSAGE_TICK); |
| 7452 | |
| 7453 | final TextView textView = mView.get(); |
| 7454 | if (textView != null && (textView.isFocused() || textView.isSelected())) { |
| 7455 | mScroll += mScrollUnit; |
| 7456 | if (mScroll > mMaxScroll) { |
| 7457 | mScroll = mMaxScroll; |
| 7458 | sendEmptyMessageDelayed(MESSAGE_RESTART, MARQUEE_RESTART_DELAY); |
| 7459 | } else { |
| 7460 | sendEmptyMessageDelayed(MESSAGE_TICK, MARQUEE_RESOLUTION); |
| 7461 | } |
| 7462 | textView.invalidate(); |
| 7463 | } |
| 7464 | } |
| 7465 | |
| 7466 | void stop() { |
| 7467 | mStatus = MARQUEE_STOPPED; |
| 7468 | removeMessages(MESSAGE_START); |
| 7469 | removeMessages(MESSAGE_RESTART); |
| 7470 | removeMessages(MESSAGE_TICK); |
| 7471 | resetScroll(); |
| 7472 | } |
| 7473 | |
| 7474 | private void resetScroll() { |
| 7475 | mScroll = 0.0f; |
| 7476 | final TextView textView = mView.get(); |
| 7477 | if (textView != null) textView.invalidate(); |
| 7478 | } |
| 7479 | |
| 7480 | void start(int repeatLimit) { |
| 7481 | if (repeatLimit == 0) { |
| 7482 | stop(); |
| 7483 | return; |
| 7484 | } |
| 7485 | mRepeatLimit = repeatLimit; |
| 7486 | final TextView textView = mView.get(); |
| 7487 | if (textView != null && textView.mLayout != null) { |
| 7488 | mStatus = MARQUEE_STARTING; |
| 7489 | mScroll = 0.0f; |
Romain Guy | c230319 | 2009-04-03 17:37:18 -0700 | [diff] [blame] | 7490 | final int textWidth = textView.getWidth() - textView.getCompoundPaddingLeft() - |
| 7491 | textView.getCompoundPaddingRight(); |
| 7492 | final float lineWidth = textView.mLayout.getLineWidth(0); |
Romain Guy | 3373ed6 | 2009-05-04 14:13:32 -0700 | [diff] [blame] | 7493 | final float gap = textWidth / 3.0f; |
| 7494 | mGhostStart = lineWidth - textWidth + gap; |
Romain Guy | c230319 | 2009-04-03 17:37:18 -0700 | [diff] [blame] | 7495 | mMaxScroll = mGhostStart + textWidth; |
Romain Guy | 3373ed6 | 2009-05-04 14:13:32 -0700 | [diff] [blame] | 7496 | mGhostOffset = lineWidth + gap; |
Romain Guy | c230319 | 2009-04-03 17:37:18 -0700 | [diff] [blame] | 7497 | mFadeStop = lineWidth + textWidth / 6.0f; |
| 7498 | mMaxFadeScroll = mGhostStart + lineWidth + lineWidth; |
| 7499 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7500 | textView.invalidate(); |
| 7501 | sendEmptyMessageDelayed(MESSAGE_START, MARQUEE_DELAY); |
| 7502 | } |
| 7503 | } |
| 7504 | |
Romain Guy | c230319 | 2009-04-03 17:37:18 -0700 | [diff] [blame] | 7505 | float getGhostOffset() { |
| 7506 | return mGhostOffset; |
| 7507 | } |
| 7508 | |
| 7509 | boolean shouldDrawLeftFade() { |
| 7510 | return mScroll <= mFadeStop; |
| 7511 | } |
| 7512 | |
| 7513 | boolean shouldDrawGhost() { |
| 7514 | return mStatus == MARQUEE_RUNNING && mScroll > mGhostStart; |
| 7515 | } |
| 7516 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7517 | boolean isRunning() { |
| 7518 | return mStatus == MARQUEE_RUNNING; |
| 7519 | } |
| 7520 | |
| 7521 | boolean isStopped() { |
| 7522 | return mStatus == MARQUEE_STOPPED; |
| 7523 | } |
| 7524 | } |
| 7525 | |
| 7526 | /** |
Gilles Debunne | 4469e60 | 2011-03-09 14:38:04 -0800 | [diff] [blame] | 7527 | * This method is called when the text is changed, in case any subclasses |
| 7528 | * would like to know. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7529 | * |
Gilles Debunne | 4469e60 | 2011-03-09 14:38:04 -0800 | [diff] [blame] | 7530 | * Within <code>text</code>, the <code>lengthAfter</code> characters |
| 7531 | * beginning at <code>start</code> have just replaced old text that had |
| 7532 | * length <code>lengthBefore</code>. It is an error to attempt to make |
| 7533 | * changes to <code>text</code> from this callback. |
| 7534 | * |
| 7535 | * @param text The text the TextView is displaying |
| 7536 | * @param start The offset of the start of the range of the text that was |
| 7537 | * modified |
| 7538 | * @param lengthBefore The length of the former text that has been replaced |
| 7539 | * @param lengthAfter The length of the replacement modified text |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7540 | */ |
Gilles Debunne | 4469e60 | 2011-03-09 14:38:04 -0800 | [diff] [blame] | 7541 | protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 7542 | // intentionally empty, template pattern method can be overridden by subclasses |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7543 | } |
| 7544 | |
| 7545 | /** |
| 7546 | * This method is called when the selection has changed, in case any |
| 7547 | * subclasses would like to know. |
| 7548 | * |
| 7549 | * @param selStart The new selection start location. |
| 7550 | * @param selEnd The new selection end location. |
| 7551 | */ |
| 7552 | protected void onSelectionChanged(int selStart, int selEnd) { |
Svetoslav Ganov | a015617 | 2011-06-26 17:55:44 -0700 | [diff] [blame] | 7553 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7554 | } |
Svetoslav Ganov | a015617 | 2011-06-26 17:55:44 -0700 | [diff] [blame] | 7555 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7556 | /** |
| 7557 | * Adds a TextWatcher to the list of those whose methods are called |
| 7558 | * whenever this TextView's text changes. |
| 7559 | * <p> |
| 7560 | * In 1.0, the {@link TextWatcher#afterTextChanged} method was erroneously |
| 7561 | * not called after {@link #setText} calls. Now, doing {@link #setText} |
| 7562 | * if there are any text changed listeners forces the buffer type to |
| 7563 | * Editable if it would not otherwise be and does call this method. |
| 7564 | */ |
| 7565 | public void addTextChangedListener(TextWatcher watcher) { |
| 7566 | if (mListeners == null) { |
| 7567 | mListeners = new ArrayList<TextWatcher>(); |
| 7568 | } |
| 7569 | |
| 7570 | mListeners.add(watcher); |
| 7571 | } |
| 7572 | |
| 7573 | /** |
| 7574 | * Removes the specified TextWatcher from the list of those whose |
| 7575 | * methods are called |
| 7576 | * whenever this TextView's text changes. |
| 7577 | */ |
| 7578 | public void removeTextChangedListener(TextWatcher watcher) { |
| 7579 | if (mListeners != null) { |
| 7580 | int i = mListeners.indexOf(watcher); |
| 7581 | |
| 7582 | if (i >= 0) { |
| 7583 | mListeners.remove(i); |
| 7584 | } |
| 7585 | } |
| 7586 | } |
| 7587 | |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 7588 | private void sendBeforeTextChanged(CharSequence text, int start, int before, int after) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7589 | if (mListeners != null) { |
| 7590 | final ArrayList<TextWatcher> list = mListeners; |
| 7591 | final int count = list.size(); |
| 7592 | for (int i = 0; i < count; i++) { |
| 7593 | list.get(i).beforeTextChanged(text, start, before, after); |
| 7594 | } |
| 7595 | } |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 7596 | |
| 7597 | // The spans that are inside or intersect the modified region no longer make sense |
| 7598 | removeIntersectingSpans(start, start + before, SpellCheckSpan.class); |
| 7599 | removeIntersectingSpans(start, start + before, SuggestionSpan.class); |
| 7600 | } |
| 7601 | |
| 7602 | // Removes all spans that are inside or actually overlap the start..end range |
| 7603 | private <T> void removeIntersectingSpans(int start, int end, Class<T> type) { |
| 7604 | if (!(mText instanceof Editable)) return; |
| 7605 | Editable text = (Editable) mText; |
| 7606 | |
| 7607 | T[] spans = text.getSpans(start, end, type); |
| 7608 | final int length = spans.length; |
| 7609 | for (int i = 0; i < length; i++) { |
| 7610 | final int s = text.getSpanStart(spans[i]); |
| 7611 | final int e = text.getSpanEnd(spans[i]); |
Gilles Debunne | b062e81 | 2011-09-27 14:58:37 -0700 | [diff] [blame] | 7612 | // Spans that are adjacent to the edited region will be handled in |
| 7613 | // updateSpellCheckSpans. Result depends on what will be added (space or text) |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 7614 | if (e == start || s == end) break; |
| 7615 | text.removeSpan(spans[i]); |
| 7616 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7617 | } |
| 7618 | |
| 7619 | /** |
| 7620 | * Not private so it can be called from an inner class without going |
| 7621 | * through a thunk. |
| 7622 | */ |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 7623 | void sendOnTextChanged(CharSequence text, int start, int before, int after) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7624 | if (mListeners != null) { |
| 7625 | final ArrayList<TextWatcher> list = mListeners; |
| 7626 | final int count = list.size(); |
| 7627 | for (int i = 0; i < count; i++) { |
| 7628 | list.get(i).onTextChanged(text, start, before, after); |
| 7629 | } |
| 7630 | } |
Gilles Debunne | 1a22db2 | 2011-11-20 22:13:21 +0100 | [diff] [blame] | 7631 | |
Gilles Debunne | c115fa0 | 2011-12-07 13:38:31 -0800 | [diff] [blame] | 7632 | updateSpellCheckSpans(start, start + after, false); |
Gilles Debunne | 1a22db2 | 2011-11-20 22:13:21 +0100 | [diff] [blame] | 7633 | |
| 7634 | // Hide the controllers as soon as text is modified (typing, procedural...) |
| 7635 | // We do not hide the span controllers, since they can be added when a new text is |
| 7636 | // inserted into the text view (voice IME). |
| 7637 | hideCursorControllers(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7638 | } |
| 7639 | |
| 7640 | /** |
| 7641 | * Not private so it can be called from an inner class without going |
| 7642 | * through a thunk. |
| 7643 | */ |
| 7644 | void sendAfterTextChanged(Editable text) { |
| 7645 | if (mListeners != null) { |
| 7646 | final ArrayList<TextWatcher> list = mListeners; |
| 7647 | final int count = list.size(); |
| 7648 | for (int i = 0; i < count; i++) { |
| 7649 | list.get(i).afterTextChanged(text); |
| 7650 | } |
| 7651 | } |
| 7652 | } |
| 7653 | |
| 7654 | /** |
| 7655 | * Not private so it can be called from an inner class without going |
| 7656 | * through a thunk. |
| 7657 | */ |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 7658 | void handleTextChanged(CharSequence buffer, int start, int before, int after) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7659 | final InputMethodState ims = mInputMethodState; |
| 7660 | if (ims == null || ims.mBatchEditNesting == 0) { |
| 7661 | updateAfterEdit(); |
| 7662 | } |
| 7663 | if (ims != null) { |
| 7664 | ims.mContentChanged = true; |
| 7665 | if (ims.mChangedStart < 0) { |
| 7666 | ims.mChangedStart = start; |
| 7667 | ims.mChangedEnd = start+before; |
| 7668 | } else { |
Viktor Yakovel | 964be41 | 2010-02-17 08:35:57 +0100 | [diff] [blame] | 7669 | ims.mChangedStart = Math.min(ims.mChangedStart, start); |
| 7670 | ims.mChangedEnd = Math.max(ims.mChangedEnd, start + before - ims.mChangedDelta); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7671 | } |
| 7672 | ims.mChangedDelta += after-before; |
| 7673 | } |
Gilles Debunne | 186aaf9 | 2011-09-16 14:26:12 -0700 | [diff] [blame] | 7674 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7675 | sendOnTextChanged(buffer, start, before, after); |
| 7676 | onTextChanged(buffer, start, before, after); |
| 7677 | } |
| 7678 | |
| 7679 | /** |
| 7680 | * Not private so it can be called from an inner class without going |
| 7681 | * through a thunk. |
| 7682 | */ |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 7683 | void spanChange(Spanned buf, Object what, int oldStart, int newStart, int oldEnd, int newEnd) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7684 | // XXX Make the start and end move together if this ends up |
| 7685 | // spending too much time invalidating. |
| 7686 | |
| 7687 | boolean selChanged = false; |
| 7688 | int newSelStart=-1, newSelEnd=-1; |
| 7689 | |
| 7690 | final InputMethodState ims = mInputMethodState; |
| 7691 | |
| 7692 | if (what == Selection.SELECTION_END) { |
| 7693 | mHighlightPathBogus = true; |
| 7694 | selChanged = true; |
| 7695 | newSelEnd = newStart; |
| 7696 | |
| 7697 | if (!isFocused()) { |
| 7698 | mSelectionMoved = true; |
| 7699 | } |
| 7700 | |
| 7701 | if (oldStart >= 0 || newStart >= 0) { |
| 7702 | invalidateCursor(Selection.getSelectionStart(buf), oldStart, newStart); |
| 7703 | registerForPreDraw(); |
Gilles Debunne | 3d01006 | 2011-02-18 14:16:41 -0800 | [diff] [blame] | 7704 | makeBlink(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7705 | } |
| 7706 | } |
| 7707 | |
| 7708 | if (what == Selection.SELECTION_START) { |
| 7709 | mHighlightPathBogus = true; |
| 7710 | selChanged = true; |
| 7711 | newSelStart = newStart; |
| 7712 | |
| 7713 | if (!isFocused()) { |
| 7714 | mSelectionMoved = true; |
| 7715 | } |
| 7716 | |
| 7717 | if (oldStart >= 0 || newStart >= 0) { |
| 7718 | int end = Selection.getSelectionEnd(buf); |
| 7719 | invalidateCursor(end, oldStart, newStart); |
| 7720 | } |
| 7721 | } |
| 7722 | |
| 7723 | if (selChanged) { |
| 7724 | if ((buf.getSpanFlags(what)&Spanned.SPAN_INTERMEDIATE) == 0) { |
| 7725 | if (newSelStart < 0) { |
| 7726 | newSelStart = Selection.getSelectionStart(buf); |
| 7727 | } |
| 7728 | if (newSelEnd < 0) { |
| 7729 | newSelEnd = Selection.getSelectionEnd(buf); |
| 7730 | } |
| 7731 | onSelectionChanged(newSelStart, newSelEnd); |
| 7732 | } |
| 7733 | } |
Gilles Debunne | 8615ac9 | 2011-11-29 15:25:03 -0800 | [diff] [blame] | 7734 | |
| 7735 | if (what instanceof UpdateAppearance || what instanceof ParagraphStyle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7736 | if (ims == null || ims.mBatchEditNesting == 0) { |
| 7737 | invalidate(); |
| 7738 | mHighlightPathBogus = true; |
| 7739 | checkForResize(); |
| 7740 | } else { |
| 7741 | ims.mContentChanged = true; |
| 7742 | } |
| 7743 | } |
| 7744 | |
| 7745 | if (MetaKeyKeyListener.isMetaTracker(buf, what)) { |
| 7746 | mHighlightPathBogus = true; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 7747 | if (ims != null && MetaKeyKeyListener.isSelectingMetaTracker(buf, what)) { |
| 7748 | ims.mSelectionModeChanged = true; |
| 7749 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7750 | |
| 7751 | if (Selection.getSelectionStart(buf) >= 0) { |
| 7752 | if (ims == null || ims.mBatchEditNesting == 0) { |
| 7753 | invalidateCursor(); |
| 7754 | } else { |
| 7755 | ims.mCursorChanged = true; |
| 7756 | } |
| 7757 | } |
| 7758 | } |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 7759 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7760 | if (what instanceof ParcelableSpan) { |
| 7761 | // If this is a span that can be sent to a remote process, |
| 7762 | // the current extract editor would be interested in it. |
| 7763 | if (ims != null && ims.mExtracting != null) { |
| 7764 | if (ims.mBatchEditNesting != 0) { |
| 7765 | if (oldStart >= 0) { |
| 7766 | if (ims.mChangedStart > oldStart) { |
| 7767 | ims.mChangedStart = oldStart; |
| 7768 | } |
| 7769 | if (ims.mChangedStart > oldEnd) { |
| 7770 | ims.mChangedStart = oldEnd; |
| 7771 | } |
| 7772 | } |
| 7773 | if (newStart >= 0) { |
| 7774 | if (ims.mChangedStart > newStart) { |
| 7775 | ims.mChangedStart = newStart; |
| 7776 | } |
| 7777 | if (ims.mChangedStart > newEnd) { |
| 7778 | ims.mChangedStart = newEnd; |
| 7779 | } |
| 7780 | } |
| 7781 | } else { |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 7782 | if (DEBUG_EXTRACT) Log.v(LOG_TAG, "Span change outside of batch: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7783 | + oldStart + "-" + oldEnd + "," |
| 7784 | + newStart + "-" + newEnd + what); |
| 7785 | ims.mContentChanged = true; |
| 7786 | } |
| 7787 | } |
| 7788 | } |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 7789 | |
Gilles Debunne | c115fa0 | 2011-12-07 13:38:31 -0800 | [diff] [blame] | 7790 | if (mSpellChecker != null && newStart < 0 && what instanceof SpellCheckSpan) { |
| 7791 | mSpellChecker.removeSpellCheckSpan((SpellCheckSpan) what); |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 7792 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7793 | } |
| 7794 | |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 7795 | /** |
| 7796 | * Create new SpellCheckSpans on the modified region. |
| 7797 | */ |
Gilles Debunne | c115fa0 | 2011-12-07 13:38:31 -0800 | [diff] [blame] | 7798 | private void updateSpellCheckSpans(int start, int end, boolean createSpellChecker) { |
Gilles Debunne | 287d6c6 | 2011-10-05 18:22:11 -0700 | [diff] [blame] | 7799 | if (isTextEditable() && isSuggestionsEnabled()) { |
Gilles Debunne | c115fa0 | 2011-12-07 13:38:31 -0800 | [diff] [blame] | 7800 | if (mSpellChecker == null && createSpellChecker) { |
| 7801 | mSpellChecker = new SpellChecker(this); |
| 7802 | } |
| 7803 | if (mSpellChecker != null) { |
| 7804 | mSpellChecker.spellCheck(start, end); |
| 7805 | } |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 7806 | } |
| 7807 | } |
| 7808 | |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7809 | /** |
| 7810 | * Controls the {@link EasyEditSpan} monitoring when it is added, and when the related |
| 7811 | * pop-up should be displayed. |
| 7812 | */ |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7813 | private class EasyEditSpanController { |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7814 | |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7815 | private static final int DISPLAY_TIMEOUT_MS = 3000; // 3 secs |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7816 | |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7817 | private EasyEditPopupWindow mPopupWindow; |
| 7818 | |
| 7819 | private EasyEditSpan mEasyEditSpan; |
| 7820 | |
| 7821 | private Runnable mHidePopup; |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7822 | |
| 7823 | private void hide() { |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7824 | if (mPopupWindow != null) { |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7825 | mPopupWindow.hide(); |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7826 | TextView.this.removeCallbacks(mHidePopup); |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7827 | } |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7828 | removeSpans(mText); |
| 7829 | mEasyEditSpan = null; |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7830 | } |
| 7831 | |
| 7832 | /** |
| 7833 | * Monitors the changes in the text. |
| 7834 | * |
| 7835 | * <p>{@link ChangeWatcher#onSpanAdded(Spannable, Object, int, int)} cannot be used, |
| 7836 | * as the notifications are not sent when a spannable (with spans) is inserted. |
| 7837 | */ |
| 7838 | public void onTextChange(CharSequence buffer) { |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7839 | adjustSpans(mText); |
| 7840 | |
| 7841 | if (getWindowVisibility() != View.VISIBLE) { |
| 7842 | // The window is not visible yet, ignore the text change. |
| 7843 | return; |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7844 | } |
| 7845 | |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7846 | if (mLayout == null) { |
| 7847 | // The view has not been layout yet, ignore the text change |
| 7848 | return; |
| 7849 | } |
| 7850 | |
| 7851 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 7852 | if (!(TextView.this instanceof ExtractEditText) |
| 7853 | && imm != null && imm.isFullscreenMode()) { |
| 7854 | // The input is in extract mode. We do not have to handle the easy edit in the |
| 7855 | // original TextView, as the ExtractEditText will do |
| 7856 | return; |
| 7857 | } |
| 7858 | |
| 7859 | // Remove the current easy edit span, as the text changed, and remove the pop-up |
| 7860 | // (if any) |
| 7861 | if (mEasyEditSpan != null) { |
| 7862 | if (mText instanceof Spannable) { |
| 7863 | ((Spannable) mText).removeSpan(mEasyEditSpan); |
| 7864 | } |
| 7865 | mEasyEditSpan = null; |
| 7866 | } |
| 7867 | if (mPopupWindow != null && mPopupWindow.isShowing()) { |
| 7868 | mPopupWindow.hide(); |
| 7869 | } |
| 7870 | |
| 7871 | // Display the new easy edit span (if any). |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7872 | if (buffer instanceof Spanned) { |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7873 | mEasyEditSpan = getSpan((Spanned) buffer); |
| 7874 | if (mEasyEditSpan != null) { |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7875 | if (mPopupWindow == null) { |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7876 | mPopupWindow = new EasyEditPopupWindow(); |
| 7877 | mHidePopup = new Runnable() { |
| 7878 | @Override |
| 7879 | public void run() { |
| 7880 | hide(); |
| 7881 | } |
| 7882 | }; |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7883 | } |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7884 | mPopupWindow.show(mEasyEditSpan); |
| 7885 | TextView.this.removeCallbacks(mHidePopup); |
| 7886 | TextView.this.postDelayed(mHidePopup, DISPLAY_TIMEOUT_MS); |
| 7887 | } |
| 7888 | } |
| 7889 | } |
| 7890 | |
| 7891 | /** |
| 7892 | * Adjusts the spans by removing all of them except the last one. |
| 7893 | */ |
| 7894 | private void adjustSpans(CharSequence buffer) { |
| 7895 | // This method enforces that only one easy edit span is attached to the text. |
| 7896 | // A better way to enforce this would be to listen for onSpanAdded, but this method |
| 7897 | // cannot be used in this scenario as no notification is triggered when a text with |
| 7898 | // spans is inserted into a text. |
| 7899 | if (buffer instanceof Spannable) { |
| 7900 | Spannable spannable = (Spannable) buffer; |
| 7901 | EasyEditSpan[] spans = spannable.getSpans(0, spannable.length(), |
| 7902 | EasyEditSpan.class); |
| 7903 | for (int i = 0; i < spans.length - 1; i++) { |
| 7904 | spannable.removeSpan(spans[i]); |
| 7905 | } |
| 7906 | } |
| 7907 | } |
| 7908 | |
| 7909 | /** |
| 7910 | * Removes all the {@link EasyEditSpan} currently attached. |
| 7911 | */ |
| 7912 | private void removeSpans(CharSequence buffer) { |
| 7913 | if (buffer instanceof Spannable) { |
| 7914 | Spannable spannable = (Spannable) buffer; |
| 7915 | EasyEditSpan[] spans = spannable.getSpans(0, spannable.length(), |
| 7916 | EasyEditSpan.class); |
| 7917 | for (int i = 0; i < spans.length; i++) { |
| 7918 | spannable.removeSpan(spans[i]); |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7919 | } |
| 7920 | } |
| 7921 | } |
| 7922 | |
| 7923 | private EasyEditSpan getSpan(Spanned spanned) { |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7924 | EasyEditSpan[] easyEditSpans = spanned.getSpans(0, spanned.length(), |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7925 | EasyEditSpan.class); |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7926 | if (easyEditSpans.length == 0) { |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7927 | return null; |
| 7928 | } else { |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7929 | return easyEditSpans[0]; |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7930 | } |
| 7931 | } |
| 7932 | } |
| 7933 | |
| 7934 | /** |
| 7935 | * Displays the actions associated to an {@link EasyEditSpan}. The pop-up is controlled |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7936 | * by {@link EasyEditSpanController}. |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7937 | */ |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7938 | private class EasyEditPopupWindow extends PinnedPopupWindow |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7939 | implements OnClickListener { |
| 7940 | private static final int POPUP_TEXT_LAYOUT = |
| 7941 | com.android.internal.R.layout.text_edit_action_popup_text; |
| 7942 | private TextView mDeleteTextView; |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7943 | private EasyEditSpan mEasyEditSpan; |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7944 | |
| 7945 | @Override |
| 7946 | protected void createPopupWindow() { |
| 7947 | mPopupWindow = new PopupWindow(TextView.this.mContext, null, |
| 7948 | com.android.internal.R.attr.textSelectHandleWindowStyle); |
| 7949 | mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); |
| 7950 | mPopupWindow.setClippingEnabled(true); |
| 7951 | } |
| 7952 | |
| 7953 | @Override |
| 7954 | protected void initContentView() { |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 7955 | LinearLayout linearLayout = new LinearLayout(TextView.this.getContext()); |
| 7956 | linearLayout.setOrientation(LinearLayout.HORIZONTAL); |
| 7957 | mContentView = linearLayout; |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7958 | mContentView.setBackgroundResource( |
| 7959 | com.android.internal.R.drawable.text_edit_side_paste_window); |
| 7960 | |
| 7961 | LayoutInflater inflater = (LayoutInflater)TextView.this.mContext. |
| 7962 | getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 7963 | |
| 7964 | LayoutParams wrapContent = new LayoutParams( |
| 7965 | ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
| 7966 | |
| 7967 | mDeleteTextView = (TextView) inflater.inflate(POPUP_TEXT_LAYOUT, null); |
| 7968 | mDeleteTextView.setLayoutParams(wrapContent); |
| 7969 | mDeleteTextView.setText(com.android.internal.R.string.delete); |
| 7970 | mDeleteTextView.setOnClickListener(this); |
| 7971 | mContentView.addView(mDeleteTextView); |
| 7972 | } |
| 7973 | |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7974 | public void show(EasyEditSpan easyEditSpan) { |
| 7975 | mEasyEditSpan = easyEditSpan; |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7976 | super.show(); |
| 7977 | } |
| 7978 | |
| 7979 | @Override |
| 7980 | public void onClick(View view) { |
| 7981 | if (view == mDeleteTextView) { |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 7982 | Editable editable = (Editable) mText; |
| 7983 | int start = editable.getSpanStart(mEasyEditSpan); |
| 7984 | int end = editable.getSpanEnd(mEasyEditSpan); |
| 7985 | if (start >= 0 && end >= 0) { |
| 7986 | deleteText_internal(start, end); |
| 7987 | } |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7988 | } |
| 7989 | } |
| 7990 | |
| 7991 | @Override |
| 7992 | protected int getTextOffset() { |
| 7993 | // Place the pop-up at the end of the span |
| 7994 | Editable editable = (Editable) mText; |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 7995 | return editable.getSpanEnd(mEasyEditSpan); |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 7996 | } |
| 7997 | |
| 7998 | @Override |
| 7999 | protected int getVerticalLocalPosition(int line) { |
| 8000 | return mLayout.getLineBottom(line); |
| 8001 | } |
| 8002 | |
| 8003 | @Override |
| 8004 | protected int clipVertically(int positionY) { |
| 8005 | // As we display the pop-up below the span, no vertical clipping is required. |
| 8006 | return positionY; |
| 8007 | } |
| 8008 | } |
| 8009 | |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 8010 | private class ChangeWatcher implements TextWatcher, SpanWatcher { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 8011 | |
| 8012 | private CharSequence mBeforeText; |
| 8013 | |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 8014 | private EasyEditSpanController mEasyEditSpanController; |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 8015 | |
| 8016 | private ChangeWatcher() { |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 8017 | mEasyEditSpanController = new EasyEditSpanController(); |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 8018 | } |
| 8019 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8020 | public void beforeTextChanged(CharSequence buffer, int start, |
| 8021 | int before, int after) { |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 8022 | if (DEBUG_EXTRACT) Log.v(LOG_TAG, "beforeTextChanged start=" + start |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8023 | + " before=" + before + " after=" + after + ": " + buffer); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 8024 | |
Amith Yamasani | 91ccdb5 | 2010-01-14 18:56:02 -0800 | [diff] [blame] | 8025 | if (AccessibilityManager.getInstance(mContext).isEnabled() |
Svetoslav Ganov | 1d1e110 | 2010-11-16 16:44:03 -0800 | [diff] [blame] | 8026 | && !isPasswordInputType(mInputType) |
| 8027 | && !hasPasswordTransformationMethod()) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 8028 | mBeforeText = buffer.toString(); |
| 8029 | } |
| 8030 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8031 | TextView.this.sendBeforeTextChanged(buffer, start, before, after); |
| 8032 | } |
| 8033 | |
| 8034 | public void onTextChanged(CharSequence buffer, int start, |
| 8035 | int before, int after) { |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 8036 | if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onTextChanged start=" + start |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8037 | + " before=" + before + " after=" + after + ": " + buffer); |
| 8038 | TextView.this.handleTextChanged(buffer, start, before, after); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 8039 | |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 8040 | mEasyEditSpanController.onTextChange(buffer); |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 8041 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 8042 | if (AccessibilityManager.getInstance(mContext).isEnabled() && |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 8043 | (isFocused() || isSelected() && isShown())) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 8044 | sendAccessibilityEventTypeViewTextChanged(mBeforeText, start, before, after); |
| 8045 | mBeforeText = null; |
| 8046 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8047 | } |
| 8048 | |
| 8049 | public void afterTextChanged(Editable buffer) { |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 8050 | if (DEBUG_EXTRACT) Log.v(LOG_TAG, "afterTextChanged: " + buffer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8051 | TextView.this.sendAfterTextChanged(buffer); |
| 8052 | |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 8053 | if (MetaKeyKeyListener.getMetaState(buffer, MetaKeyKeyListener.META_SELECTING) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8054 | MetaKeyKeyListener.stopSelecting(TextView.this, buffer); |
| 8055 | } |
| 8056 | } |
| 8057 | |
| 8058 | public void onSpanChanged(Spannable buf, |
| 8059 | Object what, int s, int e, int st, int en) { |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 8060 | if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanChanged s=" + s + " e=" + e |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8061 | + " st=" + st + " en=" + en + " what=" + what + ": " + buf); |
| 8062 | TextView.this.spanChange(buf, what, s, st, e, en); |
| 8063 | } |
| 8064 | |
| 8065 | public void onSpanAdded(Spannable buf, Object what, int s, int e) { |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 8066 | if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanAdded s=" + s + " e=" + e |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8067 | + " what=" + what + ": " + buf); |
| 8068 | TextView.this.spanChange(buf, what, -1, s, -1, e); |
| 8069 | } |
| 8070 | |
| 8071 | public void onSpanRemoved(Spannable buf, Object what, int s, int e) { |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 8072 | if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanRemoved s=" + s + " e=" + e |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8073 | + " what=" + what + ": " + buf); |
| 8074 | TextView.this.spanChange(buf, what, s, -1, e, -1); |
| 8075 | } |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 8076 | |
| 8077 | private void hideControllers() { |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 8078 | mEasyEditSpanController.hide(); |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 8079 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8080 | } |
| 8081 | |
Romain Guy | dcc490f | 2010-02-24 17:59:35 -0800 | [diff] [blame] | 8082 | /** |
| 8083 | * @hide |
| 8084 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8085 | @Override |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 8086 | public void dispatchFinishTemporaryDetach() { |
| 8087 | mDispatchTemporaryDetach = true; |
| 8088 | super.dispatchFinishTemporaryDetach(); |
| 8089 | mDispatchTemporaryDetach = false; |
| 8090 | } |
| 8091 | |
| 8092 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8093 | public void onStartTemporaryDetach() { |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 8094 | super.onStartTemporaryDetach(); |
| 8095 | // Only track when onStartTemporaryDetach() is called directly, |
| 8096 | // usually because this instance is an editable field in a list |
| 8097 | if (!mDispatchTemporaryDetach) mTemporaryDetach = true; |
Gilles Debunne | 4b2274f | 2011-02-25 15:18:03 -0800 | [diff] [blame] | 8098 | |
| 8099 | // Because of View recycling in ListView, there is no easy way to know when a TextView with |
| 8100 | // selection becomes visible again. Until a better solution is found, stop text selection |
| 8101 | // mode (if any) as soon as this TextView is recycled. |
Gilles Debunne | 4a7199a | 2011-07-11 14:58:27 -0700 | [diff] [blame] | 8102 | hideControllers(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8103 | } |
Gilles Debunne | 3784a7f | 2011-07-15 13:49:38 -0700 | [diff] [blame] | 8104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8105 | @Override |
| 8106 | public void onFinishTemporaryDetach() { |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 8107 | super.onFinishTemporaryDetach(); |
| 8108 | // Only track when onStartTemporaryDetach() is called directly, |
| 8109 | // usually because this instance is an editable field in a list |
| 8110 | if (!mDispatchTemporaryDetach) mTemporaryDetach = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8111 | } |
Gilles Debunne | 3784a7f | 2011-07-15 13:49:38 -0700 | [diff] [blame] | 8112 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8113 | @Override |
| 8114 | protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { |
| 8115 | if (mTemporaryDetach) { |
| 8116 | // If we are temporarily in the detach state, then do nothing. |
| 8117 | super.onFocusChanged(focused, direction, previouslyFocusedRect); |
| 8118 | return; |
| 8119 | } |
| 8120 | |
| 8121 | mShowCursor = SystemClock.uptimeMillis(); |
| 8122 | |
| 8123 | ensureEndedBatchEdit(); |
Gilles Debunne | 03789e8 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 8124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8125 | if (focused) { |
| 8126 | int selStart = getSelectionStart(); |
| 8127 | int selEnd = getSelectionEnd(); |
| 8128 | |
Gilles Debunne | c01f3fe | 2010-12-22 17:07:36 -0800 | [diff] [blame] | 8129 | // SelectAllOnFocus fields are highlighted and not selected. Do not start text selection |
| 8130 | // mode for these, unless there was a specific selection already started. |
| 8131 | final boolean isFocusHighlighted = mSelectAllOnFocus && selStart == 0 && |
| 8132 | selEnd == mText.length(); |
| 8133 | mCreatedWithASelection = mFrozenWithFocus && hasSelection() && !isFocusHighlighted; |
| 8134 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8135 | if (!mFrozenWithFocus || (selStart < 0 || selEnd < 0)) { |
Gilles Debunne | 528c6488 | 2010-10-08 11:56:13 -0700 | [diff] [blame] | 8136 | // If a tap was used to give focus to that view, move cursor at tap position. |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 8137 | // Has to be done before onTakeFocus, which can be overloaded. |
Gilles Debunne | 380b604 | 2010-10-08 16:12:11 -0700 | [diff] [blame] | 8138 | final int lastTapPosition = getLastTapPosition(); |
| 8139 | if (lastTapPosition >= 0) { |
| 8140 | Selection.setSelection((Spannable) mText, lastTapPosition); |
| 8141 | } |
Gilles Debunne | 2703a42 | 2010-08-23 15:14:03 -0700 | [diff] [blame] | 8142 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8143 | if (mMovement != null) { |
| 8144 | mMovement.onTakeFocus(this, (Spannable) mText, direction); |
| 8145 | } |
| 8146 | |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 8147 | // The DecorView does not have focus when the 'Done' ExtractEditText button is |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 8148 | // pressed. Since it is the ViewAncestor's mView, it requests focus before |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 8149 | // ExtractEditText clears focus, which gives focus to the ExtractEditText. |
| 8150 | // This special case ensure that we keep current selection in that case. |
| 8151 | // It would be better to know why the DecorView does not have focus at that time. |
Gilles Debunne | 47fa8e8 | 2010-09-07 18:50:07 -0700 | [diff] [blame] | 8152 | if (((this instanceof ExtractEditText) || mSelectionMoved) && |
| 8153 | selStart >= 0 && selEnd >= 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8154 | /* |
| 8155 | * Someone intentionally set the selection, so let them |
| 8156 | * do whatever it is that they wanted to do instead of |
| 8157 | * the default on-focus behavior. We reset the selection |
| 8158 | * here instead of just skipping the onTakeFocus() call |
| 8159 | * because some movement methods do something other than |
| 8160 | * just setting the selection in theirs and we still |
| 8161 | * need to go through that path. |
| 8162 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8163 | Selection.setSelection((Spannable) mText, selStart, selEnd); |
| 8164 | } |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 8165 | |
| 8166 | if (mSelectAllOnFocus) { |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 8167 | selectAll(); |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 8168 | } |
| 8169 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 8170 | mTouchFocusSelected = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8171 | } |
| 8172 | |
| 8173 | mFrozenWithFocus = false; |
| 8174 | mSelectionMoved = false; |
| 8175 | |
| 8176 | if (mText instanceof Spannable) { |
| 8177 | Spannable sp = (Spannable) mText; |
| 8178 | MetaKeyKeyListener.resetMetaState(sp); |
| 8179 | } |
| 8180 | |
| 8181 | makeBlink(); |
| 8182 | |
| 8183 | if (mError != null) { |
| 8184 | showError(); |
| 8185 | } |
| 8186 | } else { |
| 8187 | if (mError != null) { |
| 8188 | hideError(); |
| 8189 | } |
| 8190 | // Don't leave us in the middle of a batch edit. |
| 8191 | onEndBatchEdit(); |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 8192 | |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 8193 | if (this instanceof ExtractEditText) { |
| 8194 | // terminateTextSelectionMode removes selection, which we want to keep when |
| 8195 | // ExtractEditText goes out of focus. |
| 8196 | final int selStart = getSelectionStart(); |
| 8197 | final int selEnd = getSelectionEnd(); |
Gilles Debunne | b7012e84 | 2011-02-24 15:40:38 -0800 | [diff] [blame] | 8198 | hideControllers(); |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 8199 | Selection.setSelection((Spannable) mText, selStart, selEnd); |
| 8200 | } else { |
Gilles Debunne | b7012e84 | 2011-02-24 15:40:38 -0800 | [diff] [blame] | 8201 | hideControllers(); |
Luca Zanolin | fe5e983 | 2011-09-02 19:41:42 +0100 | [diff] [blame] | 8202 | downgradeEasyCorrectionSpans(); |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 8203 | } |
Gilles Debunne | 380b604 | 2010-10-08 16:12:11 -0700 | [diff] [blame] | 8204 | |
Gilles Debunne | e587d83 | 2010-11-23 20:20:11 -0800 | [diff] [blame] | 8205 | // No need to create the controller |
Gilles Debunne | 380b604 | 2010-10-08 16:12:11 -0700 | [diff] [blame] | 8206 | if (mSelectionModifierCursorController != null) { |
Gilles Debunne | e587d83 | 2010-11-23 20:20:11 -0800 | [diff] [blame] | 8207 | mSelectionModifierCursorController.resetTouchOffsets(); |
Gilles Debunne | 380b604 | 2010-10-08 16:12:11 -0700 | [diff] [blame] | 8208 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8209 | } |
| 8210 | |
| 8211 | startStopMarquee(focused); |
| 8212 | |
| 8213 | if (mTransformation != null) { |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 8214 | mTransformation.onFocusChanged(this, mText, focused, direction, previouslyFocusedRect); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8215 | } |
| 8216 | |
| 8217 | super.onFocusChanged(focused, direction, previouslyFocusedRect); |
| 8218 | } |
| 8219 | |
Gilles Debunne | 380b604 | 2010-10-08 16:12:11 -0700 | [diff] [blame] | 8220 | private int getLastTapPosition() { |
Gilles Debunne | e587d83 | 2010-11-23 20:20:11 -0800 | [diff] [blame] | 8221 | // No need to create the controller at that point, no last tap position saved |
Gilles Debunne | 528c6488 | 2010-10-08 11:56:13 -0700 | [diff] [blame] | 8222 | if (mSelectionModifierCursorController != null) { |
Gilles Debunne | e587d83 | 2010-11-23 20:20:11 -0800 | [diff] [blame] | 8223 | int lastTapPosition = mSelectionModifierCursorController.getMinTouchOffset(); |
Gilles Debunne | 380b604 | 2010-10-08 16:12:11 -0700 | [diff] [blame] | 8224 | if (lastTapPosition >= 0) { |
Gilles Debunne | 528c6488 | 2010-10-08 11:56:13 -0700 | [diff] [blame] | 8225 | // Safety check, should not be possible. |
Gilles Debunne | 380b604 | 2010-10-08 16:12:11 -0700 | [diff] [blame] | 8226 | if (lastTapPosition > mText.length()) { |
| 8227 | Log.e(LOG_TAG, "Invalid tap focus position (" + lastTapPosition + " vs " |
Gilles Debunne | 528c6488 | 2010-10-08 11:56:13 -0700 | [diff] [blame] | 8228 | + mText.length() + ")"); |
Gilles Debunne | 380b604 | 2010-10-08 16:12:11 -0700 | [diff] [blame] | 8229 | lastTapPosition = mText.length(); |
Gilles Debunne | 528c6488 | 2010-10-08 11:56:13 -0700 | [diff] [blame] | 8230 | } |
Gilles Debunne | 380b604 | 2010-10-08 16:12:11 -0700 | [diff] [blame] | 8231 | return lastTapPosition; |
Gilles Debunne | 528c6488 | 2010-10-08 11:56:13 -0700 | [diff] [blame] | 8232 | } |
| 8233 | } |
Gilles Debunne | 380b604 | 2010-10-08 16:12:11 -0700 | [diff] [blame] | 8234 | |
| 8235 | return -1; |
Gilles Debunne | 528c6488 | 2010-10-08 11:56:13 -0700 | [diff] [blame] | 8236 | } |
| 8237 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8238 | @Override |
| 8239 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 8240 | super.onWindowFocusChanged(hasWindowFocus); |
| 8241 | |
| 8242 | if (hasWindowFocus) { |
| 8243 | if (mBlink != null) { |
| 8244 | mBlink.uncancel(); |
Gilles Debunne | 3d01006 | 2011-02-18 14:16:41 -0800 | [diff] [blame] | 8245 | makeBlink(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8246 | } |
| 8247 | } else { |
| 8248 | if (mBlink != null) { |
| 8249 | mBlink.cancel(); |
| 8250 | } |
| 8251 | // Don't leave us in the middle of a batch edit. |
| 8252 | onEndBatchEdit(); |
| 8253 | if (mInputContentType != null) { |
| 8254 | mInputContentType.enterDown = false; |
| 8255 | } |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 8256 | |
Gilles Debunne | e507a9e | 2010-10-10 12:44:18 -0700 | [diff] [blame] | 8257 | hideControllers(); |
Gilles Debunne | 26c8b3a | 2011-10-12 14:06:58 -0700 | [diff] [blame] | 8258 | if (mSuggestionsPopupWindow != null) { |
| 8259 | mSuggestionsPopupWindow.onParentLostFocus(); |
| 8260 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8261 | } |
| 8262 | |
| 8263 | startStopMarquee(hasWindowFocus); |
| 8264 | } |
| 8265 | |
Adam Powell | ba0a2c3 | 2010-09-28 17:41:23 -0700 | [diff] [blame] | 8266 | @Override |
| 8267 | protected void onVisibilityChanged(View changedView, int visibility) { |
| 8268 | super.onVisibilityChanged(changedView, visibility); |
| 8269 | if (visibility != VISIBLE) { |
Gilles Debunne | e507a9e | 2010-10-10 12:44:18 -0700 | [diff] [blame] | 8270 | hideControllers(); |
Adam Powell | ba0a2c3 | 2010-09-28 17:41:23 -0700 | [diff] [blame] | 8271 | } |
| 8272 | } |
| 8273 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8274 | /** |
| 8275 | * Use {@link BaseInputConnection#removeComposingSpans |
| 8276 | * BaseInputConnection.removeComposingSpans()} to remove any IME composing |
| 8277 | * state from this text view. |
| 8278 | */ |
| 8279 | public void clearComposingText() { |
| 8280 | if (mText instanceof Spannable) { |
| 8281 | BaseInputConnection.removeComposingSpans((Spannable)mText); |
| 8282 | } |
| 8283 | } |
| 8284 | |
| 8285 | @Override |
| 8286 | public void setSelected(boolean selected) { |
| 8287 | boolean wasSelected = isSelected(); |
| 8288 | |
| 8289 | super.setSelected(selected); |
| 8290 | |
| 8291 | if (selected != wasSelected && mEllipsize == TextUtils.TruncateAt.MARQUEE) { |
| 8292 | if (selected) { |
| 8293 | startMarquee(); |
| 8294 | } else { |
| 8295 | stopMarquee(); |
| 8296 | } |
| 8297 | } |
| 8298 | } |
| 8299 | |
| 8300 | @Override |
| 8301 | public boolean onTouchEvent(MotionEvent event) { |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 8302 | final int action = event.getActionMasked(); |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 8303 | |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 8304 | if (hasSelectionController()) { |
| 8305 | getSelectionController().onTouchEvent(event); |
Gilles Debunne | 5347c58 | 2010-10-27 14:22:35 -0700 | [diff] [blame] | 8306 | } |
Adam Powell | b08013c | 2010-09-16 16:28:11 -0700 | [diff] [blame] | 8307 | |
Gilles Debunne | 5347c58 | 2010-10-27 14:22:35 -0700 | [diff] [blame] | 8308 | if (action == MotionEvent.ACTION_DOWN) { |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 8309 | mLastDownPositionX = event.getX(); |
| 8310 | mLastDownPositionY = event.getY(); |
Gilles Debunne | 9948ad7 | 2010-11-24 14:00:46 -0800 | [diff] [blame] | 8311 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 8312 | // Reset this state; it will be re-set if super.onTouchEvent |
| 8313 | // causes focus to move to the view. |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 8314 | mTouchFocusSelected = false; |
Gilles Debunne | 0eb704c | 2010-11-30 12:50:54 -0800 | [diff] [blame] | 8315 | mIgnoreActionUpEvent = false; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 8316 | } |
Gilles Debunne | bbb5d6e | 2010-06-21 16:21:51 -0700 | [diff] [blame] | 8317 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8318 | final boolean superResult = super.onTouchEvent(event); |
| 8319 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8320 | /* |
| 8321 | * Don't handle the release after a long press, because it will |
| 8322 | * move the selection away from whatever the menu action was |
| 8323 | * trying to affect. |
| 8324 | */ |
Gilles Debunne | 0eb704c | 2010-11-30 12:50:54 -0800 | [diff] [blame] | 8325 | if (mDiscardNextActionUp && action == MotionEvent.ACTION_UP) { |
| 8326 | mDiscardNextActionUp = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8327 | return superResult; |
| 8328 | } |
| 8329 | |
Gilles Debunne | 70a6312 | 2011-09-01 13:27:33 -0700 | [diff] [blame] | 8330 | final boolean touchIsFinished = (action == MotionEvent.ACTION_UP) && |
| 8331 | !shouldIgnoreActionUpEvent() && isFocused(); |
Gilles Debunne | c3e85a7 | 2011-01-21 08:46:06 -0800 | [diff] [blame] | 8332 | |
Gilles Debunne | 70a6312 | 2011-09-01 13:27:33 -0700 | [diff] [blame] | 8333 | if ((mMovement != null || onCheckIsTextEditor()) && isEnabled() |
Janos Levai | 042856c | 2010-10-15 02:53:58 +0300 | [diff] [blame] | 8334 | && mText instanceof Spannable && mLayout != null) { |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 8335 | boolean handled = false; |
| 8336 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 8337 | if (mMovement != null) { |
| 8338 | handled |= mMovement.onTouchEvent(this, (Spannable) mText, event); |
| 8339 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8340 | |
Gilles Debunne | 70a6312 | 2011-09-01 13:27:33 -0700 | [diff] [blame] | 8341 | if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && mTextIsSelectable) { |
Gilles Debunne | f3895ed | 2010-12-21 12:53:58 -0800 | [diff] [blame] | 8342 | // The LinkMovementMethod which should handle taps on links has not been installed |
Gilles Debunne | 70a6312 | 2011-09-01 13:27:33 -0700 | [diff] [blame] | 8343 | // on non editable text that support text selection. |
| 8344 | // We reproduce its behavior here to open links for these. |
Gilles Debunne | f3895ed | 2010-12-21 12:53:58 -0800 | [diff] [blame] | 8345 | ClickableSpan[] links = ((Spannable) mText).getSpans(getSelectionStart(), |
| 8346 | getSelectionEnd(), ClickableSpan.class); |
| 8347 | |
| 8348 | if (links.length != 0) { |
| 8349 | links[0].onClick(this); |
| 8350 | handled = true; |
| 8351 | } |
| 8352 | } |
| 8353 | |
Gilles Debunne | 70a6312 | 2011-09-01 13:27:33 -0700 | [diff] [blame] | 8354 | if (touchIsFinished && (isTextEditable() || mTextIsSelectable)) { |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 8355 | // Show the IME, except when selecting in read-only text. |
satok | 863fcd6 | 2011-06-21 17:38:02 +0900 | [diff] [blame] | 8356 | final InputMethodManager imm = InputMethodManager.peekInstance(); |
satok | a67a3cf | 2011-09-07 17:14:03 +0900 | [diff] [blame] | 8357 | viewClicked(imm); |
Gilles Debunne | 0f4109e | 2011-10-19 11:26:21 -0700 | [diff] [blame] | 8358 | if (!mTextIsSelectable && mSoftInputShownOnFocus) { |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 8359 | handled |= imm != null && imm.showSoftInput(this, 0); |
Adam Powell | 879fb6b | 2010-09-20 11:23:56 -0700 | [diff] [blame] | 8360 | } |
Gilles Debunne | bbb5d6e | 2010-06-21 16:21:51 -0700 | [diff] [blame] | 8361 | |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 8362 | boolean selectAllGotFocus = mSelectAllOnFocus && didTouchFocusSelect(); |
Gilles Debunne | 70a6312 | 2011-09-01 13:27:33 -0700 | [diff] [blame] | 8363 | hideControllers(); |
| 8364 | if (!selectAllGotFocus && mText.length() > 0) { |
Gilles Debunne | b062e81 | 2011-09-27 14:58:37 -0700 | [diff] [blame] | 8365 | if (mSpellChecker != null) { |
| 8366 | // When the cursor moves, the word that was typed may need spell check |
| 8367 | mSpellChecker.onSelectionChanged(); |
| 8368 | } |
Gilles Debunne | 61ddbba | 2011-11-09 09:48:40 +0100 | [diff] [blame] | 8369 | if (!extractedTextModeWillBeStarted()) { |
| 8370 | if (isCursorInsideEasyCorrectionSpan()) { |
| 8371 | showSuggestions(); |
| 8372 | } else if (hasInsertionController()) { |
| 8373 | getInsertionController().show(); |
| 8374 | } |
Gilles Debunne | 8cbb4c6 | 2011-01-24 12:33:56 -0800 | [diff] [blame] | 8375 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8376 | } |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 8377 | |
| 8378 | handled = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8379 | } |
| 8380 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 8381 | if (handled) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8382 | return true; |
| 8383 | } |
| 8384 | } |
| 8385 | |
| 8386 | return superResult; |
| 8387 | } |
| 8388 | |
Luca Zanolin | 7d1c55f | 2011-08-16 14:59:26 +0100 | [diff] [blame] | 8389 | /** |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 8390 | * @return <code>true</code> if the cursor/current selection overlaps a {@link SuggestionSpan}. |
| 8391 | */ |
| 8392 | private boolean isCursorInsideSuggestionSpan() { |
| 8393 | if (!(mText instanceof Spannable)) return false; |
| 8394 | |
| 8395 | SuggestionSpan[] suggestionSpans = ((Spannable) mText).getSpans(getSelectionStart(), |
| 8396 | getSelectionEnd(), SuggestionSpan.class); |
| 8397 | return (suggestionSpans.length > 0); |
| 8398 | } |
| 8399 | |
| 8400 | /** |
Luca Zanolin | 7d1c55f | 2011-08-16 14:59:26 +0100 | [diff] [blame] | 8401 | * @return <code>true</code> if the cursor is inside an {@link SuggestionSpan} with |
| 8402 | * {@link SuggestionSpan#FLAG_EASY_CORRECT} set. |
| 8403 | */ |
| 8404 | private boolean isCursorInsideEasyCorrectionSpan() { |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 8405 | Spannable spannable = (Spannable) mText; |
Luca Zanolin | 7d1c55f | 2011-08-16 14:59:26 +0100 | [diff] [blame] | 8406 | SuggestionSpan[] suggestionSpans = spannable.getSpans(getSelectionStart(), |
| 8407 | getSelectionEnd(), SuggestionSpan.class); |
| 8408 | for (int i = 0; i < suggestionSpans.length; i++) { |
| 8409 | if ((suggestionSpans[i].getFlags() & SuggestionSpan.FLAG_EASY_CORRECT) != 0) { |
| 8410 | return true; |
| 8411 | } |
| 8412 | } |
| 8413 | return false; |
| 8414 | } |
| 8415 | |
Luca Zanolin | fe5e983 | 2011-09-02 19:41:42 +0100 | [diff] [blame] | 8416 | /** |
| 8417 | * Downgrades to simple suggestions all the easy correction spans that are not a spell check |
| 8418 | * span. |
| 8419 | */ |
| 8420 | private void downgradeEasyCorrectionSpans() { |
| 8421 | if (mText instanceof Spannable) { |
| 8422 | Spannable spannable = (Spannable) mText; |
| 8423 | SuggestionSpan[] suggestionSpans = spannable.getSpans(0, |
| 8424 | spannable.length(), SuggestionSpan.class); |
| 8425 | for (int i = 0; i < suggestionSpans.length; i++) { |
| 8426 | int flags = suggestionSpans[i].getFlags(); |
| 8427 | if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0 |
| 8428 | && (flags & SuggestionSpan.FLAG_MISSPELLED) == 0) { |
Gilles Debunne | 186aaf9 | 2011-09-16 14:26:12 -0700 | [diff] [blame] | 8429 | flags &= ~SuggestionSpan.FLAG_EASY_CORRECT; |
Luca Zanolin | fe5e983 | 2011-09-02 19:41:42 +0100 | [diff] [blame] | 8430 | suggestionSpans[i].setFlags(flags); |
| 8431 | } |
| 8432 | } |
| 8433 | } |
| 8434 | } |
| 8435 | |
Jeff Brown | 8f34567 | 2011-02-26 13:29:53 -0800 | [diff] [blame] | 8436 | @Override |
| 8437 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 8438 | if (mMovement != null && mText instanceof Spannable && mLayout != null) { |
| 8439 | try { |
| 8440 | if (mMovement.onGenericMotionEvent(this, (Spannable) mText, event)) { |
| 8441 | return true; |
| 8442 | } |
| 8443 | } catch (AbstractMethodError ex) { |
| 8444 | // onGenericMotionEvent was added to the MovementMethod interface in API 12. |
| 8445 | // Ignore its absence in case third party applications implemented the |
| 8446 | // interface directly. |
| 8447 | } |
| 8448 | } |
| 8449 | return super.onGenericMotionEvent(event); |
| 8450 | } |
| 8451 | |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 8452 | private void prepareCursorControllers() { |
Adam Powell | 8c8293b | 2010-10-12 14:45:12 -0700 | [diff] [blame] | 8453 | boolean windowSupportsHandles = false; |
| 8454 | |
| 8455 | ViewGroup.LayoutParams params = getRootView().getLayoutParams(); |
| 8456 | if (params instanceof WindowManager.LayoutParams) { |
| 8457 | WindowManager.LayoutParams windowParams = (WindowManager.LayoutParams) params; |
| 8458 | windowSupportsHandles = windowParams.type < WindowManager.LayoutParams.FIRST_SUB_WINDOW |
| 8459 | || windowParams.type > WindowManager.LayoutParams.LAST_SUB_WINDOW; |
| 8460 | } |
| 8461 | |
Gilles Debunne | 98dbfd4 | 2011-01-24 12:54:10 -0800 | [diff] [blame] | 8462 | mInsertionControllerEnabled = windowSupportsHandles && isCursorVisible() && mLayout != null; |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 8463 | mSelectionControllerEnabled = windowSupportsHandles && textCanBeSelected() && |
| 8464 | mLayout != null; |
| 8465 | |
| 8466 | if (!mInsertionControllerEnabled) { |
Gilles Debunne | f48e83b | 2010-12-06 18:36:08 -0800 | [diff] [blame] | 8467 | hideInsertionPointCursorController(); |
Adam Powell | 65a1de9 | 2011-01-30 15:47:29 -0800 | [diff] [blame] | 8468 | if (mInsertionPointCursorController != null) { |
| 8469 | mInsertionPointCursorController.onDetached(); |
| 8470 | mInsertionPointCursorController = null; |
| 8471 | } |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 8472 | } |
| 8473 | |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 8474 | if (!mSelectionControllerEnabled) { |
Gilles Debunne | e587d83 | 2010-11-23 20:20:11 -0800 | [diff] [blame] | 8475 | stopSelectionActionMode(); |
Adam Powell | 65a1de9 | 2011-01-30 15:47:29 -0800 | [diff] [blame] | 8476 | if (mSelectionModifierCursorController != null) { |
| 8477 | mSelectionModifierCursorController.onDetached(); |
| 8478 | mSelectionModifierCursorController = null; |
| 8479 | } |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 8480 | } |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 8481 | } |
| 8482 | |
| 8483 | /** |
Gilles Debunne | 86b9c78 | 2010-11-11 10:43:48 -0800 | [diff] [blame] | 8484 | * @return True iff this TextView contains a text that can be edited, or if this is |
| 8485 | * a selectable TextView. |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 8486 | */ |
| 8487 | private boolean isTextEditable() { |
Gilles Debunne | f076eeb | 2010-11-29 11:32:53 -0800 | [diff] [blame] | 8488 | return mText instanceof Editable && onCheckIsTextEditor() && isEnabled(); |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 8489 | } |
| 8490 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 8491 | /** |
| 8492 | * Returns true, only while processing a touch gesture, if the initial |
| 8493 | * touch down event caused focus to move to the text view and as a result |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 8494 | * its selection changed. Only valid while processing the touch gesture |
| 8495 | * of interest. |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 8496 | */ |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 8497 | public boolean didTouchFocusSelect() { |
| 8498 | return mTouchFocusSelected; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 8499 | } |
| 8500 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8501 | @Override |
| 8502 | public void cancelLongPress() { |
| 8503 | super.cancelLongPress(); |
Gilles Debunne | 0eb704c | 2010-11-30 12:50:54 -0800 | [diff] [blame] | 8504 | mIgnoreActionUpEvent = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8505 | } |
Gilles Debunne | 70a6312 | 2011-09-01 13:27:33 -0700 | [diff] [blame] | 8506 | |
| 8507 | /** |
| 8508 | * This method is only valid during a touch event. |
| 8509 | * |
| 8510 | * @return true when the ACTION_UP event should be ignored, false otherwise. |
| 8511 | * |
| 8512 | * @hide |
| 8513 | */ |
| 8514 | public boolean shouldIgnoreActionUpEvent() { |
| 8515 | return mIgnoreActionUpEvent; |
| 8516 | } |
| 8517 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8518 | @Override |
| 8519 | public boolean onTrackballEvent(MotionEvent event) { |
| 8520 | if (mMovement != null && mText instanceof Spannable && |
| 8521 | mLayout != null) { |
| 8522 | if (mMovement.onTrackballEvent(this, (Spannable) mText, event)) { |
| 8523 | return true; |
| 8524 | } |
| 8525 | } |
| 8526 | |
| 8527 | return super.onTrackballEvent(event); |
| 8528 | } |
| 8529 | |
| 8530 | public void setScroller(Scroller s) { |
| 8531 | mScroller = s; |
| 8532 | } |
| 8533 | |
| 8534 | private static class Blink extends Handler implements Runnable { |
Gilles Debunne | e15b358 | 2010-06-16 15:17:21 -0700 | [diff] [blame] | 8535 | private final WeakReference<TextView> mView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8536 | private boolean mCancelled; |
| 8537 | |
| 8538 | public Blink(TextView v) { |
| 8539 | mView = new WeakReference<TextView>(v); |
| 8540 | } |
| 8541 | |
| 8542 | public void run() { |
| 8543 | if (mCancelled) { |
| 8544 | return; |
| 8545 | } |
| 8546 | |
| 8547 | removeCallbacks(Blink.this); |
| 8548 | |
| 8549 | TextView tv = mView.get(); |
| 8550 | |
Gilles Debunne | 3d01006 | 2011-02-18 14:16:41 -0800 | [diff] [blame] | 8551 | if (tv != null && tv.shouldBlink()) { |
| 8552 | if (tv.mLayout != null) { |
| 8553 | tv.invalidateCursorPath(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8554 | } |
Gilles Debunne | 3d01006 | 2011-02-18 14:16:41 -0800 | [diff] [blame] | 8555 | |
| 8556 | postAtTime(this, SystemClock.uptimeMillis() + BLINK); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8557 | } |
| 8558 | } |
| 8559 | |
| 8560 | void cancel() { |
| 8561 | if (!mCancelled) { |
| 8562 | removeCallbacks(Blink.this); |
| 8563 | mCancelled = true; |
| 8564 | } |
| 8565 | } |
| 8566 | |
| 8567 | void uncancel() { |
| 8568 | mCancelled = false; |
| 8569 | } |
| 8570 | } |
| 8571 | |
Gilles Debunne | 3d01006 | 2011-02-18 14:16:41 -0800 | [diff] [blame] | 8572 | /** |
| 8573 | * @return True when the TextView isFocused and has a valid zero-length selection (cursor). |
| 8574 | */ |
| 8575 | private boolean shouldBlink() { |
| 8576 | if (!isFocused()) return false; |
| 8577 | |
| 8578 | final int start = getSelectionStart(); |
| 8579 | if (start < 0) return false; |
| 8580 | |
| 8581 | final int end = getSelectionEnd(); |
| 8582 | if (end < 0) return false; |
| 8583 | |
| 8584 | return start == end; |
| 8585 | } |
| 8586 | |
| 8587 | private void makeBlink() { |
| 8588 | if (isCursorVisible()) { |
| 8589 | if (shouldBlink()) { |
| 8590 | mShowCursor = SystemClock.uptimeMillis(); |
| 8591 | if (mBlink == null) mBlink = new Blink(this); |
| 8592 | mBlink.removeCallbacks(mBlink); |
| 8593 | mBlink.postAtTime(mBlink, mShowCursor + BLINK); |
| 8594 | } |
| 8595 | } else { |
| 8596 | if (mBlink != null) mBlink.removeCallbacks(mBlink); |
| 8597 | } |
| 8598 | } |
| 8599 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8600 | @Override |
| 8601 | protected float getLeftFadingEdgeStrength() { |
Romain Guy | 909cbaf | 2010-10-13 18:19:48 -0700 | [diff] [blame] | 8602 | if (mCurrentAlpha <= ViewConfiguration.ALPHA_THRESHOLD_INT) return 0.0f; |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 8603 | if (mEllipsize == TextUtils.TruncateAt.MARQUEE && |
| 8604 | mMarqueeFadeMode != MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8605 | if (mMarquee != null && !mMarquee.isStopped()) { |
| 8606 | final Marquee marquee = mMarquee; |
Romain Guy | c230319 | 2009-04-03 17:37:18 -0700 | [diff] [blame] | 8607 | if (marquee.shouldDrawLeftFade()) { |
| 8608 | return marquee.mScroll / getHorizontalFadingEdgeLength(); |
| 8609 | } else { |
| 8610 | return 0.0f; |
| 8611 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8612 | } else if (getLineCount() == 1) { |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 8613 | final int layoutDirection = getResolvedLayoutDirection(); |
| 8614 | final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection); |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 8615 | switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8616 | case Gravity.LEFT: |
| 8617 | return 0.0f; |
| 8618 | case Gravity.RIGHT: |
| 8619 | return (mLayout.getLineRight(0) - (mRight - mLeft) - |
| 8620 | getCompoundPaddingLeft() - getCompoundPaddingRight() - |
| 8621 | mLayout.getLineLeft(0)) / getHorizontalFadingEdgeLength(); |
| 8622 | case Gravity.CENTER_HORIZONTAL: |
| 8623 | return 0.0f; |
| 8624 | } |
| 8625 | } |
| 8626 | } |
| 8627 | return super.getLeftFadingEdgeStrength(); |
| 8628 | } |
| 8629 | |
| 8630 | @Override |
| 8631 | protected float getRightFadingEdgeStrength() { |
Romain Guy | 909cbaf | 2010-10-13 18:19:48 -0700 | [diff] [blame] | 8632 | if (mCurrentAlpha <= ViewConfiguration.ALPHA_THRESHOLD_INT) return 0.0f; |
Adam Powell | 282e377 | 2011-08-30 16:51:11 -0700 | [diff] [blame] | 8633 | if (mEllipsize == TextUtils.TruncateAt.MARQUEE && |
| 8634 | mMarqueeFadeMode != MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8635 | if (mMarquee != null && !mMarquee.isStopped()) { |
| 8636 | final Marquee marquee = mMarquee; |
Romain Guy | c230319 | 2009-04-03 17:37:18 -0700 | [diff] [blame] | 8637 | return (marquee.mMaxFadeScroll - marquee.mScroll) / getHorizontalFadingEdgeLength(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8638 | } else if (getLineCount() == 1) { |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 8639 | final int layoutDirection = getResolvedLayoutDirection(); |
| 8640 | final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection); |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 8641 | switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8642 | case Gravity.LEFT: |
Romain Guy | 076dc9f | 2009-06-24 17:17:51 -0700 | [diff] [blame] | 8643 | final int textWidth = (mRight - mLeft) - getCompoundPaddingLeft() - |
| 8644 | getCompoundPaddingRight(); |
| 8645 | final float lineWidth = mLayout.getLineWidth(0); |
| 8646 | return (lineWidth - textWidth) / getHorizontalFadingEdgeLength(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8647 | case Gravity.RIGHT: |
| 8648 | return 0.0f; |
| 8649 | case Gravity.CENTER_HORIZONTAL: |
Gilles Debunne | 44c1473 | 2010-10-19 11:56:59 -0700 | [diff] [blame] | 8650 | case Gravity.FILL_HORIZONTAL: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8651 | return (mLayout.getLineWidth(0) - ((mRight - mLeft) - |
| 8652 | getCompoundPaddingLeft() - getCompoundPaddingRight())) / |
| 8653 | getHorizontalFadingEdgeLength(); |
| 8654 | } |
| 8655 | } |
| 8656 | } |
| 8657 | return super.getRightFadingEdgeStrength(); |
| 8658 | } |
| 8659 | |
| 8660 | @Override |
| 8661 | protected int computeHorizontalScrollRange() { |
Romain Guy | dac5f9f | 2010-07-08 11:40:54 -0700 | [diff] [blame] | 8662 | if (mLayout != null) { |
| 8663 | return mSingleLine && (mGravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.LEFT ? |
| 8664 | (int) mLayout.getLineWidth(0) : mLayout.getWidth(); |
| 8665 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8666 | |
| 8667 | return super.computeHorizontalScrollRange(); |
| 8668 | } |
| 8669 | |
| 8670 | @Override |
| 8671 | protected int computeVerticalScrollRange() { |
| 8672 | if (mLayout != null) |
| 8673 | return mLayout.getHeight(); |
| 8674 | |
| 8675 | return super.computeVerticalScrollRange(); |
| 8676 | } |
| 8677 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 8678 | @Override |
| 8679 | protected int computeVerticalScrollExtent() { |
| 8680 | return getHeight() - getCompoundPaddingTop() - getCompoundPaddingBottom(); |
| 8681 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 8682 | |
| 8683 | @Override |
Svetoslav Ganov | ea515ae | 2011-09-14 18:15:32 -0700 | [diff] [blame] | 8684 | public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) { |
| 8685 | super.findViewsWithText(outViews, searched, flags); |
| 8686 | if (!outViews.contains(this) && (flags & FIND_VIEWS_WITH_TEXT) != 0 |
| 8687 | && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mText)) { |
| 8688 | String searchedLowerCase = searched.toString().toLowerCase(); |
| 8689 | String textLowerCase = mText.toString().toLowerCase(); |
| 8690 | if (textLowerCase.contains(searchedLowerCase)) { |
| 8691 | outViews.add(this); |
| 8692 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 8693 | } |
| 8694 | } |
| 8695 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8696 | public enum BufferType { |
| 8697 | NORMAL, SPANNABLE, EDITABLE, |
| 8698 | } |
| 8699 | |
| 8700 | /** |
| 8701 | * Returns the TextView_textColor attribute from the |
| 8702 | * Resources.StyledAttributes, if set, or the TextAppearance_textColor |
| 8703 | * from the TextView_textAppearance attribute, if TextView_textColor |
| 8704 | * was not set directly. |
| 8705 | */ |
| 8706 | public static ColorStateList getTextColors(Context context, TypedArray attrs) { |
| 8707 | ColorStateList colors; |
| 8708 | colors = attrs.getColorStateList(com.android.internal.R.styleable. |
| 8709 | TextView_textColor); |
| 8710 | |
| 8711 | if (colors == null) { |
| 8712 | int ap = attrs.getResourceId(com.android.internal.R.styleable. |
| 8713 | TextView_textAppearance, -1); |
| 8714 | if (ap != -1) { |
| 8715 | TypedArray appearance; |
| 8716 | appearance = context.obtainStyledAttributes(ap, |
| 8717 | com.android.internal.R.styleable.TextAppearance); |
| 8718 | colors = appearance.getColorStateList(com.android.internal.R.styleable. |
| 8719 | TextAppearance_textColor); |
| 8720 | appearance.recycle(); |
| 8721 | } |
| 8722 | } |
| 8723 | |
| 8724 | return colors; |
| 8725 | } |
| 8726 | |
| 8727 | /** |
| 8728 | * Returns the default color from the TextView_textColor attribute |
| 8729 | * from the AttributeSet, if set, or the default color from the |
| 8730 | * TextAppearance_textColor from the TextView_textAppearance attribute, |
| 8731 | * if TextView_textColor was not set directly. |
| 8732 | */ |
| 8733 | public static int getTextColor(Context context, |
| 8734 | TypedArray attrs, |
| 8735 | int def) { |
| 8736 | ColorStateList colors = getTextColors(context, attrs); |
| 8737 | |
| 8738 | if (colors == null) { |
| 8739 | return def; |
| 8740 | } else { |
| 8741 | return colors.getDefaultColor(); |
| 8742 | } |
| 8743 | } |
| 8744 | |
| 8745 | @Override |
| 8746 | public boolean onKeyShortcut(int keyCode, KeyEvent event) { |
Jeff Brown | c1df907 | 2010-12-21 16:38:50 -0800 | [diff] [blame] | 8747 | final int filteredMetaState = event.getMetaState() & ~KeyEvent.META_CTRL_MASK; |
| 8748 | if (KeyEvent.metaStateHasNoModifiers(filteredMetaState)) { |
| 8749 | switch (keyCode) { |
| 8750 | case KeyEvent.KEYCODE_A: |
| 8751 | if (canSelectText()) { |
| 8752 | return onTextContextMenuItem(ID_SELECT_ALL); |
| 8753 | } |
| 8754 | break; |
| 8755 | case KeyEvent.KEYCODE_X: |
| 8756 | if (canCut()) { |
| 8757 | return onTextContextMenuItem(ID_CUT); |
| 8758 | } |
| 8759 | break; |
| 8760 | case KeyEvent.KEYCODE_C: |
| 8761 | if (canCopy()) { |
| 8762 | return onTextContextMenuItem(ID_COPY); |
| 8763 | } |
| 8764 | break; |
| 8765 | case KeyEvent.KEYCODE_V: |
| 8766 | if (canPaste()) { |
| 8767 | return onTextContextMenuItem(ID_PASTE); |
| 8768 | } |
| 8769 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8770 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8771 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8772 | return super.onKeyShortcut(keyCode, event); |
| 8773 | } |
| 8774 | |
Gilles Debunne | cbcb345 | 2010-12-17 15:31:02 -0800 | [diff] [blame] | 8775 | /** |
| 8776 | * Unlike {@link #textCanBeSelected()}, this method is based on the <i>current</i> state of the |
| 8777 | * TextView. {@link #textCanBeSelected()} has to be true (this is one of the conditions to have |
| 8778 | * a selection controller (see {@link #prepareCursorControllers()}), but this is not sufficient. |
| 8779 | */ |
Gilles Debunne | baaace5 | 2010-10-01 15:47:13 -0700 | [diff] [blame] | 8780 | private boolean canSelectText() { |
Gilles Debunne | 6da7e93 | 2010-12-07 14:28:14 -0800 | [diff] [blame] | 8781 | return hasSelectionController() && mText.length() != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8782 | } |
| 8783 | |
Gilles Debunne | cbcb345 | 2010-12-17 15:31:02 -0800 | [diff] [blame] | 8784 | /** |
| 8785 | * Test based on the <i>intrinsic</i> charateristics of the TextView. |
| 8786 | * The text must be spannable and the movement method must allow for arbitary selection. |
| 8787 | * |
| 8788 | * See also {@link #canSelectText()}. |
| 8789 | */ |
Gilles Debunne | baaace5 | 2010-10-01 15:47:13 -0700 | [diff] [blame] | 8790 | private boolean textCanBeSelected() { |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 8791 | // prepareCursorController() relies on this method. |
| 8792 | // If you change this condition, make sure prepareCursorController is called anywhere |
| 8793 | // the value of this condition might be changed. |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 8794 | if (mMovement == null || !mMovement.canSelectArbitrarily()) return false; |
| 8795 | return isTextEditable() || (mTextIsSelectable && mText instanceof Spannable && isEnabled()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8796 | } |
| 8797 | |
| 8798 | private boolean canCut() { |
Gilles Debunne | 0dcad2b | 2010-10-15 16:29:25 -0700 | [diff] [blame] | 8799 | if (hasPasswordTransformationMethod()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8800 | return false; |
| 8801 | } |
| 8802 | |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 8803 | if (mText.length() > 0 && hasSelection() && mText instanceof Editable && mInput != null) { |
| 8804 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8805 | } |
| 8806 | |
| 8807 | return false; |
| 8808 | } |
| 8809 | |
| 8810 | private boolean canCopy() { |
Gilles Debunne | 0dcad2b | 2010-10-15 16:29:25 -0700 | [diff] [blame] | 8811 | if (hasPasswordTransformationMethod()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8812 | return false; |
| 8813 | } |
| 8814 | |
Gilles Debunne | 03789e8 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 8815 | if (mText.length() > 0 && hasSelection()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8816 | return true; |
| 8817 | } |
| 8818 | |
| 8819 | return false; |
| 8820 | } |
| 8821 | |
| 8822 | private boolean canPaste() { |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 8823 | return (mText instanceof Editable && |
| 8824 | mInput != null && |
| 8825 | getSelectionStart() >= 0 && |
| 8826 | getSelectionEnd() >= 0 && |
| 8827 | ((ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE)). |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 8828 | hasPrimaryClip()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8829 | } |
| 8830 | |
Gilles Debunne | cf1e925 | 2010-10-07 20:46:03 -0700 | [diff] [blame] | 8831 | private static long packRangeInLong(int start, int end) { |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 8832 | return (((long) start) << 32) | end; |
| 8833 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8834 | |
Gilles Debunne | cf1e925 | 2010-10-07 20:46:03 -0700 | [diff] [blame] | 8835 | private static int extractRangeStartFromLong(long range) { |
| 8836 | return (int) (range >>> 32); |
| 8837 | } |
| 8838 | |
| 8839 | private static int extractRangeEndFromLong(long range) { |
| 8840 | return (int) (range & 0x00000000FFFFFFFFL); |
| 8841 | } |
Gilles Debunne | cbfbb52 | 2010-10-07 16:57:31 -0700 | [diff] [blame] | 8842 | |
Gilles Debunne | c59269f | 2011-04-22 11:46:09 -0700 | [diff] [blame] | 8843 | private boolean selectAll() { |
| 8844 | final int length = mText.length(); |
| 8845 | Selection.setSelection((Spannable) mText, 0, length); |
| 8846 | return length > 0; |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 8847 | } |
| 8848 | |
Gilles Debunne | c59269f | 2011-04-22 11:46:09 -0700 | [diff] [blame] | 8849 | /** |
| 8850 | * Adjusts selection to the word under last touch offset. |
| 8851 | * Return true if the operation was successfully performed. |
| 8852 | */ |
| 8853 | private boolean selectCurrentWord() { |
Gilles Debunne | 6da7e93 | 2010-12-07 14:28:14 -0800 | [diff] [blame] | 8854 | if (!canSelectText()) { |
Gilles Debunne | c59269f | 2011-04-22 11:46:09 -0700 | [diff] [blame] | 8855 | return false; |
Gilles Debunne | 6da7e93 | 2010-12-07 14:28:14 -0800 | [diff] [blame] | 8856 | } |
| 8857 | |
Gilles Debunne | 710a910 | 2010-11-23 16:50:28 -0800 | [diff] [blame] | 8858 | if (hasPasswordTransformationMethod()) { |
Gilles Debunne | 87380bc | 2011-01-04 13:24:54 -0800 | [diff] [blame] | 8859 | // Always select all on a password field. |
Gilles Debunne | 4dfe086 | 2010-12-17 15:46:28 -0800 | [diff] [blame] | 8860 | // Cut/copy menu entries are not available for passwords, but being able to select all |
| 8861 | // is however useful to delete or paste to replace the entire content. |
Gilles Debunne | c59269f | 2011-04-22 11:46:09 -0700 | [diff] [blame] | 8862 | return selectAll(); |
| 8863 | } |
| 8864 | |
| 8865 | int klass = mInputType & InputType.TYPE_MASK_CLASS; |
| 8866 | int variation = mInputType & InputType.TYPE_MASK_VARIATION; |
| 8867 | |
| 8868 | // Specific text field types: select the entire text for these |
| 8869 | if (klass == InputType.TYPE_CLASS_NUMBER || |
| 8870 | klass == InputType.TYPE_CLASS_PHONE || |
| 8871 | klass == InputType.TYPE_CLASS_DATETIME || |
| 8872 | variation == InputType.TYPE_TEXT_VARIATION_URI || |
| 8873 | variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS || |
| 8874 | variation == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS || |
| 8875 | variation == InputType.TYPE_TEXT_VARIATION_FILTER) { |
| 8876 | return selectAll(); |
Gilles Debunne | b0d6ba1 | 2010-08-17 20:01:42 -0700 | [diff] [blame] | 8877 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8878 | |
Gilles Debunne | 87380bc | 2011-01-04 13:24:54 -0800 | [diff] [blame] | 8879 | long lastTouchOffsets = getLastTouchOffsets(); |
| 8880 | final int minOffset = extractRangeStartFromLong(lastTouchOffsets); |
| 8881 | final int maxOffset = extractRangeEndFromLong(lastTouchOffsets); |
Gilles Debunne | 4dfe086 | 2010-12-17 15:46:28 -0800 | [diff] [blame] | 8882 | |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 8883 | // Safety check in case standard touch event handling has been bypassed |
| 8884 | if (minOffset < 0 || minOffset >= mText.length()) return false; |
| 8885 | if (maxOffset < 0 || maxOffset >= mText.length()) return false; |
| 8886 | |
Gilles Debunne | 4dfe086 | 2010-12-17 15:46:28 -0800 | [diff] [blame] | 8887 | int selectionStart, selectionEnd; |
| 8888 | |
| 8889 | // If a URLSpan (web address, email, phone...) is found at that position, select it. |
| 8890 | URLSpan[] urlSpans = ((Spanned) mText).getSpans(minOffset, maxOffset, URLSpan.class); |
Gilles Debunne | 8f3105f | 2011-10-03 18:28:59 -0700 | [diff] [blame] | 8891 | if (urlSpans.length >= 1) { |
| 8892 | URLSpan urlSpan = urlSpans[0]; |
| 8893 | selectionStart = ((Spanned) mText).getSpanStart(urlSpan); |
| 8894 | selectionEnd = ((Spanned) mText).getSpanEnd(urlSpan); |
Gilles Debunne | 4dfe086 | 2010-12-17 15:46:28 -0800 | [diff] [blame] | 8895 | } else { |
Gilles Debunne | 9d8d3f1 | 2011-10-13 12:15:10 -0700 | [diff] [blame] | 8896 | final WordIterator wordIterator = getWordIterator(); |
| 8897 | wordIterator.setCharSequence(mText, minOffset, maxOffset); |
Gilles Debunne | 4dfe086 | 2010-12-17 15:46:28 -0800 | [diff] [blame] | 8898 | |
Gilles Debunne | 9d8d3f1 | 2011-10-13 12:15:10 -0700 | [diff] [blame] | 8899 | selectionStart = wordIterator.getBeginning(minOffset); |
Gilles Debunne | c59269f | 2011-04-22 11:46:09 -0700 | [diff] [blame] | 8900 | if (selectionStart == BreakIterator.DONE) return false; |
| 8901 | |
Gilles Debunne | 9d8d3f1 | 2011-10-13 12:15:10 -0700 | [diff] [blame] | 8902 | selectionEnd = wordIterator.getEnd(maxOffset); |
Gilles Debunne | c59269f | 2011-04-22 11:46:09 -0700 | [diff] [blame] | 8903 | if (selectionEnd == BreakIterator.DONE) return false; |
Gilles Debunne | 8f3105f | 2011-10-03 18:28:59 -0700 | [diff] [blame] | 8904 | |
| 8905 | if (selectionStart == selectionEnd) { |
| 8906 | // Possible when the word iterator does not properly handle the text's language |
| 8907 | long range = getCharRange(selectionStart); |
| 8908 | selectionStart = extractRangeStartFromLong(range); |
| 8909 | selectionEnd = extractRangeEndFromLong(range); |
| 8910 | } |
Gilles Debunne | 4dfe086 | 2010-12-17 15:46:28 -0800 | [diff] [blame] | 8911 | } |
| 8912 | |
| 8913 | Selection.setSelection((Spannable) mText, selectionStart, selectionEnd); |
Gilles Debunne | 8f3105f | 2011-10-03 18:28:59 -0700 | [diff] [blame] | 8914 | return selectionEnd > selectionStart; |
| 8915 | } |
| 8916 | |
Gilles Debunne | 9d8d3f1 | 2011-10-13 12:15:10 -0700 | [diff] [blame] | 8917 | /** |
| 8918 | * This is a temporary method. Future versions may support multi-locale text. |
| 8919 | * |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 8920 | * @return The locale that should be used for a word iterator and a spell checker |
| 8921 | * in this TextView, based on the current spell checker settings, |
| 8922 | * the current IME's locale, or the system default locale. |
Gilles Debunne | 9d8d3f1 | 2011-10-13 12:15:10 -0700 | [diff] [blame] | 8923 | * @hide |
| 8924 | */ |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 8925 | public Locale getTextServicesLocale() { |
Gilles Debunne | 9d8d3f1 | 2011-10-13 12:15:10 -0700 | [diff] [blame] | 8926 | Locale locale = Locale.getDefault(); |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 8927 | final TextServicesManager textServicesManager = (TextServicesManager) |
| 8928 | mContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE); |
| 8929 | final SpellCheckerSubtype subtype = textServicesManager.getCurrentSpellCheckerSubtype(true); |
| 8930 | if (subtype != null) { |
| 8931 | locale = new Locale(subtype.getLocale()); |
Gilles Debunne | 9d8d3f1 | 2011-10-13 12:15:10 -0700 | [diff] [blame] | 8932 | } |
| 8933 | return locale; |
| 8934 | } |
| 8935 | |
| 8936 | void onLocaleChanged() { |
Gilles Debunne | e9b8280 | 2011-10-27 14:38:27 -0700 | [diff] [blame] | 8937 | removeMisspelledSpans((Editable) mText); |
Gilles Debunne | 9d8d3f1 | 2011-10-13 12:15:10 -0700 | [diff] [blame] | 8938 | // Will be re-created on demand in getWordIterator with the proper new locale |
| 8939 | mWordIterator = null; |
| 8940 | } |
| 8941 | |
| 8942 | /** |
| 8943 | * @hide |
| 8944 | */ |
| 8945 | public WordIterator getWordIterator() { |
| 8946 | if (mWordIterator == null) { |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 8947 | mWordIterator = new WordIterator(getTextServicesLocale()); |
Gilles Debunne | 9d8d3f1 | 2011-10-13 12:15:10 -0700 | [diff] [blame] | 8948 | } |
| 8949 | return mWordIterator; |
| 8950 | } |
| 8951 | |
Gilles Debunne | 8f3105f | 2011-10-03 18:28:59 -0700 | [diff] [blame] | 8952 | private long getCharRange(int offset) { |
| 8953 | final int textLength = mText.length(); |
| 8954 | if (offset + 1 < textLength) { |
| 8955 | final char currentChar = mText.charAt(offset); |
| 8956 | final char nextChar = mText.charAt(offset + 1); |
| 8957 | if (Character.isSurrogatePair(currentChar, nextChar)) { |
| 8958 | return packRangeInLong(offset, offset + 2); |
| 8959 | } |
| 8960 | } |
| 8961 | if (offset < textLength) { |
| 8962 | return packRangeInLong(offset, offset + 1); |
| 8963 | } |
| 8964 | if (offset - 2 >= 0) { |
| 8965 | final char previousChar = mText.charAt(offset - 1); |
| 8966 | final char previousPreviousChar = mText.charAt(offset - 2); |
| 8967 | if (Character.isSurrogatePair(previousPreviousChar, previousChar)) { |
| 8968 | return packRangeInLong(offset - 2, offset); |
| 8969 | } |
| 8970 | } |
| 8971 | if (offset - 1 >= 0) { |
| 8972 | return packRangeInLong(offset - 1, offset); |
| 8973 | } |
| 8974 | return packRangeInLong(offset, offset); |
Gilles Debunne | 4dfe086 | 2010-12-17 15:46:28 -0800 | [diff] [blame] | 8975 | } |
| 8976 | |
| 8977 | private long getLastTouchOffsets() { |
Gilles Debunne | 771d64b | 2011-11-10 10:12:13 +0100 | [diff] [blame] | 8978 | SelectionModifierCursorController selectionController = getSelectionController(); |
| 8979 | final int minOffset = selectionController.getMinTouchOffset(); |
| 8980 | final int maxOffset = selectionController.getMaxTouchOffset(); |
Gilles Debunne | 4dfe086 | 2010-12-17 15:46:28 -0800 | [diff] [blame] | 8981 | return packRangeInLong(minOffset, maxOffset); |
Gilles Debunne | b0d6ba1 | 2010-08-17 20:01:42 -0700 | [diff] [blame] | 8982 | } |
Gilles Debunne | df4ee43 | 2010-08-25 19:13:48 -0700 | [diff] [blame] | 8983 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8984 | @Override |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 8985 | public void onPopulateAccessibilityEvent(AccessibilityEvent event) { |
Svetoslav Ganov | 887e1a1 | 2011-04-29 15:09:28 -0700 | [diff] [blame] | 8986 | super.onPopulateAccessibilityEvent(event); |
| 8987 | |
Svetoslav Ganov | 1d1e110 | 2010-11-16 16:44:03 -0800 | [diff] [blame] | 8988 | final boolean isPassword = hasPasswordTransformationMethod(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 8989 | if (!isPassword) { |
Svetoslav Ganov | ab5a4057 | 2011-09-15 11:30:01 -0700 | [diff] [blame] | 8990 | CharSequence text = getTextForAccessibility(); |
Svetoslav Ganov | d37848a | 2011-09-20 14:03:55 -0700 | [diff] [blame] | 8991 | if (!TextUtils.isEmpty(text)) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 8992 | event.getText().add(text); |
| 8993 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 8994 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 8995 | } |
| 8996 | |
Svetoslav Ganov | 3040132 | 2011-05-12 18:53:45 -0700 | [diff] [blame] | 8997 | @Override |
| 8998 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 8999 | super.onInitializeAccessibilityEvent(event); |
| 9000 | |
| 9001 | final boolean isPassword = hasPasswordTransformationMethod(); |
| 9002 | event.setPassword(isPassword); |
Svetoslav Ganov | a015617 | 2011-06-26 17:55:44 -0700 | [diff] [blame] | 9003 | |
| 9004 | if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED) { |
| 9005 | event.setFromIndex(Selection.getSelectionStart(mText)); |
| 9006 | event.setToIndex(Selection.getSelectionEnd(mText)); |
| 9007 | event.setItemCount(mText.length()); |
| 9008 | } |
Svetoslav Ganov | 3040132 | 2011-05-12 18:53:45 -0700 | [diff] [blame] | 9009 | } |
| 9010 | |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 9011 | @Override |
| 9012 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 9013 | super.onInitializeAccessibilityNodeInfo(info); |
| 9014 | |
| 9015 | final boolean isPassword = hasPasswordTransformationMethod(); |
| 9016 | if (!isPassword) { |
Svetoslav Ganov | ab5a4057 | 2011-09-15 11:30:01 -0700 | [diff] [blame] | 9017 | info.setText(getTextForAccessibility()); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 9018 | } |
| 9019 | info.setPassword(isPassword); |
| 9020 | } |
| 9021 | |
Svetoslav Ganov | 4e03f59 | 2011-07-29 22:17:14 -0700 | [diff] [blame] | 9022 | @Override |
| 9023 | public void sendAccessibilityEvent(int eventType) { |
| 9024 | // Do not send scroll events since first they are not interesting for |
| 9025 | // accessibility and second such events a generated too frequently. |
| 9026 | // For details see the implementation of bringTextIntoView(). |
| 9027 | if (eventType == AccessibilityEvent.TYPE_VIEW_SCROLLED) { |
| 9028 | return; |
| 9029 | } |
| 9030 | super.sendAccessibilityEvent(eventType); |
| 9031 | } |
| 9032 | |
Svetoslav Ganov | ab5a4057 | 2011-09-15 11:30:01 -0700 | [diff] [blame] | 9033 | /** |
| 9034 | * Gets the text reported for accessibility purposes. It is the |
| 9035 | * text if not empty or the hint. |
| 9036 | * |
| 9037 | * @return The accessibility text. |
| 9038 | */ |
| 9039 | private CharSequence getTextForAccessibility() { |
| 9040 | CharSequence text = getText(); |
| 9041 | if (TextUtils.isEmpty(text)) { |
| 9042 | text = getHint(); |
| 9043 | } |
| 9044 | return text; |
| 9045 | } |
| 9046 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 9047 | void sendAccessibilityEventTypeViewTextChanged(CharSequence beforeText, |
| 9048 | int fromIndex, int removedCount, int addedCount) { |
| 9049 | AccessibilityEvent event = |
| 9050 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED); |
| 9051 | event.setFromIndex(fromIndex); |
| 9052 | event.setRemovedCount(removedCount); |
| 9053 | event.setAddedCount(addedCount); |
| 9054 | event.setBeforeText(beforeText); |
| 9055 | sendAccessibilityEventUnchecked(event); |
| 9056 | } |
| 9057 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9058 | /** |
| 9059 | * Returns whether this text view is a current input method target. The |
| 9060 | * default implementation just checks with {@link InputMethodManager}. |
| 9061 | */ |
| 9062 | public boolean isInputMethodTarget() { |
| 9063 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 9064 | return imm != null && imm.isActive(this); |
| 9065 | } |
| 9066 | |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 9067 | // Selection context mode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9068 | private static final int ID_SELECT_ALL = android.R.id.selectAll; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9069 | private static final int ID_CUT = android.R.id.cut; |
| 9070 | private static final int ID_COPY = android.R.id.copy; |
| 9071 | private static final int ID_PASTE = android.R.id.paste; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9072 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9073 | /** |
| 9074 | * Called when a context menu option for the text view is selected. Currently |
Gilles Debunne | 07194e5 | 2011-11-02 14:18:44 -0700 | [diff] [blame] | 9075 | * this will be one of {@link android.R.id#selectAll}, {@link android.R.id#cut}, |
| 9076 | * {@link android.R.id#copy} or {@link android.R.id#paste}. |
Gilles Debunne | c59269f | 2011-04-22 11:46:09 -0700 | [diff] [blame] | 9077 | * |
| 9078 | * @return true if the context menu item action was performed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9079 | */ |
| 9080 | public boolean onTextContextMenuItem(int id) { |
Gilles Debunne | b0d6ba1 | 2010-08-17 20:01:42 -0700 | [diff] [blame] | 9081 | int min = 0; |
| 9082 | int max = mText.length(); |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 9083 | |
Gilles Debunne | b0d6ba1 | 2010-08-17 20:01:42 -0700 | [diff] [blame] | 9084 | if (isFocused()) { |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 9085 | final int selStart = getSelectionStart(); |
| 9086 | final int selEnd = getSelectionEnd(); |
| 9087 | |
Gilles Debunne | b0d6ba1 | 2010-08-17 20:01:42 -0700 | [diff] [blame] | 9088 | min = Math.max(0, Math.min(selStart, selEnd)); |
| 9089 | max = Math.max(0, Math.max(selStart, selEnd)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9090 | } |
| 9091 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9092 | switch (id) { |
Jeff Brown | c1df907 | 2010-12-21 16:38:50 -0800 | [diff] [blame] | 9093 | case ID_SELECT_ALL: |
Gilles Debunne | 299733e | 2011-02-07 17:11:41 -0800 | [diff] [blame] | 9094 | // This does not enter text selection mode. Text is highlighted, so that it can be |
Gilles Debunne | c59269f | 2011-04-22 11:46:09 -0700 | [diff] [blame] | 9095 | // bulk edited, like selectAllOnFocus does. Returns true even if text is empty. |
Jeff Brown | c1df907 | 2010-12-21 16:38:50 -0800 | [diff] [blame] | 9096 | selectAll(); |
Jeff Brown | c1df907 | 2010-12-21 16:38:50 -0800 | [diff] [blame] | 9097 | return true; |
| 9098 | |
| 9099 | case ID_PASTE: |
| 9100 | paste(min, max); |
| 9101 | return true; |
| 9102 | |
| 9103 | case ID_CUT: |
Gilles Debunne | cf68fee | 2011-09-29 10:55:36 -0700 | [diff] [blame] | 9104 | setPrimaryClip(ClipData.newPlainText(null, getTransformedText(min, max))); |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 9105 | deleteText_internal(min, max); |
Jeff Brown | c1df907 | 2010-12-21 16:38:50 -0800 | [diff] [blame] | 9106 | stopSelectionActionMode(); |
| 9107 | return true; |
| 9108 | |
| 9109 | case ID_COPY: |
Gilles Debunne | cf68fee | 2011-09-29 10:55:36 -0700 | [diff] [blame] | 9110 | setPrimaryClip(ClipData.newPlainText(null, getTransformedText(min, max))); |
Jeff Brown | c1df907 | 2010-12-21 16:38:50 -0800 | [diff] [blame] | 9111 | stopSelectionActionMode(); |
| 9112 | return true; |
| 9113 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9114 | return false; |
| 9115 | } |
| 9116 | |
Gilles Debunne | cf68fee | 2011-09-29 10:55:36 -0700 | [diff] [blame] | 9117 | private CharSequence getTransformedText(int start, int end) { |
| 9118 | return removeSuggestionSpans(mTransformed.subSequence(start, end)); |
| 9119 | } |
| 9120 | |
Gilles Debunne | cf1e925 | 2010-10-07 20:46:03 -0700 | [diff] [blame] | 9121 | /** |
| 9122 | * Prepare text so that there are not zero or two spaces at beginning and end of region defined |
| 9123 | * by [min, max] when replacing this region by paste. |
Gilles Debunne | c0752ee | 2010-12-22 17:50:42 -0800 | [diff] [blame] | 9124 | * Note that if there were two spaces (or more) at that position before, they are kept. We just |
Gilles Debunne | 4ae0f29 | 2010-11-29 14:56:39 -0800 | [diff] [blame] | 9125 | * make sure we do not add an extra one from the paste content. |
Gilles Debunne | cf1e925 | 2010-10-07 20:46:03 -0700 | [diff] [blame] | 9126 | */ |
| 9127 | private long prepareSpacesAroundPaste(int min, int max, CharSequence paste) { |
Gilles Debunne | c0752ee | 2010-12-22 17:50:42 -0800 | [diff] [blame] | 9128 | if (paste.length() > 0) { |
| 9129 | if (min > 0) { |
| 9130 | final char charBefore = mTransformed.charAt(min - 1); |
| 9131 | final char charAfter = paste.charAt(0); |
Gilles Debunne | cf1e925 | 2010-10-07 20:46:03 -0700 | [diff] [blame] | 9132 | |
Gilles Debunne | c0752ee | 2010-12-22 17:50:42 -0800 | [diff] [blame] | 9133 | if (Character.isSpaceChar(charBefore) && Character.isSpaceChar(charAfter)) { |
| 9134 | // Two spaces at beginning of paste: remove one |
| 9135 | final int originalLength = mText.length(); |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 9136 | deleteText_internal(min - 1, min); |
Gilles Debunne | c0752ee | 2010-12-22 17:50:42 -0800 | [diff] [blame] | 9137 | // Due to filters, there is no guarantee that exactly one character was |
| 9138 | // removed: count instead. |
| 9139 | final int delta = mText.length() - originalLength; |
| 9140 | min += delta; |
| 9141 | max += delta; |
| 9142 | } else if (!Character.isSpaceChar(charBefore) && charBefore != '\n' && |
| 9143 | !Character.isSpaceChar(charAfter) && charAfter != '\n') { |
| 9144 | // No space at beginning of paste: add one |
| 9145 | final int originalLength = mText.length(); |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 9146 | replaceText_internal(min, min, " "); |
Gilles Debunne | c0752ee | 2010-12-22 17:50:42 -0800 | [diff] [blame] | 9147 | // Taking possible filters into account as above. |
| 9148 | final int delta = mText.length() - originalLength; |
| 9149 | min += delta; |
| 9150 | max += delta; |
| 9151 | } |
Gilles Debunne | cf1e925 | 2010-10-07 20:46:03 -0700 | [diff] [blame] | 9152 | } |
Gilles Debunne | c0752ee | 2010-12-22 17:50:42 -0800 | [diff] [blame] | 9153 | |
| 9154 | if (max < mText.length()) { |
| 9155 | final char charBefore = paste.charAt(paste.length() - 1); |
| 9156 | final char charAfter = mTransformed.charAt(max); |
| 9157 | |
| 9158 | if (Character.isSpaceChar(charBefore) && Character.isSpaceChar(charAfter)) { |
| 9159 | // Two spaces at end of paste: remove one |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 9160 | deleteText_internal(max, max + 1); |
Gilles Debunne | c0752ee | 2010-12-22 17:50:42 -0800 | [diff] [blame] | 9161 | } else if (!Character.isSpaceChar(charBefore) && charBefore != '\n' && |
| 9162 | !Character.isSpaceChar(charAfter) && charAfter != '\n') { |
| 9163 | // No space at end of paste: add one |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 9164 | replaceText_internal(max, max, " "); |
Gilles Debunne | c0752ee | 2010-12-22 17:50:42 -0800 | [diff] [blame] | 9165 | } |
Gilles Debunne | cf1e925 | 2010-10-07 20:46:03 -0700 | [diff] [blame] | 9166 | } |
| 9167 | } |
Gilles Debunne | 4ae0f29 | 2010-11-29 14:56:39 -0800 | [diff] [blame] | 9168 | |
Gilles Debunne | cf1e925 | 2010-10-07 20:46:03 -0700 | [diff] [blame] | 9169 | return packRangeInLong(min, max); |
| 9170 | } |
| 9171 | |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 9172 | private DragShadowBuilder getTextThumbnailBuilder(CharSequence text) { |
| 9173 | TextView shadowView = (TextView) inflate(mContext, |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 9174 | com.android.internal.R.layout.text_drag_thumbnail, null); |
| 9175 | |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 9176 | if (shadowView == null) { |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 9177 | throw new IllegalArgumentException("Unable to inflate text drag thumbnail"); |
| 9178 | } |
| 9179 | |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 9180 | if (text.length() > DRAG_SHADOW_MAX_TEXT_LENGTH) { |
| 9181 | text = text.subSequence(0, DRAG_SHADOW_MAX_TEXT_LENGTH); |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 9182 | } |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 9183 | shadowView.setText(text); |
| 9184 | shadowView.setTextColor(getTextColors()); |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 9185 | |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 9186 | shadowView.setTextAppearance(mContext, R.styleable.Theme_textAppearanceLarge); |
| 9187 | shadowView.setGravity(Gravity.CENTER); |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 9188 | |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 9189 | shadowView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 9190 | ViewGroup.LayoutParams.WRAP_CONTENT)); |
| 9191 | |
| 9192 | final int size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 9193 | shadowView.measure(size, size); |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 9194 | |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 9195 | shadowView.layout(0, 0, shadowView.getMeasuredWidth(), shadowView.getMeasuredHeight()); |
| 9196 | shadowView.invalidate(); |
| 9197 | return new DragShadowBuilder(shadowView); |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 9198 | } |
| 9199 | |
Gilles Debunne | aaa8479 | 2010-12-03 11:10:14 -0800 | [diff] [blame] | 9200 | private static class DragLocalState { |
| 9201 | public TextView sourceTextView; |
| 9202 | public int start, end; |
| 9203 | |
| 9204 | public DragLocalState(TextView sourceTextView, int start, int end) { |
| 9205 | this.sourceTextView = sourceTextView; |
| 9206 | this.start = start; |
| 9207 | this.end = end; |
| 9208 | } |
| 9209 | } |
| 9210 | |
Gilles Debunne | e15b358 | 2010-06-16 15:17:21 -0700 | [diff] [blame] | 9211 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9212 | public boolean performLongClick() { |
Gilles Debunne | e28454a | 2011-09-07 18:03:44 -0700 | [diff] [blame] | 9213 | boolean handled = false; |
| 9214 | boolean vibrate = true; |
| 9215 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9216 | if (super.performLongClick()) { |
Gilles Debunne | 0eb704c | 2010-11-30 12:50:54 -0800 | [diff] [blame] | 9217 | mDiscardNextActionUp = true; |
Gilles Debunne | e28454a | 2011-09-07 18:03:44 -0700 | [diff] [blame] | 9218 | handled = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9219 | } |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 9220 | |
Gilles Debunne | 33a8cfb | 2010-12-10 12:00:42 -0800 | [diff] [blame] | 9221 | // Long press in empty space moves cursor and shows the Paste affordance if available. |
Gilles Debunne | e28454a | 2011-09-07 18:03:44 -0700 | [diff] [blame] | 9222 | if (!handled && !isPositionOnText(mLastDownPositionX, mLastDownPositionY) && |
Gilles Debunne | 0a2aa40 | 2010-11-24 17:57:46 -0800 | [diff] [blame] | 9223 | mInsertionControllerEnabled) { |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 9224 | final int offset = getOffsetForPosition(mLastDownPositionX, mLastDownPositionY); |
Gilles Debunne | d1dc72a | 2010-11-30 10:16:35 -0800 | [diff] [blame] | 9225 | stopSelectionActionMode(); |
Gilles Debunne | 75beb33 | 2011-04-29 11:40:22 -0700 | [diff] [blame] | 9226 | Selection.setSelection((Spannable) mText, offset); |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 9227 | getInsertionController().showWithActionPopup(); |
Gilles Debunne | 299733e | 2011-02-07 17:11:41 -0800 | [diff] [blame] | 9228 | handled = true; |
Gilles Debunne | e28454a | 2011-09-07 18:03:44 -0700 | [diff] [blame] | 9229 | vibrate = false; |
Gilles Debunne | 9948ad7 | 2010-11-24 14:00:46 -0800 | [diff] [blame] | 9230 | } |
| 9231 | |
Gilles Debunne | 299733e | 2011-02-07 17:11:41 -0800 | [diff] [blame] | 9232 | if (!handled && mSelectionActionMode != null) { |
Gilles Debunne | 0a2aa40 | 2010-11-24 17:57:46 -0800 | [diff] [blame] | 9233 | if (touchPositionIsInSelection()) { |
| 9234 | // Start a drag |
| 9235 | final int start = getSelectionStart(); |
| 9236 | final int end = getSelectionEnd(); |
Gilles Debunne | cf68fee | 2011-09-29 10:55:36 -0700 | [diff] [blame] | 9237 | CharSequence selectedText = getTransformedText(start, end); |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 9238 | ClipData data = ClipData.newPlainText(null, selectedText); |
Gilles Debunne | aaa8479 | 2010-12-03 11:10:14 -0800 | [diff] [blame] | 9239 | DragLocalState localState = new DragLocalState(this, start, end); |
Christopher Tate | 02d2b3b | 2011-01-10 20:43:53 -0800 | [diff] [blame] | 9240 | startDrag(data, getTextThumbnailBuilder(selectedText), localState, 0); |
Gilles Debunne | 0a2aa40 | 2010-11-24 17:57:46 -0800 | [diff] [blame] | 9241 | stopSelectionActionMode(); |
| 9242 | } else { |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 9243 | getSelectionController().hide(); |
Gilles Debunne | 2037b82 | 2011-04-22 13:07:33 -0700 | [diff] [blame] | 9244 | selectCurrentWord(); |
Gilles Debunne | 57324c7 | 2011-08-29 14:42:15 -0700 | [diff] [blame] | 9245 | getSelectionController().show(); |
Gilles Debunne | 0a2aa40 | 2010-11-24 17:57:46 -0800 | [diff] [blame] | 9246 | } |
Gilles Debunne | 299733e | 2011-02-07 17:11:41 -0800 | [diff] [blame] | 9247 | handled = true; |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 9248 | } |
| 9249 | |
Gilles Debunne | 33a8cfb | 2010-12-10 12:00:42 -0800 | [diff] [blame] | 9250 | // Start a new selection |
Gilles Debunne | e28454a | 2011-09-07 18:03:44 -0700 | [diff] [blame] | 9251 | if (!handled) { |
Gilles Debunne | 8f3105f | 2011-10-03 18:28:59 -0700 | [diff] [blame] | 9252 | vibrate = handled = startSelectionActionMode(); |
Gilles Debunne | e28454a | 2011-09-07 18:03:44 -0700 | [diff] [blame] | 9253 | } |
| 9254 | |
| 9255 | if (vibrate) { |
| 9256 | performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); |
| 9257 | } |
Gilles Debunne | 299733e | 2011-02-07 17:11:41 -0800 | [diff] [blame] | 9258 | |
| 9259 | if (handled) { |
Gilles Debunne | 0eb704c | 2010-11-30 12:50:54 -0800 | [diff] [blame] | 9260 | mDiscardNextActionUp = true; |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 9261 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9262 | |
Gilles Debunne | 299733e | 2011-02-07 17:11:41 -0800 | [diff] [blame] | 9263 | return handled; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9264 | } |
| 9265 | |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 9266 | private boolean touchPositionIsInSelection() { |
| 9267 | int selectionStart = getSelectionStart(); |
| 9268 | int selectionEnd = getSelectionEnd(); |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 9269 | |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 9270 | if (selectionStart == selectionEnd) { |
| 9271 | return false; |
| 9272 | } |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 9273 | |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 9274 | if (selectionStart > selectionEnd) { |
| 9275 | int tmp = selectionStart; |
| 9276 | selectionStart = selectionEnd; |
| 9277 | selectionEnd = tmp; |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 9278 | Selection.setSelection((Spannable) mText, selectionStart, selectionEnd); |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 9279 | } |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 9280 | |
Gilles Debunne | e587d83 | 2010-11-23 20:20:11 -0800 | [diff] [blame] | 9281 | SelectionModifierCursorController selectionController = getSelectionController(); |
| 9282 | int minOffset = selectionController.getMinTouchOffset(); |
| 9283 | int maxOffset = selectionController.getMaxTouchOffset(); |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 9284 | |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 9285 | return ((minOffset >= selectionStart) && (maxOffset < selectionEnd)); |
| 9286 | } |
| 9287 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9288 | private PositionListener getPositionListener() { |
| 9289 | if (mPositionListener == null) { |
| 9290 | mPositionListener = new PositionListener(); |
| 9291 | } |
| 9292 | return mPositionListener; |
| 9293 | } |
| 9294 | |
| 9295 | private interface TextViewPositionListener { |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 9296 | public void updatePosition(int parentPositionX, int parentPositionY, |
| 9297 | boolean parentPositionChanged, boolean parentScrolled); |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9298 | } |
| 9299 | |
| 9300 | private class PositionListener implements ViewTreeObserver.OnPreDrawListener { |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 9301 | // 3 handles |
| 9302 | // 3 ActionPopup [replace, suggestion, easyedit] (suggestionsPopup first hides the others) |
| 9303 | private final int MAXIMUM_NUMBER_OF_LISTENERS = 6; |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9304 | private TextViewPositionListener[] mPositionListeners = |
| 9305 | new TextViewPositionListener[MAXIMUM_NUMBER_OF_LISTENERS]; |
| 9306 | private boolean mCanMove[] = new boolean[MAXIMUM_NUMBER_OF_LISTENERS]; |
| 9307 | private boolean mPositionHasChanged = true; |
| 9308 | // Absolute position of the TextView with respect to its parent window |
| 9309 | private int mPositionX, mPositionY; |
| 9310 | private int mNumberOfListeners; |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 9311 | private boolean mScrollHasChanged; |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9312 | |
| 9313 | public void addSubscriber(TextViewPositionListener positionListener, boolean canMove) { |
| 9314 | if (mNumberOfListeners == 0) { |
| 9315 | updatePosition(); |
| 9316 | ViewTreeObserver vto = TextView.this.getViewTreeObserver(); |
| 9317 | vto.addOnPreDrawListener(this); |
| 9318 | } |
| 9319 | |
| 9320 | int emptySlotIndex = -1; |
| 9321 | for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { |
| 9322 | TextViewPositionListener listener = mPositionListeners[i]; |
| 9323 | if (listener == positionListener) { |
| 9324 | return; |
| 9325 | } else if (emptySlotIndex < 0 && listener == null) { |
| 9326 | emptySlotIndex = i; |
| 9327 | } |
| 9328 | } |
| 9329 | |
| 9330 | mPositionListeners[emptySlotIndex] = positionListener; |
| 9331 | mCanMove[emptySlotIndex] = canMove; |
| 9332 | mNumberOfListeners++; |
| 9333 | } |
| 9334 | |
| 9335 | public void removeSubscriber(TextViewPositionListener positionListener) { |
| 9336 | for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { |
| 9337 | if (mPositionListeners[i] == positionListener) { |
| 9338 | mPositionListeners[i] = null; |
| 9339 | mNumberOfListeners--; |
| 9340 | break; |
| 9341 | } |
| 9342 | } |
| 9343 | |
| 9344 | if (mNumberOfListeners == 0) { |
| 9345 | ViewTreeObserver vto = TextView.this.getViewTreeObserver(); |
| 9346 | vto.removeOnPreDrawListener(this); |
| 9347 | } |
| 9348 | } |
| 9349 | |
| 9350 | public int getPositionX() { |
| 9351 | return mPositionX; |
| 9352 | } |
| 9353 | |
| 9354 | public int getPositionY() { |
| 9355 | return mPositionY; |
| 9356 | } |
| 9357 | |
| 9358 | @Override |
| 9359 | public boolean onPreDraw() { |
| 9360 | updatePosition(); |
| 9361 | |
| 9362 | for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 9363 | if (mPositionHasChanged || mScrollHasChanged || mCanMove[i]) { |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9364 | TextViewPositionListener positionListener = mPositionListeners[i]; |
| 9365 | if (positionListener != null) { |
| 9366 | positionListener.updatePosition(mPositionX, mPositionY, |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 9367 | mPositionHasChanged, mScrollHasChanged); |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9368 | } |
| 9369 | } |
| 9370 | } |
| 9371 | |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 9372 | mScrollHasChanged = false; |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9373 | return true; |
| 9374 | } |
| 9375 | |
| 9376 | private void updatePosition() { |
| 9377 | TextView.this.getLocationInWindow(mTempCoords); |
| 9378 | |
| 9379 | mPositionHasChanged = mTempCoords[0] != mPositionX || mTempCoords[1] != mPositionY; |
| 9380 | |
| 9381 | mPositionX = mTempCoords[0]; |
| 9382 | mPositionY = mTempCoords[1]; |
| 9383 | } |
| 9384 | |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 9385 | public void onScrollChanged() { |
| 9386 | mScrollHasChanged = true; |
| 9387 | } |
| 9388 | } |
| 9389 | |
Gilles Debunne | 335c4e6 | 2011-12-01 15:36:08 -0800 | [diff] [blame] | 9390 | private boolean isPositionVisible(int positionX, int positionY) { |
Gilles Debunne | 64901d4 | 2011-11-25 10:23:38 +0100 | [diff] [blame] | 9391 | synchronized (sTmpPosition) { |
| 9392 | final float[] position = sTmpPosition; |
| 9393 | position[0] = positionX; |
| 9394 | position[1] = positionY; |
| 9395 | View view = this; |
| 9396 | |
| 9397 | while (view != null) { |
| 9398 | if (view != this) { |
| 9399 | // Local scroll is already taken into account in positionX/Y |
| 9400 | position[0] -= view.getScrollX(); |
| 9401 | position[1] -= view.getScrollY(); |
| 9402 | } |
| 9403 | |
| 9404 | if (position[0] < 0 || position[1] < 0 || |
| 9405 | position[0] > view.getWidth() || position[1] > view.getHeight()) { |
| 9406 | return false; |
| 9407 | } |
| 9408 | |
| 9409 | if (!view.getMatrix().isIdentity()) { |
| 9410 | view.getMatrix().mapPoints(position); |
| 9411 | } |
| 9412 | |
| 9413 | position[0] += view.getLeft(); |
| 9414 | position[1] += view.getTop(); |
| 9415 | |
| 9416 | final ViewParent parent = view.getParent(); |
| 9417 | if (parent instanceof View) { |
| 9418 | view = (View) parent; |
| 9419 | } else { |
| 9420 | // We've reached the ViewRoot, stop iterating |
| 9421 | view = null; |
| 9422 | } |
| 9423 | } |
| 9424 | } |
| 9425 | |
| 9426 | // We've been able to walk up the view hierarchy and the position was never clipped |
| 9427 | return true; |
| 9428 | } |
| 9429 | |
Gilles Debunne | 335c4e6 | 2011-12-01 15:36:08 -0800 | [diff] [blame] | 9430 | private boolean isOffsetVisible(int offset) { |
Gilles Debunne | 64901d4 | 2011-11-25 10:23:38 +0100 | [diff] [blame] | 9431 | final int line = mLayout.getLineForOffset(offset); |
| 9432 | final int lineBottom = mLayout.getLineBottom(line); |
| 9433 | final int primaryHorizontal = (int) mLayout.getPrimaryHorizontal(offset); |
| 9434 | return isPositionVisible(primaryHorizontal + viewportToContentHorizontalOffset(), |
| 9435 | lineBottom + viewportToContentVerticalOffset()); |
| 9436 | } |
| 9437 | |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 9438 | @Override |
| 9439 | protected void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) { |
| 9440 | super.onScrollChanged(horiz, vert, oldHoriz, oldVert); |
| 9441 | if (mPositionListener != null) { |
| 9442 | mPositionListener.onScrollChanged(); |
| 9443 | } |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9444 | } |
| 9445 | |
| 9446 | private abstract class PinnedPopupWindow implements TextViewPositionListener { |
| 9447 | protected PopupWindow mPopupWindow; |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9448 | protected ViewGroup mContentView; |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9449 | int mPositionX, mPositionY; |
| 9450 | |
| 9451 | protected abstract void createPopupWindow(); |
| 9452 | protected abstract void initContentView(); |
| 9453 | protected abstract int getTextOffset(); |
| 9454 | protected abstract int getVerticalLocalPosition(int line); |
| 9455 | protected abstract int clipVertically(int positionY); |
| 9456 | |
| 9457 | public PinnedPopupWindow() { |
| 9458 | createPopupWindow(); |
| 9459 | |
| 9460 | mPopupWindow.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL); |
| 9461 | mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); |
| 9462 | mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); |
| 9463 | |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9464 | initContentView(); |
| 9465 | |
| 9466 | LayoutParams wrapContent = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, |
| 9467 | ViewGroup.LayoutParams.WRAP_CONTENT); |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9468 | mContentView.setLayoutParams(wrapContent); |
| 9469 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9470 | mPopupWindow.setContentView(mContentView); |
| 9471 | } |
| 9472 | |
| 9473 | public void show() { |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 9474 | TextView.this.getPositionListener().addSubscriber(this, false /* offset is fixed */); |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9475 | |
| 9476 | computeLocalPosition(); |
| 9477 | |
| 9478 | final PositionListener positionListener = TextView.this.getPositionListener(); |
| 9479 | updatePosition(positionListener.getPositionX(), positionListener.getPositionY()); |
| 9480 | } |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9481 | |
| 9482 | protected void measureContent() { |
| 9483 | final DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); |
| 9484 | mContentView.measure( |
| 9485 | View.MeasureSpec.makeMeasureSpec(displayMetrics.widthPixels, |
| 9486 | View.MeasureSpec.AT_MOST), |
| 9487 | View.MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels, |
| 9488 | View.MeasureSpec.AT_MOST)); |
| 9489 | } |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9490 | |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9491 | /* The popup window will be horizontally centered on the getTextOffset() and vertically |
| 9492 | * positioned according to viewportToContentHorizontalOffset. |
| 9493 | * |
| 9494 | * This method assumes that mContentView has properly been measured from its content. */ |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9495 | private void computeLocalPosition() { |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9496 | measureContent(); |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9497 | final int width = mContentView.getMeasuredWidth(); |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9498 | final int offset = getTextOffset(); |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9499 | mPositionX = (int) (mLayout.getPrimaryHorizontal(offset) - width / 2.0f); |
| 9500 | mPositionX += viewportToContentHorizontalOffset(); |
| 9501 | |
| 9502 | final int line = mLayout.getLineForOffset(offset); |
| 9503 | mPositionY = getVerticalLocalPosition(line); |
| 9504 | mPositionY += viewportToContentVerticalOffset(); |
| 9505 | } |
| 9506 | |
| 9507 | private void updatePosition(int parentPositionX, int parentPositionY) { |
| 9508 | int positionX = parentPositionX + mPositionX; |
| 9509 | int positionY = parentPositionY + mPositionY; |
| 9510 | |
| 9511 | positionY = clipVertically(positionY); |
| 9512 | |
| 9513 | // Horizontal clipping |
| 9514 | final DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); |
| 9515 | final int width = mContentView.getMeasuredWidth(); |
| 9516 | positionX = Math.min(displayMetrics.widthPixels - width, positionX); |
| 9517 | positionX = Math.max(0, positionX); |
| 9518 | |
| 9519 | if (isShowing()) { |
| 9520 | mPopupWindow.update(positionX, positionY, -1, -1); |
| 9521 | } else { |
| 9522 | mPopupWindow.showAtLocation(TextView.this, Gravity.NO_GRAVITY, |
| 9523 | positionX, positionY); |
| 9524 | } |
| 9525 | } |
| 9526 | |
| 9527 | public void hide() { |
| 9528 | mPopupWindow.dismiss(); |
| 9529 | TextView.this.getPositionListener().removeSubscriber(this); |
| 9530 | } |
| 9531 | |
| 9532 | @Override |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 9533 | public void updatePosition(int parentPositionX, int parentPositionY, |
| 9534 | boolean parentPositionChanged, boolean parentScrolled) { |
| 9535 | // Either parentPositionChanged or parentScrolled is true, check if still visible |
Gilles Debunne | 64901d4 | 2011-11-25 10:23:38 +0100 | [diff] [blame] | 9536 | if (isShowing() && isOffsetVisible(getTextOffset())) { |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 9537 | if (parentScrolled) computeLocalPosition(); |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9538 | updatePosition(parentPositionX, parentPositionY); |
| 9539 | } else { |
| 9540 | hide(); |
| 9541 | } |
| 9542 | } |
| 9543 | |
| 9544 | public boolean isShowing() { |
| 9545 | return mPopupWindow.isShowing(); |
| 9546 | } |
| 9547 | } |
| 9548 | |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9549 | private class SuggestionsPopupWindow extends PinnedPopupWindow implements OnItemClickListener { |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 9550 | private static final int MAX_NUMBER_SUGGESTIONS = SuggestionSpan.SUGGESTIONS_MAX_SIZE; |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9551 | private static final int ADD_TO_DICTIONARY = -1; |
| 9552 | private static final int DELETE_TEXT = -2; |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9553 | private SuggestionInfo[] mSuggestionInfos; |
| 9554 | private int mNumberOfSuggestions; |
Gilles Debunne | 28294cc | 2011-08-24 12:02:05 -0700 | [diff] [blame] | 9555 | private boolean mCursorWasVisibleBeforeSuggestions; |
Gilles Debunne | 26c8b3a | 2011-10-12 14:06:58 -0700 | [diff] [blame] | 9556 | private boolean mIsShowingUp = false; |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9557 | private SuggestionAdapter mSuggestionsAdapter; |
Gilles Debunne | c9fd978 | 2011-09-09 09:20:12 -0700 | [diff] [blame] | 9558 | private final Comparator<SuggestionSpan> mSuggestionSpanComparator; |
| 9559 | private final HashMap<SuggestionSpan, Integer> mSpansLengths; |
| 9560 | |
Gilles Debunne | 28294cc | 2011-08-24 12:02:05 -0700 | [diff] [blame] | 9561 | private class CustomPopupWindow extends PopupWindow { |
| 9562 | public CustomPopupWindow(Context context, int defStyle) { |
| 9563 | super(context, null, defStyle); |
| 9564 | } |
| 9565 | |
| 9566 | @Override |
| 9567 | public void dismiss() { |
| 9568 | super.dismiss(); |
| 9569 | |
Fabrice Di Meglio | 03e4d64 | 2011-09-06 19:06:06 -0700 | [diff] [blame] | 9570 | TextView.this.getPositionListener().removeSubscriber(SuggestionsPopupWindow.this); |
| 9571 | |
Gilles Debunne | 186aaf9 | 2011-09-16 14:26:12 -0700 | [diff] [blame] | 9572 | // Safe cast since show() checks that mText is an Editable |
| 9573 | ((Spannable) mText).removeSpan(mSuggestionRangeSpan); |
Gilles Debunne | 28294cc | 2011-08-24 12:02:05 -0700 | [diff] [blame] | 9574 | |
| 9575 | setCursorVisible(mCursorWasVisibleBeforeSuggestions); |
| 9576 | if (hasInsertionController()) { |
| 9577 | getInsertionController().show(); |
| 9578 | } |
| 9579 | } |
| 9580 | } |
| 9581 | |
| 9582 | public SuggestionsPopupWindow() { |
Gilles Debunne | 28294cc | 2011-08-24 12:02:05 -0700 | [diff] [blame] | 9583 | mCursorWasVisibleBeforeSuggestions = mCursorVisible; |
Gilles Debunne | c9fd978 | 2011-09-09 09:20:12 -0700 | [diff] [blame] | 9584 | mSuggestionSpanComparator = new SuggestionSpanComparator(); |
| 9585 | mSpansLengths = new HashMap<SuggestionSpan, Integer>(); |
Gilles Debunne | 28294cc | 2011-08-24 12:02:05 -0700 | [diff] [blame] | 9586 | } |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 9587 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9588 | @Override |
| 9589 | protected void createPopupWindow() { |
Gilles Debunne | 28294cc | 2011-08-24 12:02:05 -0700 | [diff] [blame] | 9590 | mPopupWindow = new CustomPopupWindow(TextView.this.mContext, |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9591 | com.android.internal.R.attr.textSuggestionsWindowStyle); |
Gilles Debunne | 2888524 | 2011-07-25 18:56:09 -0700 | [diff] [blame] | 9592 | mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); |
Gilles Debunne | 28294cc | 2011-08-24 12:02:05 -0700 | [diff] [blame] | 9593 | mPopupWindow.setFocusable(true); |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9594 | mPopupWindow.setClippingEnabled(false); |
| 9595 | } |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 9596 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9597 | @Override |
| 9598 | protected void initContentView() { |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9599 | ListView listView = new ListView(TextView.this.getContext()); |
| 9600 | mSuggestionsAdapter = new SuggestionAdapter(); |
| 9601 | listView.setAdapter(mSuggestionsAdapter); |
| 9602 | listView.setOnItemClickListener(this); |
| 9603 | mContentView = listView; |
Gilles Debunne | 2888524 | 2011-07-25 18:56:09 -0700 | [diff] [blame] | 9604 | |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9605 | // Inflate the suggestion items once and for all. + 2 for add to dictionary and delete |
| 9606 | mSuggestionInfos = new SuggestionInfo[MAX_NUMBER_SUGGESTIONS + 2]; |
| 9607 | for (int i = 0; i < mSuggestionInfos.length; i++) { |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9608 | mSuggestionInfos[i] = new SuggestionInfo(); |
Gilles Debunne | 2888524 | 2011-07-25 18:56:09 -0700 | [diff] [blame] | 9609 | } |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 9610 | } |
| 9611 | |
Gilles Debunne | 26c8b3a | 2011-10-12 14:06:58 -0700 | [diff] [blame] | 9612 | public boolean isShowingUp() { |
| 9613 | return mIsShowingUp; |
| 9614 | } |
| 9615 | |
| 9616 | public void onParentLostFocus() { |
| 9617 | mIsShowingUp = false; |
| 9618 | } |
| 9619 | |
Gilles Debunne | 214a862 | 2011-04-26 15:44:37 -0700 | [diff] [blame] | 9620 | private class SuggestionInfo { |
Gilles Debunne | bd4016e9 | 2011-09-14 10:10:41 -0700 | [diff] [blame] | 9621 | int suggestionStart, suggestionEnd; // range of actual suggestion within text |
Gilles Debunne | ee511cc | 2011-05-05 14:57:50 -0700 | [diff] [blame] | 9622 | SuggestionSpan suggestionSpan; // the SuggestionSpan that this TextView represents |
Gilles Debunne | bd4016e9 | 2011-09-14 10:10:41 -0700 | [diff] [blame] | 9623 | int suggestionIndex; // the index of this suggestion inside suggestionSpan |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9624 | SpannableStringBuilder text = new SpannableStringBuilder(); |
Luca Zanolin | 2346e01 | 2011-09-06 22:56:47 +0100 | [diff] [blame] | 9625 | TextAppearanceSpan highlightSpan = new TextAppearanceSpan(mContext, |
| 9626 | android.R.style.TextAppearance_SuggestionHighlight); |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9627 | } |
| 9628 | |
| 9629 | private class SuggestionAdapter extends BaseAdapter { |
| 9630 | private LayoutInflater mInflater = (LayoutInflater) TextView.this.mContext. |
| 9631 | getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 9632 | |
| 9633 | @Override |
| 9634 | public int getCount() { |
| 9635 | return mNumberOfSuggestions; |
| 9636 | } |
| 9637 | |
| 9638 | @Override |
| 9639 | public Object getItem(int position) { |
| 9640 | return mSuggestionInfos[position]; |
| 9641 | } |
| 9642 | |
| 9643 | @Override |
| 9644 | public long getItemId(int position) { |
| 9645 | return position; |
| 9646 | } |
| 9647 | |
| 9648 | @Override |
| 9649 | public View getView(int position, View convertView, ViewGroup parent) { |
| 9650 | TextView textView = (TextView) convertView; |
| 9651 | |
| 9652 | if (textView == null) { |
| 9653 | textView = (TextView) mInflater.inflate(mTextEditSuggestionItemLayout, parent, |
| 9654 | false); |
| 9655 | } |
| 9656 | |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9657 | final SuggestionInfo suggestionInfo = mSuggestionInfos[position]; |
| 9658 | textView.setText(suggestionInfo.text); |
| 9659 | |
| 9660 | if (suggestionInfo.suggestionIndex == ADD_TO_DICTIONARY) { |
| 9661 | textView.setCompoundDrawablesWithIntrinsicBounds( |
| 9662 | com.android.internal.R.drawable.ic_suggestions_add, 0, 0, 0); |
| 9663 | } else if (suggestionInfo.suggestionIndex == DELETE_TEXT) { |
| 9664 | textView.setCompoundDrawablesWithIntrinsicBounds( |
| 9665 | com.android.internal.R.drawable.ic_suggestions_delete, 0, 0, 0); |
| 9666 | } else { |
| 9667 | textView.setCompoundDrawables(null, null, null, null); |
| 9668 | } |
| 9669 | |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9670 | return textView; |
| 9671 | } |
Gilles Debunne | 214a862 | 2011-04-26 15:44:37 -0700 | [diff] [blame] | 9672 | } |
| 9673 | |
Gilles Debunne | c9fd978 | 2011-09-09 09:20:12 -0700 | [diff] [blame] | 9674 | private class SuggestionSpanComparator implements Comparator<SuggestionSpan> { |
| 9675 | public int compare(SuggestionSpan span1, SuggestionSpan span2) { |
| 9676 | final int flag1 = span1.getFlags(); |
| 9677 | final int flag2 = span2.getFlags(); |
| 9678 | if (flag1 != flag2) { |
| 9679 | // The order here should match what is used in updateDrawState |
| 9680 | final boolean easy1 = (flag1 & SuggestionSpan.FLAG_EASY_CORRECT) != 0; |
| 9681 | final boolean easy2 = (flag2 & SuggestionSpan.FLAG_EASY_CORRECT) != 0; |
| 9682 | final boolean misspelled1 = (flag1 & SuggestionSpan.FLAG_MISSPELLED) != 0; |
| 9683 | final boolean misspelled2 = (flag2 & SuggestionSpan.FLAG_MISSPELLED) != 0; |
| 9684 | if (easy1 && !misspelled1) return -1; |
| 9685 | if (easy2 && !misspelled2) return 1; |
| 9686 | if (misspelled1) return -1; |
| 9687 | if (misspelled2) return 1; |
| 9688 | } |
| 9689 | |
| 9690 | return mSpansLengths.get(span1).intValue() - mSpansLengths.get(span2).intValue(); |
| 9691 | } |
| 9692 | } |
| 9693 | |
Luca Zanolin | e3f89c0 | 2011-08-01 09:55:17 +0100 | [diff] [blame] | 9694 | /** |
| 9695 | * Returns the suggestion spans that cover the current cursor position. The suggestion |
| 9696 | * spans are sorted according to the length of text that they are attached to. |
| 9697 | */ |
| 9698 | private SuggestionSpan[] getSuggestionSpans() { |
| 9699 | int pos = TextView.this.getSelectionStart(); |
| 9700 | Spannable spannable = (Spannable) TextView.this.mText; |
| 9701 | SuggestionSpan[] suggestionSpans = spannable.getSpans(pos, pos, SuggestionSpan.class); |
| 9702 | |
Gilles Debunne | c9fd978 | 2011-09-09 09:20:12 -0700 | [diff] [blame] | 9703 | mSpansLengths.clear(); |
Luca Zanolin | e3f89c0 | 2011-08-01 09:55:17 +0100 | [diff] [blame] | 9704 | for (SuggestionSpan suggestionSpan : suggestionSpans) { |
| 9705 | int start = spannable.getSpanStart(suggestionSpan); |
| 9706 | int end = spannable.getSpanEnd(suggestionSpan); |
Gilles Debunne | c9fd978 | 2011-09-09 09:20:12 -0700 | [diff] [blame] | 9707 | mSpansLengths.put(suggestionSpan, Integer.valueOf(end - start)); |
Luca Zanolin | e3f89c0 | 2011-08-01 09:55:17 +0100 | [diff] [blame] | 9708 | } |
| 9709 | |
Gilles Debunne | c9fd978 | 2011-09-09 09:20:12 -0700 | [diff] [blame] | 9710 | // The suggestions are sorted according to their types (easy correction first, then |
| 9711 | // misspelled) and to the length of the text that they cover (shorter first). |
| 9712 | Arrays.sort(suggestionSpans, mSuggestionSpanComparator); |
Luca Zanolin | e3f89c0 | 2011-08-01 09:55:17 +0100 | [diff] [blame] | 9713 | return suggestionSpans; |
| 9714 | } |
| 9715 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9716 | @Override |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 9717 | public void show() { |
| 9718 | if (!(mText instanceof Editable)) return; |
| 9719 | |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9720 | updateSuggestions(); |
| 9721 | mCursorWasVisibleBeforeSuggestions = mCursorVisible; |
| 9722 | setCursorVisible(false); |
Gilles Debunne | 26c8b3a | 2011-10-12 14:06:58 -0700 | [diff] [blame] | 9723 | mIsShowingUp = true; |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9724 | super.show(); |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9725 | } |
| 9726 | |
| 9727 | @Override |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9728 | protected void measureContent() { |
| 9729 | final DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); |
| 9730 | final int horizontalMeasure = View.MeasureSpec.makeMeasureSpec( |
| 9731 | displayMetrics.widthPixels, View.MeasureSpec.AT_MOST); |
| 9732 | final int verticalMeasure = View.MeasureSpec.makeMeasureSpec( |
| 9733 | displayMetrics.heightPixels, View.MeasureSpec.AT_MOST); |
| 9734 | |
| 9735 | int width = 0; |
| 9736 | View view = null; |
| 9737 | for (int i = 0; i < mNumberOfSuggestions; i++) { |
| 9738 | view = mSuggestionsAdapter.getView(i, view, mContentView); |
Luca Zanolin | 58707d6 | 2011-09-28 09:27:49 +0100 | [diff] [blame] | 9739 | view.getLayoutParams().width = LayoutParams.WRAP_CONTENT; |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9740 | view.measure(horizontalMeasure, verticalMeasure); |
| 9741 | width = Math.max(width, view.getMeasuredWidth()); |
| 9742 | } |
| 9743 | |
| 9744 | // Enforce the width based on actual text widths |
| 9745 | mContentView.measure( |
| 9746 | View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), |
| 9747 | verticalMeasure); |
| 9748 | |
| 9749 | Drawable popupBackground = mPopupWindow.getBackground(); |
| 9750 | if (popupBackground != null) { |
| 9751 | if (mTempRect == null) mTempRect = new Rect(); |
| 9752 | popupBackground.getPadding(mTempRect); |
| 9753 | width += mTempRect.left + mTempRect.right; |
| 9754 | } |
| 9755 | mPopupWindow.setWidth(width); |
| 9756 | } |
| 9757 | |
| 9758 | @Override |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9759 | protected int getTextOffset() { |
| 9760 | return getSelectionStart(); |
| 9761 | } |
| 9762 | |
| 9763 | @Override |
| 9764 | protected int getVerticalLocalPosition(int line) { |
| 9765 | return mLayout.getLineBottom(line); |
| 9766 | } |
| 9767 | |
| 9768 | @Override |
| 9769 | protected int clipVertically(int positionY) { |
| 9770 | final int height = mContentView.getMeasuredHeight(); |
| 9771 | final DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); |
| 9772 | return Math.min(positionY, displayMetrics.heightPixels - height); |
| 9773 | } |
| 9774 | |
| 9775 | @Override |
| 9776 | public void hide() { |
| 9777 | super.hide(); |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 9778 | } |
| 9779 | |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9780 | private void updateSuggestions() { |
Gilles Debunne | fa4e2d9 | 2011-09-08 18:34:22 -0700 | [diff] [blame] | 9781 | Spannable spannable = (Spannable) TextView.this.mText; |
Luca Zanolin | e3f89c0 | 2011-08-01 09:55:17 +0100 | [diff] [blame] | 9782 | SuggestionSpan[] suggestionSpans = getSuggestionSpans(); |
| 9783 | |
satok | b3fc1a5 | 2011-04-06 18:28:55 +0900 | [diff] [blame] | 9784 | final int nbSpans = suggestionSpans.length; |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 9785 | |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9786 | mNumberOfSuggestions = 0; |
Gilles Debunne | 214a862 | 2011-04-26 15:44:37 -0700 | [diff] [blame] | 9787 | int spanUnionStart = mText.length(); |
| 9788 | int spanUnionEnd = 0; |
| 9789 | |
Gilles Debunne | e90bed1 | 2011-08-30 14:28:27 -0700 | [diff] [blame] | 9790 | SuggestionSpan misspelledSpan = null; |
Gilles Debunne | fa4e2d9 | 2011-09-08 18:34:22 -0700 | [diff] [blame] | 9791 | int underlineColor = 0; |
Gilles Debunne | e90bed1 | 2011-08-30 14:28:27 -0700 | [diff] [blame] | 9792 | |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 9793 | for (int spanIndex = 0; spanIndex < nbSpans; spanIndex++) { |
satok | b3fc1a5 | 2011-04-06 18:28:55 +0900 | [diff] [blame] | 9794 | SuggestionSpan suggestionSpan = suggestionSpans[spanIndex]; |
| 9795 | final int spanStart = spannable.getSpanStart(suggestionSpan); |
| 9796 | final int spanEnd = spannable.getSpanEnd(suggestionSpan); |
Gilles Debunne | 214a862 | 2011-04-26 15:44:37 -0700 | [diff] [blame] | 9797 | spanUnionStart = Math.min(spanStart, spanUnionStart); |
| 9798 | spanUnionEnd = Math.max(spanEnd, spanUnionEnd); |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 9799 | |
Gilles Debunne | e90bed1 | 2011-08-30 14:28:27 -0700 | [diff] [blame] | 9800 | if ((suggestionSpan.getFlags() & SuggestionSpan.FLAG_MISSPELLED) != 0) { |
| 9801 | misspelledSpan = suggestionSpan; |
| 9802 | } |
| 9803 | |
Gilles Debunne | fa4e2d9 | 2011-09-08 18:34:22 -0700 | [diff] [blame] | 9804 | // The first span dictates the background color of the highlighted text |
| 9805 | if (spanIndex == 0) underlineColor = suggestionSpan.getUnderlineColor(); |
| 9806 | |
satok | b3fc1a5 | 2011-04-06 18:28:55 +0900 | [diff] [blame] | 9807 | String[] suggestions = suggestionSpan.getSuggestions(); |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 9808 | int nbSuggestions = suggestions.length; |
| 9809 | for (int suggestionIndex = 0; suggestionIndex < nbSuggestions; suggestionIndex++) { |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9810 | SuggestionInfo suggestionInfo = mSuggestionInfos[mNumberOfSuggestions]; |
Gilles Debunne | ee511cc | 2011-05-05 14:57:50 -0700 | [diff] [blame] | 9811 | suggestionInfo.suggestionSpan = suggestionSpan; |
| 9812 | suggestionInfo.suggestionIndex = suggestionIndex; |
Gilles Debunne | e90bed1 | 2011-08-30 14:28:27 -0700 | [diff] [blame] | 9813 | suggestionInfo.text.replace(0, suggestionInfo.text.length(), |
| 9814 | suggestions[suggestionIndex]); |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 9815 | |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9816 | mNumberOfSuggestions++; |
| 9817 | if (mNumberOfSuggestions == MAX_NUMBER_SUGGESTIONS) { |
Gilles Debunne | 214a862 | 2011-04-26 15:44:37 -0700 | [diff] [blame] | 9818 | // Also end outer for loop |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 9819 | spanIndex = nbSpans; |
| 9820 | break; |
| 9821 | } |
| 9822 | } |
| 9823 | } |
| 9824 | |
Gilles Debunne | e90bed1 | 2011-08-30 14:28:27 -0700 | [diff] [blame] | 9825 | for (int i = 0; i < mNumberOfSuggestions; i++) { |
| 9826 | highlightTextDifferences(mSuggestionInfos[i], spanUnionStart, spanUnionEnd); |
| 9827 | } |
| 9828 | |
Gilles Debunne | e300be9 | 2011-12-06 10:15:56 -0800 | [diff] [blame] | 9829 | // Add to dictionary item if there is a span with the misspelled flag |
Gilles Debunne | e90bed1 | 2011-08-30 14:28:27 -0700 | [diff] [blame] | 9830 | if (misspelledSpan != null) { |
| 9831 | final int misspelledStart = spannable.getSpanStart(misspelledSpan); |
| 9832 | final int misspelledEnd = spannable.getSpanEnd(misspelledSpan); |
| 9833 | if (misspelledStart >= 0 && misspelledEnd > misspelledStart) { |
| 9834 | SuggestionInfo suggestionInfo = mSuggestionInfos[mNumberOfSuggestions]; |
Gilles Debunne | e90bed1 | 2011-08-30 14:28:27 -0700 | [diff] [blame] | 9835 | suggestionInfo.suggestionSpan = misspelledSpan; |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9836 | suggestionInfo.suggestionIndex = ADD_TO_DICTIONARY; |
Gilles Debunne | e90bed1 | 2011-08-30 14:28:27 -0700 | [diff] [blame] | 9837 | suggestionInfo.text.replace(0, suggestionInfo.text.length(), |
| 9838 | getContext().getString(com.android.internal.R.string.addToDictionary)); |
Gilles Debunne | e670101 | 2011-09-29 10:33:19 -0700 | [diff] [blame] | 9839 | suggestionInfo.text.setSpan(suggestionInfo.highlightSpan, 0, 0, |
| 9840 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Gilles Debunne | e90bed1 | 2011-08-30 14:28:27 -0700 | [diff] [blame] | 9841 | |
| 9842 | mNumberOfSuggestions++; |
| 9843 | } |
| 9844 | } |
| 9845 | |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9846 | // Delete item |
| 9847 | SuggestionInfo suggestionInfo = mSuggestionInfos[mNumberOfSuggestions]; |
| 9848 | suggestionInfo.suggestionSpan = null; |
| 9849 | suggestionInfo.suggestionIndex = DELETE_TEXT; |
| 9850 | suggestionInfo.text.replace(0, suggestionInfo.text.length(), |
| 9851 | getContext().getString(com.android.internal.R.string.deleteText)); |
Gilles Debunne | e670101 | 2011-09-29 10:33:19 -0700 | [diff] [blame] | 9852 | suggestionInfo.text.setSpan(suggestionInfo.highlightSpan, 0, 0, |
| 9853 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9854 | mNumberOfSuggestions++; |
Gilles Debunne | 214a862 | 2011-04-26 15:44:37 -0700 | [diff] [blame] | 9855 | |
Gilles Debunne | fa4e2d9 | 2011-09-08 18:34:22 -0700 | [diff] [blame] | 9856 | if (mSuggestionRangeSpan == null) mSuggestionRangeSpan = new SuggestionRangeSpan(); |
| 9857 | if (underlineColor == 0) { |
| 9858 | // Fallback on the default highlight color when the first span does not provide one |
| 9859 | mSuggestionRangeSpan.setBackgroundColor(mHighlightColor); |
| 9860 | } else { |
Gilles Debunne | c2deadc | 2011-09-30 10:43:43 -0700 | [diff] [blame] | 9861 | final float BACKGROUND_TRANSPARENCY = 0.4f; |
Gilles Debunne | fa4e2d9 | 2011-09-08 18:34:22 -0700 | [diff] [blame] | 9862 | final int newAlpha = (int) (Color.alpha(underlineColor) * BACKGROUND_TRANSPARENCY); |
| 9863 | mSuggestionRangeSpan.setBackgroundColor( |
| 9864 | (underlineColor & 0x00FFFFFF) + (newAlpha << 24)); |
| 9865 | } |
| 9866 | spannable.setSpan(mSuggestionRangeSpan, spanUnionStart, spanUnionEnd, |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 9867 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 9868 | |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9869 | mSuggestionsAdapter.notifyDataSetChanged(); |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 9870 | } |
| 9871 | |
Luca Zanolin | 2346e01 | 2011-09-06 22:56:47 +0100 | [diff] [blame] | 9872 | private void highlightTextDifferences(SuggestionInfo suggestionInfo, int unionStart, |
| 9873 | int unionEnd) { |
Gilles Debunne | bd4016e9 | 2011-09-14 10:10:41 -0700 | [diff] [blame] | 9874 | final Spannable text = (Spannable) mText; |
| 9875 | final int spanStart = text.getSpanStart(suggestionInfo.suggestionSpan); |
| 9876 | final int spanEnd = text.getSpanEnd(suggestionInfo.suggestionSpan); |
Gilles Debunne | 214a862 | 2011-04-26 15:44:37 -0700 | [diff] [blame] | 9877 | |
Luca Zanolin | 2346e01 | 2011-09-06 22:56:47 +0100 | [diff] [blame] | 9878 | // Adjust the start/end of the suggestion span |
| 9879 | suggestionInfo.suggestionStart = spanStart - unionStart; |
| 9880 | suggestionInfo.suggestionEnd = suggestionInfo.suggestionStart |
| 9881 | + suggestionInfo.text.length(); |
Gilles Debunne | e670101 | 2011-09-29 10:33:19 -0700 | [diff] [blame] | 9882 | |
Luca Zanolin | 2346e01 | 2011-09-06 22:56:47 +0100 | [diff] [blame] | 9883 | suggestionInfo.text.setSpan(suggestionInfo.highlightSpan, 0, |
| 9884 | suggestionInfo.text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Gilles Debunne | 214a862 | 2011-04-26 15:44:37 -0700 | [diff] [blame] | 9885 | |
Luca Zanolin | 2346e01 | 2011-09-06 22:56:47 +0100 | [diff] [blame] | 9886 | // Add the text before and after the span. |
Gilles Debunne | e1fc4f6 | 2011-10-03 17:01:19 -0700 | [diff] [blame] | 9887 | suggestionInfo.text.insert(0, mText.toString().substring(unionStart, spanStart)); |
| 9888 | suggestionInfo.text.append(mText.toString().substring(spanEnd, unionEnd)); |
Gilles Debunne | 214a862 | 2011-04-26 15:44:37 -0700 | [diff] [blame] | 9889 | } |
| 9890 | |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 9891 | @Override |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 9892 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9893 | Editable editable = (Editable) mText; |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9894 | SuggestionInfo suggestionInfo = mSuggestionInfos[position]; |
Gilles Debunne | e90bed1 | 2011-08-30 14:28:27 -0700 | [diff] [blame] | 9895 | |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9896 | if (suggestionInfo.suggestionIndex == DELETE_TEXT) { |
| 9897 | final int spanUnionStart = editable.getSpanStart(mSuggestionRangeSpan); |
| 9898 | int spanUnionEnd = editable.getSpanEnd(mSuggestionRangeSpan); |
Gilles Debunne | 28ef904 | 2011-10-11 16:01:22 -0700 | [diff] [blame] | 9899 | if (spanUnionStart >= 0 && spanUnionEnd > spanUnionStart) { |
| 9900 | // Do not leave two adjacent spaces after deletion, or one at beginning of text |
| 9901 | if (spanUnionEnd < editable.length() && |
| 9902 | Character.isSpaceChar(editable.charAt(spanUnionEnd)) && |
| 9903 | (spanUnionStart == 0 || |
| 9904 | Character.isSpaceChar(editable.charAt(spanUnionStart - 1)))) { |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9905 | spanUnionEnd = spanUnionEnd + 1; |
Gilles Debunne | 28ef904 | 2011-10-11 16:01:22 -0700 | [diff] [blame] | 9906 | } |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 9907 | deleteText_internal(spanUnionStart, spanUnionEnd); |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 9908 | } |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9909 | hide(); |
| 9910 | return; |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 9911 | } |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9912 | |
| 9913 | final int spanStart = editable.getSpanStart(suggestionInfo.suggestionSpan); |
| 9914 | final int spanEnd = editable.getSpanEnd(suggestionInfo.suggestionSpan); |
Gilles Debunne | e300be9 | 2011-12-06 10:15:56 -0800 | [diff] [blame] | 9915 | if (spanStart < 0 || spanEnd <= spanStart) { |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9916 | // Span has been removed |
| 9917 | hide(); |
| 9918 | return; |
| 9919 | } |
Gilles Debunne | e1fc4f6 | 2011-10-03 17:01:19 -0700 | [diff] [blame] | 9920 | final String originalText = mText.toString().substring(spanStart, spanEnd); |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9921 | |
| 9922 | if (suggestionInfo.suggestionIndex == ADD_TO_DICTIONARY) { |
| 9923 | Intent intent = new Intent(Settings.ACTION_USER_DICTIONARY_INSERT); |
| 9924 | intent.putExtra("word", originalText); |
Jean Chalard | 5fa6737 | 2011-12-07 13:57:59 +0900 | [diff] [blame] | 9925 | intent.putExtra("locale", getTextServicesLocale().toString()); |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9926 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 9927 | getContext().startActivity(intent); |
Gilles Debunne | 845d9c7 | 2011-09-23 11:09:46 -0700 | [diff] [blame] | 9928 | // There is no way to know if the word was indeed added. Re-check. |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 9929 | // TODO The ExtractEditText should remove the span in the original text instead |
Gilles Debunne | 845d9c7 | 2011-09-23 11:09:46 -0700 | [diff] [blame] | 9930 | editable.removeSpan(suggestionInfo.suggestionSpan); |
Gilles Debunne | c115fa0 | 2011-12-07 13:38:31 -0800 | [diff] [blame] | 9931 | updateSpellCheckSpans(spanStart, spanEnd, false); |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9932 | } else { |
| 9933 | // SuggestionSpans are removed by replace: save them before |
| 9934 | SuggestionSpan[] suggestionSpans = editable.getSpans(spanStart, spanEnd, |
| 9935 | SuggestionSpan.class); |
| 9936 | final int length = suggestionSpans.length; |
| 9937 | int[] suggestionSpansStarts = new int[length]; |
| 9938 | int[] suggestionSpansEnds = new int[length]; |
| 9939 | int[] suggestionSpansFlags = new int[length]; |
| 9940 | for (int i = 0; i < length; i++) { |
| 9941 | final SuggestionSpan suggestionSpan = suggestionSpans[i]; |
| 9942 | suggestionSpansStarts[i] = editable.getSpanStart(suggestionSpan); |
| 9943 | suggestionSpansEnds[i] = editable.getSpanEnd(suggestionSpan); |
| 9944 | suggestionSpansFlags[i] = editable.getSpanFlags(suggestionSpan); |
Gilles Debunne | 5915c88 | 2011-10-10 18:17:22 -0700 | [diff] [blame] | 9945 | |
| 9946 | // Remove potential misspelled flags |
| 9947 | int suggestionSpanFlags = suggestionSpan.getFlags(); |
| 9948 | if ((suggestionSpanFlags & SuggestionSpan.FLAG_MISSPELLED) > 0) { |
| 9949 | suggestionSpanFlags &= ~SuggestionSpan.FLAG_MISSPELLED; |
| 9950 | suggestionSpanFlags &= ~SuggestionSpan.FLAG_EASY_CORRECT; |
| 9951 | suggestionSpan.setFlags(suggestionSpanFlags); |
| 9952 | } |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9953 | } |
| 9954 | |
| 9955 | final int suggestionStart = suggestionInfo.suggestionStart; |
| 9956 | final int suggestionEnd = suggestionInfo.suggestionEnd; |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 9957 | final String suggestion = suggestionInfo.text.subSequence( |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9958 | suggestionStart, suggestionEnd).toString(); |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 9959 | replaceText_internal(spanStart, spanEnd, suggestion); |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9960 | |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9961 | // Notify source IME of the suggestion pick. Do this before swaping texts. |
| 9962 | if (!TextUtils.isEmpty( |
| 9963 | suggestionInfo.suggestionSpan.getNotificationTargetClassName())) { |
| 9964 | InputMethodManager imm = InputMethodManager.peekInstance(); |
Gilles Debunne | e1fc4f6 | 2011-10-03 17:01:19 -0700 | [diff] [blame] | 9965 | if (imm != null) { |
| 9966 | imm.notifySuggestionPicked(suggestionInfo.suggestionSpan, originalText, |
| 9967 | suggestionInfo.suggestionIndex); |
| 9968 | } |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9969 | } |
| 9970 | |
| 9971 | // Swap text content between actual text and Suggestion span |
| 9972 | String[] suggestions = suggestionInfo.suggestionSpan.getSuggestions(); |
| 9973 | suggestions[suggestionInfo.suggestionIndex] = originalText; |
| 9974 | |
| 9975 | // Restore previous SuggestionSpans |
| 9976 | final int lengthDifference = suggestion.length() - (spanEnd - spanStart); |
| 9977 | for (int i = 0; i < length; i++) { |
| 9978 | // Only spans that include the modified region make sense after replacement |
| 9979 | // Spans partially included in the replaced region are removed, there is no |
| 9980 | // way to assign them a valid range after replacement |
| 9981 | if (suggestionSpansStarts[i] <= spanStart && |
| 9982 | suggestionSpansEnds[i] >= spanEnd) { |
Gilles Debunne | e300be9 | 2011-12-06 10:15:56 -0800 | [diff] [blame] | 9983 | setSpan_internal(suggestionSpans[i], suggestionSpansStarts[i], |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9984 | suggestionSpansEnds[i] + lengthDifference, suggestionSpansFlags[i]); |
| 9985 | } |
| 9986 | } |
| 9987 | |
Gilles Debunne | e1fc4f6 | 2011-10-03 17:01:19 -0700 | [diff] [blame] | 9988 | // Move cursor at the end of the replaced word |
Gilles Debunne | e300be9 | 2011-12-06 10:15:56 -0800 | [diff] [blame] | 9989 | final int newCursorPosition = spanEnd + lengthDifference; |
| 9990 | setCursorPosition_internal(newCursorPosition, newCursorPosition); |
Gilles Debunne | a6c673b | 2011-09-27 16:45:03 -0700 | [diff] [blame] | 9991 | } |
| 9992 | |
| 9993 | hide(); |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 9994 | } |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 9995 | } |
| 9996 | |
Luca Zanolin | e076045 | 2011-09-08 12:03:37 +0100 | [diff] [blame] | 9997 | /** |
| 9998 | * Removes the suggestion spans. |
| 9999 | */ |
| 10000 | CharSequence removeSuggestionSpans(CharSequence text) { |
| 10001 | if (text instanceof Spanned) { |
| 10002 | Spannable spannable; |
| 10003 | if (text instanceof Spannable) { |
| 10004 | spannable = (Spannable) text; |
| 10005 | } else { |
| 10006 | spannable = new SpannableString(text); |
| 10007 | text = spannable; |
| 10008 | } |
Gilles Debunne | f3a135b | 2011-05-23 16:28:47 -0700 | [diff] [blame] | 10009 | |
Luca Zanolin | e076045 | 2011-09-08 12:03:37 +0100 | [diff] [blame] | 10010 | SuggestionSpan[] spans = spannable.getSpans(0, text.length(), SuggestionSpan.class); |
| 10011 | for (int i = 0; i < spans.length; i++) { |
| 10012 | spannable.removeSpan(spans[i]); |
| 10013 | } |
| 10014 | } |
| 10015 | return text; |
| 10016 | } |
| 10017 | |
| 10018 | void showSuggestions() { |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 10019 | if (mSuggestionsPopupWindow == null) { |
| 10020 | mSuggestionsPopupWindow = new SuggestionsPopupWindow(); |
| 10021 | } |
| 10022 | hideControllers(); |
| 10023 | mSuggestionsPopupWindow.show(); |
| 10024 | } |
| 10025 | |
Gilles Debunne | 4a7199a | 2011-07-11 14:58:27 -0700 | [diff] [blame] | 10026 | boolean areSuggestionsShown() { |
| 10027 | return mSuggestionsPopupWindow != null && mSuggestionsPopupWindow.isShowing(); |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10028 | } |
| 10029 | |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10030 | /** |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10031 | * Return whether or not suggestions are enabled on this TextView. The suggestions are generated |
| 10032 | * by the IME or by the spell checker as the user types. This is done by adding |
| 10033 | * {@link SuggestionSpan}s to the text. |
Gilles Debunne | f3a135b | 2011-05-23 16:28:47 -0700 | [diff] [blame] | 10034 | * |
| 10035 | * When suggestions are enabled (default), this list of suggestions will be displayed when the |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10036 | * user asks for them on these parts of the text. This value depends on the inputType of this |
| 10037 | * TextView. |
Gilles Debunne | f3a135b | 2011-05-23 16:28:47 -0700 | [diff] [blame] | 10038 | * |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10039 | * The class of the input type must be {@link InputType#TYPE_CLASS_TEXT}. |
| 10040 | * |
| 10041 | * In addition, the type variation must be one of |
Gilles Debunne | 248b112 | 2011-08-12 13:24:16 -0700 | [diff] [blame] | 10042 | * {@link InputType#TYPE_TEXT_VARIATION_NORMAL}, |
| 10043 | * {@link InputType#TYPE_TEXT_VARIATION_EMAIL_SUBJECT}, |
| 10044 | * {@link InputType#TYPE_TEXT_VARIATION_LONG_MESSAGE}, |
| 10045 | * {@link InputType#TYPE_TEXT_VARIATION_SHORT_MESSAGE} or |
| 10046 | * {@link InputType#TYPE_TEXT_VARIATION_WEB_EDIT_TEXT}. |
| 10047 | * |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10048 | * And finally, the {@link InputType#TYPE_TEXT_FLAG_NO_SUGGESTIONS} flag must <i>not</i> be set. |
Gilles Debunne | f3a135b | 2011-05-23 16:28:47 -0700 | [diff] [blame] | 10049 | * |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10050 | * @return true if the suggestions popup window is enabled, based on the inputType. |
Gilles Debunne | f3a135b | 2011-05-23 16:28:47 -0700 | [diff] [blame] | 10051 | */ |
| 10052 | public boolean isSuggestionsEnabled() { |
Gilles Debunne | 248b112 | 2011-08-12 13:24:16 -0700 | [diff] [blame] | 10053 | if ((mInputType & InputType.TYPE_MASK_CLASS) != InputType.TYPE_CLASS_TEXT) return false; |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10054 | if ((mInputType & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS) > 0) return false; |
| 10055 | |
Luca Zanolin | 15b8016 | 2011-08-17 18:47:27 +0100 | [diff] [blame] | 10056 | final int variation = mInputType & EditorInfo.TYPE_MASK_VARIATION; |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10057 | return (variation == EditorInfo.TYPE_TEXT_VARIATION_NORMAL || |
Gilles Debunne | 248b112 | 2011-08-12 13:24:16 -0700 | [diff] [blame] | 10058 | variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_SUBJECT || |
| 10059 | variation == EditorInfo.TYPE_TEXT_VARIATION_LONG_MESSAGE || |
| 10060 | variation == EditorInfo.TYPE_TEXT_VARIATION_SHORT_MESSAGE || |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10061 | variation == EditorInfo.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT); |
Gilles Debunne | f3a135b | 2011-05-23 16:28:47 -0700 | [diff] [blame] | 10062 | } |
| 10063 | |
| 10064 | /** |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 10065 | * If provided, this ActionMode.Callback will be used to create the ActionMode when text |
| 10066 | * selection is initiated in this View. |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10067 | * |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 10068 | * The standard implementation populates the menu with a subset of Select All, Cut, Copy and |
| 10069 | * Paste actions, depending on what this View supports. |
| 10070 | * |
| 10071 | * A custom implementation can add new entries in the default menu in its |
Gilles Debunne | 3784a7f | 2011-07-15 13:49:38 -0700 | [diff] [blame] | 10072 | * {@link android.view.ActionMode.Callback#onPrepareActionMode(ActionMode, Menu)} method. The |
| 10073 | * default actions can also be removed from the menu using {@link Menu#removeItem(int)} and |
| 10074 | * passing {@link android.R.id#selectAll}, {@link android.R.id#cut}, {@link android.R.id#copy} |
| 10075 | * or {@link android.R.id#paste} ids as parameters. |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 10076 | * |
Gilles Debunne | 3784a7f | 2011-07-15 13:49:38 -0700 | [diff] [blame] | 10077 | * Returning false from |
| 10078 | * {@link android.view.ActionMode.Callback#onCreateActionMode(ActionMode, Menu)} will prevent |
| 10079 | * the action mode from being started. |
Gilles Debunne | ddd6f39 | 2011-01-27 09:48:01 -0800 | [diff] [blame] | 10080 | * |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 10081 | * Action click events should be handled by the custom implementation of |
Gilles Debunne | 3784a7f | 2011-07-15 13:49:38 -0700 | [diff] [blame] | 10082 | * {@link android.view.ActionMode.Callback#onActionItemClicked(ActionMode, MenuItem)}. |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 10083 | * |
| 10084 | * Note that text selection mode is not started when a TextView receives focus and the |
| 10085 | * {@link android.R.attr#selectAllOnFocus} flag has been set. The content is highlighted in |
| 10086 | * that case, to allow for quick replacement. |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10087 | */ |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 10088 | public void setCustomSelectionActionModeCallback(ActionMode.Callback actionModeCallback) { |
| 10089 | mCustomSelectionActionModeCallback = actionModeCallback; |
| 10090 | } |
| 10091 | |
| 10092 | /** |
| 10093 | * Retrieves the value set in {@link #setCustomSelectionActionModeCallback}. Default is null. |
| 10094 | * |
| 10095 | * @return The current custom selection callback. |
| 10096 | */ |
| 10097 | public ActionMode.Callback getCustomSelectionActionModeCallback() { |
| 10098 | return mCustomSelectionActionModeCallback; |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10099 | } |
| 10100 | |
| 10101 | /** |
| 10102 | * |
| 10103 | * @return true if the selection mode was actually started. |
| 10104 | */ |
| 10105 | private boolean startSelectionActionMode() { |
| 10106 | if (mSelectionActionMode != null) { |
| 10107 | // Selection action mode is already started |
| 10108 | return false; |
| 10109 | } |
| 10110 | |
Gilles Debunne | cbcb345 | 2010-12-17 15:31:02 -0800 | [diff] [blame] | 10111 | if (!canSelectText() || !requestFocus()) { |
| 10112 | Log.w(LOG_TAG, "TextView does not support text selection. Action mode cancelled."); |
| 10113 | return false; |
| 10114 | } |
| 10115 | |
Gilles Debunne | ed67418 | 2011-06-02 19:52:22 -0700 | [diff] [blame] | 10116 | if (!hasSelection()) { |
| 10117 | // There may already be a selection on device rotation |
Gilles Debunne | e1fc4f6 | 2011-10-03 17:01:19 -0700 | [diff] [blame] | 10118 | if (!selectCurrentWord()) { |
Gilles Debunne | ed67418 | 2011-06-02 19:52:22 -0700 | [diff] [blame] | 10119 | // No word found under cursor or text selection not permitted. |
| 10120 | return false; |
| 10121 | } |
Gilles Debunne | c01f3fe | 2010-12-22 17:07:36 -0800 | [diff] [blame] | 10122 | } |
| 10123 | |
Gilles Debunne | 61ddbba | 2011-11-09 09:48:40 +0100 | [diff] [blame] | 10124 | boolean willExtract = extractedTextModeWillBeStarted(); |
Gilles Debunne | 17d31de | 2011-01-27 11:02:18 -0800 | [diff] [blame] | 10125 | |
Gilles Debunne | 98fb9ed | 2011-09-07 17:15:41 -0700 | [diff] [blame] | 10126 | // Do not start the action mode when extracted text will show up full screen, thus |
| 10127 | // immediately hiding the newly created action bar, which would be visually distracting. |
Gilles Debunne | 61ddbba | 2011-11-09 09:48:40 +0100 | [diff] [blame] | 10128 | if (!willExtract) { |
Gilles Debunne | 98fb9ed | 2011-09-07 17:15:41 -0700 | [diff] [blame] | 10129 | ActionMode.Callback actionModeCallback = new SelectionActionModeCallback(); |
| 10130 | mSelectionActionMode = startActionMode(actionModeCallback); |
| 10131 | } |
Gilles Debunne | 98fb9ed | 2011-09-07 17:15:41 -0700 | [diff] [blame] | 10132 | |
Gilles Debunne | 61ddbba | 2011-11-09 09:48:40 +0100 | [diff] [blame] | 10133 | final boolean selectionStarted = mSelectionActionMode != null || willExtract; |
| 10134 | if (selectionStarted && !mTextIsSelectable && mSoftInputShownOnFocus) { |
Gilles Debunne | 17d31de | 2011-01-27 11:02:18 -0800 | [diff] [blame] | 10135 | // Show the IME to be able to replace text, except when selecting non editable text. |
Gilles Debunne | 61ddbba | 2011-11-09 09:48:40 +0100 | [diff] [blame] | 10136 | final InputMethodManager imm = InputMethodManager.peekInstance(); |
| 10137 | if (imm != null) { |
| 10138 | imm.showSoftInput(this, 0, null); |
| 10139 | } |
Gilles Debunne | 17d31de | 2011-01-27 11:02:18 -0800 | [diff] [blame] | 10140 | } |
| 10141 | |
| 10142 | return selectionStarted; |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 10143 | } |
| 10144 | |
Gilles Debunne | 61ddbba | 2011-11-09 09:48:40 +0100 | [diff] [blame] | 10145 | private boolean extractedTextModeWillBeStarted() { |
| 10146 | if (!(this instanceof ExtractEditText)) { |
| 10147 | final InputMethodManager imm = InputMethodManager.peekInstance(); |
| 10148 | return imm != null && imm.isFullscreenMode(); |
| 10149 | } |
| 10150 | return false; |
| 10151 | } |
| 10152 | |
Gilles Debunne | ed279f8 | 2010-08-18 21:24:35 -0700 | [diff] [blame] | 10153 | private void stopSelectionActionMode() { |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10154 | if (mSelectionActionMode != null) { |
Gilles Debunne | d94f8c5 | 2011-01-10 11:29:15 -0800 | [diff] [blame] | 10155 | // This will hide the mSelectionModifierCursorController |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10156 | mSelectionActionMode.finish(); |
| 10157 | } |
| 10158 | } |
| 10159 | |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10160 | /** |
| 10161 | * Paste clipboard content between min and max positions. |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10162 | */ |
Gilles Debunne | 0a2aa40 | 2010-11-24 17:57:46 -0800 | [diff] [blame] | 10163 | private void paste(int min, int max) { |
| 10164 | ClipboardManager clipboard = |
| 10165 | (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE); |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10166 | ClipData clip = clipboard.getPrimaryClip(); |
| 10167 | if (clip != null) { |
Gilles Debunne | 75beb33 | 2011-04-29 11:40:22 -0700 | [diff] [blame] | 10168 | boolean didFirst = false; |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10169 | for (int i=0; i<clip.getItemCount(); i++) { |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 10170 | CharSequence paste = clip.getItemAt(i).coerceToText(getContext()); |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10171 | if (paste != null) { |
Gilles Debunne | 75beb33 | 2011-04-29 11:40:22 -0700 | [diff] [blame] | 10172 | if (!didFirst) { |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10173 | long minMax = prepareSpacesAroundPaste(min, max, paste); |
| 10174 | min = extractRangeStartFromLong(minMax); |
| 10175 | max = extractRangeEndFromLong(minMax); |
| 10176 | Selection.setSelection((Spannable) mText, max); |
| 10177 | ((Editable) mText).replace(min, max, paste); |
Gilles Debunne | 75beb33 | 2011-04-29 11:40:22 -0700 | [diff] [blame] | 10178 | didFirst = true; |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10179 | } else { |
| 10180 | ((Editable) mText).insert(getSelectionEnd(), "\n"); |
| 10181 | ((Editable) mText).insert(getSelectionEnd(), paste); |
| 10182 | } |
| 10183 | } |
| 10184 | } |
| 10185 | stopSelectionActionMode(); |
Gilles Debunne | 0a2aa40 | 2010-11-24 17:57:46 -0800 | [diff] [blame] | 10186 | sLastCutOrCopyTime = 0; |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10187 | } |
| 10188 | } |
| 10189 | |
Gilles Debunne | 0a2aa40 | 2010-11-24 17:57:46 -0800 | [diff] [blame] | 10190 | private void setPrimaryClip(ClipData clip) { |
| 10191 | ClipboardManager clipboard = (ClipboardManager) getContext(). |
| 10192 | getSystemService(Context.CLIPBOARD_SERVICE); |
| 10193 | clipboard.setPrimaryClip(clip); |
| 10194 | sLastCutOrCopyTime = SystemClock.uptimeMillis(); |
| 10195 | } |
| 10196 | |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 10197 | /** |
| 10198 | * An ActionMode Callback class that is used to provide actions while in text selection mode. |
| 10199 | * |
| 10200 | * The default callback provides a subset of Select All, Cut, Copy and Paste actions, depending |
| 10201 | * on which of these this TextView supports. |
| 10202 | */ |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10203 | private class SelectionActionModeCallback implements ActionMode.Callback { |
| 10204 | |
| 10205 | @Override |
| 10206 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
Adam Powell | d2b5894 | 2011-10-03 13:41:03 -0700 | [diff] [blame] | 10207 | TypedArray styledAttributes = mContext.obtainStyledAttributes( |
| 10208 | com.android.internal.R.styleable.SelectionModeDrawables); |
Gilles Debunne | 78996c9 | 2010-10-12 16:01:47 -0700 | [diff] [blame] | 10209 | |
Gilles Debunne | 5e9af2d | 2011-05-27 17:28:11 -0700 | [diff] [blame] | 10210 | boolean allowText = getContext().getResources().getBoolean( |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 10211 | com.android.internal.R.bool.config_allowActionMenuItemTextWithIcon); |
Gilles Debunne | 5e9af2d | 2011-05-27 17:28:11 -0700 | [diff] [blame] | 10212 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10213 | mode.setTitle(allowText ? |
Gilles Debunne | 5e9af2d | 2011-05-27 17:28:11 -0700 | [diff] [blame] | 10214 | mContext.getString(com.android.internal.R.string.textSelectionCABTitle) : null); |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10215 | mode.setSubtitle(null); |
| 10216 | |
Gilles Debunne | 5e9af2d | 2011-05-27 17:28:11 -0700 | [diff] [blame] | 10217 | int selectAllIconId = 0; // No icon by default |
| 10218 | if (!allowText) { |
| 10219 | // Provide an icon, text will not be displayed on smaller screens. |
| 10220 | selectAllIconId = styledAttributes.getResourceId( |
Adam Powell | d2b5894 | 2011-10-03 13:41:03 -0700 | [diff] [blame] | 10221 | R.styleable.SelectionModeDrawables_actionModeSelectAllDrawable, 0); |
Gilles Debunne | 5e9af2d | 2011-05-27 17:28:11 -0700 | [diff] [blame] | 10222 | } |
| 10223 | |
Gilles Debunne | cbcb345 | 2010-12-17 15:31:02 -0800 | [diff] [blame] | 10224 | menu.add(0, ID_SELECT_ALL, 0, com.android.internal.R.string.selectAll). |
Gilles Debunne | 5e9af2d | 2011-05-27 17:28:11 -0700 | [diff] [blame] | 10225 | setIcon(selectAllIconId). |
Adam Powell | d8404b2 | 2010-10-13 14:26:41 -0700 | [diff] [blame] | 10226 | setAlphabeticShortcut('a'). |
| 10227 | setShowAsAction( |
| 10228 | MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10229 | |
| 10230 | if (canCut()) { |
| 10231 | menu.add(0, ID_CUT, 0, com.android.internal.R.string.cut). |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 10232 | setIcon(styledAttributes.getResourceId( |
Adam Powell | d2b5894 | 2011-10-03 13:41:03 -0700 | [diff] [blame] | 10233 | R.styleable.SelectionModeDrawables_actionModeCutDrawable, 0)). |
Adam Powell | d8404b2 | 2010-10-13 14:26:41 -0700 | [diff] [blame] | 10234 | setAlphabeticShortcut('x'). |
| 10235 | setShowAsAction( |
| 10236 | MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10237 | } |
| 10238 | |
| 10239 | if (canCopy()) { |
| 10240 | menu.add(0, ID_COPY, 0, com.android.internal.R.string.copy). |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 10241 | setIcon(styledAttributes.getResourceId( |
Adam Powell | d2b5894 | 2011-10-03 13:41:03 -0700 | [diff] [blame] | 10242 | R.styleable.SelectionModeDrawables_actionModeCopyDrawable, 0)). |
Adam Powell | d8404b2 | 2010-10-13 14:26:41 -0700 | [diff] [blame] | 10243 | setAlphabeticShortcut('c'). |
| 10244 | setShowAsAction( |
| 10245 | MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10246 | } |
| 10247 | |
| 10248 | if (canPaste()) { |
| 10249 | menu.add(0, ID_PASTE, 0, com.android.internal.R.string.paste). |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 10250 | setIcon(styledAttributes.getResourceId( |
Adam Powell | d2b5894 | 2011-10-03 13:41:03 -0700 | [diff] [blame] | 10251 | R.styleable.SelectionModeDrawables_actionModePasteDrawable, 0)). |
Adam Powell | d8404b2 | 2010-10-13 14:26:41 -0700 | [diff] [blame] | 10252 | setAlphabeticShortcut('v'). |
| 10253 | setShowAsAction( |
| 10254 | MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10255 | } |
| 10256 | |
Gilles Debunne | 78996c9 | 2010-10-12 16:01:47 -0700 | [diff] [blame] | 10257 | styledAttributes.recycle(); |
| 10258 | |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 10259 | if (mCustomSelectionActionModeCallback != null) { |
Gilles Debunne | ddd6f39 | 2011-01-27 09:48:01 -0800 | [diff] [blame] | 10260 | if (!mCustomSelectionActionModeCallback.onCreateActionMode(mode, menu)) { |
| 10261 | // The custom mode can choose to cancel the action mode |
| 10262 | return false; |
| 10263 | } |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 10264 | } |
| 10265 | |
| 10266 | if (menu.hasVisibleItems() || mode.getCustomView() != null) { |
Gilles Debunne | e587d83 | 2010-11-23 20:20:11 -0800 | [diff] [blame] | 10267 | getSelectionController().show(); |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10268 | return true; |
| 10269 | } else { |
| 10270 | return false; |
| 10271 | } |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 10272 | } |
| 10273 | |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10274 | @Override |
| 10275 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 10276 | if (mCustomSelectionActionModeCallback != null) { |
| 10277 | return mCustomSelectionActionModeCallback.onPrepareActionMode(mode, menu); |
| 10278 | } |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10279 | return true; |
| 10280 | } |
| 10281 | |
| 10282 | @Override |
| 10283 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 10284 | if (mCustomSelectionActionModeCallback != null && |
| 10285 | mCustomSelectionActionModeCallback.onActionItemClicked(mode, item)) { |
| 10286 | return true; |
| 10287 | } |
Jeff Brown | c1df907 | 2010-12-21 16:38:50 -0800 | [diff] [blame] | 10288 | return onTextContextMenuItem(item.getItemId()); |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10289 | } |
| 10290 | |
| 10291 | @Override |
| 10292 | public void onDestroyActionMode(ActionMode mode) { |
Gilles Debunne | f4dceb1 | 2010-12-01 15:54:20 -0800 | [diff] [blame] | 10293 | if (mCustomSelectionActionModeCallback != null) { |
| 10294 | mCustomSelectionActionModeCallback.onDestroyActionMode(mode); |
| 10295 | } |
Gilles Debunne | d94f8c5 | 2011-01-10 11:29:15 -0800 | [diff] [blame] | 10296 | Selection.setSelection((Spannable) mText, getSelectionEnd()); |
| 10297 | |
| 10298 | if (mSelectionModifierCursorController != null) { |
| 10299 | mSelectionModifierCursorController.hide(); |
| 10300 | } |
| 10301 | |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10302 | mSelectionActionMode = null; |
| 10303 | } |
| 10304 | } |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 10305 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10306 | private class ActionPopupWindow extends PinnedPopupWindow implements OnClickListener { |
| 10307 | private static final int POPUP_TEXT_LAYOUT = |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10308 | com.android.internal.R.layout.text_edit_action_popup_text; |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10309 | private TextView mPasteTextView; |
| 10310 | private TextView mReplaceTextView; |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10311 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10312 | @Override |
| 10313 | protected void createPopupWindow() { |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10314 | mPopupWindow = new PopupWindow(TextView.this.mContext, null, |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10315 | com.android.internal.R.attr.textSelectHandleWindowStyle); |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10316 | mPopupWindow.setClippingEnabled(true); |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10317 | } |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10318 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10319 | @Override |
| 10320 | protected void initContentView() { |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 10321 | LinearLayout linearLayout = new LinearLayout(TextView.this.getContext()); |
| 10322 | linearLayout.setOrientation(LinearLayout.HORIZONTAL); |
| 10323 | mContentView = linearLayout; |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10324 | mContentView.setBackgroundResource( |
Gilles Debunne | 0eea668 | 2011-08-29 13:30:31 -0700 | [diff] [blame] | 10325 | com.android.internal.R.drawable.text_edit_paste_window); |
Gilles Debunne | e60e1e5 | 2011-01-20 12:19:44 -0800 | [diff] [blame] | 10326 | |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10327 | LayoutInflater inflater = (LayoutInflater)TextView.this.mContext. |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10328 | getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10329 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10330 | LayoutParams wrapContent = new LayoutParams( |
| 10331 | ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
| 10332 | |
| 10333 | mPasteTextView = (TextView) inflater.inflate(POPUP_TEXT_LAYOUT, null); |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10334 | mPasteTextView.setLayoutParams(wrapContent); |
| 10335 | mContentView.addView(mPasteTextView); |
| 10336 | mPasteTextView.setText(com.android.internal.R.string.paste); |
| 10337 | mPasteTextView.setOnClickListener(this); |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10338 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10339 | mReplaceTextView = (TextView) inflater.inflate(POPUP_TEXT_LAYOUT, null); |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10340 | mReplaceTextView.setLayoutParams(wrapContent); |
| 10341 | mContentView.addView(mReplaceTextView); |
| 10342 | mReplaceTextView.setText(com.android.internal.R.string.replace); |
| 10343 | mReplaceTextView.setOnClickListener(this); |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10344 | } |
| 10345 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10346 | @Override |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10347 | public void show() { |
Gilles Debunne | 248b112 | 2011-08-12 13:24:16 -0700 | [diff] [blame] | 10348 | boolean canPaste = canPaste(); |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10349 | boolean canSuggest = isSuggestionsEnabled() && isCursorInsideSuggestionSpan(); |
Gilles Debunne | 248b112 | 2011-08-12 13:24:16 -0700 | [diff] [blame] | 10350 | mPasteTextView.setVisibility(canPaste ? View.VISIBLE : View.GONE); |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10351 | mReplaceTextView.setVisibility(canSuggest ? View.VISIBLE : View.GONE); |
Gilles Debunne | 072b50c | 2011-08-02 20:22:43 -0700 | [diff] [blame] | 10352 | |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10353 | if (!canPaste && !canSuggest) return; |
Gilles Debunne | 072b50c | 2011-08-02 20:22:43 -0700 | [diff] [blame] | 10354 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10355 | super.show(); |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10356 | } |
| 10357 | |
| 10358 | @Override |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10359 | public void onClick(View view) { |
| 10360 | if (view == mPasteTextView && canPaste()) { |
Gilles Debunne | 459ac63 | 2011-07-12 16:36:33 -0700 | [diff] [blame] | 10361 | onTextContextMenuItem(ID_PASTE); |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10362 | hide(); |
| 10363 | } else if (view == mReplaceTextView) { |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10364 | final int middle = (getSelectionStart() + getSelectionEnd()) / 2; |
| 10365 | stopSelectionActionMode(); |
| 10366 | Selection.setSelection((Spannable) mText, middle); |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10367 | showSuggestions(); |
Gilles Debunne | e1c14e6 | 2010-11-03 19:24:29 -0700 | [diff] [blame] | 10368 | } |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10369 | } |
| 10370 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10371 | @Override |
| 10372 | protected int getTextOffset() { |
| 10373 | return (getSelectionStart() + getSelectionEnd()) / 2; |
| 10374 | } |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10375 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10376 | @Override |
| 10377 | protected int getVerticalLocalPosition(int line) { |
| 10378 | return mLayout.getLineTop(line) - mContentView.getMeasuredHeight(); |
| 10379 | } |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10380 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10381 | @Override |
| 10382 | protected int clipVertically(int positionY) { |
| 10383 | if (positionY < 0) { |
| 10384 | final int offset = getTextOffset(); |
| 10385 | final int line = mLayout.getLineForOffset(offset); |
| 10386 | positionY += mLayout.getLineBottom(line) - mLayout.getLineTop(line); |
| 10387 | positionY += mContentView.getMeasuredHeight(); |
Gilles Debunne | e60e1e5 | 2011-01-20 12:19:44 -0800 | [diff] [blame] | 10388 | |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10389 | // Assumes insertion and selection handles share the same height |
Gilles Debunne | e60e1e5 | 2011-01-20 12:19:44 -0800 | [diff] [blame] | 10390 | final Drawable handle = mContext.getResources().getDrawable(mTextSelectHandleRes); |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10391 | positionY += handle.getIntrinsicHeight(); |
Gilles Debunne | e60e1e5 | 2011-01-20 12:19:44 -0800 | [diff] [blame] | 10392 | } |
| 10393 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10394 | return positionY; |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10395 | } |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10396 | } |
| 10397 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10398 | private abstract class HandleView extends View implements TextViewPositionListener { |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10399 | protected Drawable mDrawable; |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 10400 | protected Drawable mDrawableLtr; |
| 10401 | protected Drawable mDrawableRtl; |
Gilles Debunne | cfc22c5 | 2011-03-07 15:50:47 -0800 | [diff] [blame] | 10402 | private final PopupWindow mContainer; |
| 10403 | // Position with respect to the parent TextView |
| 10404 | private int mPositionX, mPositionY; |
Adam Powell | 879fb6b | 2010-09-20 11:23:56 -0700 | [diff] [blame] | 10405 | private boolean mIsDragging; |
Gilles Debunne | cfc22c5 | 2011-03-07 15:50:47 -0800 | [diff] [blame] | 10406 | // Offset from touch position to mPosition |
| 10407 | private float mTouchToWindowOffsetX, mTouchToWindowOffsetY; |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10408 | protected int mHotspotX; |
Gilles Debunne | ddf00b8 | 2011-02-23 17:25:13 -0800 | [diff] [blame] | 10409 | // Offsets the hotspot point up, so that cursor is not hidden by the finger when moving up |
Adam Powell | fbb3b47 | 2010-10-06 21:04:35 -0700 | [diff] [blame] | 10410 | private float mTouchOffsetY; |
Gilles Debunne | ddf00b8 | 2011-02-23 17:25:13 -0800 | [diff] [blame] | 10411 | // Where the touch position should be on the handle to ensure a maximum cursor visibility |
| 10412 | private float mIdealVerticalOffset; |
Gilles Debunne | 2037b82 | 2011-04-22 13:07:33 -0700 | [diff] [blame] | 10413 | // Parent's (TextView) previous position in window |
Gilles Debunne | cfc22c5 | 2011-03-07 15:50:47 -0800 | [diff] [blame] | 10414 | private int mLastParentX, mLastParentY; |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10415 | // Transient action popup window for Paste and Replace actions |
| 10416 | protected ActionPopupWindow mActionPopupWindow; |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10417 | // Previous text character offset |
| 10418 | private int mPreviousOffset = -1; |
| 10419 | // Previous text character offset |
| 10420 | private boolean mPositionHasChanged = true; |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10421 | // Used to delay the appearance of the action popup window |
| 10422 | private Runnable mActionPopupShower; |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10423 | |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 10424 | public HandleView(Drawable drawableLtr, Drawable drawableRtl) { |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10425 | super(TextView.this.mContext); |
| 10426 | mContainer = new PopupWindow(TextView.this.mContext, null, |
| 10427 | com.android.internal.R.attr.textSelectHandleWindowStyle); |
| 10428 | mContainer.setSplitTouchEnabled(true); |
| 10429 | mContainer.setClippingEnabled(false); |
| 10430 | mContainer.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL); |
| 10431 | mContainer.setContentView(this); |
| 10432 | |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 10433 | mDrawableLtr = drawableLtr; |
| 10434 | mDrawableRtl = drawableRtl; |
| 10435 | |
| 10436 | updateDrawable(); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10437 | |
| 10438 | final int handleHeight = mDrawable.getIntrinsicHeight(); |
| 10439 | mTouchOffsetY = -0.3f * handleHeight; |
| 10440 | mIdealVerticalOffset = 0.7f * handleHeight; |
| 10441 | } |
| 10442 | |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 10443 | protected void updateDrawable() { |
| 10444 | final int offset = getCurrentCursorOffset(); |
| 10445 | final boolean isRtlCharAtOffset = mLayout.isRtlCharAt(offset); |
| 10446 | mDrawable = isRtlCharAtOffset ? mDrawableRtl : mDrawableLtr; |
| 10447 | mHotspotX = getHotspotX(mDrawable, isRtlCharAtOffset); |
| 10448 | } |
| 10449 | |
| 10450 | protected abstract int getHotspotX(Drawable drawable, boolean isRtlRun); |
Gilles Debunne | b0d6ba1 | 2010-08-17 20:01:42 -0700 | [diff] [blame] | 10451 | |
Gilles Debunne | aa8d73b | 2011-01-17 09:16:40 -0800 | [diff] [blame] | 10452 | // Touch-up filter: number of previous positions remembered |
| 10453 | private static final int HISTORY_SIZE = 5; |
Gilles Debunne | bc7a4c8 | 2011-02-09 10:45:51 -0800 | [diff] [blame] | 10454 | private static final int TOUCH_UP_FILTER_DELAY_AFTER = 150; |
| 10455 | private static final int TOUCH_UP_FILTER_DELAY_BEFORE = 350; |
Gilles Debunne | aa8d73b | 2011-01-17 09:16:40 -0800 | [diff] [blame] | 10456 | private final long[] mPreviousOffsetsTimes = new long[HISTORY_SIZE]; |
| 10457 | private final int[] mPreviousOffsets = new int[HISTORY_SIZE]; |
| 10458 | private int mPreviousOffsetIndex = 0; |
| 10459 | private int mNumberPreviousOffsets = 0; |
| 10460 | |
Gilles Debunne | cfc22c5 | 2011-03-07 15:50:47 -0800 | [diff] [blame] | 10461 | private void startTouchUpFilter(int offset) { |
Gilles Debunne | aa8d73b | 2011-01-17 09:16:40 -0800 | [diff] [blame] | 10462 | mNumberPreviousOffsets = 0; |
| 10463 | addPositionToTouchUpFilter(offset); |
| 10464 | } |
| 10465 | |
Gilles Debunne | cfc22c5 | 2011-03-07 15:50:47 -0800 | [diff] [blame] | 10466 | private void addPositionToTouchUpFilter(int offset) { |
Gilles Debunne | aa8d73b | 2011-01-17 09:16:40 -0800 | [diff] [blame] | 10467 | mPreviousOffsetIndex = (mPreviousOffsetIndex + 1) % HISTORY_SIZE; |
| 10468 | mPreviousOffsets[mPreviousOffsetIndex] = offset; |
| 10469 | mPreviousOffsetsTimes[mPreviousOffsetIndex] = SystemClock.uptimeMillis(); |
| 10470 | mNumberPreviousOffsets++; |
| 10471 | } |
| 10472 | |
Gilles Debunne | cfc22c5 | 2011-03-07 15:50:47 -0800 | [diff] [blame] | 10473 | private void filterOnTouchUp() { |
Gilles Debunne | aa8d73b | 2011-01-17 09:16:40 -0800 | [diff] [blame] | 10474 | final long now = SystemClock.uptimeMillis(); |
| 10475 | int i = 0; |
Gilles Debunne | bc7a4c8 | 2011-02-09 10:45:51 -0800 | [diff] [blame] | 10476 | int index = mPreviousOffsetIndex; |
Gilles Debunne | aa8d73b | 2011-01-17 09:16:40 -0800 | [diff] [blame] | 10477 | final int iMax = Math.min(mNumberPreviousOffsets, HISTORY_SIZE); |
Gilles Debunne | bc7a4c8 | 2011-02-09 10:45:51 -0800 | [diff] [blame] | 10478 | while (i < iMax && (now - mPreviousOffsetsTimes[index]) < TOUCH_UP_FILTER_DELAY_AFTER) { |
Gilles Debunne | aa8d73b | 2011-01-17 09:16:40 -0800 | [diff] [blame] | 10479 | i++; |
Gilles Debunne | bc7a4c8 | 2011-02-09 10:45:51 -0800 | [diff] [blame] | 10480 | index = (mPreviousOffsetIndex - i + HISTORY_SIZE) % HISTORY_SIZE; |
Gilles Debunne | aa8d73b | 2011-01-17 09:16:40 -0800 | [diff] [blame] | 10481 | } |
| 10482 | |
Gilles Debunne | bc7a4c8 | 2011-02-09 10:45:51 -0800 | [diff] [blame] | 10483 | if (i > 0 && i < iMax && |
| 10484 | (now - mPreviousOffsetsTimes[index]) > TOUCH_UP_FILTER_DELAY_BEFORE) { |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 10485 | positionAtCursorOffset(mPreviousOffsets[index], false); |
Gilles Debunne | bc7a4c8 | 2011-02-09 10:45:51 -0800 | [diff] [blame] | 10486 | } |
Gilles Debunne | aa8d73b | 2011-01-17 09:16:40 -0800 | [diff] [blame] | 10487 | } |
| 10488 | |
Gilles Debunne | 040023a | 2011-08-02 18:23:31 -0700 | [diff] [blame] | 10489 | public boolean offsetHasBeenChanged() { |
| 10490 | return mNumberPreviousOffsets > 1; |
| 10491 | } |
| 10492 | |
Adam Powell | 879fb6b | 2010-09-20 11:23:56 -0700 | [diff] [blame] | 10493 | @Override |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10494 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Gilles Debunne | cfc22c5 | 2011-03-07 15:50:47 -0800 | [diff] [blame] | 10495 | setMeasuredDimension(mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight()); |
Adam Powell | 879fb6b | 2010-09-20 11:23:56 -0700 | [diff] [blame] | 10496 | } |
| 10497 | |
| 10498 | public void show() { |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10499 | if (isShowing()) return; |
Gilles Debunne | cfc22c5 | 2011-03-07 15:50:47 -0800 | [diff] [blame] | 10500 | |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 10501 | getPositionListener().addSubscriber(this, true /* local position may change */); |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10502 | |
| 10503 | // Make sure the offset is always considered new, even when focusing at same position |
| 10504 | mPreviousOffset = -1; |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 10505 | positionAtCursorOffset(getCurrentCursorOffset(), false); |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10506 | |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10507 | hideActionPopupWindow(); |
Gilles Debunne | cfc22c5 | 2011-03-07 15:50:47 -0800 | [diff] [blame] | 10508 | } |
| 10509 | |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10510 | protected void dismiss() { |
Gilles Debunne | cfc22c5 | 2011-03-07 15:50:47 -0800 | [diff] [blame] | 10511 | mIsDragging = false; |
| 10512 | mContainer.dismiss(); |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10513 | onDetached(); |
Adam Powell | 879fb6b | 2010-09-20 11:23:56 -0700 | [diff] [blame] | 10514 | } |
| 10515 | |
| 10516 | public void hide() { |
Gilles Debunne | cfc22c5 | 2011-03-07 15:50:47 -0800 | [diff] [blame] | 10517 | dismiss(); |
| 10518 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10519 | TextView.this.getPositionListener().removeSubscriber(this); |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10520 | } |
| 10521 | |
Gilles Debunne | cb2516b | 2011-08-05 17:02:54 -0700 | [diff] [blame] | 10522 | void showActionPopupWindow(int delay) { |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10523 | if (mActionPopupWindow == null) { |
| 10524 | mActionPopupWindow = new ActionPopupWindow(); |
| 10525 | } |
| 10526 | if (mActionPopupShower == null) { |
| 10527 | mActionPopupShower = new Runnable() { |
| 10528 | public void run() { |
| 10529 | mActionPopupWindow.show(); |
| 10530 | } |
| 10531 | }; |
| 10532 | } else { |
| 10533 | TextView.this.removeCallbacks(mActionPopupShower); |
| 10534 | } |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10535 | TextView.this.postDelayed(mActionPopupShower, delay); |
| 10536 | } |
| 10537 | |
| 10538 | protected void hideActionPopupWindow() { |
| 10539 | if (mActionPopupShower != null) { |
| 10540 | TextView.this.removeCallbacks(mActionPopupShower); |
| 10541 | } |
| 10542 | if (mActionPopupWindow != null) { |
| 10543 | mActionPopupWindow.hide(); |
| 10544 | } |
Adam Powell | 879fb6b | 2010-09-20 11:23:56 -0700 | [diff] [blame] | 10545 | } |
| 10546 | |
| 10547 | public boolean isShowing() { |
| 10548 | return mContainer.isShowing(); |
| 10549 | } |
| 10550 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10551 | private boolean isVisible() { |
Adam Powell | abcbb1a | 2010-10-04 21:12:19 -0700 | [diff] [blame] | 10552 | // Always show a dragging handle. |
| 10553 | if (mIsDragging) { |
| 10554 | return true; |
| 10555 | } |
| 10556 | |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 10557 | if (isInBatchEditMode()) { |
| 10558 | return false; |
| 10559 | } |
| 10560 | |
Gilles Debunne | 64901d4 | 2011-11-25 10:23:38 +0100 | [diff] [blame] | 10561 | return TextView.this.isPositionVisible(mPositionX + mHotspotX, mPositionY); |
Adam Powell | 879fb6b | 2010-09-20 11:23:56 -0700 | [diff] [blame] | 10562 | } |
| 10563 | |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10564 | public abstract int getCurrentCursorOffset(); |
| 10565 | |
Gilles Debunne | 186aaf9 | 2011-09-16 14:26:12 -0700 | [diff] [blame] | 10566 | protected abstract void updateSelection(int offset); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10567 | |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 10568 | public abstract void updatePosition(float x, float y); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10569 | |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 10570 | protected void positionAtCursorOffset(int offset, boolean parentScrolled) { |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10571 | // A HandleView relies on the layout, which may be nulled by external methods |
Gilles Debunne | d4bb0b0 | 2011-05-27 15:58:55 -0700 | [diff] [blame] | 10572 | if (mLayout == null) { |
| 10573 | // Will update controllers' state, hiding them and stopping selection mode if needed |
| 10574 | prepareCursorControllers(); |
| 10575 | return; |
| 10576 | } |
| 10577 | |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 10578 | if (offset != mPreviousOffset || parentScrolled) { |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10579 | updateSelection(offset); |
| 10580 | addPositionToTouchUpFilter(offset); |
| 10581 | final int line = mLayout.getLineForOffset(offset); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10582 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10583 | mPositionX = (int) (mLayout.getPrimaryHorizontal(offset) - 0.5f - mHotspotX); |
| 10584 | mPositionY = mLayout.getLineBottom(line); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10585 | |
Gilles Debunne | fec22c6 | 2011-10-21 13:48:18 -0700 | [diff] [blame] | 10586 | // Take TextView's padding and scroll into account. |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10587 | mPositionX += viewportToContentHorizontalOffset(); |
| 10588 | mPositionY += viewportToContentVerticalOffset(); |
| 10589 | |
| 10590 | mPreviousOffset = offset; |
| 10591 | mPositionHasChanged = true; |
| 10592 | } |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10593 | } |
| 10594 | |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 10595 | public void updatePosition(int parentPositionX, int parentPositionY, |
| 10596 | boolean parentPositionChanged, boolean parentScrolled) { |
| 10597 | positionAtCursorOffset(getCurrentCursorOffset(), parentScrolled); |
| 10598 | if (parentPositionChanged || mPositionHasChanged) { |
Gilles Debunne | 2037b82 | 2011-04-22 13:07:33 -0700 | [diff] [blame] | 10599 | if (mIsDragging) { |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10600 | // Update touchToWindow offset in case of parent scrolling while dragging |
| 10601 | if (parentPositionX != mLastParentX || parentPositionY != mLastParentY) { |
| 10602 | mTouchToWindowOffsetX += parentPositionX - mLastParentX; |
| 10603 | mTouchToWindowOffsetY += parentPositionY - mLastParentY; |
| 10604 | mLastParentX = parentPositionX; |
| 10605 | mLastParentY = parentPositionY; |
Gilles Debunne | 2037b82 | 2011-04-22 13:07:33 -0700 | [diff] [blame] | 10606 | } |
Gilles Debunne | 2037b82 | 2011-04-22 13:07:33 -0700 | [diff] [blame] | 10607 | |
Gilles Debunne | 3784a7f | 2011-07-15 13:49:38 -0700 | [diff] [blame] | 10608 | onHandleMoved(); |
| 10609 | } |
Gilles Debunne | 2037b82 | 2011-04-22 13:07:33 -0700 | [diff] [blame] | 10610 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10611 | if (isVisible()) { |
| 10612 | final int positionX = parentPositionX + mPositionX; |
| 10613 | final int positionY = parentPositionY + mPositionY; |
| 10614 | if (isShowing()) { |
| 10615 | mContainer.update(positionX, positionY, -1, -1); |
| 10616 | } else { |
| 10617 | mContainer.showAtLocation(TextView.this, Gravity.NO_GRAVITY, |
| 10618 | positionX, positionY); |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10619 | } |
Adam Powell | 879fb6b | 2010-09-20 11:23:56 -0700 | [diff] [blame] | 10620 | } else { |
Gilles Debunne | cfc22c5 | 2011-03-07 15:50:47 -0800 | [diff] [blame] | 10621 | if (isShowing()) { |
| 10622 | dismiss(); |
| 10623 | } |
Adam Powell | 879fb6b | 2010-09-20 11:23:56 -0700 | [diff] [blame] | 10624 | } |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10625 | |
| 10626 | mPositionHasChanged = false; |
Adam Powell | 879fb6b | 2010-09-20 11:23:56 -0700 | [diff] [blame] | 10627 | } |
| 10628 | } |
| 10629 | |
| 10630 | @Override |
Gilles Debunne | 7b9652b | 2010-10-26 16:27:12 -0700 | [diff] [blame] | 10631 | protected void onDraw(Canvas c) { |
Adam Powell | 879fb6b | 2010-09-20 11:23:56 -0700 | [diff] [blame] | 10632 | mDrawable.setBounds(0, 0, mRight - mLeft, mBottom - mTop); |
Gilles Debunne | aa8d73b | 2011-01-17 09:16:40 -0800 | [diff] [blame] | 10633 | mDrawable.draw(c); |
Adam Powell | 879fb6b | 2010-09-20 11:23:56 -0700 | [diff] [blame] | 10634 | } |
| 10635 | |
| 10636 | @Override |
| 10637 | public boolean onTouchEvent(MotionEvent ev) { |
| 10638 | switch (ev.getActionMasked()) { |
Gilles Debunne | 874d77c | 2011-01-25 15:29:13 -0800 | [diff] [blame] | 10639 | case MotionEvent.ACTION_DOWN: { |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10640 | startTouchUpFilter(getCurrentCursorOffset()); |
| 10641 | mTouchToWindowOffsetX = ev.getRawX() - mPositionX; |
| 10642 | mTouchToWindowOffsetY = ev.getRawY() - mPositionY; |
Gilles Debunne | ddf00b8 | 2011-02-23 17:25:13 -0800 | [diff] [blame] | 10643 | |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10644 | final PositionListener positionListener = getPositionListener(); |
| 10645 | mLastParentX = positionListener.getPositionX(); |
| 10646 | mLastParentY = positionListener.getPositionY(); |
Gilles Debunne | 874d77c | 2011-01-25 15:29:13 -0800 | [diff] [blame] | 10647 | mIsDragging = true; |
| 10648 | break; |
| 10649 | } |
| 10650 | |
| 10651 | case MotionEvent.ACTION_MOVE: { |
| 10652 | final float rawX = ev.getRawX(); |
| 10653 | final float rawY = ev.getRawY(); |
Gilles Debunne | ddf00b8 | 2011-02-23 17:25:13 -0800 | [diff] [blame] | 10654 | |
| 10655 | // Vertical hysteresis: vertical down movement tends to snap to ideal offset |
| 10656 | final float previousVerticalOffset = mTouchToWindowOffsetY - mLastParentY; |
| 10657 | final float currentVerticalOffset = rawY - mPositionY - mLastParentY; |
| 10658 | float newVerticalOffset; |
| 10659 | if (previousVerticalOffset < mIdealVerticalOffset) { |
| 10660 | newVerticalOffset = Math.min(currentVerticalOffset, mIdealVerticalOffset); |
| 10661 | newVerticalOffset = Math.max(newVerticalOffset, previousVerticalOffset); |
| 10662 | } else { |
| 10663 | newVerticalOffset = Math.max(currentVerticalOffset, mIdealVerticalOffset); |
| 10664 | newVerticalOffset = Math.min(newVerticalOffset, previousVerticalOffset); |
| 10665 | } |
| 10666 | mTouchToWindowOffsetY = newVerticalOffset + mLastParentY; |
| 10667 | |
Gilles Debunne | 874d77c | 2011-01-25 15:29:13 -0800 | [diff] [blame] | 10668 | final float newPosX = rawX - mTouchToWindowOffsetX + mHotspotX; |
Gilles Debunne | ddf00b8 | 2011-02-23 17:25:13 -0800 | [diff] [blame] | 10669 | final float newPosY = rawY - mTouchToWindowOffsetY + mTouchOffsetY; |
Gilles Debunne | 874d77c | 2011-01-25 15:29:13 -0800 | [diff] [blame] | 10670 | |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 10671 | updatePosition(newPosX, newPosY); |
Gilles Debunne | 874d77c | 2011-01-25 15:29:13 -0800 | [diff] [blame] | 10672 | break; |
| 10673 | } |
| 10674 | |
| 10675 | case MotionEvent.ACTION_UP: |
Gilles Debunne | 874d77c | 2011-01-25 15:29:13 -0800 | [diff] [blame] | 10676 | filterOnTouchUp(); |
| 10677 | mIsDragging = false; |
| 10678 | break; |
Gilles Debunne | aa85a4c | 2010-12-06 18:27:17 -0800 | [diff] [blame] | 10679 | |
Gilles Debunne | 874d77c | 2011-01-25 15:29:13 -0800 | [diff] [blame] | 10680 | case MotionEvent.ACTION_CANCEL: |
| 10681 | mIsDragging = false; |
| 10682 | break; |
Adam Powell | 879fb6b | 2010-09-20 11:23:56 -0700 | [diff] [blame] | 10683 | } |
| 10684 | return true; |
| 10685 | } |
| 10686 | |
| 10687 | public boolean isDragging() { |
| 10688 | return mIsDragging; |
Gilles Debunne | a6d7ee1 | 2010-08-13 14:43:10 -0700 | [diff] [blame] | 10689 | } |
Gilles Debunne | 64e54a6 | 2010-09-07 19:07:17 -0700 | [diff] [blame] | 10690 | |
Gilles Debunne | 2037b82 | 2011-04-22 13:07:33 -0700 | [diff] [blame] | 10691 | void onHandleMoved() { |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10692 | hideActionPopupWindow(); |
Gilles Debunne | a6d7ee1 | 2010-08-13 14:43:10 -0700 | [diff] [blame] | 10693 | } |
Gilles Debunne | 6934044 | 2011-03-31 13:37:51 -0700 | [diff] [blame] | 10694 | |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10695 | public void onDetached() { |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10696 | hideActionPopupWindow(); |
Gilles Debunne | 81f0808 | 2011-02-17 14:07:19 -0800 | [diff] [blame] | 10697 | } |
Gilles Debunne | a6d7ee1 | 2010-08-13 14:43:10 -0700 | [diff] [blame] | 10698 | } |
| 10699 | |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10700 | private class InsertionHandleView extends HandleView { |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10701 | private static final int DELAY_BEFORE_HANDLE_FADES_OUT = 4000; |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10702 | private static final int RECENT_CUT_COPY_DURATION = 15 * 1000; // seconds |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 10703 | |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10704 | // Used to detect taps on the insertion handle, which will affect the ActionPopupWindow |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10705 | private float mDownPositionX, mDownPositionY; |
Gilles Debunne | 7eeba5f | 2010-12-10 16:55:55 -0800 | [diff] [blame] | 10706 | private Runnable mHider; |
Gilles Debunne | 0a2aa40 | 2010-11-24 17:57:46 -0800 | [diff] [blame] | 10707 | |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 10708 | public InsertionHandleView(Drawable drawable) { |
| 10709 | super(drawable, drawable); |
| 10710 | } |
| 10711 | |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10712 | @Override |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 10713 | public void show() { |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10714 | super.show(); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10715 | |
Gilles Debunne | e60e1e5 | 2011-01-20 12:19:44 -0800 | [diff] [blame] | 10716 | final long durationSinceCutOrCopy = SystemClock.uptimeMillis() - sLastCutOrCopyTime; |
| 10717 | if (durationSinceCutOrCopy < RECENT_CUT_COPY_DURATION) { |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10718 | showActionPopupWindow(0); |
Gilles Debunne | e60e1e5 | 2011-01-20 12:19:44 -0800 | [diff] [blame] | 10719 | } |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10720 | |
| 10721 | hideAfterDelay(); |
| 10722 | } |
| 10723 | |
| 10724 | public void showWithActionPopup() { |
| 10725 | show(); |
| 10726 | showActionPopupWindow(0); |
Gilles Debunne | 9948ad7 | 2010-11-24 14:00:46 -0800 | [diff] [blame] | 10727 | } |
| 10728 | |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10729 | private void hideAfterDelay() { |
Gilles Debunne | 7eeba5f | 2010-12-10 16:55:55 -0800 | [diff] [blame] | 10730 | removeHiderCallback(); |
| 10731 | if (mHider == null) { |
| 10732 | mHider = new Runnable() { |
| 10733 | public void run() { |
| 10734 | hide(); |
| 10735 | } |
| 10736 | }; |
| 10737 | } |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10738 | TextView.this.postDelayed(mHider, DELAY_BEFORE_HANDLE_FADES_OUT); |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 10739 | } |
| 10740 | |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10741 | private void removeHiderCallback() { |
| 10742 | if (mHider != null) { |
Gilles Debunne | 2037b82 | 2011-04-22 13:07:33 -0700 | [diff] [blame] | 10743 | TextView.this.removeCallbacks(mHider); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10744 | } |
| 10745 | } |
| 10746 | |
| 10747 | @Override |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 10748 | protected int getHotspotX(Drawable drawable, boolean isRtlRun) { |
| 10749 | return drawable.getIntrinsicWidth() / 2; |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10750 | } |
| 10751 | |
| 10752 | @Override |
| 10753 | public boolean onTouchEvent(MotionEvent ev) { |
| 10754 | final boolean result = super.onTouchEvent(ev); |
| 10755 | |
| 10756 | switch (ev.getActionMasked()) { |
| 10757 | case MotionEvent.ACTION_DOWN: |
| 10758 | mDownPositionX = ev.getRawX(); |
| 10759 | mDownPositionY = ev.getRawY(); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10760 | break; |
| 10761 | |
| 10762 | case MotionEvent.ACTION_UP: |
Gilles Debunne | 040023a | 2011-08-02 18:23:31 -0700 | [diff] [blame] | 10763 | if (!offsetHasBeenChanged()) { |
| 10764 | final float deltaX = mDownPositionX - ev.getRawX(); |
| 10765 | final float deltaY = mDownPositionY - ev.getRawY(); |
| 10766 | final float distanceSquared = deltaX * deltaX + deltaY * deltaY; |
| 10767 | if (distanceSquared < mSquaredTouchSlopDistance) { |
| 10768 | if (mActionPopupWindow != null && mActionPopupWindow.isShowing()) { |
| 10769 | // Tapping on the handle dismisses the displayed action popup |
| 10770 | mActionPopupWindow.hide(); |
| 10771 | } else { |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10772 | showWithActionPopup(); |
Gilles Debunne | 040023a | 2011-08-02 18:23:31 -0700 | [diff] [blame] | 10773 | } |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10774 | } |
| 10775 | } |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10776 | hideAfterDelay(); |
Gilles Debunne | 2037b82 | 2011-04-22 13:07:33 -0700 | [diff] [blame] | 10777 | break; |
| 10778 | |
| 10779 | case MotionEvent.ACTION_CANCEL: |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10780 | hideAfterDelay(); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10781 | break; |
| 10782 | |
| 10783 | default: |
| 10784 | break; |
| 10785 | } |
| 10786 | |
| 10787 | return result; |
| 10788 | } |
| 10789 | |
| 10790 | @Override |
| 10791 | public int getCurrentCursorOffset() { |
| 10792 | return TextView.this.getSelectionStart(); |
| 10793 | } |
| 10794 | |
| 10795 | @Override |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10796 | public void updateSelection(int offset) { |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10797 | Selection.setSelection((Spannable) mText, offset); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10798 | } |
| 10799 | |
| 10800 | @Override |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 10801 | public void updatePosition(float x, float y) { |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 10802 | positionAtCursorOffset(getOffsetForPosition(x, y), false); |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 10803 | } |
| 10804 | |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10805 | @Override |
Gilles Debunne | 2037b82 | 2011-04-22 13:07:33 -0700 | [diff] [blame] | 10806 | void onHandleMoved() { |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10807 | super.onHandleMoved(); |
Gilles Debunne | 2037b82 | 2011-04-22 13:07:33 -0700 | [diff] [blame] | 10808 | removeHiderCallback(); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10809 | } |
| 10810 | |
| 10811 | @Override |
| 10812 | public void onDetached() { |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10813 | super.onDetached(); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10814 | removeHiderCallback(); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10815 | } |
| 10816 | } |
| 10817 | |
| 10818 | private class SelectionStartHandleView extends HandleView { |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 10819 | |
| 10820 | public SelectionStartHandleView(Drawable drawableLtr, Drawable drawableRtl) { |
| 10821 | super(drawableLtr, drawableRtl); |
| 10822 | } |
| 10823 | |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10824 | @Override |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 10825 | protected int getHotspotX(Drawable drawable, boolean isRtlRun) { |
| 10826 | if (isRtlRun) { |
| 10827 | return drawable.getIntrinsicWidth() / 4; |
| 10828 | } else { |
| 10829 | return (drawable.getIntrinsicWidth() * 3) / 4; |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10830 | } |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10831 | } |
| 10832 | |
| 10833 | @Override |
| 10834 | public int getCurrentCursorOffset() { |
| 10835 | return TextView.this.getSelectionStart(); |
| 10836 | } |
| 10837 | |
| 10838 | @Override |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10839 | public void updateSelection(int offset) { |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10840 | Selection.setSelection((Spannable) mText, offset, getSelectionEnd()); |
Gilles Debunne | 186aaf9 | 2011-09-16 14:26:12 -0700 | [diff] [blame] | 10841 | updateDrawable(); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10842 | } |
| 10843 | |
| 10844 | @Override |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 10845 | public void updatePosition(float x, float y) { |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 10846 | int offset = getOffsetForPosition(x, y); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10847 | |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10848 | // Handles can not cross and selection is at least one character |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 10849 | final int selectionEnd = getSelectionEnd(); |
Gilles Debunne | 1a22db2 | 2011-11-20 22:13:21 +0100 | [diff] [blame] | 10850 | if (offset >= selectionEnd) offset = Math.max(0, selectionEnd - 1); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10851 | |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 10852 | positionAtCursorOffset(offset, false); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10853 | } |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10854 | |
| 10855 | public ActionPopupWindow getActionPopupWindow() { |
| 10856 | return mActionPopupWindow; |
| 10857 | } |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10858 | } |
| 10859 | |
| 10860 | private class SelectionEndHandleView extends HandleView { |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 10861 | |
| 10862 | public SelectionEndHandleView(Drawable drawableLtr, Drawable drawableRtl) { |
| 10863 | super(drawableLtr, drawableRtl); |
| 10864 | } |
| 10865 | |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10866 | @Override |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 10867 | protected int getHotspotX(Drawable drawable, boolean isRtlRun) { |
| 10868 | if (isRtlRun) { |
| 10869 | return (drawable.getIntrinsicWidth() * 3) / 4; |
| 10870 | } else { |
| 10871 | return drawable.getIntrinsicWidth() / 4; |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10872 | } |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10873 | } |
| 10874 | |
| 10875 | @Override |
| 10876 | public int getCurrentCursorOffset() { |
| 10877 | return TextView.this.getSelectionEnd(); |
| 10878 | } |
| 10879 | |
| 10880 | @Override |
Gilles Debunne | 21078e4 | 2011-08-02 10:22:35 -0700 | [diff] [blame] | 10881 | public void updateSelection(int offset) { |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10882 | Selection.setSelection((Spannable) mText, getSelectionStart(), offset); |
Gilles Debunne | 186aaf9 | 2011-09-16 14:26:12 -0700 | [diff] [blame] | 10883 | updateDrawable(); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10884 | } |
| 10885 | |
| 10886 | @Override |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 10887 | public void updatePosition(float x, float y) { |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 10888 | int offset = getOffsetForPosition(x, y); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10889 | |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10890 | // Handles can not cross and selection is at least one character |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 10891 | final int selectionStart = getSelectionStart(); |
Gilles Debunne | 1a22db2 | 2011-11-20 22:13:21 +0100 | [diff] [blame] | 10892 | if (offset <= selectionStart) offset = Math.min(selectionStart + 1, mText.length()); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10893 | |
Gilles Debunne | f682a77 | 2011-08-31 15:49:10 -0700 | [diff] [blame] | 10894 | positionAtCursorOffset(offset, false); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10895 | } |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10896 | |
| 10897 | public void setActionPopupWindow(ActionPopupWindow actionPopupWindow) { |
| 10898 | mActionPopupWindow = actionPopupWindow; |
| 10899 | } |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10900 | } |
| 10901 | |
| 10902 | /** |
| 10903 | * A CursorController instance can be used to control a cursor in the text. |
| 10904 | * It is not used outside of {@link TextView}. |
| 10905 | * @hide |
| 10906 | */ |
| 10907 | private interface CursorController extends ViewTreeObserver.OnTouchModeChangeListener { |
| 10908 | /** |
| 10909 | * Makes the cursor controller visible on screen. Will be drawn by {@link #draw(Canvas)}. |
| 10910 | * See also {@link #hide()}. |
| 10911 | */ |
| 10912 | public void show(); |
| 10913 | |
| 10914 | /** |
| 10915 | * Hide the cursor controller from screen. |
| 10916 | * See also {@link #show()}. |
| 10917 | */ |
| 10918 | public void hide(); |
| 10919 | |
| 10920 | /** |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10921 | * Called when the view is detached from window. Perform house keeping task, such as |
| 10922 | * stopping Runnable thread that would otherwise keep a reference on the context, thus |
| 10923 | * preventing the activity from being recycled. |
| 10924 | */ |
| 10925 | public void onDetached(); |
| 10926 | } |
| 10927 | |
| 10928 | private class InsertionPointCursorController implements CursorController { |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10929 | private InsertionHandleView mHandle; |
| 10930 | |
| 10931 | public void show() { |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10932 | getHandle().show(); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10933 | } |
| 10934 | |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10935 | public void showWithActionPopup() { |
| 10936 | getHandle().showWithActionPopup(); |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10937 | } |
| 10938 | |
| 10939 | public void hide() { |
| 10940 | if (mHandle != null) { |
| 10941 | mHandle.hide(); |
| 10942 | } |
Gilles Debunne | aa8d73b | 2011-01-17 09:16:40 -0800 | [diff] [blame] | 10943 | } |
| 10944 | |
Adam Powell | 624380a | 2010-10-02 18:12:02 -0700 | [diff] [blame] | 10945 | public void onTouchModeChanged(boolean isInTouchMode) { |
| 10946 | if (!isInTouchMode) { |
| 10947 | hide(); |
| 10948 | } |
| 10949 | } |
Gilles Debunne | c4440f0 | 2010-11-24 14:40:48 -0800 | [diff] [blame] | 10950 | |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 10951 | private InsertionHandleView getHandle() { |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 10952 | if (mSelectHandleCenter == null) { |
| 10953 | mSelectHandleCenter = mContext.getResources().getDrawable( |
| 10954 | mTextSelectHandleRes); |
| 10955 | } |
Gilles Debunne | c4440f0 | 2010-11-24 14:40:48 -0800 | [diff] [blame] | 10956 | if (mHandle == null) { |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 10957 | mHandle = new InsertionHandleView(mSelectHandleCenter); |
Gilles Debunne | c4440f0 | 2010-11-24 14:40:48 -0800 | [diff] [blame] | 10958 | } |
| 10959 | return mHandle; |
| 10960 | } |
Gilles Debunne | 7eeba5f | 2010-12-10 16:55:55 -0800 | [diff] [blame] | 10961 | |
| 10962 | @Override |
| 10963 | public void onDetached() { |
Gilles Debunne | 6f72cf8 | 2011-03-24 16:35:28 -0700 | [diff] [blame] | 10964 | final ViewTreeObserver observer = getViewTreeObserver(); |
| 10965 | observer.removeOnTouchModeChangeListener(this); |
| 10966 | |
Gilles Debunne | 6a85508 | 2011-03-11 16:51:24 -0800 | [diff] [blame] | 10967 | if (mHandle != null) mHandle.onDetached(); |
Gilles Debunne | 7eeba5f | 2010-12-10 16:55:55 -0800 | [diff] [blame] | 10968 | } |
Gilles Debunne | cc3ec6c | 2010-06-23 10:30:27 -0700 | [diff] [blame] | 10969 | } |
| 10970 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 10971 | private class SelectionModifierCursorController implements CursorController { |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 10972 | private static final int DELAY_BEFORE_REPLACE_ACTION = 200; // milliseconds |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 10973 | // The cursor controller handles, lazily created when shown. |
| 10974 | private SelectionStartHandleView mStartHandle; |
| 10975 | private SelectionEndHandleView mEndHandle; |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 10976 | // The offsets of that last touch down event. Remembered to start selection there. |
| 10977 | private int mMinTouchOffset, mMaxTouchOffset; |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 10978 | |
Gilles Debunne | 5347c58 | 2010-10-27 14:22:35 -0700 | [diff] [blame] | 10979 | // Double tap detection |
| 10980 | private long mPreviousTapUpTime = 0; |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 10981 | private float mPreviousTapPositionX, mPreviousTapPositionY; |
Gilles Debunne | 5347c58 | 2010-10-27 14:22:35 -0700 | [diff] [blame] | 10982 | |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 10983 | SelectionModifierCursorController() { |
Gilles Debunne | 380b604 | 2010-10-08 16:12:11 -0700 | [diff] [blame] | 10984 | resetTouchOffsets(); |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 10985 | } |
| 10986 | |
| 10987 | public void show() { |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 10988 | if (isInBatchEditMode()) { |
| 10989 | return; |
| 10990 | } |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 10991 | initDrawables(); |
| 10992 | initHandles(); |
| 10993 | hideInsertionPointCursorController(); |
| 10994 | } |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 10995 | |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 10996 | private void initDrawables() { |
| 10997 | if (mSelectHandleLeft == null) { |
| 10998 | mSelectHandleLeft = mContext.getResources().getDrawable( |
| 10999 | mTextSelectHandleLeftRes); |
| 11000 | } |
| 11001 | if (mSelectHandleRight == null) { |
| 11002 | mSelectHandleRight = mContext.getResources().getDrawable( |
| 11003 | mTextSelectHandleRightRes); |
| 11004 | } |
| 11005 | } |
| 11006 | |
| 11007 | private void initHandles() { |
Gilles Debunne | c4440f0 | 2010-11-24 14:40:48 -0800 | [diff] [blame] | 11008 | // Lazy object creation has to be done before updatePosition() is called. |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 11009 | if (mStartHandle == null) { |
| 11010 | mStartHandle = new SelectionStartHandleView(mSelectHandleLeft, mSelectHandleRight); |
| 11011 | } |
| 11012 | if (mEndHandle == null) { |
| 11013 | mEndHandle = new SelectionEndHandleView(mSelectHandleRight, mSelectHandleLeft); |
| 11014 | } |
Gilles Debunne | c4440f0 | 2010-11-24 14:40:48 -0800 | [diff] [blame] | 11015 | |
Adam Powell | 879fb6b | 2010-09-20 11:23:56 -0700 | [diff] [blame] | 11016 | mStartHandle.show(); |
| 11017 | mEndHandle.show(); |
Gilles Debunne | c4440f0 | 2010-11-24 14:40:48 -0800 | [diff] [blame] | 11018 | |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 11019 | // Make sure both left and right handles share the same ActionPopupWindow (so that |
| 11020 | // moving any of the handles hides the action popup). |
Gilles Debunne | cb2516b | 2011-08-05 17:02:54 -0700 | [diff] [blame] | 11021 | mStartHandle.showActionPopupWindow(DELAY_BEFORE_REPLACE_ACTION); |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 11022 | mEndHandle.setActionPopupWindow(mStartHandle.getActionPopupWindow()); |
| 11023 | |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 11024 | hideInsertionPointCursorController(); |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 11025 | } |
| 11026 | |
| 11027 | public void hide() { |
Gilles Debunne | c4440f0 | 2010-11-24 14:40:48 -0800 | [diff] [blame] | 11028 | if (mStartHandle != null) mStartHandle.hide(); |
| 11029 | if (mEndHandle != null) mEndHandle.hide(); |
Gilles Debunne | aa8d73b | 2011-01-17 09:16:40 -0800 | [diff] [blame] | 11030 | } |
| 11031 | |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 11032 | public void onTouchEvent(MotionEvent event) { |
Gilles Debunne | 528c6488 | 2010-10-08 11:56:13 -0700 | [diff] [blame] | 11033 | // This is done even when the View does not have focus, so that long presses can start |
| 11034 | // selection and tap can move cursor from this tap position. |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 11035 | switch (event.getActionMasked()) { |
| 11036 | case MotionEvent.ACTION_DOWN: |
| 11037 | final float x = event.getX(); |
| 11038 | final float y = event.getY(); |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 11039 | |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 11040 | // Remember finger down position, to be able to start selection from there |
| 11041 | mMinTouchOffset = mMaxTouchOffset = getOffsetForPosition(x, y); |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 11042 | |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 11043 | // Double tap detection |
| 11044 | long duration = SystemClock.uptimeMillis() - mPreviousTapUpTime; |
| 11045 | if (duration <= ViewConfiguration.getDoubleTapTimeout() && |
| 11046 | isPositionOnText(x, y)) { |
| 11047 | final float deltaX = x - mPreviousTapPositionX; |
| 11048 | final float deltaY = y - mPreviousTapPositionY; |
| 11049 | final float distanceSquared = deltaX * deltaX + deltaY * deltaY; |
| 11050 | if (distanceSquared < mSquaredTouchSlopDistance) { |
Gilles Debunne | 646f856 | 2011-07-27 17:44:03 -0700 | [diff] [blame] | 11051 | startSelectionActionMode(); |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 11052 | mDiscardNextActionUp = true; |
Gilles Debunne | 5347c58 | 2010-10-27 14:22:35 -0700 | [diff] [blame] | 11053 | } |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 11054 | } |
Gilles Debunne | 5347c58 | 2010-10-27 14:22:35 -0700 | [diff] [blame] | 11055 | |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 11056 | mPreviousTapPositionX = x; |
| 11057 | mPreviousTapPositionY = y; |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 11058 | break; |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 11059 | |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 11060 | case MotionEvent.ACTION_POINTER_DOWN: |
| 11061 | case MotionEvent.ACTION_POINTER_UP: |
| 11062 | // Handle multi-point gestures. Keep min and max offset positions. |
| 11063 | // Only activated for devices that correctly handle multi-touch. |
| 11064 | if (mContext.getPackageManager().hasSystemFeature( |
| 11065 | PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT)) { |
| 11066 | updateMinAndMaxOffsets(event); |
| 11067 | } |
| 11068 | break; |
Gilles Debunne | 5347c58 | 2010-10-27 14:22:35 -0700 | [diff] [blame] | 11069 | |
Gilles Debunne | bb588da | 2011-07-11 18:26:19 -0700 | [diff] [blame] | 11070 | case MotionEvent.ACTION_UP: |
| 11071 | mPreviousTapUpTime = SystemClock.uptimeMillis(); |
| 11072 | break; |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 11073 | } |
| 11074 | } |
| 11075 | |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 11076 | /** |
| 11077 | * @param event |
| 11078 | */ |
| 11079 | private void updateMinAndMaxOffsets(MotionEvent event) { |
| 11080 | int pointerCount = event.getPointerCount(); |
| 11081 | for (int index = 0; index < pointerCount; index++) { |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 11082 | int offset = getOffsetForPosition(event.getX(index), event.getY(index)); |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 11083 | if (offset < mMinTouchOffset) mMinTouchOffset = offset; |
| 11084 | if (offset > mMaxTouchOffset) mMaxTouchOffset = offset; |
| 11085 | } |
| 11086 | } |
| 11087 | |
| 11088 | public int getMinTouchOffset() { |
| 11089 | return mMinTouchOffset; |
| 11090 | } |
| 11091 | |
| 11092 | public int getMaxTouchOffset() { |
| 11093 | return mMaxTouchOffset; |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 11094 | } |
| 11095 | |
Gilles Debunne | 380b604 | 2010-10-08 16:12:11 -0700 | [diff] [blame] | 11096 | public void resetTouchOffsets() { |
| 11097 | mMinTouchOffset = mMaxTouchOffset = -1; |
| 11098 | } |
| 11099 | |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 11100 | /** |
| 11101 | * @return true iff this controller is currently used to move the selection start. |
| 11102 | */ |
| 11103 | public boolean isSelectionStartDragged() { |
Gilles Debunne | c4440f0 | 2010-11-24 14:40:48 -0800 | [diff] [blame] | 11104 | return mStartHandle != null && mStartHandle.isDragging(); |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 11105 | } |
Adam Powell | 624380a | 2010-10-02 18:12:02 -0700 | [diff] [blame] | 11106 | |
| 11107 | public void onTouchModeChanged(boolean isInTouchMode) { |
| 11108 | if (!isInTouchMode) { |
| 11109 | hide(); |
| 11110 | } |
| 11111 | } |
Gilles Debunne | 7eeba5f | 2010-12-10 16:55:55 -0800 | [diff] [blame] | 11112 | |
| 11113 | @Override |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 11114 | public void onDetached() { |
Gilles Debunne | 6f72cf8 | 2011-03-24 16:35:28 -0700 | [diff] [blame] | 11115 | final ViewTreeObserver observer = getViewTreeObserver(); |
| 11116 | observer.removeOnTouchModeChangeListener(this); |
| 11117 | |
Gilles Debunne | 180bb1b | 2011-03-10 11:14:00 -0800 | [diff] [blame] | 11118 | if (mStartHandle != null) mStartHandle.onDetached(); |
| 11119 | if (mEndHandle != null) mEndHandle.onDetached(); |
| 11120 | } |
Gilles Debunne | 0533627 | 2010-07-09 20:13:45 -0700 | [diff] [blame] | 11121 | } |
| 11122 | |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 11123 | private void hideInsertionPointCursorController() { |
Gilles Debunne | e587d83 | 2010-11-23 20:20:11 -0800 | [diff] [blame] | 11124 | // No need to create the controller to hide it. |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 11125 | if (mInsertionPointCursorController != null) { |
| 11126 | mInsertionPointCursorController.hide(); |
| 11127 | } |
| 11128 | } |
| 11129 | |
Gilles Debunne | d94f8c5 | 2011-01-10 11:29:15 -0800 | [diff] [blame] | 11130 | /** |
| 11131 | * Hides the insertion controller and stops text selection mode, hiding the selection controller |
| 11132 | */ |
Gilles Debunne | a6d7ee1 | 2010-08-13 14:43:10 -0700 | [diff] [blame] | 11133 | private void hideControllers() { |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 11134 | hideCursorControllers(); |
| 11135 | hideSpanControllers(); |
| 11136 | } |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 11137 | |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 11138 | private void hideSpanControllers() { |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 11139 | if (mChangeWatcher != null) { |
| 11140 | mChangeWatcher.hideControllers(); |
| 11141 | } |
Gilles Debunne | a6d7ee1 | 2010-08-13 14:43:10 -0700 | [diff] [blame] | 11142 | } |
| 11143 | |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 11144 | private void hideCursorControllers() { |
Gilles Debunne | 26c8b3a | 2011-10-12 14:06:58 -0700 | [diff] [blame] | 11145 | if (mSuggestionsPopupWindow != null && !mSuggestionsPopupWindow.isShowingUp()) { |
| 11146 | // Should be done before hide insertion point controller since it triggers a show of it |
| 11147 | mSuggestionsPopupWindow.hide(); |
| 11148 | } |
Luca Zanolin | 1564fc7 | 2011-09-07 00:01:28 +0100 | [diff] [blame] | 11149 | hideInsertionPointCursorController(); |
| 11150 | stopSelectionActionMode(); |
| 11151 | } |
| 11152 | |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 11153 | /** |
Gilles Debunne | 9511b41 | 2011-05-23 18:33:48 -0700 | [diff] [blame] | 11154 | * Get the character offset closest to the specified absolute position. A typical use case is to |
| 11155 | * pass the result of {@link MotionEvent#getX()} and {@link MotionEvent#getY()} to this method. |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 11156 | * |
| 11157 | * @param x The horizontal absolute position of a point on screen |
| 11158 | * @param y The vertical absolute position of a point on screen |
| 11159 | * @return the character offset for the character whose position is closest to the specified |
| 11160 | * position. Returns -1 if there is no layout. |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 11161 | */ |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 11162 | public int getOffsetForPosition(float x, float y) { |
Gilles Debunne | 3e05a0b | 2010-08-23 14:55:06 -0700 | [diff] [blame] | 11163 | if (getLayout() == null) return -1; |
Gilles Debunne | 9948ad7 | 2010-11-24 14:00:46 -0800 | [diff] [blame] | 11164 | final int line = getLineAtCoordinate(y); |
| 11165 | final int offset = getOffsetAtCoordinate(line, x); |
Gilles Debunne | 3e05a0b | 2010-08-23 14:55:06 -0700 | [diff] [blame] | 11166 | return offset; |
| 11167 | } |
| 11168 | |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 11169 | private float convertToLocalHorizontalCoordinate(float x) { |
Gilles Debunne | 9948ad7 | 2010-11-24 14:00:46 -0800 | [diff] [blame] | 11170 | x -= getTotalPaddingLeft(); |
| 11171 | // Clamp the position to inside of the view. |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 11172 | x = Math.max(0.0f, x); |
Gilles Debunne | 9948ad7 | 2010-11-24 14:00:46 -0800 | [diff] [blame] | 11173 | x = Math.min(getWidth() - getTotalPaddingRight() - 1, x); |
| 11174 | x += getScrollX(); |
| 11175 | return x; |
| 11176 | } |
| 11177 | |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 11178 | private int getLineAtCoordinate(float y) { |
Gilles Debunne | 9948ad7 | 2010-11-24 14:00:46 -0800 | [diff] [blame] | 11179 | y -= getTotalPaddingTop(); |
| 11180 | // Clamp the position to inside of the view. |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 11181 | y = Math.max(0.0f, y); |
Gilles Debunne | 9948ad7 | 2010-11-24 14:00:46 -0800 | [diff] [blame] | 11182 | y = Math.min(getHeight() - getTotalPaddingBottom() - 1, y); |
| 11183 | y += getScrollY(); |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 11184 | return getLayout().getLineForVertical((int) y); |
Gilles Debunne | 9948ad7 | 2010-11-24 14:00:46 -0800 | [diff] [blame] | 11185 | } |
| 11186 | |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 11187 | private int getOffsetAtCoordinate(int line, float x) { |
Gilles Debunne | 9948ad7 | 2010-11-24 14:00:46 -0800 | [diff] [blame] | 11188 | x = convertToLocalHorizontalCoordinate(x); |
| 11189 | return getLayout().getOffsetForHorizontal(line, x); |
| 11190 | } |
| 11191 | |
| 11192 | /** Returns true if the screen coordinates position (x,y) corresponds to a character displayed |
| 11193 | * in the view. Returns false when the position is in the empty space of left/right of text. |
| 11194 | */ |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 11195 | private boolean isPositionOnText(float x, float y) { |
Gilles Debunne | 9948ad7 | 2010-11-24 14:00:46 -0800 | [diff] [blame] | 11196 | if (getLayout() == null) return false; |
| 11197 | |
| 11198 | final int line = getLineAtCoordinate(y); |
| 11199 | x = convertToLocalHorizontalCoordinate(x); |
| 11200 | |
| 11201 | if (x < getLayout().getLineLeft(line)) return false; |
| 11202 | if (x > getLayout().getLineRight(line)) return false; |
| 11203 | return true; |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 11204 | } |
| 11205 | |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 11206 | @Override |
| 11207 | public boolean onDragEvent(DragEvent event) { |
| 11208 | switch (event.getAction()) { |
| 11209 | case DragEvent.ACTION_DRAG_STARTED: |
Gilles Debunne | e587d83 | 2010-11-23 20:20:11 -0800 | [diff] [blame] | 11210 | return hasInsertionController(); |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 11211 | |
| 11212 | case DragEvent.ACTION_DRAG_ENTERED: |
| 11213 | TextView.this.requestFocus(); |
| 11214 | return true; |
| 11215 | |
Gilles Debunne | 2226a19 | 2010-11-30 18:50:51 -0800 | [diff] [blame] | 11216 | case DragEvent.ACTION_DRAG_LOCATION: |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 11217 | final int offset = getOffsetForPosition(event.getX(), event.getY()); |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 11218 | Selection.setSelection((Spannable)mText, offset); |
| 11219 | return true; |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 11220 | |
Gilles Debunne | 2226a19 | 2010-11-30 18:50:51 -0800 | [diff] [blame] | 11221 | case DragEvent.ACTION_DROP: |
| 11222 | onDrop(event); |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 11223 | return true; |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 11224 | |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 11225 | case DragEvent.ACTION_DRAG_ENDED: |
Gilles Debunne | 4ae0f29 | 2010-11-29 14:56:39 -0800 | [diff] [blame] | 11226 | case DragEvent.ACTION_DRAG_EXITED: |
Gilles Debunne | f170a34 | 2010-11-11 11:08:59 -0800 | [diff] [blame] | 11227 | default: |
| 11228 | return true; |
| 11229 | } |
| 11230 | } |
| 11231 | |
Gilles Debunne | 2226a19 | 2010-11-30 18:50:51 -0800 | [diff] [blame] | 11232 | private void onDrop(DragEvent event) { |
| 11233 | StringBuilder content = new StringBuilder(""); |
| 11234 | ClipData clipData = event.getClipData(); |
| 11235 | final int itemCount = clipData.getItemCount(); |
| 11236 | for (int i=0; i < itemCount; i++) { |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 11237 | Item item = clipData.getItemAt(i); |
Gilles Debunne | 2226a19 | 2010-11-30 18:50:51 -0800 | [diff] [blame] | 11238 | content.append(item.coerceToText(TextView.this.mContext)); |
| 11239 | } |
| 11240 | |
Gilles Debunne | 3bca69b | 2011-05-23 18:20:22 -0700 | [diff] [blame] | 11241 | final int offset = getOffsetForPosition(event.getX(), event.getY()); |
Gilles Debunne | 2226a19 | 2010-11-30 18:50:51 -0800 | [diff] [blame] | 11242 | |
Gilles Debunne | aaa8479 | 2010-12-03 11:10:14 -0800 | [diff] [blame] | 11243 | Object localState = event.getLocalState(); |
| 11244 | DragLocalState dragLocalState = null; |
| 11245 | if (localState instanceof DragLocalState) { |
| 11246 | dragLocalState = (DragLocalState) localState; |
| 11247 | } |
| 11248 | boolean dragDropIntoItself = dragLocalState != null && |
| 11249 | dragLocalState.sourceTextView == this; |
| 11250 | |
| 11251 | if (dragDropIntoItself) { |
| 11252 | if (offset >= dragLocalState.start && offset < dragLocalState.end) { |
Gilles Debunne | 2226a19 | 2010-11-30 18:50:51 -0800 | [diff] [blame] | 11253 | // A drop inside the original selection discards the drop. |
| 11254 | return; |
| 11255 | } |
| 11256 | } |
| 11257 | |
| 11258 | final int originalLength = mText.length(); |
| 11259 | long minMax = prepareSpacesAroundPaste(offset, offset, content); |
| 11260 | int min = extractRangeStartFromLong(minMax); |
| 11261 | int max = extractRangeEndFromLong(minMax); |
| 11262 | |
| 11263 | Selection.setSelection((Spannable) mText, max); |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 11264 | replaceText_internal(min, max, content); |
Gilles Debunne | 2226a19 | 2010-11-30 18:50:51 -0800 | [diff] [blame] | 11265 | |
Gilles Debunne | aaa8479 | 2010-12-03 11:10:14 -0800 | [diff] [blame] | 11266 | if (dragDropIntoItself) { |
| 11267 | int dragSourceStart = dragLocalState.start; |
| 11268 | int dragSourceEnd = dragLocalState.end; |
Gilles Debunne | 2226a19 | 2010-11-30 18:50:51 -0800 | [diff] [blame] | 11269 | if (max <= dragSourceStart) { |
| 11270 | // Inserting text before selection has shifted positions |
| 11271 | final int shift = mText.length() - originalLength; |
| 11272 | dragSourceStart += shift; |
| 11273 | dragSourceEnd += shift; |
| 11274 | } |
| 11275 | |
| 11276 | // Delete original selection |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 11277 | deleteText_internal(dragSourceStart, dragSourceEnd); |
Gilles Debunne | 2226a19 | 2010-11-30 18:50:51 -0800 | [diff] [blame] | 11278 | |
| 11279 | // Make sure we do not leave two adjacent spaces. |
| 11280 | if ((dragSourceStart == 0 || |
| 11281 | Character.isSpaceChar(mTransformed.charAt(dragSourceStart - 1))) && |
| 11282 | (dragSourceStart == mText.length() || |
| 11283 | Character.isSpaceChar(mTransformed.charAt(dragSourceStart)))) { |
| 11284 | final int pos = dragSourceStart == mText.length() ? |
| 11285 | dragSourceStart - 1 : dragSourceStart; |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 11286 | deleteText_internal(pos, pos + 1); |
Gilles Debunne | 2226a19 | 2010-11-30 18:50:51 -0800 | [diff] [blame] | 11287 | } |
| 11288 | } |
| 11289 | } |
| 11290 | |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 11291 | /** |
| 11292 | * @return True if this view supports insertion handles. |
| 11293 | */ |
| 11294 | boolean hasInsertionController() { |
| 11295 | return mInsertionControllerEnabled; |
| 11296 | } |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 11297 | |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 11298 | /** |
| 11299 | * @return True if this view supports selection handles. |
| 11300 | */ |
| 11301 | boolean hasSelectionController() { |
| 11302 | return mSelectionControllerEnabled; |
| 11303 | } |
| 11304 | |
Gilles Debunne | 9948ad7 | 2010-11-24 14:00:46 -0800 | [diff] [blame] | 11305 | InsertionPointCursorController getInsertionController() { |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 11306 | if (!mInsertionControllerEnabled) { |
| 11307 | return null; |
| 11308 | } |
| 11309 | |
| 11310 | if (mInsertionPointCursorController == null) { |
| 11311 | mInsertionPointCursorController = new InsertionPointCursorController(); |
| 11312 | |
| 11313 | final ViewTreeObserver observer = getViewTreeObserver(); |
Gilles Debunne | 81f0808 | 2011-02-17 14:07:19 -0800 | [diff] [blame] | 11314 | observer.addOnTouchModeChangeListener(mInsertionPointCursorController); |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 11315 | } |
| 11316 | |
| 11317 | return mInsertionPointCursorController; |
| 11318 | } |
| 11319 | |
Gilles Debunne | e587d83 | 2010-11-23 20:20:11 -0800 | [diff] [blame] | 11320 | SelectionModifierCursorController getSelectionController() { |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 11321 | if (!mSelectionControllerEnabled) { |
| 11322 | return null; |
| 11323 | } |
| 11324 | |
| 11325 | if (mSelectionModifierCursorController == null) { |
| 11326 | mSelectionModifierCursorController = new SelectionModifierCursorController(); |
| 11327 | |
| 11328 | final ViewTreeObserver observer = getViewTreeObserver(); |
Gilles Debunne | 81f0808 | 2011-02-17 14:07:19 -0800 | [diff] [blame] | 11329 | observer.addOnTouchModeChangeListener(mSelectionModifierCursorController); |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 11330 | } |
| 11331 | |
| 11332 | return mSelectionModifierCursorController; |
| 11333 | } |
| 11334 | |
| 11335 | boolean isInBatchEditMode() { |
| 11336 | final InputMethodState ims = mInputMethodState; |
| 11337 | if (ims != null) { |
| 11338 | return ims.mBatchEditNesting > 0; |
| 11339 | } |
| 11340 | return mInBatchEditControllers; |
| 11341 | } |
| 11342 | |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 11343 | @Override |
| 11344 | protected void resolveTextDirection() { |
Fabrice Di Meglio | 7810b5f | 2011-08-24 18:26:14 -0700 | [diff] [blame] | 11345 | if (hasPasswordTransformationMethod()) { |
| 11346 | mTextDir = TextDirectionHeuristics.LOCALE; |
| 11347 | return; |
| 11348 | } |
| 11349 | |
Fabrice Di Meglio | 4b60c30 | 2011-08-17 16:56:55 -0700 | [diff] [blame] | 11350 | // Always need to resolve layout direction first |
| 11351 | final boolean defaultIsRtl = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL); |
| 11352 | |
| 11353 | // Then resolve text direction on the parent |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 11354 | super.resolveTextDirection(); |
| 11355 | |
Fabrice Di Meglio | 4b60c30 | 2011-08-17 16:56:55 -0700 | [diff] [blame] | 11356 | // Now, we can select the heuristic |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 11357 | int textDir = getResolvedTextDirection(); |
| 11358 | switch (textDir) { |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 11359 | default: |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 11360 | case TEXT_DIRECTION_FIRST_STRONG: |
Fabrice Di Meglio | 4b60c30 | 2011-08-17 16:56:55 -0700 | [diff] [blame] | 11361 | mTextDir = (defaultIsRtl ? TextDirectionHeuristics.FIRSTSTRONG_RTL : |
| 11362 | TextDirectionHeuristics.FIRSTSTRONG_LTR); |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 11363 | break; |
| 11364 | case TEXT_DIRECTION_ANY_RTL: |
Fabrice Di Meglio | a646145 | 2011-08-19 15:42:04 -0700 | [diff] [blame] | 11365 | mTextDir = TextDirectionHeuristics.ANYRTL_LTR; |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 11366 | break; |
| 11367 | case TEXT_DIRECTION_LTR: |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 11368 | mTextDir = TextDirectionHeuristics.LTR; |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 11369 | break; |
| 11370 | case TEXT_DIRECTION_RTL: |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 11371 | mTextDir = TextDirectionHeuristics.RTL; |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 11372 | break; |
| 11373 | } |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 11374 | } |
| 11375 | |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 11376 | /** |
| 11377 | * Subclasses will need to override this method to implement their own way of resolving |
| 11378 | * drawables depending on the layout direction. |
| 11379 | * |
| 11380 | * A call to the super method will be required from the subclasses implementation. |
| 11381 | * |
| 11382 | */ |
| 11383 | protected void resolveDrawables() { |
| 11384 | // No need to resolve twice |
Fabrice Di Meglio | f66fdad | 2011-07-19 13:08:47 -0700 | [diff] [blame] | 11385 | if (mResolvedDrawables) { |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 11386 | return; |
| 11387 | } |
| 11388 | // No drawable to resolve |
| 11389 | if (mDrawables == null) { |
| 11390 | return; |
| 11391 | } |
| 11392 | // No relative drawable to resolve |
| 11393 | if (mDrawables.mDrawableStart == null && mDrawables.mDrawableEnd == null) { |
Fabrice Di Meglio | f66fdad | 2011-07-19 13:08:47 -0700 | [diff] [blame] | 11394 | mResolvedDrawables = true; |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 11395 | return; |
| 11396 | } |
| 11397 | |
| 11398 | Drawables dr = mDrawables; |
| 11399 | switch(getResolvedLayoutDirection()) { |
| 11400 | case LAYOUT_DIRECTION_RTL: |
| 11401 | if (dr.mDrawableStart != null) { |
| 11402 | dr.mDrawableRight = dr.mDrawableStart; |
| 11403 | |
| 11404 | dr.mDrawableSizeRight = dr.mDrawableSizeStart; |
| 11405 | dr.mDrawableHeightRight = dr.mDrawableHeightStart; |
| 11406 | } |
| 11407 | if (dr.mDrawableEnd != null) { |
| 11408 | dr.mDrawableLeft = dr.mDrawableEnd; |
| 11409 | |
| 11410 | dr.mDrawableSizeLeft = dr.mDrawableSizeEnd; |
| 11411 | dr.mDrawableHeightLeft = dr.mDrawableHeightEnd; |
| 11412 | } |
| 11413 | break; |
| 11414 | |
| 11415 | case LAYOUT_DIRECTION_LTR: |
| 11416 | default: |
| 11417 | if (dr.mDrawableStart != null) { |
| 11418 | dr.mDrawableLeft = dr.mDrawableStart; |
| 11419 | |
| 11420 | dr.mDrawableSizeLeft = dr.mDrawableSizeStart; |
| 11421 | dr.mDrawableHeightLeft = dr.mDrawableHeightStart; |
| 11422 | } |
| 11423 | if (dr.mDrawableEnd != null) { |
| 11424 | dr.mDrawableRight = dr.mDrawableEnd; |
| 11425 | |
| 11426 | dr.mDrawableSizeRight = dr.mDrawableSizeEnd; |
| 11427 | dr.mDrawableHeightRight = dr.mDrawableHeightEnd; |
| 11428 | } |
| 11429 | break; |
| 11430 | } |
Fabrice Di Meglio | f66fdad | 2011-07-19 13:08:47 -0700 | [diff] [blame] | 11431 | mResolvedDrawables = true; |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 11432 | } |
| 11433 | |
| 11434 | protected void resetResolvedDrawables() { |
Fabrice Di Meglio | f66fdad | 2011-07-19 13:08:47 -0700 | [diff] [blame] | 11435 | mResolvedDrawables = false; |
Fabrice Di Meglio | a3b6b95 | 2011-06-29 16:44:43 -0700 | [diff] [blame] | 11436 | } |
| 11437 | |
satok | a67a3cf | 2011-09-07 17:14:03 +0900 | [diff] [blame] | 11438 | /** |
| 11439 | * @hide |
| 11440 | */ |
| 11441 | protected void viewClicked(InputMethodManager imm) { |
| 11442 | if (imm != null) { |
| 11443 | imm.viewClicked(this); |
| 11444 | } |
| 11445 | } |
| 11446 | |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 11447 | /** |
| 11448 | * Deletes the range of text [start, end[. |
| 11449 | * @hide |
| 11450 | */ |
| 11451 | protected void deleteText_internal(int start, int end) { |
| 11452 | ((Editable) mText).delete(start, end); |
| 11453 | } |
| 11454 | |
| 11455 | /** |
| 11456 | * Replaces the range of text [start, end[ by replacement text |
| 11457 | * @hide |
| 11458 | */ |
| 11459 | protected void replaceText_internal(int start, int end, CharSequence text) { |
| 11460 | ((Editable) mText).replace(start, end, text); |
| 11461 | } |
| 11462 | |
Gilles Debunne | e300be9 | 2011-12-06 10:15:56 -0800 | [diff] [blame] | 11463 | /** |
| 11464 | * Sets a span on the specified range of text |
| 11465 | * @hide |
| 11466 | */ |
| 11467 | protected void setSpan_internal(Object span, int start, int end, int flags) { |
| 11468 | ((Editable) mText).setSpan(span, start, end, flags); |
| 11469 | } |
| 11470 | |
| 11471 | /** |
| 11472 | * Moves the cursor to the specified offset position in text |
| 11473 | * @hide |
| 11474 | */ |
| 11475 | protected void setCursorPosition_internal(int start, int end) { |
| 11476 | Selection.setSelection(((Editable) mText), start, end); |
| 11477 | } |
| 11478 | |
Romain Guy | 6d95662 | 2010-11-29 11:38:05 -0800 | [diff] [blame] | 11479 | @ViewDebug.ExportedProperty(category = "text") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11480 | private CharSequence mText; |
| 11481 | private CharSequence mTransformed; |
| 11482 | private BufferType mBufferType = BufferType.NORMAL; |
| 11483 | |
| 11484 | private int mInputType = EditorInfo.TYPE_NULL; |
| 11485 | private CharSequence mHint; |
| 11486 | private Layout mHintLayout; |
| 11487 | |
| 11488 | private KeyListener mInput; |
| 11489 | |
| 11490 | private MovementMethod mMovement; |
| 11491 | private TransformationMethod mTransformation; |
Adam Powell | 7f8f79a | 2011-07-07 18:35:54 -0700 | [diff] [blame] | 11492 | private boolean mAllowTransformationLengthChange; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11493 | private ChangeWatcher mChangeWatcher; |
| 11494 | |
| 11495 | private ArrayList<TextWatcher> mListeners = null; |
| 11496 | |
| 11497 | // display attributes |
Gilles Debunne | b6ca723 | 2010-06-24 11:45:21 -0700 | [diff] [blame] | 11498 | private final TextPaint mTextPaint; |
Romain Guy | 939151f | 2009-04-08 14:22:40 -0700 | [diff] [blame] | 11499 | private boolean mUserSetTextScaleX; |
Gilles Debunne | b6ca723 | 2010-06-24 11:45:21 -0700 | [diff] [blame] | 11500 | private final Paint mHighlightPaint; |
Gilles Debunne | 6fbe5ca | 2011-09-06 15:24:45 -0700 | [diff] [blame] | 11501 | private int mHighlightColor = 0x6633B5E5; |
Gilles Debunne | cdfab19 | 2011-11-28 18:05:03 -0800 | [diff] [blame] | 11502 | private Layout mLayout; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11503 | |
| 11504 | private long mShowCursor; |
| 11505 | private Blink mBlink; |
| 11506 | private boolean mCursorVisible = true; |
| 11507 | |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 11508 | // Cursor Controllers. |
Gilles Debunne | 9948ad7 | 2010-11-24 14:00:46 -0800 | [diff] [blame] | 11509 | private InsertionPointCursorController mInsertionPointCursorController; |
Gilles Debunne | e587d83 | 2010-11-23 20:20:11 -0800 | [diff] [blame] | 11510 | private SelectionModifierCursorController mSelectionModifierCursorController; |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 11511 | private ActionMode mSelectionActionMode; |
Adam Powell | 965b969 | 2010-10-21 18:44:32 -0700 | [diff] [blame] | 11512 | private boolean mInsertionControllerEnabled; |
| 11513 | private boolean mSelectionControllerEnabled; |
| 11514 | private boolean mInBatchEditControllers; |
| 11515 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11516 | private boolean mSelectAllOnFocus = false; |
| 11517 | |
Fabrice Di Meglio | 9e3b002 | 2011-06-06 16:30:29 -0700 | [diff] [blame] | 11518 | private int mGravity = Gravity.TOP | Gravity.START; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11519 | private boolean mHorizontallyScrolling; |
| 11520 | |
| 11521 | private int mAutoLinkMask; |
| 11522 | private boolean mLinksClickable = true; |
| 11523 | |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 11524 | private float mSpacingMult = 1.0f; |
| 11525 | private float mSpacingAdd = 0.0f; |
Gilles Debunne | 86b9c78 | 2010-11-11 10:43:48 -0800 | [diff] [blame] | 11526 | private boolean mTextIsSelectable = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11527 | |
| 11528 | private static final int LINES = 1; |
| 11529 | private static final int EMS = LINES; |
| 11530 | private static final int PIXELS = 2; |
| 11531 | |
| 11532 | private int mMaximum = Integer.MAX_VALUE; |
| 11533 | private int mMaxMode = LINES; |
| 11534 | private int mMinimum = 0; |
| 11535 | private int mMinMode = LINES; |
| 11536 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 11537 | private int mOldMaximum = mMaximum; |
| 11538 | private int mOldMaxMode = mMaxMode; |
| 11539 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11540 | private int mMaxWidth = Integer.MAX_VALUE; |
| 11541 | private int mMaxWidthMode = PIXELS; |
| 11542 | private int mMinWidth = 0; |
| 11543 | private int mMinWidthMode = PIXELS; |
| 11544 | |
| 11545 | private boolean mSingleLine; |
| 11546 | private int mDesiredHeightAtMeasure = -1; |
| 11547 | private boolean mIncludePad = true; |
| 11548 | |
| 11549 | // tmp primitives, so we don't alloc them on each draw |
| 11550 | private Path mHighlightPath; |
| 11551 | private boolean mHighlightPathBogus = true; |
| 11552 | private static final RectF sTempRect = new RectF(); |
Gilles Debunne | 64901d4 | 2011-11-25 10:23:38 +0100 | [diff] [blame] | 11553 | private static final float[] sTmpPosition = new float[2]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11554 | |
Jeff Brown | 033a001 | 2011-11-11 15:30:16 -0800 | [diff] [blame] | 11555 | // XXX should be much larger |
| 11556 | private static final int VERY_WIDE = 1024*1024; |
| 11557 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11558 | private static final int BLINK = 500; |
| 11559 | |
| 11560 | private static final int ANIMATED_SCROLL_GAP = 250; |
| 11561 | private long mLastScroll; |
| 11562 | private Scroller mScroller = null; |
| 11563 | |
| 11564 | private BoringLayout.Metrics mBoring; |
| 11565 | private BoringLayout.Metrics mHintBoring; |
| 11566 | |
| 11567 | private BoringLayout mSavedLayout, mSavedHintLayout; |
| 11568 | |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 11569 | private TextDirectionHeuristic mTextDir = null; |
| 11570 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11571 | private static final InputFilter[] NO_FILTERS = new InputFilter[0]; |
| 11572 | private InputFilter[] mFilters = NO_FILTERS; |
| 11573 | private static final Spanned EMPTY_SPANNED = new SpannedString(""); |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 11574 | private static int DRAG_SHADOW_MAX_TEXT_LENGTH = 20; |
Gilles Debunne | 0a2aa40 | 2010-11-24 17:57:46 -0800 | [diff] [blame] | 11575 | // System wide time for last cut or copy action. |
| 11576 | private static long sLastCutOrCopyTime; |
Gilles Debunne | 12d91ce | 2010-12-10 11:36:29 -0800 | [diff] [blame] | 11577 | // Used to highlight a word when it is corrected by the IME |
| 11578 | private CorrectionHighlighter mCorrectionHighlighter; |
Gilles Debunne | da0a3f0 | 2010-12-22 10:07:15 -0800 | [diff] [blame] | 11579 | // New state used to change background based on whether this TextView is multiline. |
| 11580 | private static final int[] MULTILINE_STATE_SET = { R.attr.state_multiline }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11581 | } |