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