Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
| 19 | import android.R; |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 20 | import android.annotation.IntDef; |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Yoshiki Iguchi | ee14772 | 2015-04-14 00:12:44 +0900 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 23 | import android.app.PendingIntent; |
| 24 | import android.app.PendingIntent.CanceledException; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 25 | import android.content.ClipData; |
| 26 | import android.content.ClipData.Item; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 29 | import android.content.UndoManager; |
| 30 | import android.content.UndoOperation; |
| 31 | import android.content.UndoOwner; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 32 | import android.content.pm.PackageManager; |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 33 | import android.content.pm.ResolveInfo; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 34 | import android.content.res.TypedArray; |
| 35 | import android.graphics.Canvas; |
| 36 | import android.graphics.Color; |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 37 | import android.graphics.Matrix; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 38 | import android.graphics.Paint; |
| 39 | import android.graphics.Path; |
| 40 | import android.graphics.Rect; |
| 41 | import android.graphics.RectF; |
Seigo Nonaka | 3ed1b39 | 2016-01-19 13:54:59 +0900 | [diff] [blame] | 42 | import android.graphics.drawable.ColorDrawable; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 43 | import android.graphics.drawable.Drawable; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 44 | import android.os.Bundle; |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 45 | import android.os.Parcel; |
| 46 | import android.os.Parcelable; |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 47 | import android.os.ParcelableParcel; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 48 | import android.os.SystemClock; |
| 49 | import android.provider.Settings; |
| 50 | import android.text.DynamicLayout; |
| 51 | import android.text.Editable; |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 52 | import android.text.InputFilter; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 53 | import android.text.InputType; |
| 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; |
| 59 | import android.text.SpannableStringBuilder; |
| 60 | import android.text.Spanned; |
| 61 | import android.text.StaticLayout; |
| 62 | import android.text.TextUtils; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 63 | import android.text.method.KeyListener; |
| 64 | import android.text.method.MetaKeyKeyListener; |
| 65 | import android.text.method.MovementMethod; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 66 | import android.text.method.WordIterator; |
| 67 | import android.text.style.EasyEditSpan; |
| 68 | import android.text.style.SuggestionRangeSpan; |
| 69 | import android.text.style.SuggestionSpan; |
| 70 | import android.text.style.TextAppearanceSpan; |
| 71 | import android.text.style.URLSpan; |
| 72 | import android.util.DisplayMetrics; |
Yohei Yukawa | d469f21 | 2016-01-21 12:38:09 -0800 | [diff] [blame] | 73 | import android.util.LocaleList; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 74 | import android.util.Log; |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 75 | import android.util.SparseArray; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 76 | import android.view.ActionMode; |
| 77 | import android.view.ActionMode.Callback; |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 78 | import android.view.ContextMenu; |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 79 | import android.view.DisplayListCanvas; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 80 | import android.view.DragEvent; |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 81 | import android.view.DropPermissions; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 82 | import android.view.Gravity; |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 83 | import android.view.InputDevice; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 84 | import android.view.LayoutInflater; |
| 85 | import android.view.Menu; |
| 86 | import android.view.MenuItem; |
| 87 | import android.view.MotionEvent; |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 88 | import android.view.RenderNode; |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 89 | import android.view.SubMenu; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 90 | import android.view.View; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 91 | import android.view.View.DragShadowBuilder; |
| 92 | import android.view.View.OnClickListener; |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 93 | import android.view.ViewConfiguration; |
| 94 | import android.view.ViewGroup; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 95 | import android.view.ViewGroup.LayoutParams; |
| 96 | import android.view.ViewParent; |
| 97 | import android.view.ViewTreeObserver; |
| 98 | import android.view.WindowManager; |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 99 | import android.view.accessibility.AccessibilityNodeInfo; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 100 | import android.view.inputmethod.CorrectionInfo; |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 101 | import android.view.inputmethod.CursorAnchorInfo; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 102 | import android.view.inputmethod.EditorInfo; |
| 103 | import android.view.inputmethod.ExtractedText; |
| 104 | import android.view.inputmethod.ExtractedTextRequest; |
| 105 | import android.view.inputmethod.InputConnection; |
| 106 | import android.view.inputmethod.InputMethodManager; |
| 107 | import android.widget.AdapterView.OnItemClickListener; |
| 108 | import android.widget.TextView.Drawables; |
| 109 | import android.widget.TextView.OnEditorActionListener; |
| 110 | |
Seigo Nonaka | a60160b | 2015-08-19 12:38:35 -0700 | [diff] [blame] | 111 | import com.android.internal.annotations.VisibleForTesting; |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 112 | import com.android.internal.util.ArrayUtils; |
| 113 | import com.android.internal.util.GrowingArrayUtils; |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 114 | import com.android.internal.util.Preconditions; |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 115 | import com.android.internal.widget.EditableInputConnection; |
| 116 | |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 117 | import java.lang.annotation.Retention; |
| 118 | import java.lang.annotation.RetentionPolicy; |
Andrei Stingaceanu | 2aaeefe | 2015-10-20 19:11:23 +0100 | [diff] [blame] | 119 | import java.text.BreakIterator; |
| 120 | import java.util.Arrays; |
| 121 | import java.util.Comparator; |
| 122 | import java.util.HashMap; |
| 123 | import java.util.List; |
| 124 | |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 125 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 126 | /** |
| 127 | * Helper class used by TextView to handle editable text views. |
| 128 | * |
| 129 | * @hide |
| 130 | */ |
| 131 | public class Editor { |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 132 | private static final String TAG = "Editor"; |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 133 | private static final boolean DEBUG_UNDO = false; |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 134 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 135 | static final int BLINK = 500; |
| 136 | private static final float[] TEMP_POSITION = new float[2]; |
| 137 | private static int DRAG_SHADOW_MAX_TEXT_LENGTH = 20; |
Mady Mellor | cc65c37 | 2015-06-17 09:25:19 -0700 | [diff] [blame] | 138 | private static final float LINE_SLOP_MULTIPLIER_FOR_HANDLEVIEWS = 0.5f; |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 139 | private static final int UNSET_X_VALUE = -1; |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 140 | private static final int UNSET_LINE = -1; |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 141 | // Tag used when the Editor maintains its own separate UndoManager. |
| 142 | private static final String UNDO_OWNER_TAG = "Editor"; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 143 | |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 144 | // Ordering constants used to place the Action Mode or context menu items in their menu. |
| 145 | private static final int MENU_ITEM_ORDER_UNDO = 1; |
| 146 | private static final int MENU_ITEM_ORDER_REDO = 2; |
| 147 | private static final int MENU_ITEM_ORDER_CUT = 3; |
| 148 | private static final int MENU_ITEM_ORDER_COPY = 4; |
| 149 | private static final int MENU_ITEM_ORDER_PASTE = 5; |
| 150 | private static final int MENU_ITEM_ORDER_PASTE_AS_PLAIN_TEXT = 6; |
| 151 | private static final int MENU_ITEM_ORDER_SHARE = 7; |
| 152 | private static final int MENU_ITEM_ORDER_SELECT_ALL = 8; |
| 153 | private static final int MENU_ITEM_ORDER_REPLACE = 9; |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 154 | private static final int MENU_ITEM_ORDER_PROCESS_TEXT_INTENT_ACTIONS_START = 10; |
| 155 | |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 156 | // Each Editor manages its own undo stack. |
| 157 | private final UndoManager mUndoManager = new UndoManager(); |
| 158 | private UndoOwner mUndoOwner = mUndoManager.getOwner(UNDO_OWNER_TAG, this); |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 159 | final UndoInputFilter mUndoInputFilter = new UndoInputFilter(this); |
James Cook | f1dad1e | 2015-02-27 11:00:01 -0800 | [diff] [blame] | 160 | boolean mAllowUndo = true; |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 161 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 162 | // Cursor Controllers. |
| 163 | InsertionPointCursorController mInsertionPointCursorController; |
| 164 | SelectionModifierCursorController mSelectionModifierCursorController; |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 165 | // Action mode used when text is selected or when actions on an insertion cursor are triggered. |
| 166 | ActionMode mTextActionMode; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 167 | boolean mInsertionControllerEnabled; |
| 168 | boolean mSelectionControllerEnabled; |
| 169 | |
| 170 | // Used to highlight a word when it is corrected by the IME |
| 171 | CorrectionHighlighter mCorrectionHighlighter; |
| 172 | |
| 173 | InputContentType mInputContentType; |
| 174 | InputMethodState mInputMethodState; |
| 175 | |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 176 | private static class TextRenderNode { |
| 177 | RenderNode renderNode; |
John Reck | 7558aa7 | 2014-03-05 14:59:59 -0800 | [diff] [blame] | 178 | boolean isDirty; |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 179 | public TextRenderNode(String name) { |
John Reck | 7558aa7 | 2014-03-05 14:59:59 -0800 | [diff] [blame] | 180 | isDirty = true; |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 181 | renderNode = RenderNode.create(name, null); |
John Reck | 7558aa7 | 2014-03-05 14:59:59 -0800 | [diff] [blame] | 182 | } |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 183 | boolean needsRecord() { return isDirty || !renderNode.isValid(); } |
John Reck | 7558aa7 | 2014-03-05 14:59:59 -0800 | [diff] [blame] | 184 | } |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 185 | TextRenderNode[] mTextRenderNodes; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 186 | |
| 187 | boolean mFrozenWithFocus; |
| 188 | boolean mSelectionMoved; |
| 189 | boolean mTouchFocusSelected; |
| 190 | |
| 191 | KeyListener mKeyListener; |
| 192 | int mInputType = EditorInfo.TYPE_NULL; |
| 193 | |
| 194 | boolean mDiscardNextActionUp; |
| 195 | boolean mIgnoreActionUpEvent; |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 196 | private boolean mIgnoreNextMouseActionUpOrDown; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 197 | |
| 198 | long mShowCursor; |
| 199 | Blink mBlink; |
| 200 | |
| 201 | boolean mCursorVisible = true; |
| 202 | boolean mSelectAllOnFocus; |
| 203 | boolean mTextIsSelectable; |
| 204 | |
| 205 | CharSequence mError; |
| 206 | boolean mErrorWasChanged; |
| 207 | ErrorPopup mErrorPopup; |
Fabrice Di Meglio | 1957d28 | 2012-10-25 17:42:39 -0700 | [diff] [blame] | 208 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 209 | /** |
| 210 | * This flag is set if the TextView tries to display an error before it |
| 211 | * is attached to the window (so its position is still unknown). |
| 212 | * It causes the error to be shown later, when onAttachedToWindow() |
| 213 | * is called. |
| 214 | */ |
| 215 | boolean mShowErrorAfterAttach; |
| 216 | |
| 217 | boolean mInBatchEditControllers; |
Gilles Debunne | 3473b2b | 2012-04-20 16:21:10 -0700 | [diff] [blame] | 218 | boolean mShowSoftInputOnFocus = true; |
Keisuke Kuroyanagi | af4caa6 | 2016-02-29 12:53:58 -0800 | [diff] [blame] | 219 | private boolean mPreserveSelection; |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 220 | private boolean mRestartActionModeOnNextRefresh; |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 221 | boolean mTemporaryDetach; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 222 | |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 223 | boolean mIsBeingLongClicked; |
| 224 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 225 | SuggestionsPopupWindow mSuggestionsPopupWindow; |
| 226 | SuggestionRangeSpan mSuggestionRangeSpan; |
| 227 | Runnable mShowSuggestionRunnable; |
| 228 | |
| 229 | final Drawable[] mCursorDrawable = new Drawable[2]; |
| 230 | int mCursorCount; // Current number of used mCursorDrawable: 0 (resource=0), 1 or 2 (split) |
| 231 | |
| 232 | private Drawable mSelectHandleLeft; |
| 233 | private Drawable mSelectHandleRight; |
| 234 | private Drawable mSelectHandleCenter; |
| 235 | |
| 236 | // Global listener that detects changes in the global position of the TextView |
| 237 | private PositionListener mPositionListener; |
| 238 | |
| 239 | float mLastDownPositionX, mLastDownPositionY; |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 240 | private float mContextMenuAnchorX, mContextMenuAnchorY; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 241 | Callback mCustomSelectionActionModeCallback; |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 242 | Callback mCustomInsertionActionModeCallback; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 243 | |
| 244 | // Set when this TextView gained focus with some text selected. Will start selection mode. |
| 245 | boolean mCreatedWithASelection; |
| 246 | |
Keisuke Kuroyanagi | 155aecb | 2015-11-05 19:10:07 +0900 | [diff] [blame] | 247 | // Indicates the current tap state (first tap, double tap, or triple click). |
| 248 | private int mTapState = TAP_STATE_INITIAL; |
| 249 | private long mLastTouchUpTime = 0; |
| 250 | private static final int TAP_STATE_INITIAL = 0; |
| 251 | private static final int TAP_STATE_FIRST_TAP = 1; |
| 252 | private static final int TAP_STATE_DOUBLE_TAP = 2; |
| 253 | // Only for mouse input. |
| 254 | private static final int TAP_STATE_TRIPLE_CLICK = 3; |
Andrei Stingaceanu | fae270c | 2015-04-29 14:39:40 +0100 | [diff] [blame] | 255 | |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 256 | // The button state as of the last time #onTouchEvent is called. |
| 257 | private int mLastButtonState; |
| 258 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 259 | private Runnable mInsertionActionModeRunnable; |
Andrei Stingaceanu | fae270c | 2015-04-29 14:39:40 +0100 | [diff] [blame] | 260 | |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 261 | // The span controller helps monitoring the changes to which the Editor needs to react: |
| 262 | // - EasyEditSpans, for which we have some UI to display on attach and on hide |
| 263 | // - SelectionSpans, for which we need to call updateSelection if an IME is attached |
| 264 | private SpanController mSpanController; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 265 | |
| 266 | WordIterator mWordIterator; |
| 267 | SpellChecker mSpellChecker; |
| 268 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 269 | // This word iterator is set with text and used to determine word boundaries |
| 270 | // when a user is selecting text. |
| 271 | private WordIterator mWordIteratorWithText; |
| 272 | // Indicate that the text in the word iterator needs to be updated. |
| 273 | private boolean mUpdateWordIteratorText; |
| 274 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 275 | private Rect mTempRect; |
| 276 | |
| 277 | private TextView mTextView; |
| 278 | |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 279 | final ProcessTextIntentActionsHandler mProcessTextIntentActionsHandler; |
| 280 | |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 281 | final CursorAnchorInfoNotifier mCursorAnchorInfoNotifier = new CursorAnchorInfoNotifier(); |
| 282 | |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 283 | private final Runnable mShowFloatingToolbar = new Runnable() { |
| 284 | @Override |
| 285 | public void run() { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 286 | if (mTextActionMode != null) { |
Abodunrinwa Toki | 9e21128 | 2015-06-05 02:46:57 +0100 | [diff] [blame] | 287 | mTextActionMode.hide(0); // hide off. |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | }; |
| 291 | |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 292 | boolean mIsInsertionActionModeStartPending = false; |
| 293 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 294 | private final SuggestionHelper mSuggestionHelper = new SuggestionHelper(); |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 295 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 296 | Editor(TextView textView) { |
| 297 | mTextView = textView; |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 298 | // Synchronize the filter list, which places the undo input filter at the end. |
| 299 | mTextView.setFilters(mTextView.getFilters()); |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 300 | mProcessTextIntentActionsHandler = new ProcessTextIntentActionsHandler(this); |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | ParcelableParcel saveInstanceState() { |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 304 | ParcelableParcel state = new ParcelableParcel(getClass().getClassLoader()); |
| 305 | Parcel parcel = state.getParcel(); |
| 306 | mUndoManager.saveInstanceState(parcel); |
| 307 | mUndoInputFilter.saveInstanceState(parcel); |
| 308 | return state; |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | void restoreInstanceState(ParcelableParcel state) { |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 312 | Parcel parcel = state.getParcel(); |
| 313 | mUndoManager.restoreInstanceState(parcel, state.getClassLoader()); |
| 314 | mUndoInputFilter.restoreInstanceState(parcel); |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 315 | // Re-associate this object as the owner of undo state. |
| 316 | mUndoOwner = mUndoManager.getOwner(UNDO_OWNER_TAG, this); |
| 317 | } |
| 318 | |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 319 | /** |
| 320 | * Forgets all undo and redo operations for this Editor. |
| 321 | */ |
| 322 | void forgetUndoRedo() { |
| 323 | UndoOwner[] owners = { mUndoOwner }; |
| 324 | mUndoManager.forgetUndos(owners, -1 /* all */); |
| 325 | mUndoManager.forgetRedos(owners, -1 /* all */); |
| 326 | } |
| 327 | |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 328 | boolean canUndo() { |
| 329 | UndoOwner[] owners = { mUndoOwner }; |
James Cook | f1dad1e | 2015-02-27 11:00:01 -0800 | [diff] [blame] | 330 | return mAllowUndo && mUndoManager.countUndos(owners) > 0; |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | boolean canRedo() { |
| 334 | UndoOwner[] owners = { mUndoOwner }; |
James Cook | f1dad1e | 2015-02-27 11:00:01 -0800 | [diff] [blame] | 335 | return mAllowUndo && mUndoManager.countRedos(owners) > 0; |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void undo() { |
James Cook | f1dad1e | 2015-02-27 11:00:01 -0800 | [diff] [blame] | 339 | if (!mAllowUndo) { |
| 340 | return; |
| 341 | } |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 342 | UndoOwner[] owners = { mUndoOwner }; |
| 343 | mUndoManager.undo(owners, 1); // Undo 1 action. |
| 344 | } |
| 345 | |
| 346 | void redo() { |
James Cook | f1dad1e | 2015-02-27 11:00:01 -0800 | [diff] [blame] | 347 | if (!mAllowUndo) { |
| 348 | return; |
| 349 | } |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 350 | UndoOwner[] owners = { mUndoOwner }; |
| 351 | mUndoManager.redo(owners, 1); // Redo 1 action. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Andrei Stingaceanu | eeb9afc | 2015-05-12 12:39:07 +0100 | [diff] [blame] | 354 | void replace() { |
Keisuke Kuroyanagi | 713be06 | 2016-02-29 16:07:54 -0800 | [diff] [blame] | 355 | if (mSuggestionsPopupWindow == null) { |
| 356 | mSuggestionsPopupWindow = new SuggestionsPopupWindow(); |
| 357 | } |
| 358 | hideCursorAndSpanControllers(); |
| 359 | mSuggestionsPopupWindow.show(); |
| 360 | |
Andrei Stingaceanu | eeb9afc | 2015-05-12 12:39:07 +0100 | [diff] [blame] | 361 | int middle = (mTextView.getSelectionStart() + mTextView.getSelectionEnd()) / 2; |
Andrei Stingaceanu | eeb9afc | 2015-05-12 12:39:07 +0100 | [diff] [blame] | 362 | Selection.setSelection((Spannable) mTextView.getText(), middle); |
Andrei Stingaceanu | eeb9afc | 2015-05-12 12:39:07 +0100 | [diff] [blame] | 363 | } |
| 364 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 365 | void onAttachedToWindow() { |
| 366 | if (mShowErrorAfterAttach) { |
| 367 | showError(); |
| 368 | mShowErrorAfterAttach = false; |
| 369 | } |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 370 | mTemporaryDetach = false; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 371 | |
| 372 | final ViewTreeObserver observer = mTextView.getViewTreeObserver(); |
| 373 | // No need to create the controller. |
| 374 | // The get method will add the listener on controller creation. |
| 375 | if (mInsertionPointCursorController != null) { |
| 376 | observer.addOnTouchModeChangeListener(mInsertionPointCursorController); |
| 377 | } |
| 378 | if (mSelectionModifierCursorController != null) { |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 379 | mSelectionModifierCursorController.resetTouchOffsets(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 380 | observer.addOnTouchModeChangeListener(mSelectionModifierCursorController); |
| 381 | } |
| 382 | updateSpellCheckSpans(0, mTextView.getText().length(), |
| 383 | true /* create the spell checker if needed */); |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 384 | |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 385 | if (mTextView.hasSelection()) { |
| 386 | refreshTextActionMode(); |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 387 | } |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 388 | |
| 389 | getPositionListener().addSubscriber(mCursorAnchorInfoNotifier, true); |
Mikael Gullstrand | 5b734f2 | 2013-07-09 14:41:28 +0200 | [diff] [blame] | 390 | resumeBlink(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void onDetachedFromWindow() { |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 394 | getPositionListener().removeSubscriber(mCursorAnchorInfoNotifier); |
| 395 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 396 | if (mError != null) { |
| 397 | hideError(); |
| 398 | } |
| 399 | |
Mikael Gullstrand | 5b734f2 | 2013-07-09 14:41:28 +0200 | [diff] [blame] | 400 | suspendBlink(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 401 | |
| 402 | if (mInsertionPointCursorController != null) { |
| 403 | mInsertionPointCursorController.onDetached(); |
| 404 | } |
| 405 | |
| 406 | if (mSelectionModifierCursorController != null) { |
| 407 | mSelectionModifierCursorController.onDetached(); |
| 408 | } |
| 409 | |
| 410 | if (mShowSuggestionRunnable != null) { |
| 411 | mTextView.removeCallbacks(mShowSuggestionRunnable); |
| 412 | } |
| 413 | |
Andrei Stingaceanu | fae270c | 2015-04-29 14:39:40 +0100 | [diff] [blame] | 414 | // Cancel the single tap delayed runnable. |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 415 | if (mInsertionActionModeRunnable != null) { |
| 416 | mTextView.removeCallbacks(mInsertionActionModeRunnable); |
Andrei Stingaceanu | fae270c | 2015-04-29 14:39:40 +0100 | [diff] [blame] | 417 | } |
| 418 | |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 419 | mTextView.removeCallbacks(mShowFloatingToolbar); |
| 420 | |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 421 | discardTextDisplayLists(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 422 | |
| 423 | if (mSpellChecker != null) { |
| 424 | mSpellChecker.closeSession(); |
| 425 | // Forces the creation of a new SpellChecker next time this window is created. |
| 426 | // Will handle the cases where the settings has been changed in the meantime. |
| 427 | mSpellChecker = null; |
| 428 | } |
| 429 | |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 430 | hideCursorAndSpanControllers(); |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 431 | stopTextActionModeWithPreservingSelection(); |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 432 | mTemporaryDetach = false; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 435 | private void discardTextDisplayLists() { |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 436 | if (mTextRenderNodes != null) { |
| 437 | for (int i = 0; i < mTextRenderNodes.length; i++) { |
| 438 | RenderNode displayList = mTextRenderNodes[i] != null |
| 439 | ? mTextRenderNodes[i].renderNode : null; |
John Reck | 7558aa7 | 2014-03-05 14:59:59 -0800 | [diff] [blame] | 440 | if (displayList != null && displayList.isValid()) { |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 441 | displayList.discardDisplayList(); |
John Reck | 7558aa7 | 2014-03-05 14:59:59 -0800 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 447 | private void showError() { |
| 448 | if (mTextView.getWindowToken() == null) { |
| 449 | mShowErrorAfterAttach = true; |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | if (mErrorPopup == null) { |
| 454 | LayoutInflater inflater = LayoutInflater.from(mTextView.getContext()); |
| 455 | final TextView err = (TextView) inflater.inflate( |
| 456 | com.android.internal.R.layout.textview_hint, null); |
| 457 | |
| 458 | final float scale = mTextView.getResources().getDisplayMetrics().density; |
| 459 | mErrorPopup = new ErrorPopup(err, (int)(200 * scale + 0.5f), (int)(50 * scale + 0.5f)); |
| 460 | mErrorPopup.setFocusable(false); |
| 461 | // The user is entering text, so the input method is needed. We |
| 462 | // don't want the popup to be displayed on top of it. |
| 463 | mErrorPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); |
| 464 | } |
| 465 | |
| 466 | TextView tv = (TextView) mErrorPopup.getContentView(); |
| 467 | chooseSize(mErrorPopup, mError, tv); |
| 468 | tv.setText(mError); |
| 469 | |
| 470 | mErrorPopup.showAsDropDown(mTextView, getErrorX(), getErrorY()); |
| 471 | mErrorPopup.fixDirection(mErrorPopup.isAboveAnchor()); |
| 472 | } |
| 473 | |
| 474 | public void setError(CharSequence error, Drawable icon) { |
| 475 | mError = TextUtils.stringOrSpannedString(error); |
| 476 | mErrorWasChanged = true; |
Romain Guy | d1cc187 | 2012-11-05 17:43:25 -0800 | [diff] [blame] | 477 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 478 | if (mError == null) { |
Fabrice Di Meglio | 5acc379 | 2012-11-12 14:08:00 -0800 | [diff] [blame] | 479 | setErrorIcon(null); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 480 | if (mErrorPopup != null) { |
| 481 | if (mErrorPopup.isShowing()) { |
| 482 | mErrorPopup.dismiss(); |
| 483 | } |
| 484 | |
| 485 | mErrorPopup = null; |
| 486 | } |
Daniel 2 Olofsson | f4ecc55 | 2013-08-13 10:30:26 +0200 | [diff] [blame] | 487 | mShowErrorAfterAttach = false; |
Fabrice Di Meglio | 5acc379 | 2012-11-12 14:08:00 -0800 | [diff] [blame] | 488 | } else { |
Romain Guy | d1cc187 | 2012-11-05 17:43:25 -0800 | [diff] [blame] | 489 | setErrorIcon(icon); |
Fabrice Di Meglio | 5acc379 | 2012-11-12 14:08:00 -0800 | [diff] [blame] | 490 | if (mTextView.isFocused()) { |
| 491 | showError(); |
| 492 | } |
Romain Guy | d1cc187 | 2012-11-05 17:43:25 -0800 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
| 496 | private void setErrorIcon(Drawable icon) { |
Fabrice Di Meglio | bb0cbae | 2012-11-13 20:51:24 -0800 | [diff] [blame] | 497 | Drawables dr = mTextView.mDrawables; |
| 498 | if (dr == null) { |
Fabrice Di Meglio | f7a5cdf | 2013-03-15 15:36:51 -0700 | [diff] [blame] | 499 | mTextView.mDrawables = dr = new Drawables(mTextView.getContext()); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 500 | } |
Fabrice Di Meglio | bb0cbae | 2012-11-13 20:51:24 -0800 | [diff] [blame] | 501 | dr.setErrorDrawable(icon, mTextView); |
| 502 | |
| 503 | mTextView.resetResolvedDrawables(); |
| 504 | mTextView.invalidate(); |
| 505 | mTextView.requestLayout(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | private void hideError() { |
| 509 | if (mErrorPopup != null) { |
| 510 | if (mErrorPopup.isShowing()) { |
| 511 | mErrorPopup.dismiss(); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | mShowErrorAfterAttach = false; |
| 516 | } |
| 517 | |
| 518 | /** |
Fabrice Di Meglio | bb0cbae | 2012-11-13 20:51:24 -0800 | [diff] [blame] | 519 | * Returns the X offset to make the pointy top of the error point |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 520 | * at the middle of the error icon. |
| 521 | */ |
| 522 | private int getErrorX() { |
| 523 | /* |
| 524 | * The "25" is the distance between the point and the right edge |
| 525 | * of the background |
| 526 | */ |
| 527 | final float scale = mTextView.getResources().getDisplayMetrics().density; |
| 528 | |
| 529 | final Drawables dr = mTextView.mDrawables; |
Fabrice Di Meglio | bb0cbae | 2012-11-13 20:51:24 -0800 | [diff] [blame] | 530 | |
| 531 | final int layoutDirection = mTextView.getLayoutDirection(); |
| 532 | int errorX; |
| 533 | int offset; |
| 534 | switch (layoutDirection) { |
| 535 | default: |
| 536 | case View.LAYOUT_DIRECTION_LTR: |
| 537 | offset = - (dr != null ? dr.mDrawableSizeRight : 0) / 2 + (int) (25 * scale + 0.5f); |
| 538 | errorX = mTextView.getWidth() - mErrorPopup.getWidth() - |
| 539 | mTextView.getPaddingRight() + offset; |
| 540 | break; |
| 541 | case View.LAYOUT_DIRECTION_RTL: |
| 542 | offset = (dr != null ? dr.mDrawableSizeLeft : 0) / 2 - (int) (25 * scale + 0.5f); |
| 543 | errorX = mTextView.getPaddingLeft() + offset; |
| 544 | break; |
| 545 | } |
| 546 | return errorX; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /** |
| 550 | * Returns the Y offset to make the pointy top of the error point |
| 551 | * at the bottom of the error icon. |
| 552 | */ |
| 553 | private int getErrorY() { |
| 554 | /* |
| 555 | * Compound, not extended, because the icon is not clipped |
| 556 | * if the text height is smaller. |
| 557 | */ |
| 558 | final int compoundPaddingTop = mTextView.getCompoundPaddingTop(); |
| 559 | int vspace = mTextView.getBottom() - mTextView.getTop() - |
| 560 | mTextView.getCompoundPaddingBottom() - compoundPaddingTop; |
| 561 | |
| 562 | final Drawables dr = mTextView.mDrawables; |
Fabrice Di Meglio | bb0cbae | 2012-11-13 20:51:24 -0800 | [diff] [blame] | 563 | |
| 564 | final int layoutDirection = mTextView.getLayoutDirection(); |
| 565 | int height; |
| 566 | switch (layoutDirection) { |
| 567 | default: |
| 568 | case View.LAYOUT_DIRECTION_LTR: |
| 569 | height = (dr != null ? dr.mDrawableHeightRight : 0); |
| 570 | break; |
| 571 | case View.LAYOUT_DIRECTION_RTL: |
| 572 | height = (dr != null ? dr.mDrawableHeightLeft : 0); |
| 573 | break; |
| 574 | } |
| 575 | |
| 576 | int icontop = compoundPaddingTop + (vspace - height) / 2; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 577 | |
| 578 | /* |
| 579 | * The "2" is the distance between the point and the top edge |
| 580 | * of the background. |
| 581 | */ |
| 582 | final float scale = mTextView.getResources().getDisplayMetrics().density; |
Fabrice Di Meglio | bb0cbae | 2012-11-13 20:51:24 -0800 | [diff] [blame] | 583 | return icontop + height - mTextView.getHeight() - (int) (2 * scale + 0.5f); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void createInputContentTypeIfNeeded() { |
| 587 | if (mInputContentType == null) { |
| 588 | mInputContentType = new InputContentType(); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | void createInputMethodStateIfNeeded() { |
| 593 | if (mInputMethodState == null) { |
| 594 | mInputMethodState = new InputMethodState(); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | boolean isCursorVisible() { |
| 599 | // The default value is true, even when there is no associated Editor |
| 600 | return mCursorVisible && mTextView.isTextEditable(); |
| 601 | } |
| 602 | |
| 603 | void prepareCursorControllers() { |
| 604 | boolean windowSupportsHandles = false; |
| 605 | |
| 606 | ViewGroup.LayoutParams params = mTextView.getRootView().getLayoutParams(); |
| 607 | if (params instanceof WindowManager.LayoutParams) { |
| 608 | WindowManager.LayoutParams windowParams = (WindowManager.LayoutParams) params; |
| 609 | windowSupportsHandles = windowParams.type < WindowManager.LayoutParams.FIRST_SUB_WINDOW |
| 610 | || windowParams.type > WindowManager.LayoutParams.LAST_SUB_WINDOW; |
| 611 | } |
| 612 | |
| 613 | boolean enabled = windowSupportsHandles && mTextView.getLayout() != null; |
| 614 | mInsertionControllerEnabled = enabled && isCursorVisible(); |
| 615 | mSelectionControllerEnabled = enabled && mTextView.textCanBeSelected(); |
| 616 | |
| 617 | if (!mInsertionControllerEnabled) { |
| 618 | hideInsertionPointCursorController(); |
| 619 | if (mInsertionPointCursorController != null) { |
| 620 | mInsertionPointCursorController.onDetached(); |
| 621 | mInsertionPointCursorController = null; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | if (!mSelectionControllerEnabled) { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 626 | stopTextActionMode(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 627 | if (mSelectionModifierCursorController != null) { |
| 628 | mSelectionModifierCursorController.onDetached(); |
| 629 | mSelectionModifierCursorController = null; |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
Seigo Nonaka | bb6a62c | 2015-03-31 21:59:30 +0900 | [diff] [blame] | 634 | void hideInsertionPointCursorController() { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 635 | if (mInsertionPointCursorController != null) { |
| 636 | mInsertionPointCursorController.hide(); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /** |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 641 | * Hides the insertion and span controllers. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 642 | */ |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 643 | void hideCursorAndSpanControllers() { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 644 | hideCursorControllers(); |
| 645 | hideSpanControllers(); |
| 646 | } |
| 647 | |
| 648 | private void hideSpanControllers() { |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 649 | if (mSpanController != null) { |
| 650 | mSpanController.hide(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
| 654 | private void hideCursorControllers() { |
Yohei Yukawa | 85d08f1 | 2015-04-29 20:12:37 -0700 | [diff] [blame] | 655 | // When mTextView is not ExtractEditText, we need to distinguish two kinds of focus-lost. |
| 656 | // One is the true focus lost where suggestions pop-up (if any) should be dismissed, and the |
| 657 | // other is an side effect of showing the suggestions pop-up itself. We use isShowingUp() |
| 658 | // to distinguish one from the other. |
Andrei Stingaceanu | b1891b3 | 2015-06-19 16:44:37 +0100 | [diff] [blame] | 659 | if (mSuggestionsPopupWindow != null && ((mTextView.isInExtractedMode()) || |
Yohei Yukawa | 85d08f1 | 2015-04-29 20:12:37 -0700 | [diff] [blame] | 660 | !mSuggestionsPopupWindow.isShowingUp())) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 661 | // Should be done before hide insertion point controller since it triggers a show of it |
| 662 | mSuggestionsPopupWindow.hide(); |
| 663 | } |
| 664 | hideInsertionPointCursorController(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Create new SpellCheckSpans on the modified region. |
| 669 | */ |
| 670 | private void updateSpellCheckSpans(int start, int end, boolean createSpellChecker) { |
Satoshi Kataoka | d7429c1 | 2013-06-05 16:30:23 +0900 | [diff] [blame] | 671 | // Remove spans whose adjacent characters are text not punctuation |
| 672 | mTextView.removeAdjacentSuggestionSpans(start); |
| 673 | mTextView.removeAdjacentSuggestionSpans(end); |
| 674 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 675 | if (mTextView.isTextEditable() && mTextView.isSuggestionsEnabled() && |
Andrei Stingaceanu | b1891b3 | 2015-06-19 16:44:37 +0100 | [diff] [blame] | 676 | !(mTextView.isInExtractedMode())) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 677 | if (mSpellChecker == null && createSpellChecker) { |
| 678 | mSpellChecker = new SpellChecker(mTextView); |
| 679 | } |
| 680 | if (mSpellChecker != null) { |
| 681 | mSpellChecker.spellCheck(start, end); |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | void onScreenStateChanged(int screenState) { |
| 687 | switch (screenState) { |
| 688 | case View.SCREEN_STATE_ON: |
| 689 | resumeBlink(); |
| 690 | break; |
| 691 | case View.SCREEN_STATE_OFF: |
| 692 | suspendBlink(); |
| 693 | break; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | private void suspendBlink() { |
| 698 | if (mBlink != null) { |
| 699 | mBlink.cancel(); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | private void resumeBlink() { |
| 704 | if (mBlink != null) { |
| 705 | mBlink.uncancel(); |
| 706 | makeBlink(); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | void adjustInputType(boolean password, boolean passwordInputType, |
| 711 | boolean webPasswordInputType, boolean numberPasswordInputType) { |
| 712 | // mInputType has been set from inputType, possibly modified by mInputMethod. |
| 713 | // Specialize mInputType to [web]password if we have a text class and the original input |
| 714 | // type was a password. |
| 715 | if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { |
| 716 | if (password || passwordInputType) { |
| 717 | mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION)) |
| 718 | | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD; |
| 719 | } |
| 720 | if (webPasswordInputType) { |
| 721 | mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION)) |
| 722 | | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD; |
| 723 | } |
| 724 | } else if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_NUMBER) { |
| 725 | if (numberPasswordInputType) { |
| 726 | mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION)) |
| 727 | | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD; |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | private void chooseSize(PopupWindow pop, CharSequence text, TextView tv) { |
| 733 | int wid = tv.getPaddingLeft() + tv.getPaddingRight(); |
| 734 | int ht = tv.getPaddingTop() + tv.getPaddingBottom(); |
| 735 | |
| 736 | int defaultWidthInPixels = mTextView.getResources().getDimensionPixelSize( |
| 737 | com.android.internal.R.dimen.textview_error_popup_default_width); |
| 738 | Layout l = new StaticLayout(text, tv.getPaint(), defaultWidthInPixels, |
| 739 | Layout.Alignment.ALIGN_NORMAL, 1, 0, true); |
| 740 | float max = 0; |
| 741 | for (int i = 0; i < l.getLineCount(); i++) { |
| 742 | max = Math.max(max, l.getLineWidth(i)); |
| 743 | } |
| 744 | |
| 745 | /* |
| 746 | * Now set the popup size to be big enough for the text plus the border capped |
| 747 | * to DEFAULT_MAX_POPUP_WIDTH |
| 748 | */ |
| 749 | pop.setWidth(wid + (int) Math.ceil(max)); |
| 750 | pop.setHeight(ht + l.getHeight()); |
| 751 | } |
| 752 | |
| 753 | void setFrame() { |
| 754 | if (mErrorPopup != null) { |
| 755 | TextView tv = (TextView) mErrorPopup.getContentView(); |
| 756 | chooseSize(mErrorPopup, mError, tv); |
| 757 | mErrorPopup.update(mTextView, getErrorX(), getErrorY(), |
| 758 | mErrorPopup.getWidth(), mErrorPopup.getHeight()); |
| 759 | } |
| 760 | } |
| 761 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 762 | private int getWordStart(int offset) { |
| 763 | // FIXME - For this and similar methods we're not doing anything to check if there's |
| 764 | // a LocaleSpan in the text, this may be something we should try handling or checking for. |
Mady Mellor | 6c7b4ad | 2015-04-15 14:23:26 -0700 | [diff] [blame] | 765 | int retOffset = getWordIteratorWithText().prevBoundary(offset); |
Mady Mellor | 58c9087 | 2015-05-12 11:09:37 -0700 | [diff] [blame] | 766 | if (getWordIteratorWithText().isOnPunctuation(retOffset)) { |
| 767 | // On punctuation boundary or within group of punctuation, find punctuation start. |
| 768 | retOffset = getWordIteratorWithText().getPunctuationBeginning(offset); |
| 769 | } else { |
| 770 | // Not on a punctuation boundary, find the word start. |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 771 | retOffset = getWordIteratorWithText().getPrevWordBeginningOnTwoWordsBoundary(offset); |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 772 | } |
Mady Mellor | 6c7b4ad | 2015-04-15 14:23:26 -0700 | [diff] [blame] | 773 | if (retOffset == BreakIterator.DONE) { |
| 774 | return offset; |
| 775 | } |
| 776 | return retOffset; |
| 777 | } |
| 778 | |
| 779 | private int getWordEnd(int offset) { |
| 780 | int retOffset = getWordIteratorWithText().nextBoundary(offset); |
Mady Mellor | 58c9087 | 2015-05-12 11:09:37 -0700 | [diff] [blame] | 781 | if (getWordIteratorWithText().isAfterPunctuation(retOffset)) { |
| 782 | // On punctuation boundary or within group of punctuation, find punctuation end. |
| 783 | retOffset = getWordIteratorWithText().getPunctuationEnd(offset); |
| 784 | } else { |
| 785 | // Not on a punctuation boundary, find the word end. |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 786 | retOffset = getWordIteratorWithText().getNextWordEndOnTwoWordBoundary(offset); |
Mady Mellor | 6c7b4ad | 2015-04-15 14:23:26 -0700 | [diff] [blame] | 787 | } |
| 788 | if (retOffset == BreakIterator.DONE) { |
| 789 | return offset; |
| 790 | } |
| 791 | return retOffset; |
| 792 | } |
| 793 | |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 794 | private boolean needsToSelectAllToSelectWordOrParagraph() { |
Andrei Stingaceanu | 47f82ae | 2015-04-28 17:43:54 +0100 | [diff] [blame] | 795 | if (mTextView.hasPasswordTransformationMethod()) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 796 | // Always select all on a password field. |
| 797 | // Cut/copy menu entries are not available for passwords, but being able to select all |
| 798 | // is however useful to delete or paste to replace the entire content. |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 799 | return true; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | int inputType = mTextView.getInputType(); |
| 803 | int klass = inputType & InputType.TYPE_MASK_CLASS; |
| 804 | int variation = inputType & InputType.TYPE_MASK_VARIATION; |
| 805 | |
| 806 | // Specific text field types: select the entire text for these |
| 807 | if (klass == InputType.TYPE_CLASS_NUMBER || |
| 808 | klass == InputType.TYPE_CLASS_PHONE || |
| 809 | klass == InputType.TYPE_CLASS_DATETIME || |
| 810 | variation == InputType.TYPE_TEXT_VARIATION_URI || |
| 811 | variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS || |
| 812 | variation == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS || |
| 813 | variation == InputType.TYPE_TEXT_VARIATION_FILTER) { |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 814 | return true; |
| 815 | } |
| 816 | return false; |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * Adjusts selection to the word under last touch offset. Return true if the operation was |
| 821 | * successfully performed. |
| 822 | */ |
| 823 | private boolean selectCurrentWord() { |
| 824 | if (!mTextView.canSelectText()) { |
| 825 | return false; |
| 826 | } |
| 827 | |
| 828 | if (needsToSelectAllToSelectWordOrParagraph()) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 829 | return mTextView.selectAllText(); |
| 830 | } |
| 831 | |
| 832 | long lastTouchOffsets = getLastTouchOffsets(); |
| 833 | final int minOffset = TextUtils.unpackRangeStartFromLong(lastTouchOffsets); |
| 834 | final int maxOffset = TextUtils.unpackRangeEndFromLong(lastTouchOffsets); |
| 835 | |
| 836 | // Safety check in case standard touch event handling has been bypassed |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 837 | if (minOffset < 0 || minOffset > mTextView.getText().length()) return false; |
| 838 | if (maxOffset < 0 || maxOffset > mTextView.getText().length()) return false; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 839 | |
| 840 | int selectionStart, selectionEnd; |
| 841 | |
| 842 | // If a URLSpan (web address, email, phone...) is found at that position, select it. |
| 843 | URLSpan[] urlSpans = ((Spanned) mTextView.getText()). |
| 844 | getSpans(minOffset, maxOffset, URLSpan.class); |
| 845 | if (urlSpans.length >= 1) { |
| 846 | URLSpan urlSpan = urlSpans[0]; |
| 847 | selectionStart = ((Spanned) mTextView.getText()).getSpanStart(urlSpan); |
| 848 | selectionEnd = ((Spanned) mTextView.getText()).getSpanEnd(urlSpan); |
| 849 | } else { |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 850 | // FIXME - We should check if there's a LocaleSpan in the text, this may be |
| 851 | // something we should try handling or checking for. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 852 | final WordIterator wordIterator = getWordIterator(); |
| 853 | wordIterator.setCharSequence(mTextView.getText(), minOffset, maxOffset); |
| 854 | |
| 855 | selectionStart = wordIterator.getBeginning(minOffset); |
| 856 | selectionEnd = wordIterator.getEnd(maxOffset); |
| 857 | |
| 858 | if (selectionStart == BreakIterator.DONE || selectionEnd == BreakIterator.DONE || |
| 859 | selectionStart == selectionEnd) { |
| 860 | // Possible when the word iterator does not properly handle the text's language |
Keisuke Kuroyanagi | 9c68b29 | 2015-04-24 18:55:56 +0900 | [diff] [blame] | 861 | long range = getCharClusterRange(minOffset); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 862 | selectionStart = TextUtils.unpackRangeStartFromLong(range); |
| 863 | selectionEnd = TextUtils.unpackRangeEndFromLong(range); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd); |
| 868 | return selectionEnd > selectionStart; |
| 869 | } |
| 870 | |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 871 | /** |
| 872 | * Adjusts selection to the paragraph under last touch offset. Return true if the operation was |
| 873 | * successfully performed. |
| 874 | */ |
| 875 | private boolean selectCurrentParagraph() { |
| 876 | if (!mTextView.canSelectText()) { |
| 877 | return false; |
| 878 | } |
| 879 | |
| 880 | if (needsToSelectAllToSelectWordOrParagraph()) { |
| 881 | return mTextView.selectAllText(); |
| 882 | } |
| 883 | |
| 884 | long lastTouchOffsets = getLastTouchOffsets(); |
| 885 | final int minLastTouchOffset = TextUtils.unpackRangeStartFromLong(lastTouchOffsets); |
| 886 | final int maxLastTouchOffset = TextUtils.unpackRangeEndFromLong(lastTouchOffsets); |
| 887 | |
| 888 | final long paragraphsRange = getParagraphsRange(minLastTouchOffset, maxLastTouchOffset); |
| 889 | final int start = TextUtils.unpackRangeStartFromLong(paragraphsRange); |
| 890 | final int end = TextUtils.unpackRangeEndFromLong(paragraphsRange); |
| 891 | if (start < end) { |
| 892 | Selection.setSelection((Spannable) mTextView.getText(), start, end); |
| 893 | return true; |
| 894 | } |
| 895 | return false; |
| 896 | } |
| 897 | |
| 898 | /** |
| 899 | * Get the minimum range of paragraphs that contains startOffset and endOffset. |
| 900 | */ |
| 901 | private long getParagraphsRange(int startOffset, int endOffset) { |
| 902 | final Layout layout = mTextView.getLayout(); |
| 903 | if (layout == null) { |
| 904 | return TextUtils.packRangeInLong(-1, -1); |
| 905 | } |
| 906 | final CharSequence text = mTextView.getText(); |
| 907 | int minLine = layout.getLineForOffset(startOffset); |
| 908 | // Search paragraph start. |
| 909 | while (minLine > 0) { |
| 910 | final int prevLineEndOffset = layout.getLineEnd(minLine - 1); |
| 911 | if (text.charAt(prevLineEndOffset - 1) == '\n') { |
| 912 | break; |
| 913 | } |
| 914 | minLine--; |
| 915 | } |
| 916 | int maxLine = layout.getLineForOffset(endOffset); |
| 917 | // Search paragraph end. |
| 918 | while (maxLine < layout.getLineCount() - 1) { |
| 919 | final int lineEndOffset = layout.getLineEnd(maxLine); |
| 920 | if (text.charAt(lineEndOffset - 1) == '\n') { |
| 921 | break; |
| 922 | } |
| 923 | maxLine++; |
| 924 | } |
| 925 | return TextUtils.packRangeInLong(layout.getLineStart(minLine), layout.getLineEnd(maxLine)); |
| 926 | } |
| 927 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 928 | void onLocaleChanged() { |
Keisuke Kuroyanagi | e0ac5ac | 2016-03-09 15:33:30 +0900 | [diff] [blame] | 929 | // Will be re-created on demand in getWordIterator and getWordIteratorWithText with the |
| 930 | // proper new locale |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 931 | mWordIterator = null; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 932 | mWordIteratorWithText = null; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | /** |
| 936 | * @hide |
| 937 | */ |
| 938 | public WordIterator getWordIterator() { |
| 939 | if (mWordIterator == null) { |
| 940 | mWordIterator = new WordIterator(mTextView.getTextServicesLocale()); |
| 941 | } |
| 942 | return mWordIterator; |
| 943 | } |
| 944 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 945 | private WordIterator getWordIteratorWithText() { |
| 946 | if (mWordIteratorWithText == null) { |
| 947 | mWordIteratorWithText = new WordIterator(mTextView.getTextServicesLocale()); |
| 948 | mUpdateWordIteratorText = true; |
| 949 | } |
| 950 | if (mUpdateWordIteratorText) { |
| 951 | // FIXME - Shouldn't copy all of the text as only the area of the text relevant |
| 952 | // to the user's selection is needed. A possible solution would be to |
| 953 | // copy some number N of characters near the selection and then when the |
| 954 | // user approaches N then we'd do another copy of the next N characters. |
| 955 | CharSequence text = mTextView.getText(); |
| 956 | mWordIteratorWithText.setCharSequence(text, 0, text.length()); |
| 957 | mUpdateWordIteratorText = false; |
| 958 | } |
| 959 | return mWordIteratorWithText; |
| 960 | } |
| 961 | |
Keisuke Kuroyanagi | 9c68b29 | 2015-04-24 18:55:56 +0900 | [diff] [blame] | 962 | private int getNextCursorOffset(int offset, boolean findAfterGivenOffset) { |
| 963 | final Layout layout = mTextView.getLayout(); |
| 964 | if (layout == null) return offset; |
Keisuke Kuroyanagi | 5396d7e | 2016-02-19 15:28:26 -0800 | [diff] [blame] | 965 | return findAfterGivenOffset == layout.isRtlCharAt(offset) ? |
| 966 | layout.getOffsetToLeftOf(offset) : layout.getOffsetToRightOf(offset); |
Keisuke Kuroyanagi | 9c68b29 | 2015-04-24 18:55:56 +0900 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | private long getCharClusterRange(int offset) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 970 | final int textLength = mTextView.getText().length(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 971 | if (offset < textLength) { |
Keisuke Kuroyanagi | 5396d7e | 2016-02-19 15:28:26 -0800 | [diff] [blame] | 972 | final int clusterEndOffset = getNextCursorOffset(offset, true); |
| 973 | return TextUtils.packRangeInLong( |
| 974 | getNextCursorOffset(clusterEndOffset, false), clusterEndOffset); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 975 | } |
| 976 | if (offset - 1 >= 0) { |
Keisuke Kuroyanagi | 5396d7e | 2016-02-19 15:28:26 -0800 | [diff] [blame] | 977 | final int clusterStartOffset = getNextCursorOffset(offset, false); |
| 978 | return TextUtils.packRangeInLong(clusterStartOffset, |
| 979 | getNextCursorOffset(clusterStartOffset, true)); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 980 | } |
Keisuke Kuroyanagi | 9c68b29 | 2015-04-24 18:55:56 +0900 | [diff] [blame] | 981 | return TextUtils.packRangeInLong(offset, offset); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | private boolean touchPositionIsInSelection() { |
| 985 | int selectionStart = mTextView.getSelectionStart(); |
| 986 | int selectionEnd = mTextView.getSelectionEnd(); |
| 987 | |
| 988 | if (selectionStart == selectionEnd) { |
| 989 | return false; |
| 990 | } |
| 991 | |
| 992 | if (selectionStart > selectionEnd) { |
| 993 | int tmp = selectionStart; |
| 994 | selectionStart = selectionEnd; |
| 995 | selectionEnd = tmp; |
| 996 | Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd); |
| 997 | } |
| 998 | |
| 999 | SelectionModifierCursorController selectionController = getSelectionController(); |
| 1000 | int minOffset = selectionController.getMinTouchOffset(); |
| 1001 | int maxOffset = selectionController.getMaxTouchOffset(); |
| 1002 | |
| 1003 | return ((minOffset >= selectionStart) && (maxOffset < selectionEnd)); |
| 1004 | } |
| 1005 | |
| 1006 | private PositionListener getPositionListener() { |
| 1007 | if (mPositionListener == null) { |
| 1008 | mPositionListener = new PositionListener(); |
| 1009 | } |
| 1010 | return mPositionListener; |
| 1011 | } |
| 1012 | |
| 1013 | private interface TextViewPositionListener { |
| 1014 | public void updatePosition(int parentPositionX, int parentPositionY, |
| 1015 | boolean parentPositionChanged, boolean parentScrolled); |
| 1016 | } |
| 1017 | |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 1018 | private boolean isPositionVisible(final float positionX, final float positionY) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1019 | synchronized (TEMP_POSITION) { |
| 1020 | final float[] position = TEMP_POSITION; |
| 1021 | position[0] = positionX; |
| 1022 | position[1] = positionY; |
| 1023 | View view = mTextView; |
| 1024 | |
| 1025 | while (view != null) { |
| 1026 | if (view != mTextView) { |
| 1027 | // Local scroll is already taken into account in positionX/Y |
| 1028 | position[0] -= view.getScrollX(); |
| 1029 | position[1] -= view.getScrollY(); |
| 1030 | } |
| 1031 | |
| 1032 | if (position[0] < 0 || position[1] < 0 || |
| 1033 | position[0] > view.getWidth() || position[1] > view.getHeight()) { |
| 1034 | return false; |
| 1035 | } |
| 1036 | |
| 1037 | if (!view.getMatrix().isIdentity()) { |
| 1038 | view.getMatrix().mapPoints(position); |
| 1039 | } |
| 1040 | |
| 1041 | position[0] += view.getLeft(); |
| 1042 | position[1] += view.getTop(); |
| 1043 | |
| 1044 | final ViewParent parent = view.getParent(); |
| 1045 | if (parent instanceof View) { |
| 1046 | view = (View) parent; |
| 1047 | } else { |
| 1048 | // We've reached the ViewRoot, stop iterating |
| 1049 | view = null; |
| 1050 | } |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | // We've been able to walk up the view hierarchy and the position was never clipped |
| 1055 | return true; |
| 1056 | } |
| 1057 | |
| 1058 | private boolean isOffsetVisible(int offset) { |
| 1059 | Layout layout = mTextView.getLayout(); |
Victoria Lease | b9b77ae | 2013-10-13 15:12:52 -0700 | [diff] [blame] | 1060 | if (layout == null) return false; |
| 1061 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1062 | final int line = layout.getLineForOffset(offset); |
| 1063 | final int lineBottom = layout.getLineBottom(line); |
| 1064 | final int primaryHorizontal = (int) layout.getPrimaryHorizontal(offset); |
| 1065 | return isPositionVisible(primaryHorizontal + mTextView.viewportToContentHorizontalOffset(), |
| 1066 | lineBottom + mTextView.viewportToContentVerticalOffset()); |
| 1067 | } |
| 1068 | |
| 1069 | /** Returns true if the screen coordinates position (x,y) corresponds to a character displayed |
| 1070 | * in the view. Returns false when the position is in the empty space of left/right of text. |
| 1071 | */ |
| 1072 | private boolean isPositionOnText(float x, float y) { |
| 1073 | Layout layout = mTextView.getLayout(); |
| 1074 | if (layout == null) return false; |
| 1075 | |
| 1076 | final int line = mTextView.getLineAtCoordinate(y); |
| 1077 | x = mTextView.convertToLocalHorizontalCoordinate(x); |
| 1078 | |
| 1079 | if (x < layout.getLineLeft(line)) return false; |
| 1080 | if (x > layout.getLineRight(line)) return false; |
| 1081 | return true; |
| 1082 | } |
| 1083 | |
Keisuke Kuroyanagi | e876085 | 2015-12-21 18:30:19 +0900 | [diff] [blame] | 1084 | private void startDragAndDrop() { |
Keisuke Kuroyanagi | fdfc93d | 2016-03-15 14:47:08 +0900 | [diff] [blame] | 1085 | // TODO: Fix drag and drop in full screen extracted mode. |
| 1086 | if (mTextView.isInExtractedMode()) { |
| 1087 | return; |
| 1088 | } |
Keisuke Kuroyanagi | e876085 | 2015-12-21 18:30:19 +0900 | [diff] [blame] | 1089 | final int start = mTextView.getSelectionStart(); |
| 1090 | final int end = mTextView.getSelectionEnd(); |
| 1091 | CharSequence selectedText = mTextView.getTransformedText(start, end); |
| 1092 | ClipData data = ClipData.newPlainText(null, selectedText); |
| 1093 | DragLocalState localState = new DragLocalState(mTextView, start, end); |
Keisuke Kuroyanagi | 5396d7e | 2016-02-19 15:28:26 -0800 | [diff] [blame] | 1094 | mTextView.startDragAndDrop(data, getTextThumbnailBuilder(start, end), localState, |
Keisuke Kuroyanagi | e876085 | 2015-12-21 18:30:19 +0900 | [diff] [blame] | 1095 | View.DRAG_FLAG_GLOBAL); |
| 1096 | stopTextActionMode(); |
| 1097 | if (hasSelectionController()) { |
| 1098 | getSelectionController().resetTouchOffsets(); |
| 1099 | } |
| 1100 | } |
| 1101 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1102 | public boolean performLongClick(boolean handled) { |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 1103 | // Long press in empty space moves cursor and starts the insertion action mode. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1104 | if (!handled && !isPositionOnText(mLastDownPositionX, mLastDownPositionY) && |
| 1105 | mInsertionControllerEnabled) { |
| 1106 | final int offset = mTextView.getOffsetForPosition(mLastDownPositionX, |
| 1107 | mLastDownPositionY); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1108 | Selection.setSelection((Spannable) mTextView.getText(), offset); |
Clara Bayarri | 29d2b5aa | 2015-03-13 17:41:56 +0000 | [diff] [blame] | 1109 | getInsertionController().show(); |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 1110 | mIsInsertionActionModeStartPending = true; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1111 | handled = true; |
| 1112 | } |
| 1113 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1114 | if (!handled && mTextActionMode != null) { |
Andrei Stingaceanu | 2aaeefe | 2015-10-20 19:11:23 +0100 | [diff] [blame] | 1115 | if (touchPositionIsInSelection()) { |
Keisuke Kuroyanagi | e876085 | 2015-12-21 18:30:19 +0900 | [diff] [blame] | 1116 | startDragAndDrop(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1117 | } else { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1118 | stopTextActionMode(); |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 1119 | selectCurrentWordAndStartDrag(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1120 | } |
| 1121 | handled = true; |
| 1122 | } |
| 1123 | |
| 1124 | // Start a new selection |
| 1125 | if (!handled) { |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 1126 | handled = selectCurrentWordAndStartDrag(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | return handled; |
| 1130 | } |
| 1131 | |
| 1132 | private long getLastTouchOffsets() { |
| 1133 | SelectionModifierCursorController selectionController = getSelectionController(); |
| 1134 | final int minOffset = selectionController.getMinTouchOffset(); |
| 1135 | final int maxOffset = selectionController.getMaxTouchOffset(); |
| 1136 | return TextUtils.packRangeInLong(minOffset, maxOffset); |
| 1137 | } |
| 1138 | |
| 1139 | void onFocusChanged(boolean focused, int direction) { |
| 1140 | mShowCursor = SystemClock.uptimeMillis(); |
| 1141 | ensureEndedBatchEdit(); |
| 1142 | |
| 1143 | if (focused) { |
| 1144 | int selStart = mTextView.getSelectionStart(); |
| 1145 | int selEnd = mTextView.getSelectionEnd(); |
| 1146 | |
| 1147 | // SelectAllOnFocus fields are highlighted and not selected. Do not start text selection |
| 1148 | // mode for these, unless there was a specific selection already started. |
| 1149 | final boolean isFocusHighlighted = mSelectAllOnFocus && selStart == 0 && |
| 1150 | selEnd == mTextView.getText().length(); |
| 1151 | |
| 1152 | mCreatedWithASelection = mFrozenWithFocus && mTextView.hasSelection() && |
| 1153 | !isFocusHighlighted; |
| 1154 | |
| 1155 | if (!mFrozenWithFocus || (selStart < 0 || selEnd < 0)) { |
| 1156 | // If a tap was used to give focus to that view, move cursor at tap position. |
| 1157 | // Has to be done before onTakeFocus, which can be overloaded. |
| 1158 | final int lastTapPosition = getLastTapPosition(); |
| 1159 | if (lastTapPosition >= 0) { |
| 1160 | Selection.setSelection((Spannable) mTextView.getText(), lastTapPosition); |
| 1161 | } |
| 1162 | |
| 1163 | // Note this may have to be moved out of the Editor class |
| 1164 | MovementMethod mMovement = mTextView.getMovementMethod(); |
| 1165 | if (mMovement != null) { |
| 1166 | mMovement.onTakeFocus(mTextView, (Spannable) mTextView.getText(), direction); |
| 1167 | } |
| 1168 | |
| 1169 | // The DecorView does not have focus when the 'Done' ExtractEditText button is |
| 1170 | // pressed. Since it is the ViewAncestor's mView, it requests focus before |
| 1171 | // ExtractEditText clears focus, which gives focus to the ExtractEditText. |
| 1172 | // This special case ensure that we keep current selection in that case. |
| 1173 | // It would be better to know why the DecorView does not have focus at that time. |
Andrei Stingaceanu | b1891b3 | 2015-06-19 16:44:37 +0100 | [diff] [blame] | 1174 | if (((mTextView.isInExtractedMode()) || mSelectionMoved) && |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1175 | selStart >= 0 && selEnd >= 0) { |
| 1176 | /* |
| 1177 | * Someone intentionally set the selection, so let them |
| 1178 | * do whatever it is that they wanted to do instead of |
| 1179 | * the default on-focus behavior. We reset the selection |
| 1180 | * here instead of just skipping the onTakeFocus() call |
| 1181 | * because some movement methods do something other than |
| 1182 | * just setting the selection in theirs and we still |
| 1183 | * need to go through that path. |
| 1184 | */ |
| 1185 | Selection.setSelection((Spannable) mTextView.getText(), selStart, selEnd); |
| 1186 | } |
| 1187 | |
| 1188 | if (mSelectAllOnFocus) { |
| 1189 | mTextView.selectAllText(); |
| 1190 | } |
| 1191 | |
| 1192 | mTouchFocusSelected = true; |
| 1193 | } |
| 1194 | |
| 1195 | mFrozenWithFocus = false; |
| 1196 | mSelectionMoved = false; |
| 1197 | |
| 1198 | if (mError != null) { |
| 1199 | showError(); |
| 1200 | } |
| 1201 | |
| 1202 | makeBlink(); |
| 1203 | } else { |
| 1204 | if (mError != null) { |
| 1205 | hideError(); |
| 1206 | } |
| 1207 | // Don't leave us in the middle of a batch edit. |
| 1208 | mTextView.onEndBatchEdit(); |
| 1209 | |
Andrei Stingaceanu | b1891b3 | 2015-06-19 16:44:37 +0100 | [diff] [blame] | 1210 | if (mTextView.isInExtractedMode()) { |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 1211 | hideCursorAndSpanControllers(); |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1212 | stopTextActionModeWithPreservingSelection(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1213 | } else { |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 1214 | hideCursorAndSpanControllers(); |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1215 | if (mTemporaryDetach) { |
| 1216 | stopTextActionModeWithPreservingSelection(); |
| 1217 | } else { |
| 1218 | stopTextActionMode(); |
| 1219 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1220 | downgradeEasyCorrectionSpans(); |
| 1221 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1222 | // No need to create the controller |
| 1223 | if (mSelectionModifierCursorController != null) { |
| 1224 | mSelectionModifierCursorController.resetTouchOffsets(); |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | /** |
| 1230 | * Downgrades to simple suggestions all the easy correction spans that are not a spell check |
| 1231 | * span. |
| 1232 | */ |
| 1233 | private void downgradeEasyCorrectionSpans() { |
| 1234 | CharSequence text = mTextView.getText(); |
| 1235 | if (text instanceof Spannable) { |
| 1236 | Spannable spannable = (Spannable) text; |
| 1237 | SuggestionSpan[] suggestionSpans = spannable.getSpans(0, |
| 1238 | spannable.length(), SuggestionSpan.class); |
| 1239 | for (int i = 0; i < suggestionSpans.length; i++) { |
| 1240 | int flags = suggestionSpans[i].getFlags(); |
| 1241 | if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0 |
| 1242 | && (flags & SuggestionSpan.FLAG_MISSPELLED) == 0) { |
| 1243 | flags &= ~SuggestionSpan.FLAG_EASY_CORRECT; |
| 1244 | suggestionSpans[i].setFlags(flags); |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | void sendOnTextChanged(int start, int after) { |
| 1251 | updateSpellCheckSpans(start, start + after, false); |
| 1252 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 1253 | // Flip flag to indicate the word iterator needs to have the text reset. |
| 1254 | mUpdateWordIteratorText = true; |
| 1255 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1256 | // Hide the controllers as soon as text is modified (typing, procedural...) |
| 1257 | // We do not hide the span controllers, since they can be added when a new text is |
| 1258 | // inserted into the text view (voice IME). |
| 1259 | hideCursorControllers(); |
Keisuke Kuroyanagi | f4e347d | 2015-06-11 17:41:00 +0900 | [diff] [blame] | 1260 | // Reset drag accelerator. |
| 1261 | if (mSelectionModifierCursorController != null) { |
| 1262 | mSelectionModifierCursorController.resetTouchOffsets(); |
| 1263 | } |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1264 | stopTextActionMode(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | private int getLastTapPosition() { |
| 1268 | // No need to create the controller at that point, no last tap position saved |
| 1269 | if (mSelectionModifierCursorController != null) { |
| 1270 | int lastTapPosition = mSelectionModifierCursorController.getMinTouchOffset(); |
| 1271 | if (lastTapPosition >= 0) { |
| 1272 | // Safety check, should not be possible. |
| 1273 | if (lastTapPosition > mTextView.getText().length()) { |
| 1274 | lastTapPosition = mTextView.getText().length(); |
| 1275 | } |
| 1276 | return lastTapPosition; |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | return -1; |
| 1281 | } |
| 1282 | |
| 1283 | void onWindowFocusChanged(boolean hasWindowFocus) { |
| 1284 | if (hasWindowFocus) { |
| 1285 | if (mBlink != null) { |
| 1286 | mBlink.uncancel(); |
| 1287 | makeBlink(); |
| 1288 | } |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 1289 | final InputMethodManager imm = InputMethodManager.peekInstance(); |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1290 | if (mTextView.hasSelection() && !extractedTextModeWillBeStarted()) { |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 1291 | refreshTextActionMode(); |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 1292 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1293 | } else { |
| 1294 | if (mBlink != null) { |
| 1295 | mBlink.cancel(); |
| 1296 | } |
| 1297 | if (mInputContentType != null) { |
| 1298 | mInputContentType.enterDown = false; |
| 1299 | } |
| 1300 | // Order matters! Must be done before onParentLostFocus to rely on isShowingUp |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 1301 | hideCursorAndSpanControllers(); |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1302 | stopTextActionModeWithPreservingSelection(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1303 | if (mSuggestionsPopupWindow != null) { |
| 1304 | mSuggestionsPopupWindow.onParentLostFocus(); |
| 1305 | } |
| 1306 | |
Gilles Debunne | c72fba8 | 2012-06-26 14:47:07 -0700 | [diff] [blame] | 1307 | // Don't leave us in the middle of a batch edit. Same as in onFocusChanged |
| 1308 | ensureEndedBatchEdit(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1309 | } |
| 1310 | } |
| 1311 | |
Keisuke Kuroyanagi | 155aecb | 2015-11-05 19:10:07 +0900 | [diff] [blame] | 1312 | private void updateTapState(MotionEvent event) { |
| 1313 | final int action = event.getActionMasked(); |
| 1314 | if (action == MotionEvent.ACTION_DOWN) { |
| 1315 | final boolean isMouse = event.isFromSource(InputDevice.SOURCE_MOUSE); |
| 1316 | // Detect double tap and triple click. |
| 1317 | if (((mTapState == TAP_STATE_FIRST_TAP) |
| 1318 | || ((mTapState == TAP_STATE_DOUBLE_TAP) && isMouse)) |
| 1319 | && (SystemClock.uptimeMillis() - mLastTouchUpTime) <= |
| 1320 | ViewConfiguration.getDoubleTapTimeout()) { |
| 1321 | if (mTapState == TAP_STATE_FIRST_TAP) { |
| 1322 | mTapState = TAP_STATE_DOUBLE_TAP; |
| 1323 | } else { |
| 1324 | mTapState = TAP_STATE_TRIPLE_CLICK; |
| 1325 | } |
| 1326 | } else { |
| 1327 | mTapState = TAP_STATE_FIRST_TAP; |
| 1328 | } |
| 1329 | } |
| 1330 | if (action == MotionEvent.ACTION_UP) { |
| 1331 | mLastTouchUpTime = SystemClock.uptimeMillis(); |
| 1332 | } |
| 1333 | } |
| 1334 | |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 1335 | private boolean shouldFilterOutTouchEvent(MotionEvent event) { |
| 1336 | if (!event.isFromSource(InputDevice.SOURCE_MOUSE)) { |
| 1337 | return false; |
| 1338 | } |
| 1339 | final boolean primaryButtonStateChanged = |
| 1340 | ((mLastButtonState ^ event.getButtonState()) & MotionEvent.BUTTON_PRIMARY) != 0; |
| 1341 | final int action = event.getActionMasked(); |
| 1342 | if ((action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP) |
| 1343 | && !primaryButtonStateChanged) { |
| 1344 | return true; |
| 1345 | } |
| 1346 | if (action == MotionEvent.ACTION_MOVE |
| 1347 | && !event.isButtonPressed(MotionEvent.BUTTON_PRIMARY)) { |
| 1348 | return true; |
| 1349 | } |
| 1350 | return false; |
| 1351 | } |
| 1352 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1353 | void onTouchEvent(MotionEvent event) { |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 1354 | final boolean filterOutEvent = shouldFilterOutTouchEvent(event); |
| 1355 | mLastButtonState = event.getButtonState(); |
| 1356 | if (filterOutEvent) { |
| 1357 | if (event.getActionMasked() == MotionEvent.ACTION_UP) { |
| 1358 | mDiscardNextActionUp = true; |
| 1359 | } |
| 1360 | return; |
| 1361 | } |
Keisuke Kuroyanagi | 155aecb | 2015-11-05 19:10:07 +0900 | [diff] [blame] | 1362 | updateTapState(event); |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 1363 | updateFloatingToolbarVisibility(event); |
| 1364 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1365 | if (hasSelectionController()) { |
| 1366 | getSelectionController().onTouchEvent(event); |
| 1367 | } |
| 1368 | |
| 1369 | if (mShowSuggestionRunnable != null) { |
| 1370 | mTextView.removeCallbacks(mShowSuggestionRunnable); |
| 1371 | mShowSuggestionRunnable = null; |
| 1372 | } |
| 1373 | |
| 1374 | if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { |
| 1375 | mLastDownPositionX = event.getX(); |
| 1376 | mLastDownPositionY = event.getY(); |
| 1377 | |
| 1378 | // Reset this state; it will be re-set if super.onTouchEvent |
| 1379 | // causes focus to move to the view. |
| 1380 | mTouchFocusSelected = false; |
| 1381 | mIgnoreActionUpEvent = false; |
| 1382 | } |
| 1383 | } |
| 1384 | |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 1385 | private void updateFloatingToolbarVisibility(MotionEvent event) { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1386 | if (mTextActionMode != null) { |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 1387 | switch (event.getActionMasked()) { |
| 1388 | case MotionEvent.ACTION_MOVE: |
| 1389 | hideFloatingToolbar(); |
| 1390 | break; |
| 1391 | case MotionEvent.ACTION_UP: // fall through |
| 1392 | case MotionEvent.ACTION_CANCEL: |
| 1393 | showFloatingToolbar(); |
| 1394 | } |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | private void hideFloatingToolbar() { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1399 | if (mTextActionMode != null) { |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 1400 | mTextView.removeCallbacks(mShowFloatingToolbar); |
Abodunrinwa Toki | f444b5c | 2015-06-16 15:47:04 +0100 | [diff] [blame] | 1401 | mTextActionMode.hide(ActionMode.DEFAULT_HIDE_DURATION); |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | private void showFloatingToolbar() { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1406 | if (mTextActionMode != null) { |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 1407 | // Delay "show" so it doesn't interfere with click confirmations |
| 1408 | // or double-clicks that could "dismiss" the floating toolbar. |
| 1409 | int delay = ViewConfiguration.getDoubleTapTimeout(); |
| 1410 | mTextView.postDelayed(mShowFloatingToolbar, delay); |
| 1411 | } |
| 1412 | } |
| 1413 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1414 | public void beginBatchEdit() { |
| 1415 | mInBatchEditControllers = true; |
| 1416 | final InputMethodState ims = mInputMethodState; |
| 1417 | if (ims != null) { |
| 1418 | int nesting = ++ims.mBatchEditNesting; |
| 1419 | if (nesting == 1) { |
| 1420 | ims.mCursorChanged = false; |
| 1421 | ims.mChangedDelta = 0; |
| 1422 | if (ims.mContentChanged) { |
| 1423 | // We already have a pending change from somewhere else, |
| 1424 | // so turn this into a full update. |
| 1425 | ims.mChangedStart = 0; |
| 1426 | ims.mChangedEnd = mTextView.getText().length(); |
| 1427 | } else { |
| 1428 | ims.mChangedStart = EXTRACT_UNKNOWN; |
| 1429 | ims.mChangedEnd = EXTRACT_UNKNOWN; |
| 1430 | ims.mContentChanged = false; |
| 1431 | } |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 1432 | mUndoInputFilter.beginBatchEdit(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1433 | mTextView.onBeginBatchEdit(); |
| 1434 | } |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | public void endBatchEdit() { |
| 1439 | mInBatchEditControllers = false; |
| 1440 | final InputMethodState ims = mInputMethodState; |
| 1441 | if (ims != null) { |
| 1442 | int nesting = --ims.mBatchEditNesting; |
| 1443 | if (nesting == 0) { |
| 1444 | finishBatchEdit(ims); |
| 1445 | } |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | void ensureEndedBatchEdit() { |
| 1450 | final InputMethodState ims = mInputMethodState; |
| 1451 | if (ims != null && ims.mBatchEditNesting != 0) { |
| 1452 | ims.mBatchEditNesting = 0; |
| 1453 | finishBatchEdit(ims); |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | void finishBatchEdit(final InputMethodState ims) { |
| 1458 | mTextView.onEndBatchEdit(); |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 1459 | mUndoInputFilter.endBatchEdit(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1460 | |
| 1461 | if (ims.mContentChanged || ims.mSelectionModeChanged) { |
| 1462 | mTextView.updateAfterEdit(); |
| 1463 | reportExtractedText(); |
| 1464 | } else if (ims.mCursorChanged) { |
Jean Chalard | c99d33f | 2013-02-28 16:39:47 -0800 | [diff] [blame] | 1465 | // Cheesy way to get us to report the current cursor location. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1466 | mTextView.invalidateCursor(); |
| 1467 | } |
Jean Chalard | c99d33f | 2013-02-28 16:39:47 -0800 | [diff] [blame] | 1468 | // sendUpdateSelection knows to avoid sending if the selection did |
| 1469 | // not actually change. |
| 1470 | sendUpdateSelection(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | static final int EXTRACT_NOTHING = -2; |
| 1474 | static final int EXTRACT_UNKNOWN = -1; |
| 1475 | |
| 1476 | boolean extractText(ExtractedTextRequest request, ExtractedText outText) { |
| 1477 | return extractTextInternal(request, EXTRACT_UNKNOWN, EXTRACT_UNKNOWN, |
| 1478 | EXTRACT_UNKNOWN, outText); |
| 1479 | } |
| 1480 | |
Yoshiki Iguchi | ee14772 | 2015-04-14 00:12:44 +0900 | [diff] [blame] | 1481 | private boolean extractTextInternal(@Nullable ExtractedTextRequest request, |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1482 | int partialStartOffset, int partialEndOffset, int delta, |
Yoshiki Iguchi | ee14772 | 2015-04-14 00:12:44 +0900 | [diff] [blame] | 1483 | @Nullable ExtractedText outText) { |
| 1484 | if (request == null || outText == null) { |
| 1485 | return false; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1486 | } |
Yoshiki Iguchi | ee14772 | 2015-04-14 00:12:44 +0900 | [diff] [blame] | 1487 | |
| 1488 | final CharSequence content = mTextView.getText(); |
| 1489 | if (content == null) { |
| 1490 | return false; |
| 1491 | } |
| 1492 | |
| 1493 | if (partialStartOffset != EXTRACT_NOTHING) { |
| 1494 | final int N = content.length(); |
| 1495 | if (partialStartOffset < 0) { |
| 1496 | outText.partialStartOffset = outText.partialEndOffset = -1; |
| 1497 | partialStartOffset = 0; |
| 1498 | partialEndOffset = N; |
| 1499 | } else { |
| 1500 | // Now use the delta to determine the actual amount of text |
| 1501 | // we need. |
| 1502 | partialEndOffset += delta; |
| 1503 | // Adjust offsets to ensure we contain full spans. |
| 1504 | if (content instanceof Spanned) { |
| 1505 | Spanned spanned = (Spanned)content; |
| 1506 | Object[] spans = spanned.getSpans(partialStartOffset, |
| 1507 | partialEndOffset, ParcelableSpan.class); |
| 1508 | int i = spans.length; |
| 1509 | while (i > 0) { |
| 1510 | i--; |
| 1511 | int j = spanned.getSpanStart(spans[i]); |
| 1512 | if (j < partialStartOffset) partialStartOffset = j; |
| 1513 | j = spanned.getSpanEnd(spans[i]); |
| 1514 | if (j > partialEndOffset) partialEndOffset = j; |
| 1515 | } |
| 1516 | } |
| 1517 | outText.partialStartOffset = partialStartOffset; |
| 1518 | outText.partialEndOffset = partialEndOffset - delta; |
| 1519 | |
| 1520 | if (partialStartOffset > N) { |
| 1521 | partialStartOffset = N; |
| 1522 | } else if (partialStartOffset < 0) { |
| 1523 | partialStartOffset = 0; |
| 1524 | } |
| 1525 | if (partialEndOffset > N) { |
| 1526 | partialEndOffset = N; |
| 1527 | } else if (partialEndOffset < 0) { |
| 1528 | partialEndOffset = 0; |
| 1529 | } |
| 1530 | } |
| 1531 | if ((request.flags&InputConnection.GET_TEXT_WITH_STYLES) != 0) { |
| 1532 | outText.text = content.subSequence(partialStartOffset, |
| 1533 | partialEndOffset); |
| 1534 | } else { |
| 1535 | outText.text = TextUtils.substring(content, partialStartOffset, |
| 1536 | partialEndOffset); |
| 1537 | } |
| 1538 | } else { |
| 1539 | outText.partialStartOffset = 0; |
| 1540 | outText.partialEndOffset = 0; |
| 1541 | outText.text = ""; |
| 1542 | } |
| 1543 | outText.flags = 0; |
| 1544 | if (MetaKeyKeyListener.getMetaState(content, MetaKeyKeyListener.META_SELECTING) != 0) { |
| 1545 | outText.flags |= ExtractedText.FLAG_SELECTING; |
| 1546 | } |
| 1547 | if (mTextView.isSingleLine()) { |
| 1548 | outText.flags |= ExtractedText.FLAG_SINGLE_LINE; |
| 1549 | } |
| 1550 | outText.startOffset = 0; |
| 1551 | outText.selectionStart = mTextView.getSelectionStart(); |
| 1552 | outText.selectionEnd = mTextView.getSelectionEnd(); |
| 1553 | return true; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | boolean reportExtractedText() { |
| 1557 | final Editor.InputMethodState ims = mInputMethodState; |
| 1558 | if (ims != null) { |
| 1559 | final boolean contentChanged = ims.mContentChanged; |
| 1560 | if (contentChanged || ims.mSelectionModeChanged) { |
| 1561 | ims.mContentChanged = false; |
| 1562 | ims.mSelectionModeChanged = false; |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 1563 | final ExtractedTextRequest req = ims.mExtractedTextRequest; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1564 | if (req != null) { |
| 1565 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 1566 | if (imm != null) { |
| 1567 | if (TextView.DEBUG_EXTRACT) Log.v(TextView.LOG_TAG, |
| 1568 | "Retrieving extracted start=" + ims.mChangedStart + |
| 1569 | " end=" + ims.mChangedEnd + |
| 1570 | " delta=" + ims.mChangedDelta); |
| 1571 | if (ims.mChangedStart < 0 && !contentChanged) { |
| 1572 | ims.mChangedStart = EXTRACT_NOTHING; |
| 1573 | } |
| 1574 | if (extractTextInternal(req, ims.mChangedStart, ims.mChangedEnd, |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 1575 | ims.mChangedDelta, ims.mExtractedText)) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1576 | if (TextView.DEBUG_EXTRACT) Log.v(TextView.LOG_TAG, |
| 1577 | "Reporting extracted start=" + |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 1578 | ims.mExtractedText.partialStartOffset + |
| 1579 | " end=" + ims.mExtractedText.partialEndOffset + |
| 1580 | ": " + ims.mExtractedText.text); |
| 1581 | |
| 1582 | imm.updateExtractedText(mTextView, req.token, ims.mExtractedText); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1583 | ims.mChangedStart = EXTRACT_UNKNOWN; |
| 1584 | ims.mChangedEnd = EXTRACT_UNKNOWN; |
| 1585 | ims.mChangedDelta = 0; |
| 1586 | ims.mContentChanged = false; |
| 1587 | return true; |
| 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | return false; |
| 1594 | } |
| 1595 | |
Jean Chalard | df7c72f | 2013-02-28 15:28:54 -0800 | [diff] [blame] | 1596 | private void sendUpdateSelection() { |
| 1597 | if (null != mInputMethodState && mInputMethodState.mBatchEditNesting <= 0) { |
| 1598 | final InputMethodManager imm = InputMethodManager.peekInstance(); |
| 1599 | if (null != imm) { |
| 1600 | final int selectionStart = mTextView.getSelectionStart(); |
| 1601 | final int selectionEnd = mTextView.getSelectionEnd(); |
| 1602 | int candStart = -1; |
| 1603 | int candEnd = -1; |
| 1604 | if (mTextView.getText() instanceof Spannable) { |
| 1605 | final Spannable sp = (Spannable) mTextView.getText(); |
| 1606 | candStart = EditableInputConnection.getComposingSpanStart(sp); |
| 1607 | candEnd = EditableInputConnection.getComposingSpanEnd(sp); |
| 1608 | } |
Jean Chalard | c99d33f | 2013-02-28 16:39:47 -0800 | [diff] [blame] | 1609 | // InputMethodManager#updateSelection skips sending the message if |
| 1610 | // none of the parameters have changed since the last time we called it. |
Jean Chalard | df7c72f | 2013-02-28 15:28:54 -0800 | [diff] [blame] | 1611 | imm.updateSelection(mTextView, |
| 1612 | selectionStart, selectionEnd, candStart, candEnd); |
| 1613 | } |
| 1614 | } |
| 1615 | } |
| 1616 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1617 | void onDraw(Canvas canvas, Layout layout, Path highlight, Paint highlightPaint, |
| 1618 | int cursorOffsetVertical) { |
| 1619 | final int selectionStart = mTextView.getSelectionStart(); |
| 1620 | final int selectionEnd = mTextView.getSelectionEnd(); |
| 1621 | |
| 1622 | final InputMethodState ims = mInputMethodState; |
| 1623 | if (ims != null && ims.mBatchEditNesting == 0) { |
| 1624 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 1625 | if (imm != null) { |
| 1626 | if (imm.isActive(mTextView)) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1627 | if (ims.mContentChanged || ims.mSelectionModeChanged) { |
| 1628 | // We are in extract mode and the content has changed |
| 1629 | // in some way... just report complete new text to the |
| 1630 | // input method. |
Yohei Yukawa | b6bec1a | 2015-05-01 16:18:25 -0700 | [diff] [blame] | 1631 | reportExtractedText(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1632 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1633 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | if (mCorrectionHighlighter != null) { |
| 1638 | mCorrectionHighlighter.draw(canvas, cursorOffsetVertical); |
| 1639 | } |
| 1640 | |
| 1641 | if (highlight != null && selectionStart == selectionEnd && mCursorCount > 0) { |
| 1642 | drawCursor(canvas, cursorOffsetVertical); |
| 1643 | // Rely on the drawable entirely, do not draw the cursor line. |
| 1644 | // Has to be done after the IMM related code above which relies on the highlight. |
| 1645 | highlight = null; |
| 1646 | } |
| 1647 | |
| 1648 | if (mTextView.canHaveDisplayList() && canvas.isHardwareAccelerated()) { |
| 1649 | drawHardwareAccelerated(canvas, layout, highlight, highlightPaint, |
| 1650 | cursorOffsetVertical); |
| 1651 | } else { |
| 1652 | layout.draw(canvas, highlight, highlightPaint, cursorOffsetVertical); |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | private void drawHardwareAccelerated(Canvas canvas, Layout layout, Path highlight, |
| 1657 | Paint highlightPaint, int cursorOffsetVertical) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1658 | final long lineRange = layout.getLineRangeForDraw(canvas); |
| 1659 | int firstLine = TextUtils.unpackRangeStartFromLong(lineRange); |
| 1660 | int lastLine = TextUtils.unpackRangeEndFromLong(lineRange); |
| 1661 | if (lastLine < 0) return; |
| 1662 | |
| 1663 | layout.drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical, |
| 1664 | firstLine, lastLine); |
| 1665 | |
| 1666 | if (layout instanceof DynamicLayout) { |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 1667 | if (mTextRenderNodes == null) { |
| 1668 | mTextRenderNodes = ArrayUtils.emptyArray(TextRenderNode.class); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | DynamicLayout dynamicLayout = (DynamicLayout) layout; |
Gilles Debunne | 157aafc | 2012-04-19 17:21:57 -0700 | [diff] [blame] | 1672 | int[] blockEndLines = dynamicLayout.getBlockEndLines(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1673 | int[] blockIndices = dynamicLayout.getBlockIndices(); |
| 1674 | final int numberOfBlocks = dynamicLayout.getNumberOfBlocks(); |
Sangkyu Lee | 955beb2 | 2012-12-10 15:47:00 +0900 | [diff] [blame] | 1675 | final int indexFirstChangedBlock = dynamicLayout.getIndexFirstChangedBlock(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1676 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1677 | int endOfPreviousBlock = -1; |
| 1678 | int searchStartIndex = 0; |
| 1679 | for (int i = 0; i < numberOfBlocks; i++) { |
Gilles Debunne | 157aafc | 2012-04-19 17:21:57 -0700 | [diff] [blame] | 1680 | int blockEndLine = blockEndLines[i]; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1681 | int blockIndex = blockIndices[i]; |
| 1682 | |
| 1683 | final boolean blockIsInvalid = blockIndex == DynamicLayout.INVALID_BLOCK_INDEX; |
| 1684 | if (blockIsInvalid) { |
| 1685 | blockIndex = getAvailableDisplayListIndex(blockIndices, numberOfBlocks, |
| 1686 | searchStartIndex); |
Gilles Debunne | 157aafc | 2012-04-19 17:21:57 -0700 | [diff] [blame] | 1687 | // Note how dynamic layout's internal block indices get updated from Editor |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1688 | blockIndices[i] = blockIndex; |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 1689 | if (mTextRenderNodes[blockIndex] != null) { |
| 1690 | mTextRenderNodes[blockIndex].isDirty = true; |
Raph Levien | bb2397c | 2015-02-12 16:16:49 -0800 | [diff] [blame] | 1691 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1692 | searchStartIndex = blockIndex + 1; |
| 1693 | } |
| 1694 | |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 1695 | if (mTextRenderNodes[blockIndex] == null) { |
| 1696 | mTextRenderNodes[blockIndex] = |
| 1697 | new TextRenderNode("Text " + blockIndex); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1698 | } |
| 1699 | |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 1700 | final boolean blockDisplayListIsInvalid = mTextRenderNodes[blockIndex].needsRecord(); |
| 1701 | RenderNode blockDisplayList = mTextRenderNodes[blockIndex].renderNode; |
Sangkyu Lee | 955beb2 | 2012-12-10 15:47:00 +0900 | [diff] [blame] | 1702 | if (i >= indexFirstChangedBlock || blockDisplayListIsInvalid) { |
Gilles Debunne | 157aafc | 2012-04-19 17:21:57 -0700 | [diff] [blame] | 1703 | final int blockBeginLine = endOfPreviousBlock + 1; |
| 1704 | final int top = layout.getLineTop(blockBeginLine); |
| 1705 | final int bottom = layout.getLineBottom(blockEndLine); |
Gilles Debunne | fd5bc01 | 2012-04-23 16:21:35 -0700 | [diff] [blame] | 1706 | int left = 0; |
| 1707 | int right = mTextView.getWidth(); |
| 1708 | if (mTextView.getHorizontallyScrolling()) { |
| 1709 | float min = Float.MAX_VALUE; |
| 1710 | float max = Float.MIN_VALUE; |
| 1711 | for (int line = blockBeginLine; line <= blockEndLine; line++) { |
| 1712 | min = Math.min(min, layout.getLineLeft(line)); |
| 1713 | max = Math.max(max, layout.getLineRight(line)); |
| 1714 | } |
| 1715 | left = (int) min; |
| 1716 | right = (int) (max + 0.5f); |
| 1717 | } |
Gilles Debunne | 157aafc | 2012-04-19 17:21:57 -0700 | [diff] [blame] | 1718 | |
Sangkyu Lee | 955beb2 | 2012-12-10 15:47:00 +0900 | [diff] [blame] | 1719 | // Rebuild display list if it is invalid |
| 1720 | if (blockDisplayListIsInvalid) { |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 1721 | final DisplayListCanvas displayListCanvas = blockDisplayList.start( |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 1722 | right - left, bottom - top); |
Sangkyu Lee | 955beb2 | 2012-12-10 15:47:00 +0900 | [diff] [blame] | 1723 | try { |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 1724 | // drawText is always relative to TextView's origin, this translation |
| 1725 | // brings this range of text back to the top left corner of the viewport |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 1726 | displayListCanvas.translate(-left, -top); |
| 1727 | layout.drawText(displayListCanvas, blockBeginLine, blockEndLine); |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 1728 | mTextRenderNodes[blockIndex].isDirty = false; |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 1729 | // No need to untranslate, previous context is popped after |
| 1730 | // drawDisplayList |
Sangkyu Lee | 955beb2 | 2012-12-10 15:47:00 +0900 | [diff] [blame] | 1731 | } finally { |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 1732 | blockDisplayList.end(displayListCanvas); |
Sangkyu Lee | 955beb2 | 2012-12-10 15:47:00 +0900 | [diff] [blame] | 1733 | // Same as drawDisplayList below, handled by our TextView's parent |
Chet Haase | dd67159 | 2013-04-19 14:54:34 -0700 | [diff] [blame] | 1734 | blockDisplayList.setClipToBounds(false); |
Sangkyu Lee | 955beb2 | 2012-12-10 15:47:00 +0900 | [diff] [blame] | 1735 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1736 | } |
Sangkyu Lee | 955beb2 | 2012-12-10 15:47:00 +0900 | [diff] [blame] | 1737 | |
| 1738 | // Valid disply list whose index is >= indexFirstChangedBlock |
| 1739 | // only needs to update its drawing location. |
| 1740 | blockDisplayList.setLeftTopRightBottom(left, top, right, bottom); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1741 | } |
| 1742 | |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 1743 | ((DisplayListCanvas) canvas).drawRenderNode(blockDisplayList); |
Gilles Debunne | fd5bc01 | 2012-04-23 16:21:35 -0700 | [diff] [blame] | 1744 | |
Gilles Debunne | 157aafc | 2012-04-19 17:21:57 -0700 | [diff] [blame] | 1745 | endOfPreviousBlock = blockEndLine; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1746 | } |
Sangkyu Lee | 955beb2 | 2012-12-10 15:47:00 +0900 | [diff] [blame] | 1747 | |
| 1748 | dynamicLayout.setIndexFirstChangedBlock(numberOfBlocks); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1749 | } else { |
| 1750 | // Boring layout is used for empty and hint text |
| 1751 | layout.drawText(canvas, firstLine, lastLine); |
| 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | private int getAvailableDisplayListIndex(int[] blockIndices, int numberOfBlocks, |
| 1756 | int searchStartIndex) { |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 1757 | int length = mTextRenderNodes.length; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1758 | for (int i = searchStartIndex; i < length; i++) { |
| 1759 | boolean blockIndexFound = false; |
| 1760 | for (int j = 0; j < numberOfBlocks; j++) { |
| 1761 | if (blockIndices[j] == i) { |
| 1762 | blockIndexFound = true; |
| 1763 | break; |
| 1764 | } |
| 1765 | } |
| 1766 | if (blockIndexFound) continue; |
| 1767 | return i; |
| 1768 | } |
| 1769 | |
| 1770 | // No available index found, the pool has to grow |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 1771 | mTextRenderNodes = GrowingArrayUtils.append(mTextRenderNodes, length, null); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1772 | return length; |
| 1773 | } |
| 1774 | |
| 1775 | private void drawCursor(Canvas canvas, int cursorOffsetVertical) { |
| 1776 | final boolean translate = cursorOffsetVertical != 0; |
| 1777 | if (translate) canvas.translate(0, cursorOffsetVertical); |
| 1778 | for (int i = 0; i < mCursorCount; i++) { |
| 1779 | mCursorDrawable[i].draw(canvas); |
| 1780 | } |
| 1781 | if (translate) canvas.translate(0, -cursorOffsetVertical); |
| 1782 | } |
| 1783 | |
Gilles Debunne | ebc86af | 2012-04-20 15:10:47 -0700 | [diff] [blame] | 1784 | /** |
| 1785 | * Invalidates all the sub-display lists that overlap the specified character range |
| 1786 | */ |
| 1787 | void invalidateTextDisplayList(Layout layout, int start, int end) { |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 1788 | if (mTextRenderNodes != null && layout instanceof DynamicLayout) { |
Gilles Debunne | ebc86af | 2012-04-20 15:10:47 -0700 | [diff] [blame] | 1789 | final int firstLine = layout.getLineForOffset(start); |
| 1790 | final int lastLine = layout.getLineForOffset(end); |
| 1791 | |
| 1792 | DynamicLayout dynamicLayout = (DynamicLayout) layout; |
| 1793 | int[] blockEndLines = dynamicLayout.getBlockEndLines(); |
| 1794 | int[] blockIndices = dynamicLayout.getBlockIndices(); |
| 1795 | final int numberOfBlocks = dynamicLayout.getNumberOfBlocks(); |
| 1796 | |
| 1797 | int i = 0; |
| 1798 | // Skip the blocks before firstLine |
| 1799 | while (i < numberOfBlocks) { |
| 1800 | if (blockEndLines[i] >= firstLine) break; |
| 1801 | i++; |
| 1802 | } |
| 1803 | |
| 1804 | // Invalidate all subsequent blocks until lastLine is passed |
| 1805 | while (i < numberOfBlocks) { |
| 1806 | final int blockIndex = blockIndices[i]; |
| 1807 | if (blockIndex != DynamicLayout.INVALID_BLOCK_INDEX) { |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 1808 | mTextRenderNodes[blockIndex].isDirty = true; |
Gilles Debunne | ebc86af | 2012-04-20 15:10:47 -0700 | [diff] [blame] | 1809 | } |
| 1810 | if (blockEndLines[i] >= lastLine) break; |
| 1811 | i++; |
| 1812 | } |
| 1813 | } |
| 1814 | } |
| 1815 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1816 | void invalidateTextDisplayList() { |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 1817 | if (mTextRenderNodes != null) { |
| 1818 | for (int i = 0; i < mTextRenderNodes.length; i++) { |
| 1819 | if (mTextRenderNodes[i] != null) mTextRenderNodes[i].isDirty = true; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1820 | } |
| 1821 | } |
| 1822 | } |
| 1823 | |
| 1824 | void updateCursorsPositions() { |
| 1825 | if (mTextView.mCursorDrawableRes == 0) { |
| 1826 | mCursorCount = 0; |
| 1827 | return; |
| 1828 | } |
| 1829 | |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 1830 | Layout layout = mTextView.getLayout(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1831 | final int offset = mTextView.getSelectionStart(); |
| 1832 | final int line = layout.getLineForOffset(offset); |
| 1833 | final int top = layout.getLineTop(line); |
| 1834 | final int bottom = layout.getLineTop(line + 1); |
| 1835 | |
| 1836 | mCursorCount = layout.isLevelBoundary(offset) ? 2 : 1; |
| 1837 | |
| 1838 | int middle = bottom; |
| 1839 | if (mCursorCount == 2) { |
| 1840 | // Similar to what is done in {@link Layout.#getCursorPath(int, Path, CharSequence)} |
| 1841 | middle = (top + bottom) >> 1; |
| 1842 | } |
| 1843 | |
Raph Levien | afe8e9b | 2012-12-19 16:09:32 -0800 | [diff] [blame] | 1844 | boolean clamped = layout.shouldClampCursor(line); |
Mady Mellor | ff66ca5 | 2015-07-08 12:31:45 -0700 | [diff] [blame] | 1845 | updateCursorPosition(0, top, middle, layout.getPrimaryHorizontal(offset, clamped)); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1846 | |
| 1847 | if (mCursorCount == 2) { |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 1848 | updateCursorPosition(1, middle, bottom, layout.getSecondaryHorizontal(offset, clamped)); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1849 | } |
| 1850 | } |
| 1851 | |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1852 | void refreshTextActionMode() { |
| 1853 | if (extractedTextModeWillBeStarted()) { |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 1854 | mRestartActionModeOnNextRefresh = false; |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1855 | return; |
| 1856 | } |
| 1857 | final boolean hasSelection = mTextView.hasSelection(); |
| 1858 | final SelectionModifierCursorController selectionController = getSelectionController(); |
| 1859 | final InsertionPointCursorController insertionController = getInsertionController(); |
| 1860 | if ((selectionController != null && selectionController.isCursorBeingModified()) |
| 1861 | || (insertionController != null && insertionController.isCursorBeingModified())) { |
| 1862 | // ActionMode should be managed by the currently active cursor controller. |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 1863 | mRestartActionModeOnNextRefresh = false; |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1864 | return; |
| 1865 | } |
| 1866 | if (hasSelection) { |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 1867 | hideInsertionPointCursorController(); |
| 1868 | if (mTextActionMode == null) { |
| 1869 | if (mRestartActionModeOnNextRefresh || mTextView.isInExtractedMode()) { |
| 1870 | // To avoid distraction, newly start action mode only when selection action |
| 1871 | // mode is being restarted or in full screen extracted mode. |
| 1872 | startSelectionActionMode(); |
| 1873 | } |
| 1874 | } else if (selectionController == null || !selectionController.isActive()) { |
| 1875 | // Insertion action mode is active. Avoid dismissing the selection. |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1876 | stopTextActionModeWithPreservingSelection(); |
| 1877 | startSelectionActionMode(); |
| 1878 | } else { |
| 1879 | mTextActionMode.invalidateContentRect(); |
| 1880 | } |
| 1881 | } else { |
| 1882 | // Insertion action mode is started only when insertion controller is explicitly |
| 1883 | // activated. |
| 1884 | if (insertionController == null || !insertionController.isActive()) { |
| 1885 | stopTextActionMode(); |
| 1886 | } else if (mTextActionMode != null) { |
| 1887 | mTextActionMode.invalidateContentRect(); |
| 1888 | } |
| 1889 | } |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 1890 | mRestartActionModeOnNextRefresh = false; |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1891 | } |
| 1892 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1893 | /** |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 1894 | * Start an Insertion action mode. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1895 | */ |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 1896 | void startInsertionActionMode() { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1897 | if (mInsertionActionModeRunnable != null) { |
| 1898 | mTextView.removeCallbacks(mInsertionActionModeRunnable); |
| 1899 | } |
Andrei Stingaceanu | 975a8d0 | 2015-05-19 17:29:16 +0100 | [diff] [blame] | 1900 | if (extractedTextModeWillBeStarted()) { |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 1901 | return; |
Andrei Stingaceanu | 975a8d0 | 2015-05-19 17:29:16 +0100 | [diff] [blame] | 1902 | } |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1903 | stopTextActionMode(); |
Andrei Stingaceanu | 975a8d0 | 2015-05-19 17:29:16 +0100 | [diff] [blame] | 1904 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1905 | ActionMode.Callback actionModeCallback = |
| 1906 | new TextActionModeCallback(false /* hasSelection */); |
| 1907 | mTextActionMode = mTextView.startActionMode( |
Clara Bayarri | b8ed5b7 | 2015-04-09 15:26:41 +0100 | [diff] [blame] | 1908 | actionModeCallback, ActionMode.TYPE_FLOATING); |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 1909 | if (mTextActionMode != null && getInsertionController() != null) { |
| 1910 | getInsertionController().show(); |
| 1911 | } |
Clara Bayarri | 29d2b5aa | 2015-03-13 17:41:56 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | /** |
Clara Bayarri | 578286f | 2015-04-10 15:35:31 +0100 | [diff] [blame] | 1915 | * Starts a Selection Action Mode with the current selection and ensures the selection handles |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1916 | * are shown if there is a selection. This should be used when the mode is started from a |
| 1917 | * non-touch event. |
Clara Bayarri | 578286f | 2015-04-10 15:35:31 +0100 | [diff] [blame] | 1918 | * |
Clara Bayarri | 29d2b5aa | 2015-03-13 17:41:56 +0000 | [diff] [blame] | 1919 | * @return true if the selection mode was actually started. |
| 1920 | */ |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 1921 | boolean startSelectionActionMode() { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1922 | boolean selectionStarted = startSelectionActionModeInternal(); |
Clara Bayarri | 578286f | 2015-04-10 15:35:31 +0100 | [diff] [blame] | 1923 | if (selectionStarted) { |
| 1924 | getSelectionController().show(); |
| 1925 | } |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 1926 | mRestartActionModeOnNextRefresh = false; |
Clara Bayarri | 578286f | 2015-04-10 15:35:31 +0100 | [diff] [blame] | 1927 | return selectionStarted; |
| 1928 | } |
| 1929 | |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 1930 | /** |
| 1931 | * If the TextView allows text selection, selects the current word when no existing selection |
| 1932 | * was available and starts a drag. |
| 1933 | * |
| 1934 | * @return true if the drag was started. |
| 1935 | */ |
| 1936 | private boolean selectCurrentWordAndStartDrag() { |
Clara Bayarri | 7184c8a | 2015-06-05 17:34:09 +0100 | [diff] [blame] | 1937 | if (mInsertionActionModeRunnable != null) { |
| 1938 | mTextView.removeCallbacks(mInsertionActionModeRunnable); |
| 1939 | } |
Andrei Stingaceanu | 975a8d0 | 2015-05-19 17:29:16 +0100 | [diff] [blame] | 1940 | if (extractedTextModeWillBeStarted()) { |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 1941 | return false; |
| 1942 | } |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1943 | if (!checkField()) { |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 1944 | return false; |
| 1945 | } |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1946 | if (!mTextView.hasSelection() && !selectCurrentWord()) { |
| 1947 | // No selection and cannot select a word. |
| 1948 | return false; |
| 1949 | } |
| 1950 | stopTextActionModeWithPreservingSelection(); |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 1951 | getSelectionController().enterDrag( |
| 1952 | SelectionModifierCursorController.DRAG_ACCELERATOR_MODE_WORD); |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 1953 | return true; |
| 1954 | } |
Andrei Stingaceanu | 975a8d0 | 2015-05-19 17:29:16 +0100 | [diff] [blame] | 1955 | |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 1956 | /** |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1957 | * Checks whether a selection can be performed on the current TextView. |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 1958 | * |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1959 | * @return true if a selection can be performed |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 1960 | */ |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1961 | boolean checkField() { |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 1962 | if (!mTextView.canSelectText() || !mTextView.requestFocus()) { |
| 1963 | Log.w(TextView.LOG_TAG, |
| 1964 | "TextView does not support text selection. Selection cancelled."); |
Andrei Stingaceanu | 975a8d0 | 2015-05-19 17:29:16 +0100 | [diff] [blame] | 1965 | return false; |
| 1966 | } |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 1967 | return true; |
| 1968 | } |
| 1969 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1970 | private boolean startSelectionActionModeInternal() { |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1971 | if (extractedTextModeWillBeStarted()) { |
| 1972 | return false; |
| 1973 | } |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1974 | if (mTextActionMode != null) { |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 1975 | // Text action mode is already started |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1976 | mTextActionMode.invalidate(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1977 | return false; |
| 1978 | } |
| 1979 | |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1980 | if (!checkField() || !mTextView.hasSelection()) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1981 | return false; |
| 1982 | } |
| 1983 | |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1984 | ActionMode.Callback actionModeCallback = |
| 1985 | new TextActionModeCallback(true /* hasSelection */); |
| 1986 | mTextActionMode = mTextView.startActionMode(actionModeCallback, ActionMode.TYPE_FLOATING); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1987 | |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1988 | final boolean selectionStarted = mTextActionMode != null; |
Gilles Debunne | 3473b2b | 2012-04-20 16:21:10 -0700 | [diff] [blame] | 1989 | if (selectionStarted && !mTextView.isTextSelectable() && mShowSoftInputOnFocus) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1990 | // Show the IME to be able to replace text, except when selecting non editable text. |
| 1991 | final InputMethodManager imm = InputMethodManager.peekInstance(); |
| 1992 | if (imm != null) { |
| 1993 | imm.showSoftInput(mTextView, 0, null); |
| 1994 | } |
| 1995 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1996 | return selectionStarted; |
| 1997 | } |
| 1998 | |
Andrei Stingaceanu | 99d3bbd | 2015-06-30 16:57:03 +0100 | [diff] [blame] | 1999 | boolean extractedTextModeWillBeStarted() { |
Andrei Stingaceanu | b1891b3 | 2015-06-19 16:44:37 +0100 | [diff] [blame] | 2000 | if (!(mTextView.isInExtractedMode())) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2001 | final InputMethodManager imm = InputMethodManager.peekInstance(); |
| 2002 | return imm != null && imm.isFullscreenMode(); |
| 2003 | } |
| 2004 | return false; |
| 2005 | } |
| 2006 | |
| 2007 | /** |
Keisuke Kuroyanagi | 05fd8d5 | 2015-03-16 17:44:26 +0900 | [diff] [blame] | 2008 | * @return <code>true</code> if it's reasonable to offer to show suggestions depending on |
| 2009 | * the current cursor position or selection range. This method is consistent with the |
| 2010 | * method to show suggestions {@link SuggestionsPopupWindow#updateSuggestions}. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2011 | */ |
Keisuke Kuroyanagi | 05fd8d5 | 2015-03-16 17:44:26 +0900 | [diff] [blame] | 2012 | private boolean shouldOfferToShowSuggestions() { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2013 | CharSequence text = mTextView.getText(); |
| 2014 | if (!(text instanceof Spannable)) return false; |
| 2015 | |
Keisuke Kuroyanagi | 05fd8d5 | 2015-03-16 17:44:26 +0900 | [diff] [blame] | 2016 | final Spannable spannable = (Spannable) text; |
| 2017 | final int selectionStart = mTextView.getSelectionStart(); |
| 2018 | final int selectionEnd = mTextView.getSelectionEnd(); |
| 2019 | final SuggestionSpan[] suggestionSpans = spannable.getSpans(selectionStart, selectionEnd, |
| 2020 | SuggestionSpan.class); |
| 2021 | if (suggestionSpans.length == 0) { |
| 2022 | return false; |
| 2023 | } |
| 2024 | if (selectionStart == selectionEnd) { |
| 2025 | // Spans overlap the cursor. |
Keisuke Kuroyanagi | 7e4fbe0 | 2015-05-22 14:37:01 +0900 | [diff] [blame] | 2026 | for (int i = 0; i < suggestionSpans.length; i++) { |
| 2027 | if (suggestionSpans[i].getSuggestions().length > 0) { |
| 2028 | return true; |
| 2029 | } |
| 2030 | } |
| 2031 | return false; |
Keisuke Kuroyanagi | 05fd8d5 | 2015-03-16 17:44:26 +0900 | [diff] [blame] | 2032 | } |
| 2033 | int minSpanStart = mTextView.getText().length(); |
| 2034 | int maxSpanEnd = 0; |
| 2035 | int unionOfSpansCoveringSelectionStartStart = mTextView.getText().length(); |
| 2036 | int unionOfSpansCoveringSelectionStartEnd = 0; |
Keisuke Kuroyanagi | 7e4fbe0 | 2015-05-22 14:37:01 +0900 | [diff] [blame] | 2037 | boolean hasValidSuggestions = false; |
Keisuke Kuroyanagi | 05fd8d5 | 2015-03-16 17:44:26 +0900 | [diff] [blame] | 2038 | for (int i = 0; i < suggestionSpans.length; i++) { |
| 2039 | final int spanStart = spannable.getSpanStart(suggestionSpans[i]); |
| 2040 | final int spanEnd = spannable.getSpanEnd(suggestionSpans[i]); |
| 2041 | minSpanStart = Math.min(minSpanStart, spanStart); |
| 2042 | maxSpanEnd = Math.max(maxSpanEnd, spanEnd); |
| 2043 | if (selectionStart < spanStart || selectionStart > spanEnd) { |
| 2044 | // The span doesn't cover the current selection start point. |
| 2045 | continue; |
| 2046 | } |
Keisuke Kuroyanagi | 7e4fbe0 | 2015-05-22 14:37:01 +0900 | [diff] [blame] | 2047 | hasValidSuggestions = |
| 2048 | hasValidSuggestions || suggestionSpans[i].getSuggestions().length > 0; |
Keisuke Kuroyanagi | 05fd8d5 | 2015-03-16 17:44:26 +0900 | [diff] [blame] | 2049 | unionOfSpansCoveringSelectionStartStart = |
| 2050 | Math.min(unionOfSpansCoveringSelectionStartStart, spanStart); |
| 2051 | unionOfSpansCoveringSelectionStartEnd = |
| 2052 | Math.max(unionOfSpansCoveringSelectionStartEnd, spanEnd); |
| 2053 | } |
Keisuke Kuroyanagi | 7e4fbe0 | 2015-05-22 14:37:01 +0900 | [diff] [blame] | 2054 | if (!hasValidSuggestions) { |
| 2055 | return false; |
| 2056 | } |
Keisuke Kuroyanagi | 05fd8d5 | 2015-03-16 17:44:26 +0900 | [diff] [blame] | 2057 | if (unionOfSpansCoveringSelectionStartStart >= unionOfSpansCoveringSelectionStartEnd) { |
| 2058 | // No spans cover the selection start point. |
| 2059 | return false; |
| 2060 | } |
| 2061 | if (minSpanStart < unionOfSpansCoveringSelectionStartStart |
| 2062 | || maxSpanEnd > unionOfSpansCoveringSelectionStartEnd) { |
| 2063 | // There is a span that is not covered by the union. In this case, we soouldn't offer |
| 2064 | // to show suggestions as it's confusing. |
| 2065 | return false; |
| 2066 | } |
| 2067 | return true; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | /** |
| 2071 | * @return <code>true</code> if the cursor is inside an {@link SuggestionSpan} with |
| 2072 | * {@link SuggestionSpan#FLAG_EASY_CORRECT} set. |
| 2073 | */ |
| 2074 | private boolean isCursorInsideEasyCorrectionSpan() { |
| 2075 | Spannable spannable = (Spannable) mTextView.getText(); |
| 2076 | SuggestionSpan[] suggestionSpans = spannable.getSpans(mTextView.getSelectionStart(), |
| 2077 | mTextView.getSelectionEnd(), SuggestionSpan.class); |
| 2078 | for (int i = 0; i < suggestionSpans.length; i++) { |
| 2079 | if ((suggestionSpans[i].getFlags() & SuggestionSpan.FLAG_EASY_CORRECT) != 0) { |
| 2080 | return true; |
| 2081 | } |
| 2082 | } |
| 2083 | return false; |
| 2084 | } |
| 2085 | |
| 2086 | void onTouchUpEvent(MotionEvent event) { |
| 2087 | boolean selectAllGotFocus = mSelectAllOnFocus && mTextView.didTouchFocusSelect(); |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 2088 | hideCursorAndSpanControllers(); |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 2089 | stopTextActionMode(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2090 | CharSequence text = mTextView.getText(); |
| 2091 | if (!selectAllGotFocus && text.length() > 0) { |
| 2092 | // Move cursor |
| 2093 | final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY()); |
| 2094 | Selection.setSelection((Spannable) text, offset); |
| 2095 | if (mSpellChecker != null) { |
| 2096 | // When the cursor moves, the word that was typed may need spell check |
| 2097 | mSpellChecker.onSelectionChanged(); |
| 2098 | } |
Andrei Stingaceanu | 35c550c | 2015-05-07 16:49:49 +0100 | [diff] [blame] | 2099 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2100 | if (!extractedTextModeWillBeStarted()) { |
| 2101 | if (isCursorInsideEasyCorrectionSpan()) { |
Andrei Stingaceanu | 373816e | 2015-05-28 11:26:28 +0100 | [diff] [blame] | 2102 | // Cancel the single tap delayed runnable. |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 2103 | if (mInsertionActionModeRunnable != null) { |
| 2104 | mTextView.removeCallbacks(mInsertionActionModeRunnable); |
Andrei Stingaceanu | 373816e | 2015-05-28 11:26:28 +0100 | [diff] [blame] | 2105 | } |
| 2106 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2107 | mShowSuggestionRunnable = new Runnable() { |
| 2108 | public void run() { |
Keisuke Kuroyanagi | 713be06 | 2016-02-29 16:07:54 -0800 | [diff] [blame] | 2109 | replace(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2110 | } |
| 2111 | }; |
| 2112 | // removeCallbacks is performed on every touch |
| 2113 | mTextView.postDelayed(mShowSuggestionRunnable, |
| 2114 | ViewConfiguration.getDoubleTapTimeout()); |
| 2115 | } else if (hasInsertionController()) { |
| 2116 | getInsertionController().show(); |
| 2117 | } |
| 2118 | } |
| 2119 | } |
| 2120 | } |
| 2121 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 2122 | protected void stopTextActionMode() { |
| 2123 | if (mTextActionMode != null) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2124 | // This will hide the mSelectionModifierCursorController |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 2125 | mTextActionMode.finish(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2126 | } |
| 2127 | } |
| 2128 | |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2129 | private void stopTextActionModeWithPreservingSelection() { |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 2130 | if (mTextActionMode != null) { |
| 2131 | mRestartActionModeOnNextRefresh = true; |
| 2132 | } |
Keisuke Kuroyanagi | af4caa6 | 2016-02-29 12:53:58 -0800 | [diff] [blame] | 2133 | mPreserveSelection = true; |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2134 | stopTextActionMode(); |
Keisuke Kuroyanagi | af4caa6 | 2016-02-29 12:53:58 -0800 | [diff] [blame] | 2135 | mPreserveSelection = false; |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2136 | } |
| 2137 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2138 | /** |
| 2139 | * @return True if this view supports insertion handles. |
| 2140 | */ |
| 2141 | boolean hasInsertionController() { |
| 2142 | return mInsertionControllerEnabled; |
| 2143 | } |
| 2144 | |
| 2145 | /** |
| 2146 | * @return True if this view supports selection handles. |
| 2147 | */ |
| 2148 | boolean hasSelectionController() { |
| 2149 | return mSelectionControllerEnabled; |
| 2150 | } |
| 2151 | |
| 2152 | InsertionPointCursorController getInsertionController() { |
| 2153 | if (!mInsertionControllerEnabled) { |
| 2154 | return null; |
| 2155 | } |
| 2156 | |
| 2157 | if (mInsertionPointCursorController == null) { |
| 2158 | mInsertionPointCursorController = new InsertionPointCursorController(); |
| 2159 | |
| 2160 | final ViewTreeObserver observer = mTextView.getViewTreeObserver(); |
| 2161 | observer.addOnTouchModeChangeListener(mInsertionPointCursorController); |
| 2162 | } |
| 2163 | |
| 2164 | return mInsertionPointCursorController; |
| 2165 | } |
| 2166 | |
| 2167 | SelectionModifierCursorController getSelectionController() { |
| 2168 | if (!mSelectionControllerEnabled) { |
| 2169 | return null; |
| 2170 | } |
| 2171 | |
| 2172 | if (mSelectionModifierCursorController == null) { |
| 2173 | mSelectionModifierCursorController = new SelectionModifierCursorController(); |
| 2174 | |
| 2175 | final ViewTreeObserver observer = mTextView.getViewTreeObserver(); |
| 2176 | observer.addOnTouchModeChangeListener(mSelectionModifierCursorController); |
| 2177 | } |
| 2178 | |
| 2179 | return mSelectionModifierCursorController; |
| 2180 | } |
| 2181 | |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 2182 | /** |
| 2183 | * @hide |
| 2184 | */ |
| 2185 | @VisibleForTesting |
| 2186 | public Drawable[] getCursorDrawable() { |
| 2187 | return mCursorDrawable; |
| 2188 | } |
| 2189 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2190 | private void updateCursorPosition(int cursorIndex, int top, int bottom, float horizontal) { |
| 2191 | if (mCursorDrawable[cursorIndex] == null) |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 2192 | mCursorDrawable[cursorIndex] = mTextView.getContext().getDrawable( |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2193 | mTextView.mCursorDrawableRes); |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 2194 | final Drawable drawable = mCursorDrawable[cursorIndex]; |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 2195 | final int left = clampHorizontalPosition(drawable, horizontal); |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 2196 | final int width = drawable.getIntrinsicWidth(); |
| 2197 | drawable.setBounds(left, top - mTempRect.top, left + width, |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2198 | bottom + mTempRect.bottom); |
| 2199 | } |
| 2200 | |
| 2201 | /** |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 2202 | * Return clamped position for the drawable. If the drawable is within the boundaries of the |
| 2203 | * view, then it is offset with the left padding of the cursor drawable. If the drawable is at |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 2204 | * the beginning or the end of the text then its drawable edge is aligned with left or right of |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 2205 | * the view boundary. If the drawable is null, horizontal parameter is aligned to left or right |
| 2206 | * of the view. |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 2207 | * |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 2208 | * @param drawable Drawable. Can be null. |
| 2209 | * @param horizontal Horizontal position for the drawable. |
| 2210 | * @return The clamped horizontal position for the drawable. |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 2211 | */ |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 2212 | private int clampHorizontalPosition(@Nullable final Drawable drawable, float horizontal) { |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 2213 | horizontal = Math.max(0.5f, horizontal - 0.5f); |
| 2214 | if (mTempRect == null) mTempRect = new Rect(); |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 2215 | |
| 2216 | int drawableWidth = 0; |
| 2217 | if (drawable != null) { |
| 2218 | drawable.getPadding(mTempRect); |
| 2219 | drawableWidth = drawable.getIntrinsicWidth(); |
| 2220 | } else { |
| 2221 | mTempRect.setEmpty(); |
| 2222 | } |
| 2223 | |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 2224 | int scrollX = mTextView.getScrollX(); |
| 2225 | float horizontalDiff = horizontal - scrollX; |
| 2226 | int viewClippedWidth = mTextView.getWidth() - mTextView.getCompoundPaddingLeft() |
| 2227 | - mTextView.getCompoundPaddingRight(); |
| 2228 | |
| 2229 | final int left; |
| 2230 | if (horizontalDiff >= (viewClippedWidth - 1f)) { |
| 2231 | // at the rightmost position |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 2232 | left = viewClippedWidth + scrollX - (drawableWidth - mTempRect.right); |
| 2233 | } else if (Math.abs(horizontalDiff) <= 1f || |
| 2234 | (TextUtils.isEmpty(mTextView.getText()) |
| 2235 | && (TextView.VERY_WIDE - scrollX) <= (viewClippedWidth + 1f) |
| 2236 | && horizontal <= 1f)) { |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 2237 | // at the leftmost position |
| 2238 | left = scrollX - mTempRect.left; |
| 2239 | } else { |
| 2240 | left = (int) horizontal - mTempRect.left; |
| 2241 | } |
| 2242 | return left; |
| 2243 | } |
| 2244 | |
| 2245 | /** |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2246 | * Called by the framework in response to a text auto-correction (such as fixing a typo using a |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 2247 | * a dictionary) from the current input method, provided by it calling |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2248 | * {@link InputConnection#commitCorrection} InputConnection.commitCorrection()}. The default |
| 2249 | * implementation flashes the background of the corrected word to provide feedback to the user. |
| 2250 | * |
| 2251 | * @param info The auto correct info about the text that was corrected. |
| 2252 | */ |
| 2253 | public void onCommitCorrection(CorrectionInfo info) { |
| 2254 | if (mCorrectionHighlighter == null) { |
| 2255 | mCorrectionHighlighter = new CorrectionHighlighter(); |
| 2256 | } else { |
| 2257 | mCorrectionHighlighter.invalidate(false); |
| 2258 | } |
| 2259 | |
| 2260 | mCorrectionHighlighter.highlight(info); |
| 2261 | } |
| 2262 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2263 | void onScrollChanged() { |
Gilles Debunne | 157aafc | 2012-04-19 17:21:57 -0700 | [diff] [blame] | 2264 | if (mPositionListener != null) { |
| 2265 | mPositionListener.onScrollChanged(); |
| 2266 | } |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 2267 | if (mTextActionMode != null) { |
| 2268 | mTextActionMode.invalidateContentRect(); |
Abodunrinwa Toki | 56195db | 2015-04-22 06:46:54 +0100 | [diff] [blame] | 2269 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | /** |
| 2273 | * @return True when the TextView isFocused and has a valid zero-length selection (cursor). |
| 2274 | */ |
| 2275 | private boolean shouldBlink() { |
| 2276 | if (!isCursorVisible() || !mTextView.isFocused()) return false; |
| 2277 | |
| 2278 | final int start = mTextView.getSelectionStart(); |
| 2279 | if (start < 0) return false; |
| 2280 | |
| 2281 | final int end = mTextView.getSelectionEnd(); |
| 2282 | if (end < 0) return false; |
| 2283 | |
| 2284 | return start == end; |
| 2285 | } |
| 2286 | |
| 2287 | void makeBlink() { |
| 2288 | if (shouldBlink()) { |
| 2289 | mShowCursor = SystemClock.uptimeMillis(); |
| 2290 | if (mBlink == null) mBlink = new Blink(); |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 2291 | mTextView.removeCallbacks(mBlink); |
| 2292 | mTextView.postDelayed(mBlink, BLINK); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2293 | } else { |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 2294 | if (mBlink != null) mTextView.removeCallbacks(mBlink); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2295 | } |
| 2296 | } |
| 2297 | |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 2298 | private class Blink implements Runnable { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2299 | private boolean mCancelled; |
| 2300 | |
| 2301 | public void run() { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2302 | if (mCancelled) { |
| 2303 | return; |
| 2304 | } |
| 2305 | |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 2306 | mTextView.removeCallbacks(this); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2307 | |
| 2308 | if (shouldBlink()) { |
| 2309 | if (mTextView.getLayout() != null) { |
| 2310 | mTextView.invalidateCursorPath(); |
| 2311 | } |
| 2312 | |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 2313 | mTextView.postDelayed(this, BLINK); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2314 | } |
| 2315 | } |
| 2316 | |
| 2317 | void cancel() { |
| 2318 | if (!mCancelled) { |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 2319 | mTextView.removeCallbacks(this); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2320 | mCancelled = true; |
| 2321 | } |
| 2322 | } |
| 2323 | |
| 2324 | void uncancel() { |
| 2325 | mCancelled = false; |
| 2326 | } |
| 2327 | } |
| 2328 | |
Keisuke Kuroyanagi | 5396d7e | 2016-02-19 15:28:26 -0800 | [diff] [blame] | 2329 | private DragShadowBuilder getTextThumbnailBuilder(int start, int end) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2330 | TextView shadowView = (TextView) View.inflate(mTextView.getContext(), |
| 2331 | com.android.internal.R.layout.text_drag_thumbnail, null); |
| 2332 | |
| 2333 | if (shadowView == null) { |
| 2334 | throw new IllegalArgumentException("Unable to inflate text drag thumbnail"); |
| 2335 | } |
| 2336 | |
Keisuke Kuroyanagi | 5396d7e | 2016-02-19 15:28:26 -0800 | [diff] [blame] | 2337 | if (end - start > DRAG_SHADOW_MAX_TEXT_LENGTH) { |
| 2338 | final long range = getCharClusterRange(start + DRAG_SHADOW_MAX_TEXT_LENGTH); |
| 2339 | end = TextUtils.unpackRangeEndFromLong(range); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2340 | } |
Keisuke Kuroyanagi | 5396d7e | 2016-02-19 15:28:26 -0800 | [diff] [blame] | 2341 | final CharSequence text = mTextView.getTransformedText(start, end); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2342 | shadowView.setText(text); |
| 2343 | shadowView.setTextColor(mTextView.getTextColors()); |
| 2344 | |
Alan Viverette | bb98ebd | 2015-05-08 17:17:44 -0700 | [diff] [blame] | 2345 | shadowView.setTextAppearance(R.styleable.Theme_textAppearanceLarge); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2346 | shadowView.setGravity(Gravity.CENTER); |
| 2347 | |
| 2348 | shadowView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, |
| 2349 | ViewGroup.LayoutParams.WRAP_CONTENT)); |
| 2350 | |
| 2351 | final int size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); |
| 2352 | shadowView.measure(size, size); |
| 2353 | |
| 2354 | shadowView.layout(0, 0, shadowView.getMeasuredWidth(), shadowView.getMeasuredHeight()); |
| 2355 | shadowView.invalidate(); |
| 2356 | return new DragShadowBuilder(shadowView); |
| 2357 | } |
| 2358 | |
| 2359 | private static class DragLocalState { |
| 2360 | public TextView sourceTextView; |
| 2361 | public int start, end; |
| 2362 | |
| 2363 | public DragLocalState(TextView sourceTextView, int start, int end) { |
| 2364 | this.sourceTextView = sourceTextView; |
| 2365 | this.start = start; |
| 2366 | this.end = end; |
| 2367 | } |
| 2368 | } |
| 2369 | |
| 2370 | void onDrop(DragEvent event) { |
| 2371 | StringBuilder content = new StringBuilder(""); |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 2372 | |
| 2373 | final DropPermissions dropPermissions = DropPermissions.obtain(event); |
| 2374 | if (dropPermissions != null) { |
| 2375 | dropPermissions.takeTransient(); |
| 2376 | } |
| 2377 | |
| 2378 | try { |
| 2379 | ClipData clipData = event.getClipData(); |
| 2380 | final int itemCount = clipData.getItemCount(); |
| 2381 | for (int i=0; i < itemCount; i++) { |
| 2382 | Item item = clipData.getItemAt(i); |
| 2383 | content.append(item.coerceToStyledText(mTextView.getContext())); |
| 2384 | } |
| 2385 | } |
| 2386 | finally { |
| 2387 | if (dropPermissions != null) { |
| 2388 | dropPermissions.release(); |
| 2389 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY()); |
| 2393 | |
| 2394 | Object localState = event.getLocalState(); |
| 2395 | DragLocalState dragLocalState = null; |
| 2396 | if (localState instanceof DragLocalState) { |
| 2397 | dragLocalState = (DragLocalState) localState; |
| 2398 | } |
| 2399 | boolean dragDropIntoItself = dragLocalState != null && |
| 2400 | dragLocalState.sourceTextView == mTextView; |
| 2401 | |
| 2402 | if (dragDropIntoItself) { |
| 2403 | if (offset >= dragLocalState.start && offset < dragLocalState.end) { |
| 2404 | // A drop inside the original selection discards the drop. |
| 2405 | return; |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | final int originalLength = mTextView.getText().length(); |
Raph Levien | 5a689ce | 2014-09-10 11:03:18 -0700 | [diff] [blame] | 2410 | int min = offset; |
| 2411 | int max = offset; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2412 | |
| 2413 | Selection.setSelection((Spannable) mTextView.getText(), max); |
| 2414 | mTextView.replaceText_internal(min, max, content); |
| 2415 | |
| 2416 | if (dragDropIntoItself) { |
| 2417 | int dragSourceStart = dragLocalState.start; |
| 2418 | int dragSourceEnd = dragLocalState.end; |
| 2419 | if (max <= dragSourceStart) { |
| 2420 | // Inserting text before selection has shifted positions |
| 2421 | final int shift = mTextView.getText().length() - originalLength; |
| 2422 | dragSourceStart += shift; |
| 2423 | dragSourceEnd += shift; |
| 2424 | } |
| 2425 | |
Keisuke Kuroyanagi | fae4578 | 2016-02-24 18:53:00 -0800 | [diff] [blame] | 2426 | mUndoInputFilter.setForceMerge(true); |
| 2427 | try { |
| 2428 | // Delete original selection |
| 2429 | mTextView.deleteText_internal(dragSourceStart, dragSourceEnd); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2430 | |
Keisuke Kuroyanagi | fae4578 | 2016-02-24 18:53:00 -0800 | [diff] [blame] | 2431 | // Make sure we do not leave two adjacent spaces. |
| 2432 | final int prevCharIdx = Math.max(0, dragSourceStart - 1); |
| 2433 | final int nextCharIdx = Math.min(mTextView.getText().length(), dragSourceStart + 1); |
| 2434 | if (nextCharIdx > prevCharIdx + 1) { |
| 2435 | CharSequence t = mTextView.getTransformedText(prevCharIdx, nextCharIdx); |
| 2436 | if (Character.isSpaceChar(t.charAt(0)) && Character.isSpaceChar(t.charAt(1))) { |
| 2437 | mTextView.deleteText_internal(prevCharIdx, prevCharIdx + 1); |
| 2438 | } |
Victoria Lease | 9137320 | 2012-09-07 16:41:59 -0700 | [diff] [blame] | 2439 | } |
Keisuke Kuroyanagi | fae4578 | 2016-02-24 18:53:00 -0800 | [diff] [blame] | 2440 | } finally { |
| 2441 | mUndoInputFilter.setForceMerge(false); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2442 | } |
| 2443 | } |
| 2444 | } |
| 2445 | |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2446 | public void addSpanWatchers(Spannable text) { |
| 2447 | final int textLength = text.length(); |
| 2448 | |
| 2449 | if (mKeyListener != null) { |
| 2450 | text.setSpan(mKeyListener, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE); |
| 2451 | } |
| 2452 | |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 2453 | if (mSpanController == null) { |
| 2454 | mSpanController = new SpanController(); |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2455 | } |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 2456 | text.setSpan(mSpanController, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE); |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2457 | } |
| 2458 | |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 2459 | void setContextMenuAnchor(float x, float y) { |
| 2460 | mContextMenuAnchorX = x; |
| 2461 | mContextMenuAnchorY = y; |
| 2462 | } |
| 2463 | |
| 2464 | void onCreateContextMenu(ContextMenu menu) { |
| 2465 | if (mIsBeingLongClicked || Float.isNaN(mContextMenuAnchorX) |
| 2466 | || Float.isNaN(mContextMenuAnchorY)) { |
| 2467 | return; |
| 2468 | } |
| 2469 | final int offset = mTextView.getOffsetForPosition(mContextMenuAnchorX, mContextMenuAnchorY); |
| 2470 | if (offset == -1) { |
| 2471 | return; |
| 2472 | } |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2473 | stopTextActionModeWithPreservingSelection(); |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 2474 | final boolean isOnSelection = mTextView.hasSelection() |
| 2475 | && offset >= mTextView.getSelectionStart() && offset <= mTextView.getSelectionEnd(); |
| 2476 | if (!isOnSelection) { |
| 2477 | // Right clicked position is not on the selection. Remove the selection and move the |
| 2478 | // cursor to the right clicked position. |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 2479 | Selection.setSelection((Spannable) mTextView.getText(), offset); |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2480 | stopTextActionMode(); |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 2481 | } |
| 2482 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 2483 | if (shouldOfferToShowSuggestions()) { |
Keisuke Kuroyanagi | 182f5fe | 2016-03-11 16:31:29 +0900 | [diff] [blame] | 2484 | final SuggestionInfo[] suggestionInfoArray = |
| 2485 | new SuggestionInfo[SuggestionSpan.SUGGESTIONS_MAX_SIZE]; |
| 2486 | for (int i = 0; i < suggestionInfoArray.length; i++) { |
| 2487 | suggestionInfoArray[i] = new SuggestionInfo(); |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 2488 | } |
| 2489 | final SubMenu subMenu = menu.addSubMenu(Menu.NONE, Menu.NONE, MENU_ITEM_ORDER_REPLACE, |
| 2490 | com.android.internal.R.string.replace); |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 2491 | final int numItems = mSuggestionHelper.getSuggestionInfo(suggestionInfoArray, null); |
Keisuke Kuroyanagi | 182f5fe | 2016-03-11 16:31:29 +0900 | [diff] [blame] | 2492 | for (int i = 0; i < numItems; i++) { |
| 2493 | final SuggestionInfo info = suggestionInfoArray[i]; |
| 2494 | subMenu.add(Menu.NONE, Menu.NONE, i, info.mText) |
| 2495 | .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 2496 | @Override |
| 2497 | public boolean onMenuItemClick(MenuItem item) { |
| 2498 | replaceWithSuggestion(info); |
| 2499 | return true; |
| 2500 | } |
| 2501 | }); |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 2502 | } |
| 2503 | } |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 2504 | |
| 2505 | menu.add(Menu.NONE, TextView.ID_UNDO, MENU_ITEM_ORDER_UNDO, |
| 2506 | com.android.internal.R.string.undo) |
| 2507 | .setAlphabeticShortcut('z') |
| 2508 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener) |
| 2509 | .setEnabled(mTextView.canUndo()); |
| 2510 | menu.add(Menu.NONE, TextView.ID_REDO, MENU_ITEM_ORDER_REDO, |
| 2511 | com.android.internal.R.string.redo) |
| 2512 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener) |
| 2513 | .setEnabled(mTextView.canRedo()); |
| 2514 | |
| 2515 | menu.add(Menu.NONE, TextView.ID_CUT, MENU_ITEM_ORDER_CUT, |
| 2516 | com.android.internal.R.string.cut) |
| 2517 | .setAlphabeticShortcut('x') |
| 2518 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener) |
| 2519 | .setEnabled(mTextView.canCut()); |
| 2520 | menu.add(Menu.NONE, TextView.ID_COPY, MENU_ITEM_ORDER_COPY, |
| 2521 | com.android.internal.R.string.copy) |
| 2522 | .setAlphabeticShortcut('c') |
| 2523 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener) |
| 2524 | .setEnabled(mTextView.canCopy()); |
| 2525 | menu.add(Menu.NONE, TextView.ID_PASTE, MENU_ITEM_ORDER_PASTE, |
| 2526 | com.android.internal.R.string.paste) |
| 2527 | .setAlphabeticShortcut('v') |
| 2528 | .setEnabled(mTextView.canPaste()) |
| 2529 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener); |
| 2530 | menu.add(Menu.NONE, TextView.ID_PASTE, MENU_ITEM_ORDER_PASTE_AS_PLAIN_TEXT, |
| 2531 | com.android.internal.R.string.paste_as_plain_text) |
| 2532 | .setEnabled(mTextView.canPaste()) |
| 2533 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener); |
| 2534 | menu.add(Menu.NONE, TextView.ID_SHARE, MENU_ITEM_ORDER_SHARE, |
| 2535 | com.android.internal.R.string.share) |
| 2536 | .setEnabled(mTextView.canShare()) |
| 2537 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener); |
| 2538 | menu.add(Menu.NONE, TextView.ID_SELECT_ALL, MENU_ITEM_ORDER_SELECT_ALL, |
| 2539 | com.android.internal.R.string.selectAll) |
| 2540 | .setAlphabeticShortcut('a') |
| 2541 | .setEnabled(mTextView.canSelectAllText()) |
| 2542 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener); |
| 2543 | |
Keisuke Kuroyanagi | af4caa6 | 2016-02-29 12:53:58 -0800 | [diff] [blame] | 2544 | mPreserveSelection = true; |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 2545 | } |
| 2546 | |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 2547 | @Nullable |
| 2548 | private SuggestionSpan findEquivalentSuggestionSpan( |
| 2549 | @NonNull SuggestionSpanInfo suggestionSpanInfo) { |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 2550 | final Editable editable = (Editable) mTextView.getText(); |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 2551 | if (editable.getSpanStart(suggestionSpanInfo.mSuggestionSpan) >= 0) { |
| 2552 | // Exactly same span is found. |
| 2553 | return suggestionSpanInfo.mSuggestionSpan; |
| 2554 | } |
| 2555 | // Suggestion span couldn't be found. Try to find a suggestion span that has the same |
| 2556 | // contents. |
| 2557 | final SuggestionSpan[] suggestionSpans = editable.getSpans(suggestionSpanInfo.mSpanStart, |
| 2558 | suggestionSpanInfo.mSpanEnd, SuggestionSpan.class); |
| 2559 | for (final SuggestionSpan suggestionSpan : suggestionSpans) { |
| 2560 | final int start = editable.getSpanStart(suggestionSpan); |
| 2561 | if (start != suggestionSpanInfo.mSpanStart) { |
| 2562 | continue; |
| 2563 | } |
| 2564 | final int end = editable.getSpanEnd(suggestionSpan); |
| 2565 | if (end != suggestionSpanInfo.mSpanEnd) { |
| 2566 | continue; |
| 2567 | } |
| 2568 | if (suggestionSpan.equals(suggestionSpanInfo.mSuggestionSpan)) { |
| 2569 | return suggestionSpan; |
Keisuke Kuroyanagi | d3194503 | 2016-02-26 16:09:12 -0800 | [diff] [blame] | 2570 | } |
| 2571 | } |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 2572 | return null; |
| 2573 | } |
| 2574 | |
| 2575 | private void replaceWithSuggestion(@NonNull final SuggestionInfo suggestionInfo) { |
| 2576 | final SuggestionSpan targetSuggestionSpan = findEquivalentSuggestionSpan( |
| 2577 | suggestionInfo.mSuggestionSpanInfo); |
| 2578 | if (targetSuggestionSpan == null) { |
| 2579 | // Span has been removed |
| 2580 | return; |
| 2581 | } |
| 2582 | final Editable editable = (Editable) mTextView.getText(); |
| 2583 | final int spanStart = editable.getSpanStart(targetSuggestionSpan); |
| 2584 | final int spanEnd = editable.getSpanEnd(targetSuggestionSpan); |
Keisuke Kuroyanagi | d3194503 | 2016-02-26 16:09:12 -0800 | [diff] [blame] | 2585 | if (spanStart < 0 || spanEnd <= spanStart) { |
| 2586 | // Span has been removed |
| 2587 | return; |
| 2588 | } |
| 2589 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 2590 | final String originalText = TextUtils.substring(editable, spanStart, spanEnd); |
| 2591 | // SuggestionSpans are removed by replace: save them before |
| 2592 | SuggestionSpan[] suggestionSpans = editable.getSpans(spanStart, spanEnd, |
| 2593 | SuggestionSpan.class); |
| 2594 | final int length = suggestionSpans.length; |
| 2595 | int[] suggestionSpansStarts = new int[length]; |
| 2596 | int[] suggestionSpansEnds = new int[length]; |
| 2597 | int[] suggestionSpansFlags = new int[length]; |
| 2598 | for (int i = 0; i < length; i++) { |
| 2599 | final SuggestionSpan suggestionSpan = suggestionSpans[i]; |
| 2600 | suggestionSpansStarts[i] = editable.getSpanStart(suggestionSpan); |
| 2601 | suggestionSpansEnds[i] = editable.getSpanEnd(suggestionSpan); |
| 2602 | suggestionSpansFlags[i] = editable.getSpanFlags(suggestionSpan); |
| 2603 | |
| 2604 | // Remove potential misspelled flags |
| 2605 | int suggestionSpanFlags = suggestionSpan.getFlags(); |
| 2606 | if ((suggestionSpanFlags & SuggestionSpan.FLAG_MISSPELLED) != 0) { |
| 2607 | suggestionSpanFlags &= ~SuggestionSpan.FLAG_MISSPELLED; |
| 2608 | suggestionSpanFlags &= ~SuggestionSpan.FLAG_EASY_CORRECT; |
| 2609 | suggestionSpan.setFlags(suggestionSpanFlags); |
| 2610 | } |
| 2611 | } |
| 2612 | |
| 2613 | // Notify source IME of the suggestion pick. Do this before swapping texts. |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 2614 | targetSuggestionSpan.notifySelection( |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 2615 | mTextView.getContext(), originalText, suggestionInfo.mSuggestionIndex); |
| 2616 | |
| 2617 | // Swap text content between actual text and Suggestion span |
| 2618 | final int suggestionStart = suggestionInfo.mSuggestionStart; |
| 2619 | final int suggestionEnd = suggestionInfo.mSuggestionEnd; |
| 2620 | final String suggestion = suggestionInfo.mText.subSequence( |
| 2621 | suggestionStart, suggestionEnd).toString(); |
| 2622 | mTextView.replaceText_internal(spanStart, spanEnd, suggestion); |
| 2623 | |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 2624 | String[] suggestions = targetSuggestionSpan.getSuggestions(); |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 2625 | suggestions[suggestionInfo.mSuggestionIndex] = originalText; |
| 2626 | |
| 2627 | // Restore previous SuggestionSpans |
| 2628 | final int lengthDelta = suggestion.length() - (spanEnd - spanStart); |
| 2629 | for (int i = 0; i < length; i++) { |
| 2630 | // Only spans that include the modified region make sense after replacement |
| 2631 | // Spans partially included in the replaced region are removed, there is no |
| 2632 | // way to assign them a valid range after replacement |
| 2633 | if (suggestionSpansStarts[i] <= spanStart && suggestionSpansEnds[i] >= spanEnd) { |
| 2634 | mTextView.setSpan_internal(suggestionSpans[i], suggestionSpansStarts[i], |
| 2635 | suggestionSpansEnds[i] + lengthDelta, suggestionSpansFlags[i]); |
| 2636 | } |
| 2637 | } |
| 2638 | // Move cursor at the end of the replaced word |
| 2639 | final int newCursorPosition = spanEnd + lengthDelta; |
| 2640 | mTextView.setCursorPosition_internal(newCursorPosition, newCursorPosition); |
| 2641 | } |
| 2642 | |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 2643 | private final MenuItem.OnMenuItemClickListener mOnContextMenuItemClickListener = |
| 2644 | new MenuItem.OnMenuItemClickListener() { |
| 2645 | @Override |
| 2646 | public boolean onMenuItemClick(MenuItem item) { |
| 2647 | if (mProcessTextIntentActionsHandler.performMenuItemAction(item)) { |
| 2648 | return true; |
| 2649 | } |
| 2650 | return mTextView.onTextContextMenuItem(item.getItemId()); |
| 2651 | } |
| 2652 | }; |
| 2653 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2654 | /** |
| 2655 | * Controls the {@link EasyEditSpan} monitoring when it is added, and when the related |
| 2656 | * pop-up should be displayed. |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 2657 | * Also monitors {@link Selection} to call back to the attached input method. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2658 | */ |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 2659 | class SpanController implements SpanWatcher { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2660 | |
| 2661 | private static final int DISPLAY_TIMEOUT_MS = 3000; // 3 secs |
| 2662 | |
| 2663 | private EasyEditPopupWindow mPopupWindow; |
| 2664 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2665 | private Runnable mHidePopup; |
| 2666 | |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 2667 | // This function is pure but inner classes can't have static functions |
| 2668 | private boolean isNonIntermediateSelectionSpan(final Spannable text, |
| 2669 | final Object span) { |
| 2670 | return (Selection.SELECTION_START == span || Selection.SELECTION_END == span) |
| 2671 | && (text.getSpanFlags(span) & Spanned.SPAN_INTERMEDIATE) == 0; |
| 2672 | } |
| 2673 | |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2674 | @Override |
| 2675 | public void onSpanAdded(Spannable text, Object span, int start, int end) { |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 2676 | if (isNonIntermediateSelectionSpan(text, span)) { |
| 2677 | sendUpdateSelection(); |
| 2678 | } else if (span instanceof EasyEditSpan) { |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2679 | if (mPopupWindow == null) { |
| 2680 | mPopupWindow = new EasyEditPopupWindow(); |
| 2681 | mHidePopup = new Runnable() { |
| 2682 | @Override |
| 2683 | public void run() { |
| 2684 | hide(); |
| 2685 | } |
| 2686 | }; |
| 2687 | } |
| 2688 | |
| 2689 | // Make sure there is only at most one EasyEditSpan in the text |
| 2690 | if (mPopupWindow.mEasyEditSpan != null) { |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 2691 | mPopupWindow.mEasyEditSpan.setDeleteEnabled(false); |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2692 | } |
| 2693 | |
| 2694 | mPopupWindow.setEasyEditSpan((EasyEditSpan) span); |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 2695 | mPopupWindow.setOnDeleteListener(new EasyEditDeleteListener() { |
| 2696 | @Override |
| 2697 | public void onDeleteClick(EasyEditSpan span) { |
| 2698 | Editable editable = (Editable) mTextView.getText(); |
| 2699 | int start = editable.getSpanStart(span); |
| 2700 | int end = editable.getSpanEnd(span); |
| 2701 | if (start >= 0 && end >= 0) { |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 2702 | sendEasySpanNotification(EasyEditSpan.TEXT_DELETED, span); |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 2703 | mTextView.deleteText_internal(start, end); |
| 2704 | } |
| 2705 | editable.removeSpan(span); |
| 2706 | } |
| 2707 | }); |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2708 | |
| 2709 | if (mTextView.getWindowVisibility() != View.VISIBLE) { |
| 2710 | // The window is not visible yet, ignore the text change. |
| 2711 | return; |
| 2712 | } |
| 2713 | |
| 2714 | if (mTextView.getLayout() == null) { |
| 2715 | // The view has not been laid out yet, ignore the text change |
| 2716 | return; |
| 2717 | } |
| 2718 | |
| 2719 | if (extractedTextModeWillBeStarted()) { |
| 2720 | // The input is in extract mode. Do not handle the easy edit in |
| 2721 | // the original TextView, as the ExtractEditText will do |
| 2722 | return; |
| 2723 | } |
| 2724 | |
| 2725 | mPopupWindow.show(); |
| 2726 | mTextView.removeCallbacks(mHidePopup); |
| 2727 | mTextView.postDelayed(mHidePopup, DISPLAY_TIMEOUT_MS); |
| 2728 | } |
| 2729 | } |
| 2730 | |
| 2731 | @Override |
| 2732 | public void onSpanRemoved(Spannable text, Object span, int start, int end) { |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 2733 | if (isNonIntermediateSelectionSpan(text, span)) { |
| 2734 | sendUpdateSelection(); |
| 2735 | } else if (mPopupWindow != null && span == mPopupWindow.mEasyEditSpan) { |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2736 | hide(); |
| 2737 | } |
| 2738 | } |
| 2739 | |
| 2740 | @Override |
| 2741 | public void onSpanChanged(Spannable text, Object span, int previousStart, int previousEnd, |
| 2742 | int newStart, int newEnd) { |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 2743 | if (isNonIntermediateSelectionSpan(text, span)) { |
| 2744 | sendUpdateSelection(); |
| 2745 | } else if (mPopupWindow != null && span instanceof EasyEditSpan) { |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 2746 | EasyEditSpan easyEditSpan = (EasyEditSpan) span; |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 2747 | sendEasySpanNotification(EasyEditSpan.TEXT_MODIFIED, easyEditSpan); |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 2748 | text.removeSpan(easyEditSpan); |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2749 | } |
| 2750 | } |
| 2751 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2752 | public void hide() { |
| 2753 | if (mPopupWindow != null) { |
| 2754 | mPopupWindow.hide(); |
| 2755 | mTextView.removeCallbacks(mHidePopup); |
| 2756 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2757 | } |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 2758 | |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 2759 | private void sendEasySpanNotification(int textChangedType, EasyEditSpan span) { |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 2760 | try { |
| 2761 | PendingIntent pendingIntent = span.getPendingIntent(); |
| 2762 | if (pendingIntent != null) { |
| 2763 | Intent intent = new Intent(); |
| 2764 | intent.putExtra(EasyEditSpan.EXTRA_TEXT_CHANGED_TYPE, textChangedType); |
| 2765 | pendingIntent.send(mTextView.getContext(), 0, intent); |
| 2766 | } |
| 2767 | } catch (CanceledException e) { |
| 2768 | // This should not happen, as we should try to send the intent only once. |
| 2769 | Log.w(TAG, "PendingIntent for notification cannot be sent", e); |
| 2770 | } |
| 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | /** |
| 2775 | * Listens for the delete event triggered by {@link EasyEditPopupWindow}. |
| 2776 | */ |
| 2777 | private interface EasyEditDeleteListener { |
| 2778 | |
| 2779 | /** |
| 2780 | * Clicks the delete pop-up. |
| 2781 | */ |
| 2782 | void onDeleteClick(EasyEditSpan span); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2783 | } |
| 2784 | |
| 2785 | /** |
| 2786 | * Displays the actions associated to an {@link EasyEditSpan}. The pop-up is controlled |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 2787 | * by {@link SpanController}. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2788 | */ |
| 2789 | private class EasyEditPopupWindow extends PinnedPopupWindow |
| 2790 | implements OnClickListener { |
| 2791 | private static final int POPUP_TEXT_LAYOUT = |
| 2792 | com.android.internal.R.layout.text_edit_action_popup_text; |
| 2793 | private TextView mDeleteTextView; |
| 2794 | private EasyEditSpan mEasyEditSpan; |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 2795 | private EasyEditDeleteListener mOnDeleteListener; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2796 | |
| 2797 | @Override |
| 2798 | protected void createPopupWindow() { |
| 2799 | mPopupWindow = new PopupWindow(mTextView.getContext(), null, |
| 2800 | com.android.internal.R.attr.textSelectHandleWindowStyle); |
| 2801 | mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); |
| 2802 | mPopupWindow.setClippingEnabled(true); |
| 2803 | } |
| 2804 | |
| 2805 | @Override |
| 2806 | protected void initContentView() { |
| 2807 | LinearLayout linearLayout = new LinearLayout(mTextView.getContext()); |
| 2808 | linearLayout.setOrientation(LinearLayout.HORIZONTAL); |
| 2809 | mContentView = linearLayout; |
| 2810 | mContentView.setBackgroundResource( |
| 2811 | com.android.internal.R.drawable.text_edit_side_paste_window); |
| 2812 | |
| 2813 | LayoutInflater inflater = (LayoutInflater)mTextView.getContext(). |
| 2814 | getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 2815 | |
| 2816 | LayoutParams wrapContent = new LayoutParams( |
| 2817 | ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
| 2818 | |
| 2819 | mDeleteTextView = (TextView) inflater.inflate(POPUP_TEXT_LAYOUT, null); |
| 2820 | mDeleteTextView.setLayoutParams(wrapContent); |
| 2821 | mDeleteTextView.setText(com.android.internal.R.string.delete); |
| 2822 | mDeleteTextView.setOnClickListener(this); |
| 2823 | mContentView.addView(mDeleteTextView); |
| 2824 | } |
| 2825 | |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2826 | public void setEasyEditSpan(EasyEditSpan easyEditSpan) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2827 | mEasyEditSpan = easyEditSpan; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2828 | } |
| 2829 | |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 2830 | private void setOnDeleteListener(EasyEditDeleteListener listener) { |
| 2831 | mOnDeleteListener = listener; |
| 2832 | } |
| 2833 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2834 | @Override |
| 2835 | public void onClick(View view) { |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 2836 | if (view == mDeleteTextView |
| 2837 | && mEasyEditSpan != null && mEasyEditSpan.isDeleteEnabled() |
| 2838 | && mOnDeleteListener != null) { |
| 2839 | mOnDeleteListener.onDeleteClick(mEasyEditSpan); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2840 | } |
| 2841 | } |
| 2842 | |
| 2843 | @Override |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 2844 | public void hide() { |
| 2845 | if (mEasyEditSpan != null) { |
| 2846 | mEasyEditSpan.setDeleteEnabled(false); |
| 2847 | } |
| 2848 | mOnDeleteListener = null; |
| 2849 | super.hide(); |
| 2850 | } |
| 2851 | |
| 2852 | @Override |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2853 | protected int getTextOffset() { |
| 2854 | // Place the pop-up at the end of the span |
| 2855 | Editable editable = (Editable) mTextView.getText(); |
| 2856 | return editable.getSpanEnd(mEasyEditSpan); |
| 2857 | } |
| 2858 | |
| 2859 | @Override |
| 2860 | protected int getVerticalLocalPosition(int line) { |
| 2861 | return mTextView.getLayout().getLineBottom(line); |
| 2862 | } |
| 2863 | |
| 2864 | @Override |
| 2865 | protected int clipVertically(int positionY) { |
| 2866 | // As we display the pop-up below the span, no vertical clipping is required. |
| 2867 | return positionY; |
| 2868 | } |
| 2869 | } |
| 2870 | |
| 2871 | private class PositionListener implements ViewTreeObserver.OnPreDrawListener { |
| 2872 | // 3 handles |
| 2873 | // 3 ActionPopup [replace, suggestion, easyedit] (suggestionsPopup first hides the others) |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 2874 | // 1 CursorAnchorInfoNotifier |
| 2875 | private final int MAXIMUM_NUMBER_OF_LISTENERS = 7; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2876 | private TextViewPositionListener[] mPositionListeners = |
| 2877 | new TextViewPositionListener[MAXIMUM_NUMBER_OF_LISTENERS]; |
| 2878 | private boolean mCanMove[] = new boolean[MAXIMUM_NUMBER_OF_LISTENERS]; |
| 2879 | private boolean mPositionHasChanged = true; |
| 2880 | // Absolute position of the TextView with respect to its parent window |
| 2881 | private int mPositionX, mPositionY; |
| 2882 | private int mNumberOfListeners; |
| 2883 | private boolean mScrollHasChanged; |
| 2884 | final int[] mTempCoords = new int[2]; |
| 2885 | |
| 2886 | public void addSubscriber(TextViewPositionListener positionListener, boolean canMove) { |
| 2887 | if (mNumberOfListeners == 0) { |
| 2888 | updatePosition(); |
| 2889 | ViewTreeObserver vto = mTextView.getViewTreeObserver(); |
| 2890 | vto.addOnPreDrawListener(this); |
| 2891 | } |
| 2892 | |
| 2893 | int emptySlotIndex = -1; |
| 2894 | for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { |
| 2895 | TextViewPositionListener listener = mPositionListeners[i]; |
| 2896 | if (listener == positionListener) { |
| 2897 | return; |
| 2898 | } else if (emptySlotIndex < 0 && listener == null) { |
| 2899 | emptySlotIndex = i; |
| 2900 | } |
| 2901 | } |
| 2902 | |
| 2903 | mPositionListeners[emptySlotIndex] = positionListener; |
| 2904 | mCanMove[emptySlotIndex] = canMove; |
| 2905 | mNumberOfListeners++; |
| 2906 | } |
| 2907 | |
| 2908 | public void removeSubscriber(TextViewPositionListener positionListener) { |
| 2909 | for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { |
| 2910 | if (mPositionListeners[i] == positionListener) { |
| 2911 | mPositionListeners[i] = null; |
| 2912 | mNumberOfListeners--; |
| 2913 | break; |
| 2914 | } |
| 2915 | } |
| 2916 | |
| 2917 | if (mNumberOfListeners == 0) { |
| 2918 | ViewTreeObserver vto = mTextView.getViewTreeObserver(); |
| 2919 | vto.removeOnPreDrawListener(this); |
| 2920 | } |
| 2921 | } |
| 2922 | |
| 2923 | public int getPositionX() { |
| 2924 | return mPositionX; |
| 2925 | } |
| 2926 | |
| 2927 | public int getPositionY() { |
| 2928 | return mPositionY; |
| 2929 | } |
| 2930 | |
| 2931 | @Override |
| 2932 | public boolean onPreDraw() { |
| 2933 | updatePosition(); |
| 2934 | |
| 2935 | for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { |
| 2936 | if (mPositionHasChanged || mScrollHasChanged || mCanMove[i]) { |
| 2937 | TextViewPositionListener positionListener = mPositionListeners[i]; |
| 2938 | if (positionListener != null) { |
| 2939 | positionListener.updatePosition(mPositionX, mPositionY, |
| 2940 | mPositionHasChanged, mScrollHasChanged); |
| 2941 | } |
| 2942 | } |
| 2943 | } |
| 2944 | |
| 2945 | mScrollHasChanged = false; |
| 2946 | return true; |
| 2947 | } |
| 2948 | |
| 2949 | private void updatePosition() { |
| 2950 | mTextView.getLocationInWindow(mTempCoords); |
| 2951 | |
| 2952 | mPositionHasChanged = mTempCoords[0] != mPositionX || mTempCoords[1] != mPositionY; |
| 2953 | |
| 2954 | mPositionX = mTempCoords[0]; |
| 2955 | mPositionY = mTempCoords[1]; |
| 2956 | } |
| 2957 | |
| 2958 | public void onScrollChanged() { |
| 2959 | mScrollHasChanged = true; |
| 2960 | } |
| 2961 | } |
| 2962 | |
| 2963 | private abstract class PinnedPopupWindow implements TextViewPositionListener { |
| 2964 | protected PopupWindow mPopupWindow; |
| 2965 | protected ViewGroup mContentView; |
| 2966 | int mPositionX, mPositionY; |
Seigo Nonaka | 60490d1 | 2016-01-28 17:25:18 +0900 | [diff] [blame] | 2967 | int mClippingLimitLeft, mClippingLimitRight; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2968 | |
| 2969 | protected abstract void createPopupWindow(); |
| 2970 | protected abstract void initContentView(); |
| 2971 | protected abstract int getTextOffset(); |
| 2972 | protected abstract int getVerticalLocalPosition(int line); |
| 2973 | protected abstract int clipVertically(int positionY); |
| 2974 | |
| 2975 | public PinnedPopupWindow() { |
| 2976 | createPopupWindow(); |
| 2977 | |
Alan Viverette | 80ebe0d | 2015-04-30 15:53:11 -0700 | [diff] [blame] | 2978 | mPopupWindow.setWindowLayoutType( |
| 2979 | WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2980 | mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); |
| 2981 | mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); |
| 2982 | |
| 2983 | initContentView(); |
| 2984 | |
| 2985 | LayoutParams wrapContent = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, |
| 2986 | ViewGroup.LayoutParams.WRAP_CONTENT); |
| 2987 | mContentView.setLayoutParams(wrapContent); |
| 2988 | |
| 2989 | mPopupWindow.setContentView(mContentView); |
| 2990 | } |
| 2991 | |
| 2992 | public void show() { |
| 2993 | getPositionListener().addSubscriber(this, false /* offset is fixed */); |
| 2994 | |
| 2995 | computeLocalPosition(); |
| 2996 | |
| 2997 | final PositionListener positionListener = getPositionListener(); |
| 2998 | updatePosition(positionListener.getPositionX(), positionListener.getPositionY()); |
| 2999 | } |
| 3000 | |
| 3001 | protected void measureContent() { |
| 3002 | final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics(); |
| 3003 | mContentView.measure( |
| 3004 | View.MeasureSpec.makeMeasureSpec(displayMetrics.widthPixels, |
| 3005 | View.MeasureSpec.AT_MOST), |
| 3006 | View.MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels, |
| 3007 | View.MeasureSpec.AT_MOST)); |
| 3008 | } |
| 3009 | |
| 3010 | /* The popup window will be horizontally centered on the getTextOffset() and vertically |
| 3011 | * positioned according to viewportToContentHorizontalOffset. |
| 3012 | * |
| 3013 | * This method assumes that mContentView has properly been measured from its content. */ |
| 3014 | private void computeLocalPosition() { |
| 3015 | measureContent(); |
| 3016 | final int width = mContentView.getMeasuredWidth(); |
| 3017 | final int offset = getTextOffset(); |
| 3018 | mPositionX = (int) (mTextView.getLayout().getPrimaryHorizontal(offset) - width / 2.0f); |
| 3019 | mPositionX += mTextView.viewportToContentHorizontalOffset(); |
| 3020 | |
| 3021 | final int line = mTextView.getLayout().getLineForOffset(offset); |
| 3022 | mPositionY = getVerticalLocalPosition(line); |
| 3023 | mPositionY += mTextView.viewportToContentVerticalOffset(); |
| 3024 | } |
| 3025 | |
| 3026 | private void updatePosition(int parentPositionX, int parentPositionY) { |
| 3027 | int positionX = parentPositionX + mPositionX; |
| 3028 | int positionY = parentPositionY + mPositionY; |
| 3029 | |
| 3030 | positionY = clipVertically(positionY); |
| 3031 | |
| 3032 | // Horizontal clipping |
| 3033 | final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics(); |
| 3034 | final int width = mContentView.getMeasuredWidth(); |
Seigo Nonaka | 60490d1 | 2016-01-28 17:25:18 +0900 | [diff] [blame] | 3035 | positionX = Math.min( |
| 3036 | displayMetrics.widthPixels - width + mClippingLimitRight, positionX); |
| 3037 | positionX = Math.max(-mClippingLimitLeft, positionX); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3038 | |
| 3039 | if (isShowing()) { |
| 3040 | mPopupWindow.update(positionX, positionY, -1, -1); |
| 3041 | } else { |
| 3042 | mPopupWindow.showAtLocation(mTextView, Gravity.NO_GRAVITY, |
| 3043 | positionX, positionY); |
| 3044 | } |
| 3045 | } |
| 3046 | |
| 3047 | public void hide() { |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 3048 | if (!isShowing()) { |
| 3049 | return; |
| 3050 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3051 | mPopupWindow.dismiss(); |
| 3052 | getPositionListener().removeSubscriber(this); |
| 3053 | } |
| 3054 | |
| 3055 | @Override |
| 3056 | public void updatePosition(int parentPositionX, int parentPositionY, |
| 3057 | boolean parentPositionChanged, boolean parentScrolled) { |
| 3058 | // Either parentPositionChanged or parentScrolled is true, check if still visible |
| 3059 | if (isShowing() && isOffsetVisible(getTextOffset())) { |
| 3060 | if (parentScrolled) computeLocalPosition(); |
| 3061 | updatePosition(parentPositionX, parentPositionY); |
| 3062 | } else { |
| 3063 | hide(); |
| 3064 | } |
| 3065 | } |
| 3066 | |
| 3067 | public boolean isShowing() { |
| 3068 | return mPopupWindow.isShowing(); |
| 3069 | } |
| 3070 | } |
| 3071 | |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3072 | private static final class SuggestionInfo { |
Keisuke Kuroyanagi | d3194503 | 2016-02-26 16:09:12 -0800 | [diff] [blame] | 3073 | // Range of actual suggestion within mText |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3074 | int mSuggestionStart, mSuggestionEnd; |
| 3075 | |
| 3076 | // The SuggestionSpan that this TextView represents |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3077 | final SuggestionSpanInfo mSuggestionSpanInfo = new SuggestionSpanInfo(); |
Keisuke Kuroyanagi | d3194503 | 2016-02-26 16:09:12 -0800 | [diff] [blame] | 3078 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3079 | // The index of this suggestion inside suggestionSpan |
| 3080 | int mSuggestionIndex; |
| 3081 | |
| 3082 | final SpannableStringBuilder mText = new SpannableStringBuilder(); |
| 3083 | |
| 3084 | void clear() { |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3085 | mSuggestionSpanInfo.clear(); |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3086 | mText.clear(); |
| 3087 | } |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3088 | |
| 3089 | // Utility method to set attributes about a SuggestionSpan. |
| 3090 | void setSpanInfo(SuggestionSpan span, int spanStart, int spanEnd) { |
| 3091 | mSuggestionSpanInfo.mSuggestionSpan = span; |
| 3092 | mSuggestionSpanInfo.mSpanStart = spanStart; |
| 3093 | mSuggestionSpanInfo.mSpanEnd = spanEnd; |
| 3094 | } |
| 3095 | } |
| 3096 | |
| 3097 | private static final class SuggestionSpanInfo { |
| 3098 | // The SuggestionSpan; |
| 3099 | @Nullable |
| 3100 | SuggestionSpan mSuggestionSpan; |
| 3101 | |
| 3102 | // The SuggestionSpan start position |
| 3103 | int mSpanStart; |
| 3104 | |
| 3105 | // The SuggestionSpan end position |
| 3106 | int mSpanEnd; |
| 3107 | |
| 3108 | void clear() { |
| 3109 | mSuggestionSpan = null; |
| 3110 | } |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3111 | } |
| 3112 | |
| 3113 | private class SuggestionHelper { |
| 3114 | private final Comparator<SuggestionSpan> mSuggestionSpanComparator = |
| 3115 | new SuggestionSpanComparator(); |
| 3116 | private final HashMap<SuggestionSpan, Integer> mSpansLengths = |
| 3117 | new HashMap<SuggestionSpan, Integer>(); |
| 3118 | |
| 3119 | private class SuggestionSpanComparator implements Comparator<SuggestionSpan> { |
| 3120 | public int compare(SuggestionSpan span1, SuggestionSpan span2) { |
| 3121 | final int flag1 = span1.getFlags(); |
| 3122 | final int flag2 = span2.getFlags(); |
| 3123 | if (flag1 != flag2) { |
| 3124 | // The order here should match what is used in updateDrawState |
| 3125 | final boolean easy1 = (flag1 & SuggestionSpan.FLAG_EASY_CORRECT) != 0; |
| 3126 | final boolean easy2 = (flag2 & SuggestionSpan.FLAG_EASY_CORRECT) != 0; |
| 3127 | final boolean misspelled1 = (flag1 & SuggestionSpan.FLAG_MISSPELLED) != 0; |
| 3128 | final boolean misspelled2 = (flag2 & SuggestionSpan.FLAG_MISSPELLED) != 0; |
| 3129 | if (easy1 && !misspelled1) return -1; |
| 3130 | if (easy2 && !misspelled2) return 1; |
| 3131 | if (misspelled1) return -1; |
| 3132 | if (misspelled2) return 1; |
| 3133 | } |
| 3134 | |
| 3135 | return mSpansLengths.get(span1).intValue() - mSpansLengths.get(span2).intValue(); |
| 3136 | } |
| 3137 | } |
| 3138 | |
| 3139 | /** |
| 3140 | * Returns the suggestion spans that cover the current cursor position. The suggestion |
| 3141 | * spans are sorted according to the length of text that they are attached to. |
| 3142 | */ |
| 3143 | private SuggestionSpan[] getSortedSuggestionSpans() { |
| 3144 | int pos = mTextView.getSelectionStart(); |
| 3145 | Spannable spannable = (Spannable) mTextView.getText(); |
| 3146 | SuggestionSpan[] suggestionSpans = spannable.getSpans(pos, pos, SuggestionSpan.class); |
| 3147 | |
| 3148 | mSpansLengths.clear(); |
| 3149 | for (SuggestionSpan suggestionSpan : suggestionSpans) { |
| 3150 | int start = spannable.getSpanStart(suggestionSpan); |
| 3151 | int end = spannable.getSpanEnd(suggestionSpan); |
| 3152 | mSpansLengths.put(suggestionSpan, Integer.valueOf(end - start)); |
| 3153 | } |
| 3154 | |
| 3155 | // The suggestions are sorted according to their types (easy correction first, then |
| 3156 | // misspelled) and to the length of the text that they cover (shorter first). |
| 3157 | Arrays.sort(suggestionSpans, mSuggestionSpanComparator); |
| 3158 | mSpansLengths.clear(); |
| 3159 | |
| 3160 | return suggestionSpans; |
| 3161 | } |
| 3162 | |
| 3163 | /** |
| 3164 | * Gets the SuggestionInfo list that contains suggestion information at the current cursor |
| 3165 | * position. |
| 3166 | * |
| 3167 | * @param suggestionInfos SuggestionInfo array the results will be set. |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3168 | * @param misspelledSpanInfo a struct the misspelled SuggestionSpan info will be set. |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3169 | * @return the number of suggestions actually fetched. |
| 3170 | */ |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3171 | public int getSuggestionInfo(SuggestionInfo[] suggestionInfos, |
| 3172 | @Nullable SuggestionSpanInfo misspelledSpanInfo) { |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3173 | final Spannable spannable = (Spannable) mTextView.getText(); |
| 3174 | final SuggestionSpan[] suggestionSpans = getSortedSuggestionSpans(); |
| 3175 | final int nbSpans = suggestionSpans.length; |
| 3176 | if (nbSpans == 0) return 0; |
| 3177 | |
| 3178 | int numberOfSuggestions = 0; |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3179 | for (final SuggestionSpan suggestionSpan : suggestionSpans) { |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3180 | final int spanStart = spannable.getSpanStart(suggestionSpan); |
| 3181 | final int spanEnd = spannable.getSpanEnd(suggestionSpan); |
| 3182 | |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3183 | if (misspelledSpanInfo != null |
| 3184 | && (suggestionSpan.getFlags() & SuggestionSpan.FLAG_MISSPELLED) != 0) { |
| 3185 | misspelledSpanInfo.mSuggestionSpan = suggestionSpan; |
| 3186 | misspelledSpanInfo.mSpanStart = spanStart; |
| 3187 | misspelledSpanInfo.mSpanEnd = spanEnd; |
| 3188 | } |
| 3189 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3190 | final String[] suggestions = suggestionSpan.getSuggestions(); |
| 3191 | final int nbSuggestions = suggestions.length; |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3192 | suggestionLoop: |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3193 | for (int suggestionIndex = 0; suggestionIndex < nbSuggestions; suggestionIndex++) { |
| 3194 | final String suggestion = suggestions[suggestionIndex]; |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3195 | for (int i = 0; i < numberOfSuggestions; i++) { |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3196 | final SuggestionInfo otherSuggestionInfo = suggestionInfos[i]; |
| 3197 | if (otherSuggestionInfo.mText.toString().equals(suggestion)) { |
| 3198 | final int otherSpanStart = |
| 3199 | otherSuggestionInfo.mSuggestionSpanInfo.mSpanStart; |
| 3200 | final int otherSpanEnd = |
| 3201 | otherSuggestionInfo.mSuggestionSpanInfo.mSpanEnd; |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3202 | if (spanStart == otherSpanStart && spanEnd == otherSpanEnd) { |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3203 | continue suggestionLoop; |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3204 | } |
| 3205 | } |
| 3206 | } |
| 3207 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3208 | SuggestionInfo suggestionInfo = suggestionInfos[numberOfSuggestions]; |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3209 | suggestionInfo.setSpanInfo(suggestionSpan, spanStart, spanEnd); |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3210 | suggestionInfo.mSuggestionIndex = suggestionIndex; |
| 3211 | suggestionInfo.mSuggestionStart = 0; |
| 3212 | suggestionInfo.mSuggestionEnd = suggestion.length(); |
| 3213 | suggestionInfo.mText.replace(0, suggestionInfo.mText.length(), suggestion); |
| 3214 | numberOfSuggestions++; |
| 3215 | if (numberOfSuggestions >= suggestionInfos.length) { |
| 3216 | return numberOfSuggestions; |
| 3217 | } |
| 3218 | } |
| 3219 | } |
| 3220 | return numberOfSuggestions; |
| 3221 | } |
| 3222 | } |
| 3223 | |
Seigo Nonaka | a60160b | 2015-08-19 12:38:35 -0700 | [diff] [blame] | 3224 | @VisibleForTesting |
| 3225 | public class SuggestionsPopupWindow extends PinnedPopupWindow implements OnItemClickListener { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3226 | private static final int MAX_NUMBER_SUGGESTIONS = SuggestionSpan.SUGGESTIONS_MAX_SIZE; |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3227 | |
| 3228 | // Key of intent extras for inserting new word into user dictionary. |
| 3229 | private static final String USER_DICTIONARY_EXTRA_WORD = "word"; |
| 3230 | private static final String USER_DICTIONARY_EXTRA_LOCALE = "locale"; |
| 3231 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3232 | private SuggestionInfo[] mSuggestionInfos; |
| 3233 | private int mNumberOfSuggestions; |
| 3234 | private boolean mCursorWasVisibleBeforeSuggestions; |
| 3235 | private boolean mIsShowingUp = false; |
| 3236 | private SuggestionAdapter mSuggestionsAdapter; |
Seigo Nonaka | bffbd30 | 2015-08-18 18:27:56 -0700 | [diff] [blame] | 3237 | private final TextAppearanceSpan mHighlightSpan = new TextAppearanceSpan( |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3238 | mTextView.getContext(), mTextView.mTextEditSuggestionHighlightStyle); |
| 3239 | private TextView mAddToDictionaryButton; |
| 3240 | private TextView mDeleteButton; |
Seigo Nonaka | f47976e | 2016-03-01 09:17:37 -0800 | [diff] [blame] | 3241 | private ListView mSuggestionListView; |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3242 | private final SuggestionSpanInfo mMisspelledSpanInfo = new SuggestionSpanInfo(); |
Seigo Nonaka | 60490d1 | 2016-01-28 17:25:18 +0900 | [diff] [blame] | 3243 | private int mContainerMarginWidth; |
| 3244 | private int mContainerMarginTop; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3245 | |
| 3246 | private class CustomPopupWindow extends PopupWindow { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3247 | @Override |
| 3248 | public void dismiss() { |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 3249 | if (!isShowing()) { |
| 3250 | return; |
| 3251 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3252 | super.dismiss(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3253 | getPositionListener().removeSubscriber(SuggestionsPopupWindow.this); |
| 3254 | |
| 3255 | // Safe cast since show() checks that mTextView.getText() is an Editable |
| 3256 | ((Spannable) mTextView.getText()).removeSpan(mSuggestionRangeSpan); |
| 3257 | |
| 3258 | mTextView.setCursorVisible(mCursorWasVisibleBeforeSuggestions); |
Keisuke Kuroyanagi | 4a696ac | 2016-02-23 11:02:07 -0800 | [diff] [blame] | 3259 | if (hasInsertionController() && !extractedTextModeWillBeStarted()) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3260 | getInsertionController().show(); |
| 3261 | } |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | public SuggestionsPopupWindow() { |
| 3266 | mCursorWasVisibleBeforeSuggestions = mCursorVisible; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3267 | } |
| 3268 | |
| 3269 | @Override |
| 3270 | protected void createPopupWindow() { |
Seigo Nonaka | 3ed1b39 | 2016-01-19 13:54:59 +0900 | [diff] [blame] | 3271 | mPopupWindow = new CustomPopupWindow(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3272 | mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); |
Seigo Nonaka | 3ed1b39 | 2016-01-19 13:54:59 +0900 | [diff] [blame] | 3273 | mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3274 | mPopupWindow.setFocusable(true); |
| 3275 | mPopupWindow.setClippingEnabled(false); |
| 3276 | } |
| 3277 | |
| 3278 | @Override |
| 3279 | protected void initContentView() { |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3280 | final LayoutInflater inflater = (LayoutInflater) mTextView.getContext(). |
| 3281 | getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Seigo Nonaka | 60490d1 | 2016-01-28 17:25:18 +0900 | [diff] [blame] | 3282 | final ViewGroup relativeLayout = (ViewGroup) inflater.inflate( |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3283 | mTextView.mTextEditSuggestionContainerLayout, null); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3284 | |
Seigo Nonaka | 60490d1 | 2016-01-28 17:25:18 +0900 | [diff] [blame] | 3285 | final LinearLayout suggestionWindowContainer = |
| 3286 | (LinearLayout) relativeLayout.findViewById( |
| 3287 | com.android.internal.R.id.suggestionWindowContainer); |
| 3288 | ViewGroup.MarginLayoutParams lp = |
| 3289 | (ViewGroup.MarginLayoutParams) suggestionWindowContainer.getLayoutParams(); |
| 3290 | mContainerMarginWidth = lp.leftMargin + lp.rightMargin; |
| 3291 | mContainerMarginTop = lp.topMargin; |
| 3292 | mClippingLimitLeft = lp.leftMargin; |
| 3293 | mClippingLimitRight = lp.rightMargin; |
| 3294 | |
Seigo Nonaka | f47976e | 2016-03-01 09:17:37 -0800 | [diff] [blame] | 3295 | mSuggestionListView = (ListView) relativeLayout.findViewById( |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3296 | com.android.internal.R.id.suggestionContainer); |
| 3297 | |
| 3298 | mSuggestionsAdapter = new SuggestionAdapter(); |
Seigo Nonaka | f47976e | 2016-03-01 09:17:37 -0800 | [diff] [blame] | 3299 | mSuggestionListView.setAdapter(mSuggestionsAdapter); |
| 3300 | mSuggestionListView.setOnItemClickListener(this); |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3301 | |
| 3302 | // Inflate the suggestion items once and for all. |
| 3303 | mSuggestionInfos = new SuggestionInfo[MAX_NUMBER_SUGGESTIONS]; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3304 | for (int i = 0; i < mSuggestionInfos.length; i++) { |
| 3305 | mSuggestionInfos[i] = new SuggestionInfo(); |
| 3306 | } |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3307 | |
Seigo Nonaka | 60490d1 | 2016-01-28 17:25:18 +0900 | [diff] [blame] | 3308 | mContentView = relativeLayout; |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3309 | |
Seigo Nonaka | 60490d1 | 2016-01-28 17:25:18 +0900 | [diff] [blame] | 3310 | mAddToDictionaryButton = (TextView) relativeLayout.findViewById( |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3311 | com.android.internal.R.id.addToDictionaryButton); |
| 3312 | mAddToDictionaryButton.setOnClickListener(new View.OnClickListener() { |
| 3313 | public void onClick(View v) { |
Keisuke Kuroyanagi | 6e0860d | 2016-03-15 15:40:43 +0900 | [diff] [blame] | 3314 | final SuggestionSpan misspelledSpan = |
| 3315 | findEquivalentSuggestionSpan(mMisspelledSpanInfo); |
| 3316 | if (misspelledSpan == null) { |
| 3317 | // Span has been removed. |
| 3318 | return; |
| 3319 | } |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3320 | final Editable editable = (Editable) mTextView.getText(); |
Keisuke Kuroyanagi | 6e0860d | 2016-03-15 15:40:43 +0900 | [diff] [blame] | 3321 | final int spanStart = editable.getSpanStart(misspelledSpan); |
| 3322 | final int spanEnd = editable.getSpanEnd(misspelledSpan); |
| 3323 | if (spanStart < 0 || spanEnd <= spanStart) { |
| 3324 | return; |
| 3325 | } |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3326 | final String originalText = TextUtils.substring(editable, spanStart, spanEnd); |
| 3327 | |
| 3328 | final Intent intent = new Intent(Settings.ACTION_USER_DICTIONARY_INSERT); |
| 3329 | intent.putExtra(USER_DICTIONARY_EXTRA_WORD, originalText); |
| 3330 | intent.putExtra(USER_DICTIONARY_EXTRA_LOCALE, |
| 3331 | mTextView.getTextServicesLocale().toString()); |
| 3332 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3333 | mTextView.getContext().startActivity(intent); |
| 3334 | // There is no way to know if the word was indeed added. Re-check. |
| 3335 | // TODO The ExtractEditText should remove the span in the original text instead |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3336 | editable.removeSpan(mMisspelledSpanInfo.mSuggestionSpan); |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3337 | Selection.setSelection(editable, spanEnd); |
| 3338 | updateSpellCheckSpans(spanStart, spanEnd, false); |
| 3339 | hideWithCleanUp(); |
| 3340 | } |
| 3341 | }); |
| 3342 | |
Seigo Nonaka | 60490d1 | 2016-01-28 17:25:18 +0900 | [diff] [blame] | 3343 | mDeleteButton = (TextView) relativeLayout.findViewById( |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3344 | com.android.internal.R.id.deleteButton); |
| 3345 | mDeleteButton.setOnClickListener(new View.OnClickListener() { |
| 3346 | public void onClick(View v) { |
| 3347 | final Editable editable = (Editable) mTextView.getText(); |
| 3348 | |
| 3349 | final int spanUnionStart = editable.getSpanStart(mSuggestionRangeSpan); |
| 3350 | int spanUnionEnd = editable.getSpanEnd(mSuggestionRangeSpan); |
| 3351 | if (spanUnionStart >= 0 && spanUnionEnd > spanUnionStart) { |
| 3352 | // Do not leave two adjacent spaces after deletion, or one at beginning of |
| 3353 | // text |
| 3354 | if (spanUnionEnd < editable.length() && |
| 3355 | Character.isSpaceChar(editable.charAt(spanUnionEnd)) && |
| 3356 | (spanUnionStart == 0 || |
| 3357 | Character.isSpaceChar(editable.charAt(spanUnionStart - 1)))) { |
| 3358 | spanUnionEnd = spanUnionEnd + 1; |
| 3359 | } |
| 3360 | mTextView.deleteText_internal(spanUnionStart, spanUnionEnd); |
| 3361 | } |
| 3362 | hideWithCleanUp(); |
| 3363 | } |
| 3364 | }); |
| 3365 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3366 | } |
| 3367 | |
| 3368 | public boolean isShowingUp() { |
| 3369 | return mIsShowingUp; |
| 3370 | } |
| 3371 | |
| 3372 | public void onParentLostFocus() { |
| 3373 | mIsShowingUp = false; |
| 3374 | } |
| 3375 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3376 | private class SuggestionAdapter extends BaseAdapter { |
| 3377 | private LayoutInflater mInflater = (LayoutInflater) mTextView.getContext(). |
| 3378 | getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 3379 | |
| 3380 | @Override |
| 3381 | public int getCount() { |
| 3382 | return mNumberOfSuggestions; |
| 3383 | } |
| 3384 | |
| 3385 | @Override |
| 3386 | public Object getItem(int position) { |
| 3387 | return mSuggestionInfos[position]; |
| 3388 | } |
| 3389 | |
| 3390 | @Override |
| 3391 | public long getItemId(int position) { |
| 3392 | return position; |
| 3393 | } |
| 3394 | |
| 3395 | @Override |
| 3396 | public View getView(int position, View convertView, ViewGroup parent) { |
| 3397 | TextView textView = (TextView) convertView; |
| 3398 | |
| 3399 | if (textView == null) { |
| 3400 | textView = (TextView) mInflater.inflate(mTextView.mTextEditSuggestionItemLayout, |
| 3401 | parent, false); |
| 3402 | } |
| 3403 | |
| 3404 | final SuggestionInfo suggestionInfo = mSuggestionInfos[position]; |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3405 | textView.setText(suggestionInfo.mText); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3406 | return textView; |
| 3407 | } |
| 3408 | } |
| 3409 | |
Seigo Nonaka | a60160b | 2015-08-19 12:38:35 -0700 | [diff] [blame] | 3410 | @VisibleForTesting |
| 3411 | public ViewGroup getContentViewForTesting() { |
| 3412 | return mContentView; |
| 3413 | } |
| 3414 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3415 | @Override |
| 3416 | public void show() { |
| 3417 | if (!(mTextView.getText() instanceof Editable)) return; |
Keisuke Kuroyanagi | 4a696ac | 2016-02-23 11:02:07 -0800 | [diff] [blame] | 3418 | if (extractedTextModeWillBeStarted()) { |
| 3419 | return; |
| 3420 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3421 | |
| 3422 | if (updateSuggestions()) { |
| 3423 | mCursorWasVisibleBeforeSuggestions = mCursorVisible; |
| 3424 | mTextView.setCursorVisible(false); |
| 3425 | mIsShowingUp = true; |
| 3426 | super.show(); |
| 3427 | } |
| 3428 | } |
| 3429 | |
| 3430 | @Override |
| 3431 | protected void measureContent() { |
| 3432 | final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics(); |
| 3433 | final int horizontalMeasure = View.MeasureSpec.makeMeasureSpec( |
| 3434 | displayMetrics.widthPixels, View.MeasureSpec.AT_MOST); |
| 3435 | final int verticalMeasure = View.MeasureSpec.makeMeasureSpec( |
| 3436 | displayMetrics.heightPixels, View.MeasureSpec.AT_MOST); |
| 3437 | |
| 3438 | int width = 0; |
| 3439 | View view = null; |
| 3440 | for (int i = 0; i < mNumberOfSuggestions; i++) { |
| 3441 | view = mSuggestionsAdapter.getView(i, view, mContentView); |
| 3442 | view.getLayoutParams().width = LayoutParams.WRAP_CONTENT; |
| 3443 | view.measure(horizontalMeasure, verticalMeasure); |
| 3444 | width = Math.max(width, view.getMeasuredWidth()); |
| 3445 | } |
| 3446 | |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3447 | if (mAddToDictionaryButton.getVisibility() != View.GONE) { |
| 3448 | mAddToDictionaryButton.measure(horizontalMeasure, verticalMeasure); |
| 3449 | width = Math.max(width, mAddToDictionaryButton.getMeasuredWidth()); |
| 3450 | } |
| 3451 | |
| 3452 | mDeleteButton.measure(horizontalMeasure, verticalMeasure); |
| 3453 | width = Math.max(width, mDeleteButton.getMeasuredWidth()); |
| 3454 | |
Seigo Nonaka | 60490d1 | 2016-01-28 17:25:18 +0900 | [diff] [blame] | 3455 | width += mContainerMarginWidth; |
| 3456 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3457 | // Enforce the width based on actual text widths |
| 3458 | mContentView.measure( |
| 3459 | View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), |
| 3460 | verticalMeasure); |
| 3461 | |
| 3462 | Drawable popupBackground = mPopupWindow.getBackground(); |
| 3463 | if (popupBackground != null) { |
| 3464 | if (mTempRect == null) mTempRect = new Rect(); |
| 3465 | popupBackground.getPadding(mTempRect); |
| 3466 | width += mTempRect.left + mTempRect.right; |
| 3467 | } |
Seigo Nonaka | f47976e | 2016-03-01 09:17:37 -0800 | [diff] [blame] | 3468 | mSuggestionListView.getLayoutParams().width = width; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3469 | mPopupWindow.setWidth(width); |
| 3470 | } |
| 3471 | |
| 3472 | @Override |
| 3473 | protected int getTextOffset() { |
Keisuke Kuroyanagi | 713be06 | 2016-02-29 16:07:54 -0800 | [diff] [blame] | 3474 | return (mTextView.getSelectionStart() + mTextView.getSelectionStart()) / 2; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3475 | } |
| 3476 | |
| 3477 | @Override |
| 3478 | protected int getVerticalLocalPosition(int line) { |
Seigo Nonaka | 60490d1 | 2016-01-28 17:25:18 +0900 | [diff] [blame] | 3479 | return mTextView.getLayout().getLineBottom(line) - mContainerMarginTop; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3480 | } |
| 3481 | |
| 3482 | @Override |
| 3483 | protected int clipVertically(int positionY) { |
| 3484 | final int height = mContentView.getMeasuredHeight(); |
| 3485 | final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics(); |
| 3486 | return Math.min(positionY, displayMetrics.heightPixels - height); |
| 3487 | } |
| 3488 | |
Seigo Nonaka | 7afa67c | 2015-10-07 17:06:04 +0900 | [diff] [blame] | 3489 | private void hideWithCleanUp() { |
| 3490 | for (final SuggestionInfo info : mSuggestionInfos) { |
| 3491 | info.clear(); |
| 3492 | } |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3493 | mMisspelledSpanInfo.clear(); |
Seigo Nonaka | 7afa67c | 2015-10-07 17:06:04 +0900 | [diff] [blame] | 3494 | hide(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | private boolean updateSuggestions() { |
| 3498 | Spannable spannable = (Spannable) mTextView.getText(); |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3499 | mNumberOfSuggestions = |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3500 | mSuggestionHelper.getSuggestionInfo(mSuggestionInfos, mMisspelledSpanInfo); |
| 3501 | if (mNumberOfSuggestions == 0 && mMisspelledSpanInfo.mSuggestionSpan == null) { |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3502 | return false; |
| 3503 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3504 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3505 | int spanUnionStart = mTextView.getText().length(); |
| 3506 | int spanUnionEnd = 0; |
| 3507 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3508 | for (int i = 0; i < mNumberOfSuggestions; i++) { |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3509 | final SuggestionSpanInfo spanInfo = mSuggestionInfos[i].mSuggestionSpanInfo; |
| 3510 | spanUnionStart = Math.min(spanUnionStart, spanInfo.mSpanStart); |
| 3511 | spanUnionEnd = Math.max(spanUnionEnd, spanInfo.mSpanEnd); |
| 3512 | } |
| 3513 | if (mMisspelledSpanInfo.mSuggestionSpan != null) { |
| 3514 | spanUnionStart = Math.min(spanUnionStart, mMisspelledSpanInfo.mSpanStart); |
| 3515 | spanUnionEnd = Math.max(spanUnionEnd, mMisspelledSpanInfo.mSpanEnd); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3516 | } |
| 3517 | |
| 3518 | for (int i = 0; i < mNumberOfSuggestions; i++) { |
| 3519 | highlightTextDifferences(mSuggestionInfos[i], spanUnionStart, spanUnionEnd); |
| 3520 | } |
| 3521 | |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3522 | // Make "Add to dictionary" item visible if there is a span with the misspelled flag |
| 3523 | int addToDictionaryButtonVisibility = View.GONE; |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3524 | if (mMisspelledSpanInfo.mSuggestionSpan != null) { |
| 3525 | if (mMisspelledSpanInfo.mSpanStart >= 0 |
| 3526 | && mMisspelledSpanInfo.mSpanEnd > mMisspelledSpanInfo.mSpanStart) { |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3527 | addToDictionaryButtonVisibility = View.VISIBLE; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3528 | } |
| 3529 | } |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3530 | mAddToDictionaryButton.setVisibility(addToDictionaryButtonVisibility); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3531 | |
| 3532 | if (mSuggestionRangeSpan == null) mSuggestionRangeSpan = new SuggestionRangeSpan(); |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3533 | final int underlineColor; |
| 3534 | if (mNumberOfSuggestions != 0) { |
| 3535 | underlineColor = |
| 3536 | mSuggestionInfos[0].mSuggestionSpanInfo.mSuggestionSpan.getUnderlineColor(); |
| 3537 | } else { |
| 3538 | underlineColor = mMisspelledSpanInfo.mSuggestionSpan.getUnderlineColor(); |
| 3539 | } |
| 3540 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3541 | if (underlineColor == 0) { |
| 3542 | // Fallback on the default highlight color when the first span does not provide one |
| 3543 | mSuggestionRangeSpan.setBackgroundColor(mTextView.mHighlightColor); |
| 3544 | } else { |
| 3545 | final float BACKGROUND_TRANSPARENCY = 0.4f; |
| 3546 | final int newAlpha = (int) (Color.alpha(underlineColor) * BACKGROUND_TRANSPARENCY); |
| 3547 | mSuggestionRangeSpan.setBackgroundColor( |
| 3548 | (underlineColor & 0x00FFFFFF) + (newAlpha << 24)); |
| 3549 | } |
| 3550 | spannable.setSpan(mSuggestionRangeSpan, spanUnionStart, spanUnionEnd, |
| 3551 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 3552 | |
| 3553 | mSuggestionsAdapter.notifyDataSetChanged(); |
| 3554 | return true; |
| 3555 | } |
| 3556 | |
| 3557 | private void highlightTextDifferences(SuggestionInfo suggestionInfo, int unionStart, |
| 3558 | int unionEnd) { |
| 3559 | final Spannable text = (Spannable) mTextView.getText(); |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3560 | final int spanStart = suggestionInfo.mSuggestionSpanInfo.mSpanStart; |
| 3561 | final int spanEnd = suggestionInfo.mSuggestionSpanInfo.mSpanEnd; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3562 | |
| 3563 | // Adjust the start/end of the suggestion span |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3564 | suggestionInfo.mSuggestionStart = spanStart - unionStart; |
| 3565 | suggestionInfo.mSuggestionEnd = suggestionInfo.mSuggestionStart |
| 3566 | + suggestionInfo.mText.length(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3567 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3568 | suggestionInfo.mText.setSpan(mHighlightSpan, 0, suggestionInfo.mText.length(), |
Seigo Nonaka | bffbd30 | 2015-08-18 18:27:56 -0700 | [diff] [blame] | 3569 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3570 | |
| 3571 | // Add the text before and after the span. |
| 3572 | final String textAsString = text.toString(); |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3573 | suggestionInfo.mText.insert(0, textAsString.substring(unionStart, spanStart)); |
| 3574 | suggestionInfo.mText.append(textAsString.substring(spanEnd, unionEnd)); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | @Override |
| 3578 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3579 | SuggestionInfo suggestionInfo = mSuggestionInfos[position]; |
Keisuke Kuroyanagi | d3194503 | 2016-02-26 16:09:12 -0800 | [diff] [blame] | 3580 | replaceWithSuggestion(suggestionInfo); |
Seigo Nonaka | 7afa67c | 2015-10-07 17:06:04 +0900 | [diff] [blame] | 3581 | hideWithCleanUp(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3582 | } |
| 3583 | } |
| 3584 | |
| 3585 | /** |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3586 | * An ActionMode Callback class that is used to provide actions while in text insertion or |
| 3587 | * selection mode. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3588 | * |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3589 | * The default callback provides a subset of Select All, Cut, Copy, Paste, Share and Replace |
| 3590 | * actions, depending on which of these this TextView supports and the current selection. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3591 | */ |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3592 | private class TextActionModeCallback extends ActionMode.Callback2 { |
Clara Bayarri | ea4f150 | 2015-03-18 00:25:01 +0000 | [diff] [blame] | 3593 | private final Path mSelectionPath = new Path(); |
| 3594 | private final RectF mSelectionBounds = new RectF(); |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3595 | private final boolean mHasSelection; |
Clara Bayarri | ea4f150 | 2015-03-18 00:25:01 +0000 | [diff] [blame] | 3596 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3597 | private int mHandleHeight; |
Clara Bayarri | ea4f150 | 2015-03-18 00:25:01 +0000 | [diff] [blame] | 3598 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3599 | public TextActionModeCallback(boolean hasSelection) { |
| 3600 | mHasSelection = hasSelection; |
| 3601 | if (mHasSelection) { |
| 3602 | SelectionModifierCursorController selectionController = getSelectionController(); |
| 3603 | if (selectionController.mStartHandle == null) { |
| 3604 | // As these are for initializing selectionController, hide() must be called. |
| 3605 | selectionController.initDrawables(); |
| 3606 | selectionController.initHandles(); |
| 3607 | selectionController.hide(); |
| 3608 | } |
| 3609 | mHandleHeight = Math.max( |
| 3610 | mSelectHandleLeft.getMinimumHeight(), |
| 3611 | mSelectHandleRight.getMinimumHeight()); |
| 3612 | } else { |
| 3613 | InsertionPointCursorController insertionController = getInsertionController(); |
| 3614 | if (insertionController != null) { |
| 3615 | insertionController.getHandle(); |
| 3616 | mHandleHeight = mSelectHandleCenter.getMinimumHeight(); |
| 3617 | } |
Clara Bayarri | 7fc946e | 2015-03-31 14:48:33 +0100 | [diff] [blame] | 3618 | } |
Clara Bayarri | ea4f150 | 2015-03-18 00:25:01 +0000 | [diff] [blame] | 3619 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3620 | |
| 3621 | @Override |
| 3622 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3623 | mode.setTitle(null); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 3624 | mode.setSubtitle(null); |
| 3625 | mode.setTitleOptionalHint(true); |
| 3626 | populateMenuWithItems(menu); |
| 3627 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3628 | Callback customCallback = getCustomCallback(); |
| 3629 | if (customCallback != null) { |
| 3630 | if (!customCallback.onCreateActionMode(mode, menu)) { |
Clara Bayarri | 01243ac | 2015-06-03 00:46:29 +0100 | [diff] [blame] | 3631 | // The custom mode can choose to cancel the action mode, dismiss selection. |
| 3632 | Selection.setSelection((Spannable) mTextView.getText(), |
| 3633 | mTextView.getSelectionEnd()); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 3634 | return false; |
| 3635 | } |
| 3636 | } |
| 3637 | |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 3638 | if (mTextView.canProcessText()) { |
| 3639 | mProcessTextIntentActionsHandler.onInitializeMenu(menu); |
| 3640 | } |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 3641 | |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 3642 | if (menu.hasVisibleItems() || mode.getCustomView() != null) { |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 3643 | return true; |
| 3644 | } else { |
| 3645 | return false; |
| 3646 | } |
| 3647 | } |
| 3648 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3649 | private Callback getCustomCallback() { |
| 3650 | return mHasSelection |
| 3651 | ? mCustomSelectionActionModeCallback |
| 3652 | : mCustomInsertionActionModeCallback; |
| 3653 | } |
| 3654 | |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 3655 | private void populateMenuWithItems(Menu menu) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3656 | if (mTextView.canCut()) { |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 3657 | menu.add(Menu.NONE, TextView.ID_CUT, MENU_ITEM_ORDER_CUT, |
| 3658 | com.android.internal.R.string.cut). |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3659 | setAlphabeticShortcut('x'). |
Clara Bayarri | aee243b | 2015-05-18 17:05:46 +0100 | [diff] [blame] | 3660 | setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3661 | } |
| 3662 | |
| 3663 | if (mTextView.canCopy()) { |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 3664 | menu.add(Menu.NONE, TextView.ID_COPY, MENU_ITEM_ORDER_COPY, |
| 3665 | com.android.internal.R.string.copy). |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3666 | setAlphabeticShortcut('c'). |
Clara Bayarri | aee243b | 2015-05-18 17:05:46 +0100 | [diff] [blame] | 3667 | setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | if (mTextView.canPaste()) { |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 3671 | menu.add(Menu.NONE, TextView.ID_PASTE, MENU_ITEM_ORDER_PASTE, |
| 3672 | com.android.internal.R.string.paste). |
| 3673 | setAlphabeticShortcut('v'). |
| 3674 | setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3675 | } |
| 3676 | |
Andrei Stingaceanu | 7f0c5bd | 2015-04-14 17:12:08 +0100 | [diff] [blame] | 3677 | if (mTextView.canShare()) { |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 3678 | menu.add(Menu.NONE, TextView.ID_SHARE, MENU_ITEM_ORDER_SHARE, |
| 3679 | com.android.internal.R.string.share). |
| 3680 | setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); |
Andrei Stingaceanu | 7f0c5bd | 2015-04-14 17:12:08 +0100 | [diff] [blame] | 3681 | } |
| 3682 | |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 3683 | updateSelectAllItem(menu); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 3684 | updateReplaceItem(menu); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3685 | } |
| 3686 | |
| 3687 | @Override |
| 3688 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 3689 | updateSelectAllItem(menu); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 3690 | updateReplaceItem(menu); |
| 3691 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3692 | Callback customCallback = getCustomCallback(); |
| 3693 | if (customCallback != null) { |
| 3694 | return customCallback.onPrepareActionMode(mode, menu); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3695 | } |
| 3696 | return true; |
| 3697 | } |
| 3698 | |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 3699 | private void updateSelectAllItem(Menu menu) { |
| 3700 | boolean canSelectAll = mTextView.canSelectAllText(); |
| 3701 | boolean selectAllItemExists = menu.findItem(TextView.ID_SELECT_ALL) != null; |
| 3702 | if (canSelectAll && !selectAllItemExists) { |
| 3703 | menu.add(Menu.NONE, TextView.ID_SELECT_ALL, MENU_ITEM_ORDER_SELECT_ALL, |
| 3704 | com.android.internal.R.string.selectAll) |
| 3705 | .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); |
| 3706 | } else if (!canSelectAll && selectAllItemExists) { |
| 3707 | menu.removeItem(TextView.ID_SELECT_ALL); |
| 3708 | } |
| 3709 | } |
| 3710 | |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 3711 | private void updateReplaceItem(Menu menu) { |
Keisuke Kuroyanagi | d3194503 | 2016-02-26 16:09:12 -0800 | [diff] [blame] | 3712 | boolean canReplace = mTextView.isSuggestionsEnabled() && shouldOfferToShowSuggestions(); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 3713 | boolean replaceItemExists = menu.findItem(TextView.ID_REPLACE) != null; |
| 3714 | if (canReplace && !replaceItemExists) { |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 3715 | menu.add(Menu.NONE, TextView.ID_REPLACE, MENU_ITEM_ORDER_REPLACE, |
| 3716 | com.android.internal.R.string.replace) |
| 3717 | .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 3718 | } else if (!canReplace && replaceItemExists) { |
| 3719 | menu.removeItem(TextView.ID_REPLACE); |
| 3720 | } |
| 3721 | } |
| 3722 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3723 | @Override |
| 3724 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 3725 | if (mProcessTextIntentActionsHandler.performMenuItemAction(item)) { |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 3726 | return true; |
| 3727 | } |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3728 | Callback customCallback = getCustomCallback(); |
| 3729 | if (customCallback != null && customCallback.onActionItemClicked(mode, item)) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3730 | return true; |
| 3731 | } |
| 3732 | return mTextView.onTextContextMenuItem(item.getItemId()); |
| 3733 | } |
| 3734 | |
| 3735 | @Override |
| 3736 | public void onDestroyActionMode(ActionMode mode) { |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 3737 | // Clear mTextActionMode not to recursively destroy action mode by clearing selection. |
| 3738 | mTextActionMode = null; |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3739 | Callback customCallback = getCustomCallback(); |
| 3740 | if (customCallback != null) { |
| 3741 | customCallback.onDestroyActionMode(mode); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3742 | } |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 3743 | |
Keisuke Kuroyanagi | af4caa6 | 2016-02-29 12:53:58 -0800 | [diff] [blame] | 3744 | if (!mPreserveSelection) { |
| 3745 | /* |
| 3746 | * Leave current selection when we tentatively destroy action mode for the |
| 3747 | * selection. If we're detaching from a window, we'll bring back the selection |
| 3748 | * mode when (if) we get reattached. |
| 3749 | */ |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 3750 | Selection.setSelection((Spannable) mTextView.getText(), |
| 3751 | mTextView.getSelectionEnd()); |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 3752 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3753 | |
| 3754 | if (mSelectionModifierCursorController != null) { |
| 3755 | mSelectionModifierCursorController.hide(); |
| 3756 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3757 | } |
Clara Bayarri | ea4f150 | 2015-03-18 00:25:01 +0000 | [diff] [blame] | 3758 | |
| 3759 | @Override |
| 3760 | public void onGetContentRect(ActionMode mode, View view, Rect outRect) { |
| 3761 | if (!view.equals(mTextView) || mTextView.getLayout() == null) { |
| 3762 | super.onGetContentRect(mode, view, outRect); |
| 3763 | return; |
| 3764 | } |
| 3765 | if (mTextView.getSelectionStart() != mTextView.getSelectionEnd()) { |
| 3766 | // We have a selection. |
| 3767 | mSelectionPath.reset(); |
| 3768 | mTextView.getLayout().getSelectionPath( |
| 3769 | mTextView.getSelectionStart(), mTextView.getSelectionEnd(), mSelectionPath); |
| 3770 | mSelectionPath.computeBounds(mSelectionBounds, true); |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3771 | mSelectionBounds.bottom += mHandleHeight; |
Clara Bayarri | b7419dd | 2015-04-09 15:24:58 +0100 | [diff] [blame] | 3772 | } else if (mCursorCount == 2) { |
| 3773 | // We have a split cursor. In this case, we take the rectangle that includes both |
| 3774 | // parts of the cursor to ensure we don't obscure either of them. |
| 3775 | Rect firstCursorBounds = mCursorDrawable[0].getBounds(); |
| 3776 | Rect secondCursorBounds = mCursorDrawable[1].getBounds(); |
| 3777 | mSelectionBounds.set( |
| 3778 | Math.min(firstCursorBounds.left, secondCursorBounds.left), |
| 3779 | Math.min(firstCursorBounds.top, secondCursorBounds.top), |
| 3780 | Math.max(firstCursorBounds.right, secondCursorBounds.right), |
| 3781 | Math.max(firstCursorBounds.bottom, secondCursorBounds.bottom) |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3782 | + mHandleHeight); |
Clara Bayarri | ea4f150 | 2015-03-18 00:25:01 +0000 | [diff] [blame] | 3783 | } else { |
| 3784 | // We have a single cursor. |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 3785 | Layout layout = mTextView.getLayout(); |
Mady Mellor | ff66ca5 | 2015-07-08 12:31:45 -0700 | [diff] [blame] | 3786 | int line = layout.getLineForOffset(mTextView.getSelectionStart()); |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 3787 | float primaryHorizontal = clampHorizontalPosition(null, |
| 3788 | layout.getPrimaryHorizontal(mTextView.getSelectionStart())); |
Clara Bayarri | ea4f150 | 2015-03-18 00:25:01 +0000 | [diff] [blame] | 3789 | mSelectionBounds.set( |
| 3790 | primaryHorizontal, |
Mady Mellor | ff66ca5 | 2015-07-08 12:31:45 -0700 | [diff] [blame] | 3791 | layout.getLineTop(line), |
Clara Bayarri | f95ed10 | 2015-08-12 19:46:47 +0100 | [diff] [blame] | 3792 | primaryHorizontal, |
Mady Mellor | ff66ca5 | 2015-07-08 12:31:45 -0700 | [diff] [blame] | 3793 | layout.getLineTop(line + 1) + mHandleHeight); |
Clara Bayarri | ea4f150 | 2015-03-18 00:25:01 +0000 | [diff] [blame] | 3794 | } |
| 3795 | // Take TextView's padding and scroll into account. |
| 3796 | int textHorizontalOffset = mTextView.viewportToContentHorizontalOffset(); |
| 3797 | int textVerticalOffset = mTextView.viewportToContentVerticalOffset(); |
| 3798 | outRect.set( |
| 3799 | (int) Math.floor(mSelectionBounds.left + textHorizontalOffset), |
| 3800 | (int) Math.floor(mSelectionBounds.top + textVerticalOffset), |
| 3801 | (int) Math.ceil(mSelectionBounds.right + textHorizontalOffset), |
| 3802 | (int) Math.ceil(mSelectionBounds.bottom + textVerticalOffset)); |
| 3803 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3804 | } |
| 3805 | |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 3806 | /** |
| 3807 | * A listener to call {@link InputMethodManager#updateCursorAnchorInfo(View, CursorAnchorInfo)} |
| 3808 | * while the input method is requesting the cursor/anchor position. Does nothing as long as |
| 3809 | * {@link InputMethodManager#isWatchingCursor(View)} returns false. |
| 3810 | */ |
| 3811 | private final class CursorAnchorInfoNotifier implements TextViewPositionListener { |
Yohei Yukawa | c46b5f0 | 2014-06-10 12:26:34 +0900 | [diff] [blame] | 3812 | final CursorAnchorInfo.Builder mSelectionInfoBuilder = new CursorAnchorInfo.Builder(); |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 3813 | final int[] mTmpIntOffset = new int[2]; |
| 3814 | final Matrix mViewToScreenMatrix = new Matrix(); |
| 3815 | |
| 3816 | @Override |
| 3817 | public void updatePosition(int parentPositionX, int parentPositionY, |
| 3818 | boolean parentPositionChanged, boolean parentScrolled) { |
| 3819 | final InputMethodState ims = mInputMethodState; |
| 3820 | if (ims == null || ims.mBatchEditNesting > 0) { |
| 3821 | return; |
| 3822 | } |
| 3823 | final InputMethodManager imm = InputMethodManager.peekInstance(); |
| 3824 | if (null == imm) { |
| 3825 | return; |
| 3826 | } |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 3827 | if (!imm.isActive(mTextView)) { |
| 3828 | return; |
| 3829 | } |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 3830 | // Skip if the IME has not requested the cursor/anchor position. |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 3831 | if (!imm.isCursorAnchorInfoEnabled()) { |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 3832 | return; |
| 3833 | } |
| 3834 | Layout layout = mTextView.getLayout(); |
| 3835 | if (layout == null) { |
| 3836 | return; |
| 3837 | } |
| 3838 | |
Yohei Yukawa | c46b5f0 | 2014-06-10 12:26:34 +0900 | [diff] [blame] | 3839 | final CursorAnchorInfo.Builder builder = mSelectionInfoBuilder; |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 3840 | builder.reset(); |
| 3841 | |
| 3842 | final int selectionStart = mTextView.getSelectionStart(); |
Yohei Yukawa | 81f4cb3 | 2014-05-13 22:20:35 +0900 | [diff] [blame] | 3843 | builder.setSelectionRange(selectionStart, mTextView.getSelectionEnd()); |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 3844 | |
| 3845 | // Construct transformation matrix from view local coordinates to screen coordinates. |
| 3846 | mViewToScreenMatrix.set(mTextView.getMatrix()); |
| 3847 | mTextView.getLocationOnScreen(mTmpIntOffset); |
| 3848 | mViewToScreenMatrix.postTranslate(mTmpIntOffset[0], mTmpIntOffset[1]); |
| 3849 | builder.setMatrix(mViewToScreenMatrix); |
| 3850 | |
| 3851 | final float viewportToContentHorizontalOffset = |
| 3852 | mTextView.viewportToContentHorizontalOffset(); |
| 3853 | final float viewportToContentVerticalOffset = |
| 3854 | mTextView.viewportToContentVerticalOffset(); |
| 3855 | |
Yohei Yukawa | 81f4cb3 | 2014-05-13 22:20:35 +0900 | [diff] [blame] | 3856 | final CharSequence text = mTextView.getText(); |
| 3857 | if (text instanceof Spannable) { |
| 3858 | final Spannable sp = (Spannable) text; |
| 3859 | int composingTextStart = EditableInputConnection.getComposingSpanStart(sp); |
| 3860 | int composingTextEnd = EditableInputConnection.getComposingSpanEnd(sp); |
| 3861 | if (composingTextEnd < composingTextStart) { |
| 3862 | final int temp = composingTextEnd; |
| 3863 | composingTextEnd = composingTextStart; |
| 3864 | composingTextStart = temp; |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 3865 | } |
Yohei Yukawa | 81f4cb3 | 2014-05-13 22:20:35 +0900 | [diff] [blame] | 3866 | final boolean hasComposingText = |
| 3867 | (0 <= composingTextStart) && (composingTextStart < composingTextEnd); |
| 3868 | if (hasComposingText) { |
| 3869 | final CharSequence composingText = text.subSequence(composingTextStart, |
| 3870 | composingTextEnd); |
| 3871 | builder.setComposingText(composingTextStart, composingText); |
Yohei Yukawa | 5f183f0 | 2014-09-02 14:18:40 -0700 | [diff] [blame] | 3872 | |
| 3873 | final int minLine = layout.getLineForOffset(composingTextStart); |
| 3874 | final int maxLine = layout.getLineForOffset(composingTextEnd - 1); |
| 3875 | for (int line = minLine; line <= maxLine; ++line) { |
| 3876 | final int lineStart = layout.getLineStart(line); |
| 3877 | final int lineEnd = layout.getLineEnd(line); |
| 3878 | final int offsetStart = Math.max(lineStart, composingTextStart); |
| 3879 | final int offsetEnd = Math.min(lineEnd, composingTextEnd); |
| 3880 | final boolean ltrLine = |
| 3881 | layout.getParagraphDirection(line) == Layout.DIR_LEFT_TO_RIGHT; |
| 3882 | final float[] widths = new float[offsetEnd - offsetStart]; |
| 3883 | layout.getPaint().getTextWidths(text, offsetStart, offsetEnd, widths); |
| 3884 | final float top = layout.getLineTop(line); |
| 3885 | final float bottom = layout.getLineBottom(line); |
| 3886 | for (int offset = offsetStart; offset < offsetEnd; ++offset) { |
| 3887 | final float charWidth = widths[offset - offsetStart]; |
| 3888 | final boolean isRtl = layout.isRtlCharAt(offset); |
| 3889 | final float primary = layout.getPrimaryHorizontal(offset); |
| 3890 | final float secondary = layout.getSecondaryHorizontal(offset); |
| 3891 | // TODO: This doesn't work perfectly for text with custom styles and |
| 3892 | // TAB chars. |
| 3893 | final float left; |
| 3894 | final float right; |
| 3895 | if (ltrLine) { |
| 3896 | if (isRtl) { |
| 3897 | left = secondary - charWidth; |
| 3898 | right = secondary; |
| 3899 | } else { |
| 3900 | left = primary; |
| 3901 | right = primary + charWidth; |
| 3902 | } |
| 3903 | } else { |
| 3904 | if (!isRtl) { |
| 3905 | left = secondary; |
| 3906 | right = secondary + charWidth; |
| 3907 | } else { |
| 3908 | left = primary - charWidth; |
| 3909 | right = primary; |
| 3910 | } |
| 3911 | } |
| 3912 | // TODO: Check top-right and bottom-left as well. |
| 3913 | final float localLeft = left + viewportToContentHorizontalOffset; |
| 3914 | final float localRight = right + viewportToContentHorizontalOffset; |
| 3915 | final float localTop = top + viewportToContentVerticalOffset; |
| 3916 | final float localBottom = bottom + viewportToContentVerticalOffset; |
| 3917 | final boolean isTopLeftVisible = isPositionVisible(localLeft, localTop); |
| 3918 | final boolean isBottomRightVisible = |
| 3919 | isPositionVisible(localRight, localBottom); |
| 3920 | int characterBoundsFlags = 0; |
| 3921 | if (isTopLeftVisible || isBottomRightVisible) { |
| 3922 | characterBoundsFlags |= CursorAnchorInfo.FLAG_HAS_VISIBLE_REGION; |
| 3923 | } |
Andreas Gampe | 4976e2d | 2015-03-17 16:08:43 -0700 | [diff] [blame] | 3924 | if (!isTopLeftVisible || !isBottomRightVisible) { |
Yohei Yukawa | 5f183f0 | 2014-09-02 14:18:40 -0700 | [diff] [blame] | 3925 | characterBoundsFlags |= CursorAnchorInfo.FLAG_HAS_INVISIBLE_REGION; |
| 3926 | } |
| 3927 | if (isRtl) { |
| 3928 | characterBoundsFlags |= CursorAnchorInfo.FLAG_IS_RTL; |
| 3929 | } |
| 3930 | // Here offset is the index in Java chars. |
| 3931 | builder.addCharacterBounds(offset, localLeft, localTop, localRight, |
| 3932 | localBottom, characterBoundsFlags); |
| 3933 | } |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 3934 | } |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 3935 | } |
| 3936 | } |
| 3937 | |
| 3938 | // Treat selectionStart as the insertion point. |
| 3939 | if (0 <= selectionStart) { |
| 3940 | final int offset = selectionStart; |
| 3941 | final int line = layout.getLineForOffset(offset); |
| 3942 | final float insertionMarkerX = layout.getPrimaryHorizontal(offset) |
| 3943 | + viewportToContentHorizontalOffset; |
| 3944 | final float insertionMarkerTop = layout.getLineTop(line) |
| 3945 | + viewportToContentVerticalOffset; |
| 3946 | final float insertionMarkerBaseline = layout.getLineBaseline(line) |
| 3947 | + viewportToContentVerticalOffset; |
| 3948 | final float insertionMarkerBottom = layout.getLineBottom(line) |
| 3949 | + viewportToContentVerticalOffset; |
Yohei Yukawa | cc24e2b | 2014-08-29 20:21:10 -0700 | [diff] [blame] | 3950 | final boolean isTopVisible = |
| 3951 | isPositionVisible(insertionMarkerX, insertionMarkerTop); |
| 3952 | final boolean isBottomVisible = |
| 3953 | isPositionVisible(insertionMarkerX, insertionMarkerBottom); |
| 3954 | int insertionMarkerFlags = 0; |
| 3955 | if (isTopVisible || isBottomVisible) { |
| 3956 | insertionMarkerFlags |= CursorAnchorInfo.FLAG_HAS_VISIBLE_REGION; |
| 3957 | } |
| 3958 | if (!isTopVisible || !isBottomVisible) { |
| 3959 | insertionMarkerFlags |= CursorAnchorInfo.FLAG_HAS_INVISIBLE_REGION; |
| 3960 | } |
Yohei Yukawa | 5f183f0 | 2014-09-02 14:18:40 -0700 | [diff] [blame] | 3961 | if (layout.isRtlCharAt(offset)) { |
| 3962 | insertionMarkerFlags |= CursorAnchorInfo.FLAG_IS_RTL; |
| 3963 | } |
Yohei Yukawa | 0b01e7f | 2014-07-08 15:29:51 +0900 | [diff] [blame] | 3964 | builder.setInsertionMarkerLocation(insertionMarkerX, insertionMarkerTop, |
Yohei Yukawa | cc24e2b | 2014-08-29 20:21:10 -0700 | [diff] [blame] | 3965 | insertionMarkerBaseline, insertionMarkerBottom, insertionMarkerFlags); |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 3966 | } |
| 3967 | |
| 3968 | imm.updateCursorAnchorInfo(mTextView, builder.build()); |
| 3969 | } |
| 3970 | } |
| 3971 | |
Keisuke Kuroyanagi | da79ee6 | 2015-11-25 16:15:15 +0900 | [diff] [blame] | 3972 | @VisibleForTesting |
| 3973 | public abstract class HandleView extends View implements TextViewPositionListener { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3974 | protected Drawable mDrawable; |
| 3975 | protected Drawable mDrawableLtr; |
| 3976 | protected Drawable mDrawableRtl; |
| 3977 | private final PopupWindow mContainer; |
| 3978 | // Position with respect to the parent TextView |
| 3979 | private int mPositionX, mPositionY; |
| 3980 | private boolean mIsDragging; |
| 3981 | // Offset from touch position to mPosition |
| 3982 | private float mTouchToWindowOffsetX, mTouchToWindowOffsetY; |
| 3983 | protected int mHotspotX; |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 3984 | protected int mHorizontalGravity; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3985 | // Offsets the hotspot point up, so that cursor is not hidden by the finger when moving up |
| 3986 | private float mTouchOffsetY; |
| 3987 | // Where the touch position should be on the handle to ensure a maximum cursor visibility |
| 3988 | private float mIdealVerticalOffset; |
| 3989 | // Parent's (TextView) previous position in window |
| 3990 | private int mLastParentX, mLastParentY; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3991 | // Previous text character offset |
Mady Mellor | c2225b9 | 2015-04-01 15:59:20 -0700 | [diff] [blame] | 3992 | protected int mPreviousOffset = -1; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3993 | // Previous text character offset |
| 3994 | private boolean mPositionHasChanged = true; |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 3995 | // Minimum touch target size for handles |
| 3996 | private int mMinSize; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 3997 | // Indicates the line of text that the handle is on. |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 3998 | protected int mPrevLine = UNSET_LINE; |
| 3999 | // Indicates the line of text that the user was touching. This can differ from mPrevLine |
| 4000 | // when selecting text when the handles jump to the end / start of words which may be on |
| 4001 | // a different line. |
| 4002 | protected int mPreviousLineTouched = UNSET_LINE; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4003 | |
Keisuke Kuroyanagi | da79ee6 | 2015-11-25 16:15:15 +0900 | [diff] [blame] | 4004 | private HandleView(Drawable drawableLtr, Drawable drawableRtl, final int id) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4005 | super(mTextView.getContext()); |
Keisuke Kuroyanagi | da79ee6 | 2015-11-25 16:15:15 +0900 | [diff] [blame] | 4006 | setId(id); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4007 | mContainer = new PopupWindow(mTextView.getContext(), null, |
| 4008 | com.android.internal.R.attr.textSelectHandleWindowStyle); |
| 4009 | mContainer.setSplitTouchEnabled(true); |
| 4010 | mContainer.setClippingEnabled(false); |
| 4011 | mContainer.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL); |
Keisuke Kuroyanagi | 7340be7 | 2015-02-27 17:57:49 +0900 | [diff] [blame] | 4012 | mContainer.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); |
| 4013 | mContainer.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4014 | mContainer.setContentView(this); |
| 4015 | |
| 4016 | mDrawableLtr = drawableLtr; |
| 4017 | mDrawableRtl = drawableRtl; |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4018 | mMinSize = mTextView.getContext().getResources().getDimensionPixelSize( |
| 4019 | com.android.internal.R.dimen.text_handle_min_size); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4020 | |
| 4021 | updateDrawable(); |
| 4022 | |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4023 | final int handleHeight = getPreferredHeight(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4024 | mTouchOffsetY = -0.3f * handleHeight; |
| 4025 | mIdealVerticalOffset = 0.7f * handleHeight; |
| 4026 | } |
| 4027 | |
Mady Mellor | 7a93644 | 2015-05-20 10:05:52 -0700 | [diff] [blame] | 4028 | public float getIdealVerticalOffset() { |
| 4029 | return mIdealVerticalOffset; |
| 4030 | } |
| 4031 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4032 | protected void updateDrawable() { |
Keisuke Kuroyanagi | dbe2c29 | 2015-05-27 19:49:34 +0900 | [diff] [blame] | 4033 | if (mIsDragging) { |
| 4034 | // Don't update drawable during dragging. |
| 4035 | return; |
| 4036 | } |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4037 | final Layout layout = mTextView.getLayout(); |
| 4038 | if (layout == null) { |
| 4039 | return; |
| 4040 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4041 | final int offset = getCurrentCursorOffset(); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4042 | final boolean isRtlCharAtOffset = isAtRtlRun(layout, offset); |
Keisuke Kuroyanagi | 33f81ac | 2015-05-14 20:10:57 +0900 | [diff] [blame] | 4043 | final Drawable oldDrawable = mDrawable; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4044 | mDrawable = isRtlCharAtOffset ? mDrawableRtl : mDrawableLtr; |
| 4045 | mHotspotX = getHotspotX(mDrawable, isRtlCharAtOffset); |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4046 | mHorizontalGravity = getHorizontalGravity(isRtlCharAtOffset); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4047 | if (oldDrawable != mDrawable && isShowing()) { |
Keisuke Kuroyanagi | dbe2c29 | 2015-05-27 19:49:34 +0900 | [diff] [blame] | 4048 | // Update popup window position. |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 4049 | mPositionX = getCursorHorizontalPosition(layout, offset) - mHotspotX - |
| 4050 | getHorizontalOffset() + getCursorOffset(); |
Keisuke Kuroyanagi | dbe2c29 | 2015-05-27 19:49:34 +0900 | [diff] [blame] | 4051 | mPositionX += mTextView.viewportToContentHorizontalOffset(); |
| 4052 | mPositionHasChanged = true; |
| 4053 | updatePosition(mLastParentX, mLastParentY, false, false); |
Keisuke Kuroyanagi | 33f81ac | 2015-05-14 20:10:57 +0900 | [diff] [blame] | 4054 | postInvalidate(); |
| 4055 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4056 | } |
| 4057 | |
| 4058 | protected abstract int getHotspotX(Drawable drawable, boolean isRtlRun); |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4059 | protected abstract int getHorizontalGravity(boolean isRtlRun); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4060 | |
| 4061 | // Touch-up filter: number of previous positions remembered |
| 4062 | private static final int HISTORY_SIZE = 5; |
| 4063 | private static final int TOUCH_UP_FILTER_DELAY_AFTER = 150; |
| 4064 | private static final int TOUCH_UP_FILTER_DELAY_BEFORE = 350; |
| 4065 | private final long[] mPreviousOffsetsTimes = new long[HISTORY_SIZE]; |
| 4066 | private final int[] mPreviousOffsets = new int[HISTORY_SIZE]; |
| 4067 | private int mPreviousOffsetIndex = 0; |
| 4068 | private int mNumberPreviousOffsets = 0; |
| 4069 | |
| 4070 | private void startTouchUpFilter(int offset) { |
| 4071 | mNumberPreviousOffsets = 0; |
| 4072 | addPositionToTouchUpFilter(offset); |
| 4073 | } |
| 4074 | |
| 4075 | private void addPositionToTouchUpFilter(int offset) { |
| 4076 | mPreviousOffsetIndex = (mPreviousOffsetIndex + 1) % HISTORY_SIZE; |
| 4077 | mPreviousOffsets[mPreviousOffsetIndex] = offset; |
| 4078 | mPreviousOffsetsTimes[mPreviousOffsetIndex] = SystemClock.uptimeMillis(); |
| 4079 | mNumberPreviousOffsets++; |
| 4080 | } |
| 4081 | |
| 4082 | private void filterOnTouchUp() { |
| 4083 | final long now = SystemClock.uptimeMillis(); |
| 4084 | int i = 0; |
| 4085 | int index = mPreviousOffsetIndex; |
| 4086 | final int iMax = Math.min(mNumberPreviousOffsets, HISTORY_SIZE); |
| 4087 | while (i < iMax && (now - mPreviousOffsetsTimes[index]) < TOUCH_UP_FILTER_DELAY_AFTER) { |
| 4088 | i++; |
| 4089 | index = (mPreviousOffsetIndex - i + HISTORY_SIZE) % HISTORY_SIZE; |
| 4090 | } |
| 4091 | |
| 4092 | if (i > 0 && i < iMax && |
| 4093 | (now - mPreviousOffsetsTimes[index]) > TOUCH_UP_FILTER_DELAY_BEFORE) { |
| 4094 | positionAtCursorOffset(mPreviousOffsets[index], false); |
| 4095 | } |
| 4096 | } |
| 4097 | |
| 4098 | public boolean offsetHasBeenChanged() { |
| 4099 | return mNumberPreviousOffsets > 1; |
| 4100 | } |
| 4101 | |
| 4102 | @Override |
| 4103 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4104 | setMeasuredDimension(getPreferredWidth(), getPreferredHeight()); |
| 4105 | } |
| 4106 | |
| 4107 | private int getPreferredWidth() { |
| 4108 | return Math.max(mDrawable.getIntrinsicWidth(), mMinSize); |
| 4109 | } |
| 4110 | |
| 4111 | private int getPreferredHeight() { |
| 4112 | return Math.max(mDrawable.getIntrinsicHeight(), mMinSize); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4113 | } |
| 4114 | |
| 4115 | public void show() { |
| 4116 | if (isShowing()) return; |
| 4117 | |
| 4118 | getPositionListener().addSubscriber(this, true /* local position may change */); |
| 4119 | |
| 4120 | // Make sure the offset is always considered new, even when focusing at same position |
| 4121 | mPreviousOffset = -1; |
| 4122 | positionAtCursorOffset(getCurrentCursorOffset(), false); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | protected void dismiss() { |
| 4126 | mIsDragging = false; |
| 4127 | mContainer.dismiss(); |
| 4128 | onDetached(); |
| 4129 | } |
| 4130 | |
| 4131 | public void hide() { |
| 4132 | dismiss(); |
| 4133 | |
| 4134 | getPositionListener().removeSubscriber(this); |
| 4135 | } |
| 4136 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4137 | public boolean isShowing() { |
| 4138 | return mContainer.isShowing(); |
| 4139 | } |
| 4140 | |
| 4141 | private boolean isVisible() { |
| 4142 | // Always show a dragging handle. |
| 4143 | if (mIsDragging) { |
| 4144 | return true; |
| 4145 | } |
| 4146 | |
| 4147 | if (mTextView.isInBatchEditMode()) { |
| 4148 | return false; |
| 4149 | } |
| 4150 | |
Keisuke Kuroyanagi | 6cda1e2 | 2015-04-22 21:41:14 +0900 | [diff] [blame] | 4151 | return isPositionVisible(mPositionX + mHotspotX + getHorizontalOffset(), mPositionY); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | public abstract int getCurrentCursorOffset(); |
| 4155 | |
| 4156 | protected abstract void updateSelection(int offset); |
| 4157 | |
| 4158 | public abstract void updatePosition(float x, float y); |
| 4159 | |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4160 | protected boolean isAtRtlRun(@NonNull Layout layout, int offset) { |
| 4161 | return layout.isRtlCharAt(offset); |
| 4162 | } |
| 4163 | |
| 4164 | @VisibleForTesting |
| 4165 | public float getHorizontal(@NonNull Layout layout, int offset) { |
| 4166 | return layout.getPrimaryHorizontal(offset); |
| 4167 | } |
| 4168 | |
| 4169 | protected int getOffsetAtCoordinate(@NonNull Layout layout, int line, float x) { |
| 4170 | return mTextView.getOffsetAtCoordinate(line, x); |
| 4171 | } |
| 4172 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4173 | protected void positionAtCursorOffset(int offset, boolean parentScrolled) { |
| 4174 | // A HandleView relies on the layout, which may be nulled by external methods |
| 4175 | Layout layout = mTextView.getLayout(); |
| 4176 | if (layout == null) { |
| 4177 | // Will update controllers' state, hiding them and stopping selection mode if needed |
| 4178 | prepareCursorControllers(); |
| 4179 | return; |
| 4180 | } |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 4181 | layout = mTextView.getLayout(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4182 | |
| 4183 | boolean offsetChanged = offset != mPreviousOffset; |
| 4184 | if (offsetChanged || parentScrolled) { |
| 4185 | if (offsetChanged) { |
| 4186 | updateSelection(offset); |
| 4187 | addPositionToTouchUpFilter(offset); |
| 4188 | } |
| 4189 | final int line = layout.getLineForOffset(offset); |
Mady Mellor | b9bbbb1 | 2015-03-23 11:50:46 -0700 | [diff] [blame] | 4190 | mPrevLine = line; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4191 | |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 4192 | mPositionX = getCursorHorizontalPosition(layout, offset) - mHotspotX - |
| 4193 | getHorizontalOffset() + getCursorOffset(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4194 | mPositionY = layout.getLineBottom(line); |
| 4195 | |
| 4196 | // Take TextView's padding and scroll into account. |
| 4197 | mPositionX += mTextView.viewportToContentHorizontalOffset(); |
| 4198 | mPositionY += mTextView.viewportToContentVerticalOffset(); |
| 4199 | |
| 4200 | mPreviousOffset = offset; |
| 4201 | mPositionHasChanged = true; |
| 4202 | } |
| 4203 | } |
| 4204 | |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 4205 | /** |
| 4206 | * Return the clamped horizontal position for the first cursor. |
| 4207 | * |
| 4208 | * @param layout Text layout. |
| 4209 | * @param offset Character offset for the cursor. |
| 4210 | * @return The clamped horizontal position for the cursor. |
| 4211 | */ |
| 4212 | int getCursorHorizontalPosition(Layout layout, int offset) { |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4213 | return (int) (getHorizontal(layout, offset) - 0.5f); |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 4214 | } |
| 4215 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4216 | public void updatePosition(int parentPositionX, int parentPositionY, |
| 4217 | boolean parentPositionChanged, boolean parentScrolled) { |
| 4218 | positionAtCursorOffset(getCurrentCursorOffset(), parentScrolled); |
| 4219 | if (parentPositionChanged || mPositionHasChanged) { |
| 4220 | if (mIsDragging) { |
| 4221 | // Update touchToWindow offset in case of parent scrolling while dragging |
| 4222 | if (parentPositionX != mLastParentX || parentPositionY != mLastParentY) { |
| 4223 | mTouchToWindowOffsetX += parentPositionX - mLastParentX; |
| 4224 | mTouchToWindowOffsetY += parentPositionY - mLastParentY; |
| 4225 | mLastParentX = parentPositionX; |
| 4226 | mLastParentY = parentPositionY; |
| 4227 | } |
| 4228 | |
| 4229 | onHandleMoved(); |
| 4230 | } |
| 4231 | |
| 4232 | if (isVisible()) { |
Seigo Nonaka | 2f229ca | 2016-02-18 17:53:54 +0900 | [diff] [blame] | 4233 | // Transform to the window coordinates to follow the view tranformation. |
| 4234 | final int[] pts = { mPositionX + mHotspotX + getHorizontalOffset(), mPositionY}; |
| 4235 | mTextView.transformFromViewToWindowSpace(pts); |
| 4236 | pts[0] -= mHotspotX + getHorizontalOffset(); |
| 4237 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4238 | if (isShowing()) { |
Seigo Nonaka | 2f229ca | 2016-02-18 17:53:54 +0900 | [diff] [blame] | 4239 | mContainer.update(pts[0], pts[1], -1, -1); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4240 | } else { |
Seigo Nonaka | 2f229ca | 2016-02-18 17:53:54 +0900 | [diff] [blame] | 4241 | mContainer.showAtLocation(mTextView, Gravity.NO_GRAVITY, pts[0], pts[1]); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4242 | } |
| 4243 | } else { |
| 4244 | if (isShowing()) { |
| 4245 | dismiss(); |
| 4246 | } |
| 4247 | } |
| 4248 | |
| 4249 | mPositionHasChanged = false; |
| 4250 | } |
| 4251 | } |
| 4252 | |
| 4253 | @Override |
| 4254 | protected void onDraw(Canvas c) { |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4255 | final int drawWidth = mDrawable.getIntrinsicWidth(); |
| 4256 | final int left = getHorizontalOffset(); |
| 4257 | |
| 4258 | mDrawable.setBounds(left, 0, left + drawWidth, mDrawable.getIntrinsicHeight()); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4259 | mDrawable.draw(c); |
| 4260 | } |
| 4261 | |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4262 | private int getHorizontalOffset() { |
| 4263 | final int width = getPreferredWidth(); |
| 4264 | final int drawWidth = mDrawable.getIntrinsicWidth(); |
| 4265 | final int left; |
| 4266 | switch (mHorizontalGravity) { |
| 4267 | case Gravity.LEFT: |
| 4268 | left = 0; |
| 4269 | break; |
| 4270 | default: |
| 4271 | case Gravity.CENTER: |
| 4272 | left = (width - drawWidth) / 2; |
| 4273 | break; |
| 4274 | case Gravity.RIGHT: |
| 4275 | left = width - drawWidth; |
| 4276 | break; |
| 4277 | } |
| 4278 | return left; |
| 4279 | } |
| 4280 | |
| 4281 | protected int getCursorOffset() { |
| 4282 | return 0; |
| 4283 | } |
| 4284 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4285 | @Override |
| 4286 | public boolean onTouchEvent(MotionEvent ev) { |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 4287 | updateFloatingToolbarVisibility(ev); |
| 4288 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4289 | switch (ev.getActionMasked()) { |
| 4290 | case MotionEvent.ACTION_DOWN: { |
| 4291 | startTouchUpFilter(getCurrentCursorOffset()); |
| 4292 | mTouchToWindowOffsetX = ev.getRawX() - mPositionX; |
| 4293 | mTouchToWindowOffsetY = ev.getRawY() - mPositionY; |
| 4294 | |
| 4295 | final PositionListener positionListener = getPositionListener(); |
| 4296 | mLastParentX = positionListener.getPositionX(); |
| 4297 | mLastParentY = positionListener.getPositionY(); |
| 4298 | mIsDragging = true; |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 4299 | mPreviousLineTouched = UNSET_LINE; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4300 | break; |
| 4301 | } |
| 4302 | |
| 4303 | case MotionEvent.ACTION_MOVE: { |
| 4304 | final float rawX = ev.getRawX(); |
| 4305 | final float rawY = ev.getRawY(); |
| 4306 | |
| 4307 | // Vertical hysteresis: vertical down movement tends to snap to ideal offset |
| 4308 | final float previousVerticalOffset = mTouchToWindowOffsetY - mLastParentY; |
| 4309 | final float currentVerticalOffset = rawY - mPositionY - mLastParentY; |
| 4310 | float newVerticalOffset; |
| 4311 | if (previousVerticalOffset < mIdealVerticalOffset) { |
| 4312 | newVerticalOffset = Math.min(currentVerticalOffset, mIdealVerticalOffset); |
| 4313 | newVerticalOffset = Math.max(newVerticalOffset, previousVerticalOffset); |
| 4314 | } else { |
| 4315 | newVerticalOffset = Math.max(currentVerticalOffset, mIdealVerticalOffset); |
| 4316 | newVerticalOffset = Math.min(newVerticalOffset, previousVerticalOffset); |
| 4317 | } |
| 4318 | mTouchToWindowOffsetY = newVerticalOffset + mLastParentY; |
| 4319 | |
Keisuke Kuroyanagi | bc89a5c | 2015-05-18 14:49:29 +0900 | [diff] [blame] | 4320 | final float newPosX = |
| 4321 | rawX - mTouchToWindowOffsetX + mHotspotX + getHorizontalOffset(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4322 | final float newPosY = rawY - mTouchToWindowOffsetY + mTouchOffsetY; |
| 4323 | |
| 4324 | updatePosition(newPosX, newPosY); |
| 4325 | break; |
| 4326 | } |
| 4327 | |
| 4328 | case MotionEvent.ACTION_UP: |
| 4329 | filterOnTouchUp(); |
| 4330 | mIsDragging = false; |
Keisuke Kuroyanagi | dbe2c29 | 2015-05-27 19:49:34 +0900 | [diff] [blame] | 4331 | updateDrawable(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4332 | break; |
| 4333 | |
| 4334 | case MotionEvent.ACTION_CANCEL: |
| 4335 | mIsDragging = false; |
Keisuke Kuroyanagi | dbe2c29 | 2015-05-27 19:49:34 +0900 | [diff] [blame] | 4336 | updateDrawable(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4337 | break; |
| 4338 | } |
| 4339 | return true; |
| 4340 | } |
| 4341 | |
| 4342 | public boolean isDragging() { |
| 4343 | return mIsDragging; |
| 4344 | } |
| 4345 | |
Clara Bayarri | 6351e66 | 2015-03-16 23:17:59 +0000 | [diff] [blame] | 4346 | void onHandleMoved() {} |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4347 | |
Clara Bayarri | 6351e66 | 2015-03-16 23:17:59 +0000 | [diff] [blame] | 4348 | public void onDetached() {} |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4349 | } |
| 4350 | |
| 4351 | private class InsertionHandleView extends HandleView { |
| 4352 | private static final int DELAY_BEFORE_HANDLE_FADES_OUT = 4000; |
| 4353 | private static final int RECENT_CUT_COPY_DURATION = 15 * 1000; // seconds |
| 4354 | |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 4355 | // Used to detect taps on the insertion handle, which will affect the insertion action mode |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4356 | private float mDownPositionX, mDownPositionY; |
| 4357 | private Runnable mHider; |
| 4358 | |
| 4359 | public InsertionHandleView(Drawable drawable) { |
Keisuke Kuroyanagi | da79ee6 | 2015-11-25 16:15:15 +0900 | [diff] [blame] | 4360 | super(drawable, drawable, com.android.internal.R.id.insertion_handle); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4361 | } |
| 4362 | |
| 4363 | @Override |
| 4364 | public void show() { |
| 4365 | super.show(); |
| 4366 | |
Andrei Stingaceanu | fae270c | 2015-04-29 14:39:40 +0100 | [diff] [blame] | 4367 | final long durationSinceCutOrCopy = |
Andrei Stingaceanu | 77b9c38 | 2015-05-06 13:25:19 +0100 | [diff] [blame] | 4368 | SystemClock.uptimeMillis() - TextView.sLastCutCopyOrTextChangedTime; |
Andrei Stingaceanu | fae270c | 2015-04-29 14:39:40 +0100 | [diff] [blame] | 4369 | |
| 4370 | // Cancel the single tap delayed runnable. |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 4371 | if (mInsertionActionModeRunnable != null |
Keisuke Kuroyanagi | 155aecb | 2015-11-05 19:10:07 +0900 | [diff] [blame] | 4372 | && ((mTapState == TAP_STATE_DOUBLE_TAP) |
| 4373 | || (mTapState == TAP_STATE_TRIPLE_CLICK) |
| 4374 | || isCursorInsideEasyCorrectionSpan())) { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 4375 | mTextView.removeCallbacks(mInsertionActionModeRunnable); |
Andrei Stingaceanu | fae270c | 2015-04-29 14:39:40 +0100 | [diff] [blame] | 4376 | } |
| 4377 | |
| 4378 | // Prepare and schedule the single tap runnable to run exactly after the double tap |
| 4379 | // timeout has passed. |
Keisuke Kuroyanagi | 155aecb | 2015-11-05 19:10:07 +0900 | [diff] [blame] | 4380 | if ((mTapState != TAP_STATE_DOUBLE_TAP) && (mTapState != TAP_STATE_TRIPLE_CLICK) |
| 4381 | && !isCursorInsideEasyCorrectionSpan() |
Andrei Stingaceanu | 373816e | 2015-05-28 11:26:28 +0100 | [diff] [blame] | 4382 | && (durationSinceCutOrCopy < RECENT_CUT_COPY_DURATION)) { |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 4383 | if (mTextActionMode == null) { |
| 4384 | if (mInsertionActionModeRunnable == null) { |
| 4385 | mInsertionActionModeRunnable = new Runnable() { |
| 4386 | @Override |
| 4387 | public void run() { |
| 4388 | startInsertionActionMode(); |
| 4389 | } |
| 4390 | }; |
| 4391 | } |
| 4392 | mTextView.postDelayed( |
| 4393 | mInsertionActionModeRunnable, |
| 4394 | ViewConfiguration.getDoubleTapTimeout() + 1); |
Andrei Stingaceanu | fae270c | 2015-04-29 14:39:40 +0100 | [diff] [blame] | 4395 | } |
| 4396 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4397 | } |
| 4398 | |
| 4399 | hideAfterDelay(); |
| 4400 | } |
| 4401 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4402 | private void hideAfterDelay() { |
| 4403 | if (mHider == null) { |
| 4404 | mHider = new Runnable() { |
| 4405 | public void run() { |
| 4406 | hide(); |
| 4407 | } |
| 4408 | }; |
| 4409 | } else { |
| 4410 | removeHiderCallback(); |
| 4411 | } |
| 4412 | mTextView.postDelayed(mHider, DELAY_BEFORE_HANDLE_FADES_OUT); |
| 4413 | } |
| 4414 | |
| 4415 | private void removeHiderCallback() { |
| 4416 | if (mHider != null) { |
| 4417 | mTextView.removeCallbacks(mHider); |
| 4418 | } |
| 4419 | } |
| 4420 | |
| 4421 | @Override |
| 4422 | protected int getHotspotX(Drawable drawable, boolean isRtlRun) { |
| 4423 | return drawable.getIntrinsicWidth() / 2; |
| 4424 | } |
| 4425 | |
| 4426 | @Override |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4427 | protected int getHorizontalGravity(boolean isRtlRun) { |
| 4428 | return Gravity.CENTER_HORIZONTAL; |
| 4429 | } |
| 4430 | |
| 4431 | @Override |
| 4432 | protected int getCursorOffset() { |
| 4433 | int offset = super.getCursorOffset(); |
| 4434 | final Drawable cursor = mCursorCount > 0 ? mCursorDrawable[0] : null; |
| 4435 | if (cursor != null) { |
| 4436 | cursor.getPadding(mTempRect); |
| 4437 | offset += (cursor.getIntrinsicWidth() - mTempRect.left - mTempRect.right) / 2; |
| 4438 | } |
| 4439 | return offset; |
| 4440 | } |
| 4441 | |
| 4442 | @Override |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 4443 | int getCursorHorizontalPosition(Layout layout, int offset) { |
| 4444 | final Drawable drawable = mCursorCount > 0 ? mCursorDrawable[0] : null; |
| 4445 | if (drawable != null) { |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4446 | final float horizontal = getHorizontal(layout, offset); |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 4447 | return clampHorizontalPosition(drawable, horizontal) + mTempRect.left; |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 4448 | } |
| 4449 | return super.getCursorHorizontalPosition(layout, offset); |
| 4450 | } |
| 4451 | |
| 4452 | @Override |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4453 | public boolean onTouchEvent(MotionEvent ev) { |
| 4454 | final boolean result = super.onTouchEvent(ev); |
| 4455 | |
| 4456 | switch (ev.getActionMasked()) { |
| 4457 | case MotionEvent.ACTION_DOWN: |
| 4458 | mDownPositionX = ev.getRawX(); |
| 4459 | mDownPositionY = ev.getRawY(); |
| 4460 | break; |
| 4461 | |
| 4462 | case MotionEvent.ACTION_UP: |
| 4463 | if (!offsetHasBeenChanged()) { |
| 4464 | final float deltaX = mDownPositionX - ev.getRawX(); |
| 4465 | final float deltaY = mDownPositionY - ev.getRawY(); |
| 4466 | final float distanceSquared = deltaX * deltaX + deltaY * deltaY; |
| 4467 | |
| 4468 | final ViewConfiguration viewConfiguration = ViewConfiguration.get( |
| 4469 | mTextView.getContext()); |
| 4470 | final int touchSlop = viewConfiguration.getScaledTouchSlop(); |
| 4471 | |
| 4472 | if (distanceSquared < touchSlop * touchSlop) { |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 4473 | // Tapping on the handle toggles the insertion action mode. |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 4474 | if (mTextActionMode != null) { |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 4475 | stopTextActionMode(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4476 | } else { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 4477 | startInsertionActionMode(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4478 | } |
| 4479 | } |
Abodunrinwa Toki | bcdf0ab | 2015-04-25 00:11:25 +0100 | [diff] [blame] | 4480 | } else { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 4481 | if (mTextActionMode != null) { |
| 4482 | mTextActionMode.invalidateContentRect(); |
Abodunrinwa Toki | bcdf0ab | 2015-04-25 00:11:25 +0100 | [diff] [blame] | 4483 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4484 | } |
| 4485 | hideAfterDelay(); |
| 4486 | break; |
| 4487 | |
| 4488 | case MotionEvent.ACTION_CANCEL: |
| 4489 | hideAfterDelay(); |
| 4490 | break; |
| 4491 | |
| 4492 | default: |
| 4493 | break; |
| 4494 | } |
| 4495 | |
| 4496 | return result; |
| 4497 | } |
| 4498 | |
| 4499 | @Override |
| 4500 | public int getCurrentCursorOffset() { |
| 4501 | return mTextView.getSelectionStart(); |
| 4502 | } |
| 4503 | |
| 4504 | @Override |
| 4505 | public void updateSelection(int offset) { |
| 4506 | Selection.setSelection((Spannable) mTextView.getText(), offset); |
| 4507 | } |
| 4508 | |
| 4509 | @Override |
| 4510 | public void updatePosition(float x, float y) { |
Mady Mellor | ee3821e | 2015-06-05 11:12:01 -0700 | [diff] [blame] | 4511 | Layout layout = mTextView.getLayout(); |
| 4512 | int offset; |
| 4513 | if (layout != null) { |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 4514 | if (mPreviousLineTouched == UNSET_LINE) { |
| 4515 | mPreviousLineTouched = mTextView.getLineAtCoordinate(y); |
| 4516 | } |
| 4517 | int currLine = getCurrentLineAdjustedForSlop(layout, mPreviousLineTouched, y); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4518 | offset = getOffsetAtCoordinate(layout, currLine, x); |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 4519 | mPreviousLineTouched = currLine; |
Mady Mellor | ee3821e | 2015-06-05 11:12:01 -0700 | [diff] [blame] | 4520 | } else { |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4521 | offset = -1; |
Mady Mellor | ee3821e | 2015-06-05 11:12:01 -0700 | [diff] [blame] | 4522 | } |
| 4523 | positionAtCursorOffset(offset, false); |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 4524 | if (mTextActionMode != null) { |
| 4525 | mTextActionMode.invalidate(); |
Clara Bayarri | 1baed51 | 2015-05-11 15:29:16 +0100 | [diff] [blame] | 4526 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4527 | } |
| 4528 | |
| 4529 | @Override |
| 4530 | void onHandleMoved() { |
| 4531 | super.onHandleMoved(); |
| 4532 | removeHiderCallback(); |
| 4533 | } |
| 4534 | |
| 4535 | @Override |
| 4536 | public void onDetached() { |
| 4537 | super.onDetached(); |
| 4538 | removeHiderCallback(); |
| 4539 | } |
| 4540 | } |
| 4541 | |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4542 | @Retention(RetentionPolicy.SOURCE) |
| 4543 | @IntDef({HANDLE_TYPE_SELECTION_START, HANDLE_TYPE_SELECTION_END}) |
| 4544 | public @interface HandleType {} |
| 4545 | public static final int HANDLE_TYPE_SELECTION_START = 0; |
| 4546 | public static final int HANDLE_TYPE_SELECTION_END = 1; |
| 4547 | |
| 4548 | private class SelectionHandleView extends HandleView { |
| 4549 | // Indicates the handle type, selection start (HANDLE_TYPE_SELECTION_START) or selection |
| 4550 | // end (HANDLE_TYPE_SELECTION_END). |
| 4551 | @HandleType |
| 4552 | private final int mHandleType; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 4553 | // Indicates whether the cursor is making adjustments within a word. |
| 4554 | private boolean mInWord = false; |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 4555 | // Difference between touch position and word boundary position. |
| 4556 | private float mTouchWordDelta; |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 4557 | // X value of the previous updatePosition call. |
| 4558 | private float mPrevX; |
| 4559 | // Indicates if the handle has moved a boundary between LTR and RTL text. |
| 4560 | private boolean mLanguageDirectionChanged = false; |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 4561 | // Distance from edge of horizontally scrolling text view |
| 4562 | // to use to switch to character mode. |
| 4563 | private final float mTextViewEdgeSlop; |
| 4564 | // Used to save text view location. |
| 4565 | private final int[] mTextViewLocation = new int[2]; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4566 | |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4567 | public SelectionHandleView(Drawable drawableLtr, Drawable drawableRtl, int id, |
| 4568 | @HandleType int handleType) { |
| 4569 | super(drawableLtr, drawableRtl, id); |
| 4570 | mHandleType = handleType; |
| 4571 | ViewConfiguration viewConfiguration = ViewConfiguration.get(mTextView.getContext()); |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 4572 | mTextViewEdgeSlop = viewConfiguration.getScaledTouchSlop() * 4; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4573 | } |
| 4574 | |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4575 | private boolean isStartHandle() { |
| 4576 | return mHandleType == HANDLE_TYPE_SELECTION_START; |
| 4577 | } |
| 4578 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4579 | @Override |
| 4580 | protected int getHotspotX(Drawable drawable, boolean isRtlRun) { |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4581 | if (isRtlRun == isStartHandle()) { |
Mady Mellor | 709386f | 2015-05-14 12:41:18 -0700 | [diff] [blame] | 4582 | return drawable.getIntrinsicWidth() / 4; |
| 4583 | } else { |
| 4584 | return (drawable.getIntrinsicWidth() * 3) / 4; |
| 4585 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4586 | } |
| 4587 | |
| 4588 | @Override |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4589 | protected int getHorizontalGravity(boolean isRtlRun) { |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4590 | return (isRtlRun == isStartHandle()) ? Gravity.LEFT : Gravity.RIGHT; |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4591 | } |
| 4592 | |
| 4593 | @Override |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4594 | public int getCurrentCursorOffset() { |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4595 | return isStartHandle() ? mTextView.getSelectionStart() : mTextView.getSelectionEnd(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4596 | } |
| 4597 | |
| 4598 | @Override |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4599 | protected void updateSelection(int offset) { |
| 4600 | if (isStartHandle()) { |
| 4601 | Selection.setSelection((Spannable) mTextView.getText(), offset, |
| 4602 | mTextView.getSelectionEnd()); |
| 4603 | } else { |
| 4604 | Selection.setSelection((Spannable) mTextView.getText(), |
| 4605 | mTextView.getSelectionStart(), offset); |
| 4606 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4607 | updateDrawable(); |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 4608 | if (mTextActionMode != null) { |
| 4609 | mTextActionMode.invalidate(); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 4610 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4611 | } |
| 4612 | |
| 4613 | @Override |
| 4614 | public void updatePosition(float x, float y) { |
Mady Mellor | 81fa3e8 | 2015-05-14 09:17:41 -0700 | [diff] [blame] | 4615 | final Layout layout = mTextView.getLayout(); |
Mady Mellor | cc65c37 | 2015-06-17 09:25:19 -0700 | [diff] [blame] | 4616 | if (layout == null) { |
| 4617 | // HandleView will deal appropriately in positionAtCursorOffset when |
| 4618 | // layout is null. |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 4619 | positionAndAdjustForCrossingHandles(mTextView.getOffsetForPosition(x, y)); |
Mady Mellor | cc65c37 | 2015-06-17 09:25:19 -0700 | [diff] [blame] | 4620 | return; |
| 4621 | } |
| 4622 | |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 4623 | if (mPreviousLineTouched == UNSET_LINE) { |
| 4624 | mPreviousLineTouched = mTextView.getLineAtCoordinate(y); |
| 4625 | } |
| 4626 | |
Mady Mellor | b9bbbb1 | 2015-03-23 11:50:46 -0700 | [diff] [blame] | 4627 | boolean positionCursor = false; |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4628 | final int anotherHandleOffset = |
| 4629 | isStartHandle() ? mTextView.getSelectionEnd() : mTextView.getSelectionStart(); |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 4630 | int currLine = getCurrentLineAdjustedForSlop(layout, mPreviousLineTouched, y); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4631 | int initialOffset = getOffsetAtCoordinate(layout, currLine, x); |
Mady Mellor | 81fa3e8 | 2015-05-14 09:17:41 -0700 | [diff] [blame] | 4632 | |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4633 | if (isStartHandle() && initialOffset >= anotherHandleOffset |
| 4634 | || !isStartHandle() && initialOffset <= anotherHandleOffset) { |
| 4635 | // Handles have crossed, bound it to the first selected line and |
Mady Mellor | 81fa3e8 | 2015-05-14 09:17:41 -0700 | [diff] [blame] | 4636 | // adjust by word / char as normal. |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4637 | currLine = layout.getLineForOffset(anotherHandleOffset); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4638 | initialOffset = getOffsetAtCoordinate(layout, currLine, x); |
Mady Mellor | 81fa3e8 | 2015-05-14 09:17:41 -0700 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | int offset = initialOffset; |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4642 | final int wordEnd = getWordEnd(offset); |
| 4643 | final int wordStart = getWordStart(offset); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4644 | |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 4645 | if (mPrevX == UNSET_X_VALUE) { |
| 4646 | mPrevX = x; |
| 4647 | } |
| 4648 | |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4649 | final int currentOffset = getCurrentCursorOffset(); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4650 | final boolean rtlAtCurrentOffset = isAtRtlRun(layout, currentOffset); |
| 4651 | final boolean atRtl = isAtRtlRun(layout, offset); |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 4652 | final boolean isLvlBoundary = layout.isLevelBoundary(offset); |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 4653 | |
| 4654 | // We can't determine if the user is expanding or shrinking the selection if they're |
| 4655 | // on a bi-di boundary, so until they've moved past the boundary we'll just place |
| 4656 | // the cursor at the current position. |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4657 | if (isLvlBoundary || (rtlAtCurrentOffset && !atRtl) || (!rtlAtCurrentOffset && atRtl)) { |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 4658 | // We're on a boundary or this is the first direction change -- just update |
| 4659 | // to the current position. |
| 4660 | mLanguageDirectionChanged = true; |
| 4661 | mTouchWordDelta = 0.0f; |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 4662 | positionAndAdjustForCrossingHandles(offset); |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 4663 | return; |
| 4664 | } else if (mLanguageDirectionChanged && !isLvlBoundary) { |
| 4665 | // We've just moved past the boundary so update the position. After this we can |
| 4666 | // figure out if the user is expanding or shrinking to go by word or character. |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 4667 | positionAndAdjustForCrossingHandles(offset); |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 4668 | mTouchWordDelta = 0.0f; |
| 4669 | mLanguageDirectionChanged = false; |
| 4670 | return; |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4671 | } |
| 4672 | |
| 4673 | boolean isExpanding; |
| 4674 | final float xDiff = x - mPrevX; |
Keisuke Kuroyanagi | 2645414 | 2015-12-02 15:04:57 -0800 | [diff] [blame] | 4675 | if (isStartHandle()) { |
| 4676 | isExpanding = currLine < mPreviousLineTouched; |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 4677 | } else { |
Keisuke Kuroyanagi | 2645414 | 2015-12-02 15:04:57 -0800 | [diff] [blame] | 4678 | isExpanding = currLine > mPreviousLineTouched; |
| 4679 | } |
| 4680 | if (atRtl == isStartHandle()) { |
| 4681 | isExpanding |= xDiff > 0; |
| 4682 | } else { |
| 4683 | isExpanding |= xDiff < 0; |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 4684 | } |
| 4685 | |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 4686 | if (mTextView.getHorizontallyScrolling()) { |
| 4687 | if (positionNearEdgeOfScrollingView(x, atRtl) |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4688 | && ((isStartHandle() && mTextView.getScrollX() != 0) |
| 4689 | || (!isStartHandle() |
| 4690 | && mTextView.canScrollHorizontally(atRtl ? -1 : 1))) |
| 4691 | && ((isExpanding && ((isStartHandle() && offset < currentOffset) |
| 4692 | || (!isStartHandle() && offset > currentOffset))) |
| 4693 | || !isExpanding)) { |
| 4694 | // If we're expanding ensure that the offset is actually expanding compared to |
| 4695 | // the current offset, if the handle snapped to the word, the finger position |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 4696 | // may be out of sync and we don't want the selection to jump back. |
| 4697 | mTouchWordDelta = 0.0f; |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4698 | final int nextOffset = (atRtl == isStartHandle()) |
| 4699 | ? layout.getOffsetToRightOf(mPreviousOffset) |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 4700 | : layout.getOffsetToLeftOf(mPreviousOffset); |
| 4701 | positionAndAdjustForCrossingHandles(nextOffset); |
| 4702 | return; |
| 4703 | } |
| 4704 | } |
| 4705 | |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 4706 | if (isExpanding) { |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 4707 | // User is increasing the selection. |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4708 | int wordBoundary = isStartHandle() ? wordStart : wordEnd; |
| 4709 | final boolean snapToWord = (!mInWord |
| 4710 | || (isStartHandle() ? currLine < mPrevLine : currLine > mPrevLine)) |
| 4711 | && atRtl == isAtRtlRun(layout, wordBoundary); |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4712 | if (snapToWord) { |
Mady Mellor | a526683 | 2015-06-26 14:28:12 -0700 | [diff] [blame] | 4713 | // Sometimes words can be broken across lines (Chinese, hyphenation). |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4714 | // We still snap to the word boundary but we only use the letters on the |
Mady Mellor | a526683 | 2015-06-26 14:28:12 -0700 | [diff] [blame] | 4715 | // current line to determine if the user is far enough into the word to snap. |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4716 | if (layout.getLineForOffset(wordBoundary) != currLine) { |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4717 | wordBoundary = isStartHandle() ? |
| 4718 | layout.getLineStart(currLine) : layout.getLineEnd(currLine); |
Mady Mellor | a526683 | 2015-06-26 14:28:12 -0700 | [diff] [blame] | 4719 | } |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4720 | final int offsetThresholdToSnap = isStartHandle() |
| 4721 | ? wordEnd - ((wordEnd - wordBoundary) / 2) |
| 4722 | : wordStart + ((wordBoundary - wordStart) / 2); |
| 4723 | if (isStartHandle() |
| 4724 | && (offset <= offsetThresholdToSnap || currLine < mPrevLine)) { |
| 4725 | // User is far enough into the word or on a different line so we expand by |
| 4726 | // word. |
| 4727 | offset = wordStart; |
| 4728 | } else if (!isStartHandle() |
| 4729 | && (offset >= offsetThresholdToSnap || currLine > mPrevLine)) { |
| 4730 | // User is far enough into the word or on a different line so we expand by |
| 4731 | // word. |
| 4732 | offset = wordEnd; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 4733 | } else { |
Mady Mellor | c2225b9 | 2015-04-01 15:59:20 -0700 | [diff] [blame] | 4734 | offset = mPreviousOffset; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 4735 | } |
| 4736 | } |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4737 | if ((isStartHandle() && offset < initialOffset) |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4738 | || (!isStartHandle() && offset > initialOffset)) { |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4739 | final float adjustedX = getHorizontal(layout, offset); |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 4740 | mTouchWordDelta = |
| 4741 | mTextView.convertToLocalHorizontalCoordinate(x) - adjustedX; |
Keisuke Kuroyanagi | 50a927c | 2015-05-07 17:34:21 +0900 | [diff] [blame] | 4742 | } else { |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 4743 | mTouchWordDelta = 0.0f; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 4744 | } |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 4745 | positionCursor = true; |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 4746 | } else { |
| 4747 | final int adjustedOffset = |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4748 | getOffsetAtCoordinate(layout, currLine, x - mTouchWordDelta); |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4749 | final boolean shrinking = isStartHandle() |
| 4750 | ? adjustedOffset > mPreviousOffset || currLine > mPrevLine |
| 4751 | : adjustedOffset < mPreviousOffset || currLine < mPrevLine; |
| 4752 | if (shrinking) { |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 4753 | // User is shrinking the selection. |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4754 | if (currLine != mPrevLine) { |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 4755 | // We're on a different line, so we'll snap to word boundaries. |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4756 | offset = isStartHandle() ? wordStart : wordEnd; |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4757 | if ((isStartHandle() && offset < initialOffset) |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4758 | || (!isStartHandle() && offset > initialOffset)) { |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4759 | final float adjustedX = getHorizontal(layout, offset); |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 4760 | mTouchWordDelta = |
| 4761 | mTextView.convertToLocalHorizontalCoordinate(x) - adjustedX; |
| 4762 | } else { |
| 4763 | mTouchWordDelta = 0.0f; |
| 4764 | } |
| 4765 | } else { |
| 4766 | offset = adjustedOffset; |
| 4767 | } |
| 4768 | positionCursor = true; |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4769 | } else if ((isStartHandle() && adjustedOffset < mPreviousOffset) |
| 4770 | || (!isStartHandle() && adjustedOffset > mPreviousOffset)) { |
| 4771 | // Handle has jumped to the word boundary, and the user is moving |
Mady Mellor | 43fd2f4 | 2015-06-08 14:03:34 -0700 | [diff] [blame] | 4772 | // their finger towards the handle, the delta should be updated. |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4773 | mTouchWordDelta = mTextView.convertToLocalHorizontalCoordinate(x) - |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4774 | getHorizontal(layout, mPreviousOffset); |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 4775 | } |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 4776 | } |
| 4777 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 4778 | if (positionCursor) { |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 4779 | mPreviousLineTouched = currLine; |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 4780 | positionAndAdjustForCrossingHandles(offset); |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 4781 | } |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 4782 | mPrevX = x; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4783 | } |
| 4784 | |
Yoshiki Iguchi | 9582e15 | 2015-10-15 13:34:41 +0900 | [diff] [blame] | 4785 | /** |
| 4786 | * @param offset Cursor offset. Must be in [-1, length]. |
| 4787 | * @param parentScrolled If the parent has been scrolled or not. |
| 4788 | */ |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 4789 | @Override |
Mady Mellor | 36d5a7b | 2015-05-22 10:31:12 -0700 | [diff] [blame] | 4790 | protected void positionAtCursorOffset(int offset, boolean parentScrolled) { |
| 4791 | super.positionAtCursorOffset(offset, parentScrolled); |
Yoshiki Iguchi | 9582e15 | 2015-10-15 13:34:41 +0900 | [diff] [blame] | 4792 | mInWord = (offset != -1) && !getWordIteratorWithText().isBoundary(offset); |
Mady Mellor | 36d5a7b | 2015-05-22 10:31:12 -0700 | [diff] [blame] | 4793 | } |
| 4794 | |
| 4795 | @Override |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 4796 | public boolean onTouchEvent(MotionEvent event) { |
| 4797 | boolean superResult = super.onTouchEvent(event); |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 4798 | if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { |
| 4799 | // Reset the touch word offset and x value when the user |
| 4800 | // re-engages the handle. |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 4801 | mTouchWordDelta = 0.0f; |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 4802 | mPrevX = UNSET_X_VALUE; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 4803 | } |
| 4804 | return superResult; |
| 4805 | } |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 4806 | |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 4807 | private void positionAndAdjustForCrossingHandles(int offset) { |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4808 | final int anotherHandleOffset = |
| 4809 | isStartHandle() ? mTextView.getSelectionEnd() : mTextView.getSelectionStart(); |
| 4810 | if ((isStartHandle() && offset >= anotherHandleOffset) |
| 4811 | || (!isStartHandle() && offset <= anotherHandleOffset)) { |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4812 | mTouchWordDelta = 0.0f; |
| 4813 | final Layout layout = mTextView.getLayout(); |
| 4814 | if (layout != null && offset != anotherHandleOffset) { |
| 4815 | final float horiz = getHorizontal(layout, offset); |
| 4816 | final float anotherHandleHoriz = getHorizontal(layout, anotherHandleOffset, |
| 4817 | !isStartHandle()); |
| 4818 | final float currentHoriz = getHorizontal(layout, mPreviousOffset); |
| 4819 | if (currentHoriz < anotherHandleHoriz && horiz < anotherHandleHoriz |
| 4820 | || currentHoriz > anotherHandleHoriz && horiz > anotherHandleHoriz) { |
| 4821 | // This handle passes another one as it crossed a direction boundary. |
| 4822 | // Don't minimize the selection, but keep the handle at the run boundary. |
| 4823 | final int currentOffset = getCurrentCursorOffset(); |
| 4824 | final int offsetToGetRunRange = isStartHandle() ? |
| 4825 | currentOffset : Math.max(currentOffset - 1, 0); |
| 4826 | final long range = layout.getRunRange(offsetToGetRunRange); |
| 4827 | if (isStartHandle()) { |
| 4828 | offset = TextUtils.unpackRangeStartFromLong(range); |
| 4829 | } else { |
| 4830 | offset = TextUtils.unpackRangeEndFromLong(range); |
| 4831 | } |
| 4832 | positionAtCursorOffset(offset, false); |
| 4833 | return; |
| 4834 | } |
| 4835 | } |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 4836 | // Handles can not cross and selection is at least one character. |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4837 | offset = getNextCursorOffset(anotherHandleOffset, !isStartHandle()); |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 4838 | } |
| 4839 | positionAtCursorOffset(offset, false); |
| 4840 | } |
| 4841 | |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 4842 | private boolean positionNearEdgeOfScrollingView(float x, boolean atRtl) { |
| 4843 | mTextView.getLocationOnScreen(mTextViewLocation); |
| 4844 | boolean nearEdge; |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4845 | if (atRtl == isStartHandle()) { |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 4846 | int rightEdge = mTextViewLocation[0] + mTextView.getWidth() |
| 4847 | - mTextView.getPaddingRight(); |
| 4848 | nearEdge = x > rightEdge - mTextViewEdgeSlop; |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 4849 | } else { |
| 4850 | int leftEdge = mTextViewLocation[0] + mTextView.getPaddingLeft(); |
| 4851 | nearEdge = x < leftEdge + mTextViewEdgeSlop; |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 4852 | } |
| 4853 | return nearEdge; |
| 4854 | } |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4855 | |
| 4856 | @Override |
| 4857 | protected boolean isAtRtlRun(@NonNull Layout layout, int offset) { |
| 4858 | final int offsetToCheck = isStartHandle() ? offset : Math.max(offset - 1, 0); |
| 4859 | return layout.isRtlCharAt(offsetToCheck); |
| 4860 | } |
| 4861 | |
| 4862 | @Override |
| 4863 | public float getHorizontal(@NonNull Layout layout, int offset) { |
| 4864 | return getHorizontal(layout, offset, isStartHandle()); |
| 4865 | } |
| 4866 | |
| 4867 | private float getHorizontal(@NonNull Layout layout, int offset, boolean startHandle) { |
| 4868 | final int line = layout.getLineForOffset(offset); |
| 4869 | final int offsetToCheck = startHandle ? offset : Math.max(offset - 1, 0); |
| 4870 | final boolean isRtlChar = layout.isRtlCharAt(offsetToCheck); |
| 4871 | final boolean isRtlParagraph = layout.getParagraphDirection(line) == -1; |
| 4872 | return (isRtlChar == isRtlParagraph) ? |
| 4873 | layout.getPrimaryHorizontal(offset) : layout.getSecondaryHorizontal(offset); |
| 4874 | } |
| 4875 | |
| 4876 | @Override |
| 4877 | protected int getOffsetAtCoordinate(@NonNull Layout layout, int line, float x) { |
| 4878 | final int primaryOffset = layout.getOffsetForHorizontal(line, x, true); |
| 4879 | if (!layout.isLevelBoundary(primaryOffset)) { |
| 4880 | return primaryOffset; |
| 4881 | } |
| 4882 | final int secondaryOffset = layout.getOffsetForHorizontal(line, x, false); |
| 4883 | final int currentOffset = getCurrentCursorOffset(); |
| 4884 | final int primaryDiff = Math.abs(primaryOffset - currentOffset); |
| 4885 | final int secondaryDiff = Math.abs(secondaryOffset - currentOffset); |
| 4886 | if (primaryDiff < secondaryDiff) { |
| 4887 | return primaryOffset; |
| 4888 | } else if (primaryDiff > secondaryDiff) { |
| 4889 | return secondaryOffset; |
| 4890 | } else { |
| 4891 | final int offsetToCheck = isStartHandle() ? |
| 4892 | currentOffset : Math.max(currentOffset - 1, 0); |
| 4893 | final boolean isRtlChar = layout.isRtlCharAt(offsetToCheck); |
| 4894 | final boolean isRtlParagraph = layout.getParagraphDirection(line) == -1; |
| 4895 | return isRtlChar == isRtlParagraph ? primaryOffset : secondaryOffset; |
| 4896 | } |
| 4897 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4898 | } |
| 4899 | |
Mady Mellor | cc65c37 | 2015-06-17 09:25:19 -0700 | [diff] [blame] | 4900 | private int getCurrentLineAdjustedForSlop(Layout layout, int prevLine, float y) { |
Mady Mellor | 8067907 | 2015-07-09 16:05:36 -0700 | [diff] [blame] | 4901 | final int trueLine = mTextView.getLineAtCoordinate(y); |
Mady Mellor | cc65c37 | 2015-06-17 09:25:19 -0700 | [diff] [blame] | 4902 | if (layout == null || prevLine > layout.getLineCount() |
| 4903 | || layout.getLineCount() <= 0 || prevLine < 0) { |
| 4904 | // Invalid parameters, just return whatever line is at y. |
Mady Mellor | 8067907 | 2015-07-09 16:05:36 -0700 | [diff] [blame] | 4905 | return trueLine; |
| 4906 | } |
| 4907 | |
| 4908 | if (Math.abs(trueLine - prevLine) >= 2) { |
| 4909 | // Only stick to lines if we're within a line of the previous selection. |
| 4910 | return trueLine; |
Mady Mellor | cc65c37 | 2015-06-17 09:25:19 -0700 | [diff] [blame] | 4911 | } |
| 4912 | |
| 4913 | final float verticalOffset = mTextView.viewportToContentVerticalOffset(); |
| 4914 | final int lineCount = layout.getLineCount(); |
| 4915 | final float slop = mTextView.getLineHeight() * LINE_SLOP_MULTIPLIER_FOR_HANDLEVIEWS; |
| 4916 | |
| 4917 | final float firstLineTop = layout.getLineTop(0) + verticalOffset; |
| 4918 | final float prevLineTop = layout.getLineTop(prevLine) + verticalOffset; |
| 4919 | final float yTopBound = Math.max(prevLineTop - slop, firstLineTop + slop); |
| 4920 | |
| 4921 | final float lastLineBottom = layout.getLineBottom(lineCount - 1) + verticalOffset; |
| 4922 | final float prevLineBottom = layout.getLineBottom(prevLine) + verticalOffset; |
| 4923 | final float yBottomBound = Math.min(prevLineBottom + slop, lastLineBottom - slop); |
| 4924 | |
| 4925 | // Determine if we've moved lines based on y position and previous line. |
| 4926 | int currLine; |
| 4927 | if (y <= yTopBound) { |
| 4928 | currLine = Math.max(prevLine - 1, 0); |
| 4929 | } else if (y >= yBottomBound) { |
| 4930 | currLine = Math.min(prevLine + 1, lineCount - 1); |
| 4931 | } else { |
| 4932 | currLine = prevLine; |
| 4933 | } |
| 4934 | return currLine; |
| 4935 | } |
| 4936 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4937 | /** |
| 4938 | * A CursorController instance can be used to control a cursor in the text. |
| 4939 | */ |
| 4940 | private interface CursorController extends ViewTreeObserver.OnTouchModeChangeListener { |
| 4941 | /** |
| 4942 | * Makes the cursor controller visible on screen. |
| 4943 | * See also {@link #hide()}. |
| 4944 | */ |
| 4945 | public void show(); |
| 4946 | |
| 4947 | /** |
| 4948 | * Hide the cursor controller from screen. |
| 4949 | * See also {@link #show()}. |
| 4950 | */ |
| 4951 | public void hide(); |
| 4952 | |
| 4953 | /** |
| 4954 | * Called when the view is detached from window. Perform house keeping task, such as |
| 4955 | * stopping Runnable thread that would otherwise keep a reference on the context, thus |
| 4956 | * preventing the activity from being recycled. |
| 4957 | */ |
| 4958 | public void onDetached(); |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 4959 | |
| 4960 | public boolean isCursorBeingModified(); |
| 4961 | |
| 4962 | public boolean isActive(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | private class InsertionPointCursorController implements CursorController { |
| 4966 | private InsertionHandleView mHandle; |
| 4967 | |
| 4968 | public void show() { |
| 4969 | getHandle().show(); |
Andrei Stingaceanu | 35c550c | 2015-05-07 16:49:49 +0100 | [diff] [blame] | 4970 | |
| 4971 | if (mSelectionModifierCursorController != null) { |
| 4972 | mSelectionModifierCursorController.hide(); |
| 4973 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4974 | } |
| 4975 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4976 | public void hide() { |
| 4977 | if (mHandle != null) { |
| 4978 | mHandle.hide(); |
| 4979 | } |
| 4980 | } |
| 4981 | |
| 4982 | public void onTouchModeChanged(boolean isInTouchMode) { |
| 4983 | if (!isInTouchMode) { |
| 4984 | hide(); |
| 4985 | } |
| 4986 | } |
| 4987 | |
| 4988 | private InsertionHandleView getHandle() { |
| 4989 | if (mSelectHandleCenter == null) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 4990 | mSelectHandleCenter = mTextView.getContext().getDrawable( |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4991 | mTextView.mTextSelectHandleRes); |
| 4992 | } |
| 4993 | if (mHandle == null) { |
| 4994 | mHandle = new InsertionHandleView(mSelectHandleCenter); |
| 4995 | } |
| 4996 | return mHandle; |
| 4997 | } |
| 4998 | |
| 4999 | @Override |
| 5000 | public void onDetached() { |
| 5001 | final ViewTreeObserver observer = mTextView.getViewTreeObserver(); |
| 5002 | observer.removeOnTouchModeChangeListener(this); |
| 5003 | |
| 5004 | if (mHandle != null) mHandle.onDetached(); |
| 5005 | } |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 5006 | |
| 5007 | @Override |
| 5008 | public boolean isCursorBeingModified() { |
| 5009 | return mHandle != null && mHandle.isDragging(); |
| 5010 | } |
| 5011 | |
| 5012 | @Override |
| 5013 | public boolean isActive() { |
| 5014 | return mHandle != null && mHandle.isShowing(); |
| 5015 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5016 | } |
| 5017 | |
| 5018 | class SelectionModifierCursorController implements CursorController { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5019 | // The cursor controller handles, lazily created when shown. |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5020 | private SelectionHandleView mStartHandle; |
| 5021 | private SelectionHandleView mEndHandle; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5022 | // The offsets of that last touch down event. Remembered to start selection there. |
| 5023 | private int mMinTouchOffset, mMaxTouchOffset; |
| 5024 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5025 | private float mDownPositionX, mDownPositionY; |
| 5026 | private boolean mGestureStayedInTapRegion; |
| 5027 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5028 | // Where the user first starts the drag motion. |
| 5029 | private int mStartOffset = -1; |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5030 | |
Mady Mellor | 7a93644 | 2015-05-20 10:05:52 -0700 | [diff] [blame] | 5031 | private boolean mHaventMovedEnoughToStartDrag; |
Mady Mellor | f9f8aeb | 2015-06-17 09:46:01 -0700 | [diff] [blame] | 5032 | // The line that a selection happened most recently with the drag accelerator. |
| 5033 | private int mLineSelectionIsOn = -1; |
| 5034 | // Whether the drag accelerator has selected past the initial line. |
| 5035 | private boolean mSwitchedLines = false; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5036 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5037 | // Indicates the drag accelerator mode that the user is currently using. |
| 5038 | private int mDragAcceleratorMode = DRAG_ACCELERATOR_MODE_INACTIVE; |
| 5039 | // Drag accelerator is inactive. |
| 5040 | private static final int DRAG_ACCELERATOR_MODE_INACTIVE = 0; |
| 5041 | // Character based selection by dragging. Only for mouse. |
| 5042 | private static final int DRAG_ACCELERATOR_MODE_CHARACTER = 1; |
| 5043 | // Word based selection by dragging. Enabled after long pressing or double tapping. |
| 5044 | private static final int DRAG_ACCELERATOR_MODE_WORD = 2; |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 5045 | // Paragraph based selection by dragging. Enabled after mouse triple click. |
| 5046 | private static final int DRAG_ACCELERATOR_MODE_PARAGRAPH = 3; |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5047 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5048 | SelectionModifierCursorController() { |
| 5049 | resetTouchOffsets(); |
| 5050 | } |
| 5051 | |
| 5052 | public void show() { |
| 5053 | if (mTextView.isInBatchEditMode()) { |
| 5054 | return; |
| 5055 | } |
| 5056 | initDrawables(); |
| 5057 | initHandles(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5058 | } |
| 5059 | |
| 5060 | private void initDrawables() { |
| 5061 | if (mSelectHandleLeft == null) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 5062 | mSelectHandleLeft = mTextView.getContext().getDrawable( |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5063 | mTextView.mTextSelectHandleLeftRes); |
| 5064 | } |
| 5065 | if (mSelectHandleRight == null) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 5066 | mSelectHandleRight = mTextView.getContext().getDrawable( |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5067 | mTextView.mTextSelectHandleRightRes); |
| 5068 | } |
| 5069 | } |
| 5070 | |
| 5071 | private void initHandles() { |
| 5072 | // Lazy object creation has to be done before updatePosition() is called. |
| 5073 | if (mStartHandle == null) { |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5074 | mStartHandle = new SelectionHandleView(mSelectHandleLeft, mSelectHandleRight, |
| 5075 | com.android.internal.R.id.selection_start_handle, |
| 5076 | HANDLE_TYPE_SELECTION_START); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5077 | } |
| 5078 | if (mEndHandle == null) { |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5079 | mEndHandle = new SelectionHandleView(mSelectHandleRight, mSelectHandleLeft, |
| 5080 | com.android.internal.R.id.selection_end_handle, |
| 5081 | HANDLE_TYPE_SELECTION_END); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5082 | } |
| 5083 | |
| 5084 | mStartHandle.show(); |
| 5085 | mEndHandle.show(); |
| 5086 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5087 | hideInsertionPointCursorController(); |
| 5088 | } |
| 5089 | |
| 5090 | public void hide() { |
| 5091 | if (mStartHandle != null) mStartHandle.hide(); |
| 5092 | if (mEndHandle != null) mEndHandle.hide(); |
| 5093 | } |
| 5094 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5095 | public void enterDrag(int dragAcceleratorMode) { |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5096 | // Just need to init the handles / hide insertion cursor. |
| 5097 | show(); |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5098 | mDragAcceleratorMode = dragAcceleratorMode; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5099 | // Start location of selection. |
| 5100 | mStartOffset = mTextView.getOffsetForPosition(mLastDownPositionX, |
| 5101 | mLastDownPositionY); |
Mady Mellor | f9f8aeb | 2015-06-17 09:46:01 -0700 | [diff] [blame] | 5102 | mLineSelectionIsOn = mTextView.getLineAtCoordinate(mLastDownPositionY); |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5103 | // Don't show the handles until user has lifted finger. |
| 5104 | hide(); |
| 5105 | |
| 5106 | // This stops scrolling parents from intercepting the touch event, allowing |
| 5107 | // the user to continue dragging across the screen to select text; TextView will |
| 5108 | // scroll as necessary. |
| 5109 | mTextView.getParent().requestDisallowInterceptTouchEvent(true); |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5110 | mTextView.cancelLongPress(); |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5111 | } |
| 5112 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5113 | public void onTouchEvent(MotionEvent event) { |
| 5114 | // This is done even when the View does not have focus, so that long presses can start |
| 5115 | // selection and tap can move cursor from this tap position. |
Mady Mellor | 7a93644 | 2015-05-20 10:05:52 -0700 | [diff] [blame] | 5116 | final float eventX = event.getX(); |
| 5117 | final float eventY = event.getY(); |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5118 | final boolean isMouse = event.isFromSource(InputDevice.SOURCE_MOUSE); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5119 | switch (event.getActionMasked()) { |
| 5120 | case MotionEvent.ACTION_DOWN: |
Andrei Stingaceanu | 83830727 | 2015-06-19 17:58:47 +0100 | [diff] [blame] | 5121 | if (extractedTextModeWillBeStarted()) { |
| 5122 | // Prevent duplicating the selection handles until the mode starts. |
| 5123 | hide(); |
| 5124 | } else { |
| 5125 | // Remember finger down position, to be able to start selection from there. |
| 5126 | mMinTouchOffset = mMaxTouchOffset = mTextView.getOffsetForPosition( |
| 5127 | eventX, eventY); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5128 | |
Andrei Stingaceanu | 83830727 | 2015-06-19 17:58:47 +0100 | [diff] [blame] | 5129 | // Double tap detection |
| 5130 | if (mGestureStayedInTapRegion) { |
Keisuke Kuroyanagi | 155aecb | 2015-11-05 19:10:07 +0900 | [diff] [blame] | 5131 | if (mTapState == TAP_STATE_DOUBLE_TAP |
| 5132 | || mTapState == TAP_STATE_TRIPLE_CLICK) { |
Andrei Stingaceanu | 83830727 | 2015-06-19 17:58:47 +0100 | [diff] [blame] | 5133 | final float deltaX = eventX - mDownPositionX; |
| 5134 | final float deltaY = eventY - mDownPositionY; |
| 5135 | final float distanceSquared = deltaX * deltaX + deltaY * deltaY; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5136 | |
Andrei Stingaceanu | 83830727 | 2015-06-19 17:58:47 +0100 | [diff] [blame] | 5137 | ViewConfiguration viewConfiguration = ViewConfiguration.get( |
| 5138 | mTextView.getContext()); |
| 5139 | int doubleTapSlop = viewConfiguration.getScaledDoubleTapSlop(); |
| 5140 | boolean stayedInArea = |
| 5141 | distanceSquared < doubleTapSlop * doubleTapSlop; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5142 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5143 | if (stayedInArea && (isMouse || isPositionOnText(eventX, eventY))) { |
Keisuke Kuroyanagi | 155aecb | 2015-11-05 19:10:07 +0900 | [diff] [blame] | 5144 | if (mTapState == TAP_STATE_DOUBLE_TAP) { |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 5145 | selectCurrentWordAndStartDrag(); |
Keisuke Kuroyanagi | 155aecb | 2015-11-05 19:10:07 +0900 | [diff] [blame] | 5146 | } else if (mTapState == TAP_STATE_TRIPLE_CLICK) { |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 5147 | selectCurrentParagraphAndStartDrag(); |
| 5148 | } |
Andrei Stingaceanu | 83830727 | 2015-06-19 17:58:47 +0100 | [diff] [blame] | 5149 | mDiscardNextActionUp = true; |
| 5150 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5151 | } |
| 5152 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5153 | |
Andrei Stingaceanu | 83830727 | 2015-06-19 17:58:47 +0100 | [diff] [blame] | 5154 | mDownPositionX = eventX; |
| 5155 | mDownPositionY = eventY; |
| 5156 | mGestureStayedInTapRegion = true; |
| 5157 | mHaventMovedEnoughToStartDrag = true; |
| 5158 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5159 | break; |
| 5160 | |
| 5161 | case MotionEvent.ACTION_POINTER_DOWN: |
| 5162 | case MotionEvent.ACTION_POINTER_UP: |
| 5163 | // Handle multi-point gestures. Keep min and max offset positions. |
| 5164 | // Only activated for devices that correctly handle multi-touch. |
| 5165 | if (mTextView.getContext().getPackageManager().hasSystemFeature( |
| 5166 | PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT)) { |
| 5167 | updateMinAndMaxOffsets(event); |
| 5168 | } |
| 5169 | break; |
| 5170 | |
| 5171 | case MotionEvent.ACTION_MOVE: |
Mady Mellor | 7a93644 | 2015-05-20 10:05:52 -0700 | [diff] [blame] | 5172 | final ViewConfiguration viewConfig = ViewConfiguration.get( |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5173 | mTextView.getContext()); |
Mady Mellor | 7a93644 | 2015-05-20 10:05:52 -0700 | [diff] [blame] | 5174 | final int touchSlop = viewConfig.getScaledTouchSlop(); |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5175 | |
Mady Mellor | 7a93644 | 2015-05-20 10:05:52 -0700 | [diff] [blame] | 5176 | if (mGestureStayedInTapRegion || mHaventMovedEnoughToStartDrag) { |
| 5177 | final float deltaX = eventX - mDownPositionX; |
| 5178 | final float deltaY = eventY - mDownPositionY; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5179 | final float distanceSquared = deltaX * deltaX + deltaY * deltaY; |
| 5180 | |
Mady Mellor | 7a93644 | 2015-05-20 10:05:52 -0700 | [diff] [blame] | 5181 | if (mGestureStayedInTapRegion) { |
| 5182 | int doubleTapTouchSlop = viewConfig.getScaledDoubleTapTouchSlop(); |
| 5183 | mGestureStayedInTapRegion = |
| 5184 | distanceSquared <= doubleTapTouchSlop * doubleTapTouchSlop; |
| 5185 | } |
| 5186 | if (mHaventMovedEnoughToStartDrag) { |
| 5187 | // We don't start dragging until the user has moved enough. |
| 5188 | mHaventMovedEnoughToStartDrag = |
| 5189 | distanceSquared <= touchSlop * touchSlop; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5190 | } |
| 5191 | } |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5192 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5193 | if (isMouse && !isDragAcceleratorActive()) { |
| 5194 | final int offset = mTextView.getOffsetForPosition(eventX, eventY); |
Keisuke Kuroyanagi | e876085 | 2015-12-21 18:30:19 +0900 | [diff] [blame] | 5195 | if (mTextView.hasSelection() |
| 5196 | && (!mHaventMovedEnoughToStartDrag || mStartOffset != offset) |
| 5197 | && offset >= mTextView.getSelectionStart() |
| 5198 | && offset <= mTextView.getSelectionEnd()) { |
| 5199 | startDragAndDrop(); |
| 5200 | break; |
| 5201 | } |
| 5202 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5203 | if (mStartOffset != offset) { |
| 5204 | // Start character based drag accelerator. |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 5205 | stopTextActionMode(); |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5206 | enterDrag(DRAG_ACCELERATOR_MODE_CHARACTER); |
| 5207 | mDiscardNextActionUp = true; |
| 5208 | mHaventMovedEnoughToStartDrag = false; |
| 5209 | } |
| 5210 | } |
| 5211 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5212 | if (mStartHandle != null && mStartHandle.isShowing()) { |
| 5213 | // Don't do the drag if the handles are showing already. |
| 5214 | break; |
| 5215 | } |
| 5216 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5217 | updateSelection(event); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5218 | break; |
| 5219 | |
| 5220 | case MotionEvent.ACTION_UP: |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5221 | if (!isDragAcceleratorActive()) { |
| 5222 | break; |
| 5223 | } |
| 5224 | updateSelection(event); |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5225 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5226 | // No longer dragging to select text, let the parent intercept events. |
| 5227 | mTextView.getParent().requestDisallowInterceptTouchEvent(false); |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5228 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5229 | // No longer the first dragging motion, reset. |
| 5230 | resetDragAcceleratorState(); |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 5231 | |
| 5232 | if (mTextView.hasSelection()) { |
| 5233 | startSelectionActionMode(); |
| 5234 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5235 | break; |
| 5236 | } |
| 5237 | } |
| 5238 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5239 | private void updateSelection(MotionEvent event) { |
| 5240 | if (mTextView.getLayout() != null) { |
| 5241 | switch (mDragAcceleratorMode) { |
| 5242 | case DRAG_ACCELERATOR_MODE_CHARACTER: |
| 5243 | updateCharacterBasedSelection(event); |
| 5244 | break; |
| 5245 | case DRAG_ACCELERATOR_MODE_WORD: |
| 5246 | updateWordBasedSelection(event); |
| 5247 | break; |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 5248 | case DRAG_ACCELERATOR_MODE_PARAGRAPH: |
| 5249 | updateParagraphBasedSelection(event); |
| 5250 | break; |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5251 | } |
| 5252 | } |
| 5253 | } |
| 5254 | |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 5255 | /** |
| 5256 | * If the TextView allows text selection, selects the current paragraph and starts a drag. |
| 5257 | * |
| 5258 | * @return true if the drag was started. |
| 5259 | */ |
| 5260 | private boolean selectCurrentParagraphAndStartDrag() { |
| 5261 | if (mInsertionActionModeRunnable != null) { |
| 5262 | mTextView.removeCallbacks(mInsertionActionModeRunnable); |
| 5263 | } |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 5264 | stopTextActionMode(); |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 5265 | if (!selectCurrentParagraph()) { |
| 5266 | return false; |
| 5267 | } |
| 5268 | enterDrag(SelectionModifierCursorController.DRAG_ACCELERATOR_MODE_PARAGRAPH); |
| 5269 | return true; |
| 5270 | } |
| 5271 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5272 | private void updateCharacterBasedSelection(MotionEvent event) { |
| 5273 | final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY()); |
| 5274 | Selection.setSelection((Spannable) mTextView.getText(), mStartOffset, offset); |
| 5275 | } |
| 5276 | |
| 5277 | private void updateWordBasedSelection(MotionEvent event) { |
| 5278 | if (mHaventMovedEnoughToStartDrag) { |
| 5279 | return; |
| 5280 | } |
| 5281 | final boolean isMouse = event.isFromSource(InputDevice.SOURCE_MOUSE); |
| 5282 | final ViewConfiguration viewConfig = ViewConfiguration.get( |
| 5283 | mTextView.getContext()); |
| 5284 | final float eventX = event.getX(); |
| 5285 | final float eventY = event.getY(); |
| 5286 | final int currLine; |
| 5287 | if (isMouse) { |
| 5288 | // No need to offset the y coordinate for mouse input. |
| 5289 | currLine = mTextView.getLineAtCoordinate(eventY); |
| 5290 | } else { |
| 5291 | float y = eventY; |
| 5292 | if (mSwitchedLines) { |
| 5293 | // Offset the finger by the same vertical offset as the handles. |
| 5294 | // This improves visibility of the content being selected by |
| 5295 | // shifting the finger below the content, this is applied once |
| 5296 | // the user has switched lines. |
| 5297 | final int touchSlop = viewConfig.getScaledTouchSlop(); |
| 5298 | final float fingerOffset = (mStartHandle != null) |
| 5299 | ? mStartHandle.getIdealVerticalOffset() |
| 5300 | : touchSlop; |
| 5301 | y = eventY - fingerOffset; |
| 5302 | } |
| 5303 | |
| 5304 | currLine = getCurrentLineAdjustedForSlop(mTextView.getLayout(), mLineSelectionIsOn, |
| 5305 | y); |
| 5306 | if (!mSwitchedLines && currLine != mLineSelectionIsOn) { |
| 5307 | // Break early here, we want to offset the finger position from |
| 5308 | // the selection highlight, once the user moved their finger |
| 5309 | // to a different line we should apply the offset and *not* switch |
| 5310 | // lines until recomputing the position with the finger offset. |
| 5311 | mSwitchedLines = true; |
| 5312 | return; |
| 5313 | } |
| 5314 | } |
| 5315 | |
| 5316 | int startOffset; |
| 5317 | int offset = mTextView.getOffsetAtCoordinate(currLine, eventX); |
| 5318 | // Snap to word boundaries. |
| 5319 | if (mStartOffset < offset) { |
| 5320 | // Expanding with end handle. |
| 5321 | offset = getWordEnd(offset); |
| 5322 | startOffset = getWordStart(mStartOffset); |
| 5323 | } else { |
| 5324 | // Expanding with start handle. |
| 5325 | offset = getWordStart(offset); |
| 5326 | startOffset = getWordEnd(mStartOffset); |
| 5327 | } |
| 5328 | mLineSelectionIsOn = currLine; |
| 5329 | Selection.setSelection((Spannable) mTextView.getText(), |
| 5330 | startOffset, offset); |
| 5331 | } |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 5332 | |
| 5333 | private void updateParagraphBasedSelection(MotionEvent event) { |
| 5334 | final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY()); |
| 5335 | |
| 5336 | final int start = Math.min(offset, mStartOffset); |
| 5337 | final int end = Math.max(offset, mStartOffset); |
| 5338 | final long paragraphsRange = getParagraphsRange(start, end); |
| 5339 | final int selectionStart = TextUtils.unpackRangeStartFromLong(paragraphsRange); |
| 5340 | final int selectionEnd = TextUtils.unpackRangeEndFromLong(paragraphsRange); |
| 5341 | Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd); |
| 5342 | } |
| 5343 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5344 | /** |
| 5345 | * @param event |
| 5346 | */ |
| 5347 | private void updateMinAndMaxOffsets(MotionEvent event) { |
| 5348 | int pointerCount = event.getPointerCount(); |
| 5349 | for (int index = 0; index < pointerCount; index++) { |
| 5350 | int offset = mTextView.getOffsetForPosition(event.getX(index), event.getY(index)); |
| 5351 | if (offset < mMinTouchOffset) mMinTouchOffset = offset; |
| 5352 | if (offset > mMaxTouchOffset) mMaxTouchOffset = offset; |
| 5353 | } |
| 5354 | } |
| 5355 | |
| 5356 | public int getMinTouchOffset() { |
| 5357 | return mMinTouchOffset; |
| 5358 | } |
| 5359 | |
| 5360 | public int getMaxTouchOffset() { |
| 5361 | return mMaxTouchOffset; |
| 5362 | } |
| 5363 | |
| 5364 | public void resetTouchOffsets() { |
| 5365 | mMinTouchOffset = mMaxTouchOffset = -1; |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5366 | resetDragAcceleratorState(); |
| 5367 | } |
| 5368 | |
| 5369 | private void resetDragAcceleratorState() { |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5370 | mStartOffset = -1; |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5371 | mDragAcceleratorMode = DRAG_ACCELERATOR_MODE_INACTIVE; |
Mady Mellor | f9f8aeb | 2015-06-17 09:46:01 -0700 | [diff] [blame] | 5372 | mSwitchedLines = false; |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 5373 | final int selectionStart = mTextView.getSelectionStart(); |
| 5374 | final int selectionEnd = mTextView.getSelectionEnd(); |
| 5375 | if (selectionStart > selectionEnd) { |
| 5376 | Selection.setSelection((Spannable) mTextView.getText(), |
| 5377 | selectionEnd, selectionStart); |
| 5378 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5379 | } |
| 5380 | |
| 5381 | /** |
| 5382 | * @return true iff this controller is currently used to move the selection start. |
| 5383 | */ |
| 5384 | public boolean isSelectionStartDragged() { |
| 5385 | return mStartHandle != null && mStartHandle.isDragging(); |
| 5386 | } |
| 5387 | |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 5388 | @Override |
| 5389 | public boolean isCursorBeingModified() { |
| 5390 | return isDragAcceleratorActive() || isSelectionStartDragged() |
| 5391 | || (mEndHandle != null && mEndHandle.isDragging()); |
| 5392 | } |
| 5393 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5394 | /** |
| 5395 | * @return true if the user is selecting text using the drag accelerator. |
| 5396 | */ |
| 5397 | public boolean isDragAcceleratorActive() { |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5398 | return mDragAcceleratorMode != DRAG_ACCELERATOR_MODE_INACTIVE; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5399 | } |
| 5400 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5401 | public void onTouchModeChanged(boolean isInTouchMode) { |
| 5402 | if (!isInTouchMode) { |
| 5403 | hide(); |
| 5404 | } |
| 5405 | } |
| 5406 | |
| 5407 | @Override |
| 5408 | public void onDetached() { |
| 5409 | final ViewTreeObserver observer = mTextView.getViewTreeObserver(); |
| 5410 | observer.removeOnTouchModeChangeListener(this); |
| 5411 | |
| 5412 | if (mStartHandle != null) mStartHandle.onDetached(); |
| 5413 | if (mEndHandle != null) mEndHandle.onDetached(); |
| 5414 | } |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 5415 | |
| 5416 | @Override |
| 5417 | public boolean isActive() { |
| 5418 | return mStartHandle != null && mStartHandle.isShowing(); |
| 5419 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5420 | } |
| 5421 | |
| 5422 | private class CorrectionHighlighter { |
| 5423 | private final Path mPath = new Path(); |
Chris Craik | 6a49dde | 2015-05-12 10:28:14 -0700 | [diff] [blame] | 5424 | private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5425 | private int mStart, mEnd; |
| 5426 | private long mFadingStartTime; |
| 5427 | private RectF mTempRectF; |
| 5428 | private final static int FADE_OUT_DURATION = 400; |
| 5429 | |
| 5430 | public CorrectionHighlighter() { |
| 5431 | mPaint.setCompatibilityScaling(mTextView.getResources().getCompatibilityInfo(). |
| 5432 | applicationScale); |
| 5433 | mPaint.setStyle(Paint.Style.FILL); |
| 5434 | } |
| 5435 | |
| 5436 | public void highlight(CorrectionInfo info) { |
| 5437 | mStart = info.getOffset(); |
| 5438 | mEnd = mStart + info.getNewText().length(); |
| 5439 | mFadingStartTime = SystemClock.uptimeMillis(); |
| 5440 | |
| 5441 | if (mStart < 0 || mEnd < 0) { |
| 5442 | stopAnimation(); |
| 5443 | } |
| 5444 | } |
| 5445 | |
| 5446 | public void draw(Canvas canvas, int cursorOffsetVertical) { |
| 5447 | if (updatePath() && updatePaint()) { |
| 5448 | if (cursorOffsetVertical != 0) { |
| 5449 | canvas.translate(0, cursorOffsetVertical); |
| 5450 | } |
| 5451 | |
| 5452 | canvas.drawPath(mPath, mPaint); |
| 5453 | |
| 5454 | if (cursorOffsetVertical != 0) { |
| 5455 | canvas.translate(0, -cursorOffsetVertical); |
| 5456 | } |
| 5457 | invalidate(true); // TODO invalidate cursor region only |
| 5458 | } else { |
| 5459 | stopAnimation(); |
| 5460 | invalidate(false); // TODO invalidate cursor region only |
| 5461 | } |
| 5462 | } |
| 5463 | |
| 5464 | private boolean updatePaint() { |
| 5465 | final long duration = SystemClock.uptimeMillis() - mFadingStartTime; |
| 5466 | if (duration > FADE_OUT_DURATION) return false; |
| 5467 | |
| 5468 | final float coef = 1.0f - (float) duration / FADE_OUT_DURATION; |
| 5469 | final int highlightColorAlpha = Color.alpha(mTextView.mHighlightColor); |
| 5470 | final int color = (mTextView.mHighlightColor & 0x00FFFFFF) + |
| 5471 | ((int) (highlightColorAlpha * coef) << 24); |
| 5472 | mPaint.setColor(color); |
| 5473 | return true; |
| 5474 | } |
| 5475 | |
| 5476 | private boolean updatePath() { |
| 5477 | final Layout layout = mTextView.getLayout(); |
| 5478 | if (layout == null) return false; |
| 5479 | |
| 5480 | // Update in case text is edited while the animation is run |
| 5481 | final int length = mTextView.getText().length(); |
| 5482 | int start = Math.min(length, mStart); |
| 5483 | int end = Math.min(length, mEnd); |
| 5484 | |
| 5485 | mPath.reset(); |
| 5486 | layout.getSelectionPath(start, end, mPath); |
| 5487 | return true; |
| 5488 | } |
| 5489 | |
| 5490 | private void invalidate(boolean delayed) { |
| 5491 | if (mTextView.getLayout() == null) return; |
| 5492 | |
| 5493 | if (mTempRectF == null) mTempRectF = new RectF(); |
| 5494 | mPath.computeBounds(mTempRectF, false); |
| 5495 | |
| 5496 | int left = mTextView.getCompoundPaddingLeft(); |
| 5497 | int top = mTextView.getExtendedPaddingTop() + mTextView.getVerticalOffset(true); |
| 5498 | |
| 5499 | if (delayed) { |
| 5500 | mTextView.postInvalidateOnAnimation( |
| 5501 | left + (int) mTempRectF.left, top + (int) mTempRectF.top, |
| 5502 | left + (int) mTempRectF.right, top + (int) mTempRectF.bottom); |
| 5503 | } else { |
| 5504 | mTextView.postInvalidate((int) mTempRectF.left, (int) mTempRectF.top, |
| 5505 | (int) mTempRectF.right, (int) mTempRectF.bottom); |
| 5506 | } |
| 5507 | } |
| 5508 | |
| 5509 | private void stopAnimation() { |
| 5510 | Editor.this.mCorrectionHighlighter = null; |
| 5511 | } |
| 5512 | } |
| 5513 | |
| 5514 | private static class ErrorPopup extends PopupWindow { |
| 5515 | private boolean mAbove = false; |
| 5516 | private final TextView mView; |
| 5517 | private int mPopupInlineErrorBackgroundId = 0; |
| 5518 | private int mPopupInlineErrorAboveBackgroundId = 0; |
| 5519 | |
| 5520 | ErrorPopup(TextView v, int width, int height) { |
| 5521 | super(v, width, height); |
| 5522 | mView = v; |
| 5523 | // Make sure the TextView has a background set as it will be used the first time it is |
Fabrice Di Meglio | bb0cbae | 2012-11-13 20:51:24 -0800 | [diff] [blame] | 5524 | // shown and positioned. Initialized with below background, which should have |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5525 | // dimensions identical to the above version for this to work (and is more likely). |
| 5526 | mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId, |
| 5527 | com.android.internal.R.styleable.Theme_errorMessageBackground); |
| 5528 | mView.setBackgroundResource(mPopupInlineErrorBackgroundId); |
| 5529 | } |
| 5530 | |
| 5531 | void fixDirection(boolean above) { |
| 5532 | mAbove = above; |
| 5533 | |
| 5534 | if (above) { |
| 5535 | mPopupInlineErrorAboveBackgroundId = |
| 5536 | getResourceId(mPopupInlineErrorAboveBackgroundId, |
| 5537 | com.android.internal.R.styleable.Theme_errorMessageAboveBackground); |
| 5538 | } else { |
| 5539 | mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId, |
| 5540 | com.android.internal.R.styleable.Theme_errorMessageBackground); |
| 5541 | } |
| 5542 | |
| 5543 | mView.setBackgroundResource(above ? mPopupInlineErrorAboveBackgroundId : |
| 5544 | mPopupInlineErrorBackgroundId); |
| 5545 | } |
| 5546 | |
| 5547 | private int getResourceId(int currentId, int index) { |
| 5548 | if (currentId == 0) { |
| 5549 | TypedArray styledAttributes = mView.getContext().obtainStyledAttributes( |
| 5550 | R.styleable.Theme); |
| 5551 | currentId = styledAttributes.getResourceId(index, 0); |
| 5552 | styledAttributes.recycle(); |
| 5553 | } |
| 5554 | return currentId; |
| 5555 | } |
| 5556 | |
| 5557 | @Override |
| 5558 | public void update(int x, int y, int w, int h, boolean force) { |
| 5559 | super.update(x, y, w, h, force); |
| 5560 | |
| 5561 | boolean above = isAboveAnchor(); |
| 5562 | if (above != mAbove) { |
| 5563 | fixDirection(above); |
| 5564 | } |
| 5565 | } |
| 5566 | } |
| 5567 | |
| 5568 | static class InputContentType { |
| 5569 | int imeOptions = EditorInfo.IME_NULL; |
| 5570 | String privateImeOptions; |
| 5571 | CharSequence imeActionLabel; |
| 5572 | int imeActionId; |
| 5573 | Bundle extras; |
| 5574 | OnEditorActionListener onEditorActionListener; |
| 5575 | boolean enterDown; |
Yohei Yukawa | d469f21 | 2016-01-21 12:38:09 -0800 | [diff] [blame] | 5576 | LocaleList imeHintLocales; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5577 | } |
| 5578 | |
| 5579 | static class InputMethodState { |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 5580 | ExtractedTextRequest mExtractedTextRequest; |
| 5581 | final ExtractedText mExtractedText = new ExtractedText(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5582 | int mBatchEditNesting; |
| 5583 | boolean mCursorChanged; |
| 5584 | boolean mSelectionModeChanged; |
| 5585 | boolean mContentChanged; |
| 5586 | int mChangedStart, mChangedEnd, mChangedDelta; |
| 5587 | } |
Satoshi Kataoka | 0e3849a | 2012-12-13 14:37:19 +0900 | [diff] [blame] | 5588 | |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 5589 | /** |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5590 | * @return True iff (start, end) is a valid range within the text. |
| 5591 | */ |
| 5592 | private static boolean isValidRange(CharSequence text, int start, int end) { |
| 5593 | return 0 <= start && start <= end && end <= text.length(); |
| 5594 | } |
| 5595 | |
Seigo Nonaka | a60160b | 2015-08-19 12:38:35 -0700 | [diff] [blame] | 5596 | @VisibleForTesting |
| 5597 | public SuggestionsPopupWindow getSuggestionsPopupWindowForTesting() { |
| 5598 | return mSuggestionsPopupWindow; |
| 5599 | } |
| 5600 | |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5601 | /** |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 5602 | * An InputFilter that monitors text input to maintain undo history. It does not modify the |
| 5603 | * text being typed (and hence always returns null from the filter() method). |
| 5604 | */ |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 5605 | public static class UndoInputFilter implements InputFilter { |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 5606 | private final Editor mEditor; |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 5607 | |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5608 | // Whether the current filter pass is directly caused by an end-user text edit. |
| 5609 | private boolean mIsUserEdit; |
| 5610 | |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 5611 | // Whether the text field is handling an IME composition. Must be parceled in case the user |
| 5612 | // rotates the screen during composition. |
| 5613 | private boolean mHasComposition; |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5614 | |
Keisuke Kuroyanagi | fae4578 | 2016-02-24 18:53:00 -0800 | [diff] [blame] | 5615 | // Whether to merge events into one operation. |
| 5616 | private boolean mForceMerge; |
| 5617 | |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 5618 | public UndoInputFilter(Editor editor) { |
| 5619 | mEditor = editor; |
| 5620 | } |
| 5621 | |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 5622 | public void saveInstanceState(Parcel parcel) { |
| 5623 | parcel.writeInt(mIsUserEdit ? 1 : 0); |
| 5624 | parcel.writeInt(mHasComposition ? 1 : 0); |
| 5625 | } |
| 5626 | |
| 5627 | public void restoreInstanceState(Parcel parcel) { |
| 5628 | mIsUserEdit = parcel.readInt() != 0; |
| 5629 | mHasComposition = parcel.readInt() != 0; |
| 5630 | } |
| 5631 | |
Keisuke Kuroyanagi | fae4578 | 2016-02-24 18:53:00 -0800 | [diff] [blame] | 5632 | public void setForceMerge(boolean forceMerge) { |
| 5633 | mForceMerge = forceMerge; |
| 5634 | } |
| 5635 | |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5636 | /** |
| 5637 | * Signals that a user-triggered edit is starting. |
| 5638 | */ |
| 5639 | public void beginBatchEdit() { |
| 5640 | if (DEBUG_UNDO) Log.d(TAG, "beginBatchEdit"); |
| 5641 | mIsUserEdit = true; |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5642 | } |
| 5643 | |
| 5644 | public void endBatchEdit() { |
| 5645 | if (DEBUG_UNDO) Log.d(TAG, "endBatchEdit"); |
| 5646 | mIsUserEdit = false; |
| 5647 | } |
| 5648 | |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 5649 | @Override |
| 5650 | public CharSequence filter(CharSequence source, int start, int end, |
| 5651 | Spanned dest, int dstart, int dend) { |
| 5652 | if (DEBUG_UNDO) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5653 | Log.d(TAG, "filter: source=" + source + " (" + start + "-" + end + ") " + |
| 5654 | "dest=" + dest + " (" + dstart + "-" + dend + ")"); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 5655 | } |
James Cook | f1dad1e | 2015-02-27 11:00:01 -0800 | [diff] [blame] | 5656 | |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5657 | // Check to see if this edit should be tracked for undo. |
| 5658 | if (!canUndoEdit(source, start, end, dest, dstart, dend)) { |
James Cook | f1dad1e | 2015-02-27 11:00:01 -0800 | [diff] [blame] | 5659 | return null; |
| 5660 | } |
| 5661 | |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 5662 | // Check for and handle IME composition edits. |
| 5663 | if (handleCompositionEdit(source, start, end, dstart)) { |
| 5664 | return null; |
| 5665 | } |
| 5666 | |
| 5667 | // Handle keyboard edits. |
| 5668 | handleKeyboardEdit(source, start, end, dest, dstart, dend); |
| 5669 | return null; |
| 5670 | } |
| 5671 | |
| 5672 | /** |
| 5673 | * Returns true iff the edit was handled, either because it should be ignored or because |
| 5674 | * this function created an undo operation for it. |
| 5675 | */ |
| 5676 | private boolean handleCompositionEdit(CharSequence source, int start, int end, int dstart) { |
| 5677 | // Ignore edits while the user is composing. |
| 5678 | if (isComposition(source)) { |
| 5679 | mHasComposition = true; |
| 5680 | return true; |
| 5681 | } |
| 5682 | final boolean hadComposition = mHasComposition; |
| 5683 | mHasComposition = false; |
| 5684 | |
| 5685 | // Check for the transition out of the composing state. |
| 5686 | if (hadComposition) { |
| 5687 | // If there was no text the user canceled composition. Ignore the edit. |
| 5688 | if (start == end) { |
| 5689 | return true; |
| 5690 | } |
| 5691 | |
| 5692 | // Otherwise the user inserted the composition. |
| 5693 | String newText = TextUtils.substring(source, start, end); |
James Cook | 2205425 | 2015-03-25 14:04:01 -0700 | [diff] [blame] | 5694 | EditOperation edit = new EditOperation(mEditor, "", dstart, newText); |
Keisuke Kuroyanagi | fae4578 | 2016-02-24 18:53:00 -0800 | [diff] [blame] | 5695 | recordEdit(edit, mForceMerge); |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 5696 | return true; |
| 5697 | } |
| 5698 | |
| 5699 | // This was neither a composition event nor a transition out of composing. |
| 5700 | return false; |
| 5701 | } |
| 5702 | |
| 5703 | private void handleKeyboardEdit(CharSequence source, int start, int end, |
| 5704 | Spanned dest, int dstart, int dend) { |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5705 | // An application may install a TextWatcher to provide additional modifications after |
| 5706 | // the initial input filters run (e.g. a credit card formatter that adds spaces to a |
| 5707 | // string). This results in multiple filter() calls for what the user considers to be |
| 5708 | // a single operation. Always undo the whole set of changes in one step. |
Keisuke Kuroyanagi | fae4578 | 2016-02-24 18:53:00 -0800 | [diff] [blame] | 5709 | final boolean forceMerge = mForceMerge || isInTextWatcher(); |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5710 | |
| 5711 | // Build a new operation with all the information from this edit. |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 5712 | String newText = TextUtils.substring(source, start, end); |
| 5713 | String oldText = TextUtils.substring(dest, dstart, dend); |
James Cook | 2205425 | 2015-03-25 14:04:01 -0700 | [diff] [blame] | 5714 | EditOperation edit = new EditOperation(mEditor, oldText, dstart, newText); |
| 5715 | recordEdit(edit, forceMerge); |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 5716 | } |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5717 | |
James Cook | 2205425 | 2015-03-25 14:04:01 -0700 | [diff] [blame] | 5718 | /** |
| 5719 | * Fetches the last undo operation and checks to see if a new edit should be merged into it. |
| 5720 | * If forceMerge is true then the new edit is always merged. |
| 5721 | */ |
| 5722 | private void recordEdit(EditOperation edit, boolean forceMerge) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5723 | // Fetch the last edit operation and attempt to merge in the new edit. |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5724 | final UndoManager um = mEditor.mUndoManager; |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 5725 | um.beginUpdate("Edit text"); |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5726 | EditOperation lastEdit = um.getLastOperation( |
| 5727 | EditOperation.class, mEditor.mUndoOwner, UndoManager.MERGE_MODE_UNIQUE); |
| 5728 | if (lastEdit == null) { |
| 5729 | // Add this as the first edit. |
| 5730 | if (DEBUG_UNDO) Log.d(TAG, "filter: adding first op " + edit); |
| 5731 | um.addOperation(edit, UndoManager.MERGE_MODE_NONE); |
James Cook | 2205425 | 2015-03-25 14:04:01 -0700 | [diff] [blame] | 5732 | } else if (forceMerge) { |
| 5733 | // Forced merges take priority because they could be the result of a non-user-edit |
| 5734 | // change and this case should not create a new undo operation. |
| 5735 | if (DEBUG_UNDO) Log.d(TAG, "filter: force merge " + edit); |
| 5736 | lastEdit.forceMergeWith(edit); |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5737 | } else if (!mIsUserEdit) { |
| 5738 | // An application directly modified the Editable outside of a text edit. Treat this |
| 5739 | // as a new change and don't attempt to merge. |
| 5740 | if (DEBUG_UNDO) Log.d(TAG, "non-user edit, new op " + edit); |
| 5741 | um.commitState(mEditor.mUndoOwner); |
| 5742 | um.addOperation(edit, UndoManager.MERGE_MODE_NONE); |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5743 | } else if (lastEdit.mergeWith(edit)) { |
| 5744 | // Merge succeeded, nothing else to do. |
| 5745 | if (DEBUG_UNDO) Log.d(TAG, "filter: merge succeeded, created " + lastEdit); |
James Cook | 3ac0bcb | 2015-02-26 10:53:41 -0800 | [diff] [blame] | 5746 | } else { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5747 | // Could not merge with the last edit, so commit the last edit and add this edit. |
| 5748 | if (DEBUG_UNDO) Log.d(TAG, "filter: merge failed, adding " + edit); |
| 5749 | um.commitState(mEditor.mUndoOwner); |
| 5750 | um.addOperation(edit, UndoManager.MERGE_MODE_NONE); |
James Cook | 3ac0bcb | 2015-02-26 10:53:41 -0800 | [diff] [blame] | 5751 | } |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 5752 | um.endUpdate(); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 5753 | } |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5754 | |
| 5755 | private boolean canUndoEdit(CharSequence source, int start, int end, |
| 5756 | Spanned dest, int dstart, int dend) { |
| 5757 | if (!mEditor.mAllowUndo) { |
| 5758 | if (DEBUG_UNDO) Log.d(TAG, "filter: undo is disabled"); |
| 5759 | return false; |
| 5760 | } |
| 5761 | |
| 5762 | if (mEditor.mUndoManager.isInUndo()) { |
| 5763 | if (DEBUG_UNDO) Log.d(TAG, "filter: skipping, currently performing undo/redo"); |
| 5764 | return false; |
| 5765 | } |
| 5766 | |
| 5767 | // Text filters run before input operations are applied. However, some input operations |
| 5768 | // are invalid and will throw exceptions when applied. This is common in tests. Don't |
| 5769 | // attempt to undo invalid operations. |
| 5770 | if (!isValidRange(source, start, end) || !isValidRange(dest, dstart, dend)) { |
| 5771 | if (DEBUG_UNDO) Log.d(TAG, "filter: invalid op"); |
| 5772 | return false; |
| 5773 | } |
| 5774 | |
| 5775 | // Earlier filters can rewrite input to be a no-op, for example due to a length limit |
| 5776 | // on an input field. Skip no-op changes. |
| 5777 | if (start == end && dstart == dend) { |
| 5778 | if (DEBUG_UNDO) Log.d(TAG, "filter: skipping no-op"); |
| 5779 | return false; |
| 5780 | } |
| 5781 | |
| 5782 | return true; |
| 5783 | } |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 5784 | |
| 5785 | private boolean isComposition(CharSequence source) { |
| 5786 | if (!(source instanceof Spannable)) { |
| 5787 | return false; |
| 5788 | } |
| 5789 | // This is a composition edit if the source has a non-zero-length composing span. |
| 5790 | Spannable text = (Spannable) source; |
| 5791 | int composeBegin = EditableInputConnection.getComposingSpanStart(text); |
| 5792 | int composeEnd = EditableInputConnection.getComposingSpanEnd(text); |
| 5793 | return composeBegin < composeEnd; |
| 5794 | } |
| 5795 | |
| 5796 | private boolean isInTextWatcher() { |
| 5797 | CharSequence text = mEditor.mTextView.getText(); |
| 5798 | return (text instanceof SpannableStringBuilder) |
| 5799 | && ((SpannableStringBuilder) text).getTextWatcherDepth() > 0; |
| 5800 | } |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 5801 | } |
| 5802 | |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 5803 | /** |
| 5804 | * An operation to undo a single "edit" to a text view. |
| 5805 | */ |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5806 | public static class EditOperation extends UndoOperation<Editor> { |
| 5807 | private static final int TYPE_INSERT = 0; |
| 5808 | private static final int TYPE_DELETE = 1; |
| 5809 | private static final int TYPE_REPLACE = 2; |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 5810 | |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5811 | private int mType; |
| 5812 | private String mOldText; |
| 5813 | private int mOldTextStart; |
| 5814 | private String mNewText; |
| 5815 | private int mNewTextStart; |
| 5816 | |
| 5817 | private int mOldCursorPos; |
| 5818 | private int mNewCursorPos; |
| 5819 | |
| 5820 | /** |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 5821 | * Constructs an edit operation from a text input operation on editor that replaces the |
James Cook | 2205425 | 2015-03-25 14:04:01 -0700 | [diff] [blame] | 5822 | * oldText starting at dstart with newText. |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5823 | */ |
James Cook | 2205425 | 2015-03-25 14:04:01 -0700 | [diff] [blame] | 5824 | public EditOperation(Editor editor, String oldText, int dstart, String newText) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5825 | super(editor.mUndoOwner); |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 5826 | mOldText = oldText; |
| 5827 | mNewText = newText; |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5828 | |
| 5829 | // Determine the type of the edit and store where it occurred. Avoid storing |
| 5830 | // irrevelant data (e.g. mNewTextStart for a delete) because that makes the |
| 5831 | // merging logic more complex (e.g. merging deletes could lead to mNewTextStart being |
| 5832 | // outside the bounds of the final text). |
| 5833 | if (mNewText.length() > 0 && mOldText.length() == 0) { |
| 5834 | mType = TYPE_INSERT; |
| 5835 | mNewTextStart = dstart; |
| 5836 | } else if (mNewText.length() == 0 && mOldText.length() > 0) { |
| 5837 | mType = TYPE_DELETE; |
| 5838 | mOldTextStart = dstart; |
| 5839 | } else { |
| 5840 | mType = TYPE_REPLACE; |
| 5841 | mOldTextStart = mNewTextStart = dstart; |
| 5842 | } |
| 5843 | |
| 5844 | // Store cursor data. |
| 5845 | mOldCursorPos = editor.mTextView.getSelectionStart(); |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 5846 | mNewCursorPos = dstart + mNewText.length(); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 5847 | } |
| 5848 | |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5849 | public EditOperation(Parcel src, ClassLoader loader) { |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 5850 | super(src, loader); |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5851 | mType = src.readInt(); |
| 5852 | mOldText = src.readString(); |
| 5853 | mOldTextStart = src.readInt(); |
| 5854 | mNewText = src.readString(); |
| 5855 | mNewTextStart = src.readInt(); |
| 5856 | mOldCursorPos = src.readInt(); |
| 5857 | mNewCursorPos = src.readInt(); |
| 5858 | } |
| 5859 | |
| 5860 | @Override |
| 5861 | public void writeToParcel(Parcel dest, int flags) { |
| 5862 | dest.writeInt(mType); |
| 5863 | dest.writeString(mOldText); |
| 5864 | dest.writeInt(mOldTextStart); |
| 5865 | dest.writeString(mNewText); |
| 5866 | dest.writeInt(mNewTextStart); |
| 5867 | dest.writeInt(mOldCursorPos); |
| 5868 | dest.writeInt(mNewCursorPos); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 5869 | } |
| 5870 | |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5871 | private int getNewTextEnd() { |
| 5872 | return mNewTextStart + mNewText.length(); |
| 5873 | } |
| 5874 | |
| 5875 | private int getOldTextEnd() { |
| 5876 | return mOldTextStart + mOldText.length(); |
| 5877 | } |
| 5878 | |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 5879 | @Override |
| 5880 | public void commit() { |
| 5881 | } |
| 5882 | |
| 5883 | @Override |
| 5884 | public void undo() { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5885 | if (DEBUG_UNDO) Log.d(TAG, "undo"); |
| 5886 | // Remove the new text and insert the old. |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5887 | Editor editor = getOwnerData(); |
| 5888 | Editable text = (Editable) editor.mTextView.getText(); |
| 5889 | modifyText(text, mNewTextStart, getNewTextEnd(), mOldText, mOldTextStart, |
| 5890 | mOldCursorPos); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 5891 | } |
| 5892 | |
| 5893 | @Override |
| 5894 | public void redo() { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5895 | if (DEBUG_UNDO) Log.d(TAG, "redo"); |
| 5896 | // Remove the old text and insert the new. |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5897 | Editor editor = getOwnerData(); |
| 5898 | Editable text = (Editable) editor.mTextView.getText(); |
| 5899 | modifyText(text, mOldTextStart, getOldTextEnd(), mNewText, mNewTextStart, |
| 5900 | mNewCursorPos); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 5901 | } |
| 5902 | |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5903 | /** |
| 5904 | * Attempts to merge this existing operation with a new edit. |
| 5905 | * @param edit The new edit operation. |
| 5906 | * @return If the merge succeeded, returns true. Otherwise returns false and leaves this |
| 5907 | * object unchanged. |
| 5908 | */ |
| 5909 | private boolean mergeWith(EditOperation edit) { |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5910 | if (DEBUG_UNDO) { |
| 5911 | Log.d(TAG, "mergeWith old " + this); |
| 5912 | Log.d(TAG, "mergeWith new " + edit); |
| 5913 | } |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5914 | switch (mType) { |
| 5915 | case TYPE_INSERT: |
| 5916 | return mergeInsertWith(edit); |
| 5917 | case TYPE_DELETE: |
| 5918 | return mergeDeleteWith(edit); |
| 5919 | case TYPE_REPLACE: |
| 5920 | return mergeReplaceWith(edit); |
| 5921 | default: |
| 5922 | return false; |
| 5923 | } |
| 5924 | } |
| 5925 | |
| 5926 | private boolean mergeInsertWith(EditOperation edit) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5927 | // Only merge continuous insertions. |
| 5928 | if (edit.mType != TYPE_INSERT) { |
| 5929 | return false; |
| 5930 | } |
| 5931 | // Only merge insertions that are contiguous. |
| 5932 | if (getNewTextEnd() != edit.mNewTextStart) { |
| 5933 | return false; |
| 5934 | } |
| 5935 | mNewText += edit.mNewText; |
| 5936 | mNewCursorPos = edit.mNewCursorPos; |
| 5937 | return true; |
| 5938 | } |
| 5939 | |
| 5940 | // TODO: Support forward delete. |
| 5941 | private boolean mergeDeleteWith(EditOperation edit) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5942 | // Only merge continuous deletes. |
| 5943 | if (edit.mType != TYPE_DELETE) { |
| 5944 | return false; |
| 5945 | } |
| 5946 | // Only merge deletions that are contiguous. |
| 5947 | if (mOldTextStart != edit.getOldTextEnd()) { |
| 5948 | return false; |
| 5949 | } |
| 5950 | mOldTextStart = edit.mOldTextStart; |
| 5951 | mOldText = edit.mOldText + mOldText; |
| 5952 | mNewCursorPos = edit.mNewCursorPos; |
| 5953 | return true; |
| 5954 | } |
| 5955 | |
| 5956 | private boolean mergeReplaceWith(EditOperation edit) { |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5957 | // Replacements can merge only with adjacent inserts. |
| 5958 | if (edit.mType != TYPE_INSERT || getNewTextEnd() != edit.mNewTextStart) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 5959 | return false; |
| 5960 | } |
| 5961 | mOldText += edit.mOldText; |
| 5962 | mNewText += edit.mNewText; |
| 5963 | mNewCursorPos = edit.mNewCursorPos; |
| 5964 | return true; |
| 5965 | } |
| 5966 | |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5967 | /** |
| 5968 | * Forcibly creates a single merged edit operation by simulating the entire text |
| 5969 | * contents being replaced. |
| 5970 | */ |
James Cook | 2205425 | 2015-03-25 14:04:01 -0700 | [diff] [blame] | 5971 | public void forceMergeWith(EditOperation edit) { |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5972 | if (DEBUG_UNDO) Log.d(TAG, "forceMerge"); |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 5973 | Editor editor = getOwnerData(); |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 5974 | |
| 5975 | // Copy the text of the current field. |
| 5976 | // NOTE: Using StringBuilder instead of SpannableStringBuilder would be somewhat faster, |
| 5977 | // but would require two parallel implementations of modifyText() because Editable and |
| 5978 | // StringBuilder do not share an interface for replace/delete/insert. |
| 5979 | Editable editable = (Editable) editor.mTextView.getText(); |
| 5980 | Editable originalText = new SpannableStringBuilder(editable.toString()); |
| 5981 | |
| 5982 | // Roll back the last operation. |
| 5983 | modifyText(originalText, mNewTextStart, getNewTextEnd(), mOldText, mOldTextStart, |
| 5984 | mOldCursorPos); |
| 5985 | |
| 5986 | // Clone the text again and apply the new operation. |
| 5987 | Editable finalText = new SpannableStringBuilder(editable.toString()); |
| 5988 | modifyText(finalText, edit.mOldTextStart, edit.getOldTextEnd(), edit.mNewText, |
| 5989 | edit.mNewTextStart, edit.mNewCursorPos); |
| 5990 | |
| 5991 | // Convert this operation into a non-mergeable replacement of the entire string. |
| 5992 | mType = TYPE_REPLACE; |
| 5993 | mNewText = finalText.toString(); |
| 5994 | mNewTextStart = 0; |
| 5995 | mOldText = originalText.toString(); |
| 5996 | mOldTextStart = 0; |
| 5997 | mNewCursorPos = edit.mNewCursorPos; |
| 5998 | // mOldCursorPos is unchanged. |
| 5999 | } |
| 6000 | |
| 6001 | private static void modifyText(Editable text, int deleteFrom, int deleteTo, |
| 6002 | CharSequence newText, int newTextInsertAt, int newCursorPos) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6003 | // Apply the edit if it is still valid. |
| 6004 | if (isValidRange(text, deleteFrom, deleteTo) && |
| 6005 | newTextInsertAt <= text.length() - (deleteTo - deleteFrom)) { |
| 6006 | if (deleteFrom != deleteTo) { |
| 6007 | text.delete(deleteFrom, deleteTo); |
| 6008 | } |
| 6009 | if (newText.length() != 0) { |
| 6010 | text.insert(newTextInsertAt, newText); |
| 6011 | } |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6012 | } |
James Cook | 900185d | 2015-03-10 09:48:11 -0700 | [diff] [blame] | 6013 | // Restore the cursor position. If there wasn't an old cursor (newCursorPos == -1) then |
| 6014 | // don't explicitly set it and rely on SpannableStringBuilder to position it. |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6015 | // TODO: Select all the text that was undone. |
James Cook | 900185d | 2015-03-10 09:48:11 -0700 | [diff] [blame] | 6016 | if (0 <= newCursorPos && newCursorPos <= text.length()) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6017 | Selection.setSelection(text, newCursorPos); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6018 | } |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6019 | } |
| 6020 | |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6021 | private String getTypeString() { |
| 6022 | switch (mType) { |
| 6023 | case TYPE_INSERT: |
| 6024 | return "insert"; |
| 6025 | case TYPE_DELETE: |
| 6026 | return "delete"; |
| 6027 | case TYPE_REPLACE: |
| 6028 | return "replace"; |
| 6029 | default: |
| 6030 | return ""; |
| 6031 | } |
| 6032 | } |
| 6033 | |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6034 | @Override |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6035 | public String toString() { |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6036 | return "[mType=" + getTypeString() + ", " + |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6037 | "mOldText=" + mOldText + ", " + |
| 6038 | "mOldTextStart=" + mOldTextStart + ", " + |
| 6039 | "mNewText=" + mNewText + ", " + |
| 6040 | "mNewTextStart=" + mNewTextStart + ", " + |
| 6041 | "mOldCursorPos=" + mOldCursorPos + ", " + |
| 6042 | "mNewCursorPos=" + mNewCursorPos + "]"; |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6043 | } |
| 6044 | |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6045 | public static final Parcelable.ClassLoaderCreator<EditOperation> CREATOR |
| 6046 | = new Parcelable.ClassLoaderCreator<EditOperation>() { |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 6047 | @Override |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6048 | public EditOperation createFromParcel(Parcel in) { |
| 6049 | return new EditOperation(in, null); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6050 | } |
| 6051 | |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 6052 | @Override |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6053 | public EditOperation createFromParcel(Parcel in, ClassLoader loader) { |
| 6054 | return new EditOperation(in, loader); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6055 | } |
| 6056 | |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 6057 | @Override |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6058 | public EditOperation[] newArray(int size) { |
| 6059 | return new EditOperation[size]; |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6060 | } |
| 6061 | }; |
| 6062 | } |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 6063 | |
| 6064 | /** |
| 6065 | * A helper for enabling and handling "PROCESS_TEXT" menu actions. |
| 6066 | * These allow external applications to plug into currently selected text. |
| 6067 | */ |
| 6068 | static final class ProcessTextIntentActionsHandler { |
| 6069 | |
| 6070 | private final Editor mEditor; |
| 6071 | private final TextView mTextView; |
| 6072 | private final PackageManager mPackageManager; |
| 6073 | private final SparseArray<Intent> mAccessibilityIntents = new SparseArray<Intent>(); |
| 6074 | private final SparseArray<AccessibilityNodeInfo.AccessibilityAction> mAccessibilityActions |
| 6075 | = new SparseArray<AccessibilityNodeInfo.AccessibilityAction>(); |
| 6076 | |
| 6077 | private ProcessTextIntentActionsHandler(Editor editor) { |
| 6078 | mEditor = Preconditions.checkNotNull(editor); |
| 6079 | mTextView = Preconditions.checkNotNull(mEditor.mTextView); |
| 6080 | mPackageManager = Preconditions.checkNotNull( |
| 6081 | mTextView.getContext().getPackageManager()); |
| 6082 | } |
| 6083 | |
| 6084 | /** |
| 6085 | * Adds "PROCESS_TEXT" menu items to the specified menu. |
| 6086 | */ |
| 6087 | public void onInitializeMenu(Menu menu) { |
| 6088 | int i = 0; |
| 6089 | for (ResolveInfo resolveInfo : getSupportedActivities()) { |
| 6090 | menu.add(Menu.NONE, Menu.NONE, |
| 6091 | Editor.MENU_ITEM_ORDER_PROCESS_TEXT_INTENT_ACTIONS_START + i++, |
| 6092 | getLabel(resolveInfo)) |
| 6093 | .setIntent(createProcessTextIntentForResolveInfo(resolveInfo)) |
| 6094 | .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); |
| 6095 | } |
| 6096 | } |
| 6097 | |
| 6098 | /** |
| 6099 | * Performs a "PROCESS_TEXT" action if there is one associated with the specified |
| 6100 | * menu item. |
| 6101 | * |
| 6102 | * @return True if the action was performed, false otherwise. |
| 6103 | */ |
| 6104 | public boolean performMenuItemAction(MenuItem item) { |
| 6105 | return fireIntent(item.getIntent()); |
| 6106 | } |
| 6107 | |
| 6108 | /** |
| 6109 | * Initializes and caches "PROCESS_TEXT" accessibility actions. |
| 6110 | */ |
| 6111 | public void initializeAccessibilityActions() { |
| 6112 | mAccessibilityIntents.clear(); |
| 6113 | mAccessibilityActions.clear(); |
| 6114 | int i = 0; |
| 6115 | for (ResolveInfo resolveInfo : getSupportedActivities()) { |
| 6116 | int actionId = TextView.ACCESSIBILITY_ACTION_PROCESS_TEXT_START_ID + i++; |
| 6117 | mAccessibilityActions.put( |
| 6118 | actionId, |
| 6119 | new AccessibilityNodeInfo.AccessibilityAction( |
| 6120 | actionId, getLabel(resolveInfo))); |
| 6121 | mAccessibilityIntents.put( |
| 6122 | actionId, createProcessTextIntentForResolveInfo(resolveInfo)); |
| 6123 | } |
| 6124 | } |
| 6125 | |
| 6126 | /** |
| 6127 | * Adds "PROCESS_TEXT" accessibility actions to the specified accessibility node info. |
| 6128 | * NOTE: This needs a prior call to {@link #initializeAccessibilityActions()} to make the |
| 6129 | * latest accessibility actions available for this call. |
| 6130 | */ |
| 6131 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo nodeInfo) { |
| 6132 | for (int i = 0; i < mAccessibilityActions.size(); i++) { |
| 6133 | nodeInfo.addAction(mAccessibilityActions.valueAt(i)); |
| 6134 | } |
| 6135 | } |
| 6136 | |
| 6137 | /** |
| 6138 | * Performs a "PROCESS_TEXT" action if there is one associated with the specified |
| 6139 | * accessibility action id. |
| 6140 | * |
| 6141 | * @return True if the action was performed, false otherwise. |
| 6142 | */ |
| 6143 | public boolean performAccessibilityAction(int actionId) { |
| 6144 | return fireIntent(mAccessibilityIntents.get(actionId)); |
| 6145 | } |
| 6146 | |
| 6147 | private boolean fireIntent(Intent intent) { |
| 6148 | if (intent != null && Intent.ACTION_PROCESS_TEXT.equals(intent.getAction())) { |
| 6149 | intent.putExtra(Intent.EXTRA_PROCESS_TEXT, mTextView.getSelectedText()); |
Keisuke Kuroyanagi | af4caa6 | 2016-02-29 12:53:58 -0800 | [diff] [blame] | 6150 | mEditor.mPreserveSelection = true; |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 6151 | mTextView.startActivityForResult(intent, TextView.PROCESS_TEXT_REQUEST_CODE); |
| 6152 | return true; |
| 6153 | } |
| 6154 | return false; |
| 6155 | } |
| 6156 | |
| 6157 | private List<ResolveInfo> getSupportedActivities() { |
| 6158 | PackageManager packageManager = mTextView.getContext().getPackageManager(); |
| 6159 | return packageManager.queryIntentActivities(createProcessTextIntent(), 0); |
| 6160 | } |
| 6161 | |
| 6162 | private Intent createProcessTextIntentForResolveInfo(ResolveInfo info) { |
| 6163 | return createProcessTextIntent() |
| 6164 | .putExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, !mTextView.isTextEditable()) |
| 6165 | .setClassName(info.activityInfo.packageName, info.activityInfo.name); |
| 6166 | } |
| 6167 | |
| 6168 | private Intent createProcessTextIntent() { |
| 6169 | return new Intent() |
| 6170 | .setAction(Intent.ACTION_PROCESS_TEXT) |
| 6171 | .setType("text/plain"); |
| 6172 | } |
| 6173 | |
| 6174 | private CharSequence getLabel(ResolveInfo resolveInfo) { |
| 6175 | return resolveInfo.loadLabel(mPackageManager); |
| 6176 | } |
| 6177 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6178 | } |