| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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.os.Bundle; |
| 24 |
import android.os.LocaleList; |
| 25 |
import android.os.Parcel; |
| 26 |
import android.os.Parcelable; |
| 27 |
import android.text.SpannedString; |
| 28 |
import android.util.ArrayMap; |
| 29 |
import android.view.textclassifier.TextClassifier.EntityType; |
| 30 |
import android.view.textclassifier.TextClassifier.Utils; |
| 31 |
|
| 32 |
import com.android.internal.annotations.VisibleForTesting; |
| 33 |
import com.android.internal.util.Preconditions; |
| 34 |
|
| 35 |
import java.util.Locale; |
| 36 |
import java.util.Map; |
| 37 |
import java.util.Objects; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public final class TextSelection implements Parcelable { |
| 43 |
|
| 44 |
private final int mStartIndex; |
| 45 |
private final int mEndIndex; |
| 46 |
private final EntityConfidence mEntityConfidence; |
| 47 |
@Nullable private final String mId; |
| 48 |
@Nullable |
| 49 |
private final TextClassification mTextClassification; |
| 50 |
private final Bundle mExtras; |
| 51 |
|
| 52 |
private TextSelection( |
| 53 |
int startIndex, int endIndex, Map<String, Float> entityConfidence, String id, |
| 54 |
@Nullable TextClassification textClassification, |
| 55 |
Bundle extras) { |
| 56 |
mStartIndex = startIndex; |
| 57 |
mEndIndex = endIndex; |
| 58 |
mEntityConfidence = new EntityConfidence(entityConfidence); |
| 59 |
mId = id; |
| 60 |
mTextClassification = textClassification; |
| 61 |
mExtras = extras; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public int getSelectionStartIndex() { |
| 68 |
return mStartIndex; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public int getSelectionEndIndex() { |
| 75 |
return mEndIndex; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
@IntRange(from = 0) |
| 82 |
public int getEntityCount() { |
| 83 |
return mEntityConfidence.getEntities().size(); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
@NonNull |
| 94 |
@EntityType |
| 95 |
public String getEntity(int index) { |
| 96 |
return mEntityConfidence.getEntities().get(index); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
@FloatRange(from = 0.0, to = 1.0) |
| 105 |
public float getConfidenceScore(@EntityType String entity) { |
| 106 |
return mEntityConfidence.getConfidenceScore(entity); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
@Nullable |
| 113 |
public String getId() { |
| 114 |
return mId; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
@Nullable |
| 126 |
public TextClassification getTextClassification() { |
| 127 |
return mTextClassification; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
@NonNull |
| 136 |
public Bundle getExtras() { |
| 137 |
return mExtras; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
public TextSelection.Builder toBuilder() { |
| 142 |
return new TextSelection.Builder(mStartIndex, mEndIndex) |
| 143 |
.setId(mId) |
| 144 |
.setEntityConfidence(mEntityConfidence) |
| 145 |
.setTextClassification(mTextClassification) |
| 146 |
.setExtras(mExtras); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public String toString() { |
| 151 |
return String.format( |
| 152 |
Locale.US, |
| 153 |
"TextSelection {id=%s, startIndex=%d, endIndex=%d, entities=%s}", |
| 154 |
mId, mStartIndex, mEndIndex, mEntityConfidence); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public static final class Builder { |
| 161 |
|
| 162 |
private final int mStartIndex; |
| 163 |
private final int mEndIndex; |
| 164 |
private final Map<String, Float> mEntityConfidence = new ArrayMap<>(); |
| 165 |
@Nullable private String mId; |
| 166 |
@Nullable |
| 167 |
private TextClassification mTextClassification; |
| 168 |
@Nullable |
| 169 |
private Bundle mExtras; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public Builder(@IntRange(from = 0) int startIndex, @IntRange(from = 0) int endIndex) { |
| 178 |
Preconditions.checkArgument(startIndex >= 0); |
| 179 |
Preconditions.checkArgument(endIndex > startIndex); |
| 180 |
mStartIndex = startIndex; |
| 181 |
mEndIndex = endIndex; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
@NonNull |
| 192 |
public Builder setEntityType( |
| 193 |
@NonNull @EntityType String type, |
| 194 |
@FloatRange(from = 0.0, to = 1.0) float confidenceScore) { |
| 195 |
Objects.requireNonNull(type); |
| 196 |
mEntityConfidence.put(type, confidenceScore); |
| 197 |
return this; |
| 198 |
} |
| 199 |
|
| 200 |
Builder setEntityConfidence(EntityConfidence scores) { |
| 201 |
mEntityConfidence.clear(); |
| 202 |
mEntityConfidence.putAll(scores.toMap()); |
| 203 |
return this; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
@NonNull |
| 210 |
public Builder setId(@Nullable String id) { |
| 211 |
mId = id; |
| 212 |
return this; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
@NonNull |
| 225 |
public Builder setTextClassification(@Nullable TextClassification textClassification) { |
| 226 |
mTextClassification = textClassification; |
| 227 |
return this; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
@NonNull |
| 236 |
public Builder setExtras(@Nullable Bundle extras) { |
| 237 |
mExtras = extras; |
| 238 |
return this; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
@NonNull |
| 245 |
public TextSelection build() { |
| 246 |
return new TextSelection( |
| 247 |
mStartIndex, mEndIndex, mEntityConfidence, mId, |
| 248 |
mTextClassification, mExtras == null ? Bundle.EMPTY : mExtras); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public static final class Request implements Parcelable { |
| 256 |
|
| 257 |
private final CharSequence mText; |
| 258 |
private final int mStartIndex; |
| 259 |
private final int mEndIndex; |
| 260 |
@Nullable private final LocaleList mDefaultLocales; |
| 261 |
private final boolean mDarkLaunchAllowed; |
| 262 |
private final boolean mIncludeTextClassification; |
| 263 |
private final Bundle mExtras; |
| 264 |
@Nullable private SystemTextClassifierMetadata mSystemTcMetadata; |
| 265 |
|
| 266 |
private Request( |
| 267 |
CharSequence text, |
| 268 |
int startIndex, |
| 269 |
int endIndex, |
| 270 |
LocaleList defaultLocales, |
| 271 |
boolean darkLaunchAllowed, |
| 272 |
boolean includeTextClassification, |
| 273 |
Bundle extras) { |
| 274 |
mText = text; |
| 275 |
mStartIndex = startIndex; |
| 276 |
mEndIndex = endIndex; |
| 277 |
mDefaultLocales = defaultLocales; |
| 278 |
mDarkLaunchAllowed = darkLaunchAllowed; |
| 279 |
mIncludeTextClassification = includeTextClassification; |
| 280 |
mExtras = extras; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
@NonNull |
| 288 |
public CharSequence getText() { |
| 289 |
return mText; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
@IntRange(from = 0) |
| 296 |
public int getStartIndex() { |
| 297 |
return mStartIndex; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
@IntRange(from = 0) |
| 304 |
public int getEndIndex() { |
| 305 |
return mEndIndex; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public boolean isDarkLaunchAllowed() { |
| 315 |
return mDarkLaunchAllowed; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
@Nullable |
| 323 |
public LocaleList getDefaultLocales() { |
| 324 |
return mDefaultLocales; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
@Nullable |
| 332 |
public String getCallingPackageName() { |
| 333 |
return mSystemTcMetadata != null ? mSystemTcMetadata.getCallingPackageName() : null; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 342 |
public void setSystemTextClassifierMetadata( |
| 343 |
@Nullable SystemTextClassifierMetadata systemTcMetadata) { |
| 344 |
mSystemTcMetadata = systemTcMetadata; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
@Nullable |
| 353 |
public SystemTextClassifierMetadata getSystemTextClassifierMetadata() { |
| 354 |
return mSystemTcMetadata; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public boolean shouldIncludeTextClassification() { |
| 362 |
return mIncludeTextClassification; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
@NonNull |
| 371 |
public Bundle getExtras() { |
| 372 |
return mExtras; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
public static final class Builder { |
| 379 |
|
| 380 |
private final CharSequence mText; |
| 381 |
private final int mStartIndex; |
| 382 |
private final int mEndIndex; |
| 383 |
@Nullable private LocaleList mDefaultLocales; |
| 384 |
private boolean mDarkLaunchAllowed; |
| 385 |
private boolean mIncludeTextClassification; |
| 386 |
private Bundle mExtras; |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
public Builder( |
| 395 |
@NonNull CharSequence text, |
| 396 |
@IntRange(from = 0) int startIndex, |
| 397 |
@IntRange(from = 0) int endIndex) { |
| 398 |
Utils.checkArgument(text, startIndex, endIndex); |
| 399 |
mText = text; |
| 400 |
mStartIndex = startIndex; |
| 401 |
mEndIndex = endIndex; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
@NonNull |
| 412 |
public Builder setDefaultLocales(@Nullable LocaleList defaultLocales) { |
| 413 |
mDefaultLocales = defaultLocales; |
| 414 |
return this; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
@NonNull |
| 429 |
public Builder setDarkLaunchAllowed(boolean allowed) { |
| 430 |
mDarkLaunchAllowed = allowed; |
| 431 |
return this; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
@NonNull |
| 444 |
public Builder setIncludeTextClassification(boolean includeTextClassification) { |
| 445 |
mIncludeTextClassification = includeTextClassification; |
| 446 |
return this; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
@NonNull |
| 455 |
public Builder setExtras(@Nullable Bundle extras) { |
| 456 |
mExtras = extras; |
| 457 |
return this; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
@NonNull |
| 464 |
public Request build() { |
| 465 |
return new Request(new SpannedString(mText), mStartIndex, mEndIndex, |
| 466 |
mDefaultLocales, mDarkLaunchAllowed, |
| 467 |
mIncludeTextClassification, |
| 468 |
mExtras == null ? Bundle.EMPTY : mExtras); |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
@Override |
| 473 |
public int describeContents() { |
| 474 |
return 0; |
| 475 |
} |
| 476 |
|
| 477 |
@Override |
| 478 |
public void writeToParcel(Parcel dest, int flags) { |
| 479 |
dest.writeCharSequence(mText); |
| 480 |
dest.writeInt(mStartIndex); |
| 481 |
dest.writeInt(mEndIndex); |
| 482 |
dest.writeParcelable(mDefaultLocales, flags); |
| 483 |
dest.writeBundle(mExtras); |
| 484 |
dest.writeParcelable(mSystemTcMetadata, flags); |
| 485 |
dest.writeBoolean(mIncludeTextClassification); |
| 486 |
} |
| 487 |
|
| 488 |
private static Request readFromParcel(Parcel in) { |
| 489 |
final CharSequence text = in.readCharSequence(); |
| 490 |
final int startIndex = in.readInt(); |
| 491 |
final int endIndex = in.readInt(); |
| 492 |
final LocaleList defaultLocales = in.readParcelable(null, android.os.LocaleList.class); |
| 493 |
final Bundle extras = in.readBundle(); |
| 494 |
final SystemTextClassifierMetadata systemTcMetadata = in.readParcelable(null, android.view.textclassifier.SystemTextClassifierMetadata.class); |
| 495 |
final boolean includeTextClassification = in.readBoolean(); |
| 496 |
|
| 497 |
final Request request = new Request(text, startIndex, endIndex, defaultLocales, |
| 498 |
false, includeTextClassification, extras); |
| 499 |
request.setSystemTextClassifierMetadata(systemTcMetadata); |
| 500 |
return request; |
| 501 |
} |
| 502 |
|
| 503 |
public static final @android.annotation.NonNull Parcelable.Creator<Request> CREATOR = |
| 504 |
new Parcelable.Creator<Request>() { |
| 505 |
@Override |
| 506 |
public Request createFromParcel(Parcel in) { |
| 507 |
return readFromParcel(in); |
| 508 |
} |
| 509 |
|
| 510 |
@Override |
| 511 |
public Request[] newArray(int size) { |
| 512 |
return new Request[size]; |
| 513 |
} |
| 514 |
}; |
| 515 |
} |
| 516 |
|
| 517 |
@Override |
| 518 |
public int describeContents() { |
| 519 |
return 0; |
| 520 |
} |
| 521 |
|
| 522 |
@Override |
| 523 |
public void writeToParcel(Parcel dest, int flags) { |
| 524 |
dest.writeInt(mStartIndex); |
| 525 |
dest.writeInt(mEndIndex); |
| 526 |
mEntityConfidence.writeToParcel(dest, flags); |
| 527 |
dest.writeString(mId); |
| 528 |
dest.writeBundle(mExtras); |
| 529 |
dest.writeParcelable(mTextClassification, flags); |
| 530 |
} |
| 531 |
|
| 532 |
public static final @android.annotation.NonNull Parcelable.Creator<TextSelection> CREATOR = |
| 533 |
new Parcelable.Creator<TextSelection>() { |
| 534 |
@Override |
| 535 |
public TextSelection createFromParcel(Parcel in) { |
| 536 |
return new TextSelection(in); |
| 537 |
} |
| 538 |
|
| 539 |
@Override |
| 540 |
public TextSelection[] newArray(int size) { |
| 541 |
return new TextSelection[size]; |
| 542 |
} |
| 543 |
}; |
| 544 |
|
| 545 |
private TextSelection(Parcel in) { |
| 546 |
mStartIndex = in.readInt(); |
| 547 |
mEndIndex = in.readInt(); |
| 548 |
mEntityConfidence = EntityConfidence.CREATOR.createFromParcel(in); |
| 549 |
mId = in.readString(); |
| 550 |
mExtras = in.readBundle(); |
| 551 |
mTextClassification = in.readParcelable(TextClassification.class.getClassLoader(), android.view.textclassifier.TextClassification.class); |
| 552 |
} |
| 553 |
} |