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; 20 import android.annotation.IntRange; 21 import android.annotation.NonNull; 22 import android.annotation.Nullable; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.graphics.drawable.Drawable; 26 import android.view.View.OnClickListener; 27 import android.view.textclassifier.TextClassifier.EntityType; 28 29 import com.android.internal.util.Preconditions; 30 31 import java.util.List; 32 33 /** 34 * Information for generating a widget to handle classified text. 35 */ 36 public final class TextClassification { 37 38 /** 39 * @hide 40 */ 41 static final TextClassification EMPTY = new TextClassification.Builder().build(); 42 43 @NonNull private final String mText; 44 @Nullable private final Drawable mIcon; 45 @Nullable private final String mLabel; 46 @Nullable private final Intent mIntent; 47 @Nullable private final OnClickListener mOnClickListener; 48 @NonNull private final EntityConfidence<String> mEntityConfidence; 49 @NonNull private final List<String> mEntities; 50 private int mLogType; 51 TextClassification( @onNull String text, Drawable icon, String label, Intent intent, OnClickListener onClickListener, @NonNull EntityConfidence<String> entityConfidence, int logType)52 private TextClassification( 53 @NonNull String text, 54 Drawable icon, 55 String label, 56 Intent intent, 57 OnClickListener onClickListener, 58 @NonNull EntityConfidence<String> entityConfidence, 59 int logType) { 60 mText = text; 61 mIcon = icon; 62 mLabel = label; 63 mIntent = intent; 64 mOnClickListener = onClickListener; 65 mEntityConfidence = new EntityConfidence<>(entityConfidence); 66 mEntities = mEntityConfidence.getEntities(); 67 mLogType = logType; 68 } 69 70 /** 71 * Gets the classified text. 72 */ 73 @NonNull getText()74 public String getText() { 75 return mText; 76 } 77 78 /** 79 * Returns the number of entities found in the classified text. 80 */ 81 @IntRange(from = 0) getEntityCount()82 public int getEntityCount() { 83 return mEntities.size(); 84 } 85 86 /** 87 * Returns the entity at the specified index. Entities are ordered from high confidence 88 * to low confidence. 89 * 90 * @throws IndexOutOfBoundsException if the specified index is out of range. 91 * @see #getEntityCount() for the number of entities available. 92 */ 93 @NonNull getEntity(int index)94 public @EntityType String getEntity(int index) { 95 return mEntities.get(index); 96 } 97 98 /** 99 * Returns the confidence score for the specified entity. The value ranges from 100 * 0 (low confidence) to 1 (high confidence). 0 indicates that the entity was not found for the 101 * classified text. 102 */ 103 @FloatRange(from = 0.0, to = 1.0) getConfidenceScore(@ntityType String entity)104 public float getConfidenceScore(@EntityType String entity) { 105 return mEntityConfidence.getConfidenceScore(entity); 106 } 107 108 /** 109 * Returns an icon that may be rendered on a widget used to act on the classified text. 110 */ 111 @Nullable getIcon()112 public Drawable getIcon() { 113 return mIcon; 114 } 115 116 /** 117 * Returns a label that may be rendered on a widget used to act on the classified text. 118 */ 119 @Nullable getLabel()120 public CharSequence getLabel() { 121 return mLabel; 122 } 123 124 /** 125 * Returns an intent that may be fired to act on the classified text. 126 */ 127 @Nullable getIntent()128 public Intent getIntent() { 129 return mIntent; 130 } 131 132 /** 133 * Returns an OnClickListener that may be triggered to act on the classified text. 134 */ 135 @Nullable getOnClickListener()136 public OnClickListener getOnClickListener() { 137 return mOnClickListener; 138 } 139 140 /** 141 * Returns the MetricsLogger subtype for the action that is performed for this result. 142 * @hide 143 */ getLogType()144 public int getLogType() { 145 return mLogType; 146 } 147 148 @Override toString()149 public String toString() { 150 return String.format("TextClassification {" 151 + "text=%s, entities=%s, label=%s, intent=%s}", 152 mText, mEntityConfidence, mLabel, mIntent); 153 } 154 155 /** 156 * Creates an OnClickListener that starts an activity with the specified intent. 157 * 158 * @throws IllegalArgumentException if context or intent is null 159 * @hide 160 */ 161 @NonNull createStartActivityOnClickListener( @onNull final Context context, @NonNull final Intent intent)162 public static OnClickListener createStartActivityOnClickListener( 163 @NonNull final Context context, @NonNull final Intent intent) { 164 Preconditions.checkArgument(context != null); 165 Preconditions.checkArgument(intent != null); 166 return v -> context.startActivity(intent); 167 } 168 169 /** 170 * Builder for building {@link TextClassification} objects. 171 */ 172 public static final class Builder { 173 174 @NonNull private String mText; 175 @Nullable private Drawable mIcon; 176 @Nullable private String mLabel; 177 @Nullable private Intent mIntent; 178 @Nullable private OnClickListener mOnClickListener; 179 @NonNull private final EntityConfidence<String> mEntityConfidence = 180 new EntityConfidence<>(); 181 private int mLogType; 182 183 /** 184 * Sets the classified text. 185 */ setText(@onNull String text)186 public Builder setText(@NonNull String text) { 187 mText = Preconditions.checkNotNull(text); 188 return this; 189 } 190 191 /** 192 * Sets an entity type for the classification result and assigns a confidence score. 193 * 194 * @param confidenceScore a value from 0 (low confidence) to 1 (high confidence). 195 * 0 implies the entity does not exist for the classified text. 196 * Values greater than 1 are clamped to 1. 197 */ setEntityType( @onNull @ntityType String type, @FloatRange(from = 0.0, to = 1.0)float confidenceScore)198 public Builder setEntityType( 199 @NonNull @EntityType String type, 200 @FloatRange(from = 0.0, to = 1.0)float confidenceScore) { 201 mEntityConfidence.setEntityType(type, confidenceScore); 202 return this; 203 } 204 205 /** 206 * Sets an icon that may be rendered on a widget used to act on the classified text. 207 */ setIcon(@ullable Drawable icon)208 public Builder setIcon(@Nullable Drawable icon) { 209 mIcon = icon; 210 return this; 211 } 212 213 /** 214 * Sets a label that may be rendered on a widget used to act on the classified text. 215 */ setLabel(@ullable String label)216 public Builder setLabel(@Nullable String label) { 217 mLabel = label; 218 return this; 219 } 220 221 /** 222 * Sets an intent that may be fired to act on the classified text. 223 */ setIntent(@ullable Intent intent)224 public Builder setIntent(@Nullable Intent intent) { 225 mIntent = intent; 226 return this; 227 } 228 229 /** 230 * Sets the MetricsLogger subtype for the action that is performed for this result. 231 * @hide 232 */ setLogType(int type)233 public Builder setLogType(int type) { 234 mLogType = type; 235 return this; 236 } 237 238 /** 239 * Sets an OnClickListener that may be triggered to act on the classified text. 240 */ setOnClickListener(@ullable OnClickListener onClickListener)241 public Builder setOnClickListener(@Nullable OnClickListener onClickListener) { 242 mOnClickListener = onClickListener; 243 return this; 244 } 245 246 /** 247 * Builds and returns a {@link TextClassification} object. 248 */ build()249 public TextClassification build() { 250 return new TextClassification( 251 mText, mIcon, mLabel, mIntent, mOnClickListener, mEntityConfidence, mLogType); 252 } 253 } 254 } 255