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