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