Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.annotation.UiThread; |
| 22 | import android.annotation.WorkerThread; |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 23 | import android.content.Context; |
Petar Šegina | 5ab7bb2 | 2017-09-05 20:48:42 +0100 | [diff] [blame] | 24 | import android.graphics.Canvas; |
Petar Šegina | 91df3f9 | 2017-08-15 16:20:43 +0100 | [diff] [blame] | 25 | import android.graphics.PointF; |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 26 | import android.graphics.RectF; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 27 | import android.os.AsyncTask; |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 28 | import android.os.Build; |
Abodunrinwa Toki | 4cfda0b | 2017-02-28 18:56:47 +0000 | [diff] [blame] | 29 | import android.os.LocaleList; |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 30 | import android.text.Layout; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 31 | import android.text.Selection; |
| 32 | import android.text.Spannable; |
| 33 | import android.text.TextUtils; |
Abodunrinwa Toki | a69950c | 2018-11-29 13:51:56 +0000 | [diff] [blame] | 34 | import android.text.util.Linkify; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 35 | import android.util.Log; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 36 | import android.view.ActionMode; |
Abodunrinwa Toki | f1d9399 | 2018-03-02 13:53:21 +0000 | [diff] [blame] | 37 | import android.view.textclassifier.SelectionEvent; |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 38 | import android.view.textclassifier.SelectionEvent.InvocationMethod; |
Jan Althaus | 5a03094 | 2018-04-04 19:40:38 +0200 | [diff] [blame] | 39 | import android.view.textclassifier.SelectionSessionLogger; |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 40 | import android.view.textclassifier.TextClassification; |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 41 | import android.view.textclassifier.TextClassificationConstants; |
| 42 | import android.view.textclassifier.TextClassificationManager; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 43 | import android.view.textclassifier.TextClassifier; |
| 44 | import android.view.textclassifier.TextSelection; |
| 45 | import android.widget.Editor.SelectionModifierCursorController; |
| 46 | |
Petar Šegina | 91df3f9 | 2017-08-15 16:20:43 +0100 | [diff] [blame] | 47 | import com.android.internal.annotations.VisibleForTesting; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 48 | import com.android.internal.util.Preconditions; |
| 49 | |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 50 | import java.text.BreakIterator; |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 51 | import java.util.ArrayList; |
Petar Šegina | 7c8196f | 2017-09-11 18:03:14 +0100 | [diff] [blame] | 52 | import java.util.Comparator; |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 53 | import java.util.List; |
Abodunrinwa Toki | fdf57ba | 2017-05-02 20:32:23 +0100 | [diff] [blame] | 54 | import java.util.Objects; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 55 | import java.util.function.Consumer; |
Petar Šegina | 7c8196f | 2017-09-11 18:03:14 +0100 | [diff] [blame] | 56 | import java.util.function.Function; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 57 | import java.util.function.Supplier; |
Abodunrinwa Toki | d62a86e | 2017-09-11 18:19:53 +0100 | [diff] [blame] | 58 | import java.util.regex.Pattern; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Helper class for starting selection action mode |
| 62 | * (synchronously without the TextClassifier, asynchronously with the TextClassifier). |
Petar Šegina | 91df3f9 | 2017-08-15 16:20:43 +0100 | [diff] [blame] | 63 | * @hide |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 64 | */ |
| 65 | @UiThread |
Petar Šegina | 91df3f9 | 2017-08-15 16:20:43 +0100 | [diff] [blame] | 66 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
Petar Šegina | ba1b856 | 2017-08-31 18:09:16 +0100 | [diff] [blame] | 67 | public final class SelectionActionModeHelper { |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 68 | |
Jan Althaus | b3513a1 | 2017-09-22 18:26:06 +0200 | [diff] [blame] | 69 | private static final String LOG_TAG = "SelectActionModeHelper"; |
| 70 | |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 71 | private final Editor mEditor; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 72 | private final TextView mTextView; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 73 | private final TextClassificationHelper mTextClassificationHelper; |
| 74 | |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 75 | @Nullable private TextClassification mTextClassification; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 76 | private AsyncTask mTextClassificationAsyncTask; |
| 77 | |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 78 | private final SelectionTracker mSelectionTracker; |
Petar Šegina | 5ab7bb2 | 2017-09-05 20:48:42 +0100 | [diff] [blame] | 79 | |
| 80 | // TODO remove nullable marker once the switch gating the feature gets removed |
| 81 | @Nullable |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 82 | private final SmartSelectSprite mSmartSelectSprite; |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 83 | |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 84 | SelectionActionModeHelper(@NonNull Editor editor) { |
| 85 | mEditor = Preconditions.checkNotNull(editor); |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 86 | mTextView = mEditor.getTextView(); |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 87 | mTextClassificationHelper = new TextClassificationHelper( |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 88 | mTextView.getContext(), |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 89 | mTextView::getTextClassifier, |
Abodunrinwa Toki | 7c1e425 | 2017-09-26 20:09:06 +0100 | [diff] [blame] | 90 | getText(mTextView), |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 91 | 0, 1, mTextView.getTextLocales()); |
Abodunrinwa Toki | d62a86e | 2017-09-11 18:19:53 +0100 | [diff] [blame] | 92 | mSelectionTracker = new SelectionTracker(mTextView); |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 93 | |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 94 | if (getTextClassificationSettings().isSmartSelectionAnimationEnabled()) { |
Petar Šegina | 5ab7bb2 | 2017-09-05 20:48:42 +0100 | [diff] [blame] | 95 | mSmartSelectSprite = new SmartSelectSprite(mTextView.getContext(), |
Jan Althaus | 80620c5 | 2018-02-02 17:39:22 +0100 | [diff] [blame] | 96 | editor.getTextView().mHighlightColor, mTextView::invalidate); |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 97 | } else { |
| 98 | mSmartSelectSprite = null; |
| 99 | } |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 102 | /** |
| 103 | * Starts Selection ActionMode. |
| 104 | */ |
| 105 | public void startSelectionActionModeAsync(boolean adjustSelection) { |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 106 | // Check if the smart selection should run for editable text. |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 107 | adjustSelection &= getTextClassificationSettings().isSmartSelectionEnabled(); |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 108 | |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 109 | mSelectionTracker.onOriginalSelection( |
Abodunrinwa Toki | 7c1e425 | 2017-09-26 20:09:06 +0100 | [diff] [blame] | 110 | getText(mTextView), |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 111 | mTextView.getSelectionStart(), |
Jan Althaus | 92c6dec | 2018-02-02 09:20:14 +0100 | [diff] [blame] | 112 | mTextView.getSelectionEnd(), |
| 113 | false /*isLink*/); |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 114 | cancelAsyncTask(); |
Abodunrinwa Toki | 76b51dc | 2017-07-13 23:37:11 +0100 | [diff] [blame] | 115 | if (skipTextClassification()) { |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 116 | startSelectionActionMode(null); |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 117 | } else { |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 118 | resetTextClassificationHelper(); |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 119 | mTextClassificationAsyncTask = new TextClassificationAsyncTask( |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 120 | mTextView, |
Abodunrinwa Toki | 3521f3f | 2017-09-27 02:38:55 +0100 | [diff] [blame] | 121 | mTextClassificationHelper.getTimeoutDuration(), |
Abodunrinwa Toki | 66c1627 | 2017-05-03 20:22:55 +0100 | [diff] [blame] | 122 | adjustSelection |
| 123 | ? mTextClassificationHelper::suggestSelection |
| 124 | : mTextClassificationHelper::classifyText, |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 125 | mSmartSelectSprite != null |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 126 | ? this::startSelectionActionModeWithSmartSelectAnimation |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 127 | : this::startSelectionActionMode, |
| 128 | mTextClassificationHelper::getOriginalSelection) |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 129 | .execute(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Starts Link ActionMode. |
| 135 | */ |
Richard Ledley | 27db81b | 2018-03-01 12:34:55 +0000 | [diff] [blame] | 136 | public void startLinkActionModeAsync(int start, int end) { |
| 137 | mSelectionTracker.onOriginalSelection(getText(mTextView), start, end, true /*isLink*/); |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 138 | cancelAsyncTask(); |
| 139 | if (skipTextClassification()) { |
| 140 | startLinkActionMode(null); |
| 141 | } else { |
Richard Ledley | 27db81b | 2018-03-01 12:34:55 +0000 | [diff] [blame] | 142 | resetTextClassificationHelper(start, end); |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 143 | mTextClassificationAsyncTask = new TextClassificationAsyncTask( |
| 144 | mTextView, |
| 145 | mTextClassificationHelper.getTimeoutDuration(), |
| 146 | mTextClassificationHelper::classifyText, |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 147 | this::startLinkActionMode, |
| 148 | mTextClassificationHelper::getOriginalSelection) |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 149 | .execute(); |
| 150 | } |
| 151 | } |
| 152 | |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 153 | public void invalidateActionModeAsync() { |
| 154 | cancelAsyncTask(); |
Abodunrinwa Toki | 76b51dc | 2017-07-13 23:37:11 +0100 | [diff] [blame] | 155 | if (skipTextClassification()) { |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 156 | invalidateActionMode(null); |
| 157 | } else { |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 158 | resetTextClassificationHelper(); |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 159 | mTextClassificationAsyncTask = new TextClassificationAsyncTask( |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 160 | mTextView, |
Abodunrinwa Toki | 3521f3f | 2017-09-27 02:38:55 +0100 | [diff] [blame] | 161 | mTextClassificationHelper.getTimeoutDuration(), |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 162 | mTextClassificationHelper::classifyText, |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 163 | this::invalidateActionMode, |
| 164 | mTextClassificationHelper::getOriginalSelection) |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 165 | .execute(); |
| 166 | } |
| 167 | } |
| 168 | |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 169 | public void onSelectionAction(int menuItemId) { |
| 170 | mSelectionTracker.onSelectionAction( |
| 171 | mTextView.getSelectionStart(), mTextView.getSelectionEnd(), |
| 172 | getActionType(menuItemId), mTextClassification); |
| 173 | } |
| 174 | |
| 175 | public void onSelectionDrag() { |
| 176 | mSelectionTracker.onSelectionAction( |
| 177 | mTextView.getSelectionStart(), mTextView.getSelectionEnd(), |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 178 | SelectionEvent.ACTION_DRAG, mTextClassification); |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Abodunrinwa Toki | 78940eb | 2017-09-12 02:58:49 +0100 | [diff] [blame] | 181 | public void onTextChanged(int start, int end) { |
| 182 | mSelectionTracker.onTextChanged(start, end, mTextClassification); |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Abodunrinwa Toki | 8dd3a74 | 2017-05-02 18:44:19 +0100 | [diff] [blame] | 185 | public boolean resetSelection(int textIndex) { |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 186 | if (mSelectionTracker.resetSelection(textIndex, mEditor)) { |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 187 | invalidateActionModeAsync(); |
| 188 | return true; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 189 | } |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 190 | return false; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Nullable |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 194 | public TextClassification getTextClassification() { |
| 195 | return mTextClassification; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 198 | public void onDestroyActionMode() { |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 199 | cancelSmartSelectAnimation(); |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 200 | mSelectionTracker.onSelectionDestroyed(); |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 201 | cancelAsyncTask(); |
| 202 | } |
| 203 | |
Petar Šegina | 5ab7bb2 | 2017-09-05 20:48:42 +0100 | [diff] [blame] | 204 | public void onDraw(final Canvas canvas) { |
Jan Althaus | 80620c5 | 2018-02-02 17:39:22 +0100 | [diff] [blame] | 205 | if (isDrawingHighlight() && mSmartSelectSprite != null) { |
Petar Šegina | 5ab7bb2 | 2017-09-05 20:48:42 +0100 | [diff] [blame] | 206 | mSmartSelectSprite.draw(canvas); |
| 207 | } |
| 208 | } |
| 209 | |
Jan Althaus | 80620c5 | 2018-02-02 17:39:22 +0100 | [diff] [blame] | 210 | public boolean isDrawingHighlight() { |
| 211 | return mSmartSelectSprite != null && mSmartSelectSprite.isAnimationActive(); |
| 212 | } |
| 213 | |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 214 | private TextClassificationConstants getTextClassificationSettings() { |
| 215 | return TextClassificationManager.getSettings(mTextView.getContext()); |
| 216 | } |
| 217 | |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 218 | private void cancelAsyncTask() { |
| 219 | if (mTextClassificationAsyncTask != null) { |
| 220 | mTextClassificationAsyncTask.cancel(true); |
| 221 | mTextClassificationAsyncTask = null; |
| 222 | } |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 223 | mTextClassification = null; |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Abodunrinwa Toki | 76b51dc | 2017-07-13 23:37:11 +0100 | [diff] [blame] | 226 | private boolean skipTextClassification() { |
Abodunrinwa Toki | 76b51dc | 2017-07-13 23:37:11 +0100 | [diff] [blame] | 227 | // No need to make an async call for a no-op TextClassifier. |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 228 | final boolean noOpTextClassifier = mTextView.usesNoOpTextClassifier(); |
Abodunrinwa Toki | 76b51dc | 2017-07-13 23:37:11 +0100 | [diff] [blame] | 229 | // Do not call the TextClassifier if there is no selection. |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 230 | final boolean noSelection = mTextView.getSelectionEnd() == mTextView.getSelectionStart(); |
Abodunrinwa Toki | 76b51dc | 2017-07-13 23:37:11 +0100 | [diff] [blame] | 231 | // Do not call the TextClassifier if this is a password field. |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 232 | final boolean password = mTextView.hasPasswordTransformationMethod() |
| 233 | || TextView.isPasswordInputType(mTextView.getInputType()); |
Abodunrinwa Toki | 76b51dc | 2017-07-13 23:37:11 +0100 | [diff] [blame] | 234 | return noOpTextClassifier || noSelection || password; |
Abodunrinwa Toki | 792d820 | 2017-03-06 23:51:11 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 237 | private void startLinkActionMode(@Nullable SelectionResult result) { |
| 238 | startActionMode(Editor.TextActionMode.TEXT_LINK, result); |
| 239 | } |
| 240 | |
| 241 | private void startSelectionActionMode(@Nullable SelectionResult result) { |
| 242 | startActionMode(Editor.TextActionMode.SELECTION, result); |
| 243 | } |
| 244 | |
| 245 | private void startActionMode( |
| 246 | @Editor.TextActionMode int actionMode, @Nullable SelectionResult result) { |
Abodunrinwa Toki | 7c1e425 | 2017-09-26 20:09:06 +0100 | [diff] [blame] | 247 | final CharSequence text = getText(mTextView); |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 248 | if (result != null && text instanceof Spannable |
Richard Ledley | 27db81b | 2018-03-01 12:34:55 +0000 | [diff] [blame] | 249 | && (mTextView.isTextSelectable() || mTextView.isTextEditable())) { |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 250 | // Do not change the selection if TextClassifier should be dark launched. |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 251 | if (!getTextClassificationSettings().isModelDarkLaunchEnabled()) { |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 252 | Selection.setSelection((Spannable) text, result.mStart, result.mEnd); |
Richard Ledley | 724eff9 | 2017-12-21 10:11:34 +0000 | [diff] [blame] | 253 | mTextView.invalidate(); |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 254 | } |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 255 | mTextClassification = result.mClassification; |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 256 | } else if (result != null && actionMode == Editor.TextActionMode.TEXT_LINK) { |
Richard Ledley | 27db81b | 2018-03-01 12:34:55 +0000 | [diff] [blame] | 257 | mTextClassification = result.mClassification; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 258 | } else { |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 259 | mTextClassification = null; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 260 | } |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 261 | if (mEditor.startActionModeInternal(actionMode)) { |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 262 | final SelectionModifierCursorController controller = mEditor.getSelectionController(); |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 263 | if (controller != null |
| 264 | && (mTextView.isTextSelectable() || mTextView.isTextEditable())) { |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 265 | controller.show(); |
| 266 | } |
Richard Ledley | 724eff9 | 2017-12-21 10:11:34 +0000 | [diff] [blame] | 267 | if (result != null) { |
| 268 | switch (actionMode) { |
| 269 | case Editor.TextActionMode.SELECTION: |
| 270 | mSelectionTracker.onSmartSelection(result); |
| 271 | break; |
| 272 | case Editor.TextActionMode.TEXT_LINK: |
| 273 | mSelectionTracker.onLinkSelected(result); |
| 274 | break; |
| 275 | default: |
| 276 | break; |
| 277 | } |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 278 | } |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 279 | } |
| 280 | mEditor.setRestartActionModeOnNextRefresh(false); |
| 281 | mTextClassificationAsyncTask = null; |
| 282 | } |
| 283 | |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 284 | private void startSelectionActionModeWithSmartSelectAnimation( |
| 285 | @Nullable SelectionResult result) { |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 286 | final Layout layout = mTextView.getLayout(); |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 287 | |
Mihai Popa | 6748ff3 | 2018-05-08 19:18:43 +0100 | [diff] [blame] | 288 | final Runnable onAnimationEndCallback = () -> { |
Mihai Popa | 6e8e27b | 2018-05-25 11:57:17 +0100 | [diff] [blame] | 289 | final SelectionResult startSelectionResult; |
| 290 | if (result != null && result.mStart >= 0 && result.mEnd <= getText(mTextView).length() |
Mihai Popa | 6748ff3 | 2018-05-08 19:18:43 +0100 | [diff] [blame] | 291 | && result.mStart <= result.mEnd) { |
Mihai Popa | 6e8e27b | 2018-05-25 11:57:17 +0100 | [diff] [blame] | 292 | startSelectionResult = result; |
| 293 | } else { |
| 294 | startSelectionResult = null; |
Mihai Popa | 6748ff3 | 2018-05-08 19:18:43 +0100 | [diff] [blame] | 295 | } |
Mihai Popa | 6e8e27b | 2018-05-25 11:57:17 +0100 | [diff] [blame] | 296 | startSelectionActionMode(startSelectionResult); |
Mihai Popa | 6748ff3 | 2018-05-08 19:18:43 +0100 | [diff] [blame] | 297 | }; |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 298 | // TODO do not trigger the animation if the change included only non-printable characters |
| 299 | final boolean didSelectionChange = |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 300 | result != null && (mTextView.getSelectionStart() != result.mStart |
| 301 | || mTextView.getSelectionEnd() != result.mEnd); |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 302 | |
| 303 | if (!didSelectionChange) { |
| 304 | onAnimationEndCallback.run(); |
| 305 | return; |
| 306 | } |
| 307 | |
Petar Šegina | 7c8196f | 2017-09-11 18:03:14 +0100 | [diff] [blame] | 308 | final List<SmartSelectSprite.RectangleWithTextSelectionLayout> selectionRectangles = |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 309 | convertSelectionToRectangles(layout, result.mStart, result.mEnd); |
| 310 | |
Petar Šegina | 91df3f9 | 2017-08-15 16:20:43 +0100 | [diff] [blame] | 311 | final PointF touchPoint = new PointF( |
| 312 | mEditor.getLastUpPositionX(), |
| 313 | mEditor.getLastUpPositionY()); |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 314 | |
Petar Šegina | 91df3f9 | 2017-08-15 16:20:43 +0100 | [diff] [blame] | 315 | final PointF animationStartPoint = |
Petar Šegina | 7c8196f | 2017-09-11 18:03:14 +0100 | [diff] [blame] | 316 | movePointInsideNearestRectangle(touchPoint, selectionRectangles, |
| 317 | SmartSelectSprite.RectangleWithTextSelectionLayout::getRectangle); |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 318 | |
| 319 | mSmartSelectSprite.startAnimation( |
Petar Šegina | 91df3f9 | 2017-08-15 16:20:43 +0100 | [diff] [blame] | 320 | animationStartPoint, |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 321 | selectionRectangles, |
| 322 | onAnimationEndCallback); |
| 323 | } |
| 324 | |
Petar Šegina | 7c8196f | 2017-09-11 18:03:14 +0100 | [diff] [blame] | 325 | private List<SmartSelectSprite.RectangleWithTextSelectionLayout> convertSelectionToRectangles( |
| 326 | final Layout layout, final int start, final int end) { |
| 327 | final List<SmartSelectSprite.RectangleWithTextSelectionLayout> result = new ArrayList<>(); |
Petar Šegina | 7272925 | 2017-08-31 15:25:06 +0100 | [diff] [blame] | 328 | |
Petar Šegina | 7c8196f | 2017-09-11 18:03:14 +0100 | [diff] [blame] | 329 | final Layout.SelectionRectangleConsumer consumer = |
| 330 | (left, top, right, bottom, textSelectionLayout) -> mergeRectangleIntoList( |
| 331 | result, |
| 332 | new RectF(left, top, right, bottom), |
| 333 | SmartSelectSprite.RectangleWithTextSelectionLayout::getRectangle, |
| 334 | r -> new SmartSelectSprite.RectangleWithTextSelectionLayout(r, |
| 335 | textSelectionLayout) |
| 336 | ); |
| 337 | |
| 338 | layout.getSelection(start, end, consumer); |
| 339 | |
| 340 | result.sort(Comparator.comparing( |
| 341 | SmartSelectSprite.RectangleWithTextSelectionLayout::getRectangle, |
| 342 | SmartSelectSprite.RECTANGLE_COMPARATOR)); |
| 343 | |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 344 | return result; |
| 345 | } |
| 346 | |
Abodunrinwa Toki | 3521f3f | 2017-09-27 02:38:55 +0100 | [diff] [blame] | 347 | // TODO: Move public pure functions out of this class and make it package-private. |
Petar Šegina | ba1b856 | 2017-08-31 18:09:16 +0100 | [diff] [blame] | 348 | /** |
Petar Šegina | 7c8196f | 2017-09-11 18:03:14 +0100 | [diff] [blame] | 349 | * Merges a {@link RectF} into an existing list of any objects which contain a rectangle. |
| 350 | * While merging, this method makes sure that: |
Petar Šegina | ba1b856 | 2017-08-31 18:09:16 +0100 | [diff] [blame] | 351 | * |
| 352 | * <ol> |
| 353 | * <li>No rectangle is redundant (contained within a bigger rectangle)</li> |
| 354 | * <li>Rectangles of the same height and vertical position that intersect get merged</li> |
| 355 | * </ol> |
| 356 | * |
Petar Šegina | 7c8196f | 2017-09-11 18:03:14 +0100 | [diff] [blame] | 357 | * @param list the list of rectangles (or other rectangle containers) to merge the new |
| 358 | * rectangle into |
Petar Šegina | ba1b856 | 2017-08-31 18:09:16 +0100 | [diff] [blame] | 359 | * @param candidate the {@link RectF} to merge into the list |
Petar Šegina | 7c8196f | 2017-09-11 18:03:14 +0100 | [diff] [blame] | 360 | * @param extractor a function that can extract a {@link RectF} from an element of the given |
| 361 | * list |
| 362 | * @param packer a function that can wrap the resulting {@link RectF} into an element that |
| 363 | * the list contains |
Petar Šegina | ba1b856 | 2017-08-31 18:09:16 +0100 | [diff] [blame] | 364 | * @hide |
| 365 | */ |
| 366 | @VisibleForTesting |
Petar Šegina | 7c8196f | 2017-09-11 18:03:14 +0100 | [diff] [blame] | 367 | public static <T> void mergeRectangleIntoList(final List<T> list, |
| 368 | final RectF candidate, final Function<T, RectF> extractor, |
| 369 | final Function<RectF, T> packer) { |
Petar Šegina | ba1b856 | 2017-08-31 18:09:16 +0100 | [diff] [blame] | 370 | if (candidate.isEmpty()) { |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | final int elementCount = list.size(); |
| 375 | for (int index = 0; index < elementCount; ++index) { |
Petar Šegina | 7c8196f | 2017-09-11 18:03:14 +0100 | [diff] [blame] | 376 | final RectF existingRectangle = extractor.apply(list.get(index)); |
Petar Šegina | ba1b856 | 2017-08-31 18:09:16 +0100 | [diff] [blame] | 377 | if (existingRectangle.contains(candidate)) { |
| 378 | return; |
| 379 | } |
| 380 | if (candidate.contains(existingRectangle)) { |
| 381 | existingRectangle.setEmpty(); |
| 382 | continue; |
| 383 | } |
| 384 | |
| 385 | final boolean rectanglesContinueEachOther = candidate.left == existingRectangle.right |
| 386 | || candidate.right == existingRectangle.left; |
| 387 | final boolean canMerge = candidate.top == existingRectangle.top |
| 388 | && candidate.bottom == existingRectangle.bottom |
| 389 | && (RectF.intersects(candidate, existingRectangle) |
| 390 | || rectanglesContinueEachOther); |
| 391 | |
| 392 | if (canMerge) { |
| 393 | candidate.union(existingRectangle); |
| 394 | existingRectangle.setEmpty(); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | for (int index = elementCount - 1; index >= 0; --index) { |
Petar Šegina | 7c8196f | 2017-09-11 18:03:14 +0100 | [diff] [blame] | 399 | final RectF rectangle = extractor.apply(list.get(index)); |
| 400 | if (rectangle.isEmpty()) { |
Petar Šegina | ba1b856 | 2017-08-31 18:09:16 +0100 | [diff] [blame] | 401 | list.remove(index); |
| 402 | } |
| 403 | } |
| 404 | |
Petar Šegina | 7c8196f | 2017-09-11 18:03:14 +0100 | [diff] [blame] | 405 | list.add(packer.apply(candidate)); |
Petar Šegina | ba1b856 | 2017-08-31 18:09:16 +0100 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | |
Petar Šegina | 91df3f9 | 2017-08-15 16:20:43 +0100 | [diff] [blame] | 409 | /** @hide */ |
| 410 | @VisibleForTesting |
Petar Šegina | 7c8196f | 2017-09-11 18:03:14 +0100 | [diff] [blame] | 411 | public static <T> PointF movePointInsideNearestRectangle(final PointF point, |
| 412 | final List<T> list, final Function<T, RectF> extractor) { |
Petar Šegina | 91df3f9 | 2017-08-15 16:20:43 +0100 | [diff] [blame] | 413 | float bestX = -1; |
| 414 | float bestY = -1; |
| 415 | double bestDistance = Double.MAX_VALUE; |
| 416 | |
Petar Šegina | 7c8196f | 2017-09-11 18:03:14 +0100 | [diff] [blame] | 417 | final int elementCount = list.size(); |
Petar Šegina | ba1b856 | 2017-08-31 18:09:16 +0100 | [diff] [blame] | 418 | for (int index = 0; index < elementCount; ++index) { |
Petar Šegina | 7c8196f | 2017-09-11 18:03:14 +0100 | [diff] [blame] | 419 | final RectF rectangle = extractor.apply(list.get(index)); |
Petar Šegina | 91df3f9 | 2017-08-15 16:20:43 +0100 | [diff] [blame] | 420 | final float candidateY = rectangle.centerY(); |
| 421 | final float candidateX; |
| 422 | |
| 423 | if (point.x > rectangle.right) { |
| 424 | candidateX = rectangle.right; |
| 425 | } else if (point.x < rectangle.left) { |
| 426 | candidateX = rectangle.left; |
| 427 | } else { |
| 428 | candidateX = point.x; |
| 429 | } |
| 430 | |
| 431 | final double candidateDistance = Math.pow(point.x - candidateX, 2) |
| 432 | + Math.pow(point.y - candidateY, 2); |
| 433 | |
| 434 | if (candidateDistance < bestDistance) { |
| 435 | bestX = candidateX; |
| 436 | bestY = candidateY; |
| 437 | bestDistance = candidateDistance; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | return new PointF(bestX, bestY); |
| 442 | } |
| 443 | |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 444 | private void invalidateActionMode(@Nullable SelectionResult result) { |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 445 | cancelSmartSelectAnimation(); |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 446 | mTextClassification = result != null ? result.mClassification : null; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 447 | final ActionMode actionMode = mEditor.getTextActionMode(); |
| 448 | if (actionMode != null) { |
| 449 | actionMode.invalidate(); |
| 450 | } |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 451 | mSelectionTracker.onSelectionUpdated( |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 452 | mTextView.getSelectionStart(), mTextView.getSelectionEnd(), mTextClassification); |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 453 | mTextClassificationAsyncTask = null; |
| 454 | } |
| 455 | |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 456 | private void resetTextClassificationHelper(int selectionStart, int selectionEnd) { |
| 457 | if (selectionStart < 0 || selectionEnd < 0) { |
| 458 | // Use selection indices |
| 459 | selectionStart = mTextView.getSelectionStart(); |
| 460 | selectionEnd = mTextView.getSelectionEnd(); |
| 461 | } |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 462 | mTextClassificationHelper.init( |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 463 | mTextView::getTextClassifier, |
Abodunrinwa Toki | 7c1e425 | 2017-09-26 20:09:06 +0100 | [diff] [blame] | 464 | getText(mTextView), |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 465 | selectionStart, selectionEnd, |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 466 | mTextView.getTextLocales()); |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 467 | } |
| 468 | |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 469 | private void resetTextClassificationHelper() { |
| 470 | resetTextClassificationHelper(-1, -1); |
| 471 | } |
| 472 | |
Petar Šegina | 701ba33 | 2017-08-01 17:57:26 +0100 | [diff] [blame] | 473 | private void cancelSmartSelectAnimation() { |
| 474 | if (mSmartSelectSprite != null) { |
| 475 | mSmartSelectSprite.cancelAnimation(); |
| 476 | } |
| 477 | } |
| 478 | |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 479 | /** |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 480 | * Tracks and logs smart selection changes. |
| 481 | * It is important to trigger this object's methods at the appropriate event so that it tracks |
| 482 | * smart selection events appropriately. |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 483 | */ |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 484 | private static final class SelectionTracker { |
| 485 | |
Abodunrinwa Toki | d62a86e | 2017-09-11 18:19:53 +0100 | [diff] [blame] | 486 | private final TextView mTextView; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 487 | private SelectionMetricsLogger mLogger; |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 488 | |
| 489 | private int mOriginalStart; |
| 490 | private int mOriginalEnd; |
| 491 | private int mSelectionStart; |
| 492 | private int mSelectionEnd; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 493 | private boolean mAllowReset; |
Jan Althaus | b3513a1 | 2017-09-22 18:26:06 +0200 | [diff] [blame] | 494 | private final LogAbandonRunnable mDelayedLogAbandon = new LogAbandonRunnable(); |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 495 | |
Abodunrinwa Toki | d62a86e | 2017-09-11 18:19:53 +0100 | [diff] [blame] | 496 | SelectionTracker(TextView textView) { |
| 497 | mTextView = Preconditions.checkNotNull(textView); |
| 498 | mLogger = new SelectionMetricsLogger(textView); |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /** |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 502 | * Called when the original selection happens, before smart selection is triggered. |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 503 | */ |
Jan Althaus | 92c6dec | 2018-02-02 09:20:14 +0100 | [diff] [blame] | 504 | public void onOriginalSelection( |
| 505 | CharSequence text, int selectionStart, int selectionEnd, boolean isLink) { |
Jan Althaus | b3513a1 | 2017-09-22 18:26:06 +0200 | [diff] [blame] | 506 | // If we abandoned a selection and created a new one very shortly after, we may still |
| 507 | // have a pending request to log ABANDON, which we flush here. |
| 508 | mDelayedLogAbandon.flush(); |
| 509 | |
Abodunrinwa Toki | 78940eb | 2017-09-12 02:58:49 +0100 | [diff] [blame] | 510 | mOriginalStart = mSelectionStart = selectionStart; |
| 511 | mOriginalEnd = mSelectionEnd = selectionEnd; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 512 | mAllowReset = false; |
Abodunrinwa Toki | d62a86e | 2017-09-11 18:19:53 +0100 | [diff] [blame] | 513 | maybeInvalidateLogger(); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 514 | mLogger.logSelectionStarted(mTextView.getTextClassificationSession(), |
| 515 | text, selectionStart, |
Jan Althaus | 92c6dec | 2018-02-02 09:20:14 +0100 | [diff] [blame] | 516 | isLink ? SelectionEvent.INVOCATION_LINK : SelectionEvent.INVOCATION_MANUAL); |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 519 | /** |
Abodunrinwa Toki | e78ac52 | 2017-05-23 14:51:22 +0100 | [diff] [blame] | 520 | * Called when selection action mode is started and the results come from a classifier. |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 521 | */ |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 522 | public void onSmartSelection(SelectionResult result) { |
Richard Ledley | 724eff9 | 2017-12-21 10:11:34 +0000 | [diff] [blame] | 523 | onClassifiedSelection(result); |
| 524 | mLogger.logSelectionModified( |
| 525 | result.mStart, result.mEnd, result.mClassification, result.mSelection); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * Called when link action mode is started and the classification comes from a classifier. |
| 530 | */ |
| 531 | public void onLinkSelected(SelectionResult result) { |
| 532 | onClassifiedSelection(result); |
| 533 | // TODO: log (b/70246800) |
| 534 | } |
| 535 | |
| 536 | private void onClassifiedSelection(SelectionResult result) { |
Abodunrinwa Toki | 78940eb | 2017-09-12 02:58:49 +0100 | [diff] [blame] | 537 | if (isSelectionStarted()) { |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 538 | mSelectionStart = result.mStart; |
| 539 | mSelectionEnd = result.mEnd; |
| 540 | mAllowReset = mSelectionStart != mOriginalStart || mSelectionEnd != mOriginalEnd; |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 541 | } |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 544 | /** |
| 545 | * Called when selection bounds change. |
| 546 | */ |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 547 | public void onSelectionUpdated( |
| 548 | int selectionStart, int selectionEnd, |
| 549 | @Nullable TextClassification classification) { |
Abodunrinwa Toki | 78940eb | 2017-09-12 02:58:49 +0100 | [diff] [blame] | 550 | if (isSelectionStarted()) { |
| 551 | mSelectionStart = selectionStart; |
| 552 | mSelectionEnd = selectionEnd; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 553 | mAllowReset = false; |
| 554 | mLogger.logSelectionModified(selectionStart, selectionEnd, classification, null); |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 555 | } |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 558 | /** |
| 559 | * Called when the selection action mode is destroyed. |
| 560 | */ |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 561 | public void onSelectionDestroyed() { |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 562 | mAllowReset = false; |
Abodunrinwa Toki | 78940eb | 2017-09-12 02:58:49 +0100 | [diff] [blame] | 563 | // Wait a few ms to see if the selection was destroyed because of a text change event. |
Jan Althaus | b3513a1 | 2017-09-22 18:26:06 +0200 | [diff] [blame] | 564 | mDelayedLogAbandon.schedule(100 /* ms */); |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 567 | /** |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 568 | * Called when an action is taken on a smart selection. |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 569 | */ |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 570 | public void onSelectionAction( |
| 571 | int selectionStart, int selectionEnd, |
| 572 | @SelectionEvent.ActionType int action, |
| 573 | @Nullable TextClassification classification) { |
Abodunrinwa Toki | 78940eb | 2017-09-12 02:58:49 +0100 | [diff] [blame] | 574 | if (isSelectionStarted()) { |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 575 | mAllowReset = false; |
| 576 | mLogger.logSelectionAction(selectionStart, selectionEnd, action, classification); |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Returns true if the current smart selection should be reset to normal selection based on |
| 582 | * information that has been recorded about the original selection and the smart selection. |
| 583 | * The expected UX here is to allow the user to select a word inside of the smart selection |
| 584 | * on a single tap. |
| 585 | */ |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 586 | public boolean resetSelection(int textIndex, Editor editor) { |
| 587 | final TextView textView = editor.getTextView(); |
Abodunrinwa Toki | 78940eb | 2017-09-12 02:58:49 +0100 | [diff] [blame] | 588 | if (isSelectionStarted() |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 589 | && mAllowReset |
Abodunrinwa Toki | 8dd3a74 | 2017-05-02 18:44:19 +0100 | [diff] [blame] | 590 | && textIndex >= mSelectionStart && textIndex <= mSelectionEnd |
Abodunrinwa Toki | 7c1e425 | 2017-09-26 20:09:06 +0100 | [diff] [blame] | 591 | && getText(textView) instanceof Spannable) { |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 592 | mAllowReset = false; |
| 593 | boolean selected = editor.selectCurrentWord(); |
| 594 | if (selected) { |
Abodunrinwa Toki | 78940eb | 2017-09-12 02:58:49 +0100 | [diff] [blame] | 595 | mSelectionStart = editor.getTextView().getSelectionStart(); |
| 596 | mSelectionEnd = editor.getTextView().getSelectionEnd(); |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 597 | mLogger.logSelectionAction( |
| 598 | textView.getSelectionStart(), textView.getSelectionEnd(), |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 599 | SelectionEvent.ACTION_RESET, null /* classification */); |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 600 | } |
| 601 | return selected; |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 602 | } |
| 603 | return false; |
| 604 | } |
Abodunrinwa Toki | e78ac52 | 2017-05-23 14:51:22 +0100 | [diff] [blame] | 605 | |
Abodunrinwa Toki | 78940eb | 2017-09-12 02:58:49 +0100 | [diff] [blame] | 606 | public void onTextChanged(int start, int end, TextClassification classification) { |
| 607 | if (isSelectionStarted() && start == mSelectionStart && end == mSelectionEnd) { |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 608 | onSelectionAction(start, end, SelectionEvent.ACTION_OVERTYPE, classification); |
Abodunrinwa Toki | 78940eb | 2017-09-12 02:58:49 +0100 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
Abodunrinwa Toki | d62a86e | 2017-09-11 18:19:53 +0100 | [diff] [blame] | 612 | private void maybeInvalidateLogger() { |
| 613 | if (mLogger.isEditTextLogger() != mTextView.isTextEditable()) { |
| 614 | mLogger = new SelectionMetricsLogger(mTextView); |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 615 | } |
| 616 | } |
Abodunrinwa Toki | 78940eb | 2017-09-12 02:58:49 +0100 | [diff] [blame] | 617 | |
| 618 | private boolean isSelectionStarted() { |
| 619 | return mSelectionStart >= 0 && mSelectionEnd >= 0 && mSelectionStart != mSelectionEnd; |
| 620 | } |
Jan Althaus | b3513a1 | 2017-09-22 18:26:06 +0200 | [diff] [blame] | 621 | |
| 622 | /** A helper for keeping track of pending abandon logging requests. */ |
| 623 | private final class LogAbandonRunnable implements Runnable { |
| 624 | private boolean mIsPending; |
| 625 | |
| 626 | /** Schedules an abandon to be logged with the given delay. Flush if necessary. */ |
| 627 | void schedule(int delayMillis) { |
| 628 | if (mIsPending) { |
| 629 | Log.e(LOG_TAG, "Force flushing abandon due to new scheduling request"); |
| 630 | flush(); |
| 631 | } |
| 632 | mIsPending = true; |
| 633 | mTextView.postDelayed(this, delayMillis); |
| 634 | } |
| 635 | |
| 636 | /** If there is a pending log request, execute it now. */ |
| 637 | void flush() { |
| 638 | mTextView.removeCallbacks(this); |
| 639 | run(); |
| 640 | } |
| 641 | |
| 642 | @Override |
| 643 | public void run() { |
| 644 | if (mIsPending) { |
| 645 | mLogger.logSelectionAction( |
| 646 | mSelectionStart, mSelectionEnd, |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 647 | SelectionEvent.ACTION_ABANDON, null /* classification */); |
Jan Althaus | b3513a1 | 2017-09-22 18:26:06 +0200 | [diff] [blame] | 648 | mSelectionStart = mSelectionEnd = -1; |
Abodunrinwa Toki | f299fc0 | 2018-05-17 17:36:25 +0100 | [diff] [blame] | 649 | mLogger.endTextClassificationSession(); |
Jan Althaus | b3513a1 | 2017-09-22 18:26:06 +0200 | [diff] [blame] | 650 | mIsPending = false; |
| 651 | } |
| 652 | } |
| 653 | } |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | // TODO: Write tests |
| 657 | /** |
| 658 | * Metrics logging helper. |
| 659 | * |
| 660 | * This logger logs selection by word indices. The initial (start) single word selection is |
| 661 | * logged at [0, 1) -- end index is exclusive. Other word indices are logged relative to the |
| 662 | * initial single word selection. |
| 663 | * e.g. New York city, NY. Suppose the initial selection is "York" in |
| 664 | * "New York city, NY", then "York" is at [0, 1), "New" is at [-1, 0], and "city" is at [1, 2). |
| 665 | * "New York" is at [-1, 1). |
| 666 | * Part selection of a word e.g. "or" is counted as selecting the |
| 667 | * entire word i.e. equivalent to "York", and each special character is counted as a word, e.g. |
| 668 | * "," is at [2, 3). Whitespaces are ignored. |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 669 | * |
| 670 | * NOTE that the definition of a word is defined by the TextClassifier's Logger's token |
| 671 | * iterator. |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 672 | */ |
| 673 | private static final class SelectionMetricsLogger { |
| 674 | |
| 675 | private static final String LOG_TAG = "SelectionMetricsLogger"; |
Abodunrinwa Toki | d62a86e | 2017-09-11 18:19:53 +0100 | [diff] [blame] | 676 | private static final Pattern PATTERN_WHITESPACE = Pattern.compile("\\s+"); |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 677 | |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 678 | private final boolean mEditTextLogger; |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 679 | private final BreakIterator mTokenIterator; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 680 | |
| 681 | @Nullable private TextClassifier mClassificationSession; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 682 | private int mStartIndex; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 683 | private String mText; |
| 684 | |
Abodunrinwa Toki | d62a86e | 2017-09-11 18:19:53 +0100 | [diff] [blame] | 685 | SelectionMetricsLogger(TextView textView) { |
| 686 | Preconditions.checkNotNull(textView); |
Abodunrinwa Toki | d62a86e | 2017-09-11 18:19:53 +0100 | [diff] [blame] | 687 | mEditTextLogger = textView.isTextEditable(); |
Jan Althaus | 5a03094 | 2018-04-04 19:40:38 +0200 | [diff] [blame] | 688 | mTokenIterator = SelectionSessionLogger.getTokenIterator(textView.getTextLocale()); |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 691 | @TextClassifier.WidgetType |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 692 | private static String getWidetType(TextView textView) { |
| 693 | if (textView.isTextEditable()) { |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 694 | return TextClassifier.WIDGET_TYPE_EDITTEXT; |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 695 | } |
| 696 | if (textView.isTextSelectable()) { |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 697 | return TextClassifier.WIDGET_TYPE_TEXTVIEW; |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 698 | } |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 699 | return TextClassifier.WIDGET_TYPE_UNSELECTABLE_TEXTVIEW; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 700 | } |
| 701 | |
Jan Althaus | 92c6dec | 2018-02-02 09:20:14 +0100 | [diff] [blame] | 702 | public void logSelectionStarted( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 703 | TextClassifier classificationSession, |
Jan Althaus | 92c6dec | 2018-02-02 09:20:14 +0100 | [diff] [blame] | 704 | CharSequence text, int index, |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 705 | @InvocationMethod int invocationMethod) { |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 706 | try { |
| 707 | Preconditions.checkNotNull(text); |
| 708 | Preconditions.checkArgumentInRange(index, 0, text.length(), "index"); |
| 709 | if (mText == null || !mText.contentEquals(text)) { |
| 710 | mText = text.toString(); |
| 711 | } |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 712 | mTokenIterator.setText(mText); |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 713 | mStartIndex = index; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 714 | mClassificationSession = classificationSession; |
Abodunrinwa Toki | f299fc0 | 2018-05-17 17:36:25 +0100 | [diff] [blame] | 715 | if (hasActiveClassificationSession()) { |
| 716 | mClassificationSession.onSelectionEvent( |
| 717 | SelectionEvent.createSelectionStartedEvent(invocationMethod, 0)); |
| 718 | } |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 719 | } catch (Exception e) { |
| 720 | // Avoid crashes due to logging. |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 721 | Log.e(LOG_TAG, "" + e.getMessage(), e); |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | |
| 725 | public void logSelectionModified(int start, int end, |
| 726 | @Nullable TextClassification classification, @Nullable TextSelection selection) { |
| 727 | try { |
Abodunrinwa Toki | f299fc0 | 2018-05-17 17:36:25 +0100 | [diff] [blame] | 728 | if (hasActiveClassificationSession()) { |
| 729 | Preconditions.checkArgumentInRange(start, 0, mText.length(), "start"); |
| 730 | Preconditions.checkArgumentInRange(end, start, mText.length(), "end"); |
| 731 | int[] wordIndices = getWordDelta(start, end); |
| 732 | if (selection != null) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 733 | mClassificationSession.onSelectionEvent( |
| 734 | SelectionEvent.createSelectionModifiedEvent( |
| 735 | wordIndices[0], wordIndices[1], selection)); |
Abodunrinwa Toki | f299fc0 | 2018-05-17 17:36:25 +0100 | [diff] [blame] | 736 | } else if (classification != null) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 737 | mClassificationSession.onSelectionEvent( |
| 738 | SelectionEvent.createSelectionModifiedEvent( |
| 739 | wordIndices[0], wordIndices[1], classification)); |
Abodunrinwa Toki | f299fc0 | 2018-05-17 17:36:25 +0100 | [diff] [blame] | 740 | } else { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 741 | mClassificationSession.onSelectionEvent( |
| 742 | SelectionEvent.createSelectionModifiedEvent( |
| 743 | wordIndices[0], wordIndices[1])); |
| 744 | } |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 745 | } |
| 746 | } catch (Exception e) { |
| 747 | // Avoid crashes due to logging. |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 748 | Log.e(LOG_TAG, "" + e.getMessage(), e); |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
| 752 | public void logSelectionAction( |
| 753 | int start, int end, |
| 754 | @SelectionEvent.ActionType int action, |
| 755 | @Nullable TextClassification classification) { |
| 756 | try { |
Abodunrinwa Toki | f299fc0 | 2018-05-17 17:36:25 +0100 | [diff] [blame] | 757 | if (hasActiveClassificationSession()) { |
| 758 | Preconditions.checkArgumentInRange(start, 0, mText.length(), "start"); |
| 759 | Preconditions.checkArgumentInRange(end, start, mText.length(), "end"); |
| 760 | int[] wordIndices = getWordDelta(start, end); |
| 761 | if (classification != null) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 762 | mClassificationSession.onSelectionEvent( |
| 763 | SelectionEvent.createSelectionActionEvent( |
Abodunrinwa Toki | f299fc0 | 2018-05-17 17:36:25 +0100 | [diff] [blame] | 764 | wordIndices[0], wordIndices[1], action, |
| 765 | classification)); |
| 766 | } else { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 767 | mClassificationSession.onSelectionEvent( |
| 768 | SelectionEvent.createSelectionActionEvent( |
| 769 | wordIndices[0], wordIndices[1], action)); |
| 770 | } |
Abodunrinwa Toki | f299fc0 | 2018-05-17 17:36:25 +0100 | [diff] [blame] | 771 | if (SelectionEvent.isTerminal(action)) { |
| 772 | endTextClassificationSession(); |
| 773 | } |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 774 | } |
| 775 | } catch (Exception e) { |
| 776 | // Avoid crashes due to logging. |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 777 | Log.e(LOG_TAG, "" + e.getMessage(), e); |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
| 781 | public boolean isEditTextLogger() { |
| 782 | return mEditTextLogger; |
| 783 | } |
| 784 | |
Abodunrinwa Toki | f299fc0 | 2018-05-17 17:36:25 +0100 | [diff] [blame] | 785 | public void endTextClassificationSession() { |
| 786 | if (hasActiveClassificationSession()) { |
| 787 | mClassificationSession.destroy(); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | private boolean hasActiveClassificationSession() { |
| 792 | return mClassificationSession != null && !mClassificationSession.isDestroyed(); |
| 793 | } |
| 794 | |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 795 | private int[] getWordDelta(int start, int end) { |
| 796 | int[] wordIndices = new int[2]; |
| 797 | |
| 798 | if (start == mStartIndex) { |
| 799 | wordIndices[0] = 0; |
| 800 | } else if (start < mStartIndex) { |
| 801 | wordIndices[0] = -countWordsForward(start); |
| 802 | } else { // start > mStartIndex |
Abodunrinwa Toki | d62a86e | 2017-09-11 18:19:53 +0100 | [diff] [blame] | 803 | wordIndices[0] = countWordsBackward(start); |
| 804 | |
| 805 | // For the selection start index, avoid counting a partial word backwards. |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 806 | if (!mTokenIterator.isBoundary(start) |
Abodunrinwa Toki | d62a86e | 2017-09-11 18:19:53 +0100 | [diff] [blame] | 807 | && !isWhitespace( |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 808 | mTokenIterator.preceding(start), |
| 809 | mTokenIterator.following(start))) { |
Abodunrinwa Toki | d62a86e | 2017-09-11 18:19:53 +0100 | [diff] [blame] | 810 | // We counted a partial word. Remove it. |
| 811 | wordIndices[0]--; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
| 815 | if (end == mStartIndex) { |
| 816 | wordIndices[1] = 0; |
| 817 | } else if (end < mStartIndex) { |
| 818 | wordIndices[1] = -countWordsForward(end); |
| 819 | } else { // end > mStartIndex |
| 820 | wordIndices[1] = countWordsBackward(end); |
| 821 | } |
| 822 | |
| 823 | return wordIndices; |
| 824 | } |
| 825 | |
| 826 | private int countWordsBackward(int from) { |
| 827 | Preconditions.checkArgument(from >= mStartIndex); |
| 828 | int wordCount = 0; |
| 829 | int offset = from; |
| 830 | while (offset > mStartIndex) { |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 831 | int start = mTokenIterator.preceding(offset); |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 832 | if (!isWhitespace(start, offset)) { |
| 833 | wordCount++; |
| 834 | } |
| 835 | offset = start; |
| 836 | } |
| 837 | return wordCount; |
| 838 | } |
| 839 | |
| 840 | private int countWordsForward(int from) { |
| 841 | Preconditions.checkArgument(from <= mStartIndex); |
| 842 | int wordCount = 0; |
| 843 | int offset = from; |
| 844 | while (offset < mStartIndex) { |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 845 | int end = mTokenIterator.following(offset); |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 846 | if (!isWhitespace(offset, end)) { |
| 847 | wordCount++; |
| 848 | } |
| 849 | offset = end; |
| 850 | } |
| 851 | return wordCount; |
| 852 | } |
| 853 | |
| 854 | private boolean isWhitespace(int start, int end) { |
Abodunrinwa Toki | d62a86e | 2017-09-11 18:19:53 +0100 | [diff] [blame] | 855 | return PATTERN_WHITESPACE.matcher(mText.substring(start, end)).matches(); |
Abodunrinwa Toki | e78ac52 | 2017-05-23 14:51:22 +0100 | [diff] [blame] | 856 | } |
Abodunrinwa Toki | 89ba5fb | 2017-02-23 02:52:05 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | /** |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 860 | * AsyncTask for running a query on a background thread and returning the result on the |
| 861 | * UiThread. The AsyncTask times out after a specified time, returning a null result if the |
| 862 | * query has not yet returned. |
| 863 | */ |
| 864 | private static final class TextClassificationAsyncTask |
| 865 | extends AsyncTask<Void, Void, SelectionResult> { |
| 866 | |
| 867 | private final int mTimeOutDuration; |
| 868 | private final Supplier<SelectionResult> mSelectionResultSupplier; |
| 869 | private final Consumer<SelectionResult> mSelectionResultCallback; |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 870 | private final Supplier<SelectionResult> mTimeOutResultSupplier; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 871 | private final TextView mTextView; |
| 872 | private final String mOriginalText; |
| 873 | |
| 874 | /** |
| 875 | * @param textView the TextView |
| 876 | * @param timeOut time in milliseconds to timeout the query if it has not completed |
| 877 | * @param selectionResultSupplier fetches the selection results. Runs on a background thread |
| 878 | * @param selectionResultCallback receives the selection results. Runs on the UiThread |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 879 | * @param timeOutResultSupplier default result if the task times out |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 880 | */ |
| 881 | TextClassificationAsyncTask( |
| 882 | @NonNull TextView textView, int timeOut, |
| 883 | @NonNull Supplier<SelectionResult> selectionResultSupplier, |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 884 | @NonNull Consumer<SelectionResult> selectionResultCallback, |
| 885 | @NonNull Supplier<SelectionResult> timeOutResultSupplier) { |
Makoto Onuki | 1488a3a | 2017-05-24 12:25:46 -0700 | [diff] [blame] | 886 | super(textView != null ? textView.getHandler() : null); |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 887 | mTextView = Preconditions.checkNotNull(textView); |
| 888 | mTimeOutDuration = timeOut; |
| 889 | mSelectionResultSupplier = Preconditions.checkNotNull(selectionResultSupplier); |
| 890 | mSelectionResultCallback = Preconditions.checkNotNull(selectionResultCallback); |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 891 | mTimeOutResultSupplier = Preconditions.checkNotNull(timeOutResultSupplier); |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 892 | // Make a copy of the original text. |
Abodunrinwa Toki | 7c1e425 | 2017-09-26 20:09:06 +0100 | [diff] [blame] | 893 | mOriginalText = getText(mTextView).toString(); |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | @Override |
| 897 | @WorkerThread |
| 898 | protected SelectionResult doInBackground(Void... params) { |
| 899 | final Runnable onTimeOut = this::onTimeOut; |
| 900 | mTextView.postDelayed(onTimeOut, mTimeOutDuration); |
| 901 | final SelectionResult result = mSelectionResultSupplier.get(); |
| 902 | mTextView.removeCallbacks(onTimeOut); |
| 903 | return result; |
| 904 | } |
| 905 | |
| 906 | @Override |
| 907 | @UiThread |
| 908 | protected void onPostExecute(SelectionResult result) { |
Abodunrinwa Toki | 7c1e425 | 2017-09-26 20:09:06 +0100 | [diff] [blame] | 909 | result = TextUtils.equals(mOriginalText, getText(mTextView)) ? result : null; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 910 | mSelectionResultCallback.accept(result); |
| 911 | } |
| 912 | |
| 913 | private void onTimeOut() { |
| 914 | if (getStatus() == Status.RUNNING) { |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 915 | onPostExecute(mTimeOutResultSupplier.get()); |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 916 | } |
| 917 | cancel(true); |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | /** |
| 922 | * Helper class for querying the TextClassifier. |
| 923 | * It trims text so that only text necessary to provide context of the selected text is |
| 924 | * sent to the TextClassifier. |
| 925 | */ |
| 926 | private static final class TextClassificationHelper { |
| 927 | |
Abodunrinwa Toki | d2d1399 | 2017-03-24 21:43:13 +0000 | [diff] [blame] | 928 | private static final int TRIM_DELTA = 120; // characters |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 929 | |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 930 | private final Context mContext; |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 931 | private Supplier<TextClassifier> mTextClassifier; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 932 | |
| 933 | /** The original TextView text. **/ |
| 934 | private String mText; |
| 935 | /** Start index relative to mText. */ |
| 936 | private int mSelectionStart; |
| 937 | /** End index relative to mText. */ |
| 938 | private int mSelectionEnd; |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 939 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 940 | @Nullable |
| 941 | private LocaleList mDefaultLocales; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 942 | |
| 943 | /** Trimmed text starting from mTrimStart in mText. */ |
| 944 | private CharSequence mTrimmedText; |
| 945 | /** Index indicating the start of mTrimmedText in mText. */ |
| 946 | private int mTrimStart; |
| 947 | /** Start index relative to mTrimmedText */ |
| 948 | private int mRelativeStart; |
| 949 | /** End index relative to mTrimmedText */ |
| 950 | private int mRelativeEnd; |
| 951 | |
Abodunrinwa Toki | fdf57ba | 2017-05-02 20:32:23 +0100 | [diff] [blame] | 952 | /** Information about the last classified text to avoid re-running a query. */ |
| 953 | private CharSequence mLastClassificationText; |
| 954 | private int mLastClassificationSelectionStart; |
| 955 | private int mLastClassificationSelectionEnd; |
| 956 | private LocaleList mLastClassificationLocales; |
| 957 | private SelectionResult mLastClassificationResult; |
| 958 | |
Abodunrinwa Toki | 3521f3f | 2017-09-27 02:38:55 +0100 | [diff] [blame] | 959 | /** Whether the TextClassifier has been initialized. */ |
| 960 | private boolean mHot; |
| 961 | |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 962 | TextClassificationHelper(Context context, Supplier<TextClassifier> textClassifier, |
Abodunrinwa Toki | 4cfda0b | 2017-02-28 18:56:47 +0000 | [diff] [blame] | 963 | CharSequence text, int selectionStart, int selectionEnd, LocaleList locales) { |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 964 | init(textClassifier, text, selectionStart, selectionEnd, locales); |
| 965 | mContext = Preconditions.checkNotNull(context); |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | @UiThread |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 969 | public void init(Supplier<TextClassifier> textClassifier, CharSequence text, |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 970 | int selectionStart, int selectionEnd, LocaleList locales) { |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 971 | mTextClassifier = Preconditions.checkNotNull(textClassifier); |
| 972 | mText = Preconditions.checkNotNull(text).toString(); |
Abodunrinwa Toki | 08925e6 | 2017-05-12 13:48:50 +0100 | [diff] [blame] | 973 | mLastClassificationText = null; // invalidate. |
Abodunrinwa Toki | 792d820 | 2017-03-06 23:51:11 +0000 | [diff] [blame] | 974 | Preconditions.checkArgument(selectionEnd > selectionStart); |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 975 | mSelectionStart = selectionStart; |
| 976 | mSelectionEnd = selectionEnd; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 977 | mDefaultLocales = locales; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | @WorkerThread |
| 981 | public SelectionResult classifyText() { |
Abodunrinwa Toki | 3521f3f | 2017-09-27 02:38:55 +0100 | [diff] [blame] | 982 | mHot = true; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 983 | return performClassification(null /* selection */); |
| 984 | } |
| 985 | |
| 986 | @WorkerThread |
| 987 | public SelectionResult suggestSelection() { |
Abodunrinwa Toki | 3521f3f | 2017-09-27 02:38:55 +0100 | [diff] [blame] | 988 | mHot = true; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 989 | trimText(); |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 990 | final TextSelection selection; |
Jeff Sharkey | aa1a911 | 2018-04-10 15:18:12 -0600 | [diff] [blame] | 991 | if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 992 | final TextSelection.Request request = new TextSelection.Request.Builder( |
| 993 | mTrimmedText, mRelativeStart, mRelativeEnd) |
| 994 | .setDefaultLocales(mDefaultLocales) |
| 995 | .setDarkLaunchAllowed(true) |
| 996 | .build(); |
| 997 | selection = mTextClassifier.get().suggestSelection(request); |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 998 | } else { |
| 999 | // Use old APIs. |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 1000 | selection = mTextClassifier.get().suggestSelection( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 1001 | mTrimmedText, mRelativeStart, mRelativeEnd, mDefaultLocales); |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 1002 | } |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 1003 | // Do not classify new selection boundaries if TextClassifier should be dark launched. |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 1004 | if (!isDarkLaunchEnabled()) { |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 1005 | mSelectionStart = Math.max(0, selection.getSelectionStartIndex() + mTrimStart); |
| 1006 | mSelectionEnd = Math.min( |
| 1007 | mText.length(), selection.getSelectionEndIndex() + mTrimStart); |
| 1008 | } |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 1009 | return performClassification(selection); |
| 1010 | } |
| 1011 | |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 1012 | public SelectionResult getOriginalSelection() { |
| 1013 | return new SelectionResult(mSelectionStart, mSelectionEnd, null, null); |
| 1014 | } |
| 1015 | |
Abodunrinwa Toki | 3521f3f | 2017-09-27 02:38:55 +0100 | [diff] [blame] | 1016 | /** |
| 1017 | * Maximum time (in milliseconds) to wait for a textclassifier result before timing out. |
| 1018 | */ |
| 1019 | // TODO: Consider making this a ViewConfiguration. |
| 1020 | public int getTimeoutDuration() { |
| 1021 | if (mHot) { |
| 1022 | return 200; |
| 1023 | } else { |
| 1024 | // Return a slightly larger number than usual when the TextClassifier is first |
| 1025 | // initialized. Initialization would usually take longer than subsequent calls to |
| 1026 | // the TextClassifier. The impact of this on the UI is that we do not show the |
| 1027 | // selection handles or toolbar until after this timeout. |
| 1028 | return 500; |
| 1029 | } |
| 1030 | } |
| 1031 | |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 1032 | private boolean isDarkLaunchEnabled() { |
| 1033 | return TextClassificationManager.getSettings(mContext).isModelDarkLaunchEnabled(); |
| 1034 | } |
| 1035 | |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 1036 | private SelectionResult performClassification(@Nullable TextSelection selection) { |
Abodunrinwa Toki | fdf57ba | 2017-05-02 20:32:23 +0100 | [diff] [blame] | 1037 | if (!Objects.equals(mText, mLastClassificationText) |
| 1038 | || mSelectionStart != mLastClassificationSelectionStart |
| 1039 | || mSelectionEnd != mLastClassificationSelectionEnd |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 1040 | || !Objects.equals(mDefaultLocales, mLastClassificationLocales)) { |
Abodunrinwa Toki | fdf57ba | 2017-05-02 20:32:23 +0100 | [diff] [blame] | 1041 | |
| 1042 | mLastClassificationText = mText; |
| 1043 | mLastClassificationSelectionStart = mSelectionStart; |
| 1044 | mLastClassificationSelectionEnd = mSelectionEnd; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 1045 | mLastClassificationLocales = mDefaultLocales; |
Abodunrinwa Toki | fdf57ba | 2017-05-02 20:32:23 +0100 | [diff] [blame] | 1046 | |
| 1047 | trimText(); |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 1048 | final TextClassification classification; |
Abodunrinwa Toki | a69950c | 2018-11-29 13:51:56 +0000 | [diff] [blame] | 1049 | if (Linkify.containsUnsupportedCharacters(mText)) { |
| 1050 | // Do not show smart actions for text containing unsupported characters. |
| 1051 | android.util.EventLog.writeEvent(0x534e4554, "116321860", -1, ""); |
| 1052 | classification = TextClassification.EMPTY; |
| 1053 | } else if (mContext.getApplicationInfo().targetSdkVersion |
| 1054 | >= Build.VERSION_CODES.P) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 1055 | final TextClassification.Request request = |
| 1056 | new TextClassification.Request.Builder( |
| 1057 | mTrimmedText, mRelativeStart, mRelativeEnd) |
| 1058 | .setDefaultLocales(mDefaultLocales) |
| 1059 | .build(); |
| 1060 | classification = mTextClassifier.get().classifyText(request); |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 1061 | } else { |
| 1062 | // Use old APIs. |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 1063 | classification = mTextClassifier.get().classifyText( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 1064 | mTrimmedText, mRelativeStart, mRelativeEnd, mDefaultLocales); |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 1065 | } |
Abodunrinwa Toki | fdf57ba | 2017-05-02 20:32:23 +0100 | [diff] [blame] | 1066 | mLastClassificationResult = new SelectionResult( |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 1067 | mSelectionStart, mSelectionEnd, classification, selection); |
Abodunrinwa Toki | fdf57ba | 2017-05-02 20:32:23 +0100 | [diff] [blame] | 1068 | |
| 1069 | } |
| 1070 | return mLastClassificationResult; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 1073 | private void trimText() { |
| 1074 | mTrimStart = Math.max(0, mSelectionStart - TRIM_DELTA); |
| 1075 | final int referenceEnd = Math.min(mText.length(), mSelectionEnd + TRIM_DELTA); |
| 1076 | mTrimmedText = mText.subSequence(mTrimStart, referenceEnd); |
| 1077 | mRelativeStart = mSelectionStart - mTrimStart; |
| 1078 | mRelativeEnd = mSelectionEnd - mTrimStart; |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | /** |
| 1083 | * Selection result. |
| 1084 | */ |
| 1085 | private static final class SelectionResult { |
| 1086 | private final int mStart; |
| 1087 | private final int mEnd; |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 1088 | @Nullable private final TextClassification mClassification; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 1089 | @Nullable private final TextSelection mSelection; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 1090 | |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 1091 | SelectionResult(int start, int end, |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 1092 | @Nullable TextClassification classification, @Nullable TextSelection selection) { |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 1093 | mStart = start; |
| 1094 | mEnd = end; |
Abodunrinwa Toki | 5209691 | 2018-03-21 23:14:42 +0000 | [diff] [blame] | 1095 | mClassification = classification; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 1096 | mSelection = selection; |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | @SelectionEvent.ActionType |
| 1101 | private static int getActionType(int menuItemId) { |
| 1102 | switch (menuItemId) { |
| 1103 | case TextView.ID_SELECT_ALL: |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 1104 | return SelectionEvent.ACTION_SELECT_ALL; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 1105 | case TextView.ID_CUT: |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 1106 | return SelectionEvent.ACTION_CUT; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 1107 | case TextView.ID_COPY: |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 1108 | return SelectionEvent.ACTION_COPY; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 1109 | case TextView.ID_PASTE: // fall through |
| 1110 | case TextView.ID_PASTE_AS_PLAIN_TEXT: |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 1111 | return SelectionEvent.ACTION_PASTE; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 1112 | case TextView.ID_SHARE: |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 1113 | return SelectionEvent.ACTION_SHARE; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 1114 | case TextView.ID_ASSIST: |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 1115 | return SelectionEvent.ACTION_SMART_SHARE; |
Abodunrinwa Toki | 57ec6ea | 2017-09-04 20:32:31 +0100 | [diff] [blame] | 1116 | default: |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 1117 | return SelectionEvent.ACTION_OTHER; |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 1118 | } |
| 1119 | } |
Abodunrinwa Toki | 7c1e425 | 2017-09-26 20:09:06 +0100 | [diff] [blame] | 1120 | |
| 1121 | private static CharSequence getText(TextView textView) { |
| 1122 | // Extracts the textView's text. |
| 1123 | // TODO: Investigate why/when TextView.getText() is null. |
| 1124 | final CharSequence text = textView.getText(); |
| 1125 | if (text != null) { |
| 1126 | return text; |
| 1127 | } |
| 1128 | return ""; |
| 1129 | } |
Abodunrinwa Toki | 8710ea1 | 2017-01-24 10:34:13 -0800 | [diff] [blame] | 1130 | } |