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 | 2f19b92 | 2018-02-12 19:59:28 +0000 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
| 29 | import android.content.pm.ResolveInfo; |
Abodunrinwa Toki | ef7cb2c | 2018-02-07 22:21:08 +0000 | [diff] [blame] | 30 | import android.content.res.Resources; |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 31 | import android.graphics.BitmapFactory; |
| 32 | import android.graphics.drawable.AdaptiveIconDrawable; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 33 | import android.graphics.drawable.BitmapDrawable; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 34 | import android.graphics.drawable.Drawable; |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 35 | import android.graphics.drawable.Icon; |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 36 | import android.os.Bundle; |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 37 | import android.os.LocaleList; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 38 | import android.os.Parcel; |
| 39 | import android.os.Parcelable; |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 40 | import android.text.SpannedString; |
Jan Althaus | bbe43df | 2017-11-30 15:01:40 +0100 | [diff] [blame] | 41 | import android.util.ArrayMap; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 42 | import android.view.View.OnClickListener; |
| 43 | import android.view.textclassifier.TextClassifier.EntityType; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 44 | import android.view.textclassifier.TextClassifier.Utils; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 45 | |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 46 | import com.android.internal.annotations.VisibleForTesting; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 47 | import com.android.internal.util.Preconditions; |
| 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, |
Jan Althaus | bbe43df | 2017-11-30 15:01:40 +0100 | [diff] [blame] | 142 | @NonNull Map<String, Float> 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); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 151 | mEntityConfidence = new EntityConfidence(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 | * |
| 268 | * <p><b>NOTE: </b>Each call to this method returns a new bundle copy so clients should |
| 269 | * prefer to hold a reference to the returned bundle rather than frequently calling this |
| 270 | * method. |
| 271 | */ |
| 272 | @NonNull |
| 273 | public Bundle getExtras() { |
| 274 | return mExtras.deepCopy(); |
| 275 | } |
| 276 | |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 277 | @Override |
| 278 | public String toString() { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 279 | return String.format(Locale.US, |
Abodunrinwa Toki | 25f7fdc | 2019-02-19 23:42:30 +0000 | [diff] [blame] | 280 | "TextClassification {text=%s, entities=%s, actions=%s, id=%s, extras=%s}", |
| 281 | mText, mEntityConfidence, mActions, mId, mExtras); |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 285 | * Creates an OnClickListener that triggers the specified PendingIntent. |
| 286 | * |
| 287 | * @hide |
| 288 | */ |
| 289 | public static OnClickListener createIntentOnClickListener(@NonNull final PendingIntent intent) { |
| 290 | Preconditions.checkNotNull(intent); |
| 291 | return v -> { |
| 292 | try { |
| 293 | intent.send(); |
| 294 | } catch (PendingIntent.CanceledException e) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 295 | Log.e(LOG_TAG, "Error sending PendingIntent", e); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 296 | } |
| 297 | }; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Creates a PendingIntent for the specified intent. |
Abodunrinwa Toki | 2f19b92 | 2018-02-12 19:59:28 +0000 | [diff] [blame] | 302 | * Returns null if the intent is not supported for the specified context. |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 303 | * |
| 304 | * @throws IllegalArgumentException if context or intent is null |
| 305 | * @hide |
| 306 | */ |
Abodunrinwa Toki | 2f19b92 | 2018-02-12 19:59:28 +0000 | [diff] [blame] | 307 | @Nullable |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 308 | public static PendingIntent createPendingIntent( |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 309 | @NonNull final Context context, @NonNull final Intent intent, int requestCode) { |
Abodunrinwa Toki | aec5981 | 2018-05-15 23:09:34 +0100 | [diff] [blame] | 310 | final int flags = PendingIntent.FLAG_UPDATE_CURRENT; |
Abodunrinwa Toki | 2f19b92 | 2018-02-12 19:59:28 +0000 | [diff] [blame] | 311 | switch (getIntentType(intent, context)) { |
| 312 | case IntentType.ACTIVITY: |
Abodunrinwa Toki | aec5981 | 2018-05-15 23:09:34 +0100 | [diff] [blame] | 313 | return PendingIntent.getActivity(context, requestCode, intent, flags); |
Abodunrinwa Toki | 2f19b92 | 2018-02-12 19:59:28 +0000 | [diff] [blame] | 314 | case IntentType.SERVICE: |
Abodunrinwa Toki | aec5981 | 2018-05-15 23:09:34 +0100 | [diff] [blame] | 315 | return PendingIntent.getService(context, requestCode, intent, flags); |
Abodunrinwa Toki | 2f19b92 | 2018-02-12 19:59:28 +0000 | [diff] [blame] | 316 | default: |
| 317 | return null; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | @IntentType |
| 322 | private static int getIntentType(@NonNull Intent intent, @NonNull Context context) { |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 323 | Preconditions.checkArgument(context != null); |
| 324 | Preconditions.checkArgument(intent != null); |
Abodunrinwa Toki | 2f19b92 | 2018-02-12 19:59:28 +0000 | [diff] [blame] | 325 | |
| 326 | final ResolveInfo activityRI = context.getPackageManager().resolveActivity(intent, 0); |
| 327 | if (activityRI != null) { |
| 328 | if (context.getPackageName().equals(activityRI.activityInfo.packageName)) { |
| 329 | return IntentType.ACTIVITY; |
| 330 | } |
| 331 | final boolean exported = activityRI.activityInfo.exported; |
| 332 | if (exported && hasPermission(context, activityRI.activityInfo.permission)) { |
| 333 | return IntentType.ACTIVITY; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | final ResolveInfo serviceRI = context.getPackageManager().resolveService(intent, 0); |
| 338 | if (serviceRI != null) { |
| 339 | if (context.getPackageName().equals(serviceRI.serviceInfo.packageName)) { |
| 340 | return IntentType.SERVICE; |
| 341 | } |
| 342 | final boolean exported = serviceRI.serviceInfo.exported; |
| 343 | if (exported && hasPermission(context, serviceRI.serviceInfo.permission)) { |
| 344 | return IntentType.SERVICE; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return IntentType.UNSUPPORTED; |
| 349 | } |
| 350 | |
| 351 | private static boolean hasPermission(@NonNull Context context, @NonNull String permission) { |
| 352 | return permission == null |
| 353 | || context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /** |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 357 | * Builder for building {@link TextClassification} objects. |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 358 | * |
| 359 | * <p>e.g. |
| 360 | * |
| 361 | * <pre>{@code |
| 362 | * TextClassification classification = new TextClassification.Builder() |
| 363 | * .setText(classifiedText) |
| 364 | * .setEntityType(TextClassifier.TYPE_EMAIL, 0.9) |
| 365 | * .setEntityType(TextClassifier.TYPE_OTHER, 0.1) |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 366 | * .addAction(remoteAction1) |
| 367 | * .addAction(remoteAction2) |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 368 | * .build(); |
| 369 | * }</pre> |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 370 | */ |
| 371 | public static final class Builder { |
| 372 | |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 373 | @NonNull private List<RemoteAction> mActions = new ArrayList<>(); |
Jan Althaus | bbe43df | 2017-11-30 15:01:40 +0100 | [diff] [blame] | 374 | @NonNull private final Map<String, Float> mEntityConfidence = new ArrayMap<>(); |
Jan Althaus | 13a89c9 | 2018-04-09 16:14:36 +0200 | [diff] [blame] | 375 | @Nullable private String mText; |
| 376 | @Nullable private Drawable mLegacyIcon; |
| 377 | @Nullable private String mLegacyLabel; |
| 378 | @Nullable private Intent mLegacyIntent; |
| 379 | @Nullable private OnClickListener mLegacyOnClickListener; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 380 | @Nullable private String mId; |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 381 | @Nullable private Bundle mExtras; |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 382 | @NonNull private final ArrayList<Intent> mActionIntents = new ArrayList<>(); |
| 383 | @Nullable private Bundle mForeignLanguageExtra; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 384 | |
| 385 | /** |
| 386 | * Sets the classified text. |
| 387 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 388 | @NonNull |
Abodunrinwa Toki | d44286f | 2017-07-12 19:38:54 +0100 | [diff] [blame] | 389 | public Builder setText(@Nullable String text) { |
| 390 | mText = text; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 391 | return this; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * 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] | 396 | * If a confidence score had already been set for the specified entity type, this will |
| 397 | * override that score. |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 398 | * |
| 399 | * @param confidenceScore a value from 0 (low confidence) to 1 (high confidence). |
| 400 | * 0 implies the entity does not exist for the classified text. |
| 401 | * Values greater than 1 are clamped to 1. |
| 402 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 403 | @NonNull |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 404 | public Builder setEntityType( |
| 405 | @NonNull @EntityType String type, |
Abodunrinwa Toki | d44286f | 2017-07-12 19:38:54 +0100 | [diff] [blame] | 406 | @FloatRange(from = 0.0, to = 1.0) float confidenceScore) { |
Jan Althaus | bbe43df | 2017-11-30 15:01:40 +0100 | [diff] [blame] | 407 | mEntityConfidence.put(type, confidenceScore); |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 408 | return this; |
| 409 | } |
| 410 | |
| 411 | /** |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 412 | * Adds an action that may be performed on the classified text. Actions should be added in |
| 413 | * order of likelihood that the user will use them, with the most likely action being added |
| 414 | * first. |
Jan Althaus | 92d7683 | 2017-09-27 18:14:35 +0200 | [diff] [blame] | 415 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 416 | @NonNull |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 417 | public Builder addAction(@NonNull RemoteAction action) { |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 418 | return addAction(action, null); |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * @param intent the intent in the remote action. |
| 423 | * @see #addAction(RemoteAction) |
| 424 | * @hide |
| 425 | */ |
| 426 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 427 | public Builder addAction(RemoteAction action, @Nullable Intent intent) { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 428 | Preconditions.checkArgument(action != null); |
| 429 | mActions.add(action); |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 430 | mActionIntents.add(intent); |
Jan Althaus | 92d7683 | 2017-09-27 18:14:35 +0200 | [diff] [blame] | 431 | return this; |
| 432 | } |
| 433 | |
| 434 | /** |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 435 | * Sets the icon for the <i>primary</i> action that may be rendered on a widget used to act |
| 436 | * on the classified text. |
| 437 | * |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 438 | * <p><strong>NOTE: </strong>This field is not parcelled. If read from a parcel, the |
| 439 | * returned icon represents the icon of the first {@link RemoteAction} (if one exists). |
| 440 | * |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 441 | * @deprecated Use {@link #addAction(RemoteAction)} instead. |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 442 | */ |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 443 | @Deprecated |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 444 | @NonNull |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 445 | public Builder setIcon(@Nullable Drawable icon) { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 446 | mLegacyIcon = icon; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 447 | return this; |
| 448 | } |
| 449 | |
| 450 | /** |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 451 | * Sets the label for the <i>primary</i> action that may be rendered on a widget used to |
| 452 | * act on the classified text. |
| 453 | * |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 454 | * <p><strong>NOTE: </strong>This field is not parcelled. If read from a parcel, the |
| 455 | * returned label represents the label of the first {@link RemoteAction} (if one exists). |
| 456 | * |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 457 | * @deprecated Use {@link #addAction(RemoteAction)} instead. |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 458 | */ |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 459 | @Deprecated |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 460 | @NonNull |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 461 | public Builder setLabel(@Nullable String label) { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 462 | mLegacyLabel = label; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 463 | return this; |
| 464 | } |
| 465 | |
| 466 | /** |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 467 | * Sets the intent for the <i>primary</i> action that may be fired to act on the classified |
| 468 | * text. |
| 469 | * |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 470 | * <p><strong>NOTE: </strong>This field is not parcelled. |
| 471 | * |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 472 | * @deprecated Use {@link #addAction(RemoteAction)} instead. |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 473 | */ |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 474 | @Deprecated |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 475 | @NonNull |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 476 | public Builder setIntent(@Nullable Intent intent) { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 477 | mLegacyIntent = intent; |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 478 | return this; |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * 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] | 483 | * the classified text. |
| 484 | * |
| 485 | * <p><strong>NOTE: </strong>This field is not parcelable. If read from a parcel, the |
| 486 | * returned OnClickListener represents the first {@link RemoteAction} (if one exists). |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 487 | * |
| 488 | * @deprecated Use {@link #addAction(RemoteAction)} instead. |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 489 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 490 | @Deprecated |
| 491 | @NonNull |
Abodunrinwa Toki | ba38562 | 2017-11-29 19:30:32 +0000 | [diff] [blame] | 492 | public Builder setOnClickListener(@Nullable OnClickListener onClickListener) { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 493 | mLegacyOnClickListener = onClickListener; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 494 | return this; |
| 495 | } |
| 496 | |
| 497 | /** |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 498 | * Sets an id for the TextClassification object. |
Abodunrinwa Toki | 54486c1 | 2017-04-19 21:02:36 +0100 | [diff] [blame] | 499 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 500 | @NonNull |
| 501 | public Builder setId(@Nullable String id) { |
| 502 | mId = id; |
Abodunrinwa Toki | 692b196 | 2017-08-15 15:05:11 +0100 | [diff] [blame] | 503 | return this; |
| 504 | } |
| 505 | |
| 506 | /** |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 507 | * Sets the extended data. |
| 508 | */ |
| 509 | @NonNull |
| 510 | public Builder setExtras(@Nullable Bundle extras) { |
| 511 | mExtras = extras; |
| 512 | return this; |
| 513 | } |
| 514 | |
| 515 | /** |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 516 | * @see #setExtras(Bundle) |
| 517 | * @hide |
| 518 | */ |
| 519 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 520 | public Builder setForeignLanguageExtra(@Nullable Bundle extra) { |
| 521 | mForeignLanguageExtra = extra; |
| 522 | return this; |
| 523 | } |
| 524 | |
| 525 | /** |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 526 | * Builds and returns a {@link TextClassification} object. |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 527 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 528 | @NonNull |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 529 | public TextClassification build() { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 530 | return new TextClassification(mText, mLegacyIcon, mLegacyLabel, mLegacyIntent, |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 531 | mLegacyOnClickListener, mActions, mEntityConfidence, mId, buildExtras()); |
| 532 | } |
| 533 | |
| 534 | private Bundle buildExtras() { |
| 535 | final Bundle extras = mExtras == null ? new Bundle() : mExtras.deepCopy(); |
Abodunrinwa Toki | 25f7fdc | 2019-02-19 23:42:30 +0000 | [diff] [blame] | 536 | if (mActionIntents.stream().anyMatch(Objects::nonNull)) { |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 537 | ExtrasUtils.putActionsIntents(extras, mActionIntents); |
| 538 | } |
| 539 | if (mForeignLanguageExtra != null) { |
| 540 | ExtrasUtils.putForeignLanguageExtra(extras, mForeignLanguageExtra); |
| 541 | } |
| 542 | return extras.isEmpty() ? Bundle.EMPTY : extras; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 545 | |
| 546 | /** |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 547 | * A request object for generating TextClassification. |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 548 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 549 | public static final class Request implements Parcelable { |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 550 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 551 | private final CharSequence mText; |
| 552 | private final int mStartIndex; |
| 553 | private final int mEndIndex; |
| 554 | @Nullable private final LocaleList mDefaultLocales; |
| 555 | @Nullable private final ZonedDateTime mReferenceTime; |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 556 | @NonNull private final Bundle mExtras; |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 557 | @Nullable private String mCallingPackageName; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 558 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 559 | private Request( |
| 560 | CharSequence text, |
| 561 | int startIndex, |
| 562 | int endIndex, |
| 563 | LocaleList defaultLocales, |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 564 | ZonedDateTime referenceTime, |
| 565 | Bundle extras) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 566 | mText = text; |
| 567 | mStartIndex = startIndex; |
| 568 | mEndIndex = endIndex; |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 569 | mDefaultLocales = defaultLocales; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 570 | mReferenceTime = referenceTime; |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 571 | mExtras = extras; |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /** |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 575 | * Returns the text providing context for the text to classify (which is specified |
| 576 | * by the sub sequence starting at startIndex and ending at endIndex) |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 577 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 578 | @NonNull |
| 579 | public CharSequence getText() { |
| 580 | return mText; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Returns start index of the text to classify. |
| 585 | */ |
| 586 | @IntRange(from = 0) |
| 587 | public int getStartIndex() { |
| 588 | return mStartIndex; |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * Returns end index of the text to classify. |
| 593 | */ |
| 594 | @IntRange(from = 0) |
| 595 | public int getEndIndex() { |
| 596 | return mEndIndex; |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /** |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 600 | * @return ordered list of locale preferences that can be used to disambiguate |
| 601 | * the provided text. |
| 602 | */ |
| 603 | @Nullable |
| 604 | public LocaleList getDefaultLocales() { |
| 605 | return mDefaultLocales; |
| 606 | } |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 607 | |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 608 | /** |
| 609 | * @return reference time based on which relative dates (e.g. "tomorrow") should be |
| 610 | * interpreted. |
| 611 | */ |
| 612 | @Nullable |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 613 | public ZonedDateTime getReferenceTime() { |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 614 | return mReferenceTime; |
| 615 | } |
| 616 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 617 | /** |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 618 | * Sets the name of the package that is sending this request. |
| 619 | * <p> |
| 620 | * For SystemTextClassifier's use. |
| 621 | * @hide |
| 622 | */ |
| 623 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 624 | public void setCallingPackageName(@Nullable String callingPackageName) { |
| 625 | mCallingPackageName = callingPackageName; |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Returns the name of the package that sent this request. |
| 630 | * This returns {@code null} if no calling package name is set. |
| 631 | */ |
| 632 | @Nullable |
| 633 | public String getCallingPackageName() { |
| 634 | return mCallingPackageName; |
| 635 | } |
| 636 | |
| 637 | /** |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 638 | * Returns the extended data. |
| 639 | * |
| 640 | * <p><b>NOTE: </b>Each call to this method returns a new bundle copy so clients should |
| 641 | * prefer to hold a reference to the returned bundle rather than frequently calling this |
| 642 | * method. |
| 643 | */ |
| 644 | @NonNull |
| 645 | public Bundle getExtras() { |
| 646 | return mExtras.deepCopy(); |
| 647 | } |
| 648 | |
| 649 | /** |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 650 | * A builder for building TextClassification requests. |
| 651 | */ |
| 652 | public static final class Builder { |
| 653 | |
| 654 | private final CharSequence mText; |
| 655 | private final int mStartIndex; |
| 656 | private final int mEndIndex; |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 657 | private Bundle mExtras; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 658 | |
| 659 | @Nullable private LocaleList mDefaultLocales; |
| 660 | @Nullable private ZonedDateTime mReferenceTime; |
| 661 | |
| 662 | /** |
| 663 | * @param text text providing context for the text to classify (which is specified |
| 664 | * by the sub sequence starting at startIndex and ending at endIndex) |
| 665 | * @param startIndex start index of the text to classify |
| 666 | * @param endIndex end index of the text to classify |
| 667 | */ |
| 668 | public Builder( |
| 669 | @NonNull CharSequence text, |
| 670 | @IntRange(from = 0) int startIndex, |
| 671 | @IntRange(from = 0) int endIndex) { |
| 672 | Utils.checkArgument(text, startIndex, endIndex); |
| 673 | mText = text; |
| 674 | mStartIndex = startIndex; |
| 675 | mEndIndex = endIndex; |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * @param defaultLocales ordered list of locale preferences that may be used to |
| 680 | * disambiguate the provided text. If no locale preferences exist, set this to null |
| 681 | * or an empty locale list. |
| 682 | * |
| 683 | * @return this builder |
| 684 | */ |
| 685 | @NonNull |
| 686 | public Builder setDefaultLocales(@Nullable LocaleList defaultLocales) { |
| 687 | mDefaultLocales = defaultLocales; |
| 688 | return this; |
| 689 | } |
| 690 | |
| 691 | /** |
| 692 | * @param referenceTime reference time based on which relative dates (e.g. "tomorrow" |
| 693 | * should be interpreted. This should usually be the time when the text was |
| 694 | * originally composed. If no reference time is set, now is used. |
| 695 | * |
| 696 | * @return this builder |
| 697 | */ |
| 698 | @NonNull |
| 699 | public Builder setReferenceTime(@Nullable ZonedDateTime referenceTime) { |
| 700 | mReferenceTime = referenceTime; |
| 701 | return this; |
| 702 | } |
| 703 | |
| 704 | /** |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 705 | * Sets the extended data. |
| 706 | * |
| 707 | * @return this builder |
| 708 | */ |
| 709 | @NonNull |
| 710 | public Builder setExtras(@Nullable Bundle extras) { |
| 711 | mExtras = extras; |
| 712 | return this; |
| 713 | } |
| 714 | |
| 715 | /** |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 716 | * Builds and returns the request object. |
| 717 | */ |
| 718 | @NonNull |
| 719 | public Request build() { |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 720 | return new Request(new SpannedString(mText), mStartIndex, mEndIndex, |
| 721 | mDefaultLocales, mReferenceTime, |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 722 | mExtras == null ? Bundle.EMPTY : mExtras.deepCopy()); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 726 | @Override |
| 727 | public int describeContents() { |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | @Override |
| 732 | public void writeToParcel(Parcel dest, int flags) { |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 733 | dest.writeCharSequence(mText); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 734 | dest.writeInt(mStartIndex); |
| 735 | dest.writeInt(mEndIndex); |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 736 | dest.writeParcelable(mDefaultLocales, flags); |
| 737 | dest.writeString(mReferenceTime == null ? null : mReferenceTime.toString()); |
| 738 | dest.writeString(mCallingPackageName); |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 739 | dest.writeBundle(mExtras); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 740 | } |
| 741 | |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 742 | private static Request readFromParcel(Parcel in) { |
| 743 | final CharSequence text = in.readCharSequence(); |
| 744 | final int startIndex = in.readInt(); |
| 745 | final int endIndex = in.readInt(); |
| 746 | final LocaleList defaultLocales = in.readParcelable(null); |
| 747 | final String referenceTimeString = in.readString(); |
| 748 | final ZonedDateTime referenceTime = referenceTimeString == null |
| 749 | ? null : ZonedDateTime.parse(referenceTimeString); |
| 750 | final String callingPackageName = in.readString(); |
| 751 | final Bundle extras = in.readBundle(); |
| 752 | |
| 753 | final Request request = new Request(text, startIndex, endIndex, |
| 754 | defaultLocales, referenceTime, extras); |
| 755 | request.setCallingPackageName(callingPackageName); |
| 756 | return request; |
| 757 | } |
| 758 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 759 | public static final Parcelable.Creator<Request> CREATOR = |
| 760 | new Parcelable.Creator<Request>() { |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 761 | @Override |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 762 | public Request createFromParcel(Parcel in) { |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 763 | return readFromParcel(in); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | @Override |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 767 | public Request[] newArray(int size) { |
| 768 | return new Request[size]; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 769 | } |
| 770 | }; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 771 | } |
| 772 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 773 | @Override |
| 774 | public int describeContents() { |
| 775 | return 0; |
| 776 | } |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 777 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 778 | @Override |
| 779 | public void writeToParcel(Parcel dest, int flags) { |
| 780 | dest.writeString(mText); |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 781 | // NOTE: legacy fields are not parcelled. |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 782 | dest.writeTypedList(mActions); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 783 | mEntityConfidence.writeToParcel(dest, flags); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 784 | dest.writeString(mId); |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 785 | dest.writeBundle(mExtras); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 786 | } |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 787 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 788 | public static final Parcelable.Creator<TextClassification> CREATOR = |
| 789 | new Parcelable.Creator<TextClassification>() { |
| 790 | @Override |
| 791 | public TextClassification createFromParcel(Parcel in) { |
| 792 | return new TextClassification(in); |
| 793 | } |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 794 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 795 | @Override |
| 796 | public TextClassification[] newArray(int size) { |
| 797 | return new TextClassification[size]; |
| 798 | } |
| 799 | }; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 800 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 801 | private TextClassification(Parcel in) { |
| 802 | mText = in.readString(); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 803 | mActions = in.createTypedArrayList(RemoteAction.CREATOR); |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 804 | if (!mActions.isEmpty()) { |
| 805 | final RemoteAction action = mActions.get(0); |
| 806 | mLegacyIcon = maybeLoadDrawable(action.getIcon()); |
| 807 | mLegacyLabel = action.getTitle().toString(); |
| 808 | mLegacyOnClickListener = createIntentOnClickListener(mActions.get(0).getActionIntent()); |
| 809 | } else { |
| 810 | mLegacyIcon = null; |
| 811 | mLegacyLabel = null; |
| 812 | mLegacyOnClickListener = null; |
| 813 | } |
| 814 | mLegacyIntent = null; // mLegacyIntent is not parcelled. |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 815 | mEntityConfidence = EntityConfidence.CREATOR.createFromParcel(in); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 816 | mId = in.readString(); |
Tony Mak | d6f3fb4 | 2018-10-26 15:42:49 +0100 | [diff] [blame] | 817 | mExtras = in.readBundle(); |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 818 | } |
Abodunrinwa Toki | ae82e7a | 2018-04-03 23:49:16 +0100 | [diff] [blame] | 819 | |
Abodunrinwa Toki | ba196c5 | 2018-04-20 19:52:21 +0100 | [diff] [blame] | 820 | // Best effort attempt to try to load a drawable from the provided icon. |
| 821 | @Nullable |
| 822 | private static Drawable maybeLoadDrawable(Icon icon) { |
| 823 | if (icon == null) { |
| 824 | return null; |
| 825 | } |
| 826 | switch (icon.getType()) { |
| 827 | case Icon.TYPE_BITMAP: |
| 828 | return new BitmapDrawable(Resources.getSystem(), icon.getBitmap()); |
| 829 | case Icon.TYPE_ADAPTIVE_BITMAP: |
| 830 | return new AdaptiveIconDrawable(null, |
| 831 | new BitmapDrawable(Resources.getSystem(), icon.getBitmap())); |
| 832 | case Icon.TYPE_DATA: |
| 833 | return new BitmapDrawable( |
| 834 | Resources.getSystem(), |
| 835 | BitmapFactory.decodeByteArray( |
| 836 | icon.getDataBytes(), icon.getDataOffset(), icon.getDataLength())); |
| 837 | } |
| 838 | return null; |
| 839 | } |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 840 | } |