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; |
Mihai Popa | 3872238 | 2018-03-07 19:56:21 +0000 | [diff] [blame] | 20 | import android.animation.ValueAnimator; |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 21 | import android.annotation.IntDef; |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 22 | import android.annotation.NonNull; |
Yoshiki Iguchi | ee14772 | 2015-04-14 00:12:44 +0900 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 24 | import android.app.PendingIntent; |
| 25 | import android.app.PendingIntent.CanceledException; |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 26 | import android.app.RemoteAction; |
Artur Satayev | ed5a6ae | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 27 | import android.compat.annotation.UnsupportedAppUsage; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 28 | import android.content.ClipData; |
| 29 | import android.content.ClipData.Item; |
| 30 | import android.content.Context; |
| 31 | import android.content.Intent; |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 32 | import android.content.UndoManager; |
| 33 | import android.content.UndoOperation; |
| 34 | import android.content.UndoOwner; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 35 | import android.content.pm.PackageManager; |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 36 | import android.content.pm.ResolveInfo; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 37 | import android.content.res.TypedArray; |
| 38 | import android.graphics.Canvas; |
| 39 | import android.graphics.Color; |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 40 | import android.graphics.Matrix; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 41 | import android.graphics.Paint; |
| 42 | import android.graphics.Path; |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 43 | import android.graphics.Point; |
Mihai Popa | e301746 | 2018-03-07 12:25:21 +0000 | [diff] [blame] | 44 | import android.graphics.PointF; |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 45 | import android.graphics.RecordingCanvas; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 46 | import android.graphics.Rect; |
| 47 | import android.graphics.RectF; |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 48 | import android.graphics.RenderNode; |
Seigo Nonaka | 3ed1b39 | 2016-01-19 13:54:59 +0900 | [diff] [blame] | 49 | import android.graphics.drawable.ColorDrawable; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 50 | import android.graphics.drawable.Drawable; |
Mihai Popa | 6315a32 | 2018-10-17 17:39:57 +0100 | [diff] [blame] | 51 | import android.os.Build; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 52 | import android.os.Bundle; |
Yohei Yukawa | 23cbe85 | 2016-05-17 16:42:58 -0700 | [diff] [blame] | 53 | import android.os.LocaleList; |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 54 | import android.os.Parcel; |
| 55 | import android.os.Parcelable; |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 56 | import android.os.ParcelableParcel; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 57 | import android.os.SystemClock; |
| 58 | import android.provider.Settings; |
| 59 | import android.text.DynamicLayout; |
| 60 | import android.text.Editable; |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 61 | import android.text.InputFilter; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 62 | import android.text.InputType; |
| 63 | import android.text.Layout; |
| 64 | import android.text.ParcelableSpan; |
| 65 | import android.text.Selection; |
| 66 | import android.text.SpanWatcher; |
| 67 | import android.text.Spannable; |
| 68 | import android.text.SpannableStringBuilder; |
| 69 | import android.text.Spanned; |
| 70 | import android.text.StaticLayout; |
| 71 | import android.text.TextUtils; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 72 | import android.text.method.KeyListener; |
| 73 | import android.text.method.MetaKeyKeyListener; |
| 74 | import android.text.method.MovementMethod; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 75 | import android.text.method.WordIterator; |
| 76 | import android.text.style.EasyEditSpan; |
| 77 | import android.text.style.SuggestionRangeSpan; |
| 78 | import android.text.style.SuggestionSpan; |
| 79 | import android.text.style.TextAppearanceSpan; |
| 80 | import android.text.style.URLSpan; |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 81 | import android.util.ArraySet; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 82 | import android.util.DisplayMetrics; |
| 83 | import android.util.Log; |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 84 | import android.util.SparseArray; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 85 | import android.view.ActionMode; |
| 86 | import android.view.ActionMode.Callback; |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 87 | import android.view.ContextMenu; |
Seigo Nonaka | e9f54bf | 2016-05-12 18:18:12 +0900 | [diff] [blame] | 88 | import android.view.ContextThemeWrapper; |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 89 | import android.view.DragAndDropPermissions; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 90 | import android.view.DragEvent; |
| 91 | import android.view.Gravity; |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 92 | import android.view.HapticFeedbackConstants; |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 93 | import android.view.InputDevice; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 94 | import android.view.LayoutInflater; |
| 95 | import android.view.Menu; |
| 96 | import android.view.MenuItem; |
| 97 | import android.view.MotionEvent; |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 98 | import android.view.SubMenu; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 99 | import android.view.View; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 100 | import android.view.View.DragShadowBuilder; |
| 101 | import android.view.View.OnClickListener; |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 102 | import android.view.ViewConfiguration; |
| 103 | import android.view.ViewGroup; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 104 | import android.view.ViewGroup.LayoutParams; |
Mihai Popa | ddf9fe0 | 2018-09-28 13:54:19 +0100 | [diff] [blame] | 105 | import android.view.ViewParent; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 106 | import android.view.ViewTreeObserver; |
| 107 | import android.view.WindowManager; |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 108 | import android.view.accessibility.AccessibilityNodeInfo; |
Mihai Popa | 3872238 | 2018-03-07 19:56:21 +0000 | [diff] [blame] | 109 | import android.view.animation.LinearInterpolator; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 110 | import android.view.inputmethod.CorrectionInfo; |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 111 | import android.view.inputmethod.CursorAnchorInfo; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 112 | import android.view.inputmethod.EditorInfo; |
| 113 | import android.view.inputmethod.ExtractedText; |
| 114 | import android.view.inputmethod.ExtractedTextRequest; |
| 115 | import android.view.inputmethod.InputConnection; |
| 116 | import android.view.inputmethod.InputMethodManager; |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 117 | import android.view.textclassifier.TextClassification; |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 118 | import android.view.textclassifier.TextClassificationManager; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 119 | import android.widget.AdapterView.OnItemClickListener; |
| 120 | import android.widget.TextView.Drawables; |
| 121 | import android.widget.TextView.OnEditorActionListener; |
| 122 | |
Seigo Nonaka | a60160b | 2015-08-19 12:38:35 -0700 | [diff] [blame] | 123 | import com.android.internal.annotations.VisibleForTesting; |
Abodunrinwa Toki | 1b304e4 | 2016-11-03 23:27:58 +0000 | [diff] [blame] | 124 | import com.android.internal.logging.MetricsLogger; |
| 125 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 126 | import com.android.internal.util.ArrayUtils; |
| 127 | import com.android.internal.util.GrowingArrayUtils; |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 128 | import com.android.internal.util.Preconditions; |
Abodunrinwa Toki | 29cb768 | 2018-04-11 21:24:20 +0100 | [diff] [blame] | 129 | import com.android.internal.view.FloatingActionMode; |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 130 | import com.android.internal.widget.EditableInputConnection; |
| 131 | |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 132 | import java.lang.annotation.Retention; |
| 133 | import java.lang.annotation.RetentionPolicy; |
Andrei Stingaceanu | 2aaeefe | 2015-10-20 19:11:23 +0100 | [diff] [blame] | 134 | import java.text.BreakIterator; |
Abodunrinwa Toki | 5ba060b | 2016-05-24 18:34:40 +0100 | [diff] [blame] | 135 | import java.util.ArrayList; |
Andrei Stingaceanu | 2aaeefe | 2015-10-20 19:11:23 +0100 | [diff] [blame] | 136 | import java.util.Arrays; |
Adam Powell | 8624121 | 2019-06-10 08:38:49 -0700 | [diff] [blame] | 137 | import java.util.Collections; |
Andrei Stingaceanu | 2aaeefe | 2015-10-20 19:11:23 +0100 | [diff] [blame] | 138 | import java.util.Comparator; |
| 139 | import java.util.HashMap; |
| 140 | import java.util.List; |
Abodunrinwa Toki | 3049d8c | 2017-10-17 20:33:00 +0100 | [diff] [blame] | 141 | import java.util.Map; |
Daulet Zhanguzin | cb0d19b | 2019-12-18 15:08:09 +0000 | [diff] [blame] | 142 | import java.util.Objects; |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 143 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 144 | /** |
| 145 | * Helper class used by TextView to handle editable text views. |
| 146 | * |
| 147 | * @hide |
| 148 | */ |
| 149 | public class Editor { |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 150 | private static final String TAG = "Editor"; |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 151 | private static final boolean DEBUG_UNDO = false; |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 152 | |
| 153 | // Specifies whether to allow starting a cursor drag by dragging anywhere over the text. |
| 154 | @VisibleForTesting |
| 155 | public static boolean FLAG_ENABLE_CURSOR_DRAG = false; |
| 156 | // Specifies whether to use the magnifier when pressing the insertion or selection handles. |
Andrei Stingaceanu | 060b3d7 | 2017-10-04 11:27:08 +0100 | [diff] [blame] | 157 | private static final boolean FLAG_USE_MAGNIFIER = true; |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 158 | |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 159 | private static final int DELAY_BEFORE_HANDLE_FADES_OUT = 4000; |
| 160 | private static final int RECENT_CUT_COPY_DURATION_MS = 15 * 1000; // 15 seconds in millis |
| 161 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 162 | static final int BLINK = 500; |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 163 | private static final int DRAG_SHADOW_MAX_TEXT_LENGTH = 20; |
Mady Mellor | cc65c37 | 2015-06-17 09:25:19 -0700 | [diff] [blame] | 164 | private static final float LINE_SLOP_MULTIPLIER_FOR_HANDLEVIEWS = 0.5f; |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 165 | private static final int UNSET_X_VALUE = -1; |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 166 | private static final int UNSET_LINE = -1; |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 167 | // Tag used when the Editor maintains its own separate UndoManager. |
| 168 | private static final String UNDO_OWNER_TAG = "Editor"; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 169 | |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 170 | // Ordering constants used to place the Action Mode or context menu items in their menu. |
Abodunrinwa Toki | 6eecdc9 | 2017-04-11 05:15:25 +0100 | [diff] [blame] | 171 | private static final int MENU_ITEM_ORDER_ASSIST = 0; |
Abodunrinwa Toki | d0d9ceb | 2016-11-21 18:41:02 +0000 | [diff] [blame] | 172 | private static final int MENU_ITEM_ORDER_UNDO = 2; |
| 173 | private static final int MENU_ITEM_ORDER_REDO = 3; |
Abodunrinwa Toki | 5fedfb8 | 2017-02-06 19:34:00 +0000 | [diff] [blame] | 174 | private static final int MENU_ITEM_ORDER_CUT = 4; |
| 175 | private static final int MENU_ITEM_ORDER_COPY = 5; |
| 176 | private static final int MENU_ITEM_ORDER_PASTE = 6; |
| 177 | private static final int MENU_ITEM_ORDER_SHARE = 7; |
Abodunrinwa Toki | ea6cb12 | 2017-04-28 22:14:13 +0100 | [diff] [blame] | 178 | private static final int MENU_ITEM_ORDER_SELECT_ALL = 8; |
| 179 | private static final int MENU_ITEM_ORDER_REPLACE = 9; |
| 180 | private static final int MENU_ITEM_ORDER_AUTOFILL = 10; |
| 181 | private static final int MENU_ITEM_ORDER_PASTE_AS_PLAIN_TEXT = 11; |
Abodunrinwa Toki | 3049d8c | 2017-10-17 20:33:00 +0100 | [diff] [blame] | 182 | private static final int MENU_ITEM_ORDER_SECONDARY_ASSIST_ACTIONS_START = 50; |
Abodunrinwa Toki | 6eecdc9 | 2017-04-11 05:15:25 +0100 | [diff] [blame] | 183 | private static final int MENU_ITEM_ORDER_PROCESS_TEXT_INTENT_ACTIONS_START = 100; |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 184 | |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 185 | @IntDef({MagnifierHandleTrigger.SELECTION_START, |
| 186 | MagnifierHandleTrigger.SELECTION_END, |
| 187 | MagnifierHandleTrigger.INSERTION}) |
| 188 | @Retention(RetentionPolicy.SOURCE) |
| 189 | private @interface MagnifierHandleTrigger { |
| 190 | int INSERTION = 0; |
| 191 | int SELECTION_START = 1; |
| 192 | int SELECTION_END = 2; |
| 193 | } |
| 194 | |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 195 | @IntDef({TextActionMode.SELECTION, TextActionMode.INSERTION, TextActionMode.TEXT_LINK}) |
| 196 | @interface TextActionMode { |
| 197 | int SELECTION = 0; |
| 198 | int INSERTION = 1; |
| 199 | int TEXT_LINK = 2; |
| 200 | } |
| 201 | |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 202 | // Each Editor manages its own undo stack. |
| 203 | private final UndoManager mUndoManager = new UndoManager(); |
| 204 | private UndoOwner mUndoOwner = mUndoManager.getOwner(UNDO_OWNER_TAG, this); |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 205 | final UndoInputFilter mUndoInputFilter = new UndoInputFilter(this); |
James Cook | f1dad1e | 2015-02-27 11:00:01 -0800 | [diff] [blame] | 206 | boolean mAllowUndo = true; |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 207 | |
Abodunrinwa Toki | 54486c1 | 2017-04-19 21:02:36 +0100 | [diff] [blame] | 208 | private final MetricsLogger mMetricsLogger = new MetricsLogger(); |
| 209 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 210 | // Cursor Controllers. |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 211 | InsertionPointCursorController mInsertionPointCursorController; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 212 | SelectionModifierCursorController mSelectionModifierCursorController; |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 213 | // Action mode used when text is selected or when actions on an insertion cursor are triggered. |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 214 | private ActionMode mTextActionMode; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 215 | @UnsupportedAppUsage |
Keisuke Kuroyanagi | c5a4320 | 2016-06-10 16:27:27 +0900 | [diff] [blame] | 216 | private boolean mInsertionControllerEnabled; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 217 | @UnsupportedAppUsage |
Keisuke Kuroyanagi | c5a4320 | 2016-06-10 16:27:27 +0900 | [diff] [blame] | 218 | private boolean mSelectionControllerEnabled; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 219 | |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 220 | private final boolean mHapticTextHandleEnabled; |
| 221 | |
Mihai Popa | 3872238 | 2018-03-07 19:56:21 +0000 | [diff] [blame] | 222 | private final MagnifierMotionAnimator mMagnifierAnimator; |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 223 | private final Runnable mUpdateMagnifierRunnable = new Runnable() { |
| 224 | @Override |
| 225 | public void run() { |
Mihai Popa | 3872238 | 2018-03-07 19:56:21 +0000 | [diff] [blame] | 226 | mMagnifierAnimator.update(); |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 227 | } |
| 228 | }; |
| 229 | // Update the magnifier contents whenever anything in the view hierarchy is updated. |
| 230 | // Note: this only captures UI thread-visible changes, so it's a known issue that an animating |
| 231 | // VectorDrawable or Ripple animation will not trigger capture, since they're owned by |
| 232 | // RenderThread. |
| 233 | private final ViewTreeObserver.OnDrawListener mMagnifierOnDrawListener = |
| 234 | new ViewTreeObserver.OnDrawListener() { |
| 235 | @Override |
| 236 | public void onDraw() { |
Mihai Popa | 3872238 | 2018-03-07 19:56:21 +0000 | [diff] [blame] | 237 | if (mMagnifierAnimator != null) { |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 238 | // Posting the method will ensure that updating the magnifier contents will |
| 239 | // happen right after the rendering of the current frame. |
| 240 | mTextView.post(mUpdateMagnifierRunnable); |
| 241 | } |
| 242 | } |
| 243 | }; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 244 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 245 | // Used to highlight a word when it is corrected by the IME |
Keisuke Kuroyanagi | c5a4320 | 2016-06-10 16:27:27 +0900 | [diff] [blame] | 246 | private CorrectionHighlighter mCorrectionHighlighter; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 247 | |
| 248 | InputContentType mInputContentType; |
| 249 | InputMethodState mInputMethodState; |
| 250 | |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 251 | private static class TextRenderNode { |
Keisuke Kuroyanagi | 499c159 | 2016-09-05 17:45:30 +0900 | [diff] [blame] | 252 | // Render node has 3 recording states: |
| 253 | // 1. Recorded operations are valid. |
| 254 | // #needsRecord() returns false, but needsToBeShifted is false. |
| 255 | // 2. Recorded operations are not valid, but just the position needed to be updated. |
| 256 | // #needsRecord() returns false, but needsToBeShifted is true. |
| 257 | // 3. Recorded operations are not valid. Need to record operations. #needsRecord() returns |
| 258 | // true. |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 259 | RenderNode renderNode; |
John Reck | 7558aa7 | 2014-03-05 14:59:59 -0800 | [diff] [blame] | 260 | boolean isDirty; |
Keisuke Kuroyanagi | 499c159 | 2016-09-05 17:45:30 +0900 | [diff] [blame] | 261 | // Becomes true when recorded operations can be reused, but the position has to be updated. |
| 262 | boolean needsToBeShifted; |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 263 | public TextRenderNode(String name) { |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 264 | renderNode = RenderNode.create(name, null); |
Keisuke Kuroyanagi | 499c159 | 2016-09-05 17:45:30 +0900 | [diff] [blame] | 265 | isDirty = true; |
| 266 | needsToBeShifted = true; |
John Reck | 7558aa7 | 2014-03-05 14:59:59 -0800 | [diff] [blame] | 267 | } |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 268 | boolean needsRecord() { |
John Reck | c7ddcf3 | 2018-10-25 13:56:17 -0700 | [diff] [blame] | 269 | return isDirty || !renderNode.hasDisplayList(); |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 270 | } |
John Reck | 7558aa7 | 2014-03-05 14:59:59 -0800 | [diff] [blame] | 271 | } |
Keisuke Kuroyanagi | c5a4320 | 2016-06-10 16:27:27 +0900 | [diff] [blame] | 272 | private TextRenderNode[] mTextRenderNodes; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 273 | |
| 274 | boolean mFrozenWithFocus; |
| 275 | boolean mSelectionMoved; |
| 276 | boolean mTouchFocusSelected; |
| 277 | |
| 278 | KeyListener mKeyListener; |
| 279 | int mInputType = EditorInfo.TYPE_NULL; |
| 280 | |
| 281 | boolean mDiscardNextActionUp; |
| 282 | boolean mIgnoreActionUpEvent; |
| 283 | |
Louis Pullen-Freilich | 1c400a3 | 2019-02-05 14:35:20 +0000 | [diff] [blame] | 284 | /** |
| 285 | * To set a custom cursor, you should use {@link TextView#setTextCursorDrawable(Drawable)} |
| 286 | * or {@link TextView#setTextCursorDrawable(int)}. |
| 287 | */ |
| 288 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Mihai Popa | a4e39c4 | 2018-02-20 15:31:11 +0000 | [diff] [blame] | 289 | private long mShowCursor; |
| 290 | private boolean mRenderCursorRegardlessTiming; |
Keisuke Kuroyanagi | c5a4320 | 2016-06-10 16:27:27 +0900 | [diff] [blame] | 291 | private Blink mBlink; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 292 | |
| 293 | boolean mCursorVisible = true; |
| 294 | boolean mSelectAllOnFocus; |
| 295 | boolean mTextIsSelectable; |
| 296 | |
| 297 | CharSequence mError; |
| 298 | boolean mErrorWasChanged; |
Keisuke Kuroyanagi | c5a4320 | 2016-06-10 16:27:27 +0900 | [diff] [blame] | 299 | private ErrorPopup mErrorPopup; |
Fabrice Di Meglio | 1957d28 | 2012-10-25 17:42:39 -0700 | [diff] [blame] | 300 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 301 | /** |
| 302 | * This flag is set if the TextView tries to display an error before it |
| 303 | * is attached to the window (so its position is still unknown). |
| 304 | * It causes the error to be shown later, when onAttachedToWindow() |
| 305 | * is called. |
| 306 | */ |
Keisuke Kuroyanagi | c5a4320 | 2016-06-10 16:27:27 +0900 | [diff] [blame] | 307 | private boolean mShowErrorAfterAttach; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 308 | |
| 309 | boolean mInBatchEditControllers; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 310 | @UnsupportedAppUsage |
Gilles Debunne | 3473b2b | 2012-04-20 16:21:10 -0700 | [diff] [blame] | 311 | boolean mShowSoftInputOnFocus = true; |
Keisuke Kuroyanagi | af4caa6 | 2016-02-29 12:53:58 -0800 | [diff] [blame] | 312 | private boolean mPreserveSelection; |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 313 | private boolean mRestartActionModeOnNextRefresh; |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 314 | private boolean mRequestingLinkActionMode; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 315 | |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 316 | private SelectionActionModeHelper mSelectionActionModeHelper; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 317 | |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 318 | boolean mIsBeingLongClicked; |
| 319 | |
Keisuke Kuroyanagi | c5a4320 | 2016-06-10 16:27:27 +0900 | [diff] [blame] | 320 | private SuggestionsPopupWindow mSuggestionsPopupWindow; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 321 | SuggestionRangeSpan mSuggestionRangeSpan; |
Keisuke Kuroyanagi | c5a4320 | 2016-06-10 16:27:27 +0900 | [diff] [blame] | 322 | private Runnable mShowSuggestionRunnable; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 323 | |
Roozbeh Pournader | e06eebf | 2017-10-03 12:13:46 -0700 | [diff] [blame] | 324 | Drawable mDrawableForCursor = null; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 325 | |
Mihai Popa | 6315a32 | 2018-10-17 17:39:57 +0100 | [diff] [blame] | 326 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 327 | Drawable mSelectHandleLeft; |
| 328 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 329 | Drawable mSelectHandleRight; |
| 330 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 331 | Drawable mSelectHandleCenter; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 332 | |
| 333 | // Global listener that detects changes in the global position of the TextView |
| 334 | private PositionListener mPositionListener; |
| 335 | |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 336 | private float mContextMenuAnchorX, mContextMenuAnchorY; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 337 | Callback mCustomSelectionActionModeCallback; |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 338 | Callback mCustomInsertionActionModeCallback; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 339 | |
| 340 | // Set when this TextView gained focus with some text selected. Will start selection mode. |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 341 | @UnsupportedAppUsage |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 342 | boolean mCreatedWithASelection; |
| 343 | |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 344 | // The button state as of the last time #onTouchEvent is called. |
| 345 | private int mLastButtonState; |
| 346 | |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 347 | private final EditorTouchState mTouchState = new EditorTouchState(); |
| 348 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 349 | private Runnable mInsertionActionModeRunnable; |
Andrei Stingaceanu | fae270c | 2015-04-29 14:39:40 +0100 | [diff] [blame] | 350 | |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 351 | // The span controller helps monitoring the changes to which the Editor needs to react: |
| 352 | // - EasyEditSpans, for which we have some UI to display on attach and on hide |
| 353 | // - SelectionSpans, for which we need to call updateSelection if an IME is attached |
| 354 | private SpanController mSpanController; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 355 | |
Keisuke Kuroyanagi | c5a4320 | 2016-06-10 16:27:27 +0900 | [diff] [blame] | 356 | private WordIterator mWordIterator; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 357 | SpellChecker mSpellChecker; |
| 358 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 359 | // This word iterator is set with text and used to determine word boundaries |
| 360 | // when a user is selecting text. |
| 361 | private WordIterator mWordIteratorWithText; |
| 362 | // Indicate that the text in the word iterator needs to be updated. |
| 363 | private boolean mUpdateWordIteratorText; |
| 364 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 365 | private Rect mTempRect; |
| 366 | |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 367 | private final TextView mTextView; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 368 | |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 369 | final ProcessTextIntentActionsHandler mProcessTextIntentActionsHandler; |
| 370 | |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 371 | private final CursorAnchorInfoNotifier mCursorAnchorInfoNotifier = |
| 372 | new CursorAnchorInfoNotifier(); |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 373 | |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 374 | private final Runnable mShowFloatingToolbar = new Runnable() { |
| 375 | @Override |
| 376 | public void run() { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 377 | if (mTextActionMode != null) { |
Abodunrinwa Toki | 9e21128 | 2015-06-05 02:46:57 +0100 | [diff] [blame] | 378 | mTextActionMode.hide(0); // hide off. |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | }; |
| 382 | |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 383 | boolean mIsInsertionActionModeStartPending = false; |
| 384 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 385 | private final SuggestionHelper mSuggestionHelper = new SuggestionHelper(); |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 386 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 387 | Editor(TextView textView) { |
| 388 | mTextView = textView; |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 389 | // Synchronize the filter list, which places the undo input filter at the end. |
| 390 | mTextView.setFilters(mTextView.getFilters()); |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 391 | mProcessTextIntentActionsHandler = new ProcessTextIntentActionsHandler(this); |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 392 | mHapticTextHandleEnabled = mTextView.getContext().getResources().getBoolean( |
| 393 | com.android.internal.R.bool.config_enableHapticTextHandle); |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 394 | |
Mihai Popa | 3872238 | 2018-03-07 19:56:21 +0000 | [diff] [blame] | 395 | if (FLAG_USE_MAGNIFIER) { |
Mihai Popa | c695029 | 2018-11-15 21:32:42 +0000 | [diff] [blame] | 396 | final Magnifier magnifier = |
| 397 | Magnifier.createBuilderWithOldMagnifierDefaults(mTextView).build(); |
Mihai Popa | b6ca909 | 2018-09-24 21:14:50 +0100 | [diff] [blame] | 398 | mMagnifierAnimator = new MagnifierMotionAnimator(magnifier); |
Mihai Popa | 3872238 | 2018-03-07 19:56:21 +0000 | [diff] [blame] | 399 | } |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | ParcelableParcel saveInstanceState() { |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 403 | ParcelableParcel state = new ParcelableParcel(getClass().getClassLoader()); |
| 404 | Parcel parcel = state.getParcel(); |
| 405 | mUndoManager.saveInstanceState(parcel); |
| 406 | mUndoInputFilter.saveInstanceState(parcel); |
| 407 | return state; |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | void restoreInstanceState(ParcelableParcel state) { |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 411 | Parcel parcel = state.getParcel(); |
| 412 | mUndoManager.restoreInstanceState(parcel, state.getClassLoader()); |
| 413 | mUndoInputFilter.restoreInstanceState(parcel); |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 414 | // Re-associate this object as the owner of undo state. |
| 415 | mUndoOwner = mUndoManager.getOwner(UNDO_OWNER_TAG, this); |
| 416 | } |
| 417 | |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 418 | /** |
| 419 | * Forgets all undo and redo operations for this Editor. |
| 420 | */ |
| 421 | void forgetUndoRedo() { |
| 422 | UndoOwner[] owners = { mUndoOwner }; |
| 423 | mUndoManager.forgetUndos(owners, -1 /* all */); |
| 424 | mUndoManager.forgetRedos(owners, -1 /* all */); |
| 425 | } |
| 426 | |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 427 | boolean canUndo() { |
| 428 | UndoOwner[] owners = { mUndoOwner }; |
James Cook | f1dad1e | 2015-02-27 11:00:01 -0800 | [diff] [blame] | 429 | return mAllowUndo && mUndoManager.countUndos(owners) > 0; |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | boolean canRedo() { |
| 433 | UndoOwner[] owners = { mUndoOwner }; |
James Cook | f1dad1e | 2015-02-27 11:00:01 -0800 | [diff] [blame] | 434 | return mAllowUndo && mUndoManager.countRedos(owners) > 0; |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | void undo() { |
James Cook | f1dad1e | 2015-02-27 11:00:01 -0800 | [diff] [blame] | 438 | if (!mAllowUndo) { |
| 439 | return; |
| 440 | } |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 441 | UndoOwner[] owners = { mUndoOwner }; |
| 442 | mUndoManager.undo(owners, 1); // Undo 1 action. |
| 443 | } |
| 444 | |
| 445 | void redo() { |
James Cook | f1dad1e | 2015-02-27 11:00:01 -0800 | [diff] [blame] | 446 | if (!mAllowUndo) { |
| 447 | return; |
| 448 | } |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 449 | UndoOwner[] owners = { mUndoOwner }; |
| 450 | mUndoManager.redo(owners, 1); // Redo 1 action. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Andrei Stingaceanu | eeb9afc | 2015-05-12 12:39:07 +0100 | [diff] [blame] | 453 | void replace() { |
Keisuke Kuroyanagi | 713be06 | 2016-02-29 16:07:54 -0800 | [diff] [blame] | 454 | if (mSuggestionsPopupWindow == null) { |
| 455 | mSuggestionsPopupWindow = new SuggestionsPopupWindow(); |
| 456 | } |
| 457 | hideCursorAndSpanControllers(); |
| 458 | mSuggestionsPopupWindow.show(); |
| 459 | |
Andrei Stingaceanu | eeb9afc | 2015-05-12 12:39:07 +0100 | [diff] [blame] | 460 | int middle = (mTextView.getSelectionStart() + mTextView.getSelectionEnd()) / 2; |
Andrei Stingaceanu | eeb9afc | 2015-05-12 12:39:07 +0100 | [diff] [blame] | 461 | Selection.setSelection((Spannable) mTextView.getText(), middle); |
Andrei Stingaceanu | eeb9afc | 2015-05-12 12:39:07 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 464 | void onAttachedToWindow() { |
| 465 | if (mShowErrorAfterAttach) { |
| 466 | showError(); |
| 467 | mShowErrorAfterAttach = false; |
| 468 | } |
| 469 | |
| 470 | final ViewTreeObserver observer = mTextView.getViewTreeObserver(); |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 471 | if (observer.isAlive()) { |
| 472 | // No need to create the controller. |
| 473 | // The get method will add the listener on controller creation. |
| 474 | if (mInsertionPointCursorController != null) { |
| 475 | observer.addOnTouchModeChangeListener(mInsertionPointCursorController); |
| 476 | } |
| 477 | if (mSelectionModifierCursorController != null) { |
| 478 | mSelectionModifierCursorController.resetTouchOffsets(); |
| 479 | observer.addOnTouchModeChangeListener(mSelectionModifierCursorController); |
| 480 | } |
| 481 | if (FLAG_USE_MAGNIFIER) { |
| 482 | observer.addOnDrawListener(mMagnifierOnDrawListener); |
| 483 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 484 | } |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 485 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 486 | updateSpellCheckSpans(0, mTextView.getText().length(), |
| 487 | true /* create the spell checker if needed */); |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 488 | |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 489 | if (mTextView.hasSelection()) { |
| 490 | refreshTextActionMode(); |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 491 | } |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 492 | |
| 493 | getPositionListener().addSubscriber(mCursorAnchorInfoNotifier, true); |
Mikael Gullstrand | 5b734f2 | 2013-07-09 14:41:28 +0200 | [diff] [blame] | 494 | resumeBlink(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | void onDetachedFromWindow() { |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 498 | getPositionListener().removeSubscriber(mCursorAnchorInfoNotifier); |
| 499 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 500 | if (mError != null) { |
| 501 | hideError(); |
| 502 | } |
| 503 | |
Mikael Gullstrand | 5b734f2 | 2013-07-09 14:41:28 +0200 | [diff] [blame] | 504 | suspendBlink(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 505 | |
| 506 | if (mInsertionPointCursorController != null) { |
| 507 | mInsertionPointCursorController.onDetached(); |
| 508 | } |
| 509 | |
| 510 | if (mSelectionModifierCursorController != null) { |
| 511 | mSelectionModifierCursorController.onDetached(); |
| 512 | } |
| 513 | |
| 514 | if (mShowSuggestionRunnable != null) { |
| 515 | mTextView.removeCallbacks(mShowSuggestionRunnable); |
| 516 | } |
| 517 | |
Andrei Stingaceanu | fae270c | 2015-04-29 14:39:40 +0100 | [diff] [blame] | 518 | // Cancel the single tap delayed runnable. |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 519 | if (mInsertionActionModeRunnable != null) { |
| 520 | mTextView.removeCallbacks(mInsertionActionModeRunnable); |
Andrei Stingaceanu | fae270c | 2015-04-29 14:39:40 +0100 | [diff] [blame] | 521 | } |
| 522 | |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 523 | mTextView.removeCallbacks(mShowFloatingToolbar); |
| 524 | |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 525 | discardTextDisplayLists(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 526 | |
| 527 | if (mSpellChecker != null) { |
| 528 | mSpellChecker.closeSession(); |
| 529 | // Forces the creation of a new SpellChecker next time this window is created. |
| 530 | // Will handle the cases where the settings has been changed in the meantime. |
| 531 | mSpellChecker = null; |
| 532 | } |
| 533 | |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 534 | if (FLAG_USE_MAGNIFIER) { |
| 535 | final ViewTreeObserver observer = mTextView.getViewTreeObserver(); |
| 536 | if (observer.isAlive()) { |
| 537 | observer.removeOnDrawListener(mMagnifierOnDrawListener); |
| 538 | } |
| 539 | } |
| 540 | |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 541 | hideCursorAndSpanControllers(); |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 542 | stopTextActionModeWithPreservingSelection(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 545 | private void discardTextDisplayLists() { |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 546 | if (mTextRenderNodes != null) { |
| 547 | for (int i = 0; i < mTextRenderNodes.length; i++) { |
| 548 | RenderNode displayList = mTextRenderNodes[i] != null |
| 549 | ? mTextRenderNodes[i].renderNode : null; |
John Reck | c7ddcf3 | 2018-10-25 13:56:17 -0700 | [diff] [blame] | 550 | if (displayList != null && displayList.hasDisplayList()) { |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 551 | displayList.discardDisplayList(); |
John Reck | 7558aa7 | 2014-03-05 14:59:59 -0800 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 557 | private void showError() { |
| 558 | if (mTextView.getWindowToken() == null) { |
| 559 | mShowErrorAfterAttach = true; |
| 560 | return; |
| 561 | } |
| 562 | |
| 563 | if (mErrorPopup == null) { |
| 564 | LayoutInflater inflater = LayoutInflater.from(mTextView.getContext()); |
| 565 | final TextView err = (TextView) inflater.inflate( |
| 566 | com.android.internal.R.layout.textview_hint, null); |
| 567 | |
| 568 | final float scale = mTextView.getResources().getDisplayMetrics().density; |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 569 | mErrorPopup = |
| 570 | new ErrorPopup(err, (int) (200 * scale + 0.5f), (int) (50 * scale + 0.5f)); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 571 | mErrorPopup.setFocusable(false); |
| 572 | // The user is entering text, so the input method is needed. We |
| 573 | // don't want the popup to be displayed on top of it. |
| 574 | mErrorPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); |
| 575 | } |
| 576 | |
| 577 | TextView tv = (TextView) mErrorPopup.getContentView(); |
| 578 | chooseSize(mErrorPopup, mError, tv); |
| 579 | tv.setText(mError); |
| 580 | |
Hidehiko Tsuchiya | a0c8c1c | 2017-11-13 10:52:23 +0900 | [diff] [blame] | 581 | mErrorPopup.showAsDropDown(mTextView, getErrorX(), getErrorY(), |
| 582 | Gravity.TOP | Gravity.LEFT); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 583 | mErrorPopup.fixDirection(mErrorPopup.isAboveAnchor()); |
| 584 | } |
| 585 | |
| 586 | public void setError(CharSequence error, Drawable icon) { |
| 587 | mError = TextUtils.stringOrSpannedString(error); |
| 588 | mErrorWasChanged = true; |
Romain Guy | d1cc187 | 2012-11-05 17:43:25 -0800 | [diff] [blame] | 589 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 590 | if (mError == null) { |
Fabrice Di Meglio | 5acc379 | 2012-11-12 14:08:00 -0800 | [diff] [blame] | 591 | setErrorIcon(null); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 592 | if (mErrorPopup != null) { |
| 593 | if (mErrorPopup.isShowing()) { |
| 594 | mErrorPopup.dismiss(); |
| 595 | } |
| 596 | |
| 597 | mErrorPopup = null; |
| 598 | } |
Daniel 2 Olofsson | f4ecc55 | 2013-08-13 10:30:26 +0200 | [diff] [blame] | 599 | mShowErrorAfterAttach = false; |
Fabrice Di Meglio | 5acc379 | 2012-11-12 14:08:00 -0800 | [diff] [blame] | 600 | } else { |
Romain Guy | d1cc187 | 2012-11-05 17:43:25 -0800 | [diff] [blame] | 601 | setErrorIcon(icon); |
Fabrice Di Meglio | 5acc379 | 2012-11-12 14:08:00 -0800 | [diff] [blame] | 602 | if (mTextView.isFocused()) { |
| 603 | showError(); |
| 604 | } |
Romain Guy | d1cc187 | 2012-11-05 17:43:25 -0800 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
| 608 | private void setErrorIcon(Drawable icon) { |
Fabrice Di Meglio | bb0cbae | 2012-11-13 20:51:24 -0800 | [diff] [blame] | 609 | Drawables dr = mTextView.mDrawables; |
| 610 | if (dr == null) { |
Fabrice Di Meglio | f7a5cdf | 2013-03-15 15:36:51 -0700 | [diff] [blame] | 611 | mTextView.mDrawables = dr = new Drawables(mTextView.getContext()); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 612 | } |
Fabrice Di Meglio | bb0cbae | 2012-11-13 20:51:24 -0800 | [diff] [blame] | 613 | dr.setErrorDrawable(icon, mTextView); |
| 614 | |
| 615 | mTextView.resetResolvedDrawables(); |
| 616 | mTextView.invalidate(); |
| 617 | mTextView.requestLayout(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | private void hideError() { |
| 621 | if (mErrorPopup != null) { |
| 622 | if (mErrorPopup.isShowing()) { |
| 623 | mErrorPopup.dismiss(); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | mShowErrorAfterAttach = false; |
| 628 | } |
| 629 | |
| 630 | /** |
Fabrice Di Meglio | bb0cbae | 2012-11-13 20:51:24 -0800 | [diff] [blame] | 631 | * 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] | 632 | * at the middle of the error icon. |
| 633 | */ |
| 634 | private int getErrorX() { |
| 635 | /* |
| 636 | * The "25" is the distance between the point and the right edge |
| 637 | * of the background |
| 638 | */ |
| 639 | final float scale = mTextView.getResources().getDisplayMetrics().density; |
| 640 | |
| 641 | final Drawables dr = mTextView.mDrawables; |
Fabrice Di Meglio | bb0cbae | 2012-11-13 20:51:24 -0800 | [diff] [blame] | 642 | |
| 643 | final int layoutDirection = mTextView.getLayoutDirection(); |
| 644 | int errorX; |
| 645 | int offset; |
| 646 | switch (layoutDirection) { |
| 647 | default: |
| 648 | case View.LAYOUT_DIRECTION_LTR: |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 649 | offset = -(dr != null ? dr.mDrawableSizeRight : 0) / 2 + (int) (25 * scale + 0.5f); |
| 650 | errorX = mTextView.getWidth() - mErrorPopup.getWidth() |
| 651 | - mTextView.getPaddingRight() + offset; |
Fabrice Di Meglio | bb0cbae | 2012-11-13 20:51:24 -0800 | [diff] [blame] | 652 | break; |
| 653 | case View.LAYOUT_DIRECTION_RTL: |
| 654 | offset = (dr != null ? dr.mDrawableSizeLeft : 0) / 2 - (int) (25 * scale + 0.5f); |
| 655 | errorX = mTextView.getPaddingLeft() + offset; |
| 656 | break; |
| 657 | } |
| 658 | return errorX; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Returns the Y offset to make the pointy top of the error point |
| 663 | * at the bottom of the error icon. |
| 664 | */ |
| 665 | private int getErrorY() { |
| 666 | /* |
| 667 | * Compound, not extended, because the icon is not clipped |
| 668 | * if the text height is smaller. |
| 669 | */ |
| 670 | final int compoundPaddingTop = mTextView.getCompoundPaddingTop(); |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 671 | int vspace = mTextView.getBottom() - mTextView.getTop() |
| 672 | - mTextView.getCompoundPaddingBottom() - compoundPaddingTop; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 673 | |
| 674 | final Drawables dr = mTextView.mDrawables; |
Fabrice Di Meglio | bb0cbae | 2012-11-13 20:51:24 -0800 | [diff] [blame] | 675 | |
| 676 | final int layoutDirection = mTextView.getLayoutDirection(); |
| 677 | int height; |
| 678 | switch (layoutDirection) { |
| 679 | default: |
| 680 | case View.LAYOUT_DIRECTION_LTR: |
| 681 | height = (dr != null ? dr.mDrawableHeightRight : 0); |
| 682 | break; |
| 683 | case View.LAYOUT_DIRECTION_RTL: |
| 684 | height = (dr != null ? dr.mDrawableHeightLeft : 0); |
| 685 | break; |
| 686 | } |
| 687 | |
| 688 | int icontop = compoundPaddingTop + (vspace - height) / 2; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 689 | |
| 690 | /* |
| 691 | * The "2" is the distance between the point and the top edge |
| 692 | * of the background. |
| 693 | */ |
| 694 | final float scale = mTextView.getResources().getDisplayMetrics().density; |
Fabrice Di Meglio | bb0cbae | 2012-11-13 20:51:24 -0800 | [diff] [blame] | 695 | return icontop + height - mTextView.getHeight() - (int) (2 * scale + 0.5f); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | void createInputContentTypeIfNeeded() { |
| 699 | if (mInputContentType == null) { |
| 700 | mInputContentType = new InputContentType(); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | void createInputMethodStateIfNeeded() { |
| 705 | if (mInputMethodState == null) { |
| 706 | mInputMethodState = new InputMethodState(); |
| 707 | } |
| 708 | } |
| 709 | |
Mihai Popa | a4e39c4 | 2018-02-20 15:31:11 +0000 | [diff] [blame] | 710 | private boolean isCursorVisible() { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 711 | // The default value is true, even when there is no associated Editor |
| 712 | return mCursorVisible && mTextView.isTextEditable(); |
| 713 | } |
| 714 | |
Mihai Popa | a4e39c4 | 2018-02-20 15:31:11 +0000 | [diff] [blame] | 715 | boolean shouldRenderCursor() { |
| 716 | if (!isCursorVisible()) { |
| 717 | return false; |
| 718 | } |
| 719 | if (mRenderCursorRegardlessTiming) { |
| 720 | return true; |
| 721 | } |
| 722 | final long showCursorDelta = SystemClock.uptimeMillis() - mShowCursor; |
| 723 | return showCursorDelta % (2 * BLINK) < BLINK; |
| 724 | } |
| 725 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 726 | void prepareCursorControllers() { |
| 727 | boolean windowSupportsHandles = false; |
| 728 | |
| 729 | ViewGroup.LayoutParams params = mTextView.getRootView().getLayoutParams(); |
| 730 | if (params instanceof WindowManager.LayoutParams) { |
| 731 | WindowManager.LayoutParams windowParams = (WindowManager.LayoutParams) params; |
| 732 | windowSupportsHandles = windowParams.type < WindowManager.LayoutParams.FIRST_SUB_WINDOW |
| 733 | || windowParams.type > WindowManager.LayoutParams.LAST_SUB_WINDOW; |
| 734 | } |
| 735 | |
| 736 | boolean enabled = windowSupportsHandles && mTextView.getLayout() != null; |
| 737 | mInsertionControllerEnabled = enabled && isCursorVisible(); |
| 738 | mSelectionControllerEnabled = enabled && mTextView.textCanBeSelected(); |
| 739 | |
| 740 | if (!mInsertionControllerEnabled) { |
| 741 | hideInsertionPointCursorController(); |
| 742 | if (mInsertionPointCursorController != null) { |
| 743 | mInsertionPointCursorController.onDetached(); |
| 744 | mInsertionPointCursorController = null; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | if (!mSelectionControllerEnabled) { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 749 | stopTextActionMode(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 750 | if (mSelectionModifierCursorController != null) { |
| 751 | mSelectionModifierCursorController.onDetached(); |
| 752 | mSelectionModifierCursorController = null; |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
Seigo Nonaka | bb6a62c | 2015-03-31 21:59:30 +0900 | [diff] [blame] | 757 | void hideInsertionPointCursorController() { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 758 | if (mInsertionPointCursorController != null) { |
| 759 | mInsertionPointCursorController.hide(); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | /** |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 764 | * Hides the insertion and span controllers. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 765 | */ |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 766 | void hideCursorAndSpanControllers() { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 767 | hideCursorControllers(); |
| 768 | hideSpanControllers(); |
| 769 | } |
| 770 | |
| 771 | private void hideSpanControllers() { |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 772 | if (mSpanController != null) { |
| 773 | mSpanController.hide(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
| 777 | private void hideCursorControllers() { |
Yohei Yukawa | 85d08f1 | 2015-04-29 20:12:37 -0700 | [diff] [blame] | 778 | // When mTextView is not ExtractEditText, we need to distinguish two kinds of focus-lost. |
| 779 | // One is the true focus lost where suggestions pop-up (if any) should be dismissed, and the |
| 780 | // other is an side effect of showing the suggestions pop-up itself. We use isShowingUp() |
| 781 | // to distinguish one from the other. |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 782 | if (mSuggestionsPopupWindow != null && ((mTextView.isInExtractedMode()) |
| 783 | || !mSuggestionsPopupWindow.isShowingUp())) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 784 | // Should be done before hide insertion point controller since it triggers a show of it |
| 785 | mSuggestionsPopupWindow.hide(); |
| 786 | } |
| 787 | hideInsertionPointCursorController(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /** |
| 791 | * Create new SpellCheckSpans on the modified region. |
| 792 | */ |
| 793 | private void updateSpellCheckSpans(int start, int end, boolean createSpellChecker) { |
Satoshi Kataoka | d7429c1 | 2013-06-05 16:30:23 +0900 | [diff] [blame] | 794 | // Remove spans whose adjacent characters are text not punctuation |
| 795 | mTextView.removeAdjacentSuggestionSpans(start); |
| 796 | mTextView.removeAdjacentSuggestionSpans(end); |
| 797 | |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 798 | if (mTextView.isTextEditable() && mTextView.isSuggestionsEnabled() |
| 799 | && !(mTextView.isInExtractedMode())) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 800 | if (mSpellChecker == null && createSpellChecker) { |
| 801 | mSpellChecker = new SpellChecker(mTextView); |
| 802 | } |
| 803 | if (mSpellChecker != null) { |
| 804 | mSpellChecker.spellCheck(start, end); |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | void onScreenStateChanged(int screenState) { |
| 810 | switch (screenState) { |
| 811 | case View.SCREEN_STATE_ON: |
| 812 | resumeBlink(); |
| 813 | break; |
| 814 | case View.SCREEN_STATE_OFF: |
| 815 | suspendBlink(); |
| 816 | break; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | private void suspendBlink() { |
| 821 | if (mBlink != null) { |
| 822 | mBlink.cancel(); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | private void resumeBlink() { |
| 827 | if (mBlink != null) { |
| 828 | mBlink.uncancel(); |
| 829 | makeBlink(); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | void adjustInputType(boolean password, boolean passwordInputType, |
| 834 | boolean webPasswordInputType, boolean numberPasswordInputType) { |
| 835 | // mInputType has been set from inputType, possibly modified by mInputMethod. |
| 836 | // Specialize mInputType to [web]password if we have a text class and the original input |
| 837 | // type was a password. |
| 838 | if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { |
| 839 | if (password || passwordInputType) { |
| 840 | mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION)) |
| 841 | | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD; |
| 842 | } |
| 843 | if (webPasswordInputType) { |
| 844 | mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION)) |
| 845 | | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD; |
| 846 | } |
| 847 | } else if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_NUMBER) { |
| 848 | if (numberPasswordInputType) { |
| 849 | mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION)) |
| 850 | | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD; |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
Roozbeh Pournader | 5caf5a6 | 2017-08-22 18:08:09 -0700 | [diff] [blame] | 855 | private void chooseSize(@NonNull PopupWindow pop, @NonNull CharSequence text, |
| 856 | @NonNull TextView tv) { |
| 857 | final int wid = tv.getPaddingLeft() + tv.getPaddingRight(); |
| 858 | final int ht = tv.getPaddingTop() + tv.getPaddingBottom(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 859 | |
Roozbeh Pournader | 5caf5a6 | 2017-08-22 18:08:09 -0700 | [diff] [blame] | 860 | final int defaultWidthInPixels = mTextView.getResources().getDimensionPixelSize( |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 861 | com.android.internal.R.dimen.textview_error_popup_default_width); |
Roozbeh Pournader | 5caf5a6 | 2017-08-22 18:08:09 -0700 | [diff] [blame] | 862 | final StaticLayout l = StaticLayout.Builder.obtain(text, 0, text.length(), tv.getPaint(), |
| 863 | defaultWidthInPixels) |
| 864 | .setUseLineSpacingFromFallbacks(tv.mUseFallbackLineSpacing) |
| 865 | .build(); |
| 866 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 867 | float max = 0; |
| 868 | for (int i = 0; i < l.getLineCount(); i++) { |
| 869 | max = Math.max(max, l.getLineWidth(i)); |
| 870 | } |
| 871 | |
| 872 | /* |
| 873 | * Now set the popup size to be big enough for the text plus the border capped |
| 874 | * to DEFAULT_MAX_POPUP_WIDTH |
| 875 | */ |
| 876 | pop.setWidth(wid + (int) Math.ceil(max)); |
| 877 | pop.setHeight(ht + l.getHeight()); |
| 878 | } |
| 879 | |
| 880 | void setFrame() { |
| 881 | if (mErrorPopup != null) { |
| 882 | TextView tv = (TextView) mErrorPopup.getContentView(); |
| 883 | chooseSize(mErrorPopup, mError, tv); |
| 884 | mErrorPopup.update(mTextView, getErrorX(), getErrorY(), |
| 885 | mErrorPopup.getWidth(), mErrorPopup.getHeight()); |
| 886 | } |
| 887 | } |
| 888 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 889 | private int getWordStart(int offset) { |
| 890 | // FIXME - For this and similar methods we're not doing anything to check if there's |
| 891 | // 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] | 892 | int retOffset = getWordIteratorWithText().prevBoundary(offset); |
Mady Mellor | 58c9087 | 2015-05-12 11:09:37 -0700 | [diff] [blame] | 893 | if (getWordIteratorWithText().isOnPunctuation(retOffset)) { |
| 894 | // On punctuation boundary or within group of punctuation, find punctuation start. |
| 895 | retOffset = getWordIteratorWithText().getPunctuationBeginning(offset); |
| 896 | } else { |
| 897 | // Not on a punctuation boundary, find the word start. |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 898 | retOffset = getWordIteratorWithText().getPrevWordBeginningOnTwoWordsBoundary(offset); |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 899 | } |
Mady Mellor | 6c7b4ad | 2015-04-15 14:23:26 -0700 | [diff] [blame] | 900 | if (retOffset == BreakIterator.DONE) { |
| 901 | return offset; |
| 902 | } |
| 903 | return retOffset; |
| 904 | } |
| 905 | |
| 906 | private int getWordEnd(int offset) { |
| 907 | int retOffset = getWordIteratorWithText().nextBoundary(offset); |
Mady Mellor | 58c9087 | 2015-05-12 11:09:37 -0700 | [diff] [blame] | 908 | if (getWordIteratorWithText().isAfterPunctuation(retOffset)) { |
| 909 | // On punctuation boundary or within group of punctuation, find punctuation end. |
| 910 | retOffset = getWordIteratorWithText().getPunctuationEnd(offset); |
| 911 | } else { |
| 912 | // Not on a punctuation boundary, find the word end. |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 913 | retOffset = getWordIteratorWithText().getNextWordEndOnTwoWordBoundary(offset); |
Mady Mellor | 6c7b4ad | 2015-04-15 14:23:26 -0700 | [diff] [blame] | 914 | } |
| 915 | if (retOffset == BreakIterator.DONE) { |
| 916 | return offset; |
| 917 | } |
| 918 | return retOffset; |
| 919 | } |
| 920 | |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 921 | private boolean needsToSelectAllToSelectWordOrParagraph() { |
Andrei Stingaceanu | 47f82ae | 2015-04-28 17:43:54 +0100 | [diff] [blame] | 922 | if (mTextView.hasPasswordTransformationMethod()) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 923 | // Always select all on a password field. |
| 924 | // Cut/copy menu entries are not available for passwords, but being able to select all |
| 925 | // is however useful to delete or paste to replace the entire content. |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 926 | return true; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | int inputType = mTextView.getInputType(); |
| 930 | int klass = inputType & InputType.TYPE_MASK_CLASS; |
| 931 | int variation = inputType & InputType.TYPE_MASK_VARIATION; |
| 932 | |
| 933 | // Specific text field types: select the entire text for these |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 934 | if (klass == InputType.TYPE_CLASS_NUMBER |
| 935 | || klass == InputType.TYPE_CLASS_PHONE |
| 936 | || klass == InputType.TYPE_CLASS_DATETIME |
| 937 | || variation == InputType.TYPE_TEXT_VARIATION_URI |
| 938 | || variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS |
| 939 | || variation == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS |
| 940 | || variation == InputType.TYPE_TEXT_VARIATION_FILTER) { |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 941 | return true; |
| 942 | } |
| 943 | return false; |
| 944 | } |
| 945 | |
| 946 | /** |
| 947 | * Adjusts selection to the word under last touch offset. Return true if the operation was |
| 948 | * successfully performed. |
| 949 | */ |
Abodunrinwa Toki | 8dd3a74 | 2017-05-02 18:44:19 +0100 | [diff] [blame] | 950 | boolean selectCurrentWord() { |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 951 | if (!mTextView.canSelectText()) { |
| 952 | return false; |
| 953 | } |
| 954 | |
| 955 | if (needsToSelectAllToSelectWordOrParagraph()) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 956 | return mTextView.selectAllText(); |
| 957 | } |
| 958 | |
| 959 | long lastTouchOffsets = getLastTouchOffsets(); |
| 960 | final int minOffset = TextUtils.unpackRangeStartFromLong(lastTouchOffsets); |
| 961 | final int maxOffset = TextUtils.unpackRangeEndFromLong(lastTouchOffsets); |
| 962 | |
| 963 | // Safety check in case standard touch event handling has been bypassed |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 964 | if (minOffset < 0 || minOffset > mTextView.getText().length()) return false; |
| 965 | if (maxOffset < 0 || maxOffset > mTextView.getText().length()) return false; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 966 | |
| 967 | int selectionStart, selectionEnd; |
| 968 | |
| 969 | // If a URLSpan (web address, email, phone...) is found at that position, select it. |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 970 | URLSpan[] urlSpans = |
| 971 | ((Spanned) mTextView.getText()).getSpans(minOffset, maxOffset, URLSpan.class); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 972 | if (urlSpans.length >= 1) { |
| 973 | URLSpan urlSpan = urlSpans[0]; |
| 974 | selectionStart = ((Spanned) mTextView.getText()).getSpanStart(urlSpan); |
| 975 | selectionEnd = ((Spanned) mTextView.getText()).getSpanEnd(urlSpan); |
| 976 | } else { |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 977 | // FIXME - We should check if there's a LocaleSpan in the text, this may be |
| 978 | // something we should try handling or checking for. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 979 | final WordIterator wordIterator = getWordIterator(); |
| 980 | wordIterator.setCharSequence(mTextView.getText(), minOffset, maxOffset); |
| 981 | |
| 982 | selectionStart = wordIterator.getBeginning(minOffset); |
| 983 | selectionEnd = wordIterator.getEnd(maxOffset); |
| 984 | |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 985 | if (selectionStart == BreakIterator.DONE || selectionEnd == BreakIterator.DONE |
| 986 | || selectionStart == selectionEnd) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 987 | // 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] | 988 | long range = getCharClusterRange(minOffset); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 989 | selectionStart = TextUtils.unpackRangeStartFromLong(range); |
| 990 | selectionEnd = TextUtils.unpackRangeEndFromLong(range); |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd); |
| 995 | return selectionEnd > selectionStart; |
| 996 | } |
| 997 | |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 998 | /** |
| 999 | * Adjusts selection to the paragraph under last touch offset. Return true if the operation was |
| 1000 | * successfully performed. |
| 1001 | */ |
| 1002 | private boolean selectCurrentParagraph() { |
| 1003 | if (!mTextView.canSelectText()) { |
| 1004 | return false; |
| 1005 | } |
| 1006 | |
| 1007 | if (needsToSelectAllToSelectWordOrParagraph()) { |
| 1008 | return mTextView.selectAllText(); |
| 1009 | } |
| 1010 | |
| 1011 | long lastTouchOffsets = getLastTouchOffsets(); |
| 1012 | final int minLastTouchOffset = TextUtils.unpackRangeStartFromLong(lastTouchOffsets); |
| 1013 | final int maxLastTouchOffset = TextUtils.unpackRangeEndFromLong(lastTouchOffsets); |
| 1014 | |
| 1015 | final long paragraphsRange = getParagraphsRange(minLastTouchOffset, maxLastTouchOffset); |
| 1016 | final int start = TextUtils.unpackRangeStartFromLong(paragraphsRange); |
| 1017 | final int end = TextUtils.unpackRangeEndFromLong(paragraphsRange); |
| 1018 | if (start < end) { |
| 1019 | Selection.setSelection((Spannable) mTextView.getText(), start, end); |
| 1020 | return true; |
| 1021 | } |
| 1022 | return false; |
| 1023 | } |
| 1024 | |
| 1025 | /** |
| 1026 | * Get the minimum range of paragraphs that contains startOffset and endOffset. |
| 1027 | */ |
| 1028 | private long getParagraphsRange(int startOffset, int endOffset) { |
| 1029 | final Layout layout = mTextView.getLayout(); |
| 1030 | if (layout == null) { |
| 1031 | return TextUtils.packRangeInLong(-1, -1); |
| 1032 | } |
| 1033 | final CharSequence text = mTextView.getText(); |
| 1034 | int minLine = layout.getLineForOffset(startOffset); |
| 1035 | // Search paragraph start. |
| 1036 | while (minLine > 0) { |
| 1037 | final int prevLineEndOffset = layout.getLineEnd(minLine - 1); |
| 1038 | if (text.charAt(prevLineEndOffset - 1) == '\n') { |
| 1039 | break; |
| 1040 | } |
| 1041 | minLine--; |
| 1042 | } |
| 1043 | int maxLine = layout.getLineForOffset(endOffset); |
| 1044 | // Search paragraph end. |
| 1045 | while (maxLine < layout.getLineCount() - 1) { |
| 1046 | final int lineEndOffset = layout.getLineEnd(maxLine); |
| 1047 | if (text.charAt(lineEndOffset - 1) == '\n') { |
| 1048 | break; |
| 1049 | } |
| 1050 | maxLine++; |
| 1051 | } |
| 1052 | return TextUtils.packRangeInLong(layout.getLineStart(minLine), layout.getLineEnd(maxLine)); |
| 1053 | } |
| 1054 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1055 | void onLocaleChanged() { |
Keisuke Kuroyanagi | e0ac5ac | 2016-03-09 15:33:30 +0900 | [diff] [blame] | 1056 | // Will be re-created on demand in getWordIterator and getWordIteratorWithText with the |
| 1057 | // proper new locale |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1058 | mWordIterator = null; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 1059 | mWordIteratorWithText = null; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1062 | public WordIterator getWordIterator() { |
| 1063 | if (mWordIterator == null) { |
| 1064 | mWordIterator = new WordIterator(mTextView.getTextServicesLocale()); |
| 1065 | } |
| 1066 | return mWordIterator; |
| 1067 | } |
| 1068 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 1069 | private WordIterator getWordIteratorWithText() { |
| 1070 | if (mWordIteratorWithText == null) { |
| 1071 | mWordIteratorWithText = new WordIterator(mTextView.getTextServicesLocale()); |
| 1072 | mUpdateWordIteratorText = true; |
| 1073 | } |
| 1074 | if (mUpdateWordIteratorText) { |
| 1075 | // FIXME - Shouldn't copy all of the text as only the area of the text relevant |
| 1076 | // to the user's selection is needed. A possible solution would be to |
| 1077 | // copy some number N of characters near the selection and then when the |
| 1078 | // user approaches N then we'd do another copy of the next N characters. |
| 1079 | CharSequence text = mTextView.getText(); |
| 1080 | mWordIteratorWithText.setCharSequence(text, 0, text.length()); |
| 1081 | mUpdateWordIteratorText = false; |
| 1082 | } |
| 1083 | return mWordIteratorWithText; |
| 1084 | } |
| 1085 | |
Keisuke Kuroyanagi | 9c68b29 | 2015-04-24 18:55:56 +0900 | [diff] [blame] | 1086 | private int getNextCursorOffset(int offset, boolean findAfterGivenOffset) { |
| 1087 | final Layout layout = mTextView.getLayout(); |
| 1088 | if (layout == null) return offset; |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 1089 | return findAfterGivenOffset == layout.isRtlCharAt(offset) |
| 1090 | ? layout.getOffsetToLeftOf(offset) : layout.getOffsetToRightOf(offset); |
Keisuke Kuroyanagi | 9c68b29 | 2015-04-24 18:55:56 +0900 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | private long getCharClusterRange(int offset) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1094 | final int textLength = mTextView.getText().length(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1095 | if (offset < textLength) { |
Keisuke Kuroyanagi | 5396d7e | 2016-02-19 15:28:26 -0800 | [diff] [blame] | 1096 | final int clusterEndOffset = getNextCursorOffset(offset, true); |
| 1097 | return TextUtils.packRangeInLong( |
| 1098 | getNextCursorOffset(clusterEndOffset, false), clusterEndOffset); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1099 | } |
| 1100 | if (offset - 1 >= 0) { |
Keisuke Kuroyanagi | 5396d7e | 2016-02-19 15:28:26 -0800 | [diff] [blame] | 1101 | final int clusterStartOffset = getNextCursorOffset(offset, false); |
| 1102 | return TextUtils.packRangeInLong(clusterStartOffset, |
| 1103 | getNextCursorOffset(clusterStartOffset, true)); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1104 | } |
Keisuke Kuroyanagi | 9c68b29 | 2015-04-24 18:55:56 +0900 | [diff] [blame] | 1105 | return TextUtils.packRangeInLong(offset, offset); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | private boolean touchPositionIsInSelection() { |
| 1109 | int selectionStart = mTextView.getSelectionStart(); |
| 1110 | int selectionEnd = mTextView.getSelectionEnd(); |
| 1111 | |
| 1112 | if (selectionStart == selectionEnd) { |
| 1113 | return false; |
| 1114 | } |
| 1115 | |
| 1116 | if (selectionStart > selectionEnd) { |
| 1117 | int tmp = selectionStart; |
| 1118 | selectionStart = selectionEnd; |
| 1119 | selectionEnd = tmp; |
| 1120 | Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd); |
| 1121 | } |
| 1122 | |
| 1123 | SelectionModifierCursorController selectionController = getSelectionController(); |
| 1124 | int minOffset = selectionController.getMinTouchOffset(); |
| 1125 | int maxOffset = selectionController.getMaxTouchOffset(); |
| 1126 | |
| 1127 | return ((minOffset >= selectionStart) && (maxOffset < selectionEnd)); |
| 1128 | } |
| 1129 | |
| 1130 | private PositionListener getPositionListener() { |
| 1131 | if (mPositionListener == null) { |
| 1132 | mPositionListener = new PositionListener(); |
| 1133 | } |
| 1134 | return mPositionListener; |
| 1135 | } |
| 1136 | |
| 1137 | private interface TextViewPositionListener { |
| 1138 | public void updatePosition(int parentPositionX, int parentPositionY, |
| 1139 | boolean parentPositionChanged, boolean parentScrolled); |
| 1140 | } |
| 1141 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1142 | private boolean isOffsetVisible(int offset) { |
| 1143 | Layout layout = mTextView.getLayout(); |
Victoria Lease | b9b77ae | 2013-10-13 15:12:52 -0700 | [diff] [blame] | 1144 | if (layout == null) return false; |
| 1145 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1146 | final int line = layout.getLineForOffset(offset); |
| 1147 | final int lineBottom = layout.getLineBottom(line); |
| 1148 | final int primaryHorizontal = (int) layout.getPrimaryHorizontal(offset); |
Phil Weaver | c2e2893 | 2016-12-08 12:29:25 -0800 | [diff] [blame] | 1149 | return mTextView.isPositionVisible( |
| 1150 | primaryHorizontal + mTextView.viewportToContentHorizontalOffset(), |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1151 | lineBottom + mTextView.viewportToContentVerticalOffset()); |
| 1152 | } |
| 1153 | |
| 1154 | /** Returns true if the screen coordinates position (x,y) corresponds to a character displayed |
| 1155 | * in the view. Returns false when the position is in the empty space of left/right of text. |
| 1156 | */ |
| 1157 | private boolean isPositionOnText(float x, float y) { |
| 1158 | Layout layout = mTextView.getLayout(); |
| 1159 | if (layout == null) return false; |
| 1160 | |
| 1161 | final int line = mTextView.getLineAtCoordinate(y); |
| 1162 | x = mTextView.convertToLocalHorizontalCoordinate(x); |
| 1163 | |
| 1164 | if (x < layout.getLineLeft(line)) return false; |
| 1165 | if (x > layout.getLineRight(line)) return false; |
| 1166 | return true; |
| 1167 | } |
| 1168 | |
Keisuke Kuroyanagi | e876085 | 2015-12-21 18:30:19 +0900 | [diff] [blame] | 1169 | private void startDragAndDrop() { |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 1170 | getSelectionActionModeHelper().onSelectionDrag(); |
| 1171 | |
Keisuke Kuroyanagi | fdfc93d | 2016-03-15 14:47:08 +0900 | [diff] [blame] | 1172 | // TODO: Fix drag and drop in full screen extracted mode. |
| 1173 | if (mTextView.isInExtractedMode()) { |
| 1174 | return; |
| 1175 | } |
Keisuke Kuroyanagi | e876085 | 2015-12-21 18:30:19 +0900 | [diff] [blame] | 1176 | final int start = mTextView.getSelectionStart(); |
| 1177 | final int end = mTextView.getSelectionEnd(); |
| 1178 | CharSequence selectedText = mTextView.getTransformedText(start, end); |
| 1179 | ClipData data = ClipData.newPlainText(null, selectedText); |
| 1180 | DragLocalState localState = new DragLocalState(mTextView, start, end); |
Keisuke Kuroyanagi | 5396d7e | 2016-02-19 15:28:26 -0800 | [diff] [blame] | 1181 | mTextView.startDragAndDrop(data, getTextThumbnailBuilder(start, end), localState, |
Keisuke Kuroyanagi | e876085 | 2015-12-21 18:30:19 +0900 | [diff] [blame] | 1182 | View.DRAG_FLAG_GLOBAL); |
| 1183 | stopTextActionMode(); |
| 1184 | if (hasSelectionController()) { |
| 1185 | getSelectionController().resetTouchOffsets(); |
| 1186 | } |
| 1187 | } |
| 1188 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1189 | public boolean performLongClick(boolean handled) { |
Nikita Dubrovsky | 05cfcc8 | 2019-10-24 08:57:32 -0700 | [diff] [blame] | 1190 | if (TextView.DEBUG_CURSOR) { |
| 1191 | logCursor("performLongClick", "handled=%s", handled); |
| 1192 | } |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 1193 | // Long press in empty space moves cursor and starts the insertion action mode. |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 1194 | if (!handled && !isPositionOnText(mTouchState.getLastDownX(), mTouchState.getLastDownY()) |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 1195 | && mInsertionControllerEnabled) { |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 1196 | final int offset = mTextView.getOffsetForPosition(mTouchState.getLastDownX(), |
| 1197 | mTouchState.getLastDownY()); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1198 | Selection.setSelection((Spannable) mTextView.getText(), offset); |
Clara Bayarri | 29d2b5aa | 2015-03-13 17:41:56 +0000 | [diff] [blame] | 1199 | getInsertionController().show(); |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 1200 | mIsInsertionActionModeStartPending = true; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1201 | handled = true; |
Abodunrinwa Toki | 1b304e4 | 2016-11-03 23:27:58 +0000 | [diff] [blame] | 1202 | MetricsLogger.action( |
| 1203 | mTextView.getContext(), |
| 1204 | MetricsEvent.TEXT_LONGPRESS, |
| 1205 | TextViewMetrics.SUBTYPE_LONG_PRESS_OTHER); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1208 | if (!handled && mTextActionMode != null) { |
Andrei Stingaceanu | 2aaeefe | 2015-10-20 19:11:23 +0100 | [diff] [blame] | 1209 | if (touchPositionIsInSelection()) { |
Keisuke Kuroyanagi | e876085 | 2015-12-21 18:30:19 +0900 | [diff] [blame] | 1210 | startDragAndDrop(); |
Abodunrinwa Toki | 1b304e4 | 2016-11-03 23:27:58 +0000 | [diff] [blame] | 1211 | MetricsLogger.action( |
| 1212 | mTextView.getContext(), |
| 1213 | MetricsEvent.TEXT_LONGPRESS, |
| 1214 | TextViewMetrics.SUBTYPE_LONG_PRESS_DRAG_AND_DROP); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1215 | } else { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1216 | stopTextActionMode(); |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 1217 | selectCurrentWordAndStartDrag(); |
Abodunrinwa Toki | 1b304e4 | 2016-11-03 23:27:58 +0000 | [diff] [blame] | 1218 | MetricsLogger.action( |
| 1219 | mTextView.getContext(), |
| 1220 | MetricsEvent.TEXT_LONGPRESS, |
| 1221 | TextViewMetrics.SUBTYPE_LONG_PRESS_SELECTION); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1222 | } |
| 1223 | handled = true; |
| 1224 | } |
| 1225 | |
| 1226 | // Start a new selection |
| 1227 | if (!handled) { |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 1228 | handled = selectCurrentWordAndStartDrag(); |
Abodunrinwa Toki | 1b304e4 | 2016-11-03 23:27:58 +0000 | [diff] [blame] | 1229 | if (handled) { |
| 1230 | MetricsLogger.action( |
| 1231 | mTextView.getContext(), |
| 1232 | MetricsEvent.TEXT_LONGPRESS, |
| 1233 | TextViewMetrics.SUBTYPE_LONG_PRESS_SELECTION); |
| 1234 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | return handled; |
| 1238 | } |
| 1239 | |
Petar Å egina | 91df3f9 | 2017-08-15 16:20:43 +0100 | [diff] [blame] | 1240 | float getLastUpPositionX() { |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 1241 | return mTouchState.getLastUpX(); |
Petar Å egina | 91df3f9 | 2017-08-15 16:20:43 +0100 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | float getLastUpPositionY() { |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 1245 | return mTouchState.getLastUpY(); |
Petar Å egina | 91df3f9 | 2017-08-15 16:20:43 +0100 | [diff] [blame] | 1246 | } |
| 1247 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1248 | private long getLastTouchOffsets() { |
| 1249 | SelectionModifierCursorController selectionController = getSelectionController(); |
| 1250 | final int minOffset = selectionController.getMinTouchOffset(); |
| 1251 | final int maxOffset = selectionController.getMaxTouchOffset(); |
| 1252 | return TextUtils.packRangeInLong(minOffset, maxOffset); |
| 1253 | } |
| 1254 | |
| 1255 | void onFocusChanged(boolean focused, int direction) { |
Nikita Dubrovsky | 05cfcc8 | 2019-10-24 08:57:32 -0700 | [diff] [blame] | 1256 | if (TextView.DEBUG_CURSOR) { |
| 1257 | logCursor("onFocusChanged", "focused=%s", focused); |
| 1258 | } |
| 1259 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1260 | mShowCursor = SystemClock.uptimeMillis(); |
| 1261 | ensureEndedBatchEdit(); |
| 1262 | |
| 1263 | if (focused) { |
| 1264 | int selStart = mTextView.getSelectionStart(); |
| 1265 | int selEnd = mTextView.getSelectionEnd(); |
| 1266 | |
| 1267 | // SelectAllOnFocus fields are highlighted and not selected. Do not start text selection |
| 1268 | // mode for these, unless there was a specific selection already started. |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 1269 | final boolean isFocusHighlighted = mSelectAllOnFocus && selStart == 0 |
| 1270 | && selEnd == mTextView.getText().length(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1271 | |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 1272 | mCreatedWithASelection = mFrozenWithFocus && mTextView.hasSelection() |
| 1273 | && !isFocusHighlighted; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1274 | |
| 1275 | if (!mFrozenWithFocus || (selStart < 0 || selEnd < 0)) { |
| 1276 | // If a tap was used to give focus to that view, move cursor at tap position. |
| 1277 | // Has to be done before onTakeFocus, which can be overloaded. |
| 1278 | final int lastTapPosition = getLastTapPosition(); |
| 1279 | if (lastTapPosition >= 0) { |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 1280 | if (TextView.DEBUG_CURSOR) { |
| 1281 | logCursor("onFocusChanged", "setting cursor position: %d", lastTapPosition); |
| 1282 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1283 | Selection.setSelection((Spannable) mTextView.getText(), lastTapPosition); |
| 1284 | } |
| 1285 | |
| 1286 | // Note this may have to be moved out of the Editor class |
| 1287 | MovementMethod mMovement = mTextView.getMovementMethod(); |
| 1288 | if (mMovement != null) { |
| 1289 | mMovement.onTakeFocus(mTextView, (Spannable) mTextView.getText(), direction); |
| 1290 | } |
| 1291 | |
| 1292 | // The DecorView does not have focus when the 'Done' ExtractEditText button is |
| 1293 | // pressed. Since it is the ViewAncestor's mView, it requests focus before |
| 1294 | // ExtractEditText clears focus, which gives focus to the ExtractEditText. |
| 1295 | // This special case ensure that we keep current selection in that case. |
| 1296 | // It would be better to know why the DecorView does not have focus at that time. |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 1297 | if (((mTextView.isInExtractedMode()) || mSelectionMoved) |
| 1298 | && selStart >= 0 && selEnd >= 0) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1299 | /* |
| 1300 | * Someone intentionally set the selection, so let them |
| 1301 | * do whatever it is that they wanted to do instead of |
| 1302 | * the default on-focus behavior. We reset the selection |
| 1303 | * here instead of just skipping the onTakeFocus() call |
| 1304 | * because some movement methods do something other than |
| 1305 | * just setting the selection in theirs and we still |
| 1306 | * need to go through that path. |
| 1307 | */ |
| 1308 | Selection.setSelection((Spannable) mTextView.getText(), selStart, selEnd); |
| 1309 | } |
| 1310 | |
| 1311 | if (mSelectAllOnFocus) { |
| 1312 | mTextView.selectAllText(); |
| 1313 | } |
| 1314 | |
| 1315 | mTouchFocusSelected = true; |
| 1316 | } |
| 1317 | |
| 1318 | mFrozenWithFocus = false; |
| 1319 | mSelectionMoved = false; |
| 1320 | |
| 1321 | if (mError != null) { |
| 1322 | showError(); |
| 1323 | } |
| 1324 | |
| 1325 | makeBlink(); |
| 1326 | } else { |
| 1327 | if (mError != null) { |
| 1328 | hideError(); |
| 1329 | } |
| 1330 | // Don't leave us in the middle of a batch edit. |
| 1331 | mTextView.onEndBatchEdit(); |
| 1332 | |
Andrei Stingaceanu | b1891b3 | 2015-06-19 16:44:37 +0100 | [diff] [blame] | 1333 | if (mTextView.isInExtractedMode()) { |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 1334 | hideCursorAndSpanControllers(); |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1335 | stopTextActionModeWithPreservingSelection(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1336 | } else { |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 1337 | hideCursorAndSpanControllers(); |
Yohei Yukawa | 24df931 | 2016-03-31 17:15:23 -0700 | [diff] [blame] | 1338 | if (mTextView.isTemporarilyDetached()) { |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1339 | stopTextActionModeWithPreservingSelection(); |
| 1340 | } else { |
| 1341 | stopTextActionMode(); |
| 1342 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1343 | downgradeEasyCorrectionSpans(); |
| 1344 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1345 | // No need to create the controller |
| 1346 | if (mSelectionModifierCursorController != null) { |
| 1347 | mSelectionModifierCursorController.resetTouchOffsets(); |
| 1348 | } |
Richard Ledley | 5f2f820 | 2018-02-05 14:55:47 +0000 | [diff] [blame] | 1349 | |
| 1350 | ensureNoSelectionIfNonSelectable(); |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | private void ensureNoSelectionIfNonSelectable() { |
| 1355 | // This could be the case if a TextLink has been tapped. |
| 1356 | if (!mTextView.textCanBeSelected() && mTextView.hasSelection()) { |
| 1357 | Selection.setSelection((Spannable) mTextView.getText(), |
| 1358 | mTextView.length(), mTextView.length()); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | /** |
| 1363 | * Downgrades to simple suggestions all the easy correction spans that are not a spell check |
| 1364 | * span. |
| 1365 | */ |
| 1366 | private void downgradeEasyCorrectionSpans() { |
| 1367 | CharSequence text = mTextView.getText(); |
| 1368 | if (text instanceof Spannable) { |
| 1369 | Spannable spannable = (Spannable) text; |
| 1370 | SuggestionSpan[] suggestionSpans = spannable.getSpans(0, |
| 1371 | spannable.length(), SuggestionSpan.class); |
| 1372 | for (int i = 0; i < suggestionSpans.length; i++) { |
| 1373 | int flags = suggestionSpans[i].getFlags(); |
| 1374 | if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0 |
| 1375 | && (flags & SuggestionSpan.FLAG_MISSPELLED) == 0) { |
| 1376 | flags &= ~SuggestionSpan.FLAG_EASY_CORRECT; |
| 1377 | suggestionSpans[i].setFlags(flags); |
| 1378 | } |
| 1379 | } |
| 1380 | } |
| 1381 | } |
| 1382 | |
Abodunrinwa Toki | 78940eb | 2017-09-12 02:58:49 +0100 | [diff] [blame] | 1383 | void sendOnTextChanged(int start, int before, int after) { |
| 1384 | getSelectionActionModeHelper().onTextChanged(start, start + before); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1385 | updateSpellCheckSpans(start, start + after, false); |
| 1386 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 1387 | // Flip flag to indicate the word iterator needs to have the text reset. |
| 1388 | mUpdateWordIteratorText = true; |
| 1389 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1390 | // Hide the controllers as soon as text is modified (typing, procedural...) |
| 1391 | // We do not hide the span controllers, since they can be added when a new text is |
| 1392 | // inserted into the text view (voice IME). |
| 1393 | hideCursorControllers(); |
Keisuke Kuroyanagi | f4e347d | 2015-06-11 17:41:00 +0900 | [diff] [blame] | 1394 | // Reset drag accelerator. |
| 1395 | if (mSelectionModifierCursorController != null) { |
| 1396 | mSelectionModifierCursorController.resetTouchOffsets(); |
| 1397 | } |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1398 | stopTextActionMode(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | private int getLastTapPosition() { |
| 1402 | // No need to create the controller at that point, no last tap position saved |
| 1403 | if (mSelectionModifierCursorController != null) { |
| 1404 | int lastTapPosition = mSelectionModifierCursorController.getMinTouchOffset(); |
| 1405 | if (lastTapPosition >= 0) { |
| 1406 | // Safety check, should not be possible. |
| 1407 | if (lastTapPosition > mTextView.getText().length()) { |
| 1408 | lastTapPosition = mTextView.getText().length(); |
| 1409 | } |
| 1410 | return lastTapPosition; |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | return -1; |
| 1415 | } |
| 1416 | |
| 1417 | void onWindowFocusChanged(boolean hasWindowFocus) { |
| 1418 | if (hasWindowFocus) { |
| 1419 | if (mBlink != null) { |
| 1420 | mBlink.uncancel(); |
| 1421 | makeBlink(); |
| 1422 | } |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1423 | if (mTextView.hasSelection() && !extractedTextModeWillBeStarted()) { |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 1424 | refreshTextActionMode(); |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 1425 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1426 | } else { |
| 1427 | if (mBlink != null) { |
| 1428 | mBlink.cancel(); |
| 1429 | } |
| 1430 | if (mInputContentType != null) { |
| 1431 | mInputContentType.enterDown = false; |
| 1432 | } |
| 1433 | // Order matters! Must be done before onParentLostFocus to rely on isShowingUp |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 1434 | hideCursorAndSpanControllers(); |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 1435 | stopTextActionModeWithPreservingSelection(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1436 | if (mSuggestionsPopupWindow != null) { |
| 1437 | mSuggestionsPopupWindow.onParentLostFocus(); |
| 1438 | } |
| 1439 | |
Gilles Debunne | c72fba8 | 2012-06-26 14:47:07 -0700 | [diff] [blame] | 1440 | // Don't leave us in the middle of a batch edit. Same as in onFocusChanged |
| 1441 | ensureEndedBatchEdit(); |
Richard Ledley | 5f2f820 | 2018-02-05 14:55:47 +0000 | [diff] [blame] | 1442 | |
| 1443 | ensureNoSelectionIfNonSelectable(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1444 | } |
| 1445 | } |
| 1446 | |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 1447 | private boolean shouldFilterOutTouchEvent(MotionEvent event) { |
| 1448 | if (!event.isFromSource(InputDevice.SOURCE_MOUSE)) { |
| 1449 | return false; |
| 1450 | } |
| 1451 | final boolean primaryButtonStateChanged = |
| 1452 | ((mLastButtonState ^ event.getButtonState()) & MotionEvent.BUTTON_PRIMARY) != 0; |
| 1453 | final int action = event.getActionMasked(); |
| 1454 | if ((action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP) |
| 1455 | && !primaryButtonStateChanged) { |
| 1456 | return true; |
| 1457 | } |
| 1458 | if (action == MotionEvent.ACTION_MOVE |
| 1459 | && !event.isButtonPressed(MotionEvent.BUTTON_PRIMARY)) { |
| 1460 | return true; |
| 1461 | } |
| 1462 | return false; |
| 1463 | } |
| 1464 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1465 | void onTouchEvent(MotionEvent event) { |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 1466 | final boolean filterOutEvent = shouldFilterOutTouchEvent(event); |
| 1467 | mLastButtonState = event.getButtonState(); |
| 1468 | if (filterOutEvent) { |
| 1469 | if (event.getActionMasked() == MotionEvent.ACTION_UP) { |
| 1470 | mDiscardNextActionUp = true; |
| 1471 | } |
| 1472 | return; |
| 1473 | } |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 1474 | ViewConfiguration viewConfiguration = ViewConfiguration.get(mTextView.getContext()); |
| 1475 | mTouchState.update(event, viewConfiguration); |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 1476 | updateFloatingToolbarVisibility(event); |
| 1477 | |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 1478 | if (hasInsertionController()) { |
| 1479 | getInsertionController().onTouchEvent(event); |
| 1480 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1481 | if (hasSelectionController()) { |
| 1482 | getSelectionController().onTouchEvent(event); |
| 1483 | } |
| 1484 | |
| 1485 | if (mShowSuggestionRunnable != null) { |
| 1486 | mTextView.removeCallbacks(mShowSuggestionRunnable); |
| 1487 | mShowSuggestionRunnable = null; |
| 1488 | } |
| 1489 | |
| 1490 | if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1491 | // Reset this state; it will be re-set if super.onTouchEvent |
| 1492 | // causes focus to move to the view. |
| 1493 | mTouchFocusSelected = false; |
| 1494 | mIgnoreActionUpEvent = false; |
| 1495 | } |
| 1496 | } |
| 1497 | |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 1498 | private void updateFloatingToolbarVisibility(MotionEvent event) { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1499 | if (mTextActionMode != null) { |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 1500 | switch (event.getActionMasked()) { |
| 1501 | case MotionEvent.ACTION_MOVE: |
Abodunrinwa Toki | 4a7aeb3 | 2017-07-13 02:06:56 +0100 | [diff] [blame] | 1502 | hideFloatingToolbar(ActionMode.DEFAULT_HIDE_DURATION); |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 1503 | break; |
| 1504 | case MotionEvent.ACTION_UP: // fall through |
| 1505 | case MotionEvent.ACTION_CANCEL: |
| 1506 | showFloatingToolbar(); |
| 1507 | } |
| 1508 | } |
| 1509 | } |
| 1510 | |
Abodunrinwa Toki | 4a7aeb3 | 2017-07-13 02:06:56 +0100 | [diff] [blame] | 1511 | void hideFloatingToolbar(int duration) { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1512 | if (mTextActionMode != null) { |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 1513 | mTextView.removeCallbacks(mShowFloatingToolbar); |
Abodunrinwa Toki | 4a7aeb3 | 2017-07-13 02:06:56 +0100 | [diff] [blame] | 1514 | mTextActionMode.hide(duration); |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | private void showFloatingToolbar() { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 1519 | if (mTextActionMode != null) { |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 1520 | // Delay "show" so it doesn't interfere with click confirmations |
| 1521 | // or double-clicks that could "dismiss" the floating toolbar. |
| 1522 | int delay = ViewConfiguration.getDoubleTapTimeout(); |
| 1523 | mTextView.postDelayed(mShowFloatingToolbar, delay); |
Abodunrinwa Toki | 66c1627 | 2017-05-03 20:22:55 +0100 | [diff] [blame] | 1524 | |
| 1525 | // This classifies the text and most likely returns before the toolbar is actually |
| 1526 | // shown. If not, it will update the toolbar with the result when classification |
| 1527 | // returns. We would rather not wait for a long running classification process. |
| 1528 | invalidateActionModeAsync(); |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 1529 | } |
| 1530 | } |
| 1531 | |
Yohei Yukawa | 484d4af | 2018-09-17 16:47:08 -0700 | [diff] [blame] | 1532 | private InputMethodManager getInputMethodManager() { |
| 1533 | return mTextView.getContext().getSystemService(InputMethodManager.class); |
| 1534 | } |
| 1535 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1536 | public void beginBatchEdit() { |
| 1537 | mInBatchEditControllers = true; |
| 1538 | final InputMethodState ims = mInputMethodState; |
| 1539 | if (ims != null) { |
| 1540 | int nesting = ++ims.mBatchEditNesting; |
| 1541 | if (nesting == 1) { |
| 1542 | ims.mCursorChanged = false; |
| 1543 | ims.mChangedDelta = 0; |
| 1544 | if (ims.mContentChanged) { |
| 1545 | // We already have a pending change from somewhere else, |
| 1546 | // so turn this into a full update. |
| 1547 | ims.mChangedStart = 0; |
| 1548 | ims.mChangedEnd = mTextView.getText().length(); |
| 1549 | } else { |
| 1550 | ims.mChangedStart = EXTRACT_UNKNOWN; |
| 1551 | ims.mChangedEnd = EXTRACT_UNKNOWN; |
| 1552 | ims.mContentChanged = false; |
| 1553 | } |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 1554 | mUndoInputFilter.beginBatchEdit(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1555 | mTextView.onBeginBatchEdit(); |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | public void endBatchEdit() { |
| 1561 | mInBatchEditControllers = false; |
| 1562 | final InputMethodState ims = mInputMethodState; |
| 1563 | if (ims != null) { |
| 1564 | int nesting = --ims.mBatchEditNesting; |
| 1565 | if (nesting == 0) { |
| 1566 | finishBatchEdit(ims); |
| 1567 | } |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | void ensureEndedBatchEdit() { |
| 1572 | final InputMethodState ims = mInputMethodState; |
| 1573 | if (ims != null && ims.mBatchEditNesting != 0) { |
| 1574 | ims.mBatchEditNesting = 0; |
| 1575 | finishBatchEdit(ims); |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | void finishBatchEdit(final InputMethodState ims) { |
| 1580 | mTextView.onEndBatchEdit(); |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 1581 | mUndoInputFilter.endBatchEdit(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1582 | |
| 1583 | if (ims.mContentChanged || ims.mSelectionModeChanged) { |
| 1584 | mTextView.updateAfterEdit(); |
| 1585 | reportExtractedText(); |
| 1586 | } else if (ims.mCursorChanged) { |
Jean Chalard | c99d33f | 2013-02-28 16:39:47 -0800 | [diff] [blame] | 1587 | // Cheesy way to get us to report the current cursor location. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1588 | mTextView.invalidateCursor(); |
| 1589 | } |
Jean Chalard | c99d33f | 2013-02-28 16:39:47 -0800 | [diff] [blame] | 1590 | // sendUpdateSelection knows to avoid sending if the selection did |
| 1591 | // not actually change. |
| 1592 | sendUpdateSelection(); |
Keisuke Kuroyanagi | c6fad96 | 2016-05-02 15:11:41 +0900 | [diff] [blame] | 1593 | |
| 1594 | // Show drag handles if they were blocked by batch edit mode. |
| 1595 | if (mTextActionMode != null) { |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 1596 | final CursorController cursorController = mTextView.hasSelection() |
| 1597 | ? getSelectionController() : getInsertionController(); |
Keisuke Kuroyanagi | c6fad96 | 2016-05-02 15:11:41 +0900 | [diff] [blame] | 1598 | if (cursorController != null && !cursorController.isActive() |
| 1599 | && !cursorController.isCursorBeingModified()) { |
| 1600 | cursorController.show(); |
| 1601 | } |
| 1602 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | static final int EXTRACT_NOTHING = -2; |
| 1606 | static final int EXTRACT_UNKNOWN = -1; |
| 1607 | |
| 1608 | boolean extractText(ExtractedTextRequest request, ExtractedText outText) { |
| 1609 | return extractTextInternal(request, EXTRACT_UNKNOWN, EXTRACT_UNKNOWN, |
| 1610 | EXTRACT_UNKNOWN, outText); |
| 1611 | } |
| 1612 | |
Yoshiki Iguchi | ee14772 | 2015-04-14 00:12:44 +0900 | [diff] [blame] | 1613 | private boolean extractTextInternal(@Nullable ExtractedTextRequest request, |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1614 | int partialStartOffset, int partialEndOffset, int delta, |
Yoshiki Iguchi | ee14772 | 2015-04-14 00:12:44 +0900 | [diff] [blame] | 1615 | @Nullable ExtractedText outText) { |
| 1616 | if (request == null || outText == null) { |
| 1617 | return false; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1618 | } |
Yoshiki Iguchi | ee14772 | 2015-04-14 00:12:44 +0900 | [diff] [blame] | 1619 | |
| 1620 | final CharSequence content = mTextView.getText(); |
| 1621 | if (content == null) { |
| 1622 | return false; |
| 1623 | } |
| 1624 | |
| 1625 | if (partialStartOffset != EXTRACT_NOTHING) { |
| 1626 | final int N = content.length(); |
| 1627 | if (partialStartOffset < 0) { |
| 1628 | outText.partialStartOffset = outText.partialEndOffset = -1; |
| 1629 | partialStartOffset = 0; |
| 1630 | partialEndOffset = N; |
| 1631 | } else { |
| 1632 | // Now use the delta to determine the actual amount of text |
| 1633 | // we need. |
| 1634 | partialEndOffset += delta; |
| 1635 | // Adjust offsets to ensure we contain full spans. |
| 1636 | if (content instanceof Spanned) { |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 1637 | Spanned spanned = (Spanned) content; |
Yoshiki Iguchi | ee14772 | 2015-04-14 00:12:44 +0900 | [diff] [blame] | 1638 | Object[] spans = spanned.getSpans(partialStartOffset, |
| 1639 | partialEndOffset, ParcelableSpan.class); |
| 1640 | int i = spans.length; |
| 1641 | while (i > 0) { |
| 1642 | i--; |
| 1643 | int j = spanned.getSpanStart(spans[i]); |
| 1644 | if (j < partialStartOffset) partialStartOffset = j; |
| 1645 | j = spanned.getSpanEnd(spans[i]); |
| 1646 | if (j > partialEndOffset) partialEndOffset = j; |
| 1647 | } |
| 1648 | } |
| 1649 | outText.partialStartOffset = partialStartOffset; |
| 1650 | outText.partialEndOffset = partialEndOffset - delta; |
| 1651 | |
| 1652 | if (partialStartOffset > N) { |
| 1653 | partialStartOffset = N; |
| 1654 | } else if (partialStartOffset < 0) { |
| 1655 | partialStartOffset = 0; |
| 1656 | } |
| 1657 | if (partialEndOffset > N) { |
| 1658 | partialEndOffset = N; |
| 1659 | } else if (partialEndOffset < 0) { |
| 1660 | partialEndOffset = 0; |
| 1661 | } |
| 1662 | } |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 1663 | if ((request.flags & InputConnection.GET_TEXT_WITH_STYLES) != 0) { |
Yoshiki Iguchi | ee14772 | 2015-04-14 00:12:44 +0900 | [diff] [blame] | 1664 | outText.text = content.subSequence(partialStartOffset, |
| 1665 | partialEndOffset); |
| 1666 | } else { |
| 1667 | outText.text = TextUtils.substring(content, partialStartOffset, |
| 1668 | partialEndOffset); |
| 1669 | } |
| 1670 | } else { |
| 1671 | outText.partialStartOffset = 0; |
| 1672 | outText.partialEndOffset = 0; |
| 1673 | outText.text = ""; |
| 1674 | } |
| 1675 | outText.flags = 0; |
| 1676 | if (MetaKeyKeyListener.getMetaState(content, MetaKeyKeyListener.META_SELECTING) != 0) { |
| 1677 | outText.flags |= ExtractedText.FLAG_SELECTING; |
| 1678 | } |
| 1679 | if (mTextView.isSingleLine()) { |
| 1680 | outText.flags |= ExtractedText.FLAG_SINGLE_LINE; |
| 1681 | } |
| 1682 | outText.startOffset = 0; |
| 1683 | outText.selectionStart = mTextView.getSelectionStart(); |
| 1684 | outText.selectionEnd = mTextView.getSelectionEnd(); |
Clara Bayarri | d8c5e7f | 2017-07-24 15:29:58 +0100 | [diff] [blame] | 1685 | outText.hint = mTextView.getHint(); |
Yoshiki Iguchi | ee14772 | 2015-04-14 00:12:44 +0900 | [diff] [blame] | 1686 | return true; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | boolean reportExtractedText() { |
| 1690 | final Editor.InputMethodState ims = mInputMethodState; |
Clara Bayarri | d8c5e7f | 2017-07-24 15:29:58 +0100 | [diff] [blame] | 1691 | if (ims == null) { |
| 1692 | return false; |
| 1693 | } |
Clara Bayarri | 038f7a8 | 2018-06-04 15:00:07 +0100 | [diff] [blame] | 1694 | final boolean wasContentChanged = ims.mContentChanged; |
| 1695 | if (!wasContentChanged && !ims.mSelectionModeChanged) { |
| 1696 | return false; |
| 1697 | } |
| 1698 | ims.mContentChanged = false; |
Clara Bayarri | d8c5e7f | 2017-07-24 15:29:58 +0100 | [diff] [blame] | 1699 | ims.mSelectionModeChanged = false; |
| 1700 | final ExtractedTextRequest req = ims.mExtractedTextRequest; |
| 1701 | if (req == null) { |
| 1702 | return false; |
| 1703 | } |
Yohei Yukawa | 484d4af | 2018-09-17 16:47:08 -0700 | [diff] [blame] | 1704 | final InputMethodManager imm = getInputMethodManager(); |
Clara Bayarri | d8c5e7f | 2017-07-24 15:29:58 +0100 | [diff] [blame] | 1705 | if (imm == null) { |
| 1706 | return false; |
| 1707 | } |
| 1708 | if (TextView.DEBUG_EXTRACT) { |
| 1709 | Log.v(TextView.LOG_TAG, "Retrieving extracted start=" |
| 1710 | + ims.mChangedStart |
| 1711 | + " end=" + ims.mChangedEnd |
| 1712 | + " delta=" + ims.mChangedDelta); |
| 1713 | } |
Clara Bayarri | 038f7a8 | 2018-06-04 15:00:07 +0100 | [diff] [blame] | 1714 | if (ims.mChangedStart < 0 && !wasContentChanged) { |
Clara Bayarri | d8c5e7f | 2017-07-24 15:29:58 +0100 | [diff] [blame] | 1715 | ims.mChangedStart = EXTRACT_NOTHING; |
| 1716 | } |
| 1717 | if (extractTextInternal(req, ims.mChangedStart, ims.mChangedEnd, |
| 1718 | ims.mChangedDelta, ims.mExtractedText)) { |
| 1719 | if (TextView.DEBUG_EXTRACT) { |
| 1720 | Log.v(TextView.LOG_TAG, |
| 1721 | "Reporting extracted start=" |
| 1722 | + ims.mExtractedText.partialStartOffset |
| 1723 | + " end=" + ims.mExtractedText.partialEndOffset |
| 1724 | + ": " + ims.mExtractedText.text); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1725 | } |
Clara Bayarri | d8c5e7f | 2017-07-24 15:29:58 +0100 | [diff] [blame] | 1726 | |
| 1727 | imm.updateExtractedText(mTextView, req.token, ims.mExtractedText); |
| 1728 | ims.mChangedStart = EXTRACT_UNKNOWN; |
| 1729 | ims.mChangedEnd = EXTRACT_UNKNOWN; |
| 1730 | ims.mChangedDelta = 0; |
| 1731 | ims.mContentChanged = false; |
| 1732 | return true; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1733 | } |
| 1734 | return false; |
| 1735 | } |
| 1736 | |
Jean Chalard | df7c72f | 2013-02-28 15:28:54 -0800 | [diff] [blame] | 1737 | private void sendUpdateSelection() { |
| 1738 | if (null != mInputMethodState && mInputMethodState.mBatchEditNesting <= 0) { |
Yohei Yukawa | 484d4af | 2018-09-17 16:47:08 -0700 | [diff] [blame] | 1739 | final InputMethodManager imm = getInputMethodManager(); |
Jean Chalard | df7c72f | 2013-02-28 15:28:54 -0800 | [diff] [blame] | 1740 | if (null != imm) { |
| 1741 | final int selectionStart = mTextView.getSelectionStart(); |
| 1742 | final int selectionEnd = mTextView.getSelectionEnd(); |
| 1743 | int candStart = -1; |
| 1744 | int candEnd = -1; |
| 1745 | if (mTextView.getText() instanceof Spannable) { |
| 1746 | final Spannable sp = (Spannable) mTextView.getText(); |
| 1747 | candStart = EditableInputConnection.getComposingSpanStart(sp); |
| 1748 | candEnd = EditableInputConnection.getComposingSpanEnd(sp); |
| 1749 | } |
Jean Chalard | c99d33f | 2013-02-28 16:39:47 -0800 | [diff] [blame] | 1750 | // InputMethodManager#updateSelection skips sending the message if |
| 1751 | // 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] | 1752 | imm.updateSelection(mTextView, |
| 1753 | selectionStart, selectionEnd, candStart, candEnd); |
| 1754 | } |
| 1755 | } |
| 1756 | } |
| 1757 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1758 | void onDraw(Canvas canvas, Layout layout, Path highlight, Paint highlightPaint, |
| 1759 | int cursorOffsetVertical) { |
| 1760 | final int selectionStart = mTextView.getSelectionStart(); |
| 1761 | final int selectionEnd = mTextView.getSelectionEnd(); |
| 1762 | |
| 1763 | final InputMethodState ims = mInputMethodState; |
| 1764 | if (ims != null && ims.mBatchEditNesting == 0) { |
Yohei Yukawa | 484d4af | 2018-09-17 16:47:08 -0700 | [diff] [blame] | 1765 | InputMethodManager imm = getInputMethodManager(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1766 | if (imm != null) { |
| 1767 | if (imm.isActive(mTextView)) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1768 | if (ims.mContentChanged || ims.mSelectionModeChanged) { |
| 1769 | // We are in extract mode and the content has changed |
| 1770 | // in some way... just report complete new text to the |
| 1771 | // input method. |
Yohei Yukawa | b6bec1a | 2015-05-01 16:18:25 -0700 | [diff] [blame] | 1772 | reportExtractedText(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1773 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1774 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | if (mCorrectionHighlighter != null) { |
| 1779 | mCorrectionHighlighter.draw(canvas, cursorOffsetVertical); |
| 1780 | } |
| 1781 | |
Roozbeh Pournader | e06eebf | 2017-10-03 12:13:46 -0700 | [diff] [blame] | 1782 | if (highlight != null && selectionStart == selectionEnd && mDrawableForCursor != null) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1783 | drawCursor(canvas, cursorOffsetVertical); |
| 1784 | // Rely on the drawable entirely, do not draw the cursor line. |
| 1785 | // Has to be done after the IMM related code above which relies on the highlight. |
| 1786 | highlight = null; |
| 1787 | } |
| 1788 | |
Jan Althaus | 80620c5 | 2018-02-02 17:39:22 +0100 | [diff] [blame] | 1789 | if (mSelectionActionModeHelper != null) { |
| 1790 | mSelectionActionModeHelper.onDraw(canvas); |
| 1791 | if (mSelectionActionModeHelper.isDrawingHighlight()) { |
| 1792 | highlight = null; |
| 1793 | } |
| 1794 | } |
| 1795 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1796 | if (mTextView.canHaveDisplayList() && canvas.isHardwareAccelerated()) { |
| 1797 | drawHardwareAccelerated(canvas, layout, highlight, highlightPaint, |
| 1798 | cursorOffsetVertical); |
| 1799 | } else { |
| 1800 | layout.draw(canvas, highlight, highlightPaint, cursorOffsetVertical); |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | private void drawHardwareAccelerated(Canvas canvas, Layout layout, Path highlight, |
| 1805 | Paint highlightPaint, int cursorOffsetVertical) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1806 | final long lineRange = layout.getLineRangeForDraw(canvas); |
| 1807 | int firstLine = TextUtils.unpackRangeStartFromLong(lineRange); |
| 1808 | int lastLine = TextUtils.unpackRangeEndFromLong(lineRange); |
| 1809 | if (lastLine < 0) return; |
| 1810 | |
| 1811 | layout.drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical, |
| 1812 | firstLine, lastLine); |
| 1813 | |
| 1814 | if (layout instanceof DynamicLayout) { |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 1815 | if (mTextRenderNodes == null) { |
| 1816 | mTextRenderNodes = ArrayUtils.emptyArray(TextRenderNode.class); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | DynamicLayout dynamicLayout = (DynamicLayout) layout; |
Gilles Debunne | 157aafc | 2012-04-19 17:21:57 -0700 | [diff] [blame] | 1820 | int[] blockEndLines = dynamicLayout.getBlockEndLines(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1821 | int[] blockIndices = dynamicLayout.getBlockIndices(); |
| 1822 | final int numberOfBlocks = dynamicLayout.getNumberOfBlocks(); |
Sangkyu Lee | 955beb2 | 2012-12-10 15:47:00 +0900 | [diff] [blame] | 1823 | final int indexFirstChangedBlock = dynamicLayout.getIndexFirstChangedBlock(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1824 | |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 1825 | final ArraySet<Integer> blockSet = dynamicLayout.getBlocksAlwaysNeedToBeRedrawn(); |
| 1826 | if (blockSet != null) { |
| 1827 | for (int i = 0; i < blockSet.size(); i++) { |
| 1828 | final int blockIndex = dynamicLayout.getBlockIndex(blockSet.valueAt(i)); |
| 1829 | if (blockIndex != DynamicLayout.INVALID_BLOCK_INDEX |
| 1830 | && mTextRenderNodes[blockIndex] != null) { |
| 1831 | mTextRenderNodes[blockIndex].needsToBeShifted = true; |
| 1832 | } |
| 1833 | } |
| 1834 | } |
| 1835 | |
Keisuke Kuroyanagi | 499c159 | 2016-09-05 17:45:30 +0900 | [diff] [blame] | 1836 | int startBlock = Arrays.binarySearch(blockEndLines, 0, numberOfBlocks, firstLine); |
| 1837 | if (startBlock < 0) { |
| 1838 | startBlock = -(startBlock + 1); |
| 1839 | } |
| 1840 | startBlock = Math.min(indexFirstChangedBlock, startBlock); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1841 | |
Keisuke Kuroyanagi | 499c159 | 2016-09-05 17:45:30 +0900 | [diff] [blame] | 1842 | int startIndexToFindAvailableRenderNode = 0; |
| 1843 | int lastIndex = numberOfBlocks; |
| 1844 | |
| 1845 | for (int i = startBlock; i < numberOfBlocks; i++) { |
| 1846 | final int blockIndex = blockIndices[i]; |
| 1847 | if (i >= indexFirstChangedBlock |
| 1848 | && blockIndex != DynamicLayout.INVALID_BLOCK_INDEX |
| 1849 | && mTextRenderNodes[blockIndex] != null) { |
| 1850 | mTextRenderNodes[blockIndex].needsToBeShifted = true; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1851 | } |
Keisuke Kuroyanagi | 499c159 | 2016-09-05 17:45:30 +0900 | [diff] [blame] | 1852 | if (blockEndLines[i] < firstLine) { |
| 1853 | // Blocks in [indexFirstChangedBlock, firstLine) are not redrawn here. They will |
| 1854 | // be redrawn after they get scrolled into drawing range. |
| 1855 | continue; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1856 | } |
Keisuke Kuroyanagi | 499c159 | 2016-09-05 17:45:30 +0900 | [diff] [blame] | 1857 | startIndexToFindAvailableRenderNode = drawHardwareAcceleratedInner(canvas, layout, |
| 1858 | highlight, highlightPaint, cursorOffsetVertical, blockEndLines, |
| 1859 | blockIndices, i, numberOfBlocks, startIndexToFindAvailableRenderNode); |
| 1860 | if (blockEndLines[i] >= lastLine) { |
| 1861 | lastIndex = Math.max(indexFirstChangedBlock, i + 1); |
| 1862 | break; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1863 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1864 | } |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 1865 | if (blockSet != null) { |
| 1866 | for (int i = 0; i < blockSet.size(); i++) { |
| 1867 | final int block = blockSet.valueAt(i); |
| 1868 | final int blockIndex = dynamicLayout.getBlockIndex(block); |
| 1869 | if (blockIndex == DynamicLayout.INVALID_BLOCK_INDEX |
| 1870 | || mTextRenderNodes[blockIndex] == null |
| 1871 | || mTextRenderNodes[blockIndex].needsToBeShifted) { |
| 1872 | startIndexToFindAvailableRenderNode = drawHardwareAcceleratedInner(canvas, |
| 1873 | layout, highlight, highlightPaint, cursorOffsetVertical, |
| 1874 | blockEndLines, blockIndices, block, numberOfBlocks, |
| 1875 | startIndexToFindAvailableRenderNode); |
| 1876 | } |
| 1877 | } |
| 1878 | } |
Sangkyu Lee | 955beb2 | 2012-12-10 15:47:00 +0900 | [diff] [blame] | 1879 | |
Keisuke Kuroyanagi | 499c159 | 2016-09-05 17:45:30 +0900 | [diff] [blame] | 1880 | dynamicLayout.setIndexFirstChangedBlock(lastIndex); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1881 | } else { |
| 1882 | // Boring layout is used for empty and hint text |
| 1883 | layout.drawText(canvas, firstLine, lastLine); |
| 1884 | } |
| 1885 | } |
| 1886 | |
Keisuke Kuroyanagi | 499c159 | 2016-09-05 17:45:30 +0900 | [diff] [blame] | 1887 | private int drawHardwareAcceleratedInner(Canvas canvas, Layout layout, Path highlight, |
| 1888 | Paint highlightPaint, int cursorOffsetVertical, int[] blockEndLines, |
| 1889 | int[] blockIndices, int blockInfoIndex, int numberOfBlocks, |
| 1890 | int startIndexToFindAvailableRenderNode) { |
| 1891 | final int blockEndLine = blockEndLines[blockInfoIndex]; |
| 1892 | int blockIndex = blockIndices[blockInfoIndex]; |
| 1893 | |
| 1894 | final boolean blockIsInvalid = blockIndex == DynamicLayout.INVALID_BLOCK_INDEX; |
| 1895 | if (blockIsInvalid) { |
| 1896 | blockIndex = getAvailableDisplayListIndex(blockIndices, numberOfBlocks, |
| 1897 | startIndexToFindAvailableRenderNode); |
| 1898 | // Note how dynamic layout's internal block indices get updated from Editor |
| 1899 | blockIndices[blockInfoIndex] = blockIndex; |
| 1900 | if (mTextRenderNodes[blockIndex] != null) { |
| 1901 | mTextRenderNodes[blockIndex].isDirty = true; |
| 1902 | } |
| 1903 | startIndexToFindAvailableRenderNode = blockIndex + 1; |
| 1904 | } |
| 1905 | |
| 1906 | if (mTextRenderNodes[blockIndex] == null) { |
| 1907 | mTextRenderNodes[blockIndex] = new TextRenderNode("Text " + blockIndex); |
| 1908 | } |
| 1909 | |
| 1910 | final boolean blockDisplayListIsInvalid = mTextRenderNodes[blockIndex].needsRecord(); |
| 1911 | RenderNode blockDisplayList = mTextRenderNodes[blockIndex].renderNode; |
| 1912 | if (mTextRenderNodes[blockIndex].needsToBeShifted || blockDisplayListIsInvalid) { |
| 1913 | final int blockBeginLine = blockInfoIndex == 0 ? |
| 1914 | 0 : blockEndLines[blockInfoIndex - 1] + 1; |
| 1915 | final int top = layout.getLineTop(blockBeginLine); |
| 1916 | final int bottom = layout.getLineBottom(blockEndLine); |
| 1917 | int left = 0; |
| 1918 | int right = mTextView.getWidth(); |
| 1919 | if (mTextView.getHorizontallyScrolling()) { |
| 1920 | float min = Float.MAX_VALUE; |
| 1921 | float max = Float.MIN_VALUE; |
| 1922 | for (int line = blockBeginLine; line <= blockEndLine; line++) { |
| 1923 | min = Math.min(min, layout.getLineLeft(line)); |
| 1924 | max = Math.max(max, layout.getLineRight(line)); |
| 1925 | } |
| 1926 | left = (int) min; |
| 1927 | right = (int) (max + 0.5f); |
| 1928 | } |
| 1929 | |
| 1930 | // Rebuild display list if it is invalid |
| 1931 | if (blockDisplayListIsInvalid) { |
John Reck | e57475e | 2019-02-20 17:39:52 -0800 | [diff] [blame] | 1932 | final RecordingCanvas recordingCanvas = blockDisplayList.beginRecording( |
Keisuke Kuroyanagi | 499c159 | 2016-09-05 17:45:30 +0900 | [diff] [blame] | 1933 | right - left, bottom - top); |
| 1934 | try { |
| 1935 | // drawText is always relative to TextView's origin, this translation |
| 1936 | // brings this range of text back to the top left corner of the viewport |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 1937 | recordingCanvas.translate(-left, -top); |
| 1938 | layout.drawText(recordingCanvas, blockBeginLine, blockEndLine); |
Keisuke Kuroyanagi | 499c159 | 2016-09-05 17:45:30 +0900 | [diff] [blame] | 1939 | mTextRenderNodes[blockIndex].isDirty = false; |
| 1940 | // No need to untranslate, previous context is popped after |
| 1941 | // drawDisplayList |
| 1942 | } finally { |
John Reck | e57475e | 2019-02-20 17:39:52 -0800 | [diff] [blame] | 1943 | blockDisplayList.endRecording(); |
Keisuke Kuroyanagi | 499c159 | 2016-09-05 17:45:30 +0900 | [diff] [blame] | 1944 | // Same as drawDisplayList below, handled by our TextView's parent |
| 1945 | blockDisplayList.setClipToBounds(false); |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | // Valid display list only needs to update its drawing location. |
| 1950 | blockDisplayList.setLeftTopRightBottom(left, top, right, bottom); |
| 1951 | mTextRenderNodes[blockIndex].needsToBeShifted = false; |
| 1952 | } |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 1953 | ((RecordingCanvas) canvas).drawRenderNode(blockDisplayList); |
Keisuke Kuroyanagi | 499c159 | 2016-09-05 17:45:30 +0900 | [diff] [blame] | 1954 | return startIndexToFindAvailableRenderNode; |
| 1955 | } |
| 1956 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1957 | private int getAvailableDisplayListIndex(int[] blockIndices, int numberOfBlocks, |
| 1958 | int searchStartIndex) { |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 1959 | int length = mTextRenderNodes.length; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1960 | for (int i = searchStartIndex; i < length; i++) { |
| 1961 | boolean blockIndexFound = false; |
| 1962 | for (int j = 0; j < numberOfBlocks; j++) { |
| 1963 | if (blockIndices[j] == i) { |
| 1964 | blockIndexFound = true; |
| 1965 | break; |
| 1966 | } |
| 1967 | } |
| 1968 | if (blockIndexFound) continue; |
| 1969 | return i; |
| 1970 | } |
| 1971 | |
| 1972 | // No available index found, the pool has to grow |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 1973 | mTextRenderNodes = GrowingArrayUtils.append(mTextRenderNodes, length, null); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1974 | return length; |
| 1975 | } |
| 1976 | |
| 1977 | private void drawCursor(Canvas canvas, int cursorOffsetVertical) { |
| 1978 | final boolean translate = cursorOffsetVertical != 0; |
| 1979 | if (translate) canvas.translate(0, cursorOffsetVertical); |
Roozbeh Pournader | e06eebf | 2017-10-03 12:13:46 -0700 | [diff] [blame] | 1980 | if (mDrawableForCursor != null) { |
| 1981 | mDrawableForCursor.draw(canvas); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 1982 | } |
| 1983 | if (translate) canvas.translate(0, -cursorOffsetVertical); |
| 1984 | } |
| 1985 | |
Keisuke Kuroyanagi | c14e127 | 2016-04-05 15:39:24 +0900 | [diff] [blame] | 1986 | void invalidateHandlesAndActionMode() { |
| 1987 | if (mSelectionModifierCursorController != null) { |
| 1988 | mSelectionModifierCursorController.invalidateHandles(); |
| 1989 | } |
| 1990 | if (mInsertionPointCursorController != null) { |
| 1991 | mInsertionPointCursorController.invalidateHandle(); |
| 1992 | } |
| 1993 | if (mTextActionMode != null) { |
Abodunrinwa Toki | 66c1627 | 2017-05-03 20:22:55 +0100 | [diff] [blame] | 1994 | invalidateActionMode(); |
Keisuke Kuroyanagi | c14e127 | 2016-04-05 15:39:24 +0900 | [diff] [blame] | 1995 | } |
| 1996 | } |
| 1997 | |
Gilles Debunne | ebc86af | 2012-04-20 15:10:47 -0700 | [diff] [blame] | 1998 | /** |
| 1999 | * Invalidates all the sub-display lists that overlap the specified character range |
| 2000 | */ |
| 2001 | void invalidateTextDisplayList(Layout layout, int start, int end) { |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 2002 | if (mTextRenderNodes != null && layout instanceof DynamicLayout) { |
Gilles Debunne | ebc86af | 2012-04-20 15:10:47 -0700 | [diff] [blame] | 2003 | final int firstLine = layout.getLineForOffset(start); |
| 2004 | final int lastLine = layout.getLineForOffset(end); |
| 2005 | |
| 2006 | DynamicLayout dynamicLayout = (DynamicLayout) layout; |
| 2007 | int[] blockEndLines = dynamicLayout.getBlockEndLines(); |
| 2008 | int[] blockIndices = dynamicLayout.getBlockIndices(); |
| 2009 | final int numberOfBlocks = dynamicLayout.getNumberOfBlocks(); |
| 2010 | |
| 2011 | int i = 0; |
| 2012 | // Skip the blocks before firstLine |
| 2013 | while (i < numberOfBlocks) { |
| 2014 | if (blockEndLines[i] >= firstLine) break; |
| 2015 | i++; |
| 2016 | } |
| 2017 | |
| 2018 | // Invalidate all subsequent blocks until lastLine is passed |
| 2019 | while (i < numberOfBlocks) { |
| 2020 | final int blockIndex = blockIndices[i]; |
| 2021 | if (blockIndex != DynamicLayout.INVALID_BLOCK_INDEX) { |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 2022 | mTextRenderNodes[blockIndex].isDirty = true; |
Gilles Debunne | ebc86af | 2012-04-20 15:10:47 -0700 | [diff] [blame] | 2023 | } |
| 2024 | if (blockEndLines[i] >= lastLine) break; |
| 2025 | i++; |
| 2026 | } |
| 2027 | } |
| 2028 | } |
| 2029 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 2030 | @UnsupportedAppUsage |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2031 | void invalidateTextDisplayList() { |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 2032 | if (mTextRenderNodes != null) { |
| 2033 | for (int i = 0; i < mTextRenderNodes.length; i++) { |
| 2034 | if (mTextRenderNodes[i] != null) mTextRenderNodes[i].isDirty = true; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2035 | } |
| 2036 | } |
| 2037 | } |
| 2038 | |
Roozbeh Pournader | 9c13307 | 2017-07-26 22:36:27 -0700 | [diff] [blame] | 2039 | void updateCursorPosition() { |
Mihai Popa | 6c7ad1d | 2018-12-04 15:45:00 +0000 | [diff] [blame] | 2040 | loadCursorDrawable(); |
| 2041 | if (mDrawableForCursor == null) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2042 | return; |
| 2043 | } |
| 2044 | |
Roozbeh Pournader | 9c13307 | 2017-07-26 22:36:27 -0700 | [diff] [blame] | 2045 | final Layout layout = mTextView.getLayout(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2046 | final int offset = mTextView.getSelectionStart(); |
| 2047 | final int line = layout.getLineForOffset(offset); |
| 2048 | final int top = layout.getLineTop(line); |
Siyamed Sinir | a60b59d | 2017-07-26 09:26:41 -0700 | [diff] [blame] | 2049 | final int bottom = layout.getLineBottomWithoutSpacing(line); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2050 | |
Roozbeh Pournader | 9c13307 | 2017-07-26 22:36:27 -0700 | [diff] [blame] | 2051 | final boolean clamped = layout.shouldClampCursor(line); |
| 2052 | updateCursorPosition(top, bottom, layout.getPrimaryHorizontal(offset, clamped)); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2053 | } |
| 2054 | |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2055 | void refreshTextActionMode() { |
| 2056 | if (extractedTextModeWillBeStarted()) { |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 2057 | mRestartActionModeOnNextRefresh = false; |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2058 | return; |
| 2059 | } |
| 2060 | final boolean hasSelection = mTextView.hasSelection(); |
| 2061 | final SelectionModifierCursorController selectionController = getSelectionController(); |
| 2062 | final InsertionPointCursorController insertionController = getInsertionController(); |
| 2063 | if ((selectionController != null && selectionController.isCursorBeingModified()) |
| 2064 | || (insertionController != null && insertionController.isCursorBeingModified())) { |
| 2065 | // ActionMode should be managed by the currently active cursor controller. |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 2066 | mRestartActionModeOnNextRefresh = false; |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2067 | return; |
| 2068 | } |
| 2069 | if (hasSelection) { |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 2070 | hideInsertionPointCursorController(); |
| 2071 | if (mTextActionMode == null) { |
Keisuke Kuroyanagi | 0fd28c9 | 2016-04-04 17:43:06 +0900 | [diff] [blame] | 2072 | if (mRestartActionModeOnNextRefresh) { |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 2073 | // To avoid distraction, newly start action mode only when selection action |
Keisuke Kuroyanagi | 0fd28c9 | 2016-04-04 17:43:06 +0900 | [diff] [blame] | 2074 | // mode is being restarted. |
Abodunrinwa Toki | 66c1627 | 2017-05-03 20:22:55 +0100 | [diff] [blame] | 2075 | startSelectionActionModeAsync(false); |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 2076 | } |
| 2077 | } else if (selectionController == null || !selectionController.isActive()) { |
| 2078 | // Insertion action mode is active. Avoid dismissing the selection. |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2079 | stopTextActionModeWithPreservingSelection(); |
Abodunrinwa Toki | 66c1627 | 2017-05-03 20:22:55 +0100 | [diff] [blame] | 2080 | startSelectionActionModeAsync(false); |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2081 | } else { |
| 2082 | mTextActionMode.invalidateContentRect(); |
| 2083 | } |
| 2084 | } else { |
| 2085 | // Insertion action mode is started only when insertion controller is explicitly |
| 2086 | // activated. |
| 2087 | if (insertionController == null || !insertionController.isActive()) { |
| 2088 | stopTextActionMode(); |
| 2089 | } else if (mTextActionMode != null) { |
| 2090 | mTextActionMode.invalidateContentRect(); |
| 2091 | } |
| 2092 | } |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 2093 | mRestartActionModeOnNextRefresh = false; |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2094 | } |
| 2095 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2096 | /** |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 2097 | * Start an Insertion action mode. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2098 | */ |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 2099 | void startInsertionActionMode() { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 2100 | if (mInsertionActionModeRunnable != null) { |
| 2101 | mTextView.removeCallbacks(mInsertionActionModeRunnable); |
| 2102 | } |
Andrei Stingaceanu | 975a8d0 | 2015-05-19 17:29:16 +0100 | [diff] [blame] | 2103 | if (extractedTextModeWillBeStarted()) { |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 2104 | return; |
Andrei Stingaceanu | 975a8d0 | 2015-05-19 17:29:16 +0100 | [diff] [blame] | 2105 | } |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 2106 | stopTextActionMode(); |
Andrei Stingaceanu | 975a8d0 | 2015-05-19 17:29:16 +0100 | [diff] [blame] | 2107 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 2108 | ActionMode.Callback actionModeCallback = |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 2109 | new TextActionModeCallback(TextActionMode.INSERTION); |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 2110 | mTextActionMode = mTextView.startActionMode( |
Clara Bayarri | b8ed5b7 | 2015-04-09 15:26:41 +0100 | [diff] [blame] | 2111 | actionModeCallback, ActionMode.TYPE_FLOATING); |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 2112 | if (mTextActionMode != null && getInsertionController() != null) { |
| 2113 | getInsertionController().show(); |
| 2114 | } |
Clara Bayarri | 29d2b5aa | 2015-03-13 17:41:56 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 2117 | @NonNull |
| 2118 | TextView getTextView() { |
| 2119 | return mTextView; |
| 2120 | } |
| 2121 | |
| 2122 | @Nullable |
| 2123 | ActionMode getTextActionMode() { |
| 2124 | return mTextActionMode; |
| 2125 | } |
| 2126 | |
| 2127 | void setRestartActionModeOnNextRefresh(boolean value) { |
| 2128 | mRestartActionModeOnNextRefresh = value; |
| 2129 | } |
| 2130 | |
Clara Bayarri | 29d2b5aa | 2015-03-13 17:41:56 +0000 | [diff] [blame] | 2131 | /** |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 2132 | * Asynchronously starts a selection action mode using the TextClassifier. |
Clara Bayarri | 29d2b5aa | 2015-03-13 17:41:56 +0000 | [diff] [blame] | 2133 | */ |
Abodunrinwa Toki | 66c1627 | 2017-05-03 20:22:55 +0100 | [diff] [blame] | 2134 | void startSelectionActionModeAsync(boolean adjustSelection) { |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 2135 | getSelectionActionModeHelper().startSelectionActionModeAsync(adjustSelection); |
| 2136 | } |
| 2137 | |
Richard Ledley | 27db81b | 2018-03-01 12:34:55 +0000 | [diff] [blame] | 2138 | void startLinkActionModeAsync(int start, int end) { |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 2139 | if (!(mTextView.getText() instanceof Spannable)) { |
| 2140 | return; |
| 2141 | } |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 2142 | stopTextActionMode(); |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 2143 | mRequestingLinkActionMode = true; |
Richard Ledley | 27db81b | 2018-03-01 12:34:55 +0000 | [diff] [blame] | 2144 | getSelectionActionModeHelper().startLinkActionModeAsync(start, end); |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | /** |
| 2148 | * Asynchronously invalidates an action mode using the TextClassifier. |
| 2149 | */ |
Abodunrinwa Toki | 4ce651e | 2017-05-12 15:37:29 +0100 | [diff] [blame] | 2150 | void invalidateActionModeAsync() { |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 2151 | getSelectionActionModeHelper().invalidateActionModeAsync(); |
| 2152 | } |
| 2153 | |
Abodunrinwa Toki | 66c1627 | 2017-05-03 20:22:55 +0100 | [diff] [blame] | 2154 | /** |
| 2155 | * Synchronously invalidates an action mode without the TextClassifier. |
| 2156 | */ |
| 2157 | private void invalidateActionMode() { |
| 2158 | if (mTextActionMode != null) { |
| 2159 | mTextActionMode.invalidate(); |
| 2160 | } |
| 2161 | } |
| 2162 | |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 2163 | private SelectionActionModeHelper getSelectionActionModeHelper() { |
| 2164 | if (mSelectionActionModeHelper == null) { |
| 2165 | mSelectionActionModeHelper = new SelectionActionModeHelper(this); |
Clara Bayarri | 578286f | 2015-04-10 15:35:31 +0100 | [diff] [blame] | 2166 | } |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 2167 | return mSelectionActionModeHelper; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 2170 | /** |
| 2171 | * If the TextView allows text selection, selects the current word when no existing selection |
| 2172 | * was available and starts a drag. |
| 2173 | * |
| 2174 | * @return true if the drag was started. |
| 2175 | */ |
| 2176 | private boolean selectCurrentWordAndStartDrag() { |
Clara Bayarri | 7184c8a | 2015-06-05 17:34:09 +0100 | [diff] [blame] | 2177 | if (mInsertionActionModeRunnable != null) { |
| 2178 | mTextView.removeCallbacks(mInsertionActionModeRunnable); |
| 2179 | } |
Andrei Stingaceanu | 975a8d0 | 2015-05-19 17:29:16 +0100 | [diff] [blame] | 2180 | if (extractedTextModeWillBeStarted()) { |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 2181 | return false; |
| 2182 | } |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2183 | if (!checkField()) { |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 2184 | return false; |
| 2185 | } |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2186 | if (!mTextView.hasSelection() && !selectCurrentWord()) { |
| 2187 | // No selection and cannot select a word. |
| 2188 | return false; |
| 2189 | } |
| 2190 | stopTextActionModeWithPreservingSelection(); |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 2191 | getSelectionController().enterDrag( |
| 2192 | SelectionModifierCursorController.DRAG_ACCELERATOR_MODE_WORD); |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 2193 | return true; |
| 2194 | } |
Andrei Stingaceanu | 975a8d0 | 2015-05-19 17:29:16 +0100 | [diff] [blame] | 2195 | |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 2196 | /** |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2197 | * Checks whether a selection can be performed on the current TextView. |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 2198 | * |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2199 | * @return true if a selection can be performed |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 2200 | */ |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2201 | boolean checkField() { |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 2202 | if (!mTextView.canSelectText() || !mTextView.requestFocus()) { |
| 2203 | Log.w(TextView.LOG_TAG, |
| 2204 | "TextView does not support text selection. Selection cancelled."); |
Andrei Stingaceanu | 975a8d0 | 2015-05-19 17:29:16 +0100 | [diff] [blame] | 2205 | return false; |
| 2206 | } |
Clara Bayarri | dfac443 | 2015-05-15 12:18:24 +0100 | [diff] [blame] | 2207 | return true; |
| 2208 | } |
| 2209 | |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 2210 | boolean startActionModeInternal(@TextActionMode int actionMode) { |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2211 | if (extractedTextModeWillBeStarted()) { |
| 2212 | return false; |
| 2213 | } |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 2214 | if (mTextActionMode != null) { |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 2215 | // Text action mode is already started |
Abodunrinwa Toki | 66c1627 | 2017-05-03 20:22:55 +0100 | [diff] [blame] | 2216 | invalidateActionMode(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2217 | return false; |
| 2218 | } |
| 2219 | |
Richard Ledley | 724eff9 | 2017-12-21 10:11:34 +0000 | [diff] [blame] | 2220 | if (actionMode != TextActionMode.TEXT_LINK |
| 2221 | && (!checkField() || !mTextView.hasSelection())) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2222 | return false; |
| 2223 | } |
| 2224 | |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 2225 | ActionMode.Callback actionModeCallback = new TextActionModeCallback(actionMode); |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2226 | mTextActionMode = mTextView.startActionMode(actionModeCallback, ActionMode.TYPE_FLOATING); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2227 | |
Abodunrinwa Toki | 29cb768 | 2018-04-11 21:24:20 +0100 | [diff] [blame] | 2228 | final boolean selectableText = mTextView.isTextEditable() || mTextView.isTextSelectable(); |
| 2229 | if (actionMode == TextActionMode.TEXT_LINK && !selectableText |
| 2230 | && mTextActionMode instanceof FloatingActionMode) { |
| 2231 | // Make the toolbar outside-touchable so that it can be dismissed when the user clicks |
| 2232 | // outside of it. |
| 2233 | ((FloatingActionMode) mTextActionMode).setOutsideTouchable(true, |
| 2234 | () -> stopTextActionMode()); |
| 2235 | } |
| 2236 | |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2237 | final boolean selectionStarted = mTextActionMode != null; |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 2238 | if (selectionStarted |
| 2239 | && mTextView.isTextEditable() && !mTextView.isTextSelectable() |
| 2240 | && mShowSoftInputOnFocus) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2241 | // Show the IME to be able to replace text, except when selecting non editable text. |
Yohei Yukawa | 484d4af | 2018-09-17 16:47:08 -0700 | [diff] [blame] | 2242 | final InputMethodManager imm = getInputMethodManager(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2243 | if (imm != null) { |
| 2244 | imm.showSoftInput(mTextView, 0, null); |
| 2245 | } |
| 2246 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2247 | return selectionStarted; |
| 2248 | } |
| 2249 | |
Keisuke Kuroyanagi | c5a4320 | 2016-06-10 16:27:27 +0900 | [diff] [blame] | 2250 | private boolean extractedTextModeWillBeStarted() { |
Andrei Stingaceanu | b1891b3 | 2015-06-19 16:44:37 +0100 | [diff] [blame] | 2251 | if (!(mTextView.isInExtractedMode())) { |
Yohei Yukawa | 484d4af | 2018-09-17 16:47:08 -0700 | [diff] [blame] | 2252 | final InputMethodManager imm = getInputMethodManager(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2253 | return imm != null && imm.isFullscreenMode(); |
| 2254 | } |
| 2255 | return false; |
| 2256 | } |
| 2257 | |
| 2258 | /** |
Keisuke Kuroyanagi | 05fd8d5 | 2015-03-16 17:44:26 +0900 | [diff] [blame] | 2259 | * @return <code>true</code> if it's reasonable to offer to show suggestions depending on |
| 2260 | * the current cursor position or selection range. This method is consistent with the |
| 2261 | * method to show suggestions {@link SuggestionsPopupWindow#updateSuggestions}. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2262 | */ |
Keisuke Kuroyanagi | 05fd8d5 | 2015-03-16 17:44:26 +0900 | [diff] [blame] | 2263 | private boolean shouldOfferToShowSuggestions() { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2264 | CharSequence text = mTextView.getText(); |
| 2265 | if (!(text instanceof Spannable)) return false; |
| 2266 | |
Keisuke Kuroyanagi | 05fd8d5 | 2015-03-16 17:44:26 +0900 | [diff] [blame] | 2267 | final Spannable spannable = (Spannable) text; |
| 2268 | final int selectionStart = mTextView.getSelectionStart(); |
| 2269 | final int selectionEnd = mTextView.getSelectionEnd(); |
| 2270 | final SuggestionSpan[] suggestionSpans = spannable.getSpans(selectionStart, selectionEnd, |
| 2271 | SuggestionSpan.class); |
| 2272 | if (suggestionSpans.length == 0) { |
| 2273 | return false; |
| 2274 | } |
| 2275 | if (selectionStart == selectionEnd) { |
| 2276 | // Spans overlap the cursor. |
Keisuke Kuroyanagi | 7e4fbe0 | 2015-05-22 14:37:01 +0900 | [diff] [blame] | 2277 | for (int i = 0; i < suggestionSpans.length; i++) { |
| 2278 | if (suggestionSpans[i].getSuggestions().length > 0) { |
| 2279 | return true; |
| 2280 | } |
| 2281 | } |
| 2282 | return false; |
Keisuke Kuroyanagi | 05fd8d5 | 2015-03-16 17:44:26 +0900 | [diff] [blame] | 2283 | } |
| 2284 | int minSpanStart = mTextView.getText().length(); |
| 2285 | int maxSpanEnd = 0; |
| 2286 | int unionOfSpansCoveringSelectionStartStart = mTextView.getText().length(); |
| 2287 | int unionOfSpansCoveringSelectionStartEnd = 0; |
Keisuke Kuroyanagi | 7e4fbe0 | 2015-05-22 14:37:01 +0900 | [diff] [blame] | 2288 | boolean hasValidSuggestions = false; |
Keisuke Kuroyanagi | 05fd8d5 | 2015-03-16 17:44:26 +0900 | [diff] [blame] | 2289 | for (int i = 0; i < suggestionSpans.length; i++) { |
| 2290 | final int spanStart = spannable.getSpanStart(suggestionSpans[i]); |
| 2291 | final int spanEnd = spannable.getSpanEnd(suggestionSpans[i]); |
| 2292 | minSpanStart = Math.min(minSpanStart, spanStart); |
| 2293 | maxSpanEnd = Math.max(maxSpanEnd, spanEnd); |
| 2294 | if (selectionStart < spanStart || selectionStart > spanEnd) { |
| 2295 | // The span doesn't cover the current selection start point. |
| 2296 | continue; |
| 2297 | } |
Keisuke Kuroyanagi | 7e4fbe0 | 2015-05-22 14:37:01 +0900 | [diff] [blame] | 2298 | hasValidSuggestions = |
| 2299 | hasValidSuggestions || suggestionSpans[i].getSuggestions().length > 0; |
Keisuke Kuroyanagi | 05fd8d5 | 2015-03-16 17:44:26 +0900 | [diff] [blame] | 2300 | unionOfSpansCoveringSelectionStartStart = |
| 2301 | Math.min(unionOfSpansCoveringSelectionStartStart, spanStart); |
| 2302 | unionOfSpansCoveringSelectionStartEnd = |
| 2303 | Math.max(unionOfSpansCoveringSelectionStartEnd, spanEnd); |
| 2304 | } |
Keisuke Kuroyanagi | 7e4fbe0 | 2015-05-22 14:37:01 +0900 | [diff] [blame] | 2305 | if (!hasValidSuggestions) { |
| 2306 | return false; |
| 2307 | } |
Keisuke Kuroyanagi | 05fd8d5 | 2015-03-16 17:44:26 +0900 | [diff] [blame] | 2308 | if (unionOfSpansCoveringSelectionStartStart >= unionOfSpansCoveringSelectionStartEnd) { |
| 2309 | // No spans cover the selection start point. |
| 2310 | return false; |
| 2311 | } |
| 2312 | if (minSpanStart < unionOfSpansCoveringSelectionStartStart |
| 2313 | || maxSpanEnd > unionOfSpansCoveringSelectionStartEnd) { |
| 2314 | // There is a span that is not covered by the union. In this case, we soouldn't offer |
| 2315 | // to show suggestions as it's confusing. |
| 2316 | return false; |
| 2317 | } |
| 2318 | return true; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | /** |
| 2322 | * @return <code>true</code> if the cursor is inside an {@link SuggestionSpan} with |
| 2323 | * {@link SuggestionSpan#FLAG_EASY_CORRECT} set. |
| 2324 | */ |
| 2325 | private boolean isCursorInsideEasyCorrectionSpan() { |
| 2326 | Spannable spannable = (Spannable) mTextView.getText(); |
| 2327 | SuggestionSpan[] suggestionSpans = spannable.getSpans(mTextView.getSelectionStart(), |
| 2328 | mTextView.getSelectionEnd(), SuggestionSpan.class); |
| 2329 | for (int i = 0; i < suggestionSpans.length; i++) { |
| 2330 | if ((suggestionSpans[i].getFlags() & SuggestionSpan.FLAG_EASY_CORRECT) != 0) { |
| 2331 | return true; |
| 2332 | } |
| 2333 | } |
| 2334 | return false; |
| 2335 | } |
| 2336 | |
| 2337 | void onTouchUpEvent(MotionEvent event) { |
Nikita Dubrovsky | 05cfcc8 | 2019-10-24 08:57:32 -0700 | [diff] [blame] | 2338 | if (TextView.DEBUG_CURSOR) { |
| 2339 | logCursor("onTouchUpEvent", null); |
| 2340 | } |
Abodunrinwa Toki | 8dd3a74 | 2017-05-02 18:44:19 +0100 | [diff] [blame] | 2341 | if (getSelectionActionModeHelper().resetSelection( |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 2342 | getTextView().getOffsetForPosition(event.getX(), event.getY()))) { |
| 2343 | return; |
| 2344 | } |
| 2345 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2346 | boolean selectAllGotFocus = mSelectAllOnFocus && mTextView.didTouchFocusSelect(); |
Mady Mellor | a286145 | 2015-06-25 08:40:27 -0700 | [diff] [blame] | 2347 | hideCursorAndSpanControllers(); |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 2348 | stopTextActionMode(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2349 | CharSequence text = mTextView.getText(); |
| 2350 | if (!selectAllGotFocus && text.length() > 0) { |
| 2351 | // Move cursor |
| 2352 | final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY()); |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 2353 | |
| 2354 | final boolean shouldInsertCursor = !mRequestingLinkActionMode; |
| 2355 | if (shouldInsertCursor) { |
| 2356 | Selection.setSelection((Spannable) text, offset); |
| 2357 | if (mSpellChecker != null) { |
| 2358 | // When the cursor moves, the word that was typed may need spell check |
| 2359 | mSpellChecker.onSelectionChanged(); |
| 2360 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2361 | } |
Andrei Stingaceanu | 35c550c | 2015-05-07 16:49:49 +0100 | [diff] [blame] | 2362 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2363 | if (!extractedTextModeWillBeStarted()) { |
| 2364 | if (isCursorInsideEasyCorrectionSpan()) { |
Andrei Stingaceanu | 373816e | 2015-05-28 11:26:28 +0100 | [diff] [blame] | 2365 | // Cancel the single tap delayed runnable. |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 2366 | if (mInsertionActionModeRunnable != null) { |
| 2367 | mTextView.removeCallbacks(mInsertionActionModeRunnable); |
Andrei Stingaceanu | 373816e | 2015-05-28 11:26:28 +0100 | [diff] [blame] | 2368 | } |
| 2369 | |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 2370 | mShowSuggestionRunnable = this::replace; |
| 2371 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2372 | // removeCallbacks is performed on every touch |
| 2373 | mTextView.postDelayed(mShowSuggestionRunnable, |
| 2374 | ViewConfiguration.getDoubleTapTimeout()); |
| 2375 | } else if (hasInsertionController()) { |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 2376 | if (shouldInsertCursor) { |
| 2377 | getInsertionController().show(); |
| 2378 | } else { |
| 2379 | getInsertionController().hide(); |
| 2380 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2381 | } |
| 2382 | } |
| 2383 | } |
| 2384 | } |
| 2385 | |
Yohei Yukawa | 401e3d4 | 2019-01-19 11:49:37 -0800 | [diff] [blame] | 2386 | /** |
| 2387 | * Called when {@link TextView#mTextOperationUser} has changed. |
| 2388 | * |
| 2389 | * <p>Any user-specific resources need to be refreshed here.</p> |
| 2390 | */ |
| 2391 | final void onTextOperationUserChanged() { |
| 2392 | if (mSpellChecker != null) { |
| 2393 | mSpellChecker.resetSession(); |
| 2394 | } |
| 2395 | } |
| 2396 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 2397 | protected void stopTextActionMode() { |
| 2398 | if (mTextActionMode != null) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2399 | // This will hide the mSelectionModifierCursorController |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 2400 | mTextActionMode.finish(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2401 | } |
| 2402 | } |
| 2403 | |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2404 | private void stopTextActionModeWithPreservingSelection() { |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 2405 | if (mTextActionMode != null) { |
| 2406 | mRestartActionModeOnNextRefresh = true; |
| 2407 | } |
Keisuke Kuroyanagi | af4caa6 | 2016-02-29 12:53:58 -0800 | [diff] [blame] | 2408 | mPreserveSelection = true; |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2409 | stopTextActionMode(); |
Keisuke Kuroyanagi | af4caa6 | 2016-02-29 12:53:58 -0800 | [diff] [blame] | 2410 | mPreserveSelection = false; |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2411 | } |
| 2412 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2413 | /** |
| 2414 | * @return True if this view supports insertion handles. |
| 2415 | */ |
| 2416 | boolean hasInsertionController() { |
| 2417 | return mInsertionControllerEnabled; |
| 2418 | } |
| 2419 | |
| 2420 | /** |
| 2421 | * @return True if this view supports selection handles. |
| 2422 | */ |
| 2423 | boolean hasSelectionController() { |
| 2424 | return mSelectionControllerEnabled; |
| 2425 | } |
| 2426 | |
Keisuke Kuroyanagi | c5a4320 | 2016-06-10 16:27:27 +0900 | [diff] [blame] | 2427 | private InsertionPointCursorController getInsertionController() { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2428 | if (!mInsertionControllerEnabled) { |
| 2429 | return null; |
| 2430 | } |
| 2431 | |
| 2432 | if (mInsertionPointCursorController == null) { |
| 2433 | mInsertionPointCursorController = new InsertionPointCursorController(); |
| 2434 | |
| 2435 | final ViewTreeObserver observer = mTextView.getViewTreeObserver(); |
| 2436 | observer.addOnTouchModeChangeListener(mInsertionPointCursorController); |
| 2437 | } |
| 2438 | |
| 2439 | return mInsertionPointCursorController; |
| 2440 | } |
| 2441 | |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 2442 | @Nullable |
| 2443 | SelectionModifierCursorController getSelectionController() { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2444 | if (!mSelectionControllerEnabled) { |
| 2445 | return null; |
| 2446 | } |
| 2447 | |
| 2448 | if (mSelectionModifierCursorController == null) { |
| 2449 | mSelectionModifierCursorController = new SelectionModifierCursorController(); |
| 2450 | |
| 2451 | final ViewTreeObserver observer = mTextView.getViewTreeObserver(); |
| 2452 | observer.addOnTouchModeChangeListener(mSelectionModifierCursorController); |
| 2453 | } |
| 2454 | |
| 2455 | return mSelectionModifierCursorController; |
| 2456 | } |
| 2457 | |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 2458 | @VisibleForTesting |
Roozbeh Pournader | 9c13307 | 2017-07-26 22:36:27 -0700 | [diff] [blame] | 2459 | @Nullable |
| 2460 | public Drawable getCursorDrawable() { |
Roozbeh Pournader | e06eebf | 2017-10-03 12:13:46 -0700 | [diff] [blame] | 2461 | return mDrawableForCursor; |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 2462 | } |
| 2463 | |
Roozbeh Pournader | 9c13307 | 2017-07-26 22:36:27 -0700 | [diff] [blame] | 2464 | private void updateCursorPosition(int top, int bottom, float horizontal) { |
Mihai Popa | 6c7ad1d | 2018-12-04 15:45:00 +0000 | [diff] [blame] | 2465 | loadCursorDrawable(); |
Roozbeh Pournader | e06eebf | 2017-10-03 12:13:46 -0700 | [diff] [blame] | 2466 | final int left = clampHorizontalPosition(mDrawableForCursor, horizontal); |
| 2467 | final int width = mDrawableForCursor.getIntrinsicWidth(); |
Nikita Dubrovsky | 05cfcc8 | 2019-10-24 08:57:32 -0700 | [diff] [blame] | 2468 | if (TextView.DEBUG_CURSOR) { |
| 2469 | logCursor("updateCursorPosition", "left=%s, top=%s", left, (top - mTempRect.top)); |
| 2470 | } |
Roozbeh Pournader | e06eebf | 2017-10-03 12:13:46 -0700 | [diff] [blame] | 2471 | mDrawableForCursor.setBounds(left, top - mTempRect.top, left + width, |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2472 | bottom + mTempRect.bottom); |
| 2473 | } |
| 2474 | |
| 2475 | /** |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 2476 | * Return clamped position for the drawable. If the drawable is within the boundaries of the |
| 2477 | * 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] | 2478 | * 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] | 2479 | * the view boundary. If the drawable is null, horizontal parameter is aligned to left or right |
| 2480 | * of the view. |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 2481 | * |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 2482 | * @param drawable Drawable. Can be null. |
| 2483 | * @param horizontal Horizontal position for the drawable. |
| 2484 | * @return The clamped horizontal position for the drawable. |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 2485 | */ |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 2486 | private int clampHorizontalPosition(@Nullable final Drawable drawable, float horizontal) { |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 2487 | horizontal = Math.max(0.5f, horizontal - 0.5f); |
| 2488 | if (mTempRect == null) mTempRect = new Rect(); |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 2489 | |
| 2490 | int drawableWidth = 0; |
| 2491 | if (drawable != null) { |
| 2492 | drawable.getPadding(mTempRect); |
| 2493 | drawableWidth = drawable.getIntrinsicWidth(); |
| 2494 | } else { |
| 2495 | mTempRect.setEmpty(); |
| 2496 | } |
| 2497 | |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 2498 | int scrollX = mTextView.getScrollX(); |
| 2499 | float horizontalDiff = horizontal - scrollX; |
| 2500 | int viewClippedWidth = mTextView.getWidth() - mTextView.getCompoundPaddingLeft() |
| 2501 | - mTextView.getCompoundPaddingRight(); |
| 2502 | |
| 2503 | final int left; |
| 2504 | if (horizontalDiff >= (viewClippedWidth - 1f)) { |
| 2505 | // at the rightmost position |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 2506 | left = viewClippedWidth + scrollX - (drawableWidth - mTempRect.right); |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 2507 | } else if (Math.abs(horizontalDiff) <= 1f |
| 2508 | || (TextUtils.isEmpty(mTextView.getText()) |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 2509 | && (TextView.VERY_WIDE - scrollX) <= (viewClippedWidth + 1f) |
| 2510 | && horizontal <= 1f)) { |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 2511 | // at the leftmost position |
| 2512 | left = scrollX - mTempRect.left; |
| 2513 | } else { |
| 2514 | left = (int) horizontal - mTempRect.left; |
| 2515 | } |
| 2516 | return left; |
| 2517 | } |
| 2518 | |
| 2519 | /** |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2520 | * 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] | 2521 | * a dictionary) from the current input method, provided by it calling |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2522 | * {@link InputConnection#commitCorrection} InputConnection.commitCorrection()}. The default |
| 2523 | * implementation flashes the background of the corrected word to provide feedback to the user. |
| 2524 | * |
| 2525 | * @param info The auto correct info about the text that was corrected. |
| 2526 | */ |
| 2527 | public void onCommitCorrection(CorrectionInfo info) { |
| 2528 | if (mCorrectionHighlighter == null) { |
| 2529 | mCorrectionHighlighter = new CorrectionHighlighter(); |
| 2530 | } else { |
| 2531 | mCorrectionHighlighter.invalidate(false); |
| 2532 | } |
| 2533 | |
| 2534 | mCorrectionHighlighter.highlight(info); |
Keisuke Kuroyanagi | fa2d7b1 | 2016-07-22 17:09:48 +0900 | [diff] [blame] | 2535 | mUndoInputFilter.freezeLastEdit(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2536 | } |
| 2537 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2538 | void onScrollChanged() { |
Gilles Debunne | 157aafc | 2012-04-19 17:21:57 -0700 | [diff] [blame] | 2539 | if (mPositionListener != null) { |
| 2540 | mPositionListener.onScrollChanged(); |
| 2541 | } |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 2542 | if (mTextActionMode != null) { |
| 2543 | mTextActionMode.invalidateContentRect(); |
Abodunrinwa Toki | 56195db | 2015-04-22 06:46:54 +0100 | [diff] [blame] | 2544 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | /** |
| 2548 | * @return True when the TextView isFocused and has a valid zero-length selection (cursor). |
| 2549 | */ |
| 2550 | private boolean shouldBlink() { |
Long Ling | 0c27fbb | 2019-11-19 18:41:21 +0000 | [diff] [blame] | 2551 | if (!isCursorVisible() || !mTextView.isFocused()) return false; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2552 | |
| 2553 | final int start = mTextView.getSelectionStart(); |
| 2554 | if (start < 0) return false; |
| 2555 | |
| 2556 | final int end = mTextView.getSelectionEnd(); |
| 2557 | if (end < 0) return false; |
| 2558 | |
| 2559 | return start == end; |
| 2560 | } |
| 2561 | |
| 2562 | void makeBlink() { |
| 2563 | if (shouldBlink()) { |
| 2564 | mShowCursor = SystemClock.uptimeMillis(); |
| 2565 | if (mBlink == null) mBlink = new Blink(); |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 2566 | mTextView.removeCallbacks(mBlink); |
| 2567 | mTextView.postDelayed(mBlink, BLINK); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2568 | } else { |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 2569 | if (mBlink != null) mTextView.removeCallbacks(mBlink); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2570 | } |
| 2571 | } |
| 2572 | |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 2573 | private class Blink implements Runnable { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2574 | private boolean mCancelled; |
| 2575 | |
| 2576 | public void run() { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2577 | if (mCancelled) { |
| 2578 | return; |
| 2579 | } |
| 2580 | |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 2581 | mTextView.removeCallbacks(this); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2582 | |
| 2583 | if (shouldBlink()) { |
| 2584 | if (mTextView.getLayout() != null) { |
| 2585 | mTextView.invalidateCursorPath(); |
| 2586 | } |
| 2587 | |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 2588 | mTextView.postDelayed(this, BLINK); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2589 | } |
| 2590 | } |
| 2591 | |
| 2592 | void cancel() { |
| 2593 | if (!mCancelled) { |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 2594 | mTextView.removeCallbacks(this); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2595 | mCancelled = true; |
| 2596 | } |
| 2597 | } |
| 2598 | |
| 2599 | void uncancel() { |
| 2600 | mCancelled = false; |
| 2601 | } |
| 2602 | } |
| 2603 | |
Keisuke Kuroyanagi | 5396d7e | 2016-02-19 15:28:26 -0800 | [diff] [blame] | 2604 | private DragShadowBuilder getTextThumbnailBuilder(int start, int end) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2605 | TextView shadowView = (TextView) View.inflate(mTextView.getContext(), |
| 2606 | com.android.internal.R.layout.text_drag_thumbnail, null); |
| 2607 | |
| 2608 | if (shadowView == null) { |
| 2609 | throw new IllegalArgumentException("Unable to inflate text drag thumbnail"); |
| 2610 | } |
| 2611 | |
Keisuke Kuroyanagi | 5396d7e | 2016-02-19 15:28:26 -0800 | [diff] [blame] | 2612 | if (end - start > DRAG_SHADOW_MAX_TEXT_LENGTH) { |
| 2613 | final long range = getCharClusterRange(start + DRAG_SHADOW_MAX_TEXT_LENGTH); |
| 2614 | end = TextUtils.unpackRangeEndFromLong(range); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2615 | } |
Keisuke Kuroyanagi | 5396d7e | 2016-02-19 15:28:26 -0800 | [diff] [blame] | 2616 | final CharSequence text = mTextView.getTransformedText(start, end); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2617 | shadowView.setText(text); |
| 2618 | shadowView.setTextColor(mTextView.getTextColors()); |
| 2619 | |
Alan Viverette | bb98ebd | 2015-05-08 17:17:44 -0700 | [diff] [blame] | 2620 | shadowView.setTextAppearance(R.styleable.Theme_textAppearanceLarge); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2621 | shadowView.setGravity(Gravity.CENTER); |
| 2622 | |
| 2623 | shadowView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, |
| 2624 | ViewGroup.LayoutParams.WRAP_CONTENT)); |
| 2625 | |
| 2626 | final int size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); |
| 2627 | shadowView.measure(size, size); |
| 2628 | |
| 2629 | shadowView.layout(0, 0, shadowView.getMeasuredWidth(), shadowView.getMeasuredHeight()); |
| 2630 | shadowView.invalidate(); |
| 2631 | return new DragShadowBuilder(shadowView); |
| 2632 | } |
| 2633 | |
| 2634 | private static class DragLocalState { |
| 2635 | public TextView sourceTextView; |
| 2636 | public int start, end; |
| 2637 | |
| 2638 | public DragLocalState(TextView sourceTextView, int start, int end) { |
| 2639 | this.sourceTextView = sourceTextView; |
| 2640 | this.start = start; |
| 2641 | this.end = end; |
| 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | void onDrop(DragEvent event) { |
Ben Murdoch | 3dac460 | 2017-01-17 11:27:37 +0000 | [diff] [blame] | 2646 | SpannableStringBuilder content = new SpannableStringBuilder(); |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 2647 | |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 2648 | final DragAndDropPermissions permissions = DragAndDropPermissions.obtain(event); |
| 2649 | if (permissions != null) { |
| 2650 | permissions.takeTransient(); |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | try { |
| 2654 | ClipData clipData = event.getClipData(); |
| 2655 | final int itemCount = clipData.getItemCount(); |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 2656 | for (int i = 0; i < itemCount; i++) { |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 2657 | Item item = clipData.getItemAt(i); |
| 2658 | content.append(item.coerceToStyledText(mTextView.getContext())); |
| 2659 | } |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 2660 | } finally { |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 2661 | if (permissions != null) { |
| 2662 | permissions.release(); |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 2663 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2664 | } |
| 2665 | |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 2666 | mTextView.beginBatchEdit(); |
Keisuke Kuroyanagi | fa2d7b1 | 2016-07-22 17:09:48 +0900 | [diff] [blame] | 2667 | mUndoInputFilter.freezeLastEdit(); |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 2668 | try { |
| 2669 | final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY()); |
| 2670 | Object localState = event.getLocalState(); |
| 2671 | DragLocalState dragLocalState = null; |
| 2672 | if (localState instanceof DragLocalState) { |
| 2673 | dragLocalState = (DragLocalState) localState; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2674 | } |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 2675 | boolean dragDropIntoItself = dragLocalState != null |
| 2676 | && dragLocalState.sourceTextView == mTextView; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2677 | |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 2678 | if (dragDropIntoItself) { |
| 2679 | if (offset >= dragLocalState.start && offset < dragLocalState.end) { |
| 2680 | // A drop inside the original selection discards the drop. |
| 2681 | return; |
| 2682 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2683 | } |
| 2684 | |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 2685 | final int originalLength = mTextView.getText().length(); |
| 2686 | int min = offset; |
| 2687 | int max = offset; |
| 2688 | |
| 2689 | Selection.setSelection((Spannable) mTextView.getText(), max); |
| 2690 | mTextView.replaceText_internal(min, max, content); |
| 2691 | |
| 2692 | if (dragDropIntoItself) { |
| 2693 | int dragSourceStart = dragLocalState.start; |
| 2694 | int dragSourceEnd = dragLocalState.end; |
| 2695 | if (max <= dragSourceStart) { |
| 2696 | // Inserting text before selection has shifted positions |
| 2697 | final int shift = mTextView.getText().length() - originalLength; |
| 2698 | dragSourceStart += shift; |
| 2699 | dragSourceEnd += shift; |
| 2700 | } |
| 2701 | |
Keisuke Kuroyanagi | fae4578 | 2016-02-24 18:53:00 -0800 | [diff] [blame] | 2702 | // Delete original selection |
| 2703 | mTextView.deleteText_internal(dragSourceStart, dragSourceEnd); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2704 | |
Keisuke Kuroyanagi | fae4578 | 2016-02-24 18:53:00 -0800 | [diff] [blame] | 2705 | // Make sure we do not leave two adjacent spaces. |
| 2706 | final int prevCharIdx = Math.max(0, dragSourceStart - 1); |
| 2707 | final int nextCharIdx = Math.min(mTextView.getText().length(), dragSourceStart + 1); |
| 2708 | if (nextCharIdx > prevCharIdx + 1) { |
| 2709 | CharSequence t = mTextView.getTransformedText(prevCharIdx, nextCharIdx); |
| 2710 | if (Character.isSpaceChar(t.charAt(0)) && Character.isSpaceChar(t.charAt(1))) { |
| 2711 | mTextView.deleteText_internal(prevCharIdx, prevCharIdx + 1); |
| 2712 | } |
Victoria Lease | 9137320 | 2012-09-07 16:41:59 -0700 | [diff] [blame] | 2713 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2714 | } |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 2715 | } finally { |
| 2716 | mTextView.endBatchEdit(); |
Keisuke Kuroyanagi | fa2d7b1 | 2016-07-22 17:09:48 +0900 | [diff] [blame] | 2717 | mUndoInputFilter.freezeLastEdit(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2718 | } |
| 2719 | } |
| 2720 | |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2721 | public void addSpanWatchers(Spannable text) { |
| 2722 | final int textLength = text.length(); |
| 2723 | |
| 2724 | if (mKeyListener != null) { |
| 2725 | text.setSpan(mKeyListener, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE); |
| 2726 | } |
| 2727 | |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 2728 | if (mSpanController == null) { |
| 2729 | mSpanController = new SpanController(); |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2730 | } |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 2731 | text.setSpan(mSpanController, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE); |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2732 | } |
| 2733 | |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 2734 | void setContextMenuAnchor(float x, float y) { |
| 2735 | mContextMenuAnchorX = x; |
| 2736 | mContextMenuAnchorY = y; |
| 2737 | } |
| 2738 | |
| 2739 | void onCreateContextMenu(ContextMenu menu) { |
| 2740 | if (mIsBeingLongClicked || Float.isNaN(mContextMenuAnchorX) |
| 2741 | || Float.isNaN(mContextMenuAnchorY)) { |
| 2742 | return; |
| 2743 | } |
| 2744 | final int offset = mTextView.getOffsetForPosition(mContextMenuAnchorX, mContextMenuAnchorY); |
| 2745 | if (offset == -1) { |
| 2746 | return; |
| 2747 | } |
Siyamed Sinir | 532f3c9 | 2017-06-15 18:22:31 -0700 | [diff] [blame] | 2748 | |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 2749 | stopTextActionModeWithPreservingSelection(); |
Siyamed Sinir | 532f3c9 | 2017-06-15 18:22:31 -0700 | [diff] [blame] | 2750 | if (mTextView.canSelectText()) { |
| 2751 | final boolean isOnSelection = mTextView.hasSelection() |
| 2752 | && offset >= mTextView.getSelectionStart() |
| 2753 | && offset <= mTextView.getSelectionEnd(); |
| 2754 | if (!isOnSelection) { |
| 2755 | // Right clicked position is not on the selection. Remove the selection and move the |
| 2756 | // cursor to the right clicked position. |
| 2757 | Selection.setSelection((Spannable) mTextView.getText(), offset); |
| 2758 | stopTextActionMode(); |
| 2759 | } |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 2760 | } |
| 2761 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 2762 | if (shouldOfferToShowSuggestions()) { |
Keisuke Kuroyanagi | 182f5fe | 2016-03-11 16:31:29 +0900 | [diff] [blame] | 2763 | final SuggestionInfo[] suggestionInfoArray = |
| 2764 | new SuggestionInfo[SuggestionSpan.SUGGESTIONS_MAX_SIZE]; |
| 2765 | for (int i = 0; i < suggestionInfoArray.length; i++) { |
| 2766 | suggestionInfoArray[i] = new SuggestionInfo(); |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 2767 | } |
| 2768 | final SubMenu subMenu = menu.addSubMenu(Menu.NONE, Menu.NONE, MENU_ITEM_ORDER_REPLACE, |
| 2769 | com.android.internal.R.string.replace); |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 2770 | final int numItems = mSuggestionHelper.getSuggestionInfo(suggestionInfoArray, null); |
Keisuke Kuroyanagi | 182f5fe | 2016-03-11 16:31:29 +0900 | [diff] [blame] | 2771 | for (int i = 0; i < numItems; i++) { |
| 2772 | final SuggestionInfo info = suggestionInfoArray[i]; |
| 2773 | subMenu.add(Menu.NONE, Menu.NONE, i, info.mText) |
| 2774 | .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 2775 | @Override |
| 2776 | public boolean onMenuItemClick(MenuItem item) { |
| 2777 | replaceWithSuggestion(info); |
| 2778 | return true; |
| 2779 | } |
| 2780 | }); |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 2781 | } |
| 2782 | } |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 2783 | |
| 2784 | menu.add(Menu.NONE, TextView.ID_UNDO, MENU_ITEM_ORDER_UNDO, |
| 2785 | com.android.internal.R.string.undo) |
| 2786 | .setAlphabeticShortcut('z') |
| 2787 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener) |
| 2788 | .setEnabled(mTextView.canUndo()); |
| 2789 | menu.add(Menu.NONE, TextView.ID_REDO, MENU_ITEM_ORDER_REDO, |
| 2790 | com.android.internal.R.string.redo) |
| 2791 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener) |
| 2792 | .setEnabled(mTextView.canRedo()); |
| 2793 | |
| 2794 | menu.add(Menu.NONE, TextView.ID_CUT, MENU_ITEM_ORDER_CUT, |
| 2795 | com.android.internal.R.string.cut) |
| 2796 | .setAlphabeticShortcut('x') |
| 2797 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener) |
| 2798 | .setEnabled(mTextView.canCut()); |
| 2799 | menu.add(Menu.NONE, TextView.ID_COPY, MENU_ITEM_ORDER_COPY, |
| 2800 | com.android.internal.R.string.copy) |
| 2801 | .setAlphabeticShortcut('c') |
| 2802 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener) |
| 2803 | .setEnabled(mTextView.canCopy()); |
| 2804 | menu.add(Menu.NONE, TextView.ID_PASTE, MENU_ITEM_ORDER_PASTE, |
| 2805 | com.android.internal.R.string.paste) |
| 2806 | .setAlphabeticShortcut('v') |
| 2807 | .setEnabled(mTextView.canPaste()) |
| 2808 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener); |
Abodunrinwa Toki | ea6cb12 | 2017-04-28 22:14:13 +0100 | [diff] [blame] | 2809 | menu.add(Menu.NONE, TextView.ID_PASTE_AS_PLAIN_TEXT, MENU_ITEM_ORDER_PASTE_AS_PLAIN_TEXT, |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 2810 | com.android.internal.R.string.paste_as_plain_text) |
Abodunrinwa Toki | ea6cb12 | 2017-04-28 22:14:13 +0100 | [diff] [blame] | 2811 | .setEnabled(mTextView.canPasteAsPlainText()) |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 2812 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener); |
| 2813 | menu.add(Menu.NONE, TextView.ID_SHARE, MENU_ITEM_ORDER_SHARE, |
| 2814 | com.android.internal.R.string.share) |
| 2815 | .setEnabled(mTextView.canShare()) |
| 2816 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener); |
| 2817 | menu.add(Menu.NONE, TextView.ID_SELECT_ALL, MENU_ITEM_ORDER_SELECT_ALL, |
| 2818 | com.android.internal.R.string.selectAll) |
| 2819 | .setAlphabeticShortcut('a') |
| 2820 | .setEnabled(mTextView.canSelectAllText()) |
| 2821 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener); |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 2822 | menu.add(Menu.NONE, TextView.ID_AUTOFILL, MENU_ITEM_ORDER_AUTOFILL, |
Felipe Leme | 555bcac | 2017-06-26 12:53:56 -0700 | [diff] [blame] | 2823 | android.R.string.autofill) |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 2824 | .setEnabled(mTextView.canRequestAutofill()) |
| 2825 | .setOnMenuItemClickListener(mOnContextMenuItemClickListener); |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 2826 | |
Keisuke Kuroyanagi | af4caa6 | 2016-02-29 12:53:58 -0800 | [diff] [blame] | 2827 | mPreserveSelection = true; |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 2828 | } |
| 2829 | |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 2830 | @Nullable |
| 2831 | private SuggestionSpan findEquivalentSuggestionSpan( |
| 2832 | @NonNull SuggestionSpanInfo suggestionSpanInfo) { |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 2833 | final Editable editable = (Editable) mTextView.getText(); |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 2834 | if (editable.getSpanStart(suggestionSpanInfo.mSuggestionSpan) >= 0) { |
| 2835 | // Exactly same span is found. |
| 2836 | return suggestionSpanInfo.mSuggestionSpan; |
| 2837 | } |
| 2838 | // Suggestion span couldn't be found. Try to find a suggestion span that has the same |
| 2839 | // contents. |
| 2840 | final SuggestionSpan[] suggestionSpans = editable.getSpans(suggestionSpanInfo.mSpanStart, |
| 2841 | suggestionSpanInfo.mSpanEnd, SuggestionSpan.class); |
| 2842 | for (final SuggestionSpan suggestionSpan : suggestionSpans) { |
| 2843 | final int start = editable.getSpanStart(suggestionSpan); |
| 2844 | if (start != suggestionSpanInfo.mSpanStart) { |
| 2845 | continue; |
| 2846 | } |
| 2847 | final int end = editable.getSpanEnd(suggestionSpan); |
| 2848 | if (end != suggestionSpanInfo.mSpanEnd) { |
| 2849 | continue; |
| 2850 | } |
| 2851 | if (suggestionSpan.equals(suggestionSpanInfo.mSuggestionSpan)) { |
| 2852 | return suggestionSpan; |
Keisuke Kuroyanagi | d3194503 | 2016-02-26 16:09:12 -0800 | [diff] [blame] | 2853 | } |
| 2854 | } |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 2855 | return null; |
| 2856 | } |
| 2857 | |
| 2858 | private void replaceWithSuggestion(@NonNull final SuggestionInfo suggestionInfo) { |
| 2859 | final SuggestionSpan targetSuggestionSpan = findEquivalentSuggestionSpan( |
| 2860 | suggestionInfo.mSuggestionSpanInfo); |
| 2861 | if (targetSuggestionSpan == null) { |
| 2862 | // Span has been removed |
| 2863 | return; |
| 2864 | } |
| 2865 | final Editable editable = (Editable) mTextView.getText(); |
| 2866 | final int spanStart = editable.getSpanStart(targetSuggestionSpan); |
| 2867 | final int spanEnd = editable.getSpanEnd(targetSuggestionSpan); |
Keisuke Kuroyanagi | d3194503 | 2016-02-26 16:09:12 -0800 | [diff] [blame] | 2868 | if (spanStart < 0 || spanEnd <= spanStart) { |
| 2869 | // Span has been removed |
| 2870 | return; |
| 2871 | } |
| 2872 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 2873 | final String originalText = TextUtils.substring(editable, spanStart, spanEnd); |
| 2874 | // SuggestionSpans are removed by replace: save them before |
| 2875 | SuggestionSpan[] suggestionSpans = editable.getSpans(spanStart, spanEnd, |
| 2876 | SuggestionSpan.class); |
| 2877 | final int length = suggestionSpans.length; |
| 2878 | int[] suggestionSpansStarts = new int[length]; |
| 2879 | int[] suggestionSpansEnds = new int[length]; |
| 2880 | int[] suggestionSpansFlags = new int[length]; |
| 2881 | for (int i = 0; i < length; i++) { |
| 2882 | final SuggestionSpan suggestionSpan = suggestionSpans[i]; |
| 2883 | suggestionSpansStarts[i] = editable.getSpanStart(suggestionSpan); |
| 2884 | suggestionSpansEnds[i] = editable.getSpanEnd(suggestionSpan); |
| 2885 | suggestionSpansFlags[i] = editable.getSpanFlags(suggestionSpan); |
| 2886 | |
| 2887 | // Remove potential misspelled flags |
| 2888 | int suggestionSpanFlags = suggestionSpan.getFlags(); |
| 2889 | if ((suggestionSpanFlags & SuggestionSpan.FLAG_MISSPELLED) != 0) { |
| 2890 | suggestionSpanFlags &= ~SuggestionSpan.FLAG_MISSPELLED; |
| 2891 | suggestionSpanFlags &= ~SuggestionSpan.FLAG_EASY_CORRECT; |
| 2892 | suggestionSpan.setFlags(suggestionSpanFlags); |
| 2893 | } |
| 2894 | } |
| 2895 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 2896 | // Swap text content between actual text and Suggestion span |
| 2897 | final int suggestionStart = suggestionInfo.mSuggestionStart; |
| 2898 | final int suggestionEnd = suggestionInfo.mSuggestionEnd; |
| 2899 | final String suggestion = suggestionInfo.mText.subSequence( |
| 2900 | suggestionStart, suggestionEnd).toString(); |
| 2901 | mTextView.replaceText_internal(spanStart, spanEnd, suggestion); |
| 2902 | |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 2903 | String[] suggestions = targetSuggestionSpan.getSuggestions(); |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 2904 | suggestions[suggestionInfo.mSuggestionIndex] = originalText; |
| 2905 | |
| 2906 | // Restore previous SuggestionSpans |
| 2907 | final int lengthDelta = suggestion.length() - (spanEnd - spanStart); |
| 2908 | for (int i = 0; i < length; i++) { |
| 2909 | // Only spans that include the modified region make sense after replacement |
| 2910 | // Spans partially included in the replaced region are removed, there is no |
| 2911 | // way to assign them a valid range after replacement |
| 2912 | if (suggestionSpansStarts[i] <= spanStart && suggestionSpansEnds[i] >= spanEnd) { |
| 2913 | mTextView.setSpan_internal(suggestionSpans[i], suggestionSpansStarts[i], |
| 2914 | suggestionSpansEnds[i] + lengthDelta, suggestionSpansFlags[i]); |
| 2915 | } |
| 2916 | } |
| 2917 | // Move cursor at the end of the replaced word |
| 2918 | final int newCursorPosition = spanEnd + lengthDelta; |
| 2919 | mTextView.setCursorPosition_internal(newCursorPosition, newCursorPosition); |
| 2920 | } |
| 2921 | |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 2922 | private final MenuItem.OnMenuItemClickListener mOnContextMenuItemClickListener = |
| 2923 | new MenuItem.OnMenuItemClickListener() { |
| 2924 | @Override |
| 2925 | public boolean onMenuItemClick(MenuItem item) { |
| 2926 | if (mProcessTextIntentActionsHandler.performMenuItemAction(item)) { |
| 2927 | return true; |
| 2928 | } |
| 2929 | return mTextView.onTextContextMenuItem(item.getItemId()); |
| 2930 | } |
| 2931 | }; |
| 2932 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2933 | /** |
| 2934 | * Controls the {@link EasyEditSpan} monitoring when it is added, and when the related |
| 2935 | * pop-up should be displayed. |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 2936 | * Also monitors {@link Selection} to call back to the attached input method. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2937 | */ |
Keisuke Kuroyanagi | c5a4320 | 2016-06-10 16:27:27 +0900 | [diff] [blame] | 2938 | private class SpanController implements SpanWatcher { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2939 | |
| 2940 | private static final int DISPLAY_TIMEOUT_MS = 3000; // 3 secs |
| 2941 | |
| 2942 | private EasyEditPopupWindow mPopupWindow; |
| 2943 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 2944 | private Runnable mHidePopup; |
| 2945 | |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 2946 | // This function is pure but inner classes can't have static functions |
| 2947 | private boolean isNonIntermediateSelectionSpan(final Spannable text, |
| 2948 | final Object span) { |
| 2949 | return (Selection.SELECTION_START == span || Selection.SELECTION_END == span) |
| 2950 | && (text.getSpanFlags(span) & Spanned.SPAN_INTERMEDIATE) == 0; |
| 2951 | } |
| 2952 | |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2953 | @Override |
| 2954 | public void onSpanAdded(Spannable text, Object span, int start, int end) { |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 2955 | if (isNonIntermediateSelectionSpan(text, span)) { |
| 2956 | sendUpdateSelection(); |
| 2957 | } else if (span instanceof EasyEditSpan) { |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2958 | if (mPopupWindow == null) { |
| 2959 | mPopupWindow = new EasyEditPopupWindow(); |
| 2960 | mHidePopup = new Runnable() { |
| 2961 | @Override |
| 2962 | public void run() { |
| 2963 | hide(); |
| 2964 | } |
| 2965 | }; |
| 2966 | } |
| 2967 | |
| 2968 | // Make sure there is only at most one EasyEditSpan in the text |
| 2969 | if (mPopupWindow.mEasyEditSpan != null) { |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 2970 | mPopupWindow.mEasyEditSpan.setDeleteEnabled(false); |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | mPopupWindow.setEasyEditSpan((EasyEditSpan) span); |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 2974 | mPopupWindow.setOnDeleteListener(new EasyEditDeleteListener() { |
| 2975 | @Override |
| 2976 | public void onDeleteClick(EasyEditSpan span) { |
| 2977 | Editable editable = (Editable) mTextView.getText(); |
| 2978 | int start = editable.getSpanStart(span); |
| 2979 | int end = editable.getSpanEnd(span); |
| 2980 | if (start >= 0 && end >= 0) { |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 2981 | sendEasySpanNotification(EasyEditSpan.TEXT_DELETED, span); |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 2982 | mTextView.deleteText_internal(start, end); |
| 2983 | } |
| 2984 | editable.removeSpan(span); |
| 2985 | } |
| 2986 | }); |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 2987 | |
| 2988 | if (mTextView.getWindowVisibility() != View.VISIBLE) { |
| 2989 | // The window is not visible yet, ignore the text change. |
| 2990 | return; |
| 2991 | } |
| 2992 | |
| 2993 | if (mTextView.getLayout() == null) { |
| 2994 | // The view has not been laid out yet, ignore the text change |
| 2995 | return; |
| 2996 | } |
| 2997 | |
| 2998 | if (extractedTextModeWillBeStarted()) { |
| 2999 | // The input is in extract mode. Do not handle the easy edit in |
| 3000 | // the original TextView, as the ExtractEditText will do |
| 3001 | return; |
| 3002 | } |
| 3003 | |
| 3004 | mPopupWindow.show(); |
| 3005 | mTextView.removeCallbacks(mHidePopup); |
| 3006 | mTextView.postDelayed(mHidePopup, DISPLAY_TIMEOUT_MS); |
| 3007 | } |
| 3008 | } |
| 3009 | |
| 3010 | @Override |
| 3011 | public void onSpanRemoved(Spannable text, Object span, int start, int end) { |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 3012 | if (isNonIntermediateSelectionSpan(text, span)) { |
| 3013 | sendUpdateSelection(); |
| 3014 | } else if (mPopupWindow != null && span == mPopupWindow.mEasyEditSpan) { |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 3015 | hide(); |
| 3016 | } |
| 3017 | } |
| 3018 | |
| 3019 | @Override |
| 3020 | public void onSpanChanged(Spannable text, Object span, int previousStart, int previousEnd, |
| 3021 | int newStart, int newEnd) { |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 3022 | if (isNonIntermediateSelectionSpan(text, span)) { |
| 3023 | sendUpdateSelection(); |
| 3024 | } else if (mPopupWindow != null && span instanceof EasyEditSpan) { |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 3025 | EasyEditSpan easyEditSpan = (EasyEditSpan) span; |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 3026 | sendEasySpanNotification(EasyEditSpan.TEXT_MODIFIED, easyEditSpan); |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 3027 | text.removeSpan(easyEditSpan); |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 3028 | } |
| 3029 | } |
| 3030 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3031 | public void hide() { |
| 3032 | if (mPopupWindow != null) { |
| 3033 | mPopupWindow.hide(); |
| 3034 | mTextView.removeCallbacks(mHidePopup); |
| 3035 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3036 | } |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 3037 | |
Jean Chalard | baf3094 | 2013-02-28 16:01:51 -0800 | [diff] [blame] | 3038 | private void sendEasySpanNotification(int textChangedType, EasyEditSpan span) { |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 3039 | try { |
| 3040 | PendingIntent pendingIntent = span.getPendingIntent(); |
| 3041 | if (pendingIntent != null) { |
| 3042 | Intent intent = new Intent(); |
| 3043 | intent.putExtra(EasyEditSpan.EXTRA_TEXT_CHANGED_TYPE, textChangedType); |
| 3044 | pendingIntent.send(mTextView.getContext(), 0, intent); |
| 3045 | } |
| 3046 | } catch (CanceledException e) { |
| 3047 | // This should not happen, as we should try to send the intent only once. |
| 3048 | Log.w(TAG, "PendingIntent for notification cannot be sent", e); |
| 3049 | } |
| 3050 | } |
| 3051 | } |
| 3052 | |
| 3053 | /** |
| 3054 | * Listens for the delete event triggered by {@link EasyEditPopupWindow}. |
| 3055 | */ |
| 3056 | private interface EasyEditDeleteListener { |
| 3057 | |
| 3058 | /** |
| 3059 | * Clicks the delete pop-up. |
| 3060 | */ |
| 3061 | void onDeleteClick(EasyEditSpan span); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | /** |
| 3065 | * 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] | 3066 | * by {@link SpanController}. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3067 | */ |
| 3068 | private class EasyEditPopupWindow extends PinnedPopupWindow |
| 3069 | implements OnClickListener { |
| 3070 | private static final int POPUP_TEXT_LAYOUT = |
| 3071 | com.android.internal.R.layout.text_edit_action_popup_text; |
| 3072 | private TextView mDeleteTextView; |
| 3073 | private EasyEditSpan mEasyEditSpan; |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 3074 | private EasyEditDeleteListener mOnDeleteListener; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3075 | |
| 3076 | @Override |
| 3077 | protected void createPopupWindow() { |
| 3078 | mPopupWindow = new PopupWindow(mTextView.getContext(), null, |
| 3079 | com.android.internal.R.attr.textSelectHandleWindowStyle); |
| 3080 | mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); |
| 3081 | mPopupWindow.setClippingEnabled(true); |
| 3082 | } |
| 3083 | |
| 3084 | @Override |
| 3085 | protected void initContentView() { |
| 3086 | LinearLayout linearLayout = new LinearLayout(mTextView.getContext()); |
| 3087 | linearLayout.setOrientation(LinearLayout.HORIZONTAL); |
| 3088 | mContentView = linearLayout; |
| 3089 | mContentView.setBackgroundResource( |
| 3090 | com.android.internal.R.drawable.text_edit_side_paste_window); |
| 3091 | |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 3092 | LayoutInflater inflater = (LayoutInflater) mTextView.getContext() |
| 3093 | .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3094 | |
| 3095 | LayoutParams wrapContent = new LayoutParams( |
| 3096 | ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
| 3097 | |
| 3098 | mDeleteTextView = (TextView) inflater.inflate(POPUP_TEXT_LAYOUT, null); |
| 3099 | mDeleteTextView.setLayoutParams(wrapContent); |
| 3100 | mDeleteTextView.setText(com.android.internal.R.string.delete); |
| 3101 | mDeleteTextView.setOnClickListener(this); |
| 3102 | mContentView.addView(mDeleteTextView); |
| 3103 | } |
| 3104 | |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 3105 | public void setEasyEditSpan(EasyEditSpan easyEditSpan) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3106 | mEasyEditSpan = easyEditSpan; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3107 | } |
| 3108 | |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 3109 | private void setOnDeleteListener(EasyEditDeleteListener listener) { |
| 3110 | mOnDeleteListener = listener; |
| 3111 | } |
| 3112 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3113 | @Override |
| 3114 | public void onClick(View view) { |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 3115 | if (view == mDeleteTextView |
| 3116 | && mEasyEditSpan != null && mEasyEditSpan.isDeleteEnabled() |
| 3117 | && mOnDeleteListener != null) { |
| 3118 | mOnDeleteListener.onDeleteClick(mEasyEditSpan); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3119 | } |
| 3120 | } |
| 3121 | |
| 3122 | @Override |
Luca Zanolin | 1b15ba5 | 2013-02-20 14:31:37 +0000 | [diff] [blame] | 3123 | public void hide() { |
| 3124 | if (mEasyEditSpan != null) { |
| 3125 | mEasyEditSpan.setDeleteEnabled(false); |
| 3126 | } |
| 3127 | mOnDeleteListener = null; |
| 3128 | super.hide(); |
| 3129 | } |
| 3130 | |
| 3131 | @Override |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3132 | protected int getTextOffset() { |
| 3133 | // Place the pop-up at the end of the span |
| 3134 | Editable editable = (Editable) mTextView.getText(); |
| 3135 | return editable.getSpanEnd(mEasyEditSpan); |
| 3136 | } |
| 3137 | |
| 3138 | @Override |
| 3139 | protected int getVerticalLocalPosition(int line) { |
Siyamed Sinir | a60b59d | 2017-07-26 09:26:41 -0700 | [diff] [blame] | 3140 | final Layout layout = mTextView.getLayout(); |
| 3141 | return layout.getLineBottomWithoutSpacing(line); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3142 | } |
| 3143 | |
| 3144 | @Override |
| 3145 | protected int clipVertically(int positionY) { |
| 3146 | // As we display the pop-up below the span, no vertical clipping is required. |
| 3147 | return positionY; |
| 3148 | } |
| 3149 | } |
| 3150 | |
| 3151 | private class PositionListener implements ViewTreeObserver.OnPreDrawListener { |
| 3152 | // 3 handles |
| 3153 | // 3 ActionPopup [replace, suggestion, easyedit] (suggestionsPopup first hides the others) |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 3154 | // 1 CursorAnchorInfoNotifier |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 3155 | private static final int MAXIMUM_NUMBER_OF_LISTENERS = 7; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3156 | private TextViewPositionListener[] mPositionListeners = |
| 3157 | new TextViewPositionListener[MAXIMUM_NUMBER_OF_LISTENERS]; |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 3158 | private boolean[] mCanMove = new boolean[MAXIMUM_NUMBER_OF_LISTENERS]; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3159 | private boolean mPositionHasChanged = true; |
| 3160 | // Absolute position of the TextView with respect to its parent window |
| 3161 | private int mPositionX, mPositionY; |
Keisuke Kuroyanagi | e2a3b1e | 2016-04-28 12:55:18 +0900 | [diff] [blame] | 3162 | private int mPositionXOnScreen, mPositionYOnScreen; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3163 | private int mNumberOfListeners; |
| 3164 | private boolean mScrollHasChanged; |
| 3165 | final int[] mTempCoords = new int[2]; |
| 3166 | |
| 3167 | public void addSubscriber(TextViewPositionListener positionListener, boolean canMove) { |
| 3168 | if (mNumberOfListeners == 0) { |
| 3169 | updatePosition(); |
| 3170 | ViewTreeObserver vto = mTextView.getViewTreeObserver(); |
| 3171 | vto.addOnPreDrawListener(this); |
| 3172 | } |
| 3173 | |
| 3174 | int emptySlotIndex = -1; |
| 3175 | for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { |
| 3176 | TextViewPositionListener listener = mPositionListeners[i]; |
| 3177 | if (listener == positionListener) { |
| 3178 | return; |
| 3179 | } else if (emptySlotIndex < 0 && listener == null) { |
| 3180 | emptySlotIndex = i; |
| 3181 | } |
| 3182 | } |
| 3183 | |
| 3184 | mPositionListeners[emptySlotIndex] = positionListener; |
| 3185 | mCanMove[emptySlotIndex] = canMove; |
| 3186 | mNumberOfListeners++; |
| 3187 | } |
| 3188 | |
| 3189 | public void removeSubscriber(TextViewPositionListener positionListener) { |
| 3190 | for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { |
| 3191 | if (mPositionListeners[i] == positionListener) { |
| 3192 | mPositionListeners[i] = null; |
| 3193 | mNumberOfListeners--; |
| 3194 | break; |
| 3195 | } |
| 3196 | } |
| 3197 | |
| 3198 | if (mNumberOfListeners == 0) { |
| 3199 | ViewTreeObserver vto = mTextView.getViewTreeObserver(); |
| 3200 | vto.removeOnPreDrawListener(this); |
| 3201 | } |
| 3202 | } |
| 3203 | |
| 3204 | public int getPositionX() { |
| 3205 | return mPositionX; |
| 3206 | } |
| 3207 | |
| 3208 | public int getPositionY() { |
| 3209 | return mPositionY; |
| 3210 | } |
| 3211 | |
Keisuke Kuroyanagi | e2a3b1e | 2016-04-28 12:55:18 +0900 | [diff] [blame] | 3212 | public int getPositionXOnScreen() { |
| 3213 | return mPositionXOnScreen; |
| 3214 | } |
| 3215 | |
| 3216 | public int getPositionYOnScreen() { |
| 3217 | return mPositionYOnScreen; |
| 3218 | } |
| 3219 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3220 | @Override |
| 3221 | public boolean onPreDraw() { |
| 3222 | updatePosition(); |
| 3223 | |
| 3224 | for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { |
| 3225 | if (mPositionHasChanged || mScrollHasChanged || mCanMove[i]) { |
| 3226 | TextViewPositionListener positionListener = mPositionListeners[i]; |
| 3227 | if (positionListener != null) { |
| 3228 | positionListener.updatePosition(mPositionX, mPositionY, |
| 3229 | mPositionHasChanged, mScrollHasChanged); |
| 3230 | } |
| 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | mScrollHasChanged = false; |
| 3235 | return true; |
| 3236 | } |
| 3237 | |
| 3238 | private void updatePosition() { |
| 3239 | mTextView.getLocationInWindow(mTempCoords); |
| 3240 | |
| 3241 | mPositionHasChanged = mTempCoords[0] != mPositionX || mTempCoords[1] != mPositionY; |
| 3242 | |
| 3243 | mPositionX = mTempCoords[0]; |
| 3244 | mPositionY = mTempCoords[1]; |
Keisuke Kuroyanagi | e2a3b1e | 2016-04-28 12:55:18 +0900 | [diff] [blame] | 3245 | |
| 3246 | mTextView.getLocationOnScreen(mTempCoords); |
| 3247 | |
| 3248 | mPositionXOnScreen = mTempCoords[0]; |
| 3249 | mPositionYOnScreen = mTempCoords[1]; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3250 | } |
| 3251 | |
| 3252 | public void onScrollChanged() { |
| 3253 | mScrollHasChanged = true; |
| 3254 | } |
| 3255 | } |
| 3256 | |
| 3257 | private abstract class PinnedPopupWindow implements TextViewPositionListener { |
| 3258 | protected PopupWindow mPopupWindow; |
| 3259 | protected ViewGroup mContentView; |
| 3260 | int mPositionX, mPositionY; |
Seigo Nonaka | 60490d1 | 2016-01-28 17:25:18 +0900 | [diff] [blame] | 3261 | int mClippingLimitLeft, mClippingLimitRight; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3262 | |
| 3263 | protected abstract void createPopupWindow(); |
| 3264 | protected abstract void initContentView(); |
| 3265 | protected abstract int getTextOffset(); |
| 3266 | protected abstract int getVerticalLocalPosition(int line); |
| 3267 | protected abstract int clipVertically(int positionY); |
Seigo Nonaka | e9f54bf | 2016-05-12 18:18:12 +0900 | [diff] [blame] | 3268 | protected void setUp() { |
| 3269 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3270 | |
| 3271 | public PinnedPopupWindow() { |
Seigo Nonaka | e9f54bf | 2016-05-12 18:18:12 +0900 | [diff] [blame] | 3272 | // Due to calling subclass methods in base constructor, subclass constructor is not |
| 3273 | // called before subclass methods, e.g. createPopupWindow or initContentView. To give |
| 3274 | // a chance to initialize subclasses, call setUp() method here. |
| 3275 | // TODO: It is good to extract non trivial initialization code from constructor. |
| 3276 | setUp(); |
| 3277 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3278 | createPopupWindow(); |
| 3279 | |
Alan Viverette | 80ebe0d | 2015-04-30 15:53:11 -0700 | [diff] [blame] | 3280 | mPopupWindow.setWindowLayoutType( |
| 3281 | WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3282 | mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); |
| 3283 | mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); |
| 3284 | |
| 3285 | initContentView(); |
| 3286 | |
| 3287 | LayoutParams wrapContent = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, |
| 3288 | ViewGroup.LayoutParams.WRAP_CONTENT); |
| 3289 | mContentView.setLayoutParams(wrapContent); |
| 3290 | |
| 3291 | mPopupWindow.setContentView(mContentView); |
| 3292 | } |
| 3293 | |
| 3294 | public void show() { |
| 3295 | getPositionListener().addSubscriber(this, false /* offset is fixed */); |
| 3296 | |
| 3297 | computeLocalPosition(); |
| 3298 | |
| 3299 | final PositionListener positionListener = getPositionListener(); |
| 3300 | updatePosition(positionListener.getPositionX(), positionListener.getPositionY()); |
| 3301 | } |
| 3302 | |
| 3303 | protected void measureContent() { |
| 3304 | final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics(); |
| 3305 | mContentView.measure( |
| 3306 | View.MeasureSpec.makeMeasureSpec(displayMetrics.widthPixels, |
| 3307 | View.MeasureSpec.AT_MOST), |
| 3308 | View.MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels, |
| 3309 | View.MeasureSpec.AT_MOST)); |
| 3310 | } |
| 3311 | |
| 3312 | /* The popup window will be horizontally centered on the getTextOffset() and vertically |
| 3313 | * positioned according to viewportToContentHorizontalOffset. |
| 3314 | * |
| 3315 | * This method assumes that mContentView has properly been measured from its content. */ |
| 3316 | private void computeLocalPosition() { |
| 3317 | measureContent(); |
| 3318 | final int width = mContentView.getMeasuredWidth(); |
| 3319 | final int offset = getTextOffset(); |
| 3320 | mPositionX = (int) (mTextView.getLayout().getPrimaryHorizontal(offset) - width / 2.0f); |
| 3321 | mPositionX += mTextView.viewportToContentHorizontalOffset(); |
| 3322 | |
| 3323 | final int line = mTextView.getLayout().getLineForOffset(offset); |
| 3324 | mPositionY = getVerticalLocalPosition(line); |
| 3325 | mPositionY += mTextView.viewportToContentVerticalOffset(); |
| 3326 | } |
| 3327 | |
| 3328 | private void updatePosition(int parentPositionX, int parentPositionY) { |
| 3329 | int positionX = parentPositionX + mPositionX; |
| 3330 | int positionY = parentPositionY + mPositionY; |
| 3331 | |
| 3332 | positionY = clipVertically(positionY); |
| 3333 | |
| 3334 | // Horizontal clipping |
| 3335 | final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics(); |
| 3336 | final int width = mContentView.getMeasuredWidth(); |
Seigo Nonaka | 60490d1 | 2016-01-28 17:25:18 +0900 | [diff] [blame] | 3337 | positionX = Math.min( |
| 3338 | displayMetrics.widthPixels - width + mClippingLimitRight, positionX); |
| 3339 | positionX = Math.max(-mClippingLimitLeft, positionX); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3340 | |
| 3341 | if (isShowing()) { |
| 3342 | mPopupWindow.update(positionX, positionY, -1, -1); |
| 3343 | } else { |
| 3344 | mPopupWindow.showAtLocation(mTextView, Gravity.NO_GRAVITY, |
| 3345 | positionX, positionY); |
| 3346 | } |
| 3347 | } |
| 3348 | |
| 3349 | public void hide() { |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 3350 | if (!isShowing()) { |
| 3351 | return; |
| 3352 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3353 | mPopupWindow.dismiss(); |
| 3354 | getPositionListener().removeSubscriber(this); |
| 3355 | } |
| 3356 | |
| 3357 | @Override |
| 3358 | public void updatePosition(int parentPositionX, int parentPositionY, |
| 3359 | boolean parentPositionChanged, boolean parentScrolled) { |
| 3360 | // Either parentPositionChanged or parentScrolled is true, check if still visible |
| 3361 | if (isShowing() && isOffsetVisible(getTextOffset())) { |
| 3362 | if (parentScrolled) computeLocalPosition(); |
| 3363 | updatePosition(parentPositionX, parentPositionY); |
| 3364 | } else { |
| 3365 | hide(); |
| 3366 | } |
| 3367 | } |
| 3368 | |
| 3369 | public boolean isShowing() { |
| 3370 | return mPopupWindow.isShowing(); |
| 3371 | } |
| 3372 | } |
| 3373 | |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3374 | private static final class SuggestionInfo { |
Keisuke Kuroyanagi | d3194503 | 2016-02-26 16:09:12 -0800 | [diff] [blame] | 3375 | // Range of actual suggestion within mText |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3376 | int mSuggestionStart, mSuggestionEnd; |
| 3377 | |
| 3378 | // The SuggestionSpan that this TextView represents |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3379 | final SuggestionSpanInfo mSuggestionSpanInfo = new SuggestionSpanInfo(); |
Keisuke Kuroyanagi | d3194503 | 2016-02-26 16:09:12 -0800 | [diff] [blame] | 3380 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3381 | // The index of this suggestion inside suggestionSpan |
| 3382 | int mSuggestionIndex; |
| 3383 | |
| 3384 | final SpannableStringBuilder mText = new SpannableStringBuilder(); |
| 3385 | |
| 3386 | void clear() { |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3387 | mSuggestionSpanInfo.clear(); |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3388 | mText.clear(); |
| 3389 | } |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3390 | |
| 3391 | // Utility method to set attributes about a SuggestionSpan. |
| 3392 | void setSpanInfo(SuggestionSpan span, int spanStart, int spanEnd) { |
| 3393 | mSuggestionSpanInfo.mSuggestionSpan = span; |
| 3394 | mSuggestionSpanInfo.mSpanStart = spanStart; |
| 3395 | mSuggestionSpanInfo.mSpanEnd = spanEnd; |
| 3396 | } |
| 3397 | } |
| 3398 | |
| 3399 | private static final class SuggestionSpanInfo { |
| 3400 | // The SuggestionSpan; |
| 3401 | @Nullable |
| 3402 | SuggestionSpan mSuggestionSpan; |
| 3403 | |
| 3404 | // The SuggestionSpan start position |
| 3405 | int mSpanStart; |
| 3406 | |
| 3407 | // The SuggestionSpan end position |
| 3408 | int mSpanEnd; |
| 3409 | |
| 3410 | void clear() { |
| 3411 | mSuggestionSpan = null; |
| 3412 | } |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | private class SuggestionHelper { |
| 3416 | private final Comparator<SuggestionSpan> mSuggestionSpanComparator = |
| 3417 | new SuggestionSpanComparator(); |
| 3418 | private final HashMap<SuggestionSpan, Integer> mSpansLengths = |
| 3419 | new HashMap<SuggestionSpan, Integer>(); |
| 3420 | |
| 3421 | private class SuggestionSpanComparator implements Comparator<SuggestionSpan> { |
| 3422 | public int compare(SuggestionSpan span1, SuggestionSpan span2) { |
| 3423 | final int flag1 = span1.getFlags(); |
| 3424 | final int flag2 = span2.getFlags(); |
| 3425 | if (flag1 != flag2) { |
| 3426 | // The order here should match what is used in updateDrawState |
| 3427 | final boolean easy1 = (flag1 & SuggestionSpan.FLAG_EASY_CORRECT) != 0; |
| 3428 | final boolean easy2 = (flag2 & SuggestionSpan.FLAG_EASY_CORRECT) != 0; |
| 3429 | final boolean misspelled1 = (flag1 & SuggestionSpan.FLAG_MISSPELLED) != 0; |
| 3430 | final boolean misspelled2 = (flag2 & SuggestionSpan.FLAG_MISSPELLED) != 0; |
| 3431 | if (easy1 && !misspelled1) return -1; |
| 3432 | if (easy2 && !misspelled2) return 1; |
| 3433 | if (misspelled1) return -1; |
| 3434 | if (misspelled2) return 1; |
| 3435 | } |
| 3436 | |
| 3437 | return mSpansLengths.get(span1).intValue() - mSpansLengths.get(span2).intValue(); |
| 3438 | } |
| 3439 | } |
| 3440 | |
| 3441 | /** |
| 3442 | * Returns the suggestion spans that cover the current cursor position. The suggestion |
| 3443 | * spans are sorted according to the length of text that they are attached to. |
| 3444 | */ |
| 3445 | private SuggestionSpan[] getSortedSuggestionSpans() { |
| 3446 | int pos = mTextView.getSelectionStart(); |
| 3447 | Spannable spannable = (Spannable) mTextView.getText(); |
| 3448 | SuggestionSpan[] suggestionSpans = spannable.getSpans(pos, pos, SuggestionSpan.class); |
| 3449 | |
| 3450 | mSpansLengths.clear(); |
| 3451 | for (SuggestionSpan suggestionSpan : suggestionSpans) { |
| 3452 | int start = spannable.getSpanStart(suggestionSpan); |
| 3453 | int end = spannable.getSpanEnd(suggestionSpan); |
| 3454 | mSpansLengths.put(suggestionSpan, Integer.valueOf(end - start)); |
| 3455 | } |
| 3456 | |
| 3457 | // The suggestions are sorted according to their types (easy correction first, then |
| 3458 | // misspelled) and to the length of the text that they cover (shorter first). |
| 3459 | Arrays.sort(suggestionSpans, mSuggestionSpanComparator); |
| 3460 | mSpansLengths.clear(); |
| 3461 | |
| 3462 | return suggestionSpans; |
| 3463 | } |
| 3464 | |
| 3465 | /** |
| 3466 | * Gets the SuggestionInfo list that contains suggestion information at the current cursor |
| 3467 | * position. |
| 3468 | * |
| 3469 | * @param suggestionInfos SuggestionInfo array the results will be set. |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3470 | * @param misspelledSpanInfo a struct the misspelled SuggestionSpan info will be set. |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3471 | * @return the number of suggestions actually fetched. |
| 3472 | */ |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3473 | public int getSuggestionInfo(SuggestionInfo[] suggestionInfos, |
| 3474 | @Nullable SuggestionSpanInfo misspelledSpanInfo) { |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3475 | final Spannable spannable = (Spannable) mTextView.getText(); |
| 3476 | final SuggestionSpan[] suggestionSpans = getSortedSuggestionSpans(); |
| 3477 | final int nbSpans = suggestionSpans.length; |
| 3478 | if (nbSpans == 0) return 0; |
| 3479 | |
| 3480 | int numberOfSuggestions = 0; |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3481 | for (final SuggestionSpan suggestionSpan : suggestionSpans) { |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3482 | final int spanStart = spannable.getSpanStart(suggestionSpan); |
| 3483 | final int spanEnd = spannable.getSpanEnd(suggestionSpan); |
| 3484 | |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3485 | if (misspelledSpanInfo != null |
| 3486 | && (suggestionSpan.getFlags() & SuggestionSpan.FLAG_MISSPELLED) != 0) { |
| 3487 | misspelledSpanInfo.mSuggestionSpan = suggestionSpan; |
| 3488 | misspelledSpanInfo.mSpanStart = spanStart; |
| 3489 | misspelledSpanInfo.mSpanEnd = spanEnd; |
| 3490 | } |
| 3491 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3492 | final String[] suggestions = suggestionSpan.getSuggestions(); |
| 3493 | final int nbSuggestions = suggestions.length; |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3494 | suggestionLoop: |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3495 | for (int suggestionIndex = 0; suggestionIndex < nbSuggestions; suggestionIndex++) { |
| 3496 | final String suggestion = suggestions[suggestionIndex]; |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3497 | for (int i = 0; i < numberOfSuggestions; i++) { |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3498 | final SuggestionInfo otherSuggestionInfo = suggestionInfos[i]; |
| 3499 | if (otherSuggestionInfo.mText.toString().equals(suggestion)) { |
| 3500 | final int otherSpanStart = |
| 3501 | otherSuggestionInfo.mSuggestionSpanInfo.mSpanStart; |
| 3502 | final int otherSpanEnd = |
| 3503 | otherSuggestionInfo.mSuggestionSpanInfo.mSpanEnd; |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3504 | if (spanStart == otherSpanStart && spanEnd == otherSpanEnd) { |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3505 | continue suggestionLoop; |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3506 | } |
| 3507 | } |
| 3508 | } |
| 3509 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3510 | SuggestionInfo suggestionInfo = suggestionInfos[numberOfSuggestions]; |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3511 | suggestionInfo.setSpanInfo(suggestionSpan, spanStart, spanEnd); |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3512 | suggestionInfo.mSuggestionIndex = suggestionIndex; |
| 3513 | suggestionInfo.mSuggestionStart = 0; |
| 3514 | suggestionInfo.mSuggestionEnd = suggestion.length(); |
| 3515 | suggestionInfo.mText.replace(0, suggestionInfo.mText.length(), suggestion); |
| 3516 | numberOfSuggestions++; |
| 3517 | if (numberOfSuggestions >= suggestionInfos.length) { |
| 3518 | return numberOfSuggestions; |
| 3519 | } |
| 3520 | } |
| 3521 | } |
| 3522 | return numberOfSuggestions; |
| 3523 | } |
| 3524 | } |
| 3525 | |
Yohei Yukawa | ca9376c | 2019-02-01 23:38:30 -0800 | [diff] [blame] | 3526 | private final class SuggestionsPopupWindow extends PinnedPopupWindow |
| 3527 | implements OnItemClickListener { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3528 | private static final int MAX_NUMBER_SUGGESTIONS = SuggestionSpan.SUGGESTIONS_MAX_SIZE; |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3529 | |
| 3530 | // Key of intent extras for inserting new word into user dictionary. |
| 3531 | private static final String USER_DICTIONARY_EXTRA_WORD = "word"; |
| 3532 | private static final String USER_DICTIONARY_EXTRA_LOCALE = "locale"; |
| 3533 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3534 | private SuggestionInfo[] mSuggestionInfos; |
| 3535 | private int mNumberOfSuggestions; |
| 3536 | private boolean mCursorWasVisibleBeforeSuggestions; |
| 3537 | private boolean mIsShowingUp = false; |
| 3538 | private SuggestionAdapter mSuggestionsAdapter; |
Seigo Nonaka | e9f54bf | 2016-05-12 18:18:12 +0900 | [diff] [blame] | 3539 | private TextAppearanceSpan mHighlightSpan; // TODO: Make mHighlightSpan final. |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3540 | private TextView mAddToDictionaryButton; |
| 3541 | private TextView mDeleteButton; |
Seigo Nonaka | f47976e | 2016-03-01 09:17:37 -0800 | [diff] [blame] | 3542 | private ListView mSuggestionListView; |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3543 | private final SuggestionSpanInfo mMisspelledSpanInfo = new SuggestionSpanInfo(); |
Seigo Nonaka | 60490d1 | 2016-01-28 17:25:18 +0900 | [diff] [blame] | 3544 | private int mContainerMarginWidth; |
| 3545 | private int mContainerMarginTop; |
Seigo Nonaka | 77972bb | 2016-04-20 15:45:34 +0900 | [diff] [blame] | 3546 | private LinearLayout mContainerView; |
Seigo Nonaka | e9f54bf | 2016-05-12 18:18:12 +0900 | [diff] [blame] | 3547 | private Context mContext; // TODO: Make mContext final. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3548 | |
| 3549 | private class CustomPopupWindow extends PopupWindow { |
Seigo Nonaka | e9f54bf | 2016-05-12 18:18:12 +0900 | [diff] [blame] | 3550 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3551 | @Override |
| 3552 | public void dismiss() { |
Keisuke Kuroyanagi | d056081 | 2015-12-17 17:50:42 +0900 | [diff] [blame] | 3553 | if (!isShowing()) { |
| 3554 | return; |
| 3555 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3556 | super.dismiss(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3557 | getPositionListener().removeSubscriber(SuggestionsPopupWindow.this); |
| 3558 | |
| 3559 | // Safe cast since show() checks that mTextView.getText() is an Editable |
| 3560 | ((Spannable) mTextView.getText()).removeSpan(mSuggestionRangeSpan); |
| 3561 | |
| 3562 | mTextView.setCursorVisible(mCursorWasVisibleBeforeSuggestions); |
Keisuke Kuroyanagi | 4a696ac | 2016-02-23 11:02:07 -0800 | [diff] [blame] | 3563 | if (hasInsertionController() && !extractedTextModeWillBeStarted()) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3564 | getInsertionController().show(); |
| 3565 | } |
| 3566 | } |
| 3567 | } |
| 3568 | |
| 3569 | public SuggestionsPopupWindow() { |
| 3570 | mCursorWasVisibleBeforeSuggestions = mCursorVisible; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | @Override |
Seigo Nonaka | e9f54bf | 2016-05-12 18:18:12 +0900 | [diff] [blame] | 3574 | protected void setUp() { |
| 3575 | mContext = applyDefaultTheme(mTextView.getContext()); |
| 3576 | mHighlightSpan = new TextAppearanceSpan(mContext, |
| 3577 | mTextView.mTextEditSuggestionHighlightStyle); |
| 3578 | } |
| 3579 | |
| 3580 | private Context applyDefaultTheme(Context originalContext) { |
| 3581 | TypedArray a = originalContext.obtainStyledAttributes( |
| 3582 | new int[]{com.android.internal.R.attr.isLightTheme}); |
| 3583 | boolean isLightTheme = a.getBoolean(0, true); |
| 3584 | int themeId = isLightTheme ? R.style.ThemeOverlay_Material_Light |
| 3585 | : R.style.ThemeOverlay_Material_Dark; |
| 3586 | a.recycle(); |
| 3587 | return new ContextThemeWrapper(originalContext, themeId); |
| 3588 | } |
| 3589 | |
| 3590 | @Override |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3591 | protected void createPopupWindow() { |
Seigo Nonaka | 3ed1b39 | 2016-01-19 13:54:59 +0900 | [diff] [blame] | 3592 | mPopupWindow = new CustomPopupWindow(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3593 | mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); |
Seigo Nonaka | 3ed1b39 | 2016-01-19 13:54:59 +0900 | [diff] [blame] | 3594 | mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3595 | mPopupWindow.setFocusable(true); |
| 3596 | mPopupWindow.setClippingEnabled(false); |
| 3597 | } |
| 3598 | |
| 3599 | @Override |
| 3600 | protected void initContentView() { |
Seigo Nonaka | e9f54bf | 2016-05-12 18:18:12 +0900 | [diff] [blame] | 3601 | final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( |
| 3602 | Context.LAYOUT_INFLATER_SERVICE); |
Seigo Nonaka | 77972bb | 2016-04-20 15:45:34 +0900 | [diff] [blame] | 3603 | mContentView = (ViewGroup) inflater.inflate( |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3604 | mTextView.mTextEditSuggestionContainerLayout, null); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3605 | |
Seigo Nonaka | 77972bb | 2016-04-20 15:45:34 +0900 | [diff] [blame] | 3606 | mContainerView = (LinearLayout) mContentView.findViewById( |
| 3607 | com.android.internal.R.id.suggestionWindowContainer); |
Seigo Nonaka | 60490d1 | 2016-01-28 17:25:18 +0900 | [diff] [blame] | 3608 | ViewGroup.MarginLayoutParams lp = |
Seigo Nonaka | 77972bb | 2016-04-20 15:45:34 +0900 | [diff] [blame] | 3609 | (ViewGroup.MarginLayoutParams) mContainerView.getLayoutParams(); |
Seigo Nonaka | 60490d1 | 2016-01-28 17:25:18 +0900 | [diff] [blame] | 3610 | mContainerMarginWidth = lp.leftMargin + lp.rightMargin; |
| 3611 | mContainerMarginTop = lp.topMargin; |
| 3612 | mClippingLimitLeft = lp.leftMargin; |
| 3613 | mClippingLimitRight = lp.rightMargin; |
| 3614 | |
Seigo Nonaka | 77972bb | 2016-04-20 15:45:34 +0900 | [diff] [blame] | 3615 | mSuggestionListView = (ListView) mContentView.findViewById( |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3616 | com.android.internal.R.id.suggestionContainer); |
| 3617 | |
| 3618 | mSuggestionsAdapter = new SuggestionAdapter(); |
Seigo Nonaka | f47976e | 2016-03-01 09:17:37 -0800 | [diff] [blame] | 3619 | mSuggestionListView.setAdapter(mSuggestionsAdapter); |
| 3620 | mSuggestionListView.setOnItemClickListener(this); |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3621 | |
| 3622 | // Inflate the suggestion items once and for all. |
| 3623 | mSuggestionInfos = new SuggestionInfo[MAX_NUMBER_SUGGESTIONS]; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3624 | for (int i = 0; i < mSuggestionInfos.length; i++) { |
| 3625 | mSuggestionInfos[i] = new SuggestionInfo(); |
| 3626 | } |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3627 | |
Seigo Nonaka | 77972bb | 2016-04-20 15:45:34 +0900 | [diff] [blame] | 3628 | mAddToDictionaryButton = (TextView) mContentView.findViewById( |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3629 | com.android.internal.R.id.addToDictionaryButton); |
| 3630 | mAddToDictionaryButton.setOnClickListener(new View.OnClickListener() { |
| 3631 | public void onClick(View v) { |
Keisuke Kuroyanagi | 6e0860d | 2016-03-15 15:40:43 +0900 | [diff] [blame] | 3632 | final SuggestionSpan misspelledSpan = |
| 3633 | findEquivalentSuggestionSpan(mMisspelledSpanInfo); |
| 3634 | if (misspelledSpan == null) { |
| 3635 | // Span has been removed. |
| 3636 | return; |
| 3637 | } |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3638 | final Editable editable = (Editable) mTextView.getText(); |
Keisuke Kuroyanagi | 6e0860d | 2016-03-15 15:40:43 +0900 | [diff] [blame] | 3639 | final int spanStart = editable.getSpanStart(misspelledSpan); |
| 3640 | final int spanEnd = editable.getSpanEnd(misspelledSpan); |
| 3641 | if (spanStart < 0 || spanEnd <= spanStart) { |
| 3642 | return; |
| 3643 | } |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3644 | final String originalText = TextUtils.substring(editable, spanStart, spanEnd); |
| 3645 | |
| 3646 | final Intent intent = new Intent(Settings.ACTION_USER_DICTIONARY_INSERT); |
| 3647 | intent.putExtra(USER_DICTIONARY_EXTRA_WORD, originalText); |
| 3648 | intent.putExtra(USER_DICTIONARY_EXTRA_LOCALE, |
| 3649 | mTextView.getTextServicesLocale().toString()); |
| 3650 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
Yohei Yukawa | 0115ac1 | 2019-02-05 22:27:20 -0800 | [diff] [blame] | 3651 | mTextView.startActivityAsTextOperationUserIfNecessary(intent); |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3652 | // There is no way to know if the word was indeed added. Re-check. |
| 3653 | // TODO The ExtractEditText should remove the span in the original text instead |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3654 | editable.removeSpan(mMisspelledSpanInfo.mSuggestionSpan); |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3655 | Selection.setSelection(editable, spanEnd); |
| 3656 | updateSpellCheckSpans(spanStart, spanEnd, false); |
| 3657 | hideWithCleanUp(); |
| 3658 | } |
| 3659 | }); |
| 3660 | |
Seigo Nonaka | 77972bb | 2016-04-20 15:45:34 +0900 | [diff] [blame] | 3661 | mDeleteButton = (TextView) mContentView.findViewById( |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3662 | com.android.internal.R.id.deleteButton); |
| 3663 | mDeleteButton.setOnClickListener(new View.OnClickListener() { |
| 3664 | public void onClick(View v) { |
| 3665 | final Editable editable = (Editable) mTextView.getText(); |
| 3666 | |
| 3667 | final int spanUnionStart = editable.getSpanStart(mSuggestionRangeSpan); |
| 3668 | int spanUnionEnd = editable.getSpanEnd(mSuggestionRangeSpan); |
| 3669 | if (spanUnionStart >= 0 && spanUnionEnd > spanUnionStart) { |
| 3670 | // Do not leave two adjacent spaces after deletion, or one at beginning of |
| 3671 | // text |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 3672 | if (spanUnionEnd < editable.length() |
| 3673 | && Character.isSpaceChar(editable.charAt(spanUnionEnd)) |
| 3674 | && (spanUnionStart == 0 |
| 3675 | || Character.isSpaceChar( |
| 3676 | editable.charAt(spanUnionStart - 1)))) { |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3677 | spanUnionEnd = spanUnionEnd + 1; |
| 3678 | } |
| 3679 | mTextView.deleteText_internal(spanUnionStart, spanUnionEnd); |
| 3680 | } |
| 3681 | hideWithCleanUp(); |
| 3682 | } |
| 3683 | }); |
| 3684 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3685 | } |
| 3686 | |
| 3687 | public boolean isShowingUp() { |
| 3688 | return mIsShowingUp; |
| 3689 | } |
| 3690 | |
| 3691 | public void onParentLostFocus() { |
| 3692 | mIsShowingUp = false; |
| 3693 | } |
| 3694 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3695 | private class SuggestionAdapter extends BaseAdapter { |
Seigo Nonaka | e9f54bf | 2016-05-12 18:18:12 +0900 | [diff] [blame] | 3696 | private LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService( |
| 3697 | Context.LAYOUT_INFLATER_SERVICE); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3698 | |
| 3699 | @Override |
| 3700 | public int getCount() { |
| 3701 | return mNumberOfSuggestions; |
| 3702 | } |
| 3703 | |
| 3704 | @Override |
| 3705 | public Object getItem(int position) { |
| 3706 | return mSuggestionInfos[position]; |
| 3707 | } |
| 3708 | |
| 3709 | @Override |
| 3710 | public long getItemId(int position) { |
| 3711 | return position; |
| 3712 | } |
| 3713 | |
| 3714 | @Override |
| 3715 | public View getView(int position, View convertView, ViewGroup parent) { |
| 3716 | TextView textView = (TextView) convertView; |
| 3717 | |
| 3718 | if (textView == null) { |
| 3719 | textView = (TextView) mInflater.inflate(mTextView.mTextEditSuggestionItemLayout, |
| 3720 | parent, false); |
| 3721 | } |
| 3722 | |
| 3723 | final SuggestionInfo suggestionInfo = mSuggestionInfos[position]; |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3724 | textView.setText(suggestionInfo.mText); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3725 | return textView; |
| 3726 | } |
| 3727 | } |
| 3728 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3729 | @Override |
| 3730 | public void show() { |
| 3731 | if (!(mTextView.getText() instanceof Editable)) return; |
Keisuke Kuroyanagi | 4a696ac | 2016-02-23 11:02:07 -0800 | [diff] [blame] | 3732 | if (extractedTextModeWillBeStarted()) { |
| 3733 | return; |
| 3734 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3735 | |
| 3736 | if (updateSuggestions()) { |
| 3737 | mCursorWasVisibleBeforeSuggestions = mCursorVisible; |
| 3738 | mTextView.setCursorVisible(false); |
| 3739 | mIsShowingUp = true; |
| 3740 | super.show(); |
| 3741 | } |
Clara Bayarri | 428e523 | 2017-07-18 16:42:16 +0100 | [diff] [blame] | 3742 | |
| 3743 | mSuggestionListView.setVisibility(mNumberOfSuggestions == 0 ? View.GONE : View.VISIBLE); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3744 | } |
| 3745 | |
| 3746 | @Override |
| 3747 | protected void measureContent() { |
| 3748 | final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics(); |
| 3749 | final int horizontalMeasure = View.MeasureSpec.makeMeasureSpec( |
| 3750 | displayMetrics.widthPixels, View.MeasureSpec.AT_MOST); |
| 3751 | final int verticalMeasure = View.MeasureSpec.makeMeasureSpec( |
| 3752 | displayMetrics.heightPixels, View.MeasureSpec.AT_MOST); |
| 3753 | |
| 3754 | int width = 0; |
| 3755 | View view = null; |
| 3756 | for (int i = 0; i < mNumberOfSuggestions; i++) { |
| 3757 | view = mSuggestionsAdapter.getView(i, view, mContentView); |
| 3758 | view.getLayoutParams().width = LayoutParams.WRAP_CONTENT; |
| 3759 | view.measure(horizontalMeasure, verticalMeasure); |
| 3760 | width = Math.max(width, view.getMeasuredWidth()); |
| 3761 | } |
| 3762 | |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3763 | if (mAddToDictionaryButton.getVisibility() != View.GONE) { |
| 3764 | mAddToDictionaryButton.measure(horizontalMeasure, verticalMeasure); |
| 3765 | width = Math.max(width, mAddToDictionaryButton.getMeasuredWidth()); |
| 3766 | } |
| 3767 | |
| 3768 | mDeleteButton.measure(horizontalMeasure, verticalMeasure); |
| 3769 | width = Math.max(width, mDeleteButton.getMeasuredWidth()); |
| 3770 | |
Seigo Nonaka | 77972bb | 2016-04-20 15:45:34 +0900 | [diff] [blame] | 3771 | width += mContainerView.getPaddingLeft() + mContainerView.getPaddingRight() |
| 3772 | + mContainerMarginWidth; |
Seigo Nonaka | 60490d1 | 2016-01-28 17:25:18 +0900 | [diff] [blame] | 3773 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3774 | // Enforce the width based on actual text widths |
| 3775 | mContentView.measure( |
| 3776 | View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), |
| 3777 | verticalMeasure); |
| 3778 | |
| 3779 | Drawable popupBackground = mPopupWindow.getBackground(); |
| 3780 | if (popupBackground != null) { |
| 3781 | if (mTempRect == null) mTempRect = new Rect(); |
| 3782 | popupBackground.getPadding(mTempRect); |
| 3783 | width += mTempRect.left + mTempRect.right; |
| 3784 | } |
| 3785 | mPopupWindow.setWidth(width); |
| 3786 | } |
| 3787 | |
| 3788 | @Override |
| 3789 | protected int getTextOffset() { |
Keisuke Kuroyanagi | 713be06 | 2016-02-29 16:07:54 -0800 | [diff] [blame] | 3790 | return (mTextView.getSelectionStart() + mTextView.getSelectionStart()) / 2; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3791 | } |
| 3792 | |
| 3793 | @Override |
| 3794 | protected int getVerticalLocalPosition(int line) { |
Siyamed Sinir | a60b59d | 2017-07-26 09:26:41 -0700 | [diff] [blame] | 3795 | final Layout layout = mTextView.getLayout(); |
| 3796 | return layout.getLineBottomWithoutSpacing(line) - mContainerMarginTop; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3797 | } |
| 3798 | |
| 3799 | @Override |
| 3800 | protected int clipVertically(int positionY) { |
| 3801 | final int height = mContentView.getMeasuredHeight(); |
| 3802 | final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics(); |
| 3803 | return Math.min(positionY, displayMetrics.heightPixels - height); |
| 3804 | } |
| 3805 | |
Seigo Nonaka | 7afa67c | 2015-10-07 17:06:04 +0900 | [diff] [blame] | 3806 | private void hideWithCleanUp() { |
| 3807 | for (final SuggestionInfo info : mSuggestionInfos) { |
| 3808 | info.clear(); |
| 3809 | } |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3810 | mMisspelledSpanInfo.clear(); |
Seigo Nonaka | 7afa67c | 2015-10-07 17:06:04 +0900 | [diff] [blame] | 3811 | hide(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3812 | } |
| 3813 | |
| 3814 | private boolean updateSuggestions() { |
| 3815 | Spannable spannable = (Spannable) mTextView.getText(); |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3816 | mNumberOfSuggestions = |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3817 | mSuggestionHelper.getSuggestionInfo(mSuggestionInfos, mMisspelledSpanInfo); |
| 3818 | if (mNumberOfSuggestions == 0 && mMisspelledSpanInfo.mSuggestionSpan == null) { |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3819 | return false; |
| 3820 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3821 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3822 | int spanUnionStart = mTextView.getText().length(); |
| 3823 | int spanUnionEnd = 0; |
| 3824 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3825 | for (int i = 0; i < mNumberOfSuggestions; i++) { |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3826 | final SuggestionSpanInfo spanInfo = mSuggestionInfos[i].mSuggestionSpanInfo; |
| 3827 | spanUnionStart = Math.min(spanUnionStart, spanInfo.mSpanStart); |
| 3828 | spanUnionEnd = Math.max(spanUnionEnd, spanInfo.mSpanEnd); |
| 3829 | } |
| 3830 | if (mMisspelledSpanInfo.mSuggestionSpan != null) { |
| 3831 | spanUnionStart = Math.min(spanUnionStart, mMisspelledSpanInfo.mSpanStart); |
| 3832 | spanUnionEnd = Math.max(spanUnionEnd, mMisspelledSpanInfo.mSpanEnd); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3833 | } |
| 3834 | |
| 3835 | for (int i = 0; i < mNumberOfSuggestions; i++) { |
| 3836 | highlightTextDifferences(mSuggestionInfos[i], spanUnionStart, spanUnionEnd); |
| 3837 | } |
| 3838 | |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3839 | // Make "Add to dictionary" item visible if there is a span with the misspelled flag |
| 3840 | int addToDictionaryButtonVisibility = View.GONE; |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3841 | if (mMisspelledSpanInfo.mSuggestionSpan != null) { |
| 3842 | if (mMisspelledSpanInfo.mSpanStart >= 0 |
| 3843 | && mMisspelledSpanInfo.mSpanEnd > mMisspelledSpanInfo.mSpanStart) { |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3844 | addToDictionaryButtonVisibility = View.VISIBLE; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3845 | } |
| 3846 | } |
Seigo Nonaka | a71a244 | 2015-06-19 15:00:43 +0900 | [diff] [blame] | 3847 | mAddToDictionaryButton.setVisibility(addToDictionaryButtonVisibility); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3848 | |
| 3849 | if (mSuggestionRangeSpan == null) mSuggestionRangeSpan = new SuggestionRangeSpan(); |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3850 | final int underlineColor; |
| 3851 | if (mNumberOfSuggestions != 0) { |
| 3852 | underlineColor = |
| 3853 | mSuggestionInfos[0].mSuggestionSpanInfo.mSuggestionSpan.getUnderlineColor(); |
| 3854 | } else { |
| 3855 | underlineColor = mMisspelledSpanInfo.mSuggestionSpan.getUnderlineColor(); |
| 3856 | } |
| 3857 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3858 | if (underlineColor == 0) { |
| 3859 | // Fallback on the default highlight color when the first span does not provide one |
| 3860 | mSuggestionRangeSpan.setBackgroundColor(mTextView.mHighlightColor); |
| 3861 | } else { |
| 3862 | final float BACKGROUND_TRANSPARENCY = 0.4f; |
| 3863 | final int newAlpha = (int) (Color.alpha(underlineColor) * BACKGROUND_TRANSPARENCY); |
| 3864 | mSuggestionRangeSpan.setBackgroundColor( |
| 3865 | (underlineColor & 0x00FFFFFF) + (newAlpha << 24)); |
| 3866 | } |
| 3867 | spannable.setSpan(mSuggestionRangeSpan, spanUnionStart, spanUnionEnd, |
| 3868 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 3869 | |
| 3870 | mSuggestionsAdapter.notifyDataSetChanged(); |
| 3871 | return true; |
| 3872 | } |
| 3873 | |
| 3874 | private void highlightTextDifferences(SuggestionInfo suggestionInfo, int unionStart, |
| 3875 | int unionEnd) { |
| 3876 | final Spannable text = (Spannable) mTextView.getText(); |
Keisuke Kuroyanagi | f8e0da2 | 2016-03-14 15:10:57 +0900 | [diff] [blame] | 3877 | final int spanStart = suggestionInfo.mSuggestionSpanInfo.mSpanStart; |
| 3878 | final int spanEnd = suggestionInfo.mSuggestionSpanInfo.mSpanEnd; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3879 | |
| 3880 | // Adjust the start/end of the suggestion span |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3881 | suggestionInfo.mSuggestionStart = spanStart - unionStart; |
| 3882 | suggestionInfo.mSuggestionEnd = suggestionInfo.mSuggestionStart |
| 3883 | + suggestionInfo.mText.length(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3884 | |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3885 | suggestionInfo.mText.setSpan(mHighlightSpan, 0, suggestionInfo.mText.length(), |
Seigo Nonaka | bffbd30 | 2015-08-18 18:27:56 -0700 | [diff] [blame] | 3886 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3887 | |
| 3888 | // Add the text before and after the span. |
| 3889 | final String textAsString = text.toString(); |
Keisuke Kuroyanagi | 1cd8aac | 2015-12-21 18:01:40 +0900 | [diff] [blame] | 3890 | suggestionInfo.mText.insert(0, textAsString.substring(unionStart, spanStart)); |
| 3891 | suggestionInfo.mText.append(textAsString.substring(spanEnd, unionEnd)); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | @Override |
| 3895 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3896 | SuggestionInfo suggestionInfo = mSuggestionInfos[position]; |
Keisuke Kuroyanagi | d3194503 | 2016-02-26 16:09:12 -0800 | [diff] [blame] | 3897 | replaceWithSuggestion(suggestionInfo); |
Seigo Nonaka | 7afa67c | 2015-10-07 17:06:04 +0900 | [diff] [blame] | 3898 | hideWithCleanUp(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3899 | } |
| 3900 | } |
| 3901 | |
| 3902 | /** |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3903 | * An ActionMode Callback class that is used to provide actions while in text insertion or |
| 3904 | * selection mode. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3905 | * |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3906 | * The default callback provides a subset of Select All, Cut, Copy, Paste, Share and Replace |
| 3907 | * 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] | 3908 | */ |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3909 | private class TextActionModeCallback extends ActionMode.Callback2 { |
Clara Bayarri | ea4f150 | 2015-03-18 00:25:01 +0000 | [diff] [blame] | 3910 | private final Path mSelectionPath = new Path(); |
| 3911 | private final RectF mSelectionBounds = new RectF(); |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3912 | private final boolean mHasSelection; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 3913 | private final int mHandleHeight; |
Abodunrinwa Toki | 3049d8c | 2017-10-17 20:33:00 +0100 | [diff] [blame] | 3914 | private final Map<MenuItem, OnClickListener> mAssistClickHandlers = new HashMap<>(); |
Clara Bayarri | ea4f150 | 2015-03-18 00:25:01 +0000 | [diff] [blame] | 3915 | |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 3916 | TextActionModeCallback(@TextActionMode int mode) { |
| 3917 | mHasSelection = mode == TextActionMode.SELECTION |
| 3918 | || (mTextIsSelectable && mode == TextActionMode.TEXT_LINK); |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3919 | if (mHasSelection) { |
| 3920 | SelectionModifierCursorController selectionController = getSelectionController(); |
| 3921 | if (selectionController.mStartHandle == null) { |
| 3922 | // As these are for initializing selectionController, hide() must be called. |
Mihai Popa | db68c54 | 2018-11-08 15:23:01 +0000 | [diff] [blame] | 3923 | loadHandleDrawables(false /* overwrite */); |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3924 | selectionController.initHandles(); |
| 3925 | selectionController.hide(); |
| 3926 | } |
| 3927 | mHandleHeight = Math.max( |
| 3928 | mSelectHandleLeft.getMinimumHeight(), |
| 3929 | mSelectHandleRight.getMinimumHeight()); |
| 3930 | } else { |
| 3931 | InsertionPointCursorController insertionController = getInsertionController(); |
| 3932 | if (insertionController != null) { |
| 3933 | insertionController.getHandle(); |
| 3934 | mHandleHeight = mSelectHandleCenter.getMinimumHeight(); |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 3935 | } else { |
| 3936 | mHandleHeight = 0; |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3937 | } |
Clara Bayarri | 7fc946e | 2015-03-31 14:48:33 +0100 | [diff] [blame] | 3938 | } |
Clara Bayarri | ea4f150 | 2015-03-18 00:25:01 +0000 | [diff] [blame] | 3939 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3940 | |
| 3941 | @Override |
| 3942 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
Abodunrinwa Toki | 3049d8c | 2017-10-17 20:33:00 +0100 | [diff] [blame] | 3943 | mAssistClickHandlers.clear(); |
| 3944 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3945 | mode.setTitle(null); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 3946 | mode.setSubtitle(null); |
| 3947 | mode.setTitleOptionalHint(true); |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 3948 | populateMenuWithItems(menu); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 3949 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3950 | Callback customCallback = getCustomCallback(); |
| 3951 | if (customCallback != null) { |
| 3952 | if (!customCallback.onCreateActionMode(mode, menu)) { |
Clara Bayarri | 01243ac | 2015-06-03 00:46:29 +0100 | [diff] [blame] | 3953 | // The custom mode can choose to cancel the action mode, dismiss selection. |
| 3954 | Selection.setSelection((Spannable) mTextView.getText(), |
| 3955 | mTextView.getSelectionEnd()); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 3956 | return false; |
| 3957 | } |
| 3958 | } |
| 3959 | |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 3960 | if (mTextView.canProcessText()) { |
| 3961 | mProcessTextIntentActionsHandler.onInitializeMenu(menu); |
| 3962 | } |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 3963 | |
Abodunrinwa Toki | deb2f49 | 2017-11-06 18:55:17 +0000 | [diff] [blame] | 3964 | if (mHasSelection && !mTextView.hasTransientState()) { |
| 3965 | mTextView.setHasTransientState(true); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 3966 | } |
Abodunrinwa Toki | deb2f49 | 2017-11-06 18:55:17 +0000 | [diff] [blame] | 3967 | return true; |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 3968 | } |
| 3969 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 3970 | private Callback getCustomCallback() { |
| 3971 | return mHasSelection |
| 3972 | ? mCustomSelectionActionModeCallback |
| 3973 | : mCustomInsertionActionModeCallback; |
| 3974 | } |
| 3975 | |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 3976 | private void populateMenuWithItems(Menu menu) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3977 | if (mTextView.canCut()) { |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 3978 | menu.add(Menu.NONE, TextView.ID_CUT, MENU_ITEM_ORDER_CUT, |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 3979 | com.android.internal.R.string.cut) |
| 3980 | .setAlphabeticShortcut('x') |
| 3981 | .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | if (mTextView.canCopy()) { |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 3985 | menu.add(Menu.NONE, TextView.ID_COPY, MENU_ITEM_ORDER_COPY, |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 3986 | com.android.internal.R.string.copy) |
| 3987 | .setAlphabeticShortcut('c') |
| 3988 | .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3989 | } |
| 3990 | |
| 3991 | if (mTextView.canPaste()) { |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 3992 | menu.add(Menu.NONE, TextView.ID_PASTE, MENU_ITEM_ORDER_PASTE, |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 3993 | com.android.internal.R.string.paste) |
| 3994 | .setAlphabeticShortcut('v') |
| 3995 | .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 3996 | } |
| 3997 | |
Andrei Stingaceanu | 7f0c5bd | 2015-04-14 17:12:08 +0100 | [diff] [blame] | 3998 | if (mTextView.canShare()) { |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 3999 | menu.add(Menu.NONE, TextView.ID_SHARE, MENU_ITEM_ORDER_SHARE, |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 4000 | com.android.internal.R.string.share) |
Abodunrinwa Toki | d0d9ceb | 2016-11-21 18:41:02 +0000 | [diff] [blame] | 4001 | .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); |
Andrei Stingaceanu | 7f0c5bd | 2015-04-14 17:12:08 +0100 | [diff] [blame] | 4002 | } |
| 4003 | |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 4004 | if (mTextView.canRequestAutofill()) { |
Felipe Leme | 1c1626e | 2017-06-02 10:53:13 -0700 | [diff] [blame] | 4005 | final String selected = mTextView.getSelectedText(); |
| 4006 | if (selected == null || selected.isEmpty()) { |
| 4007 | menu.add(Menu.NONE, TextView.ID_AUTOFILL, MENU_ITEM_ORDER_AUTOFILL, |
| 4008 | com.android.internal.R.string.autofill) |
Abodunrinwa Toki | 9c881f2 | 2017-10-16 21:05:41 +0100 | [diff] [blame] | 4009 | .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); |
Felipe Leme | 1c1626e | 2017-06-02 10:53:13 -0700 | [diff] [blame] | 4010 | } |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 4011 | } |
| 4012 | |
Abodunrinwa Toki | ea6cb12 | 2017-04-28 22:14:13 +0100 | [diff] [blame] | 4013 | if (mTextView.canPasteAsPlainText()) { |
| 4014 | menu.add( |
| 4015 | Menu.NONE, |
| 4016 | TextView.ID_PASTE_AS_PLAIN_TEXT, |
| 4017 | MENU_ITEM_ORDER_PASTE_AS_PLAIN_TEXT, |
| 4018 | com.android.internal.R.string.paste_as_plain_text) |
| 4019 | .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); |
| 4020 | } |
| 4021 | |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 4022 | updateSelectAllItem(menu); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 4023 | updateReplaceItem(menu); |
Abodunrinwa Toki | 3049d8c | 2017-10-17 20:33:00 +0100 | [diff] [blame] | 4024 | updateAssistMenuItems(menu); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | @Override |
| 4028 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 4029 | updateSelectAllItem(menu); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 4030 | updateReplaceItem(menu); |
Abodunrinwa Toki | 3049d8c | 2017-10-17 20:33:00 +0100 | [diff] [blame] | 4031 | updateAssistMenuItems(menu); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 4032 | |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 4033 | Callback customCallback = getCustomCallback(); |
| 4034 | if (customCallback != null) { |
| 4035 | return customCallback.onPrepareActionMode(mode, menu); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4036 | } |
| 4037 | return true; |
| 4038 | } |
| 4039 | |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 4040 | private void updateSelectAllItem(Menu menu) { |
| 4041 | boolean canSelectAll = mTextView.canSelectAllText(); |
| 4042 | boolean selectAllItemExists = menu.findItem(TextView.ID_SELECT_ALL) != null; |
| 4043 | if (canSelectAll && !selectAllItemExists) { |
| 4044 | menu.add(Menu.NONE, TextView.ID_SELECT_ALL, MENU_ITEM_ORDER_SELECT_ALL, |
| 4045 | com.android.internal.R.string.selectAll) |
| 4046 | .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); |
| 4047 | } else if (!canSelectAll && selectAllItemExists) { |
| 4048 | menu.removeItem(TextView.ID_SELECT_ALL); |
| 4049 | } |
| 4050 | } |
| 4051 | |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 4052 | private void updateReplaceItem(Menu menu) { |
Keisuke Kuroyanagi | d3194503 | 2016-02-26 16:09:12 -0800 | [diff] [blame] | 4053 | boolean canReplace = mTextView.isSuggestionsEnabled() && shouldOfferToShowSuggestions(); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 4054 | boolean replaceItemExists = menu.findItem(TextView.ID_REPLACE) != null; |
| 4055 | if (canReplace && !replaceItemExists) { |
Clara Bayarri | 3b69fd8 | 2015-06-03 21:52:02 +0100 | [diff] [blame] | 4056 | menu.add(Menu.NONE, TextView.ID_REPLACE, MENU_ITEM_ORDER_REPLACE, |
| 4057 | com.android.internal.R.string.replace) |
| 4058 | .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 4059 | } else if (!canReplace && replaceItemExists) { |
| 4060 | menu.removeItem(TextView.ID_REPLACE); |
| 4061 | } |
| 4062 | } |
| 4063 | |
Abodunrinwa Toki | 3049d8c | 2017-10-17 20:33:00 +0100 | [diff] [blame] | 4064 | private void updateAssistMenuItems(Menu menu) { |
| 4065 | clearAssistMenuItems(menu); |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 4066 | if (!shouldEnableAssistMenuItems()) { |
Abodunrinwa Toki | 3049d8c | 2017-10-17 20:33:00 +0100 | [diff] [blame] | 4067 | return; |
| 4068 | } |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 4069 | final TextClassification textClassification = |
| 4070 | getSelectionActionModeHelper().getTextClassification(); |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 4071 | if (textClassification == null) { |
| 4072 | return; |
| 4073 | } |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 4074 | if (!textClassification.getActions().isEmpty()) { |
| 4075 | // Primary assist action (Always shown). |
| 4076 | final MenuItem item = addAssistMenuItem(menu, |
| 4077 | textClassification.getActions().get(0), TextView.ID_ASSIST, |
| 4078 | MENU_ITEM_ORDER_ASSIST, MenuItem.SHOW_AS_ACTION_ALWAYS); |
| 4079 | item.setIntent(textClassification.getIntent()); |
| 4080 | } else if (hasLegacyAssistItem(textClassification)) { |
| 4081 | // Legacy primary assist action (Always shown). |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 4082 | final MenuItem item = menu.add( |
| 4083 | TextView.ID_ASSIST, TextView.ID_ASSIST, MENU_ITEM_ORDER_ASSIST, |
| 4084 | textClassification.getLabel()) |
| 4085 | .setIcon(textClassification.getIcon()) |
| 4086 | .setIntent(textClassification.getIntent()); |
| 4087 | item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 4088 | mAssistClickHandlers.put(item, TextClassification.createIntentOnClickListener( |
| 4089 | TextClassification.createPendingIntent(mTextView.getContext(), |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 4090 | textClassification.getIntent(), |
| 4091 | createAssistMenuItemPendingIntentRequestCode()))); |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 4092 | } |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 4093 | final int count = textClassification.getActions().size(); |
| 4094 | for (int i = 1; i < count; i++) { |
| 4095 | // Secondary assist action (Never shown). |
| 4096 | addAssistMenuItem(menu, textClassification.getActions().get(i), Menu.NONE, |
| 4097 | MENU_ITEM_ORDER_SECONDARY_ASSIST_ACTIONS_START + i - 1, |
| 4098 | MenuItem.SHOW_AS_ACTION_NEVER); |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 4099 | } |
Abodunrinwa Toki | d0d9ceb | 2016-11-21 18:41:02 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 4102 | private MenuItem addAssistMenuItem(Menu menu, RemoteAction action, int itemId, int order, |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 4103 | int showAsAction) { |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 4104 | final MenuItem item = menu.add(TextView.ID_ASSIST, itemId, order, action.getTitle()) |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 4105 | .setContentDescription(action.getContentDescription()); |
| 4106 | if (action.shouldShowIcon()) { |
| 4107 | item.setIcon(action.getIcon().loadDrawable(mTextView.getContext())); |
| 4108 | } |
| 4109 | item.setShowAsAction(showAsAction); |
| 4110 | mAssistClickHandlers.put(item, |
| 4111 | TextClassification.createIntentOnClickListener(action.getActionIntent())); |
| 4112 | return item; |
| 4113 | } |
| 4114 | |
Abodunrinwa Toki | 3049d8c | 2017-10-17 20:33:00 +0100 | [diff] [blame] | 4115 | private void clearAssistMenuItems(Menu menu) { |
| 4116 | int i = 0; |
| 4117 | while (i < menu.size()) { |
| 4118 | final MenuItem menuItem = menu.getItem(i); |
| 4119 | if (menuItem.getGroupId() == TextView.ID_ASSIST) { |
| 4120 | menu.removeItem(menuItem.getItemId()); |
| 4121 | continue; |
| 4122 | } |
| 4123 | i++; |
| 4124 | } |
| 4125 | } |
| 4126 | |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 4127 | private boolean hasLegacyAssistItem(TextClassification classification) { |
| 4128 | // Check whether we have the UI data and and action. |
| 4129 | return (classification.getIcon() != null || !TextUtils.isEmpty( |
| 4130 | classification.getLabel())) && (classification.getIntent() != null |
| 4131 | || classification.getOnClickListener() != null); |
Abodunrinwa Toki | 3049d8c | 2017-10-17 20:33:00 +0100 | [diff] [blame] | 4132 | } |
| 4133 | |
| 4134 | private boolean onAssistMenuItemClicked(MenuItem assistMenuItem) { |
| 4135 | Preconditions.checkArgument(assistMenuItem.getGroupId() == TextView.ID_ASSIST); |
| 4136 | |
| 4137 | final TextClassification textClassification = |
| 4138 | getSelectionActionModeHelper().getTextClassification(); |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 4139 | if (!shouldEnableAssistMenuItems() || textClassification == null) { |
Abodunrinwa Toki | 3049d8c | 2017-10-17 20:33:00 +0100 | [diff] [blame] | 4140 | // No textClassification result to handle the click. Eat the click. |
| 4141 | return true; |
| 4142 | } |
| 4143 | |
| 4144 | OnClickListener onClickListener = mAssistClickHandlers.get(assistMenuItem); |
| 4145 | if (onClickListener == null) { |
| 4146 | final Intent intent = assistMenuItem.getIntent(); |
| 4147 | if (intent != null) { |
Abodunrinwa Toki | 2f19b92 | 2018-02-12 19:59:28 +0000 | [diff] [blame] | 4148 | onClickListener = TextClassification.createIntentOnClickListener( |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 4149 | TextClassification.createPendingIntent( |
| 4150 | mTextView.getContext(), intent, |
| 4151 | createAssistMenuItemPendingIntentRequestCode())); |
Abodunrinwa Toki | 3049d8c | 2017-10-17 20:33:00 +0100 | [diff] [blame] | 4152 | } |
| 4153 | } |
| 4154 | if (onClickListener != null) { |
| 4155 | onClickListener.onClick(mTextView); |
| 4156 | stopTextActionMode(); |
Abodunrinwa Toki | 3049d8c | 2017-10-17 20:33:00 +0100 | [diff] [blame] | 4157 | } |
| 4158 | // We tried our best. |
| 4159 | return true; |
Abodunrinwa Toki | 9796a1b | 2017-06-28 02:49:07 +0100 | [diff] [blame] | 4160 | } |
| 4161 | |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 4162 | private int createAssistMenuItemPendingIntentRequestCode() { |
| 4163 | return mTextView.hasSelection() |
| 4164 | ? mTextView.getText().subSequence( |
| 4165 | mTextView.getSelectionStart(), mTextView.getSelectionEnd()) |
| 4166 | .hashCode() |
| 4167 | : 0; |
| 4168 | } |
| 4169 | |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 4170 | private boolean shouldEnableAssistMenuItems() { |
| 4171 | return mTextView.isDeviceProvisioned() |
| 4172 | && TextClassificationManager.getSettings(mTextView.getContext()) |
| 4173 | .isSmartTextShareEnabled(); |
| 4174 | } |
| 4175 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4176 | @Override |
| 4177 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 4178 | getSelectionActionModeHelper() |
| 4179 | .onSelectionAction(item.getItemId(), item.getTitle().toString()); |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 4180 | |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 4181 | if (mProcessTextIntentActionsHandler.performMenuItemAction(item)) { |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 4182 | return true; |
| 4183 | } |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 4184 | Callback customCallback = getCustomCallback(); |
| 4185 | if (customCallback != null && customCallback.onActionItemClicked(mode, item)) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4186 | return true; |
| 4187 | } |
Abodunrinwa Toki | 3049d8c | 2017-10-17 20:33:00 +0100 | [diff] [blame] | 4188 | if (item.getGroupId() == TextView.ID_ASSIST && onAssistMenuItemClicked(item)) { |
Abodunrinwa Toki | fafdb73 | 2017-02-02 11:07:05 +0000 | [diff] [blame] | 4189 | return true; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 4190 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4191 | return mTextView.onTextContextMenuItem(item.getItemId()); |
| 4192 | } |
| 4193 | |
| 4194 | @Override |
| 4195 | public void onDestroyActionMode(ActionMode mode) { |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 4196 | // Clear mTextActionMode not to recursively destroy action mode by clearing selection. |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 4197 | getSelectionActionModeHelper().onDestroyActionMode(); |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 4198 | mTextActionMode = null; |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 4199 | Callback customCallback = getCustomCallback(); |
| 4200 | if (customCallback != null) { |
| 4201 | customCallback.onDestroyActionMode(mode); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4202 | } |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 4203 | |
Keisuke Kuroyanagi | af4caa6 | 2016-02-29 12:53:58 -0800 | [diff] [blame] | 4204 | if (!mPreserveSelection) { |
| 4205 | /* |
| 4206 | * Leave current selection when we tentatively destroy action mode for the |
| 4207 | * selection. If we're detaching from a window, we'll bring back the selection |
| 4208 | * mode when (if) we get reattached. |
| 4209 | */ |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 4210 | Selection.setSelection((Spannable) mTextView.getText(), |
| 4211 | mTextView.getSelectionEnd()); |
Adam Powell | 057a585 | 2012-05-11 10:28:38 -0700 | [diff] [blame] | 4212 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4213 | |
| 4214 | if (mSelectionModifierCursorController != null) { |
| 4215 | mSelectionModifierCursorController.hide(); |
| 4216 | } |
Abodunrinwa Toki | 3049d8c | 2017-10-17 20:33:00 +0100 | [diff] [blame] | 4217 | |
| 4218 | mAssistClickHandlers.clear(); |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 4219 | mRequestingLinkActionMode = false; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4220 | } |
Clara Bayarri | ea4f150 | 2015-03-18 00:25:01 +0000 | [diff] [blame] | 4221 | |
| 4222 | @Override |
| 4223 | public void onGetContentRect(ActionMode mode, View view, Rect outRect) { |
| 4224 | if (!view.equals(mTextView) || mTextView.getLayout() == null) { |
| 4225 | super.onGetContentRect(mode, view, outRect); |
| 4226 | return; |
| 4227 | } |
| 4228 | if (mTextView.getSelectionStart() != mTextView.getSelectionEnd()) { |
| 4229 | // We have a selection. |
| 4230 | mSelectionPath.reset(); |
| 4231 | mTextView.getLayout().getSelectionPath( |
| 4232 | mTextView.getSelectionStart(), mTextView.getSelectionEnd(), mSelectionPath); |
| 4233 | mSelectionPath.computeBounds(mSelectionBounds, true); |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 4234 | mSelectionBounds.bottom += mHandleHeight; |
Clara Bayarri | ea4f150 | 2015-03-18 00:25:01 +0000 | [diff] [blame] | 4235 | } else { |
Roozbeh Pournader | 9c13307 | 2017-07-26 22:36:27 -0700 | [diff] [blame] | 4236 | // We have a cursor. |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 4237 | Layout layout = mTextView.getLayout(); |
Mady Mellor | ff66ca5 | 2015-07-08 12:31:45 -0700 | [diff] [blame] | 4238 | int line = layout.getLineForOffset(mTextView.getSelectionStart()); |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 4239 | float primaryHorizontal = clampHorizontalPosition(null, |
| 4240 | layout.getPrimaryHorizontal(mTextView.getSelectionStart())); |
Clara Bayarri | ea4f150 | 2015-03-18 00:25:01 +0000 | [diff] [blame] | 4241 | mSelectionBounds.set( |
| 4242 | primaryHorizontal, |
Mady Mellor | ff66ca5 | 2015-07-08 12:31:45 -0700 | [diff] [blame] | 4243 | layout.getLineTop(line), |
Clara Bayarri | f95ed10 | 2015-08-12 19:46:47 +0100 | [diff] [blame] | 4244 | primaryHorizontal, |
Siyamed Sinir | fdbc5ee | 2018-02-09 11:24:16 -0800 | [diff] [blame] | 4245 | layout.getLineBottom(line) + mHandleHeight); |
Clara Bayarri | ea4f150 | 2015-03-18 00:25:01 +0000 | [diff] [blame] | 4246 | } |
| 4247 | // Take TextView's padding and scroll into account. |
| 4248 | int textHorizontalOffset = mTextView.viewportToContentHorizontalOffset(); |
| 4249 | int textVerticalOffset = mTextView.viewportToContentVerticalOffset(); |
| 4250 | outRect.set( |
| 4251 | (int) Math.floor(mSelectionBounds.left + textHorizontalOffset), |
| 4252 | (int) Math.floor(mSelectionBounds.top + textVerticalOffset), |
| 4253 | (int) Math.ceil(mSelectionBounds.right + textHorizontalOffset), |
| 4254 | (int) Math.ceil(mSelectionBounds.bottom + textVerticalOffset)); |
| 4255 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4256 | } |
| 4257 | |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 4258 | /** |
| 4259 | * A listener to call {@link InputMethodManager#updateCursorAnchorInfo(View, CursorAnchorInfo)} |
| 4260 | * while the input method is requesting the cursor/anchor position. Does nothing as long as |
| 4261 | * {@link InputMethodManager#isWatchingCursor(View)} returns false. |
| 4262 | */ |
| 4263 | private final class CursorAnchorInfoNotifier implements TextViewPositionListener { |
Yohei Yukawa | c46b5f0 | 2014-06-10 12:26:34 +0900 | [diff] [blame] | 4264 | final CursorAnchorInfo.Builder mSelectionInfoBuilder = new CursorAnchorInfo.Builder(); |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 4265 | final int[] mTmpIntOffset = new int[2]; |
| 4266 | final Matrix mViewToScreenMatrix = new Matrix(); |
| 4267 | |
| 4268 | @Override |
| 4269 | public void updatePosition(int parentPositionX, int parentPositionY, |
| 4270 | boolean parentPositionChanged, boolean parentScrolled) { |
| 4271 | final InputMethodState ims = mInputMethodState; |
| 4272 | if (ims == null || ims.mBatchEditNesting > 0) { |
| 4273 | return; |
| 4274 | } |
Yohei Yukawa | 484d4af | 2018-09-17 16:47:08 -0700 | [diff] [blame] | 4275 | final InputMethodManager imm = getInputMethodManager(); |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 4276 | if (null == imm) { |
| 4277 | return; |
| 4278 | } |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 4279 | if (!imm.isActive(mTextView)) { |
| 4280 | return; |
| 4281 | } |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 4282 | // Skip if the IME has not requested the cursor/anchor position. |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 4283 | if (!imm.isCursorAnchorInfoEnabled()) { |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 4284 | return; |
| 4285 | } |
| 4286 | Layout layout = mTextView.getLayout(); |
| 4287 | if (layout == null) { |
| 4288 | return; |
| 4289 | } |
| 4290 | |
Yohei Yukawa | c46b5f0 | 2014-06-10 12:26:34 +0900 | [diff] [blame] | 4291 | final CursorAnchorInfo.Builder builder = mSelectionInfoBuilder; |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 4292 | builder.reset(); |
| 4293 | |
| 4294 | final int selectionStart = mTextView.getSelectionStart(); |
Yohei Yukawa | 81f4cb3 | 2014-05-13 22:20:35 +0900 | [diff] [blame] | 4295 | builder.setSelectionRange(selectionStart, mTextView.getSelectionEnd()); |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 4296 | |
| 4297 | // Construct transformation matrix from view local coordinates to screen coordinates. |
| 4298 | mViewToScreenMatrix.set(mTextView.getMatrix()); |
| 4299 | mTextView.getLocationOnScreen(mTmpIntOffset); |
| 4300 | mViewToScreenMatrix.postTranslate(mTmpIntOffset[0], mTmpIntOffset[1]); |
| 4301 | builder.setMatrix(mViewToScreenMatrix); |
| 4302 | |
| 4303 | final float viewportToContentHorizontalOffset = |
| 4304 | mTextView.viewportToContentHorizontalOffset(); |
| 4305 | final float viewportToContentVerticalOffset = |
| 4306 | mTextView.viewportToContentVerticalOffset(); |
| 4307 | |
Yohei Yukawa | 81f4cb3 | 2014-05-13 22:20:35 +0900 | [diff] [blame] | 4308 | final CharSequence text = mTextView.getText(); |
| 4309 | if (text instanceof Spannable) { |
| 4310 | final Spannable sp = (Spannable) text; |
| 4311 | int composingTextStart = EditableInputConnection.getComposingSpanStart(sp); |
| 4312 | int composingTextEnd = EditableInputConnection.getComposingSpanEnd(sp); |
| 4313 | if (composingTextEnd < composingTextStart) { |
| 4314 | final int temp = composingTextEnd; |
| 4315 | composingTextEnd = composingTextStart; |
| 4316 | composingTextStart = temp; |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 4317 | } |
Yohei Yukawa | 81f4cb3 | 2014-05-13 22:20:35 +0900 | [diff] [blame] | 4318 | final boolean hasComposingText = |
| 4319 | (0 <= composingTextStart) && (composingTextStart < composingTextEnd); |
| 4320 | if (hasComposingText) { |
| 4321 | final CharSequence composingText = text.subSequence(composingTextStart, |
| 4322 | composingTextEnd); |
| 4323 | builder.setComposingText(composingTextStart, composingText); |
Phil Weaver | c2e2893 | 2016-12-08 12:29:25 -0800 | [diff] [blame] | 4324 | mTextView.populateCharacterBounds(builder, composingTextStart, |
| 4325 | composingTextEnd, viewportToContentHorizontalOffset, |
| 4326 | viewportToContentVerticalOffset); |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 4327 | } |
| 4328 | } |
| 4329 | |
| 4330 | // Treat selectionStart as the insertion point. |
| 4331 | if (0 <= selectionStart) { |
| 4332 | final int offset = selectionStart; |
| 4333 | final int line = layout.getLineForOffset(offset); |
| 4334 | final float insertionMarkerX = layout.getPrimaryHorizontal(offset) |
| 4335 | + viewportToContentHorizontalOffset; |
| 4336 | final float insertionMarkerTop = layout.getLineTop(line) |
| 4337 | + viewportToContentVerticalOffset; |
| 4338 | final float insertionMarkerBaseline = layout.getLineBaseline(line) |
| 4339 | + viewportToContentVerticalOffset; |
Siyamed Sinir | a60b59d | 2017-07-26 09:26:41 -0700 | [diff] [blame] | 4340 | final float insertionMarkerBottom = layout.getLineBottomWithoutSpacing(line) |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 4341 | + viewportToContentVerticalOffset; |
Phil Weaver | c2e2893 | 2016-12-08 12:29:25 -0800 | [diff] [blame] | 4342 | final boolean isTopVisible = mTextView |
| 4343 | .isPositionVisible(insertionMarkerX, insertionMarkerTop); |
| 4344 | final boolean isBottomVisible = mTextView |
| 4345 | .isPositionVisible(insertionMarkerX, insertionMarkerBottom); |
Yohei Yukawa | cc24e2b | 2014-08-29 20:21:10 -0700 | [diff] [blame] | 4346 | int insertionMarkerFlags = 0; |
| 4347 | if (isTopVisible || isBottomVisible) { |
| 4348 | insertionMarkerFlags |= CursorAnchorInfo.FLAG_HAS_VISIBLE_REGION; |
| 4349 | } |
| 4350 | if (!isTopVisible || !isBottomVisible) { |
| 4351 | insertionMarkerFlags |= CursorAnchorInfo.FLAG_HAS_INVISIBLE_REGION; |
| 4352 | } |
Yohei Yukawa | 5f183f0 | 2014-09-02 14:18:40 -0700 | [diff] [blame] | 4353 | if (layout.isRtlCharAt(offset)) { |
| 4354 | insertionMarkerFlags |= CursorAnchorInfo.FLAG_IS_RTL; |
| 4355 | } |
Yohei Yukawa | 0b01e7f | 2014-07-08 15:29:51 +0900 | [diff] [blame] | 4356 | builder.setInsertionMarkerLocation(insertionMarkerX, insertionMarkerTop, |
Yohei Yukawa | cc24e2b | 2014-08-29 20:21:10 -0700 | [diff] [blame] | 4357 | insertionMarkerBaseline, insertionMarkerBottom, insertionMarkerFlags); |
Yohei Yukawa | 83b68ba | 2014-05-12 15:46:25 +0900 | [diff] [blame] | 4358 | } |
| 4359 | |
| 4360 | imm.updateCursorAnchorInfo(mTextView, builder.build()); |
| 4361 | } |
| 4362 | } |
| 4363 | |
Mihai Popa | 3872238 | 2018-03-07 19:56:21 +0000 | [diff] [blame] | 4364 | private static class MagnifierMotionAnimator { |
| 4365 | private static final long DURATION = 100 /* miliseconds */; |
| 4366 | |
| 4367 | // The magnifier being animated. |
| 4368 | private final Magnifier mMagnifier; |
| 4369 | // A value animator used to animate the magnifier. |
| 4370 | private final ValueAnimator mAnimator; |
| 4371 | |
| 4372 | // Whether the magnifier is currently visible. |
| 4373 | private boolean mMagnifierIsShowing; |
| 4374 | // The coordinates of the magnifier when the currently running animation started. |
| 4375 | private float mAnimationStartX; |
| 4376 | private float mAnimationStartY; |
| 4377 | // The coordinates of the magnifier in the latest animation frame. |
| 4378 | private float mAnimationCurrentX; |
| 4379 | private float mAnimationCurrentY; |
| 4380 | // The latest coordinates the motion animator was asked to #show() the magnifier at. |
| 4381 | private float mLastX; |
| 4382 | private float mLastY; |
| 4383 | |
| 4384 | private MagnifierMotionAnimator(final Magnifier magnifier) { |
| 4385 | mMagnifier = magnifier; |
| 4386 | // Prepare the animator used to run the motion animation. |
| 4387 | mAnimator = ValueAnimator.ofFloat(0, 1); |
| 4388 | mAnimator.setDuration(DURATION); |
| 4389 | mAnimator.setInterpolator(new LinearInterpolator()); |
| 4390 | mAnimator.addUpdateListener((animation) -> { |
| 4391 | // Interpolate to find the current position of the magnifier. |
| 4392 | mAnimationCurrentX = mAnimationStartX |
| 4393 | + (mLastX - mAnimationStartX) * animation.getAnimatedFraction(); |
| 4394 | mAnimationCurrentY = mAnimationStartY |
| 4395 | + (mLastY - mAnimationStartY) * animation.getAnimatedFraction(); |
| 4396 | mMagnifier.show(mAnimationCurrentX, mAnimationCurrentY); |
| 4397 | }); |
| 4398 | } |
| 4399 | |
| 4400 | /** |
| 4401 | * Shows the magnifier at a new position. |
| 4402 | * If the y coordinate is different from the previous y coordinate |
| 4403 | * (probably corresponding to a line jump in the text), a short |
| 4404 | * animation is added to the jump. |
| 4405 | */ |
| 4406 | private void show(final float x, final float y) { |
| 4407 | final boolean startNewAnimation = mMagnifierIsShowing && y != mLastY; |
| 4408 | |
| 4409 | if (startNewAnimation) { |
| 4410 | if (mAnimator.isRunning()) { |
| 4411 | mAnimator.cancel(); |
| 4412 | mAnimationStartX = mAnimationCurrentX; |
| 4413 | mAnimationStartY = mAnimationCurrentY; |
| 4414 | } else { |
| 4415 | mAnimationStartX = mLastX; |
| 4416 | mAnimationStartY = mLastY; |
| 4417 | } |
| 4418 | mAnimator.start(); |
| 4419 | } else { |
| 4420 | if (!mAnimator.isRunning()) { |
| 4421 | mMagnifier.show(x, y); |
| 4422 | } |
| 4423 | } |
| 4424 | mLastX = x; |
| 4425 | mLastY = y; |
| 4426 | mMagnifierIsShowing = true; |
| 4427 | } |
| 4428 | |
| 4429 | /** |
| 4430 | * Updates the content of the magnifier. |
| 4431 | */ |
| 4432 | private void update() { |
| 4433 | mMagnifier.update(); |
| 4434 | } |
| 4435 | |
| 4436 | /** |
| 4437 | * Dismisses the magnifier, or does nothing if it is already dismissed. |
| 4438 | */ |
| 4439 | private void dismiss() { |
| 4440 | mMagnifier.dismiss(); |
| 4441 | mAnimator.cancel(); |
| 4442 | mMagnifierIsShowing = false; |
| 4443 | } |
| 4444 | } |
| 4445 | |
Keisuke Kuroyanagi | da79ee6 | 2015-11-25 16:15:15 +0900 | [diff] [blame] | 4446 | @VisibleForTesting |
| 4447 | public abstract class HandleView extends View implements TextViewPositionListener { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4448 | protected Drawable mDrawable; |
| 4449 | protected Drawable mDrawableLtr; |
| 4450 | protected Drawable mDrawableRtl; |
| 4451 | private final PopupWindow mContainer; |
| 4452 | // Position with respect to the parent TextView |
| 4453 | private int mPositionX, mPositionY; |
| 4454 | private boolean mIsDragging; |
| 4455 | // Offset from touch position to mPosition |
| 4456 | private float mTouchToWindowOffsetX, mTouchToWindowOffsetY; |
| 4457 | protected int mHotspotX; |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4458 | protected int mHorizontalGravity; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4459 | // Offsets the hotspot point up, so that cursor is not hidden by the finger when moving up |
| 4460 | private float mTouchOffsetY; |
| 4461 | // Where the touch position should be on the handle to ensure a maximum cursor visibility |
| 4462 | private float mIdealVerticalOffset; |
| 4463 | // Parent's (TextView) previous position in window |
| 4464 | private int mLastParentX, mLastParentY; |
Keisuke Kuroyanagi | e2a3b1e | 2016-04-28 12:55:18 +0900 | [diff] [blame] | 4465 | // Parent's (TextView) previous position on screen |
| 4466 | private int mLastParentXOnScreen, mLastParentYOnScreen; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4467 | // Previous text character offset |
Mady Mellor | c2225b9 | 2015-04-01 15:59:20 -0700 | [diff] [blame] | 4468 | protected int mPreviousOffset = -1; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4469 | // Previous text character offset |
| 4470 | private boolean mPositionHasChanged = true; |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4471 | // Minimum touch target size for handles |
| 4472 | private int mMinSize; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 4473 | // Indicates the line of text that the handle is on. |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 4474 | protected int mPrevLine = UNSET_LINE; |
| 4475 | // Indicates the line of text that the user was touching. This can differ from mPrevLine |
| 4476 | // when selecting text when the handles jump to the end / start of words which may be on |
| 4477 | // a different line. |
| 4478 | protected int mPreviousLineTouched = UNSET_LINE; |
Mihai Popa | 6d26d15 | 2019-01-30 15:36:47 +0000 | [diff] [blame] | 4479 | // The raw x coordinate of the motion down event which started the current dragging session. |
| 4480 | // Only used and stored when magnifier is used. |
| 4481 | private float mCurrentDragInitialTouchRawX = UNSET_X_VALUE; |
| 4482 | // The scale transform applied by containers to the TextView. Only used and computed |
| 4483 | // when magnifier is used. |
| 4484 | private float mTextViewScaleX; |
| 4485 | private float mTextViewScaleY; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4486 | |
Keisuke Kuroyanagi | da79ee6 | 2015-11-25 16:15:15 +0900 | [diff] [blame] | 4487 | private HandleView(Drawable drawableLtr, Drawable drawableRtl, final int id) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4488 | super(mTextView.getContext()); |
Keisuke Kuroyanagi | da79ee6 | 2015-11-25 16:15:15 +0900 | [diff] [blame] | 4489 | setId(id); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4490 | mContainer = new PopupWindow(mTextView.getContext(), null, |
| 4491 | com.android.internal.R.attr.textSelectHandleWindowStyle); |
| 4492 | mContainer.setSplitTouchEnabled(true); |
| 4493 | mContainer.setClippingEnabled(false); |
| 4494 | mContainer.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL); |
Keisuke Kuroyanagi | 7340be7 | 2015-02-27 17:57:49 +0900 | [diff] [blame] | 4495 | mContainer.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); |
| 4496 | mContainer.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4497 | mContainer.setContentView(this); |
| 4498 | |
Mihai Popa | 6315a32 | 2018-10-17 17:39:57 +0100 | [diff] [blame] | 4499 | setDrawables(drawableLtr, drawableRtl); |
| 4500 | |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4501 | mMinSize = mTextView.getContext().getResources().getDimensionPixelSize( |
| 4502 | com.android.internal.R.dimen.text_handle_min_size); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4503 | |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4504 | final int handleHeight = getPreferredHeight(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4505 | mTouchOffsetY = -0.3f * handleHeight; |
| 4506 | mIdealVerticalOffset = 0.7f * handleHeight; |
| 4507 | } |
| 4508 | |
Mady Mellor | 7a93644 | 2015-05-20 10:05:52 -0700 | [diff] [blame] | 4509 | public float getIdealVerticalOffset() { |
| 4510 | return mIdealVerticalOffset; |
| 4511 | } |
| 4512 | |
Mihai Popa | 6315a32 | 2018-10-17 17:39:57 +0100 | [diff] [blame] | 4513 | void setDrawables(final Drawable drawableLtr, final Drawable drawableRtl) { |
| 4514 | mDrawableLtr = drawableLtr; |
| 4515 | mDrawableRtl = drawableRtl; |
| 4516 | updateDrawable(true /* updateDrawableWhenDragging */); |
| 4517 | } |
| 4518 | |
| 4519 | protected void updateDrawable(final boolean updateDrawableWhenDragging) { |
| 4520 | if (!updateDrawableWhenDragging && mIsDragging) { |
Keisuke Kuroyanagi | dbe2c29 | 2015-05-27 19:49:34 +0900 | [diff] [blame] | 4521 | return; |
| 4522 | } |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4523 | final Layout layout = mTextView.getLayout(); |
| 4524 | if (layout == null) { |
| 4525 | return; |
| 4526 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4527 | final int offset = getCurrentCursorOffset(); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4528 | final boolean isRtlCharAtOffset = isAtRtlRun(layout, offset); |
Keisuke Kuroyanagi | 33f81ac | 2015-05-14 20:10:57 +0900 | [diff] [blame] | 4529 | final Drawable oldDrawable = mDrawable; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4530 | mDrawable = isRtlCharAtOffset ? mDrawableRtl : mDrawableLtr; |
| 4531 | mHotspotX = getHotspotX(mDrawable, isRtlCharAtOffset); |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4532 | mHorizontalGravity = getHorizontalGravity(isRtlCharAtOffset); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4533 | if (oldDrawable != mDrawable && isShowing()) { |
Keisuke Kuroyanagi | dbe2c29 | 2015-05-27 19:49:34 +0900 | [diff] [blame] | 4534 | // Update popup window position. |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 4535 | mPositionX = getCursorHorizontalPosition(layout, offset) - mHotspotX |
| 4536 | - getHorizontalOffset() + getCursorOffset(); |
Keisuke Kuroyanagi | dbe2c29 | 2015-05-27 19:49:34 +0900 | [diff] [blame] | 4537 | mPositionX += mTextView.viewportToContentHorizontalOffset(); |
| 4538 | mPositionHasChanged = true; |
| 4539 | updatePosition(mLastParentX, mLastParentY, false, false); |
Keisuke Kuroyanagi | 33f81ac | 2015-05-14 20:10:57 +0900 | [diff] [blame] | 4540 | postInvalidate(); |
| 4541 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4542 | } |
| 4543 | |
| 4544 | protected abstract int getHotspotX(Drawable drawable, boolean isRtlRun); |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4545 | protected abstract int getHorizontalGravity(boolean isRtlRun); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4546 | |
| 4547 | // Touch-up filter: number of previous positions remembered |
| 4548 | private static final int HISTORY_SIZE = 5; |
| 4549 | private static final int TOUCH_UP_FILTER_DELAY_AFTER = 150; |
| 4550 | private static final int TOUCH_UP_FILTER_DELAY_BEFORE = 350; |
| 4551 | private final long[] mPreviousOffsetsTimes = new long[HISTORY_SIZE]; |
| 4552 | private final int[] mPreviousOffsets = new int[HISTORY_SIZE]; |
| 4553 | private int mPreviousOffsetIndex = 0; |
| 4554 | private int mNumberPreviousOffsets = 0; |
| 4555 | |
| 4556 | private void startTouchUpFilter(int offset) { |
| 4557 | mNumberPreviousOffsets = 0; |
| 4558 | addPositionToTouchUpFilter(offset); |
| 4559 | } |
| 4560 | |
| 4561 | private void addPositionToTouchUpFilter(int offset) { |
| 4562 | mPreviousOffsetIndex = (mPreviousOffsetIndex + 1) % HISTORY_SIZE; |
| 4563 | mPreviousOffsets[mPreviousOffsetIndex] = offset; |
| 4564 | mPreviousOffsetsTimes[mPreviousOffsetIndex] = SystemClock.uptimeMillis(); |
| 4565 | mNumberPreviousOffsets++; |
| 4566 | } |
| 4567 | |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 4568 | private void filterOnTouchUp(boolean fromTouchScreen) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4569 | final long now = SystemClock.uptimeMillis(); |
| 4570 | int i = 0; |
| 4571 | int index = mPreviousOffsetIndex; |
| 4572 | final int iMax = Math.min(mNumberPreviousOffsets, HISTORY_SIZE); |
| 4573 | while (i < iMax && (now - mPreviousOffsetsTimes[index]) < TOUCH_UP_FILTER_DELAY_AFTER) { |
| 4574 | i++; |
| 4575 | index = (mPreviousOffsetIndex - i + HISTORY_SIZE) % HISTORY_SIZE; |
| 4576 | } |
| 4577 | |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 4578 | if (i > 0 && i < iMax |
| 4579 | && (now - mPreviousOffsetsTimes[index]) > TOUCH_UP_FILTER_DELAY_BEFORE) { |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 4580 | positionAtCursorOffset(mPreviousOffsets[index], false, fromTouchScreen); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4581 | } |
| 4582 | } |
| 4583 | |
| 4584 | public boolean offsetHasBeenChanged() { |
| 4585 | return mNumberPreviousOffsets > 1; |
| 4586 | } |
| 4587 | |
| 4588 | @Override |
| 4589 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4590 | setMeasuredDimension(getPreferredWidth(), getPreferredHeight()); |
| 4591 | } |
| 4592 | |
Keisuke Kuroyanagi | c14e127 | 2016-04-05 15:39:24 +0900 | [diff] [blame] | 4593 | @Override |
| 4594 | public void invalidate() { |
| 4595 | super.invalidate(); |
| 4596 | if (isShowing()) { |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 4597 | positionAtCursorOffset(getCurrentCursorOffset(), true, false); |
Keisuke Kuroyanagi | c14e127 | 2016-04-05 15:39:24 +0900 | [diff] [blame] | 4598 | } |
| 4599 | }; |
| 4600 | |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4601 | private int getPreferredWidth() { |
| 4602 | return Math.max(mDrawable.getIntrinsicWidth(), mMinSize); |
| 4603 | } |
| 4604 | |
| 4605 | private int getPreferredHeight() { |
| 4606 | return Math.max(mDrawable.getIntrinsicHeight(), mMinSize); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4607 | } |
| 4608 | |
| 4609 | public void show() { |
Nikita Dubrovsky | 05cfcc8 | 2019-10-24 08:57:32 -0700 | [diff] [blame] | 4610 | if (TextView.DEBUG_CURSOR) { |
| 4611 | logCursor(getClass().getSimpleName() + ": HandleView: show()", "offset=%s", |
| 4612 | getCurrentCursorOffset()); |
| 4613 | } |
| 4614 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4615 | if (isShowing()) return; |
| 4616 | |
| 4617 | getPositionListener().addSubscriber(this, true /* local position may change */); |
| 4618 | |
| 4619 | // Make sure the offset is always considered new, even when focusing at same position |
| 4620 | mPreviousOffset = -1; |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 4621 | positionAtCursorOffset(getCurrentCursorOffset(), false, false); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4622 | } |
| 4623 | |
| 4624 | protected void dismiss() { |
| 4625 | mIsDragging = false; |
| 4626 | mContainer.dismiss(); |
| 4627 | onDetached(); |
| 4628 | } |
| 4629 | |
| 4630 | public void hide() { |
Nikita Dubrovsky | 05cfcc8 | 2019-10-24 08:57:32 -0700 | [diff] [blame] | 4631 | if (TextView.DEBUG_CURSOR) { |
| 4632 | logCursor(getClass().getSimpleName() + ": HandleView: hide()", "offset=%s", |
| 4633 | getCurrentCursorOffset()); |
| 4634 | } |
| 4635 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4636 | dismiss(); |
| 4637 | |
| 4638 | getPositionListener().removeSubscriber(this); |
| 4639 | } |
| 4640 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4641 | public boolean isShowing() { |
| 4642 | return mContainer.isShowing(); |
| 4643 | } |
| 4644 | |
Mihai Popa | b1b423a | 2018-03-27 19:03:09 +0100 | [diff] [blame] | 4645 | private boolean shouldShow() { |
| 4646 | // A dragging handle should always be shown. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4647 | if (mIsDragging) { |
| 4648 | return true; |
| 4649 | } |
| 4650 | |
| 4651 | if (mTextView.isInBatchEditMode()) { |
| 4652 | return false; |
| 4653 | } |
| 4654 | |
Phil Weaver | c2e2893 | 2016-12-08 12:29:25 -0800 | [diff] [blame] | 4655 | return mTextView.isPositionVisible( |
| 4656 | mPositionX + mHotspotX + getHorizontalOffset(), mPositionY); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4657 | } |
| 4658 | |
Mihai Popa | b1b423a | 2018-03-27 19:03:09 +0100 | [diff] [blame] | 4659 | private void setVisible(final boolean visible) { |
| 4660 | mContainer.getContentView().setVisibility(visible ? VISIBLE : INVISIBLE); |
| 4661 | } |
| 4662 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4663 | public abstract int getCurrentCursorOffset(); |
| 4664 | |
| 4665 | protected abstract void updateSelection(int offset); |
| 4666 | |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 4667 | protected abstract void updatePosition(float x, float y, boolean fromTouchScreen); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4668 | |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 4669 | @MagnifierHandleTrigger |
| 4670 | protected abstract int getMagnifierHandleTrigger(); |
| 4671 | |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4672 | protected boolean isAtRtlRun(@NonNull Layout layout, int offset) { |
| 4673 | return layout.isRtlCharAt(offset); |
| 4674 | } |
| 4675 | |
| 4676 | @VisibleForTesting |
| 4677 | public float getHorizontal(@NonNull Layout layout, int offset) { |
| 4678 | return layout.getPrimaryHorizontal(offset); |
| 4679 | } |
| 4680 | |
| 4681 | protected int getOffsetAtCoordinate(@NonNull Layout layout, int line, float x) { |
| 4682 | return mTextView.getOffsetAtCoordinate(line, x); |
| 4683 | } |
| 4684 | |
Keisuke Kuroyanagi | c14e127 | 2016-04-05 15:39:24 +0900 | [diff] [blame] | 4685 | /** |
| 4686 | * @param offset Cursor offset. Must be in [-1, length]. |
| 4687 | * @param forceUpdatePosition whether to force update the position. This should be true |
| 4688 | * when If the parent has been scrolled, for example. |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 4689 | * @param fromTouchScreen {@code true} if the cursor is moved with motion events from the |
| 4690 | * touch screen. |
Keisuke Kuroyanagi | c14e127 | 2016-04-05 15:39:24 +0900 | [diff] [blame] | 4691 | */ |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 4692 | protected void positionAtCursorOffset(int offset, boolean forceUpdatePosition, |
| 4693 | boolean fromTouchScreen) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4694 | // A HandleView relies on the layout, which may be nulled by external methods |
| 4695 | Layout layout = mTextView.getLayout(); |
| 4696 | if (layout == null) { |
| 4697 | // Will update controllers' state, hiding them and stopping selection mode if needed |
| 4698 | prepareCursorControllers(); |
| 4699 | return; |
| 4700 | } |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 4701 | layout = mTextView.getLayout(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4702 | |
| 4703 | boolean offsetChanged = offset != mPreviousOffset; |
Keisuke Kuroyanagi | c14e127 | 2016-04-05 15:39:24 +0900 | [diff] [blame] | 4704 | if (offsetChanged || forceUpdatePosition) { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4705 | if (offsetChanged) { |
| 4706 | updateSelection(offset); |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 4707 | if (fromTouchScreen && mHapticTextHandleEnabled) { |
| 4708 | mTextView.performHapticFeedback(HapticFeedbackConstants.TEXT_HANDLE_MOVE); |
| 4709 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4710 | addPositionToTouchUpFilter(offset); |
| 4711 | } |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 4712 | final int line = layout.getLineForOffset(offset); |
Mady Mellor | b9bbbb1 | 2015-03-23 11:50:46 -0700 | [diff] [blame] | 4713 | mPrevLine = line; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4714 | |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 4715 | mPositionX = getCursorHorizontalPosition(layout, offset) - mHotspotX |
| 4716 | - getHorizontalOffset() + getCursorOffset(); |
Siyamed Sinir | a60b59d | 2017-07-26 09:26:41 -0700 | [diff] [blame] | 4717 | mPositionY = layout.getLineBottomWithoutSpacing(line); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4718 | |
| 4719 | // Take TextView's padding and scroll into account. |
| 4720 | mPositionX += mTextView.viewportToContentHorizontalOffset(); |
| 4721 | mPositionY += mTextView.viewportToContentVerticalOffset(); |
| 4722 | |
| 4723 | mPreviousOffset = offset; |
| 4724 | mPositionHasChanged = true; |
| 4725 | } |
| 4726 | } |
| 4727 | |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 4728 | /** |
Roozbeh Pournader | 9c13307 | 2017-07-26 22:36:27 -0700 | [diff] [blame] | 4729 | * Return the clamped horizontal position for the cursor. |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 4730 | * |
| 4731 | * @param layout Text layout. |
| 4732 | * @param offset Character offset for the cursor. |
| 4733 | * @return The clamped horizontal position for the cursor. |
| 4734 | */ |
| 4735 | int getCursorHorizontalPosition(Layout layout, int offset) { |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 4736 | return (int) (getHorizontal(layout, offset) - 0.5f); |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 4737 | } |
| 4738 | |
Keisuke Kuroyanagi | e2a3b1e | 2016-04-28 12:55:18 +0900 | [diff] [blame] | 4739 | @Override |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4740 | public void updatePosition(int parentPositionX, int parentPositionY, |
| 4741 | boolean parentPositionChanged, boolean parentScrolled) { |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 4742 | positionAtCursorOffset(getCurrentCursorOffset(), parentScrolled, false); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4743 | if (parentPositionChanged || mPositionHasChanged) { |
| 4744 | if (mIsDragging) { |
| 4745 | // Update touchToWindow offset in case of parent scrolling while dragging |
| 4746 | if (parentPositionX != mLastParentX || parentPositionY != mLastParentY) { |
| 4747 | mTouchToWindowOffsetX += parentPositionX - mLastParentX; |
| 4748 | mTouchToWindowOffsetY += parentPositionY - mLastParentY; |
| 4749 | mLastParentX = parentPositionX; |
| 4750 | mLastParentY = parentPositionY; |
| 4751 | } |
| 4752 | |
| 4753 | onHandleMoved(); |
| 4754 | } |
| 4755 | |
Mihai Popa | b1b423a | 2018-03-27 19:03:09 +0100 | [diff] [blame] | 4756 | if (shouldShow()) { |
Seigo Nonaka | 2f229ca | 2016-02-18 17:53:54 +0900 | [diff] [blame] | 4757 | // Transform to the window coordinates to follow the view tranformation. |
| 4758 | final int[] pts = { mPositionX + mHotspotX + getHorizontalOffset(), mPositionY}; |
| 4759 | mTextView.transformFromViewToWindowSpace(pts); |
| 4760 | pts[0] -= mHotspotX + getHorizontalOffset(); |
| 4761 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4762 | if (isShowing()) { |
Seigo Nonaka | 2f229ca | 2016-02-18 17:53:54 +0900 | [diff] [blame] | 4763 | mContainer.update(pts[0], pts[1], -1, -1); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4764 | } else { |
Seigo Nonaka | 2f229ca | 2016-02-18 17:53:54 +0900 | [diff] [blame] | 4765 | mContainer.showAtLocation(mTextView, Gravity.NO_GRAVITY, pts[0], pts[1]); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4766 | } |
| 4767 | } else { |
| 4768 | if (isShowing()) { |
| 4769 | dismiss(); |
| 4770 | } |
| 4771 | } |
| 4772 | |
| 4773 | mPositionHasChanged = false; |
| 4774 | } |
| 4775 | } |
| 4776 | |
| 4777 | @Override |
| 4778 | protected void onDraw(Canvas c) { |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4779 | final int drawWidth = mDrawable.getIntrinsicWidth(); |
| 4780 | final int left = getHorizontalOffset(); |
| 4781 | |
| 4782 | mDrawable.setBounds(left, 0, left + drawWidth, mDrawable.getIntrinsicHeight()); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 4783 | mDrawable.draw(c); |
| 4784 | } |
| 4785 | |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 4786 | private int getHorizontalOffset() { |
| 4787 | final int width = getPreferredWidth(); |
| 4788 | final int drawWidth = mDrawable.getIntrinsicWidth(); |
| 4789 | final int left; |
| 4790 | switch (mHorizontalGravity) { |
| 4791 | case Gravity.LEFT: |
| 4792 | left = 0; |
| 4793 | break; |
| 4794 | default: |
| 4795 | case Gravity.CENTER: |
| 4796 | left = (width - drawWidth) / 2; |
| 4797 | break; |
| 4798 | case Gravity.RIGHT: |
| 4799 | left = width - drawWidth; |
| 4800 | break; |
| 4801 | } |
| 4802 | return left; |
| 4803 | } |
| 4804 | |
| 4805 | protected int getCursorOffset() { |
| 4806 | return 0; |
| 4807 | } |
| 4808 | |
Mihai Popa | b1b423a | 2018-03-27 19:03:09 +0100 | [diff] [blame] | 4809 | private boolean tooLargeTextForMagnifier() { |
| 4810 | final float magnifierContentHeight = Math.round( |
| 4811 | mMagnifierAnimator.mMagnifier.getHeight() |
| 4812 | / mMagnifierAnimator.mMagnifier.getZoom()); |
| 4813 | final Paint.FontMetrics fontMetrics = mTextView.getPaint().getFontMetrics(); |
| 4814 | final float glyphHeight = fontMetrics.descent - fontMetrics.ascent; |
Mihai Popa | 6d26d15 | 2019-01-30 15:36:47 +0000 | [diff] [blame] | 4815 | return glyphHeight * mTextViewScaleY > magnifierContentHeight; |
Mihai Popa | b1b423a | 2018-03-27 19:03:09 +0100 | [diff] [blame] | 4816 | } |
| 4817 | |
Mihai Popa | 6d26d15 | 2019-01-30 15:36:47 +0000 | [diff] [blame] | 4818 | /** |
| 4819 | * Traverses the hierarchy above the text view, and computes the total scale applied |
| 4820 | * to it. If a rotation is encountered, the method returns {@code false}, indicating |
| 4821 | * that the magnifier should not be shown anyways. It would be nice to keep these two |
| 4822 | * pieces of logic separate (the rotation check and the total scale calculation), |
| 4823 | * but for efficiency we can do them in a single go. |
| 4824 | * @return whether the text view is rotated |
| 4825 | */ |
| 4826 | private boolean checkForTransforms() { |
Mihai Popa | ddf9fe0 | 2018-09-28 13:54:19 +0100 | [diff] [blame] | 4827 | if (mMagnifierAnimator.mMagnifierIsShowing) { |
| 4828 | // Do not check again when the magnifier is currently showing. |
Mihai Popa | ddf9fe0 | 2018-09-28 13:54:19 +0100 | [diff] [blame] | 4829 | return true; |
| 4830 | } |
Mihai Popa | 6d26d15 | 2019-01-30 15:36:47 +0000 | [diff] [blame] | 4831 | |
| 4832 | if (mTextView.getRotation() != 0f || mTextView.getRotationX() != 0f |
| 4833 | || mTextView.getRotationY() != 0f) { |
| 4834 | return false; |
| 4835 | } |
| 4836 | mTextViewScaleX = mTextView.getScaleX(); |
| 4837 | mTextViewScaleY = mTextView.getScaleY(); |
| 4838 | |
Mihai Popa | ddf9fe0 | 2018-09-28 13:54:19 +0100 | [diff] [blame] | 4839 | ViewParent viewParent = mTextView.getParent(); |
| 4840 | while (viewParent != null) { |
Mihai Popa | 6d26d15 | 2019-01-30 15:36:47 +0000 | [diff] [blame] | 4841 | if (viewParent instanceof View) { |
| 4842 | final View view = (View) viewParent; |
| 4843 | if (view.getRotation() != 0f || view.getRotationX() != 0f |
| 4844 | || view.getRotationY() != 0f) { |
| 4845 | return false; |
| 4846 | } |
| 4847 | mTextViewScaleX *= view.getScaleX(); |
| 4848 | mTextViewScaleY *= view.getScaleY(); |
Mihai Popa | ddf9fe0 | 2018-09-28 13:54:19 +0100 | [diff] [blame] | 4849 | } |
| 4850 | viewParent = viewParent.getParent(); |
| 4851 | } |
Mihai Popa | 6d26d15 | 2019-01-30 15:36:47 +0000 | [diff] [blame] | 4852 | return true; |
Mihai Popa | ddf9fe0 | 2018-09-28 13:54:19 +0100 | [diff] [blame] | 4853 | } |
| 4854 | |
Mihai Popa | e301746 | 2018-03-07 12:25:21 +0000 | [diff] [blame] | 4855 | /** |
| 4856 | * Computes the position where the magnifier should be shown, relative to |
| 4857 | * {@code mTextView}, and writes them to {@code showPosInView}. Also decides |
| 4858 | * whether the magnifier should be shown or dismissed after this touch event. |
| 4859 | * @return Whether the magnifier should be shown at the computed coordinates or dismissed. |
| 4860 | */ |
| 4861 | private boolean obtainMagnifierShowCoordinates(@NonNull final MotionEvent event, |
| 4862 | final PointF showPosInView) { |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 4863 | |
| 4864 | final int trigger = getMagnifierHandleTrigger(); |
| 4865 | final int offset; |
Mihai Popa | 27e4dfb | 2018-03-07 14:52:05 +0000 | [diff] [blame] | 4866 | final int otherHandleOffset; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 4867 | switch (trigger) { |
Mihai Popa | 27e4dfb | 2018-03-07 14:52:05 +0000 | [diff] [blame] | 4868 | case MagnifierHandleTrigger.INSERTION: |
| 4869 | offset = mTextView.getSelectionStart(); |
| 4870 | otherHandleOffset = -1; |
| 4871 | break; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 4872 | case MagnifierHandleTrigger.SELECTION_START: |
| 4873 | offset = mTextView.getSelectionStart(); |
Mihai Popa | 27e4dfb | 2018-03-07 14:52:05 +0000 | [diff] [blame] | 4874 | otherHandleOffset = mTextView.getSelectionEnd(); |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 4875 | break; |
| 4876 | case MagnifierHandleTrigger.SELECTION_END: |
| 4877 | offset = mTextView.getSelectionEnd(); |
Mihai Popa | 27e4dfb | 2018-03-07 14:52:05 +0000 | [diff] [blame] | 4878 | otherHandleOffset = mTextView.getSelectionStart(); |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 4879 | break; |
| 4880 | default: |
| 4881 | offset = -1; |
Mihai Popa | 27e4dfb | 2018-03-07 14:52:05 +0000 | [diff] [blame] | 4882 | otherHandleOffset = -1; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 4883 | break; |
| 4884 | } |
| 4885 | |
| 4886 | if (offset == -1) { |
Mihai Popa | e301746 | 2018-03-07 12:25:21 +0000 | [diff] [blame] | 4887 | return false; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 4888 | } |
| 4889 | |
Mihai Popa | 6d26d15 | 2019-01-30 15:36:47 +0000 | [diff] [blame] | 4890 | if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { |
| 4891 | mCurrentDragInitialTouchRawX = event.getRawX(); |
| 4892 | } else if (event.getActionMasked() == MotionEvent.ACTION_UP) { |
| 4893 | mCurrentDragInitialTouchRawX = UNSET_X_VALUE; |
| 4894 | } |
| 4895 | |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 4896 | final Layout layout = mTextView.getLayout(); |
| 4897 | final int lineNumber = layout.getLineForOffset(offset); |
Mihai Popa | 27e4dfb | 2018-03-07 14:52:05 +0000 | [diff] [blame] | 4898 | // Compute whether the selection handles are currently on the same line, and, |
| 4899 | // in this particular case, whether the selected text is right to left. |
| 4900 | final boolean sameLineSelection = otherHandleOffset != -1 |
| 4901 | && lineNumber == layout.getLineForOffset(otherHandleOffset); |
| 4902 | final boolean rtl = sameLineSelection |
| 4903 | && (offset < otherHandleOffset) |
| 4904 | != (getHorizontal(mTextView.getLayout(), offset) |
| 4905 | < getHorizontal(mTextView.getLayout(), otherHandleOffset)); |
Mihai Popa | e301746 | 2018-03-07 12:25:21 +0000 | [diff] [blame] | 4906 | |
Mihai Popa | 27e4dfb | 2018-03-07 14:52:05 +0000 | [diff] [blame] | 4907 | // Horizontally move the magnifier smoothly, clamp inside the current line / selection. |
Mihai Popa | 1d1ed0c | 2018-01-12 12:38:12 +0000 | [diff] [blame] | 4908 | final int[] textViewLocationOnScreen = new int[2]; |
| 4909 | mTextView.getLocationOnScreen(textViewLocationOnScreen); |
Mihai Popa | e301746 | 2018-03-07 12:25:21 +0000 | [diff] [blame] | 4910 | final float touchXInView = event.getRawX() - textViewLocationOnScreen[0]; |
Mihai Popa | 27e4dfb | 2018-03-07 14:52:05 +0000 | [diff] [blame] | 4911 | float leftBound = mTextView.getTotalPaddingLeft() - mTextView.getScrollX(); |
| 4912 | float rightBound = mTextView.getTotalPaddingLeft() - mTextView.getScrollX(); |
| 4913 | if (sameLineSelection && ((trigger == MagnifierHandleTrigger.SELECTION_END) ^ rtl)) { |
| 4914 | leftBound += getHorizontal(mTextView.getLayout(), otherHandleOffset); |
| 4915 | } else { |
| 4916 | leftBound += mTextView.getLayout().getLineLeft(lineNumber); |
| 4917 | } |
| 4918 | if (sameLineSelection && ((trigger == MagnifierHandleTrigger.SELECTION_START) ^ rtl)) { |
| 4919 | rightBound += getHorizontal(mTextView.getLayout(), otherHandleOffset); |
| 4920 | } else { |
| 4921 | rightBound += mTextView.getLayout().getLineRight(lineNumber); |
| 4922 | } |
Mihai Popa | 6d26d15 | 2019-01-30 15:36:47 +0000 | [diff] [blame] | 4923 | leftBound *= mTextViewScaleX; |
| 4924 | rightBound *= mTextViewScaleX; |
Mihai Popa | 3872238 | 2018-03-07 19:56:21 +0000 | [diff] [blame] | 4925 | final float contentWidth = Math.round(mMagnifierAnimator.mMagnifier.getWidth() |
| 4926 | / mMagnifierAnimator.mMagnifier.getZoom()); |
Mihai Popa | 27e4dfb | 2018-03-07 14:52:05 +0000 | [diff] [blame] | 4927 | if (touchXInView < leftBound - contentWidth / 2 |
| 4928 | || touchXInView > rightBound + contentWidth / 2) { |
| 4929 | // The touch is too far from the current line / selection, so hide the magnifier. |
Mihai Popa | e301746 | 2018-03-07 12:25:21 +0000 | [diff] [blame] | 4930 | return false; |
| 4931 | } |
Mihai Popa | 6d26d15 | 2019-01-30 15:36:47 +0000 | [diff] [blame] | 4932 | |
| 4933 | final float scaledTouchXInView; |
| 4934 | if (mTextViewScaleX == 1f) { |
| 4935 | // In the common case, do not use mCurrentDragInitialTouchRawX to compute this |
| 4936 | // coordinate, although the formula on the else branch should be equivalent. |
| 4937 | // Since the formula relies on mCurrentDragInitialTouchRawX being set on |
| 4938 | // MotionEvent.ACTION_DOWN, this makes us more defensive against cases when |
| 4939 | // the sequence of events might not look as expected: for example, a sequence of |
| 4940 | // ACTION_MOVE not preceded by ACTION_DOWN. |
| 4941 | scaledTouchXInView = touchXInView; |
| 4942 | } else { |
| 4943 | scaledTouchXInView = (event.getRawX() - mCurrentDragInitialTouchRawX) |
| 4944 | * mTextViewScaleX + mCurrentDragInitialTouchRawX |
| 4945 | - textViewLocationOnScreen[0]; |
| 4946 | } |
| 4947 | showPosInView.x = Math.max(leftBound, Math.min(rightBound, scaledTouchXInView)); |
Mihai Popa | e301746 | 2018-03-07 12:25:21 +0000 | [diff] [blame] | 4948 | |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 4949 | // Vertically snap to middle of current line. |
Mihai Popa | 6d26d15 | 2019-01-30 15:36:47 +0000 | [diff] [blame] | 4950 | showPosInView.y = ((mTextView.getLayout().getLineTop(lineNumber) |
Andrei Stingaceanu | ca189fe | 2017-10-19 17:02:22 +0100 | [diff] [blame] | 4951 | + mTextView.getLayout().getLineBottom(lineNumber)) / 2.0f |
Mihai Popa | 6d26d15 | 2019-01-30 15:36:47 +0000 | [diff] [blame] | 4952 | + mTextView.getTotalPaddingTop() - mTextView.getScrollY()) * mTextViewScaleY; |
Mihai Popa | e301746 | 2018-03-07 12:25:21 +0000 | [diff] [blame] | 4953 | return true; |
| 4954 | } |
Mihai Popa | a4e39c4 | 2018-02-20 15:31:11 +0000 | [diff] [blame] | 4955 | |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 4956 | private boolean handleOverlapsMagnifier(@NonNull final HandleView handle, |
| 4957 | @NonNull final Rect magnifierRect) { |
| 4958 | final PopupWindow window = handle.mContainer; |
| 4959 | if (!window.hasDecorView()) { |
| 4960 | return false; |
| 4961 | } |
| 4962 | final Rect handleRect = new Rect( |
| 4963 | window.getDecorViewLayoutParams().x, |
| 4964 | window.getDecorViewLayoutParams().y, |
| 4965 | window.getDecorViewLayoutParams().x + window.getContentView().getWidth(), |
| 4966 | window.getDecorViewLayoutParams().y + window.getContentView().getHeight()); |
| 4967 | return Rect.intersects(handleRect, magnifierRect); |
Mihai Popa | 894469c | 2018-03-21 19:45:06 +0000 | [diff] [blame] | 4968 | } |
| 4969 | |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 4970 | private @Nullable HandleView getOtherSelectionHandle() { |
| 4971 | final SelectionModifierCursorController controller = getSelectionController(); |
| 4972 | if (controller == null || !controller.isActive()) { |
| 4973 | return null; |
| 4974 | } |
| 4975 | return controller.mStartHandle != this |
| 4976 | ? controller.mStartHandle |
| 4977 | : controller.mEndHandle; |
| 4978 | } |
| 4979 | |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 4980 | private void updateHandlesVisibility() { |
| 4981 | final Point magnifierTopLeft = mMagnifierAnimator.mMagnifier.getPosition(); |
| 4982 | if (magnifierTopLeft == null) { |
| 4983 | return; |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 4984 | } |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 4985 | final Rect magnifierRect = new Rect(magnifierTopLeft.x, magnifierTopLeft.y, |
| 4986 | magnifierTopLeft.x + mMagnifierAnimator.mMagnifier.getWidth(), |
| 4987 | magnifierTopLeft.y + mMagnifierAnimator.mMagnifier.getHeight()); |
| 4988 | setVisible(!handleOverlapsMagnifier(HandleView.this, magnifierRect)); |
| 4989 | final HandleView otherHandle = getOtherSelectionHandle(); |
| 4990 | if (otherHandle != null) { |
| 4991 | otherHandle.setVisible(!handleOverlapsMagnifier(otherHandle, magnifierRect)); |
| 4992 | } |
| 4993 | } |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 4994 | |
Mihai Popa | e301746 | 2018-03-07 12:25:21 +0000 | [diff] [blame] | 4995 | protected final void updateMagnifier(@NonNull final MotionEvent event) { |
Mihai Popa | 3872238 | 2018-03-07 19:56:21 +0000 | [diff] [blame] | 4996 | if (mMagnifierAnimator == null) { |
Mihai Popa | e301746 | 2018-03-07 12:25:21 +0000 | [diff] [blame] | 4997 | return; |
| 4998 | } |
| 4999 | |
| 5000 | final PointF showPosInView = new PointF(); |
Mihai Popa | 6d26d15 | 2019-01-30 15:36:47 +0000 | [diff] [blame] | 5001 | final boolean shouldShow = checkForTransforms() /*check not rotated and compute scale*/ |
| 5002 | && !tooLargeTextForMagnifier() |
Mihai Popa | 894469c | 2018-03-21 19:45:06 +0000 | [diff] [blame] | 5003 | && obtainMagnifierShowCoordinates(event, showPosInView); |
Mihai Popa | e301746 | 2018-03-07 12:25:21 +0000 | [diff] [blame] | 5004 | if (shouldShow) { |
| 5005 | // Make the cursor visible and stop blinking. |
| 5006 | mRenderCursorRegardlessTiming = true; |
| 5007 | mTextView.invalidateCursorPath(); |
| 5008 | suspendBlink(); |
Mihai Popa | b1b423a | 2018-03-27 19:03:09 +0100 | [diff] [blame] | 5009 | |
Mihai Popa | 3872238 | 2018-03-07 19:56:21 +0000 | [diff] [blame] | 5010 | mMagnifierAnimator.show(showPosInView.x, showPosInView.y); |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 5011 | updateHandlesVisibility(); |
Mihai Popa | e301746 | 2018-03-07 12:25:21 +0000 | [diff] [blame] | 5012 | } else { |
| 5013 | dismissMagnifier(); |
| 5014 | } |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 5015 | } |
| 5016 | |
| 5017 | protected final void dismissMagnifier() { |
Mihai Popa | 3872238 | 2018-03-07 19:56:21 +0000 | [diff] [blame] | 5018 | if (mMagnifierAnimator != null) { |
| 5019 | mMagnifierAnimator.dismiss(); |
Mihai Popa | a4e39c4 | 2018-02-20 15:31:11 +0000 | [diff] [blame] | 5020 | mRenderCursorRegardlessTiming = false; |
Andrei Stingaceanu | 451f947 | 2017-10-13 16:41:28 +0100 | [diff] [blame] | 5021 | resumeBlink(); |
Mihai Popa | b1b423a | 2018-03-27 19:03:09 +0100 | [diff] [blame] | 5022 | setVisible(true); |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 5023 | final HandleView otherHandle = getOtherSelectionHandle(); |
| 5024 | if (otherHandle != null) { |
| 5025 | otherHandle.setVisible(true); |
| 5026 | } |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 5027 | } |
| 5028 | } |
| 5029 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5030 | @Override |
| 5031 | public boolean onTouchEvent(MotionEvent ev) { |
Nikita Dubrovsky | 05cfcc8 | 2019-10-24 08:57:32 -0700 | [diff] [blame] | 5032 | if (TextView.DEBUG_CURSOR) { |
| 5033 | logCursor(this.getClass().getSimpleName() + ": HandleView: onTouchEvent", |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 5034 | "%d: %s (%f,%f)", |
| 5035 | ev.getSequenceNumber(), |
| 5036 | MotionEvent.actionToString(ev.getActionMasked()), |
| 5037 | ev.getX(), ev.getY()); |
Nikita Dubrovsky | 05cfcc8 | 2019-10-24 08:57:32 -0700 | [diff] [blame] | 5038 | } |
| 5039 | |
Abodunrinwa Toki | fd3a3a1 | 2015-05-05 20:04:34 +0100 | [diff] [blame] | 5040 | updateFloatingToolbarVisibility(ev); |
| 5041 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5042 | switch (ev.getActionMasked()) { |
| 5043 | case MotionEvent.ACTION_DOWN: { |
| 5044 | startTouchUpFilter(getCurrentCursorOffset()); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5045 | |
| 5046 | final PositionListener positionListener = getPositionListener(); |
| 5047 | mLastParentX = positionListener.getPositionX(); |
| 5048 | mLastParentY = positionListener.getPositionY(); |
Keisuke Kuroyanagi | e2a3b1e | 2016-04-28 12:55:18 +0900 | [diff] [blame] | 5049 | mLastParentXOnScreen = positionListener.getPositionXOnScreen(); |
| 5050 | mLastParentYOnScreen = positionListener.getPositionYOnScreen(); |
| 5051 | |
| 5052 | final float xInWindow = ev.getRawX() - mLastParentXOnScreen + mLastParentX; |
| 5053 | final float yInWindow = ev.getRawY() - mLastParentYOnScreen + mLastParentY; |
| 5054 | mTouchToWindowOffsetX = xInWindow - mPositionX; |
| 5055 | mTouchToWindowOffsetY = yInWindow - mPositionY; |
| 5056 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5057 | mIsDragging = true; |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 5058 | mPreviousLineTouched = UNSET_LINE; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5059 | break; |
| 5060 | } |
| 5061 | |
| 5062 | case MotionEvent.ACTION_MOVE: { |
Keisuke Kuroyanagi | e2a3b1e | 2016-04-28 12:55:18 +0900 | [diff] [blame] | 5063 | final float xInWindow = ev.getRawX() - mLastParentXOnScreen + mLastParentX; |
| 5064 | final float yInWindow = ev.getRawY() - mLastParentYOnScreen + mLastParentY; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5065 | |
| 5066 | // Vertical hysteresis: vertical down movement tends to snap to ideal offset |
| 5067 | final float previousVerticalOffset = mTouchToWindowOffsetY - mLastParentY; |
Keisuke Kuroyanagi | e2a3b1e | 2016-04-28 12:55:18 +0900 | [diff] [blame] | 5068 | final float currentVerticalOffset = yInWindow - mPositionY - mLastParentY; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5069 | float newVerticalOffset; |
| 5070 | if (previousVerticalOffset < mIdealVerticalOffset) { |
| 5071 | newVerticalOffset = Math.min(currentVerticalOffset, mIdealVerticalOffset); |
| 5072 | newVerticalOffset = Math.max(newVerticalOffset, previousVerticalOffset); |
| 5073 | } else { |
| 5074 | newVerticalOffset = Math.max(currentVerticalOffset, mIdealVerticalOffset); |
| 5075 | newVerticalOffset = Math.min(newVerticalOffset, previousVerticalOffset); |
| 5076 | } |
| 5077 | mTouchToWindowOffsetY = newVerticalOffset + mLastParentY; |
| 5078 | |
Keisuke Kuroyanagi | bc89a5c | 2015-05-18 14:49:29 +0900 | [diff] [blame] | 5079 | final float newPosX = |
Keisuke Kuroyanagi | e2a3b1e | 2016-04-28 12:55:18 +0900 | [diff] [blame] | 5080 | xInWindow - mTouchToWindowOffsetX + mHotspotX + getHorizontalOffset(); |
| 5081 | final float newPosY = yInWindow - mTouchToWindowOffsetY + mTouchOffsetY; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5082 | |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 5083 | updatePosition(newPosX, newPosY, |
| 5084 | ev.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5085 | break; |
| 5086 | } |
| 5087 | |
| 5088 | case MotionEvent.ACTION_UP: |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 5089 | filterOnTouchUp(ev.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)); |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 5090 | // Fall through. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5091 | case MotionEvent.ACTION_CANCEL: |
| 5092 | mIsDragging = false; |
Mihai Popa | 6315a32 | 2018-10-17 17:39:57 +0100 | [diff] [blame] | 5093 | updateDrawable(false /* updateDrawableWhenDragging */); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5094 | break; |
| 5095 | } |
| 5096 | return true; |
| 5097 | } |
| 5098 | |
| 5099 | public boolean isDragging() { |
| 5100 | return mIsDragging; |
| 5101 | } |
| 5102 | |
Clara Bayarri | 6351e66 | 2015-03-16 23:17:59 +0000 | [diff] [blame] | 5103 | void onHandleMoved() {} |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5104 | |
Clara Bayarri | 6351e66 | 2015-03-16 23:17:59 +0000 | [diff] [blame] | 5105 | public void onDetached() {} |
Adam Powell | 8624121 | 2019-06-10 08:38:49 -0700 | [diff] [blame] | 5106 | |
| 5107 | @Override |
| 5108 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 5109 | super.onSizeChanged(w, h, oldw, oldh); |
| 5110 | setSystemGestureExclusionRects(Collections.singletonList(new Rect(0, 0, w, h))); |
| 5111 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5112 | } |
| 5113 | |
| 5114 | private class InsertionHandleView extends HandleView { |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 5115 | // Used to detect taps on the insertion handle, which will affect the insertion action mode |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 5116 | private float mLastDownRawX, mLastDownRawY; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5117 | private Runnable mHider; |
| 5118 | |
| 5119 | public InsertionHandleView(Drawable drawable) { |
Keisuke Kuroyanagi | da79ee6 | 2015-11-25 16:15:15 +0900 | [diff] [blame] | 5120 | super(drawable, drawable, com.android.internal.R.id.insertion_handle); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5121 | } |
| 5122 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5123 | private void hideAfterDelay() { |
| 5124 | if (mHider == null) { |
| 5125 | mHider = new Runnable() { |
| 5126 | public void run() { |
| 5127 | hide(); |
| 5128 | } |
| 5129 | }; |
| 5130 | } else { |
| 5131 | removeHiderCallback(); |
| 5132 | } |
| 5133 | mTextView.postDelayed(mHider, DELAY_BEFORE_HANDLE_FADES_OUT); |
| 5134 | } |
| 5135 | |
| 5136 | private void removeHiderCallback() { |
| 5137 | if (mHider != null) { |
| 5138 | mTextView.removeCallbacks(mHider); |
| 5139 | } |
| 5140 | } |
| 5141 | |
| 5142 | @Override |
| 5143 | protected int getHotspotX(Drawable drawable, boolean isRtlRun) { |
| 5144 | return drawable.getIntrinsicWidth() / 2; |
| 5145 | } |
| 5146 | |
| 5147 | @Override |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 5148 | protected int getHorizontalGravity(boolean isRtlRun) { |
| 5149 | return Gravity.CENTER_HORIZONTAL; |
| 5150 | } |
| 5151 | |
| 5152 | @Override |
| 5153 | protected int getCursorOffset() { |
| 5154 | int offset = super.getCursorOffset(); |
Roozbeh Pournader | e06eebf | 2017-10-03 12:13:46 -0700 | [diff] [blame] | 5155 | if (mDrawableForCursor != null) { |
| 5156 | mDrawableForCursor.getPadding(mTempRect); |
| 5157 | offset += (mDrawableForCursor.getIntrinsicWidth() |
Roozbeh Pournader | 9c13307 | 2017-07-26 22:36:27 -0700 | [diff] [blame] | 5158 | - mTempRect.left - mTempRect.right) / 2; |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 5159 | } |
| 5160 | return offset; |
| 5161 | } |
| 5162 | |
| 5163 | @Override |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 5164 | int getCursorHorizontalPosition(Layout layout, int offset) { |
Roozbeh Pournader | e06eebf | 2017-10-03 12:13:46 -0700 | [diff] [blame] | 5165 | if (mDrawableForCursor != null) { |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5166 | final float horizontal = getHorizontal(layout, offset); |
Roozbeh Pournader | e06eebf | 2017-10-03 12:13:46 -0700 | [diff] [blame] | 5167 | return clampHorizontalPosition(mDrawableForCursor, horizontal) + mTempRect.left; |
Siyamed Sinir | 217c0f7 | 2016-02-01 18:30:02 -0800 | [diff] [blame] | 5168 | } |
| 5169 | return super.getCursorHorizontalPosition(layout, offset); |
| 5170 | } |
| 5171 | |
| 5172 | @Override |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5173 | public boolean onTouchEvent(MotionEvent ev) { |
| 5174 | final boolean result = super.onTouchEvent(ev); |
| 5175 | |
| 5176 | switch (ev.getActionMasked()) { |
| 5177 | case MotionEvent.ACTION_DOWN: |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 5178 | mLastDownRawX = ev.getRawX(); |
| 5179 | mLastDownRawY = ev.getRawY(); |
Mihai Popa | e301746 | 2018-03-07 12:25:21 +0000 | [diff] [blame] | 5180 | updateMagnifier(ev); |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 5181 | break; |
| 5182 | |
| 5183 | case MotionEvent.ACTION_MOVE: |
Mihai Popa | e301746 | 2018-03-07 12:25:21 +0000 | [diff] [blame] | 5184 | updateMagnifier(ev); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5185 | break; |
| 5186 | |
| 5187 | case MotionEvent.ACTION_UP: |
| 5188 | if (!offsetHasBeenChanged()) { |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 5189 | ViewConfiguration config = ViewConfiguration.get(mTextView.getContext()); |
| 5190 | boolean isWithinTouchSlop = EditorTouchState.isDistanceWithin( |
| 5191 | mLastDownRawX, mLastDownRawY, ev.getRawX(), ev.getRawY(), |
| 5192 | config.getScaledTouchSlop()); |
| 5193 | if (isWithinTouchSlop) { |
Clara Bayarri | b71dddd | 2015-06-04 23:17:30 +0100 | [diff] [blame] | 5194 | // Tapping on the handle toggles the insertion action mode. |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 5195 | if (mTextActionMode != null) { |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 5196 | stopTextActionMode(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5197 | } else { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 5198 | startInsertionActionMode(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5199 | } |
| 5200 | } |
Abodunrinwa Toki | bcdf0ab | 2015-04-25 00:11:25 +0100 | [diff] [blame] | 5201 | } else { |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 5202 | if (mTextActionMode != null) { |
| 5203 | mTextActionMode.invalidateContentRect(); |
Abodunrinwa Toki | bcdf0ab | 2015-04-25 00:11:25 +0100 | [diff] [blame] | 5204 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5205 | } |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 5206 | // Fall through. |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5207 | case MotionEvent.ACTION_CANCEL: |
| 5208 | hideAfterDelay(); |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 5209 | dismissMagnifier(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5210 | break; |
| 5211 | |
| 5212 | default: |
| 5213 | break; |
| 5214 | } |
| 5215 | |
| 5216 | return result; |
| 5217 | } |
| 5218 | |
| 5219 | @Override |
| 5220 | public int getCurrentCursorOffset() { |
| 5221 | return mTextView.getSelectionStart(); |
| 5222 | } |
| 5223 | |
| 5224 | @Override |
| 5225 | public void updateSelection(int offset) { |
| 5226 | Selection.setSelection((Spannable) mTextView.getText(), offset); |
| 5227 | } |
| 5228 | |
| 5229 | @Override |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 5230 | protected void updatePosition(float x, float y, boolean fromTouchScreen) { |
Mady Mellor | ee3821e | 2015-06-05 11:12:01 -0700 | [diff] [blame] | 5231 | Layout layout = mTextView.getLayout(); |
| 5232 | int offset; |
| 5233 | if (layout != null) { |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 5234 | if (mPreviousLineTouched == UNSET_LINE) { |
| 5235 | mPreviousLineTouched = mTextView.getLineAtCoordinate(y); |
| 5236 | } |
| 5237 | int currLine = getCurrentLineAdjustedForSlop(layout, mPreviousLineTouched, y); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5238 | offset = getOffsetAtCoordinate(layout, currLine, x); |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 5239 | mPreviousLineTouched = currLine; |
Mady Mellor | ee3821e | 2015-06-05 11:12:01 -0700 | [diff] [blame] | 5240 | } else { |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5241 | offset = -1; |
Mady Mellor | ee3821e | 2015-06-05 11:12:01 -0700 | [diff] [blame] | 5242 | } |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 5243 | if (TextView.DEBUG_CURSOR) { |
| 5244 | logCursor("InsertionHandleView: updatePosition", "x=%f, y=%f, offset=%d, line=%d", |
| 5245 | x, y, offset, mPreviousLineTouched); |
| 5246 | } |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 5247 | positionAtCursorOffset(offset, false, fromTouchScreen); |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 5248 | if (mTextActionMode != null) { |
Abodunrinwa Toki | 66c1627 | 2017-05-03 20:22:55 +0100 | [diff] [blame] | 5249 | invalidateActionMode(); |
Clara Bayarri | 1baed51 | 2015-05-11 15:29:16 +0100 | [diff] [blame] | 5250 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5251 | } |
| 5252 | |
| 5253 | @Override |
| 5254 | void onHandleMoved() { |
| 5255 | super.onHandleMoved(); |
| 5256 | removeHiderCallback(); |
| 5257 | } |
| 5258 | |
| 5259 | @Override |
| 5260 | public void onDetached() { |
| 5261 | super.onDetached(); |
| 5262 | removeHiderCallback(); |
| 5263 | } |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 5264 | |
| 5265 | @Override |
| 5266 | @MagnifierHandleTrigger |
| 5267 | protected int getMagnifierHandleTrigger() { |
| 5268 | return MagnifierHandleTrigger.INSERTION; |
| 5269 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5270 | } |
| 5271 | |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5272 | @Retention(RetentionPolicy.SOURCE) |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 5273 | @IntDef(prefix = { "HANDLE_TYPE_" }, value = { |
| 5274 | HANDLE_TYPE_SELECTION_START, |
| 5275 | HANDLE_TYPE_SELECTION_END |
| 5276 | }) |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5277 | public @interface HandleType {} |
| 5278 | public static final int HANDLE_TYPE_SELECTION_START = 0; |
| 5279 | public static final int HANDLE_TYPE_SELECTION_END = 1; |
| 5280 | |
Abodunrinwa Toki | 4a056a5 | 2017-08-05 01:56:40 +0100 | [diff] [blame] | 5281 | /** For selection handles */ |
| 5282 | @VisibleForTesting |
| 5283 | public final class SelectionHandleView extends HandleView { |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5284 | // Indicates the handle type, selection start (HANDLE_TYPE_SELECTION_START) or selection |
| 5285 | // end (HANDLE_TYPE_SELECTION_END). |
| 5286 | @HandleType |
| 5287 | private final int mHandleType; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5288 | // Indicates whether the cursor is making adjustments within a word. |
| 5289 | private boolean mInWord = false; |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 5290 | // Difference between touch position and word boundary position. |
| 5291 | private float mTouchWordDelta; |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 5292 | // X value of the previous updatePosition call. |
| 5293 | private float mPrevX; |
| 5294 | // Indicates if the handle has moved a boundary between LTR and RTL text. |
| 5295 | private boolean mLanguageDirectionChanged = false; |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 5296 | // Distance from edge of horizontally scrolling text view |
| 5297 | // to use to switch to character mode. |
| 5298 | private final float mTextViewEdgeSlop; |
| 5299 | // Used to save text view location. |
| 5300 | private final int[] mTextViewLocation = new int[2]; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5301 | |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5302 | public SelectionHandleView(Drawable drawableLtr, Drawable drawableRtl, int id, |
| 5303 | @HandleType int handleType) { |
| 5304 | super(drawableLtr, drawableRtl, id); |
| 5305 | mHandleType = handleType; |
| 5306 | ViewConfiguration viewConfiguration = ViewConfiguration.get(mTextView.getContext()); |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 5307 | mTextViewEdgeSlop = viewConfiguration.getScaledTouchSlop() * 4; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5308 | } |
| 5309 | |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5310 | private boolean isStartHandle() { |
| 5311 | return mHandleType == HANDLE_TYPE_SELECTION_START; |
| 5312 | } |
| 5313 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5314 | @Override |
| 5315 | protected int getHotspotX(Drawable drawable, boolean isRtlRun) { |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5316 | if (isRtlRun == isStartHandle()) { |
Mady Mellor | 709386f | 2015-05-14 12:41:18 -0700 | [diff] [blame] | 5317 | return drawable.getIntrinsicWidth() / 4; |
| 5318 | } else { |
| 5319 | return (drawable.getIntrinsicWidth() * 3) / 4; |
| 5320 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5321 | } |
| 5322 | |
| 5323 | @Override |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 5324 | protected int getHorizontalGravity(boolean isRtlRun) { |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5325 | return (isRtlRun == isStartHandle()) ? Gravity.LEFT : Gravity.RIGHT; |
Adam Powell | 3fceabd | 2014-08-19 18:28:04 -0700 | [diff] [blame] | 5326 | } |
| 5327 | |
| 5328 | @Override |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5329 | public int getCurrentCursorOffset() { |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5330 | return isStartHandle() ? mTextView.getSelectionStart() : mTextView.getSelectionEnd(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5331 | } |
| 5332 | |
| 5333 | @Override |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5334 | protected void updateSelection(int offset) { |
| 5335 | if (isStartHandle()) { |
| 5336 | Selection.setSelection((Spannable) mTextView.getText(), offset, |
| 5337 | mTextView.getSelectionEnd()); |
| 5338 | } else { |
| 5339 | Selection.setSelection((Spannable) mTextView.getText(), |
| 5340 | mTextView.getSelectionStart(), offset); |
| 5341 | } |
Mihai Popa | 6315a32 | 2018-10-17 17:39:57 +0100 | [diff] [blame] | 5342 | updateDrawable(false /* updateDrawableWhenDragging */); |
Clara Bayarri | 7938cdb | 2015-06-02 20:03:45 +0100 | [diff] [blame] | 5343 | if (mTextActionMode != null) { |
Abodunrinwa Toki | 66c1627 | 2017-05-03 20:22:55 +0100 | [diff] [blame] | 5344 | invalidateActionMode(); |
Clara Bayarri | 13152d1 | 2015-04-09 12:02:04 +0100 | [diff] [blame] | 5345 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5346 | } |
| 5347 | |
| 5348 | @Override |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 5349 | protected void updatePosition(float x, float y, boolean fromTouchScreen) { |
Mady Mellor | 81fa3e8 | 2015-05-14 09:17:41 -0700 | [diff] [blame] | 5350 | final Layout layout = mTextView.getLayout(); |
Mady Mellor | cc65c37 | 2015-06-17 09:25:19 -0700 | [diff] [blame] | 5351 | if (layout == null) { |
| 5352 | // HandleView will deal appropriately in positionAtCursorOffset when |
| 5353 | // layout is null. |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 5354 | positionAndAdjustForCrossingHandles(mTextView.getOffsetForPosition(x, y), |
| 5355 | fromTouchScreen); |
Mady Mellor | cc65c37 | 2015-06-17 09:25:19 -0700 | [diff] [blame] | 5356 | return; |
| 5357 | } |
| 5358 | |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 5359 | if (mPreviousLineTouched == UNSET_LINE) { |
| 5360 | mPreviousLineTouched = mTextView.getLineAtCoordinate(y); |
| 5361 | } |
| 5362 | |
Mady Mellor | b9bbbb1 | 2015-03-23 11:50:46 -0700 | [diff] [blame] | 5363 | boolean positionCursor = false; |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5364 | final int anotherHandleOffset = |
| 5365 | isStartHandle() ? mTextView.getSelectionEnd() : mTextView.getSelectionStart(); |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 5366 | int currLine = getCurrentLineAdjustedForSlop(layout, mPreviousLineTouched, y); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5367 | int initialOffset = getOffsetAtCoordinate(layout, currLine, x); |
Mady Mellor | 81fa3e8 | 2015-05-14 09:17:41 -0700 | [diff] [blame] | 5368 | |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5369 | if (isStartHandle() && initialOffset >= anotherHandleOffset |
| 5370 | || !isStartHandle() && initialOffset <= anotherHandleOffset) { |
| 5371 | // Handles have crossed, bound it to the first selected line and |
Mady Mellor | 81fa3e8 | 2015-05-14 09:17:41 -0700 | [diff] [blame] | 5372 | // adjust by word / char as normal. |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 5373 | currLine = layout.getLineForOffset(anotherHandleOffset); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5374 | initialOffset = getOffsetAtCoordinate(layout, currLine, x); |
Mady Mellor | 81fa3e8 | 2015-05-14 09:17:41 -0700 | [diff] [blame] | 5375 | } |
| 5376 | |
| 5377 | int offset = initialOffset; |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5378 | final int wordEnd = getWordEnd(offset); |
| 5379 | final int wordStart = getWordStart(offset); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5380 | |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 5381 | if (mPrevX == UNSET_X_VALUE) { |
| 5382 | mPrevX = x; |
| 5383 | } |
| 5384 | |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5385 | final int currentOffset = getCurrentCursorOffset(); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5386 | final boolean rtlAtCurrentOffset = isAtRtlRun(layout, currentOffset); |
| 5387 | final boolean atRtl = isAtRtlRun(layout, offset); |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 5388 | final boolean isLvlBoundary = layout.isLevelBoundary(offset); |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 5389 | |
| 5390 | // We can't determine if the user is expanding or shrinking the selection if they're |
| 5391 | // on a bi-di boundary, so until they've moved past the boundary we'll just place |
| 5392 | // the cursor at the current position. |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5393 | if (isLvlBoundary || (rtlAtCurrentOffset && !atRtl) || (!rtlAtCurrentOffset && atRtl)) { |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 5394 | // We're on a boundary or this is the first direction change -- just update |
| 5395 | // to the current position. |
| 5396 | mLanguageDirectionChanged = true; |
| 5397 | mTouchWordDelta = 0.0f; |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 5398 | positionAndAdjustForCrossingHandles(offset, fromTouchScreen); |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 5399 | return; |
| 5400 | } else if (mLanguageDirectionChanged && !isLvlBoundary) { |
| 5401 | // We've just moved past the boundary so update the position. After this we can |
| 5402 | // figure out if the user is expanding or shrinking to go by word or character. |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 5403 | positionAndAdjustForCrossingHandles(offset, fromTouchScreen); |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 5404 | mTouchWordDelta = 0.0f; |
| 5405 | mLanguageDirectionChanged = false; |
| 5406 | return; |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5407 | } |
| 5408 | |
| 5409 | boolean isExpanding; |
| 5410 | final float xDiff = x - mPrevX; |
Keisuke Kuroyanagi | 2645414 | 2015-12-02 15:04:57 -0800 | [diff] [blame] | 5411 | if (isStartHandle()) { |
| 5412 | isExpanding = currLine < mPreviousLineTouched; |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 5413 | } else { |
Keisuke Kuroyanagi | 2645414 | 2015-12-02 15:04:57 -0800 | [diff] [blame] | 5414 | isExpanding = currLine > mPreviousLineTouched; |
| 5415 | } |
| 5416 | if (atRtl == isStartHandle()) { |
| 5417 | isExpanding |= xDiff > 0; |
| 5418 | } else { |
| 5419 | isExpanding |= xDiff < 0; |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 5420 | } |
| 5421 | |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 5422 | if (mTextView.getHorizontallyScrolling()) { |
| 5423 | if (positionNearEdgeOfScrollingView(x, atRtl) |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5424 | && ((isStartHandle() && mTextView.getScrollX() != 0) |
| 5425 | || (!isStartHandle() |
| 5426 | && mTextView.canScrollHorizontally(atRtl ? -1 : 1))) |
| 5427 | && ((isExpanding && ((isStartHandle() && offset < currentOffset) |
| 5428 | || (!isStartHandle() && offset > currentOffset))) |
| 5429 | || !isExpanding)) { |
| 5430 | // If we're expanding ensure that the offset is actually expanding compared to |
| 5431 | // 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] | 5432 | // may be out of sync and we don't want the selection to jump back. |
| 5433 | mTouchWordDelta = 0.0f; |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5434 | final int nextOffset = (atRtl == isStartHandle()) |
| 5435 | ? layout.getOffsetToRightOf(mPreviousOffset) |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 5436 | : layout.getOffsetToLeftOf(mPreviousOffset); |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 5437 | positionAndAdjustForCrossingHandles(nextOffset, fromTouchScreen); |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 5438 | return; |
| 5439 | } |
| 5440 | } |
| 5441 | |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 5442 | if (isExpanding) { |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5443 | // User is increasing the selection. |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5444 | int wordBoundary = isStartHandle() ? wordStart : wordEnd; |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 5445 | final boolean snapToWord = (!mInWord |
| 5446 | || (isStartHandle() ? currLine < mPrevLine : currLine > mPrevLine)) |
| 5447 | && atRtl == isAtRtlRun(layout, wordBoundary); |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5448 | if (snapToWord) { |
Mady Mellor | a526683 | 2015-06-26 14:28:12 -0700 | [diff] [blame] | 5449 | // Sometimes words can be broken across lines (Chinese, hyphenation). |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5450 | // 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] | 5451 | // current line to determine if the user is far enough into the word to snap. |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 5452 | if (layout.getLineForOffset(wordBoundary) != currLine) { |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 5453 | wordBoundary = isStartHandle() |
| 5454 | ? layout.getLineStart(currLine) : layout.getLineEnd(currLine); |
Mady Mellor | a526683 | 2015-06-26 14:28:12 -0700 | [diff] [blame] | 5455 | } |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5456 | final int offsetThresholdToSnap = isStartHandle() |
| 5457 | ? wordEnd - ((wordEnd - wordBoundary) / 2) |
| 5458 | : wordStart + ((wordBoundary - wordStart) / 2); |
| 5459 | if (isStartHandle() |
| 5460 | && (offset <= offsetThresholdToSnap || currLine < mPrevLine)) { |
| 5461 | // User is far enough into the word or on a different line so we expand by |
| 5462 | // word. |
| 5463 | offset = wordStart; |
| 5464 | } else if (!isStartHandle() |
| 5465 | && (offset >= offsetThresholdToSnap || currLine > mPrevLine)) { |
| 5466 | // User is far enough into the word or on a different line so we expand by |
| 5467 | // word. |
| 5468 | offset = wordEnd; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5469 | } else { |
Mady Mellor | c2225b9 | 2015-04-01 15:59:20 -0700 | [diff] [blame] | 5470 | offset = mPreviousOffset; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5471 | } |
| 5472 | } |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5473 | if ((isStartHandle() && offset < initialOffset) |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5474 | || (!isStartHandle() && offset > initialOffset)) { |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5475 | final float adjustedX = getHorizontal(layout, offset); |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 5476 | mTouchWordDelta = |
| 5477 | mTextView.convertToLocalHorizontalCoordinate(x) - adjustedX; |
Keisuke Kuroyanagi | 50a927c | 2015-05-07 17:34:21 +0900 | [diff] [blame] | 5478 | } else { |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 5479 | mTouchWordDelta = 0.0f; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5480 | } |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5481 | positionCursor = true; |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 5482 | } else { |
| 5483 | final int adjustedOffset = |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5484 | getOffsetAtCoordinate(layout, currLine, x - mTouchWordDelta); |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5485 | final boolean shrinking = isStartHandle() |
| 5486 | ? adjustedOffset > mPreviousOffset || currLine > mPrevLine |
| 5487 | : adjustedOffset < mPreviousOffset || currLine < mPrevLine; |
| 5488 | if (shrinking) { |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 5489 | // User is shrinking the selection. |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5490 | if (currLine != mPrevLine) { |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 5491 | // 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] | 5492 | offset = isStartHandle() ? wordStart : wordEnd; |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5493 | if ((isStartHandle() && offset < initialOffset) |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5494 | || (!isStartHandle() && offset > initialOffset)) { |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5495 | final float adjustedX = getHorizontal(layout, offset); |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 5496 | mTouchWordDelta = |
| 5497 | mTextView.convertToLocalHorizontalCoordinate(x) - adjustedX; |
| 5498 | } else { |
| 5499 | mTouchWordDelta = 0.0f; |
| 5500 | } |
| 5501 | } else { |
| 5502 | offset = adjustedOffset; |
| 5503 | } |
| 5504 | positionCursor = true; |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5505 | } else if ((isStartHandle() && adjustedOffset < mPreviousOffset) |
| 5506 | || (!isStartHandle() && adjustedOffset > mPreviousOffset)) { |
| 5507 | // Handle has jumped to the word boundary, and the user is moving |
Mady Mellor | 43fd2f4 | 2015-06-08 14:03:34 -0700 | [diff] [blame] | 5508 | // their finger towards the handle, the delta should be updated. |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 5509 | mTouchWordDelta = mTextView.convertToLocalHorizontalCoordinate(x) |
| 5510 | - getHorizontal(layout, mPreviousOffset); |
Keisuke Kuroyanagi | 0138e4c | 2015-05-12 12:51:26 +0900 | [diff] [blame] | 5511 | } |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5512 | } |
| 5513 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5514 | if (positionCursor) { |
Mady Mellor | a6a0f78 | 2015-07-10 16:43:32 -0700 | [diff] [blame] | 5515 | mPreviousLineTouched = currLine; |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 5516 | positionAndAdjustForCrossingHandles(offset, fromTouchScreen); |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5517 | } |
Mady Mellor | e264ac3 | 2015-06-22 16:46:29 -0700 | [diff] [blame] | 5518 | mPrevX = x; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5519 | } |
| 5520 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5521 | @Override |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 5522 | protected void positionAtCursorOffset(int offset, boolean forceUpdatePosition, |
| 5523 | boolean fromTouchScreen) { |
| 5524 | super.positionAtCursorOffset(offset, forceUpdatePosition, fromTouchScreen); |
Yoshiki Iguchi | 9582e15 | 2015-10-15 13:34:41 +0900 | [diff] [blame] | 5525 | mInWord = (offset != -1) && !getWordIteratorWithText().isBoundary(offset); |
Mady Mellor | 36d5a7b | 2015-05-22 10:31:12 -0700 | [diff] [blame] | 5526 | } |
| 5527 | |
| 5528 | @Override |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5529 | public boolean onTouchEvent(MotionEvent event) { |
| 5530 | boolean superResult = super.onTouchEvent(event); |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 5531 | |
| 5532 | switch (event.getActionMasked()) { |
| 5533 | case MotionEvent.ACTION_DOWN: |
| 5534 | // Reset the touch word offset and x value when the user |
| 5535 | // re-engages the handle. |
| 5536 | mTouchWordDelta = 0.0f; |
| 5537 | mPrevX = UNSET_X_VALUE; |
Mihai Popa | e301746 | 2018-03-07 12:25:21 +0000 | [diff] [blame] | 5538 | updateMagnifier(event); |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 5539 | break; |
| 5540 | |
| 5541 | case MotionEvent.ACTION_MOVE: |
Mihai Popa | e301746 | 2018-03-07 12:25:21 +0000 | [diff] [blame] | 5542 | updateMagnifier(event); |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 5543 | break; |
| 5544 | |
| 5545 | case MotionEvent.ACTION_UP: |
| 5546 | case MotionEvent.ACTION_CANCEL: |
| 5547 | dismissMagnifier(); |
| 5548 | break; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5549 | } |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 5550 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5551 | return superResult; |
| 5552 | } |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 5553 | |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 5554 | private void positionAndAdjustForCrossingHandles(int offset, boolean fromTouchScreen) { |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5555 | final int anotherHandleOffset = |
| 5556 | isStartHandle() ? mTextView.getSelectionEnd() : mTextView.getSelectionStart(); |
| 5557 | if ((isStartHandle() && offset >= anotherHandleOffset) |
| 5558 | || (!isStartHandle() && offset <= anotherHandleOffset)) { |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5559 | mTouchWordDelta = 0.0f; |
| 5560 | final Layout layout = mTextView.getLayout(); |
| 5561 | if (layout != null && offset != anotherHandleOffset) { |
| 5562 | final float horiz = getHorizontal(layout, offset); |
| 5563 | final float anotherHandleHoriz = getHorizontal(layout, anotherHandleOffset, |
| 5564 | !isStartHandle()); |
| 5565 | final float currentHoriz = getHorizontal(layout, mPreviousOffset); |
| 5566 | if (currentHoriz < anotherHandleHoriz && horiz < anotherHandleHoriz |
| 5567 | || currentHoriz > anotherHandleHoriz && horiz > anotherHandleHoriz) { |
| 5568 | // This handle passes another one as it crossed a direction boundary. |
| 5569 | // Don't minimize the selection, but keep the handle at the run boundary. |
| 5570 | final int currentOffset = getCurrentCursorOffset(); |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 5571 | final int offsetToGetRunRange = isStartHandle() |
| 5572 | ? currentOffset : Math.max(currentOffset - 1, 0); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5573 | final long range = layout.getRunRange(offsetToGetRunRange); |
| 5574 | if (isStartHandle()) { |
| 5575 | offset = TextUtils.unpackRangeStartFromLong(range); |
| 5576 | } else { |
| 5577 | offset = TextUtils.unpackRangeEndFromLong(range); |
| 5578 | } |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 5579 | positionAtCursorOffset(offset, false, fromTouchScreen); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5580 | return; |
| 5581 | } |
| 5582 | } |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 5583 | // Handles can not cross and selection is at least one character. |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5584 | offset = getNextCursorOffset(anotherHandleOffset, !isStartHandle()); |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 5585 | } |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 5586 | positionAtCursorOffset(offset, false, fromTouchScreen); |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 5587 | } |
| 5588 | |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 5589 | private boolean positionNearEdgeOfScrollingView(float x, boolean atRtl) { |
| 5590 | mTextView.getLocationOnScreen(mTextViewLocation); |
| 5591 | boolean nearEdge; |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5592 | if (atRtl == isStartHandle()) { |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 5593 | int rightEdge = mTextViewLocation[0] + mTextView.getWidth() |
| 5594 | - mTextView.getPaddingRight(); |
| 5595 | nearEdge = x > rightEdge - mTextViewEdgeSlop; |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5596 | } else { |
| 5597 | int leftEdge = mTextViewLocation[0] + mTextView.getPaddingLeft(); |
| 5598 | nearEdge = x < leftEdge + mTextViewEdgeSlop; |
Mady Mellor | 42390aa | 2015-07-24 13:08:42 -0700 | [diff] [blame] | 5599 | } |
| 5600 | return nearEdge; |
| 5601 | } |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5602 | |
| 5603 | @Override |
| 5604 | protected boolean isAtRtlRun(@NonNull Layout layout, int offset) { |
| 5605 | final int offsetToCheck = isStartHandle() ? offset : Math.max(offset - 1, 0); |
| 5606 | return layout.isRtlCharAt(offsetToCheck); |
| 5607 | } |
| 5608 | |
| 5609 | @Override |
| 5610 | public float getHorizontal(@NonNull Layout layout, int offset) { |
| 5611 | return getHorizontal(layout, offset, isStartHandle()); |
| 5612 | } |
| 5613 | |
| 5614 | private float getHorizontal(@NonNull Layout layout, int offset, boolean startHandle) { |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 5615 | final int line = layout.getLineForOffset(offset); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5616 | final int offsetToCheck = startHandle ? offset : Math.max(offset - 1, 0); |
| 5617 | final boolean isRtlChar = layout.isRtlCharAt(offsetToCheck); |
| 5618 | final boolean isRtlParagraph = layout.getParagraphDirection(line) == -1; |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 5619 | return (isRtlChar == isRtlParagraph) |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 5620 | ? layout.getPrimaryHorizontal(offset) : layout.getSecondaryHorizontal(offset); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5621 | } |
| 5622 | |
| 5623 | @Override |
| 5624 | protected int getOffsetAtCoordinate(@NonNull Layout layout, int line, float x) { |
Keisuke Kuroyanagi | b1b8865 | 2016-04-05 16:26:16 +0900 | [diff] [blame] | 5625 | final float localX = mTextView.convertToLocalHorizontalCoordinate(x); |
| 5626 | final int primaryOffset = layout.getOffsetForHorizontal(line, localX, true); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5627 | if (!layout.isLevelBoundary(primaryOffset)) { |
| 5628 | return primaryOffset; |
| 5629 | } |
Keisuke Kuroyanagi | b1b8865 | 2016-04-05 16:26:16 +0900 | [diff] [blame] | 5630 | final int secondaryOffset = layout.getOffsetForHorizontal(line, localX, false); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5631 | final int currentOffset = getCurrentCursorOffset(); |
| 5632 | final int primaryDiff = Math.abs(primaryOffset - currentOffset); |
| 5633 | final int secondaryDiff = Math.abs(secondaryOffset - currentOffset); |
| 5634 | if (primaryDiff < secondaryDiff) { |
| 5635 | return primaryOffset; |
| 5636 | } else if (primaryDiff > secondaryDiff) { |
| 5637 | return secondaryOffset; |
| 5638 | } else { |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 5639 | final int offsetToCheck = isStartHandle() |
| 5640 | ? currentOffset : Math.max(currentOffset - 1, 0); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 5641 | final boolean isRtlChar = layout.isRtlCharAt(offsetToCheck); |
| 5642 | final boolean isRtlParagraph = layout.getParagraphDirection(line) == -1; |
| 5643 | return isRtlChar == isRtlParagraph ? primaryOffset : secondaryOffset; |
| 5644 | } |
| 5645 | } |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 5646 | |
| 5647 | @MagnifierHandleTrigger |
| 5648 | protected int getMagnifierHandleTrigger() { |
| 5649 | return isStartHandle() |
| 5650 | ? MagnifierHandleTrigger.SELECTION_START |
| 5651 | : MagnifierHandleTrigger.SELECTION_END; |
| 5652 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5653 | } |
| 5654 | |
Mady Mellor | cc65c37 | 2015-06-17 09:25:19 -0700 | [diff] [blame] | 5655 | private int getCurrentLineAdjustedForSlop(Layout layout, int prevLine, float y) { |
Mady Mellor | 8067907 | 2015-07-09 16:05:36 -0700 | [diff] [blame] | 5656 | final int trueLine = mTextView.getLineAtCoordinate(y); |
Mady Mellor | cc65c37 | 2015-06-17 09:25:19 -0700 | [diff] [blame] | 5657 | if (layout == null || prevLine > layout.getLineCount() |
| 5658 | || layout.getLineCount() <= 0 || prevLine < 0) { |
| 5659 | // Invalid parameters, just return whatever line is at y. |
Mady Mellor | 8067907 | 2015-07-09 16:05:36 -0700 | [diff] [blame] | 5660 | return trueLine; |
| 5661 | } |
| 5662 | |
| 5663 | if (Math.abs(trueLine - prevLine) >= 2) { |
| 5664 | // Only stick to lines if we're within a line of the previous selection. |
| 5665 | return trueLine; |
Mady Mellor | cc65c37 | 2015-06-17 09:25:19 -0700 | [diff] [blame] | 5666 | } |
| 5667 | |
| 5668 | final float verticalOffset = mTextView.viewportToContentVerticalOffset(); |
| 5669 | final int lineCount = layout.getLineCount(); |
| 5670 | final float slop = mTextView.getLineHeight() * LINE_SLOP_MULTIPLIER_FOR_HANDLEVIEWS; |
| 5671 | |
| 5672 | final float firstLineTop = layout.getLineTop(0) + verticalOffset; |
| 5673 | final float prevLineTop = layout.getLineTop(prevLine) + verticalOffset; |
| 5674 | final float yTopBound = Math.max(prevLineTop - slop, firstLineTop + slop); |
| 5675 | |
| 5676 | final float lastLineBottom = layout.getLineBottom(lineCount - 1) + verticalOffset; |
| 5677 | final float prevLineBottom = layout.getLineBottom(prevLine) + verticalOffset; |
| 5678 | final float yBottomBound = Math.min(prevLineBottom + slop, lastLineBottom - slop); |
| 5679 | |
| 5680 | // Determine if we've moved lines based on y position and previous line. |
| 5681 | int currLine; |
| 5682 | if (y <= yTopBound) { |
| 5683 | currLine = Math.max(prevLine - 1, 0); |
| 5684 | } else if (y >= yBottomBound) { |
| 5685 | currLine = Math.min(prevLine + 1, lineCount - 1); |
| 5686 | } else { |
| 5687 | currLine = prevLine; |
| 5688 | } |
| 5689 | return currLine; |
| 5690 | } |
| 5691 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5692 | /** |
| 5693 | * A CursorController instance can be used to control a cursor in the text. |
| 5694 | */ |
| 5695 | private interface CursorController extends ViewTreeObserver.OnTouchModeChangeListener { |
| 5696 | /** |
| 5697 | * Makes the cursor controller visible on screen. |
| 5698 | * See also {@link #hide()}. |
| 5699 | */ |
| 5700 | public void show(); |
| 5701 | |
| 5702 | /** |
| 5703 | * Hide the cursor controller from screen. |
| 5704 | * See also {@link #show()}. |
| 5705 | */ |
| 5706 | public void hide(); |
| 5707 | |
| 5708 | /** |
| 5709 | * Called when the view is detached from window. Perform house keeping task, such as |
| 5710 | * stopping Runnable thread that would otherwise keep a reference on the context, thus |
| 5711 | * preventing the activity from being recycled. |
| 5712 | */ |
| 5713 | public void onDetached(); |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 5714 | |
| 5715 | public boolean isCursorBeingModified(); |
| 5716 | |
| 5717 | public boolean isActive(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5718 | } |
| 5719 | |
Mihai Popa | 6c7ad1d | 2018-12-04 15:45:00 +0000 | [diff] [blame] | 5720 | void loadCursorDrawable() { |
| 5721 | if (mDrawableForCursor == null) { |
| 5722 | mDrawableForCursor = mTextView.getTextCursorDrawable(); |
| 5723 | } |
| 5724 | } |
| 5725 | |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 5726 | class InsertionPointCursorController implements CursorController { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5727 | private InsertionHandleView mHandle; |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 5728 | private boolean mIsDraggingCursor; |
| 5729 | |
| 5730 | public void onTouchEvent(MotionEvent event) { |
| 5731 | switch (event.getActionMasked()) { |
| 5732 | case MotionEvent.ACTION_DOWN: |
| 5733 | mIsDraggingCursor = false; |
| 5734 | break; |
| 5735 | case MotionEvent.ACTION_MOVE: |
| 5736 | if (mIsDraggingCursor) { |
| 5737 | performCursorDrag(event); |
| 5738 | } else if (FLAG_ENABLE_CURSOR_DRAG |
| 5739 | && mTextView.getLayout() != null |
| 5740 | && mTextView.isFocused() |
| 5741 | && mTouchState.isMovedEnoughForDrag()) { |
| 5742 | startCursorDrag(event); |
| 5743 | } |
| 5744 | break; |
| 5745 | case MotionEvent.ACTION_UP: |
| 5746 | case MotionEvent.ACTION_CANCEL: |
| 5747 | if (mIsDraggingCursor) { |
| 5748 | endCursorDrag(event); |
| 5749 | } |
| 5750 | break; |
| 5751 | } |
| 5752 | } |
| 5753 | |
| 5754 | private void positionCursorDuringDrag(MotionEvent event) { |
| 5755 | int line = mTextView.getLineAtCoordinate(event.getY()); |
| 5756 | int offset = mTextView.getOffsetAtCoordinate(line, event.getX()); |
| 5757 | int oldSelectionStart = mTextView.getSelectionStart(); |
| 5758 | int oldSelectionEnd = mTextView.getSelectionEnd(); |
| 5759 | if (offset == oldSelectionStart && offset == oldSelectionEnd) { |
| 5760 | return; |
| 5761 | } |
| 5762 | Selection.setSelection((Spannable) mTextView.getText(), offset); |
| 5763 | updateCursorPosition(); |
| 5764 | if (mHapticTextHandleEnabled) { |
| 5765 | mTextView.performHapticFeedback(HapticFeedbackConstants.TEXT_HANDLE_MOVE); |
| 5766 | } |
| 5767 | } |
| 5768 | |
| 5769 | private void startCursorDrag(MotionEvent event) { |
| 5770 | if (TextView.DEBUG_CURSOR) { |
| 5771 | logCursor("InsertionPointCursorController", "start cursor drag"); |
| 5772 | } |
| 5773 | mIsDraggingCursor = true; |
| 5774 | // We don't want the parent scroll/long-press handlers to take over while dragging. |
| 5775 | mTextView.getParent().requestDisallowInterceptTouchEvent(true); |
| 5776 | mTextView.cancelLongPress(); |
| 5777 | // Update the cursor position. |
| 5778 | positionCursorDuringDrag(event); |
| 5779 | // Show the cursor handle and magnifier. |
| 5780 | show(); |
| 5781 | getHandle().removeHiderCallback(); |
| 5782 | getHandle().updateMagnifier(event); |
| 5783 | // TODO(b/146555651): Figure out if suspendBlink() should be called here. |
| 5784 | } |
| 5785 | |
| 5786 | private void performCursorDrag(MotionEvent event) { |
| 5787 | positionCursorDuringDrag(event); |
| 5788 | getHandle().updateMagnifier(event); |
| 5789 | } |
| 5790 | |
| 5791 | private void endCursorDrag(MotionEvent event) { |
| 5792 | if (TextView.DEBUG_CURSOR) { |
| 5793 | logCursor("InsertionPointCursorController", "end cursor drag"); |
| 5794 | } |
| 5795 | mIsDraggingCursor = false; |
| 5796 | // Hide the magnifier and set the handle to be hidden after a delay. |
| 5797 | getHandle().dismissMagnifier(); |
| 5798 | getHandle().hideAfterDelay(); |
| 5799 | // We're no longer dragging, so let the parent receive events. |
| 5800 | mTextView.getParent().requestDisallowInterceptTouchEvent(false); |
| 5801 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5802 | |
| 5803 | public void show() { |
| 5804 | getHandle().show(); |
Andrei Stingaceanu | 35c550c | 2015-05-07 16:49:49 +0100 | [diff] [blame] | 5805 | |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 5806 | final long durationSinceCutOrCopy = |
| 5807 | SystemClock.uptimeMillis() - TextView.sLastCutCopyOrTextChangedTime; |
| 5808 | |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 5809 | if (mInsertionActionModeRunnable != null) { |
| 5810 | if (mIsDraggingCursor |
| 5811 | || mTouchState.isMultiTap() |
| 5812 | || isCursorInsideEasyCorrectionSpan()) { |
| 5813 | // Cancel the runnable for showing the floating toolbar. |
| 5814 | mTextView.removeCallbacks(mInsertionActionModeRunnable); |
| 5815 | } |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 5816 | } |
| 5817 | |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 5818 | // If the user recently performed a Cut or Copy action, we want to show the floating |
| 5819 | // toolbar even for a single tap. |
| 5820 | if (!mIsDraggingCursor |
| 5821 | && !mTouchState.isMultiTap() |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 5822 | && !isCursorInsideEasyCorrectionSpan() |
| 5823 | && (durationSinceCutOrCopy < RECENT_CUT_COPY_DURATION_MS)) { |
| 5824 | if (mTextActionMode == null) { |
| 5825 | if (mInsertionActionModeRunnable == null) { |
| 5826 | mInsertionActionModeRunnable = new Runnable() { |
| 5827 | @Override |
| 5828 | public void run() { |
| 5829 | startInsertionActionMode(); |
| 5830 | } |
| 5831 | }; |
| 5832 | } |
| 5833 | mTextView.postDelayed( |
| 5834 | mInsertionActionModeRunnable, |
| 5835 | ViewConfiguration.getDoubleTapTimeout() + 1); |
| 5836 | } |
| 5837 | } |
| 5838 | |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 5839 | if (!mIsDraggingCursor) { |
| 5840 | getHandle().hideAfterDelay(); |
| 5841 | } |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 5842 | |
Andrei Stingaceanu | 35c550c | 2015-05-07 16:49:49 +0100 | [diff] [blame] | 5843 | if (mSelectionModifierCursorController != null) { |
| 5844 | mSelectionModifierCursorController.hide(); |
| 5845 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5846 | } |
| 5847 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5848 | public void hide() { |
| 5849 | if (mHandle != null) { |
| 5850 | mHandle.hide(); |
| 5851 | } |
| 5852 | } |
| 5853 | |
| 5854 | public void onTouchModeChanged(boolean isInTouchMode) { |
| 5855 | if (!isInTouchMode) { |
| 5856 | hide(); |
| 5857 | } |
| 5858 | } |
| 5859 | |
| 5860 | private InsertionHandleView getHandle() { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5861 | if (mHandle == null) { |
Mihai Popa | 6315a32 | 2018-10-17 17:39:57 +0100 | [diff] [blame] | 5862 | loadHandleDrawables(false /* overwrite */); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5863 | mHandle = new InsertionHandleView(mSelectHandleCenter); |
| 5864 | } |
| 5865 | return mHandle; |
| 5866 | } |
| 5867 | |
Mihai Popa | 6315a32 | 2018-10-17 17:39:57 +0100 | [diff] [blame] | 5868 | private void reloadHandleDrawable() { |
| 5869 | if (mHandle == null) { |
| 5870 | // No need to reload, the potentially new drawable will |
| 5871 | // be used when the handle is created. |
| 5872 | return; |
| 5873 | } |
| 5874 | mHandle.setDrawables(mSelectHandleCenter, mSelectHandleCenter); |
| 5875 | } |
| 5876 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5877 | @Override |
| 5878 | public void onDetached() { |
| 5879 | final ViewTreeObserver observer = mTextView.getViewTreeObserver(); |
| 5880 | observer.removeOnTouchModeChangeListener(this); |
| 5881 | |
| 5882 | if (mHandle != null) mHandle.onDetached(); |
| 5883 | } |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 5884 | |
| 5885 | @Override |
| 5886 | public boolean isCursorBeingModified() { |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 5887 | return mIsDraggingCursor || (mHandle != null && mHandle.isDragging()); |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 5888 | } |
| 5889 | |
| 5890 | @Override |
| 5891 | public boolean isActive() { |
| 5892 | return mHandle != null && mHandle.isShowing(); |
| 5893 | } |
Keisuke Kuroyanagi | c14e127 | 2016-04-05 15:39:24 +0900 | [diff] [blame] | 5894 | |
| 5895 | public void invalidateHandle() { |
| 5896 | if (mHandle != null) { |
| 5897 | mHandle.invalidate(); |
| 5898 | } |
| 5899 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5900 | } |
| 5901 | |
| 5902 | class SelectionModifierCursorController implements CursorController { |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5903 | // The cursor controller handles, lazily created when shown. |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5904 | private SelectionHandleView mStartHandle; |
| 5905 | private SelectionHandleView mEndHandle; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5906 | // The offsets of that last touch down event. Remembered to start selection there. |
| 5907 | private int mMinTouchOffset, mMaxTouchOffset; |
| 5908 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5909 | private boolean mGestureStayedInTapRegion; |
| 5910 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5911 | // Where the user first starts the drag motion. |
| 5912 | private int mStartOffset = -1; |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5913 | |
Mady Mellor | 7a93644 | 2015-05-20 10:05:52 -0700 | [diff] [blame] | 5914 | private boolean mHaventMovedEnoughToStartDrag; |
Mady Mellor | f9f8aeb | 2015-06-17 09:46:01 -0700 | [diff] [blame] | 5915 | // The line that a selection happened most recently with the drag accelerator. |
| 5916 | private int mLineSelectionIsOn = -1; |
| 5917 | // Whether the drag accelerator has selected past the initial line. |
| 5918 | private boolean mSwitchedLines = false; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5919 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5920 | // Indicates the drag accelerator mode that the user is currently using. |
| 5921 | private int mDragAcceleratorMode = DRAG_ACCELERATOR_MODE_INACTIVE; |
| 5922 | // Drag accelerator is inactive. |
| 5923 | private static final int DRAG_ACCELERATOR_MODE_INACTIVE = 0; |
| 5924 | // Character based selection by dragging. Only for mouse. |
| 5925 | private static final int DRAG_ACCELERATOR_MODE_CHARACTER = 1; |
| 5926 | // Word based selection by dragging. Enabled after long pressing or double tapping. |
| 5927 | private static final int DRAG_ACCELERATOR_MODE_WORD = 2; |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 5928 | // Paragraph based selection by dragging. Enabled after mouse triple click. |
| 5929 | private static final int DRAG_ACCELERATOR_MODE_PARAGRAPH = 3; |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5930 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5931 | SelectionModifierCursorController() { |
| 5932 | resetTouchOffsets(); |
| 5933 | } |
| 5934 | |
| 5935 | public void show() { |
| 5936 | if (mTextView.isInBatchEditMode()) { |
| 5937 | return; |
| 5938 | } |
Mihai Popa | 6315a32 | 2018-10-17 17:39:57 +0100 | [diff] [blame] | 5939 | loadHandleDrawables(false /* overwrite */); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5940 | initHandles(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5941 | } |
| 5942 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5943 | private void initHandles() { |
| 5944 | // Lazy object creation has to be done before updatePosition() is called. |
| 5945 | if (mStartHandle == null) { |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5946 | mStartHandle = new SelectionHandleView(mSelectHandleLeft, mSelectHandleRight, |
| 5947 | com.android.internal.R.id.selection_start_handle, |
| 5948 | HANDLE_TYPE_SELECTION_START); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5949 | } |
| 5950 | if (mEndHandle == null) { |
Keisuke Kuroyanagi | 5d7657e | 2015-11-26 13:59:05 +0900 | [diff] [blame] | 5951 | mEndHandle = new SelectionHandleView(mSelectHandleRight, mSelectHandleLeft, |
| 5952 | com.android.internal.R.id.selection_end_handle, |
| 5953 | HANDLE_TYPE_SELECTION_END); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5954 | } |
| 5955 | |
| 5956 | mStartHandle.show(); |
| 5957 | mEndHandle.show(); |
| 5958 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5959 | hideInsertionPointCursorController(); |
| 5960 | } |
| 5961 | |
Mihai Popa | 6315a32 | 2018-10-17 17:39:57 +0100 | [diff] [blame] | 5962 | private void reloadHandleDrawables() { |
| 5963 | if (mStartHandle == null) { |
| 5964 | // No need to reload, the potentially new drawables will |
| 5965 | // be used when the handles are created. |
| 5966 | return; |
| 5967 | } |
| 5968 | mStartHandle.setDrawables(mSelectHandleLeft, mSelectHandleRight); |
| 5969 | mEndHandle.setDrawables(mSelectHandleRight, mSelectHandleLeft); |
| 5970 | } |
| 5971 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 5972 | public void hide() { |
| 5973 | if (mStartHandle != null) mStartHandle.hide(); |
| 5974 | if (mEndHandle != null) mEndHandle.hide(); |
| 5975 | } |
| 5976 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5977 | public void enterDrag(int dragAcceleratorMode) { |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 5978 | if (TextView.DEBUG_CURSOR) { |
| 5979 | logCursor("SelectionModifierCursorController: enterDrag", |
| 5980 | "starting selection drag: mode=%s", dragAcceleratorMode); |
| 5981 | } |
| 5982 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5983 | // Just need to init the handles / hide insertion cursor. |
| 5984 | show(); |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5985 | mDragAcceleratorMode = dragAcceleratorMode; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5986 | // Start location of selection. |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 5987 | mStartOffset = mTextView.getOffsetForPosition(mTouchState.getLastDownX(), |
| 5988 | mTouchState.getLastDownY()); |
| 5989 | mLineSelectionIsOn = mTextView.getLineAtCoordinate(mTouchState.getLastDownY()); |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5990 | // Don't show the handles until user has lifted finger. |
| 5991 | hide(); |
| 5992 | |
| 5993 | // This stops scrolling parents from intercepting the touch event, allowing |
| 5994 | // the user to continue dragging across the screen to select text; TextView will |
| 5995 | // scroll as necessary. |
| 5996 | mTextView.getParent().requestDisallowInterceptTouchEvent(true); |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 5997 | mTextView.cancelLongPress(); |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 5998 | } |
| 5999 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6000 | public void onTouchEvent(MotionEvent event) { |
| 6001 | // This is done even when the View does not have focus, so that long presses can start |
| 6002 | // selection and tap can move cursor from this tap position. |
Mady Mellor | 7a93644 | 2015-05-20 10:05:52 -0700 | [diff] [blame] | 6003 | final float eventX = event.getX(); |
| 6004 | final float eventY = event.getY(); |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6005 | final boolean isMouse = event.isFromSource(InputDevice.SOURCE_MOUSE); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6006 | switch (event.getActionMasked()) { |
| 6007 | case MotionEvent.ACTION_DOWN: |
Andrei Stingaceanu | 83830727 | 2015-06-19 17:58:47 +0100 | [diff] [blame] | 6008 | if (extractedTextModeWillBeStarted()) { |
| 6009 | // Prevent duplicating the selection handles until the mode starts. |
| 6010 | hide(); |
| 6011 | } else { |
| 6012 | // Remember finger down position, to be able to start selection from there. |
| 6013 | mMinTouchOffset = mMaxTouchOffset = mTextView.getOffsetForPosition( |
| 6014 | eventX, eventY); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6015 | |
Andrei Stingaceanu | 83830727 | 2015-06-19 17:58:47 +0100 | [diff] [blame] | 6016 | // Double tap detection |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 6017 | if (mGestureStayedInTapRegion |
| 6018 | && mTouchState.isMultiTapInSameArea() |
| 6019 | && (isMouse || isPositionOnText(eventX, eventY))) { |
| 6020 | if (TextView.DEBUG_CURSOR) { |
| 6021 | logCursor("SelectionModifierCursorController: onTouchEvent", |
| 6022 | "ACTION_DOWN: select and start drag"); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6023 | } |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 6024 | if (mTouchState.isDoubleTap()) { |
| 6025 | selectCurrentWordAndStartDrag(); |
| 6026 | } else if (mTouchState.isTripleClick()) { |
| 6027 | selectCurrentParagraphAndStartDrag(); |
| 6028 | } |
| 6029 | mDiscardNextActionUp = true; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6030 | } |
Andrei Stingaceanu | 83830727 | 2015-06-19 17:58:47 +0100 | [diff] [blame] | 6031 | mGestureStayedInTapRegion = true; |
| 6032 | mHaventMovedEnoughToStartDrag = true; |
| 6033 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6034 | break; |
| 6035 | |
| 6036 | case MotionEvent.ACTION_POINTER_DOWN: |
| 6037 | case MotionEvent.ACTION_POINTER_UP: |
| 6038 | // Handle multi-point gestures. Keep min and max offset positions. |
| 6039 | // Only activated for devices that correctly handle multi-touch. |
| 6040 | if (mTextView.getContext().getPackageManager().hasSystemFeature( |
| 6041 | PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT)) { |
| 6042 | updateMinAndMaxOffsets(event); |
| 6043 | } |
| 6044 | break; |
| 6045 | |
| 6046 | case MotionEvent.ACTION_MOVE: |
Mady Mellor | 7a93644 | 2015-05-20 10:05:52 -0700 | [diff] [blame] | 6047 | final ViewConfiguration viewConfig = ViewConfiguration.get( |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 6048 | mTextView.getContext()); |
| 6049 | |
Mady Mellor | 7a93644 | 2015-05-20 10:05:52 -0700 | [diff] [blame] | 6050 | if (mGestureStayedInTapRegion || mHaventMovedEnoughToStartDrag) { |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 6051 | final float deltaX = eventX - mTouchState.getLastDownX(); |
| 6052 | final float deltaY = eventY - mTouchState.getLastDownY(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6053 | final float distanceSquared = deltaX * deltaX + deltaY * deltaY; |
| 6054 | |
Mady Mellor | 7a93644 | 2015-05-20 10:05:52 -0700 | [diff] [blame] | 6055 | if (mGestureStayedInTapRegion) { |
| 6056 | int doubleTapTouchSlop = viewConfig.getScaledDoubleTapTouchSlop(); |
| 6057 | mGestureStayedInTapRegion = |
| 6058 | distanceSquared <= doubleTapTouchSlop * doubleTapTouchSlop; |
| 6059 | } |
| 6060 | if (mHaventMovedEnoughToStartDrag) { |
| 6061 | // We don't start dragging until the user has moved enough. |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 6062 | int touchSlop = viewConfig.getScaledTouchSlop(); |
Mady Mellor | 7a93644 | 2015-05-20 10:05:52 -0700 | [diff] [blame] | 6063 | mHaventMovedEnoughToStartDrag = |
| 6064 | distanceSquared <= touchSlop * touchSlop; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6065 | } |
| 6066 | } |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 6067 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6068 | if (isMouse && !isDragAcceleratorActive()) { |
| 6069 | final int offset = mTextView.getOffsetForPosition(eventX, eventY); |
Keisuke Kuroyanagi | e876085 | 2015-12-21 18:30:19 +0900 | [diff] [blame] | 6070 | if (mTextView.hasSelection() |
| 6071 | && (!mHaventMovedEnoughToStartDrag || mStartOffset != offset) |
| 6072 | && offset >= mTextView.getSelectionStart() |
| 6073 | && offset <= mTextView.getSelectionEnd()) { |
| 6074 | startDragAndDrop(); |
| 6075 | break; |
| 6076 | } |
| 6077 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6078 | if (mStartOffset != offset) { |
| 6079 | // Start character based drag accelerator. |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 6080 | stopTextActionMode(); |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6081 | enterDrag(DRAG_ACCELERATOR_MODE_CHARACTER); |
| 6082 | mDiscardNextActionUp = true; |
| 6083 | mHaventMovedEnoughToStartDrag = false; |
| 6084 | } |
| 6085 | } |
| 6086 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 6087 | if (mStartHandle != null && mStartHandle.isShowing()) { |
| 6088 | // Don't do the drag if the handles are showing already. |
| 6089 | break; |
| 6090 | } |
| 6091 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6092 | updateSelection(event); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6093 | break; |
| 6094 | |
| 6095 | case MotionEvent.ACTION_UP: |
Nikita Dubrovsky | 05cfcc8 | 2019-10-24 08:57:32 -0700 | [diff] [blame] | 6096 | if (TextView.DEBUG_CURSOR) { |
| 6097 | logCursor("SelectionModifierCursorController: onTouchEvent", "ACTION_UP"); |
| 6098 | } |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6099 | if (!isDragAcceleratorActive()) { |
| 6100 | break; |
| 6101 | } |
| 6102 | updateSelection(event); |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 6103 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6104 | // No longer dragging to select text, let the parent intercept events. |
| 6105 | mTextView.getParent().requestDisallowInterceptTouchEvent(false); |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 6106 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6107 | // No longer the first dragging motion, reset. |
| 6108 | resetDragAcceleratorState(); |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 6109 | |
| 6110 | if (mTextView.hasSelection()) { |
Abodunrinwa Toki | 66c1627 | 2017-05-03 20:22:55 +0100 | [diff] [blame] | 6111 | // Drag selection should not be adjusted by the text classifier. |
| 6112 | startSelectionActionModeAsync(mHaventMovedEnoughToStartDrag); |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 6113 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6114 | break; |
| 6115 | } |
| 6116 | } |
| 6117 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6118 | private void updateSelection(MotionEvent event) { |
| 6119 | if (mTextView.getLayout() != null) { |
| 6120 | switch (mDragAcceleratorMode) { |
| 6121 | case DRAG_ACCELERATOR_MODE_CHARACTER: |
| 6122 | updateCharacterBasedSelection(event); |
| 6123 | break; |
| 6124 | case DRAG_ACCELERATOR_MODE_WORD: |
| 6125 | updateWordBasedSelection(event); |
| 6126 | break; |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 6127 | case DRAG_ACCELERATOR_MODE_PARAGRAPH: |
| 6128 | updateParagraphBasedSelection(event); |
| 6129 | break; |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6130 | } |
| 6131 | } |
| 6132 | } |
| 6133 | |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 6134 | /** |
| 6135 | * If the TextView allows text selection, selects the current paragraph and starts a drag. |
| 6136 | * |
| 6137 | * @return true if the drag was started. |
| 6138 | */ |
| 6139 | private boolean selectCurrentParagraphAndStartDrag() { |
| 6140 | if (mInsertionActionModeRunnable != null) { |
| 6141 | mTextView.removeCallbacks(mInsertionActionModeRunnable); |
| 6142 | } |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 6143 | stopTextActionMode(); |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 6144 | if (!selectCurrentParagraph()) { |
| 6145 | return false; |
| 6146 | } |
| 6147 | enterDrag(SelectionModifierCursorController.DRAG_ACCELERATOR_MODE_PARAGRAPH); |
| 6148 | return true; |
| 6149 | } |
| 6150 | |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6151 | private void updateCharacterBasedSelection(MotionEvent event) { |
| 6152 | final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY()); |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 6153 | updateSelectionInternal(mStartOffset, offset, |
| 6154 | event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)); |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6155 | } |
| 6156 | |
| 6157 | private void updateWordBasedSelection(MotionEvent event) { |
| 6158 | if (mHaventMovedEnoughToStartDrag) { |
| 6159 | return; |
| 6160 | } |
| 6161 | final boolean isMouse = event.isFromSource(InputDevice.SOURCE_MOUSE); |
| 6162 | final ViewConfiguration viewConfig = ViewConfiguration.get( |
| 6163 | mTextView.getContext()); |
| 6164 | final float eventX = event.getX(); |
| 6165 | final float eventY = event.getY(); |
| 6166 | final int currLine; |
| 6167 | if (isMouse) { |
| 6168 | // No need to offset the y coordinate for mouse input. |
| 6169 | currLine = mTextView.getLineAtCoordinate(eventY); |
| 6170 | } else { |
| 6171 | float y = eventY; |
| 6172 | if (mSwitchedLines) { |
| 6173 | // Offset the finger by the same vertical offset as the handles. |
| 6174 | // This improves visibility of the content being selected by |
| 6175 | // shifting the finger below the content, this is applied once |
| 6176 | // the user has switched lines. |
| 6177 | final int touchSlop = viewConfig.getScaledTouchSlop(); |
| 6178 | final float fingerOffset = (mStartHandle != null) |
| 6179 | ? mStartHandle.getIdealVerticalOffset() |
| 6180 | : touchSlop; |
| 6181 | y = eventY - fingerOffset; |
| 6182 | } |
| 6183 | |
| 6184 | currLine = getCurrentLineAdjustedForSlop(mTextView.getLayout(), mLineSelectionIsOn, |
| 6185 | y); |
| 6186 | if (!mSwitchedLines && currLine != mLineSelectionIsOn) { |
| 6187 | // Break early here, we want to offset the finger position from |
| 6188 | // the selection highlight, once the user moved their finger |
| 6189 | // to a different line we should apply the offset and *not* switch |
| 6190 | // lines until recomputing the position with the finger offset. |
| 6191 | mSwitchedLines = true; |
| 6192 | return; |
| 6193 | } |
| 6194 | } |
| 6195 | |
| 6196 | int startOffset; |
| 6197 | int offset = mTextView.getOffsetAtCoordinate(currLine, eventX); |
| 6198 | // Snap to word boundaries. |
| 6199 | if (mStartOffset < offset) { |
| 6200 | // Expanding with end handle. |
| 6201 | offset = getWordEnd(offset); |
| 6202 | startOffset = getWordStart(mStartOffset); |
| 6203 | } else { |
| 6204 | // Expanding with start handle. |
| 6205 | offset = getWordStart(offset); |
| 6206 | startOffset = getWordEnd(mStartOffset); |
Keisuke Kuroyanagi | 133dfc0 | 2016-07-21 18:07:23 +0900 | [diff] [blame] | 6207 | if (startOffset == offset) { |
| 6208 | offset = getNextCursorOffset(offset, false); |
| 6209 | } |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6210 | } |
| 6211 | mLineSelectionIsOn = currLine; |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 6212 | updateSelectionInternal(startOffset, offset, |
| 6213 | event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)); |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6214 | } |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 6215 | |
| 6216 | private void updateParagraphBasedSelection(MotionEvent event) { |
| 6217 | final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY()); |
| 6218 | |
| 6219 | final int start = Math.min(offset, mStartOffset); |
| 6220 | final int end = Math.max(offset, mStartOffset); |
| 6221 | final long paragraphsRange = getParagraphsRange(start, end); |
| 6222 | final int selectionStart = TextUtils.unpackRangeStartFromLong(paragraphsRange); |
| 6223 | final int selectionEnd = TextUtils.unpackRangeEndFromLong(paragraphsRange); |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 6224 | updateSelectionInternal(selectionStart, selectionEnd, |
| 6225 | event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)); |
| 6226 | } |
| 6227 | |
| 6228 | private void updateSelectionInternal(int selectionStart, int selectionEnd, |
| 6229 | boolean fromTouchScreen) { |
| 6230 | final boolean performHapticFeedback = fromTouchScreen && mHapticTextHandleEnabled |
| 6231 | && ((mTextView.getSelectionStart() != selectionStart) |
| 6232 | || (mTextView.getSelectionEnd() != selectionEnd)); |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 6233 | Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd); |
Yohei Yukawa | c9cd9db | 2017-06-19 18:27:34 -0700 | [diff] [blame] | 6234 | if (performHapticFeedback) { |
| 6235 | mTextView.performHapticFeedback(HapticFeedbackConstants.TEXT_HANDLE_MOVE); |
| 6236 | } |
Keisuke Kuroyanagi | 4368d05 | 2015-11-05 18:51:00 +0900 | [diff] [blame] | 6237 | } |
| 6238 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6239 | /** |
| 6240 | * @param event |
| 6241 | */ |
| 6242 | private void updateMinAndMaxOffsets(MotionEvent event) { |
| 6243 | int pointerCount = event.getPointerCount(); |
| 6244 | for (int index = 0; index < pointerCount; index++) { |
| 6245 | int offset = mTextView.getOffsetForPosition(event.getX(index), event.getY(index)); |
| 6246 | if (offset < mMinTouchOffset) mMinTouchOffset = offset; |
| 6247 | if (offset > mMaxTouchOffset) mMaxTouchOffset = offset; |
| 6248 | } |
| 6249 | } |
| 6250 | |
| 6251 | public int getMinTouchOffset() { |
| 6252 | return mMinTouchOffset; |
| 6253 | } |
| 6254 | |
| 6255 | public int getMaxTouchOffset() { |
| 6256 | return mMaxTouchOffset; |
| 6257 | } |
| 6258 | |
| 6259 | public void resetTouchOffsets() { |
| 6260 | mMinTouchOffset = mMaxTouchOffset = -1; |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6261 | resetDragAcceleratorState(); |
| 6262 | } |
| 6263 | |
| 6264 | private void resetDragAcceleratorState() { |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 6265 | mStartOffset = -1; |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6266 | mDragAcceleratorMode = DRAG_ACCELERATOR_MODE_INACTIVE; |
Mady Mellor | f9f8aeb | 2015-06-17 09:46:01 -0700 | [diff] [blame] | 6267 | mSwitchedLines = false; |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 6268 | final int selectionStart = mTextView.getSelectionStart(); |
| 6269 | final int selectionEnd = mTextView.getSelectionEnd(); |
Clara Bayarri | 4e51877 | 2018-03-27 14:25:33 +0100 | [diff] [blame] | 6270 | if (selectionStart < 0 || selectionEnd < 0) { |
| 6271 | Selection.removeSelection((Spannable) mTextView.getText()); |
| 6272 | } else if (selectionStart > selectionEnd) { |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 6273 | Selection.setSelection((Spannable) mTextView.getText(), |
| 6274 | selectionEnd, selectionStart); |
| 6275 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6276 | } |
| 6277 | |
| 6278 | /** |
| 6279 | * @return true iff this controller is currently used to move the selection start. |
| 6280 | */ |
| 6281 | public boolean isSelectionStartDragged() { |
| 6282 | return mStartHandle != null && mStartHandle.isDragging(); |
| 6283 | } |
| 6284 | |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 6285 | @Override |
| 6286 | public boolean isCursorBeingModified() { |
| 6287 | return isDragAcceleratorActive() || isSelectionStartDragged() |
| 6288 | || (mEndHandle != null && mEndHandle.isDragging()); |
| 6289 | } |
| 6290 | |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 6291 | /** |
| 6292 | * @return true if the user is selecting text using the drag accelerator. |
| 6293 | */ |
| 6294 | public boolean isDragAcceleratorActive() { |
Keisuke Kuroyanagi | 97af673 | 2015-12-04 16:56:38 -0800 | [diff] [blame] | 6295 | return mDragAcceleratorMode != DRAG_ACCELERATOR_MODE_INACTIVE; |
Mady Mellor | 2ff2cd8 | 2015-03-02 10:37:01 -0800 | [diff] [blame] | 6296 | } |
| 6297 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6298 | public void onTouchModeChanged(boolean isInTouchMode) { |
| 6299 | if (!isInTouchMode) { |
| 6300 | hide(); |
| 6301 | } |
| 6302 | } |
| 6303 | |
| 6304 | @Override |
| 6305 | public void onDetached() { |
| 6306 | final ViewTreeObserver observer = mTextView.getViewTreeObserver(); |
| 6307 | observer.removeOnTouchModeChangeListener(this); |
| 6308 | |
| 6309 | if (mStartHandle != null) mStartHandle.onDetached(); |
| 6310 | if (mEndHandle != null) mEndHandle.onDetached(); |
| 6311 | } |
Keisuke Kuroyanagi | bec9715 | 2016-02-24 18:40:09 -0800 | [diff] [blame] | 6312 | |
| 6313 | @Override |
| 6314 | public boolean isActive() { |
| 6315 | return mStartHandle != null && mStartHandle.isShowing(); |
| 6316 | } |
Keisuke Kuroyanagi | c14e127 | 2016-04-05 15:39:24 +0900 | [diff] [blame] | 6317 | |
| 6318 | public void invalidateHandles() { |
| 6319 | if (mStartHandle != null) { |
| 6320 | mStartHandle.invalidate(); |
| 6321 | } |
| 6322 | if (mEndHandle != null) { |
| 6323 | mEndHandle.invalidate(); |
| 6324 | } |
| 6325 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6326 | } |
| 6327 | |
Mihai Popa | 6315a32 | 2018-10-17 17:39:57 +0100 | [diff] [blame] | 6328 | /** |
| 6329 | * Loads the insertion and selection handle Drawables from TextView. If the handle |
| 6330 | * drawables are already loaded, do not overwrite them unless the method parameter |
| 6331 | * is set to true. This logic is required to avoid overwriting Drawables assigned |
| 6332 | * to mSelectHandle[Center/Left/Right] by developers using reflection, unless they |
| 6333 | * explicitly call the setters in TextView. |
| 6334 | * |
| 6335 | * @param overwrite whether to overwrite already existing nonnull Drawables |
| 6336 | */ |
| 6337 | void loadHandleDrawables(final boolean overwrite) { |
| 6338 | if (mSelectHandleCenter == null || overwrite) { |
| 6339 | mSelectHandleCenter = mTextView.getTextSelectHandle(); |
| 6340 | if (hasInsertionController()) { |
| 6341 | getInsertionController().reloadHandleDrawable(); |
| 6342 | } |
| 6343 | } |
| 6344 | |
| 6345 | if (mSelectHandleLeft == null || mSelectHandleRight == null || overwrite) { |
| 6346 | mSelectHandleLeft = mTextView.getTextSelectHandleLeft(); |
| 6347 | mSelectHandleRight = mTextView.getTextSelectHandleRight(); |
| 6348 | if (hasSelectionController()) { |
| 6349 | getSelectionController().reloadHandleDrawables(); |
| 6350 | } |
| 6351 | } |
| 6352 | } |
| 6353 | |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6354 | private class CorrectionHighlighter { |
| 6355 | private final Path mPath = new Path(); |
Chris Craik | 6a49dde | 2015-05-12 10:28:14 -0700 | [diff] [blame] | 6356 | private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6357 | private int mStart, mEnd; |
| 6358 | private long mFadingStartTime; |
| 6359 | private RectF mTempRectF; |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 6360 | private static final int FADE_OUT_DURATION = 400; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6361 | |
| 6362 | public CorrectionHighlighter() { |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 6363 | mPaint.setCompatibilityScaling( |
| 6364 | mTextView.getResources().getCompatibilityInfo().applicationScale); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6365 | mPaint.setStyle(Paint.Style.FILL); |
| 6366 | } |
| 6367 | |
| 6368 | public void highlight(CorrectionInfo info) { |
| 6369 | mStart = info.getOffset(); |
| 6370 | mEnd = mStart + info.getNewText().length(); |
| 6371 | mFadingStartTime = SystemClock.uptimeMillis(); |
| 6372 | |
| 6373 | if (mStart < 0 || mEnd < 0) { |
| 6374 | stopAnimation(); |
| 6375 | } |
| 6376 | } |
| 6377 | |
| 6378 | public void draw(Canvas canvas, int cursorOffsetVertical) { |
| 6379 | if (updatePath() && updatePaint()) { |
| 6380 | if (cursorOffsetVertical != 0) { |
| 6381 | canvas.translate(0, cursorOffsetVertical); |
| 6382 | } |
| 6383 | |
| 6384 | canvas.drawPath(mPath, mPaint); |
| 6385 | |
| 6386 | if (cursorOffsetVertical != 0) { |
| 6387 | canvas.translate(0, -cursorOffsetVertical); |
| 6388 | } |
| 6389 | invalidate(true); // TODO invalidate cursor region only |
| 6390 | } else { |
| 6391 | stopAnimation(); |
| 6392 | invalidate(false); // TODO invalidate cursor region only |
| 6393 | } |
| 6394 | } |
| 6395 | |
| 6396 | private boolean updatePaint() { |
| 6397 | final long duration = SystemClock.uptimeMillis() - mFadingStartTime; |
| 6398 | if (duration > FADE_OUT_DURATION) return false; |
| 6399 | |
| 6400 | final float coef = 1.0f - (float) duration / FADE_OUT_DURATION; |
| 6401 | final int highlightColorAlpha = Color.alpha(mTextView.mHighlightColor); |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 6402 | final int color = (mTextView.mHighlightColor & 0x00FFFFFF) |
| 6403 | + ((int) (highlightColorAlpha * coef) << 24); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6404 | mPaint.setColor(color); |
| 6405 | return true; |
| 6406 | } |
| 6407 | |
| 6408 | private boolean updatePath() { |
| 6409 | final Layout layout = mTextView.getLayout(); |
| 6410 | if (layout == null) return false; |
| 6411 | |
| 6412 | // Update in case text is edited while the animation is run |
| 6413 | final int length = mTextView.getText().length(); |
| 6414 | int start = Math.min(length, mStart); |
| 6415 | int end = Math.min(length, mEnd); |
| 6416 | |
| 6417 | mPath.reset(); |
| 6418 | layout.getSelectionPath(start, end, mPath); |
| 6419 | return true; |
| 6420 | } |
| 6421 | |
| 6422 | private void invalidate(boolean delayed) { |
| 6423 | if (mTextView.getLayout() == null) return; |
| 6424 | |
| 6425 | if (mTempRectF == null) mTempRectF = new RectF(); |
| 6426 | mPath.computeBounds(mTempRectF, false); |
| 6427 | |
| 6428 | int left = mTextView.getCompoundPaddingLeft(); |
| 6429 | int top = mTextView.getExtendedPaddingTop() + mTextView.getVerticalOffset(true); |
| 6430 | |
| 6431 | if (delayed) { |
| 6432 | mTextView.postInvalidateOnAnimation( |
| 6433 | left + (int) mTempRectF.left, top + (int) mTempRectF.top, |
| 6434 | left + (int) mTempRectF.right, top + (int) mTempRectF.bottom); |
| 6435 | } else { |
| 6436 | mTextView.postInvalidate((int) mTempRectF.left, (int) mTempRectF.top, |
| 6437 | (int) mTempRectF.right, (int) mTempRectF.bottom); |
| 6438 | } |
| 6439 | } |
| 6440 | |
| 6441 | private void stopAnimation() { |
| 6442 | Editor.this.mCorrectionHighlighter = null; |
| 6443 | } |
| 6444 | } |
| 6445 | |
| 6446 | private static class ErrorPopup extends PopupWindow { |
| 6447 | private boolean mAbove = false; |
| 6448 | private final TextView mView; |
| 6449 | private int mPopupInlineErrorBackgroundId = 0; |
| 6450 | private int mPopupInlineErrorAboveBackgroundId = 0; |
| 6451 | |
| 6452 | ErrorPopup(TextView v, int width, int height) { |
| 6453 | super(v, width, height); |
| 6454 | mView = v; |
| 6455 | // 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] | 6456 | // shown and positioned. Initialized with below background, which should have |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6457 | // dimensions identical to the above version for this to work (and is more likely). |
| 6458 | mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId, |
| 6459 | com.android.internal.R.styleable.Theme_errorMessageBackground); |
| 6460 | mView.setBackgroundResource(mPopupInlineErrorBackgroundId); |
| 6461 | } |
| 6462 | |
| 6463 | void fixDirection(boolean above) { |
| 6464 | mAbove = above; |
| 6465 | |
| 6466 | if (above) { |
| 6467 | mPopupInlineErrorAboveBackgroundId = |
| 6468 | getResourceId(mPopupInlineErrorAboveBackgroundId, |
| 6469 | com.android.internal.R.styleable.Theme_errorMessageAboveBackground); |
| 6470 | } else { |
| 6471 | mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId, |
| 6472 | com.android.internal.R.styleable.Theme_errorMessageBackground); |
| 6473 | } |
| 6474 | |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 6475 | mView.setBackgroundResource( |
| 6476 | above ? mPopupInlineErrorAboveBackgroundId : mPopupInlineErrorBackgroundId); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6477 | } |
| 6478 | |
| 6479 | private int getResourceId(int currentId, int index) { |
| 6480 | if (currentId == 0) { |
| 6481 | TypedArray styledAttributes = mView.getContext().obtainStyledAttributes( |
| 6482 | R.styleable.Theme); |
| 6483 | currentId = styledAttributes.getResourceId(index, 0); |
| 6484 | styledAttributes.recycle(); |
| 6485 | } |
| 6486 | return currentId; |
| 6487 | } |
| 6488 | |
| 6489 | @Override |
| 6490 | public void update(int x, int y, int w, int h, boolean force) { |
| 6491 | super.update(x, y, w, h, force); |
| 6492 | |
| 6493 | boolean above = isAboveAnchor(); |
| 6494 | if (above != mAbove) { |
| 6495 | fixDirection(above); |
| 6496 | } |
| 6497 | } |
| 6498 | } |
| 6499 | |
| 6500 | static class InputContentType { |
| 6501 | int imeOptions = EditorInfo.IME_NULL; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 6502 | @UnsupportedAppUsage |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6503 | String privateImeOptions; |
| 6504 | CharSequence imeActionLabel; |
| 6505 | int imeActionId; |
| 6506 | Bundle extras; |
| 6507 | OnEditorActionListener onEditorActionListener; |
| 6508 | boolean enterDown; |
Yohei Yukawa | d469f21 | 2016-01-21 12:38:09 -0800 | [diff] [blame] | 6509 | LocaleList imeHintLocales; |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6510 | } |
| 6511 | |
| 6512 | static class InputMethodState { |
Gilles Debunne | c62589c | 2012-04-12 14:50:23 -0700 | [diff] [blame] | 6513 | ExtractedTextRequest mExtractedTextRequest; |
| 6514 | final ExtractedText mExtractedText = new ExtractedText(); |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 6515 | int mBatchEditNesting; |
| 6516 | boolean mCursorChanged; |
| 6517 | boolean mSelectionModeChanged; |
| 6518 | boolean mContentChanged; |
| 6519 | int mChangedStart, mChangedEnd, mChangedDelta; |
| 6520 | } |
Satoshi Kataoka | 0e3849a | 2012-12-13 14:37:19 +0900 | [diff] [blame] | 6521 | |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 6522 | /** |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6523 | * @return True iff (start, end) is a valid range within the text. |
| 6524 | */ |
| 6525 | private static boolean isValidRange(CharSequence text, int start, int end) { |
| 6526 | return 0 <= start && start <= end && end <= text.length(); |
| 6527 | } |
| 6528 | |
| 6529 | /** |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 6530 | * An InputFilter that monitors text input to maintain undo history. It does not modify the |
| 6531 | * text being typed (and hence always returns null from the filter() method). |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6532 | * |
| 6533 | * TODO: Make this span aware. |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 6534 | */ |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6535 | public static class UndoInputFilter implements InputFilter { |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 6536 | private final Editor mEditor; |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6537 | |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6538 | // Whether the current filter pass is directly caused by an end-user text edit. |
| 6539 | private boolean mIsUserEdit; |
| 6540 | |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 6541 | // Whether the text field is handling an IME composition. Must be parceled in case the user |
| 6542 | // rotates the screen during composition. |
| 6543 | private boolean mHasComposition; |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6544 | |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6545 | // Whether the user is expanding or shortening the text |
| 6546 | private boolean mExpanding; |
| 6547 | |
| 6548 | // Whether the previous edit operation was in the current batch edit. |
| 6549 | private boolean mPreviousOperationWasInSameBatchEdit; |
Keisuke Kuroyanagi | fae4578 | 2016-02-24 18:53:00 -0800 | [diff] [blame] | 6550 | |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6551 | public UndoInputFilter(Editor editor) { |
| 6552 | mEditor = editor; |
| 6553 | } |
| 6554 | |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 6555 | public void saveInstanceState(Parcel parcel) { |
| 6556 | parcel.writeInt(mIsUserEdit ? 1 : 0); |
| 6557 | parcel.writeInt(mHasComposition ? 1 : 0); |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6558 | parcel.writeInt(mExpanding ? 1 : 0); |
| 6559 | parcel.writeInt(mPreviousOperationWasInSameBatchEdit ? 1 : 0); |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 6560 | } |
| 6561 | |
| 6562 | public void restoreInstanceState(Parcel parcel) { |
| 6563 | mIsUserEdit = parcel.readInt() != 0; |
| 6564 | mHasComposition = parcel.readInt() != 0; |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6565 | mExpanding = parcel.readInt() != 0; |
| 6566 | mPreviousOperationWasInSameBatchEdit = parcel.readInt() != 0; |
Keisuke Kuroyanagi | fae4578 | 2016-02-24 18:53:00 -0800 | [diff] [blame] | 6567 | } |
| 6568 | |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6569 | /** |
| 6570 | * Signals that a user-triggered edit is starting. |
| 6571 | */ |
| 6572 | public void beginBatchEdit() { |
| 6573 | if (DEBUG_UNDO) Log.d(TAG, "beginBatchEdit"); |
| 6574 | mIsUserEdit = true; |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6575 | } |
| 6576 | |
| 6577 | public void endBatchEdit() { |
| 6578 | if (DEBUG_UNDO) Log.d(TAG, "endBatchEdit"); |
| 6579 | mIsUserEdit = false; |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6580 | mPreviousOperationWasInSameBatchEdit = false; |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6581 | } |
| 6582 | |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6583 | @Override |
| 6584 | public CharSequence filter(CharSequence source, int start, int end, |
| 6585 | Spanned dest, int dstart, int dend) { |
| 6586 | if (DEBUG_UNDO) { |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 6587 | Log.d(TAG, "filter: source=" + source + " (" + start + "-" + end + ") " |
| 6588 | + "dest=" + dest + " (" + dstart + "-" + dend + ")"); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6589 | } |
James Cook | f1dad1e | 2015-02-27 11:00:01 -0800 | [diff] [blame] | 6590 | |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6591 | // Check to see if this edit should be tracked for undo. |
| 6592 | if (!canUndoEdit(source, start, end, dest, dstart, dend)) { |
James Cook | f1dad1e | 2015-02-27 11:00:01 -0800 | [diff] [blame] | 6593 | return null; |
| 6594 | } |
| 6595 | |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6596 | final boolean hadComposition = mHasComposition; |
| 6597 | mHasComposition = isComposition(source); |
| 6598 | final boolean wasExpanding = mExpanding; |
| 6599 | boolean shouldCreateSeparateState = false; |
| 6600 | if ((end - start) != (dend - dstart)) { |
| 6601 | mExpanding = (end - start) > (dend - dstart); |
| 6602 | if (hadComposition && mExpanding != wasExpanding) { |
| 6603 | shouldCreateSeparateState = true; |
| 6604 | } |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 6605 | } |
| 6606 | |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6607 | // Handle edit. |
| 6608 | handleEdit(source, start, end, dest, dstart, dend, shouldCreateSeparateState); |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 6609 | return null; |
| 6610 | } |
| 6611 | |
Keisuke Kuroyanagi | fa2d7b1 | 2016-07-22 17:09:48 +0900 | [diff] [blame] | 6612 | void freezeLastEdit() { |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6613 | mEditor.mUndoManager.beginUpdate("Edit text"); |
| 6614 | EditOperation lastEdit = getLastEdit(); |
| 6615 | if (lastEdit != null) { |
| 6616 | lastEdit.mFrozen = true; |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 6617 | } |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6618 | mEditor.mUndoManager.endUpdate(); |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 6619 | } |
| 6620 | |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6621 | @Retention(RetentionPolicy.SOURCE) |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 6622 | @IntDef(prefix = { "MERGE_EDIT_MODE_" }, value = { |
| 6623 | MERGE_EDIT_MODE_FORCE_MERGE, |
| 6624 | MERGE_EDIT_MODE_NEVER_MERGE, |
| 6625 | MERGE_EDIT_MODE_NORMAL |
| 6626 | }) |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6627 | private @interface MergeMode {} |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 6628 | private static final int MERGE_EDIT_MODE_FORCE_MERGE = 0; |
| 6629 | private static final int MERGE_EDIT_MODE_NEVER_MERGE = 1; |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6630 | /** Use {@link EditOperation#mergeWith} to merge */ |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 6631 | private static final int MERGE_EDIT_MODE_NORMAL = 2; |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6632 | |
| 6633 | private void handleEdit(CharSequence source, int start, int end, |
| 6634 | Spanned dest, int dstart, int dend, boolean shouldCreateSeparateState) { |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6635 | // An application may install a TextWatcher to provide additional modifications after |
| 6636 | // the initial input filters run (e.g. a credit card formatter that adds spaces to a |
| 6637 | // string). This results in multiple filter() calls for what the user considers to be |
| 6638 | // a single operation. Always undo the whole set of changes in one step. |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6639 | @MergeMode |
| 6640 | final int mergeMode; |
| 6641 | if (isInTextWatcher() || mPreviousOperationWasInSameBatchEdit) { |
| 6642 | mergeMode = MERGE_EDIT_MODE_FORCE_MERGE; |
| 6643 | } else if (shouldCreateSeparateState) { |
| 6644 | mergeMode = MERGE_EDIT_MODE_NEVER_MERGE; |
| 6645 | } else { |
| 6646 | mergeMode = MERGE_EDIT_MODE_NORMAL; |
| 6647 | } |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6648 | // Build a new operation with all the information from this edit. |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 6649 | String newText = TextUtils.substring(source, start, end); |
| 6650 | String oldText = TextUtils.substring(dest, dstart, dend); |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6651 | EditOperation edit = new EditOperation(mEditor, oldText, dstart, newText, |
| 6652 | mHasComposition); |
| 6653 | if (mHasComposition && TextUtils.equals(edit.mNewText, edit.mOldText)) { |
| 6654 | return; |
| 6655 | } |
| 6656 | recordEdit(edit, mergeMode); |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 6657 | } |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6658 | |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6659 | private EditOperation getLastEdit() { |
| 6660 | final UndoManager um = mEditor.mUndoManager; |
| 6661 | return um.getLastOperation( |
| 6662 | EditOperation.class, mEditor.mUndoOwner, UndoManager.MERGE_MODE_UNIQUE); |
| 6663 | } |
James Cook | 2205425 | 2015-03-25 14:04:01 -0700 | [diff] [blame] | 6664 | /** |
| 6665 | * Fetches the last undo operation and checks to see if a new edit should be merged into it. |
| 6666 | * If forceMerge is true then the new edit is always merged. |
| 6667 | */ |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6668 | private void recordEdit(EditOperation edit, @MergeMode int mergeMode) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6669 | // 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] | 6670 | final UndoManager um = mEditor.mUndoManager; |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6671 | um.beginUpdate("Edit text"); |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6672 | EditOperation lastEdit = getLastEdit(); |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6673 | if (lastEdit == null) { |
| 6674 | // Add this as the first edit. |
| 6675 | if (DEBUG_UNDO) Log.d(TAG, "filter: adding first op " + edit); |
| 6676 | um.addOperation(edit, UndoManager.MERGE_MODE_NONE); |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6677 | } else if (mergeMode == MERGE_EDIT_MODE_FORCE_MERGE) { |
James Cook | 2205425 | 2015-03-25 14:04:01 -0700 | [diff] [blame] | 6678 | // Forced merges take priority because they could be the result of a non-user-edit |
| 6679 | // change and this case should not create a new undo operation. |
| 6680 | if (DEBUG_UNDO) Log.d(TAG, "filter: force merge " + edit); |
| 6681 | lastEdit.forceMergeWith(edit); |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6682 | } else if (!mIsUserEdit) { |
| 6683 | // An application directly modified the Editable outside of a text edit. Treat this |
| 6684 | // as a new change and don't attempt to merge. |
| 6685 | if (DEBUG_UNDO) Log.d(TAG, "non-user edit, new op " + edit); |
| 6686 | um.commitState(mEditor.mUndoOwner); |
| 6687 | um.addOperation(edit, UndoManager.MERGE_MODE_NONE); |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6688 | } else if (mergeMode == MERGE_EDIT_MODE_NORMAL && lastEdit.mergeWith(edit)) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6689 | // Merge succeeded, nothing else to do. |
| 6690 | if (DEBUG_UNDO) Log.d(TAG, "filter: merge succeeded, created " + lastEdit); |
James Cook | 3ac0bcb | 2015-02-26 10:53:41 -0800 | [diff] [blame] | 6691 | } else { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6692 | // Could not merge with the last edit, so commit the last edit and add this edit. |
| 6693 | if (DEBUG_UNDO) Log.d(TAG, "filter: merge failed, adding " + edit); |
| 6694 | um.commitState(mEditor.mUndoOwner); |
| 6695 | um.addOperation(edit, UndoManager.MERGE_MODE_NONE); |
James Cook | 3ac0bcb | 2015-02-26 10:53:41 -0800 | [diff] [blame] | 6696 | } |
Keisuke Kuroyanagi | fa2d7b1 | 2016-07-22 17:09:48 +0900 | [diff] [blame] | 6697 | mPreviousOperationWasInSameBatchEdit = mIsUserEdit; |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6698 | um.endUpdate(); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6699 | } |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6700 | |
| 6701 | private boolean canUndoEdit(CharSequence source, int start, int end, |
| 6702 | Spanned dest, int dstart, int dend) { |
| 6703 | if (!mEditor.mAllowUndo) { |
| 6704 | if (DEBUG_UNDO) Log.d(TAG, "filter: undo is disabled"); |
| 6705 | return false; |
| 6706 | } |
| 6707 | |
| 6708 | if (mEditor.mUndoManager.isInUndo()) { |
| 6709 | if (DEBUG_UNDO) Log.d(TAG, "filter: skipping, currently performing undo/redo"); |
| 6710 | return false; |
| 6711 | } |
| 6712 | |
| 6713 | // Text filters run before input operations are applied. However, some input operations |
| 6714 | // are invalid and will throw exceptions when applied. This is common in tests. Don't |
| 6715 | // attempt to undo invalid operations. |
| 6716 | if (!isValidRange(source, start, end) || !isValidRange(dest, dstart, dend)) { |
| 6717 | if (DEBUG_UNDO) Log.d(TAG, "filter: invalid op"); |
| 6718 | return false; |
| 6719 | } |
| 6720 | |
| 6721 | // Earlier filters can rewrite input to be a no-op, for example due to a length limit |
| 6722 | // on an input field. Skip no-op changes. |
| 6723 | if (start == end && dstart == dend) { |
| 6724 | if (DEBUG_UNDO) Log.d(TAG, "filter: skipping no-op"); |
| 6725 | return false; |
| 6726 | } |
| 6727 | |
| 6728 | return true; |
| 6729 | } |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 6730 | |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6731 | private static boolean isComposition(CharSequence source) { |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 6732 | if (!(source instanceof Spannable)) { |
| 6733 | return false; |
| 6734 | } |
| 6735 | // This is a composition edit if the source has a non-zero-length composing span. |
| 6736 | Spannable text = (Spannable) source; |
| 6737 | int composeBegin = EditableInputConnection.getComposingSpanStart(text); |
| 6738 | int composeEnd = EditableInputConnection.getComposingSpanEnd(text); |
| 6739 | return composeBegin < composeEnd; |
| 6740 | } |
| 6741 | |
| 6742 | private boolean isInTextWatcher() { |
| 6743 | CharSequence text = mEditor.mTextView.getText(); |
| 6744 | return (text instanceof SpannableStringBuilder) |
| 6745 | && ((SpannableStringBuilder) text).getTextWatcherDepth() > 0; |
| 6746 | } |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6747 | } |
| 6748 | |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 6749 | /** |
| 6750 | * An operation to undo a single "edit" to a text view. |
| 6751 | */ |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6752 | public static class EditOperation extends UndoOperation<Editor> { |
| 6753 | private static final int TYPE_INSERT = 0; |
| 6754 | private static final int TYPE_DELETE = 1; |
| 6755 | private static final int TYPE_REPLACE = 2; |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6756 | |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6757 | private int mType; |
| 6758 | private String mOldText; |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6759 | private String mNewText; |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6760 | private int mStart; |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6761 | |
| 6762 | private int mOldCursorPos; |
| 6763 | private int mNewCursorPos; |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6764 | private boolean mFrozen; |
| 6765 | private boolean mIsComposition; |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6766 | |
| 6767 | /** |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 6768 | * 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] | 6769 | * oldText starting at dstart with newText. |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6770 | */ |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6771 | public EditOperation(Editor editor, String oldText, int dstart, String newText, |
| 6772 | boolean isComposition) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6773 | super(editor.mUndoOwner); |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 6774 | mOldText = oldText; |
| 6775 | mNewText = newText; |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6776 | |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6777 | // Determine the type of the edit. |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6778 | if (mNewText.length() > 0 && mOldText.length() == 0) { |
| 6779 | mType = TYPE_INSERT; |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6780 | } else if (mNewText.length() == 0 && mOldText.length() > 0) { |
| 6781 | mType = TYPE_DELETE; |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6782 | } else { |
| 6783 | mType = TYPE_REPLACE; |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6784 | } |
| 6785 | |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6786 | mStart = dstart; |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6787 | // Store cursor data. |
| 6788 | mOldCursorPos = editor.mTextView.getSelectionStart(); |
James Cook | d202668 | 2015-03-03 14:40:14 -0800 | [diff] [blame] | 6789 | mNewCursorPos = dstart + mNewText.length(); |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6790 | mIsComposition = isComposition; |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6791 | } |
| 6792 | |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6793 | public EditOperation(Parcel src, ClassLoader loader) { |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6794 | super(src, loader); |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6795 | mType = src.readInt(); |
| 6796 | mOldText = src.readString(); |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6797 | mNewText = src.readString(); |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6798 | mStart = src.readInt(); |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6799 | mOldCursorPos = src.readInt(); |
| 6800 | mNewCursorPos = src.readInt(); |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6801 | mFrozen = src.readInt() == 1; |
| 6802 | mIsComposition = src.readInt() == 1; |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6803 | } |
| 6804 | |
| 6805 | @Override |
| 6806 | public void writeToParcel(Parcel dest, int flags) { |
| 6807 | dest.writeInt(mType); |
| 6808 | dest.writeString(mOldText); |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6809 | dest.writeString(mNewText); |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6810 | dest.writeInt(mStart); |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6811 | dest.writeInt(mOldCursorPos); |
| 6812 | dest.writeInt(mNewCursorPos); |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6813 | dest.writeInt(mFrozen ? 1 : 0); |
| 6814 | dest.writeInt(mIsComposition ? 1 : 0); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6815 | } |
| 6816 | |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6817 | private int getNewTextEnd() { |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6818 | return mStart + mNewText.length(); |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6819 | } |
| 6820 | |
| 6821 | private int getOldTextEnd() { |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6822 | return mStart + mOldText.length(); |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6823 | } |
| 6824 | |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6825 | @Override |
| 6826 | public void commit() { |
| 6827 | } |
| 6828 | |
| 6829 | @Override |
| 6830 | public void undo() { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6831 | if (DEBUG_UNDO) Log.d(TAG, "undo"); |
| 6832 | // Remove the new text and insert the old. |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6833 | Editor editor = getOwnerData(); |
| 6834 | Editable text = (Editable) editor.mTextView.getText(); |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6835 | modifyText(text, mStart, getNewTextEnd(), mOldText, mStart, mOldCursorPos); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6836 | } |
| 6837 | |
| 6838 | @Override |
| 6839 | public void redo() { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6840 | if (DEBUG_UNDO) Log.d(TAG, "redo"); |
| 6841 | // Remove the old text and insert the new. |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6842 | Editor editor = getOwnerData(); |
| 6843 | Editable text = (Editable) editor.mTextView.getText(); |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6844 | modifyText(text, mStart, getOldTextEnd(), mNewText, mStart, mNewCursorPos); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6845 | } |
| 6846 | |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6847 | /** |
| 6848 | * Attempts to merge this existing operation with a new edit. |
| 6849 | * @param edit The new edit operation. |
| 6850 | * @return If the merge succeeded, returns true. Otherwise returns false and leaves this |
| 6851 | * object unchanged. |
| 6852 | */ |
| 6853 | private boolean mergeWith(EditOperation edit) { |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6854 | if (DEBUG_UNDO) { |
| 6855 | Log.d(TAG, "mergeWith old " + this); |
| 6856 | Log.d(TAG, "mergeWith new " + edit); |
| 6857 | } |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6858 | |
| 6859 | if (mFrozen) { |
| 6860 | return false; |
| 6861 | } |
| 6862 | |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6863 | switch (mType) { |
| 6864 | case TYPE_INSERT: |
| 6865 | return mergeInsertWith(edit); |
| 6866 | case TYPE_DELETE: |
| 6867 | return mergeDeleteWith(edit); |
| 6868 | case TYPE_REPLACE: |
| 6869 | return mergeReplaceWith(edit); |
| 6870 | default: |
| 6871 | return false; |
| 6872 | } |
| 6873 | } |
| 6874 | |
| 6875 | private boolean mergeInsertWith(EditOperation edit) { |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6876 | if (edit.mType == TYPE_INSERT) { |
| 6877 | // Merge insertions that are contiguous even when it's frozen. |
| 6878 | if (getNewTextEnd() != edit.mStart) { |
| 6879 | return false; |
| 6880 | } |
| 6881 | mNewText += edit.mNewText; |
| 6882 | mNewCursorPos = edit.mNewCursorPos; |
| 6883 | mFrozen = edit.mFrozen; |
| 6884 | mIsComposition = edit.mIsComposition; |
| 6885 | return true; |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6886 | } |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 6887 | if (mIsComposition && edit.mType == TYPE_REPLACE |
| 6888 | && mStart <= edit.mStart && getNewTextEnd() >= edit.getOldTextEnd()) { |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6889 | // Merge insertion with replace as they can be single insertion. |
| 6890 | mNewText = mNewText.substring(0, edit.mStart - mStart) + edit.mNewText |
| 6891 | + mNewText.substring(edit.getOldTextEnd() - mStart, mNewText.length()); |
| 6892 | mNewCursorPos = edit.mNewCursorPos; |
| 6893 | mIsComposition = edit.mIsComposition; |
| 6894 | return true; |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6895 | } |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6896 | return false; |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6897 | } |
| 6898 | |
| 6899 | // TODO: Support forward delete. |
| 6900 | private boolean mergeDeleteWith(EditOperation edit) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6901 | // Only merge continuous deletes. |
| 6902 | if (edit.mType != TYPE_DELETE) { |
| 6903 | return false; |
| 6904 | } |
| 6905 | // Only merge deletions that are contiguous. |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6906 | if (mStart != edit.getOldTextEnd()) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6907 | return false; |
| 6908 | } |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6909 | mStart = edit.mStart; |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6910 | mOldText = edit.mOldText + mOldText; |
| 6911 | mNewCursorPos = edit.mNewCursorPos; |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6912 | mIsComposition = edit.mIsComposition; |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6913 | return true; |
| 6914 | } |
| 6915 | |
| 6916 | private boolean mergeReplaceWith(EditOperation edit) { |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6917 | if (edit.mType == TYPE_INSERT && getNewTextEnd() == edit.mStart) { |
| 6918 | // Merge with adjacent insert. |
| 6919 | mNewText += edit.mNewText; |
| 6920 | mNewCursorPos = edit.mNewCursorPos; |
| 6921 | return true; |
| 6922 | } |
| 6923 | if (!mIsComposition) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6924 | return false; |
| 6925 | } |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6926 | if (edit.mType == TYPE_DELETE && mStart <= edit.mStart |
| 6927 | && getNewTextEnd() >= edit.getOldTextEnd()) { |
| 6928 | // Merge with delete as they can be single operation. |
| 6929 | mNewText = mNewText.substring(0, edit.mStart - mStart) |
| 6930 | + mNewText.substring(edit.getOldTextEnd() - mStart, mNewText.length()); |
| 6931 | if (mNewText.isEmpty()) { |
| 6932 | mType = TYPE_DELETE; |
| 6933 | } |
| 6934 | mNewCursorPos = edit.mNewCursorPos; |
| 6935 | mIsComposition = edit.mIsComposition; |
| 6936 | return true; |
| 6937 | } |
| 6938 | if (edit.mType == TYPE_REPLACE && mStart == edit.mStart |
| 6939 | && TextUtils.equals(mNewText, edit.mOldText)) { |
| 6940 | // Merge with the replace that replaces the same region. |
| 6941 | mNewText = edit.mNewText; |
| 6942 | mNewCursorPos = edit.mNewCursorPos; |
| 6943 | mIsComposition = edit.mIsComposition; |
| 6944 | return true; |
| 6945 | } |
| 6946 | return false; |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6947 | } |
| 6948 | |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6949 | /** |
| 6950 | * Forcibly creates a single merged edit operation by simulating the entire text |
| 6951 | * contents being replaced. |
| 6952 | */ |
James Cook | 2205425 | 2015-03-25 14:04:01 -0700 | [diff] [blame] | 6953 | public void forceMergeWith(EditOperation edit) { |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6954 | if (DEBUG_UNDO) Log.d(TAG, "forceMerge"); |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6955 | if (mergeWith(edit)) { |
| 6956 | return; |
| 6957 | } |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 6958 | Editor editor = getOwnerData(); |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6959 | |
| 6960 | // Copy the text of the current field. |
| 6961 | // NOTE: Using StringBuilder instead of SpannableStringBuilder would be somewhat faster, |
| 6962 | // but would require two parallel implementations of modifyText() because Editable and |
| 6963 | // StringBuilder do not share an interface for replace/delete/insert. |
| 6964 | Editable editable = (Editable) editor.mTextView.getText(); |
| 6965 | Editable originalText = new SpannableStringBuilder(editable.toString()); |
| 6966 | |
| 6967 | // Roll back the last operation. |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6968 | modifyText(originalText, mStart, getNewTextEnd(), mOldText, mStart, mOldCursorPos); |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6969 | |
| 6970 | // Clone the text again and apply the new operation. |
| 6971 | Editable finalText = new SpannableStringBuilder(editable.toString()); |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6972 | modifyText(finalText, edit.mStart, edit.getOldTextEnd(), |
| 6973 | edit.mNewText, edit.mStart, edit.mNewCursorPos); |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6974 | |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6975 | // Convert this operation into a replace operation. |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6976 | mType = TYPE_REPLACE; |
| 6977 | mNewText = finalText.toString(); |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6978 | mOldText = originalText.toString(); |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6979 | mStart = 0; |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6980 | mNewCursorPos = edit.mNewCursorPos; |
Keisuke Kuroyanagi | 113c004 | 2016-07-01 18:42:05 +0900 | [diff] [blame] | 6981 | mIsComposition = edit.mIsComposition; |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 6982 | // mOldCursorPos is unchanged. |
| 6983 | } |
| 6984 | |
| 6985 | private static void modifyText(Editable text, int deleteFrom, int deleteTo, |
| 6986 | CharSequence newText, int newTextInsertAt, int newCursorPos) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6987 | // Apply the edit if it is still valid. |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 6988 | if (isValidRange(text, deleteFrom, deleteTo) |
| 6989 | && newTextInsertAt <= text.length() - (deleteTo - deleteFrom)) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6990 | if (deleteFrom != deleteTo) { |
| 6991 | text.delete(deleteFrom, deleteTo); |
| 6992 | } |
| 6993 | if (newText.length() != 0) { |
| 6994 | text.insert(newTextInsertAt, newText); |
| 6995 | } |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 6996 | } |
James Cook | 900185d | 2015-03-10 09:48:11 -0700 | [diff] [blame] | 6997 | // Restore the cursor position. If there wasn't an old cursor (newCursorPos == -1) then |
| 6998 | // don't explicitly set it and rely on SpannableStringBuilder to position it. |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 6999 | // TODO: Select all the text that was undone. |
James Cook | 900185d | 2015-03-10 09:48:11 -0700 | [diff] [blame] | 7000 | if (0 <= newCursorPos && newCursorPos <= text.length()) { |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 7001 | Selection.setSelection(text, newCursorPos); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 7002 | } |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 7003 | } |
| 7004 | |
James Cook | 48e0fac | 2015-02-25 15:44:51 -0800 | [diff] [blame] | 7005 | private String getTypeString() { |
| 7006 | switch (mType) { |
| 7007 | case TYPE_INSERT: |
| 7008 | return "insert"; |
| 7009 | case TYPE_DELETE: |
| 7010 | return "delete"; |
| 7011 | case TYPE_REPLACE: |
| 7012 | return "replace"; |
| 7013 | default: |
| 7014 | return ""; |
| 7015 | } |
| 7016 | } |
| 7017 | |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 7018 | @Override |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 7019 | public String toString() { |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 7020 | return "[mType=" + getTypeString() + ", " |
| 7021 | + "mOldText=" + mOldText + ", " |
| 7022 | + "mNewText=" + mNewText + ", " |
| 7023 | + "mStart=" + mStart + ", " |
| 7024 | + "mOldCursorPos=" + mOldCursorPos + ", " |
| 7025 | + "mNewCursorPos=" + mNewCursorPos + ", " |
| 7026 | + "mFrozen=" + mFrozen + ", " |
| 7027 | + "mIsComposition=" + mIsComposition + "]"; |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 7028 | } |
| 7029 | |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 7030 | public static final Parcelable.ClassLoaderCreator<EditOperation> CREATOR = |
| 7031 | new Parcelable.ClassLoaderCreator<EditOperation>() { |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 7032 | @Override |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 7033 | public EditOperation createFromParcel(Parcel in) { |
| 7034 | return new EditOperation(in, null); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 7035 | } |
| 7036 | |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 7037 | @Override |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 7038 | public EditOperation createFromParcel(Parcel in, ClassLoader loader) { |
| 7039 | return new EditOperation(in, loader); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 7040 | } |
| 7041 | |
James Cook | f59152c | 2015-02-26 18:03:58 -0800 | [diff] [blame] | 7042 | @Override |
James Cook | 471559f | 2015-02-27 10:31:20 -0800 | [diff] [blame] | 7043 | public EditOperation[] newArray(int size) { |
| 7044 | return new EditOperation[size]; |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 7045 | } |
| 7046 | }; |
| 7047 | } |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 7048 | |
| 7049 | /** |
| 7050 | * A helper for enabling and handling "PROCESS_TEXT" menu actions. |
| 7051 | * These allow external applications to plug into currently selected text. |
| 7052 | */ |
| 7053 | static final class ProcessTextIntentActionsHandler { |
| 7054 | |
| 7055 | private final Editor mEditor; |
| 7056 | private final TextView mTextView; |
Abodunrinwa Toki | 5ba060b | 2016-05-24 18:34:40 +0100 | [diff] [blame] | 7057 | private final Context mContext; |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 7058 | private final PackageManager mPackageManager; |
Abodunrinwa Toki | 5ba060b | 2016-05-24 18:34:40 +0100 | [diff] [blame] | 7059 | private final String mPackageName; |
| 7060 | private final SparseArray<Intent> mAccessibilityIntents = new SparseArray<>(); |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 7061 | private final SparseArray<AccessibilityNodeInfo.AccessibilityAction> mAccessibilityActions = |
| 7062 | new SparseArray<>(); |
| 7063 | private final List<ResolveInfo> mSupportedActivities = new ArrayList<>(); |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 7064 | |
| 7065 | private ProcessTextIntentActionsHandler(Editor editor) { |
Daulet Zhanguzin | cb0d19b | 2019-12-18 15:08:09 +0000 | [diff] [blame] | 7066 | mEditor = Objects.requireNonNull(editor); |
| 7067 | mTextView = Objects.requireNonNull(mEditor.mTextView); |
| 7068 | mContext = Objects.requireNonNull(mTextView.getContext()); |
| 7069 | mPackageManager = Objects.requireNonNull(mContext.getPackageManager()); |
| 7070 | mPackageName = Objects.requireNonNull(mContext.getPackageName()); |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 7071 | } |
| 7072 | |
| 7073 | /** |
| 7074 | * Adds "PROCESS_TEXT" menu items to the specified menu. |
| 7075 | */ |
| 7076 | public void onInitializeMenu(Menu menu) { |
Abodunrinwa Toki | 5ba060b | 2016-05-24 18:34:40 +0100 | [diff] [blame] | 7077 | loadSupportedActivities(); |
Abodunrinwa Toki | c28be38 | 2017-11-07 18:46:50 +0000 | [diff] [blame] | 7078 | final int size = mSupportedActivities.size(); |
Abodunrinwa Toki | 6eecdc9 | 2017-04-11 05:15:25 +0100 | [diff] [blame] | 7079 | for (int i = 0; i < size; i++) { |
| 7080 | final ResolveInfo resolveInfo = mSupportedActivities.get(i); |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 7081 | menu.add(Menu.NONE, Menu.NONE, |
Abodunrinwa Toki | c28be38 | 2017-11-07 18:46:50 +0000 | [diff] [blame] | 7082 | Editor.MENU_ITEM_ORDER_PROCESS_TEXT_INTENT_ACTIONS_START + i, |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 7083 | getLabel(resolveInfo)) |
| 7084 | .setIntent(createProcessTextIntentForResolveInfo(resolveInfo)) |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 7085 | .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 7086 | } |
| 7087 | } |
| 7088 | |
| 7089 | /** |
| 7090 | * Performs a "PROCESS_TEXT" action if there is one associated with the specified |
| 7091 | * menu item. |
| 7092 | * |
| 7093 | * @return True if the action was performed, false otherwise. |
| 7094 | */ |
| 7095 | public boolean performMenuItemAction(MenuItem item) { |
| 7096 | return fireIntent(item.getIntent()); |
| 7097 | } |
| 7098 | |
| 7099 | /** |
| 7100 | * Initializes and caches "PROCESS_TEXT" accessibility actions. |
| 7101 | */ |
| 7102 | public void initializeAccessibilityActions() { |
| 7103 | mAccessibilityIntents.clear(); |
| 7104 | mAccessibilityActions.clear(); |
| 7105 | int i = 0; |
Abodunrinwa Toki | 5ba060b | 2016-05-24 18:34:40 +0100 | [diff] [blame] | 7106 | loadSupportedActivities(); |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 7107 | for (ResolveInfo resolveInfo : mSupportedActivities) { |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 7108 | int actionId = TextView.ACCESSIBILITY_ACTION_PROCESS_TEXT_START_ID + i++; |
| 7109 | mAccessibilityActions.put( |
| 7110 | actionId, |
| 7111 | new AccessibilityNodeInfo.AccessibilityAction( |
| 7112 | actionId, getLabel(resolveInfo))); |
| 7113 | mAccessibilityIntents.put( |
| 7114 | actionId, createProcessTextIntentForResolveInfo(resolveInfo)); |
| 7115 | } |
| 7116 | } |
| 7117 | |
| 7118 | /** |
| 7119 | * Adds "PROCESS_TEXT" accessibility actions to the specified accessibility node info. |
| 7120 | * NOTE: This needs a prior call to {@link #initializeAccessibilityActions()} to make the |
| 7121 | * latest accessibility actions available for this call. |
| 7122 | */ |
| 7123 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo nodeInfo) { |
| 7124 | for (int i = 0; i < mAccessibilityActions.size(); i++) { |
| 7125 | nodeInfo.addAction(mAccessibilityActions.valueAt(i)); |
| 7126 | } |
| 7127 | } |
| 7128 | |
| 7129 | /** |
| 7130 | * Performs a "PROCESS_TEXT" action if there is one associated with the specified |
| 7131 | * accessibility action id. |
| 7132 | * |
| 7133 | * @return True if the action was performed, false otherwise. |
| 7134 | */ |
| 7135 | public boolean performAccessibilityAction(int actionId) { |
| 7136 | return fireIntent(mAccessibilityIntents.get(actionId)); |
| 7137 | } |
| 7138 | |
| 7139 | private boolean fireIntent(Intent intent) { |
| 7140 | if (intent != null && Intent.ACTION_PROCESS_TEXT.equals(intent.getAction())) { |
Siyamed Sinir | ce3b05a | 2017-07-18 18:54:31 -0700 | [diff] [blame] | 7141 | String selectedText = mTextView.getSelectedText(); |
| 7142 | selectedText = TextUtils.trimToParcelableSize(selectedText); |
| 7143 | intent.putExtra(Intent.EXTRA_PROCESS_TEXT, selectedText); |
Keisuke Kuroyanagi | af4caa6 | 2016-02-29 12:53:58 -0800 | [diff] [blame] | 7144 | mEditor.mPreserveSelection = true; |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 7145 | mTextView.startActivityForResult(intent, TextView.PROCESS_TEXT_REQUEST_CODE); |
| 7146 | return true; |
| 7147 | } |
| 7148 | return false; |
| 7149 | } |
| 7150 | |
Abodunrinwa Toki | 5ba060b | 2016-05-24 18:34:40 +0100 | [diff] [blame] | 7151 | private void loadSupportedActivities() { |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 7152 | mSupportedActivities.clear(); |
Abodunrinwa Toki | 3049d8c | 2017-10-17 20:33:00 +0100 | [diff] [blame] | 7153 | if (!mContext.canStartActivityForResult()) { |
| 7154 | return; |
| 7155 | } |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 7156 | PackageManager packageManager = mTextView.getContext().getPackageManager(); |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 7157 | List<ResolveInfo> unfiltered = |
| 7158 | packageManager.queryIntentActivities(createProcessTextIntent(), 0); |
Abodunrinwa Toki | 5ba060b | 2016-05-24 18:34:40 +0100 | [diff] [blame] | 7159 | for (ResolveInfo info : unfiltered) { |
| 7160 | if (isSupportedActivity(info)) { |
Aurimas Liutikas | ee62c29 | 2016-07-21 15:05:40 -0700 | [diff] [blame] | 7161 | mSupportedActivities.add(info); |
Abodunrinwa Toki | 5ba060b | 2016-05-24 18:34:40 +0100 | [diff] [blame] | 7162 | } |
| 7163 | } |
| 7164 | } |
| 7165 | |
| 7166 | private boolean isSupportedActivity(ResolveInfo info) { |
| 7167 | return mPackageName.equals(info.activityInfo.packageName) |
| 7168 | || info.activityInfo.exported |
| 7169 | && (info.activityInfo.permission == null |
| 7170 | || mContext.checkSelfPermission(info.activityInfo.permission) |
| 7171 | == PackageManager.PERMISSION_GRANTED); |
Abodunrinwa Toki | deaf0db | 2015-06-26 18:21:30 -0700 | [diff] [blame] | 7172 | } |
| 7173 | |
| 7174 | private Intent createProcessTextIntentForResolveInfo(ResolveInfo info) { |
| 7175 | return createProcessTextIntent() |
| 7176 | .putExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, !mTextView.isTextEditable()) |
| 7177 | .setClassName(info.activityInfo.packageName, info.activityInfo.name); |
| 7178 | } |
| 7179 | |
| 7180 | private Intent createProcessTextIntent() { |
| 7181 | return new Intent() |
| 7182 | .setAction(Intent.ACTION_PROCESS_TEXT) |
| 7183 | .setType("text/plain"); |
| 7184 | } |
| 7185 | |
| 7186 | private CharSequence getLabel(ResolveInfo resolveInfo) { |
| 7187 | return resolveInfo.loadLabel(mPackageManager); |
| 7188 | } |
| 7189 | } |
Nikita Dubrovsky | 05cfcc8 | 2019-10-24 08:57:32 -0700 | [diff] [blame] | 7190 | |
Nikita Dubrovsky | d63f203 | 2019-11-20 14:33:21 -0800 | [diff] [blame] | 7191 | static void logCursor(String location, @Nullable String msgFormat, Object ... msgArgs) { |
Nikita Dubrovsky | 05cfcc8 | 2019-10-24 08:57:32 -0700 | [diff] [blame] | 7192 | if (msgFormat == null) { |
| 7193 | Log.d(TAG, location); |
| 7194 | } else { |
| 7195 | Log.d(TAG, location + ": " + String.format(msgFormat, msgArgs)); |
| 7196 | } |
| 7197 | } |
Gilles Debunne | d88876a | 2012-03-16 17:34:04 -0700 | [diff] [blame] | 7198 | } |