Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [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.view.textclassifier; |
| 18 | |
| 19 | import android.annotation.FloatRange; |
Abodunrinwa Toki | 2f19b92 | 2018-02-12 19:59:28 +0000 | [diff] [blame] | 20 | import android.annotation.IntDef; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 21 | import android.annotation.IntRange; |
| 22 | import android.annotation.NonNull; |
| 23 | import android.annotation.Nullable; |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 24 | import android.app.PendingIntent; |
| 25 | import android.app.RemoteAction; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
Abodunrinwa Toki | ef7cb2c | 2018-02-07 22:21:08 +0000 | [diff] [blame] | 28 | import android.content.res.Resources; |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 29 | import android.graphics.BitmapFactory; |
| 30 | import android.graphics.drawable.AdaptiveIconDrawable; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 31 | import android.graphics.drawable.BitmapDrawable; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 32 | import android.graphics.drawable.Drawable; |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 33 | import android.graphics.drawable.Icon; |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 34 | import android.os.Bundle; |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 35 | import android.os.LocaleList; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 36 | import android.os.Parcel; |
| 37 | import android.os.Parcelable; |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 38 | import android.text.SpannedString; |
Jan Althaus | bbe43df | 2017-11-30 15:01:40 +0100 | [diff] [blame] | 39 | import android.util.ArrayMap; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 40 | import android.view.View.OnClickListener; |
| 41 | import android.view.textclassifier.TextClassifier.EntityType; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 42 | import android.view.textclassifier.TextClassifier.Utils; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 43 | |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 44 | import com.android.internal.annotations.VisibleForTesting; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 45 | import com.android.internal.util.Preconditions; |
| 46 | |
Tony Mak | fdb3554 | 2019-03-22 12:01:50 +0000 | [diff] [blame] | 47 | import com.google.android.textclassifier.AnnotatorModel; |
| 48 | |
Abodunrinwa Toki | 2f19b92 | 2018-02-12 19:59:28 +0000 | [diff] [blame] | 49 | import java.lang.annotation.Retention; |
| 50 | import java.lang.annotation.RetentionPolicy; |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 51 | import java.time.ZonedDateTime; |
Jan Althaus | 92d7683 | 2017-09-27 18:14:35 +0200 | [diff] [blame] | 52 | import java.util.ArrayList; |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 53 | import java.util.Collections; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 54 | import java.util.List; |
Abodunrinwa Toki | 4d232d6 | 2017-11-23 12:22:45 +0000 | [diff] [blame] | 55 | import java.util.Locale; |
Jan Althaus | bbe43df | 2017-11-30 15:01:40 +0100 | [diff] [blame] | 56 | import java.util.Map; |
Abodunrinwa Toki | 25f7fdc | 2019-02-19 23:42:30 +0000 | [diff] [blame] | 57 | import java.util.Objects; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Information for generating a widget to handle classified text. |
Abodunrinwa Toki | 33ff200 | 2017-10-24 00:49:27 +0100 | [diff] [blame] | 61 | * |
| 62 | * <p>A TextClassification object contains icons, labels, onClickListeners and intents that may |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 63 | * be used to build a widget that can be used to act on classified text. There is the concept of a |
| 64 | * <i>primary action</i> and other <i>secondary actions</i>. |
Abodunrinwa Toki | 33ff200 | 2017-10-24 00:49:27 +0100 | [diff] [blame] | 65 | * |
| 66 | * <p>e.g. building a view that, when clicked, shares the classified text with the preferred app: |
| 67 | * |
| 68 | * <pre>{@code |
| 69 | * // Called preferably outside the UiThread. |
Abodunrinwa Toki | 4d232d6 | 2017-11-23 12:22:45 +0000 | [diff] [blame] | 70 | * TextClassification classification = textClassifier.classifyText(allText, 10, 25); |
Abodunrinwa Toki | 33ff200 | 2017-10-24 00:49:27 +0100 | [diff] [blame] | 71 | * |
| 72 | * // Called on the UiThread. |
| 73 | * Button button = new Button(context); |
| 74 | * button.setCompoundDrawablesWithIntrinsicBounds(classification.getIcon(), null, null, null); |
| 75 | * button.setText(classification.getLabel()); |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 76 | * button.setOnClickListener(v -> classification.getActions().get(0).getActionIntent().send()); |
Abodunrinwa Toki | 33ff200 | 2017-10-24 00:49:27 +0100 | [diff] [blame] | 77 | * }</pre> |
| 78 | * |
| 79 | * <p>e.g. starting an action mode with menu items that can handle the classified text: |
| 80 | * |
| 81 | * <pre>{@code |
| 82 | * // Called preferably outside the UiThread. |
Abodunrinwa Toki | 4d232d6 | 2017-11-23 12:22:45 +0000 | [diff] [blame] | 83 | * final TextClassification classification = textClassifier.classifyText(allText, 10, 25); |
Abodunrinwa Toki | 33ff200 | 2017-10-24 00:49:27 +0100 | [diff] [blame] | 84 | * |
| 85 | * // Called on the UiThread. |
| 86 | * view.startActionMode(new ActionMode.Callback() { |
| 87 | * |
| 88 | * public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 89 | * for (int i = 0; i < classification.getActions().size(); ++i) { |
| 90 | * RemoteAction action = classification.getActions().get(i); |
| 91 | * menu.add(Menu.NONE, i, 20, action.getTitle()) |
| 92 | * .setIcon(action.getIcon()); |
Abodunrinwa Toki | 33ff200 | 2017-10-24 00:49:27 +0100 | [diff] [blame] | 93 | * } |
| 94 | * return true; |
| 95 | * } |
| 96 | * |
| 97 | * public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 98 | * classification.getActions().get(item.getItemId()).getActionIntent().send(); |
Abodunrinwa Toki | 33ff200 | 2017-10-24 00:49:27 +0100 | [diff] [blame] | 99 | * return true; |
| 100 | * } |
| 101 | * |
| 102 | * ... |
| 103 | * }); |
| 104 | * }</pre> |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 105 | */ |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 106 | public final class TextClassification implements Parcelable { |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * @hide |
| 110 | */ |
Abodunrinwa Toki | adc1940 | 2018-11-22 17:10:25 +0000 | [diff] [blame] | 111 | public static final TextClassification EMPTY = new TextClassification.Builder().build(); |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 112 | |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 113 | private static final String LOG_TAG = "TextClassification"; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 114 | // TODO(toki): investigate a way to derive this based on device properties. |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 115 | private static final int MAX_LEGACY_ICON_SIZE = 192; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 116 | |
Abodunrinwa Toki | 2f19b92 | 2018-02-12 19:59:28 +0000 | [diff] [blame] | 117 | @Retention(RetentionPolicy.SOURCE) |
| 118 | @IntDef(value = {IntentType.UNSUPPORTED, IntentType.ACTIVITY, IntentType.SERVICE}) |
| 119 | private @interface IntentType { |
| 120 | int UNSUPPORTED = -1; |
| 121 | int ACTIVITY = 0; |
| 122 | int SERVICE = 1; |
| 123 | } |
| 124 | |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 125 | @NonNull private final String mText; |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 126 | @Nullable private final Drawable mLegacyIcon; |
| 127 | @Nullable private final String mLegacyLabel; |
| 128 | @Nullable private final Intent mLegacyIntent; |
| 129 | @Nullable private final OnClickListener mLegacyOnClickListener; |
| 130 | @NonNull private final List<RemoteAction> mActions; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 131 | @NonNull private final EntityConfidence mEntityConfidence; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 132 | @Nullable private final String mId; |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 133 | @NonNull private final Bundle mExtras; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 134 | |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 135 | private TextClassification( |
Abodunrinwa Toki | d44286f | 2017-07-12 19:38:54 +0100 | [diff] [blame] | 136 | @Nullable String text, |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 137 | @Nullable Drawable legacyIcon, |
| 138 | @Nullable String legacyLabel, |
| 139 | @Nullable Intent legacyIntent, |
| 140 | @Nullable OnClickListener legacyOnClickListener, |
| 141 | @NonNull List<RemoteAction> actions, |
Tony Mak | fdb3554 | 2019-03-22 12:01:50 +0000 | [diff] [blame] | 142 | @NonNull EntityConfidence entityConfidence, |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 143 | @Nullable String id, |
| 144 | @NonNull Bundle extras) { |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 145 | mText = text; |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 146 | mLegacyIcon = legacyIcon; |
| 147 | mLegacyLabel = legacyLabel; |
| 148 | mLegacyIntent = legacyIntent; |
| 149 | mLegacyOnClickListener = legacyOnClickListener; |
| 150 | mActions = Collections.unmodifiableList(actions); |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 151 | mEntityConfidence = Objects.requireNonNull(entityConfidence); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 152 | mId = id; |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 153 | mExtras = extras; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Gets the classified text. |
| 158 | */ |
Abodunrinwa Toki | d44286f | 2017-07-12 19:38:54 +0100 | [diff] [blame] | 159 | @Nullable |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 160 | public String getText() { |
| 161 | return mText; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Returns the number of entities found in the classified text. |
| 166 | */ |
| 167 | @IntRange(from = 0) |
| 168 | public int getEntityCount() { |
Jan Althaus | bbe43df | 2017-11-30 15:01:40 +0100 | [diff] [blame] | 169 | return mEntityConfidence.getEntities().size(); |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Returns the entity at the specified index. Entities are ordered from high confidence |
| 174 | * to low confidence. |
| 175 | * |
| 176 | * @throws IndexOutOfBoundsException if the specified index is out of range. |
| 177 | * @see #getEntityCount() for the number of entities available. |
| 178 | */ |
| 179 | @NonNull |
| 180 | public @EntityType String getEntity(int index) { |
Jan Althaus | bbe43df | 2017-11-30 15:01:40 +0100 | [diff] [blame] | 181 | return mEntityConfidence.getEntities().get(index); |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Returns the confidence score for the specified entity. The value ranges from |
| 186 | * 0 (low confidence) to 1 (high confidence). 0 indicates that the entity was not found for the |
| 187 | * classified text. |
| 188 | */ |
| 189 | @FloatRange(from = 0.0, to = 1.0) |
| 190 | public float getConfidenceScore(@EntityType String entity) { |
| 191 | return mEntityConfidence.getConfidenceScore(entity); |
| 192 | } |
| 193 | |
| 194 | /** |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 195 | * Returns a list of actions that may be performed on the text. The list is ordered based on |
| 196 | * the likelihood that a user will use the action, with the most likely action appearing first. |
Jan Althaus | 92d7683 | 2017-09-27 18:14:35 +0200 | [diff] [blame] | 197 | */ |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 198 | public List<RemoteAction> getActions() { |
| 199 | return mActions; |
Jan Althaus | 92d7683 | 2017-09-27 18:14:35 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 203 | * Returns an icon that may be rendered on a widget used to act on the classified text. |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 204 | * |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 205 | * <p><strong>NOTE: </strong>This field is not parcelable and only represents the icon of the |
| 206 | * first {@link RemoteAction} (if one exists) when this object is read from a parcel. |
| 207 | * |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 208 | * @deprecated Use {@link #getActions()} instead. |
Jan Althaus | 92d7683 | 2017-09-27 18:14:35 +0200 | [diff] [blame] | 209 | */ |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 210 | @Deprecated |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 211 | @Nullable |
| 212 | public Drawable getIcon() { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 213 | return mLegacyIcon; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /** |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 217 | * Returns a label that may be rendered on a widget used to act on the classified text. |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 218 | * |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 219 | * <p><strong>NOTE: </strong>This field is not parcelable and only represents the label of the |
| 220 | * first {@link RemoteAction} (if one exists) when this object is read from a parcel. |
| 221 | * |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 222 | * @deprecated Use {@link #getActions()} instead. |
Jan Althaus | 92d7683 | 2017-09-27 18:14:35 +0200 | [diff] [blame] | 223 | */ |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 224 | @Deprecated |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 225 | @Nullable |
| 226 | public CharSequence getLabel() { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 227 | return mLegacyLabel; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /** |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 231 | * Returns an intent that may be fired to act on the classified text. |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 232 | * |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 233 | * <p><strong>NOTE: </strong>This field is not parcelled and will always return null when this |
| 234 | * object is read from a parcel. |
| 235 | * |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 236 | * @deprecated Use {@link #getActions()} instead. |
Jan Althaus | 92d7683 | 2017-09-27 18:14:35 +0200 | [diff] [blame] | 237 | */ |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 238 | @Deprecated |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 239 | @Nullable |
| 240 | public Intent getIntent() { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 241 | return mLegacyIntent; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /** |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 245 | * Returns the OnClickListener that may be triggered to act on the classified text. |
| 246 | * |
| 247 | * <p><strong>NOTE: </strong>This field is not parcelable and only represents the first |
| 248 | * {@link RemoteAction} (if one exists) when this object is read from a parcel. |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 249 | * |
| 250 | * @deprecated Use {@link #getActions()} instead. |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 251 | */ |
| 252 | @Nullable |
| 253 | public OnClickListener getOnClickListener() { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 254 | return mLegacyOnClickListener; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Abodunrinwa Toki | 54486c1 | 2017-04-19 21:02:36 +0100 | [diff] [blame] | 257 | /** |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 258 | * Returns the id, if one exists, for this object. |
Abodunrinwa Toki | 692b196 | 2017-08-15 15:05:11 +0100 | [diff] [blame] | 259 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 260 | @Nullable |
| 261 | public String getId() { |
| 262 | return mId; |
Abodunrinwa Toki | 692b196 | 2017-08-15 15:05:11 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 265 | /** |
| 266 | * Returns the extended data. |
| 267 | * |
Tony Mak | 5a8d827 | 2019-04-08 15:46:13 +0100 | [diff] [blame] | 268 | * <p><b>NOTE: </b>Do not modify this bundle. |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 269 | */ |
| 270 | @NonNull |
| 271 | public Bundle getExtras() { |
Tony Mak | 5a8d827 | 2019-04-08 15:46:13 +0100 | [diff] [blame] | 272 | return mExtras; |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 275 | @Override |
| 276 | public String toString() { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 277 | return String.format(Locale.US, |
Abodunrinwa Toki | 25f7fdc | 2019-02-19 23:42:30 +0000 | [diff] [blame] | 278 | "TextClassification {text=%s, entities=%s, actions=%s, id=%s, extras=%s}", |
| 279 | mText, mEntityConfidence, mActions, mId, mExtras); |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /** |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 283 | * Creates an OnClickListener that triggers the specified PendingIntent. |
| 284 | * |
| 285 | * @hide |
| 286 | */ |
| 287 | public static OnClickListener createIntentOnClickListener(@NonNull final PendingIntent intent) { |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 288 | Objects.requireNonNull(intent); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 289 | return v -> { |
| 290 | try { |
| 291 | intent.send(); |
| 292 | } catch (PendingIntent.CanceledException e) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 293 | Log.e(LOG_TAG, "Error sending PendingIntent", e); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 294 | } |
| 295 | }; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Creates a PendingIntent for the specified intent. |
Abodunrinwa Toki | 2f19b92 | 2018-02-12 19:59:28 +0000 | [diff] [blame] | 300 | * Returns null if the intent is not supported for the specified context. |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 301 | * |
| 302 | * @throws IllegalArgumentException if context or intent is null |
| 303 | * @hide |
| 304 | */ |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 305 | public static PendingIntent createPendingIntent( |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 306 | @NonNull final Context context, @NonNull final Intent intent, int requestCode) { |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 307 | return PendingIntent.getActivity( |
| 308 | context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /** |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 312 | * Builder for building {@link TextClassification} objects. |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 313 | * |
| 314 | * <p>e.g. |
| 315 | * |
| 316 | * <pre>{@code |
| 317 | * TextClassification classification = new TextClassification.Builder() |
| 318 | * .setText(classifiedText) |
| 319 | * .setEntityType(TextClassifier.TYPE_EMAIL, 0.9) |
| 320 | * .setEntityType(TextClassifier.TYPE_OTHER, 0.1) |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 321 | * .addAction(remoteAction1) |
| 322 | * .addAction(remoteAction2) |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 323 | * .build(); |
| 324 | * }</pre> |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 325 | */ |
| 326 | public static final class Builder { |
| 327 | |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 328 | @NonNull private List<RemoteAction> mActions = new ArrayList<>(); |
Tony Mak | fdb3554 | 2019-03-22 12:01:50 +0000 | [diff] [blame] | 329 | @NonNull private final Map<String, Float> mTypeScoreMap = new ArrayMap<>(); |
| 330 | @NonNull |
| 331 | private final Map<String, AnnotatorModel.ClassificationResult> mClassificationResults = |
| 332 | new ArrayMap<>(); |
Jan Althaus | 13a89c9 | 2018-04-09 16:14:36 +0200 | [diff] [blame] | 333 | @Nullable private String mText; |
| 334 | @Nullable private Drawable mLegacyIcon; |
| 335 | @Nullable private String mLegacyLabel; |
| 336 | @Nullable private Intent mLegacyIntent; |
| 337 | @Nullable private OnClickListener mLegacyOnClickListener; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 338 | @Nullable private String mId; |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 339 | @Nullable private Bundle mExtras; |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 340 | @NonNull private final ArrayList<Intent> mActionIntents = new ArrayList<>(); |
| 341 | @Nullable private Bundle mForeignLanguageExtra; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 342 | |
| 343 | /** |
| 344 | * Sets the classified text. |
| 345 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 346 | @NonNull |
Abodunrinwa Toki | d44286f | 2017-07-12 19:38:54 +0100 | [diff] [blame] | 347 | public Builder setText(@Nullable String text) { |
| 348 | mText = text; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 349 | return this; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Sets an entity type for the classification result and assigns a confidence score. |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 354 | * If a confidence score had already been set for the specified entity type, this will |
| 355 | * override that score. |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 356 | * |
| 357 | * @param confidenceScore a value from 0 (low confidence) to 1 (high confidence). |
| 358 | * 0 implies the entity does not exist for the classified text. |
| 359 | * Values greater than 1 are clamped to 1. |
| 360 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 361 | @NonNull |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 362 | public Builder setEntityType( |
| 363 | @NonNull @EntityType String type, |
Abodunrinwa Toki | d44286f | 2017-07-12 19:38:54 +0100 | [diff] [blame] | 364 | @FloatRange(from = 0.0, to = 1.0) float confidenceScore) { |
Tony Mak | fdb3554 | 2019-03-22 12:01:50 +0000 | [diff] [blame] | 365 | setEntityType(type, confidenceScore, null); |
| 366 | return this; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * @see #setEntityType(String, float) |
| 371 | * |
| 372 | * @hide |
| 373 | */ |
| 374 | @NonNull |
| 375 | public Builder setEntityType(AnnotatorModel.ClassificationResult classificationResult) { |
| 376 | setEntityType( |
| 377 | classificationResult.getCollection(), |
| 378 | classificationResult.getScore(), |
| 379 | classificationResult); |
| 380 | return this; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * @see #setEntityType(String, float) |
| 385 | * |
| 386 | * @hide |
| 387 | */ |
| 388 | @NonNull |
| 389 | private Builder setEntityType( |
| 390 | @NonNull @EntityType String type, |
| 391 | @FloatRange(from = 0.0, to = 1.0) float confidenceScore, |
| 392 | @Nullable AnnotatorModel.ClassificationResult classificationResult) { |
| 393 | mTypeScoreMap.put(type, confidenceScore); |
| 394 | mClassificationResults.put(type, classificationResult); |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 395 | return this; |
| 396 | } |
| 397 | |
| 398 | /** |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 399 | * Adds an action that may be performed on the classified text. Actions should be added in |
| 400 | * order of likelihood that the user will use them, with the most likely action being added |
| 401 | * first. |
Jan Althaus | 92d7683 | 2017-09-27 18:14:35 +0200 | [diff] [blame] | 402 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 403 | @NonNull |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 404 | public Builder addAction(@NonNull RemoteAction action) { |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 405 | return addAction(action, null); |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * @param intent the intent in the remote action. |
| 410 | * @see #addAction(RemoteAction) |
| 411 | * @hide |
| 412 | */ |
| 413 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 414 | public Builder addAction(RemoteAction action, @Nullable Intent intent) { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 415 | Preconditions.checkArgument(action != null); |
| 416 | mActions.add(action); |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 417 | mActionIntents.add(intent); |
Jan Althaus | 92d7683 | 2017-09-27 18:14:35 +0200 | [diff] [blame] | 418 | return this; |
| 419 | } |
| 420 | |
| 421 | /** |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 422 | * Sets the icon for the <i>primary</i> action that may be rendered on a widget used to act |
| 423 | * on the classified text. |
| 424 | * |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 425 | * <p><strong>NOTE: </strong>This field is not parcelled. If read from a parcel, the |
| 426 | * returned icon represents the icon of the first {@link RemoteAction} (if one exists). |
| 427 | * |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 428 | * @deprecated Use {@link #addAction(RemoteAction)} instead. |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 429 | */ |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 430 | @Deprecated |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 431 | @NonNull |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 432 | public Builder setIcon(@Nullable Drawable icon) { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 433 | mLegacyIcon = icon; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 434 | return this; |
| 435 | } |
| 436 | |
| 437 | /** |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 438 | * Sets the label for the <i>primary</i> action that may be rendered on a widget used to |
| 439 | * act on the classified text. |
| 440 | * |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 441 | * <p><strong>NOTE: </strong>This field is not parcelled. If read from a parcel, the |
| 442 | * returned label represents the label of the first {@link RemoteAction} (if one exists). |
| 443 | * |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 444 | * @deprecated Use {@link #addAction(RemoteAction)} instead. |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 445 | */ |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 446 | @Deprecated |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 447 | @NonNull |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 448 | public Builder setLabel(@Nullable String label) { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 449 | mLegacyLabel = label; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 450 | return this; |
| 451 | } |
| 452 | |
| 453 | /** |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 454 | * Sets the intent for the <i>primary</i> action that may be fired to act on the classified |
| 455 | * text. |
| 456 | * |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 457 | * <p><strong>NOTE: </strong>This field is not parcelled. |
| 458 | * |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 459 | * @deprecated Use {@link #addAction(RemoteAction)} instead. |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 460 | */ |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 461 | @Deprecated |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 462 | @NonNull |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 463 | public Builder setIntent(@Nullable Intent intent) { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 464 | mLegacyIntent = intent; |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 465 | return this; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Sets the OnClickListener for the <i>primary</i> action that may be triggered to act on |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 470 | * the classified text. |
| 471 | * |
| 472 | * <p><strong>NOTE: </strong>This field is not parcelable. If read from a parcel, the |
| 473 | * returned OnClickListener represents the first {@link RemoteAction} (if one exists). |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 474 | * |
| 475 | * @deprecated Use {@link #addAction(RemoteAction)} instead. |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 476 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 477 | @Deprecated |
| 478 | @NonNull |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 479 | public Builder setOnClickListener(@Nullable OnClickListener onClickListener) { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 480 | mLegacyOnClickListener = onClickListener; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 481 | return this; |
| 482 | } |
| 483 | |
| 484 | /** |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 485 | * Sets an id for the TextClassification object. |
Abodunrinwa Toki | 54486c1 | 2017-04-19 21:02:36 +0100 | [diff] [blame] | 486 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 487 | @NonNull |
| 488 | public Builder setId(@Nullable String id) { |
| 489 | mId = id; |
Abodunrinwa Toki | 692b196 | 2017-08-15 15:05:11 +0100 | [diff] [blame] | 490 | return this; |
| 491 | } |
| 492 | |
| 493 | /** |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 494 | * Sets the extended data. |
| 495 | */ |
| 496 | @NonNull |
| 497 | public Builder setExtras(@Nullable Bundle extras) { |
| 498 | mExtras = extras; |
| 499 | return this; |
| 500 | } |
| 501 | |
| 502 | /** |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 503 | * @see #setExtras(Bundle) |
| 504 | * @hide |
| 505 | */ |
| 506 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 507 | public Builder setForeignLanguageExtra(@Nullable Bundle extra) { |
| 508 | mForeignLanguageExtra = extra; |
| 509 | return this; |
| 510 | } |
| 511 | |
| 512 | /** |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 513 | * Builds and returns a {@link TextClassification} object. |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 514 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 515 | @NonNull |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 516 | public TextClassification build() { |
Tony Mak | fdb3554 | 2019-03-22 12:01:50 +0000 | [diff] [blame] | 517 | EntityConfidence entityConfidence = new EntityConfidence(mTypeScoreMap); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 518 | return new TextClassification(mText, mLegacyIcon, mLegacyLabel, mLegacyIntent, |
Tony Mak | fdb3554 | 2019-03-22 12:01:50 +0000 | [diff] [blame] | 519 | mLegacyOnClickListener, mActions, entityConfidence, mId, |
| 520 | buildExtras(entityConfidence)); |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Tony Mak | fdb3554 | 2019-03-22 12:01:50 +0000 | [diff] [blame] | 523 | private Bundle buildExtras(EntityConfidence entityConfidence) { |
Tony Mak | 7482810 | 2019-04-10 17:53:13 +0100 | [diff] [blame] | 524 | final Bundle extras = mExtras == null ? new Bundle() : mExtras; |
Abodunrinwa Toki | 25f7fdc | 2019-02-19 23:42:30 +0000 | [diff] [blame] | 525 | if (mActionIntents.stream().anyMatch(Objects::nonNull)) { |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 526 | ExtrasUtils.putActionsIntents(extras, mActionIntents); |
| 527 | } |
| 528 | if (mForeignLanguageExtra != null) { |
| 529 | ExtrasUtils.putForeignLanguageExtra(extras, mForeignLanguageExtra); |
| 530 | } |
Tony Mak | fdb3554 | 2019-03-22 12:01:50 +0000 | [diff] [blame] | 531 | List<String> sortedTypes = entityConfidence.getEntities(); |
| 532 | ArrayList<AnnotatorModel.ClassificationResult> sortedEntities = new ArrayList<>(); |
| 533 | for (String type : sortedTypes) { |
| 534 | sortedEntities.add(mClassificationResults.get(type)); |
| 535 | } |
| 536 | ExtrasUtils.putEntities( |
| 537 | extras, sortedEntities.toArray(new AnnotatorModel.ClassificationResult[0])); |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 538 | return extras.isEmpty() ? Bundle.EMPTY : extras; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 539 | } |
| 540 | } |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 541 | |
| 542 | /** |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 543 | * A request object for generating TextClassification. |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 544 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 545 | public static final class Request implements Parcelable { |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 546 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 547 | private final CharSequence mText; |
| 548 | private final int mStartIndex; |
| 549 | private final int mEndIndex; |
| 550 | @Nullable private final LocaleList mDefaultLocales; |
| 551 | @Nullable private final ZonedDateTime mReferenceTime; |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 552 | @NonNull private final Bundle mExtras; |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 553 | @Nullable private SystemTextClassifierMetadata mSystemTcMetadata; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 554 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 555 | private Request( |
| 556 | CharSequence text, |
| 557 | int startIndex, |
| 558 | int endIndex, |
| 559 | LocaleList defaultLocales, |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 560 | ZonedDateTime referenceTime, |
| 561 | Bundle extras) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 562 | mText = text; |
| 563 | mStartIndex = startIndex; |
| 564 | mEndIndex = endIndex; |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 565 | mDefaultLocales = defaultLocales; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 566 | mReferenceTime = referenceTime; |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 567 | mExtras = extras; |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /** |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 571 | * Returns the text providing context for the text to classify (which is specified |
| 572 | * by the sub sequence starting at startIndex and ending at endIndex) |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 573 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 574 | @NonNull |
| 575 | public CharSequence getText() { |
| 576 | return mText; |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * Returns start index of the text to classify. |
| 581 | */ |
| 582 | @IntRange(from = 0) |
| 583 | public int getStartIndex() { |
| 584 | return mStartIndex; |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Returns end index of the text to classify. |
| 589 | */ |
| 590 | @IntRange(from = 0) |
| 591 | public int getEndIndex() { |
| 592 | return mEndIndex; |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /** |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 596 | * @return ordered list of locale preferences that can be used to disambiguate |
| 597 | * the provided text. |
| 598 | */ |
| 599 | @Nullable |
| 600 | public LocaleList getDefaultLocales() { |
| 601 | return mDefaultLocales; |
| 602 | } |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 603 | |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 604 | /** |
| 605 | * @return reference time based on which relative dates (e.g. "tomorrow") should be |
| 606 | * interpreted. |
| 607 | */ |
| 608 | @Nullable |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 609 | public ZonedDateTime getReferenceTime() { |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 610 | return mReferenceTime; |
| 611 | } |
| 612 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 613 | /** |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 614 | * Returns the name of the package that sent this request. |
| 615 | * This returns {@code null} if no calling package name is set. |
| 616 | */ |
| 617 | @Nullable |
| 618 | public String getCallingPackageName() { |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 619 | return mSystemTcMetadata != null ? mSystemTcMetadata.getCallingPackageName() : null; |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /** |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 623 | * Sets the information about the {@link SystemTextClassifier} that sent this request. |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 624 | * |
| 625 | * @hide |
| 626 | */ |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 627 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 628 | public void setSystemTextClassifierMetadata( |
| 629 | @Nullable SystemTextClassifierMetadata systemTcMetadata) { |
| 630 | mSystemTcMetadata = systemTcMetadata; |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /** |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 634 | * Returns the information about the {@link SystemTextClassifier} that sent this request. |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 635 | * |
| 636 | * @hide |
| 637 | */ |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 638 | @Nullable |
| 639 | public SystemTextClassifierMetadata getSystemTextClassifierMetadata() { |
| 640 | return mSystemTcMetadata; |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /** |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 644 | * Returns the extended data. |
| 645 | * |
Tony Mak | 5a8d827 | 2019-04-08 15:46:13 +0100 | [diff] [blame] | 646 | * <p><b>NOTE: </b>Do not modify this bundle. |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 647 | */ |
| 648 | @NonNull |
| 649 | public Bundle getExtras() { |
Tony Mak | 5a8d827 | 2019-04-08 15:46:13 +0100 | [diff] [blame] | 650 | return mExtras; |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /** |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 654 | * A builder for building TextClassification requests. |
| 655 | */ |
| 656 | public static final class Builder { |
| 657 | |
| 658 | private final CharSequence mText; |
| 659 | private final int mStartIndex; |
| 660 | private final int mEndIndex; |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 661 | private Bundle mExtras; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 662 | |
| 663 | @Nullable private LocaleList mDefaultLocales; |
| 664 | @Nullable private ZonedDateTime mReferenceTime; |
| 665 | |
| 666 | /** |
| 667 | * @param text text providing context for the text to classify (which is specified |
| 668 | * by the sub sequence starting at startIndex and ending at endIndex) |
| 669 | * @param startIndex start index of the text to classify |
| 670 | * @param endIndex end index of the text to classify |
| 671 | */ |
| 672 | public Builder( |
| 673 | @NonNull CharSequence text, |
| 674 | @IntRange(from = 0) int startIndex, |
| 675 | @IntRange(from = 0) int endIndex) { |
| 676 | Utils.checkArgument(text, startIndex, endIndex); |
| 677 | mText = text; |
| 678 | mStartIndex = startIndex; |
| 679 | mEndIndex = endIndex; |
| 680 | } |
| 681 | |
| 682 | /** |
| 683 | * @param defaultLocales ordered list of locale preferences that may be used to |
| 684 | * disambiguate the provided text. If no locale preferences exist, set this to null |
| 685 | * or an empty locale list. |
| 686 | * |
| 687 | * @return this builder |
| 688 | */ |
| 689 | @NonNull |
| 690 | public Builder setDefaultLocales(@Nullable LocaleList defaultLocales) { |
| 691 | mDefaultLocales = defaultLocales; |
| 692 | return this; |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * @param referenceTime reference time based on which relative dates (e.g. "tomorrow" |
| 697 | * should be interpreted. This should usually be the time when the text was |
| 698 | * originally composed. If no reference time is set, now is used. |
| 699 | * |
| 700 | * @return this builder |
| 701 | */ |
| 702 | @NonNull |
| 703 | public Builder setReferenceTime(@Nullable ZonedDateTime referenceTime) { |
| 704 | mReferenceTime = referenceTime; |
| 705 | return this; |
| 706 | } |
| 707 | |
| 708 | /** |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 709 | * Sets the extended data. |
| 710 | * |
| 711 | * @return this builder |
| 712 | */ |
| 713 | @NonNull |
| 714 | public Builder setExtras(@Nullable Bundle extras) { |
| 715 | mExtras = extras; |
| 716 | return this; |
| 717 | } |
| 718 | |
| 719 | /** |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 720 | * Builds and returns the request object. |
| 721 | */ |
| 722 | @NonNull |
| 723 | public Request build() { |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 724 | return new Request(new SpannedString(mText), mStartIndex, mEndIndex, |
| 725 | mDefaultLocales, mReferenceTime, |
Tony Mak | 7482810 | 2019-04-10 17:53:13 +0100 | [diff] [blame] | 726 | mExtras == null ? Bundle.EMPTY : mExtras); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 730 | @Override |
| 731 | public int describeContents() { |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | @Override |
| 736 | public void writeToParcel(Parcel dest, int flags) { |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 737 | dest.writeCharSequence(mText); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 738 | dest.writeInt(mStartIndex); |
| 739 | dest.writeInt(mEndIndex); |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 740 | dest.writeParcelable(mDefaultLocales, flags); |
| 741 | dest.writeString(mReferenceTime == null ? null : mReferenceTime.toString()); |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 742 | dest.writeBundle(mExtras); |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 743 | dest.writeParcelable(mSystemTcMetadata, flags); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 744 | } |
| 745 | |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 746 | private static Request readFromParcel(Parcel in) { |
| 747 | final CharSequence text = in.readCharSequence(); |
| 748 | final int startIndex = in.readInt(); |
| 749 | final int endIndex = in.readInt(); |
| 750 | final LocaleList defaultLocales = in.readParcelable(null); |
| 751 | final String referenceTimeString = in.readString(); |
| 752 | final ZonedDateTime referenceTime = referenceTimeString == null |
| 753 | ? null : ZonedDateTime.parse(referenceTimeString); |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 754 | final Bundle extras = in.readBundle(); |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 755 | final SystemTextClassifierMetadata systemTcMetadata = in.readParcelable(null); |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 756 | |
| 757 | final Request request = new Request(text, startIndex, endIndex, |
| 758 | defaultLocales, referenceTime, extras); |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 759 | request.setSystemTextClassifierMetadata(systemTcMetadata); |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 760 | return request; |
| 761 | } |
| 762 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 763 | public static final @android.annotation.NonNull Parcelable.Creator<Request> CREATOR = |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 764 | new Parcelable.Creator<Request>() { |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 765 | @Override |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 766 | public Request createFromParcel(Parcel in) { |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 767 | return readFromParcel(in); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | @Override |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 771 | public Request[] newArray(int size) { |
| 772 | return new Request[size]; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 773 | } |
| 774 | }; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 775 | } |
| 776 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 777 | @Override |
| 778 | public int describeContents() { |
| 779 | return 0; |
| 780 | } |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 781 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 782 | @Override |
| 783 | public void writeToParcel(Parcel dest, int flags) { |
| 784 | dest.writeString(mText); |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 785 | // NOTE: legacy fields are not parcelled. |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 786 | dest.writeTypedList(mActions); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 787 | mEntityConfidence.writeToParcel(dest, flags); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 788 | dest.writeString(mId); |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 789 | dest.writeBundle(mExtras); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 790 | } |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 791 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 792 | public static final @android.annotation.NonNull Parcelable.Creator<TextClassification> CREATOR = |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 793 | new Parcelable.Creator<TextClassification>() { |
| 794 | @Override |
| 795 | public TextClassification createFromParcel(Parcel in) { |
| 796 | return new TextClassification(in); |
| 797 | } |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 798 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 799 | @Override |
| 800 | public TextClassification[] newArray(int size) { |
| 801 | return new TextClassification[size]; |
| 802 | } |
| 803 | }; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 804 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 805 | private TextClassification(Parcel in) { |
| 806 | mText = in.readString(); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 807 | mActions = in.createTypedArrayList(RemoteAction.CREATOR); |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 808 | if (!mActions.isEmpty()) { |
| 809 | final RemoteAction action = mActions.get(0); |
| 810 | mLegacyIcon = maybeLoadDrawable(action.getIcon()); |
| 811 | mLegacyLabel = action.getTitle().toString(); |
| 812 | mLegacyOnClickListener = createIntentOnClickListener(mActions.get(0).getActionIntent()); |
| 813 | } else { |
| 814 | mLegacyIcon = null; |
| 815 | mLegacyLabel = null; |
| 816 | mLegacyOnClickListener = null; |
| 817 | } |
| 818 | mLegacyIntent = null; // mLegacyIntent is not parcelled. |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 819 | mEntityConfidence = EntityConfidence.CREATOR.createFromParcel(in); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 820 | mId = in.readString(); |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 821 | mExtras = in.readBundle(); |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 822 | } |
Abodunrinwa Toki | ae82e7a | 2018-04-03 23:49:16 +0100 | [diff] [blame] | 823 | |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 824 | // Best effort attempt to try to load a drawable from the provided icon. |
| 825 | @Nullable |
| 826 | private static Drawable maybeLoadDrawable(Icon icon) { |
| 827 | if (icon == null) { |
| 828 | return null; |
| 829 | } |
| 830 | switch (icon.getType()) { |
| 831 | case Icon.TYPE_BITMAP: |
| 832 | return new BitmapDrawable(Resources.getSystem(), icon.getBitmap()); |
| 833 | case Icon.TYPE_ADAPTIVE_BITMAP: |
| 834 | return new AdaptiveIconDrawable(null, |
| 835 | new BitmapDrawable(Resources.getSystem(), icon.getBitmap())); |
| 836 | case Icon.TYPE_DATA: |
| 837 | return new BitmapDrawable( |
| 838 | Resources.getSystem(), |
| 839 | BitmapFactory.decodeByteArray( |
| 840 | icon.getDataBytes(), icon.getDataOffset(), icon.getDataLength())); |
| 841 | } |
| 842 | return null; |
| 843 | } |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 844 | } |