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