| 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.IntDef; |
| 21 |
import android.annotation.IntRange; |
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.Nullable; |
| 24 |
import android.app.PendingIntent; |
| 25 |
import android.app.RemoteAction; |
| 26 |
import android.content.Context; |
| 27 |
import android.content.Intent; |
| 28 |
import android.content.res.Resources; |
| 29 |
import android.graphics.BitmapFactory; |
| 30 |
import android.graphics.drawable.AdaptiveIconDrawable; |
| 31 |
import android.graphics.drawable.BitmapDrawable; |
| 32 |
import android.graphics.drawable.Drawable; |
| 33 |
import android.graphics.drawable.Icon; |
| 34 |
import android.os.Bundle; |
| 35 |
import android.os.LocaleList; |
| 36 |
import android.os.Parcel; |
| 37 |
import android.os.Parcelable; |
| 38 |
import android.text.SpannedString; |
| 39 |
import android.util.ArrayMap; |
| 40 |
import android.view.View.OnClickListener; |
| 41 |
import android.view.textclassifier.TextClassifier.EntityType; |
| 42 |
import android.view.textclassifier.TextClassifier.Utils; |
| 43 |
|
| 44 |
import com.android.internal.annotations.VisibleForTesting; |
| 45 |
import com.android.internal.util.Preconditions; |
| 46 |
|
| 47 |
import java.lang.annotation.Retention; |
| 48 |
import java.lang.annotation.RetentionPolicy; |
| 49 |
import java.time.ZonedDateTime; |
| 50 |
import java.util.ArrayList; |
| 51 |
import java.util.Collection; |
| 52 |
import java.util.Collections; |
| 53 |
import java.util.List; |
| 54 |
import java.util.Locale; |
| 55 |
import java.util.Map; |
| 56 |
import java.util.Objects; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public final class TextClassification implements Parcelable { |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public static final TextClassification EMPTY = new TextClassification.Builder().build(); |
| 111 |
|
| 112 |
private static final String LOG_TAG = "TextClassification"; |
| 113 |
|
| 114 |
private static final int MAX_LEGACY_ICON_SIZE = 192; |
| 115 |
|
| 116 |
@Retention(RetentionPolicy.SOURCE) |
| 117 |
@IntDef(value = {IntentType.UNSUPPORTED, IntentType.ACTIVITY, IntentType.SERVICE}) |
| 118 |
private @interface IntentType { |
| 119 |
int UNSUPPORTED = -1; |
| 120 |
int ACTIVITY = 0; |
| 121 |
int SERVICE = 1; |
| 122 |
} |
| 123 |
|
| 124 |
@NonNull private final String mText; |
| 125 |
@Nullable private final Drawable mLegacyIcon; |
| 126 |
@Nullable private final String mLegacyLabel; |
| 127 |
@Nullable private final Intent mLegacyIntent; |
| 128 |
@Nullable private final OnClickListener mLegacyOnClickListener; |
| 129 |
@NonNull private final List<RemoteAction> mActions; |
| 130 |
@NonNull private final EntityConfidence mEntityConfidence; |
| 131 |
@Nullable private final String mId; |
| 132 |
@NonNull private final Bundle mExtras; |
| 133 |
|
| 134 |
private TextClassification( |
| 135 |
@Nullable String text, |
| 136 |
@Nullable Drawable legacyIcon, |
| 137 |
@Nullable String legacyLabel, |
| 138 |
@Nullable Intent legacyIntent, |
| 139 |
@Nullable OnClickListener legacyOnClickListener, |
| 140 |
@NonNull List<RemoteAction> actions, |
| 141 |
@NonNull EntityConfidence entityConfidence, |
| 142 |
@Nullable String id, |
| 143 |
@NonNull Bundle extras) { |
| 144 |
mText = text; |
| 145 |
mLegacyIcon = legacyIcon; |
| 146 |
mLegacyLabel = legacyLabel; |
| 147 |
mLegacyIntent = legacyIntent; |
| 148 |
mLegacyOnClickListener = legacyOnClickListener; |
| 149 |
mActions = Collections.unmodifiableList(actions); |
| 150 |
mEntityConfidence = Objects.requireNonNull(entityConfidence); |
| 151 |
mId = id; |
| 152 |
mExtras = extras; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
@Nullable |
| 159 |
public String getText() { |
| 160 |
return mText; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
@IntRange(from = 0) |
| 167 |
public int getEntityCount() { |
| 168 |
return mEntityConfidence.getEntities().size(); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
@NonNull |
| 179 |
public @EntityType String getEntity(int index) { |
| 180 |
return mEntityConfidence.getEntities().get(index); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
@FloatRange(from = 0.0, to = 1.0) |
| 189 |
public float getConfidenceScore(@EntityType String entity) { |
| 190 |
return mEntityConfidence.getConfidenceScore(entity); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public List<RemoteAction> getActions() { |
| 198 |
return mActions; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
@Deprecated |
| 210 |
@Nullable |
| 211 |
public Drawable getIcon() { |
| 212 |
return mLegacyIcon; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
@Deprecated |
| 224 |
@Nullable |
| 225 |
public CharSequence getLabel() { |
| 226 |
return mLegacyLabel; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
@Deprecated |
| 238 |
@Nullable |
| 239 |
public Intent getIntent() { |
| 240 |
return mLegacyIntent; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
@Nullable |
| 252 |
public OnClickListener getOnClickListener() { |
| 253 |
return mLegacyOnClickListener; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
@Nullable |
| 260 |
public String getId() { |
| 261 |
return mId; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
@NonNull |
| 270 |
public Bundle getExtras() { |
| 271 |
return mExtras; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
public Builder toBuilder() { |
| 276 |
return new Builder() |
| 277 |
.setId(mId) |
| 278 |
.setText(mText) |
| 279 |
.addActions(mActions) |
| 280 |
.setEntityConfidence(mEntityConfidence) |
| 281 |
.setIcon(mLegacyIcon) |
| 282 |
.setLabel(mLegacyLabel) |
| 283 |
.setIntent(mLegacyIntent) |
| 284 |
.setOnClickListener(mLegacyOnClickListener) |
| 285 |
.setExtras(mExtras); |
| 286 |
} |
| 287 |
|
| 288 |
@Override |
| 289 |
public String toString() { |
| 290 |
return String.format(Locale.US, |
| 291 |
"TextClassification {text=%s, entities=%s, actions=%s, id=%s, extras=%s}", |
| 292 |
mText, mEntityConfidence, mActions, mId, mExtras); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public static OnClickListener createIntentOnClickListener(@NonNull final PendingIntent intent) { |
| 301 |
Objects.requireNonNull(intent); |
| 302 |
return v -> { |
| 303 |
try { |
| 304 |
intent.send(); |
| 305 |
} catch (PendingIntent.CanceledException e) { |
| 306 |
Log.e(LOG_TAG, "Error sending PendingIntent", e); |
| 307 |
} |
| 308 |
}; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
public static PendingIntent createPendingIntent( |
| 319 |
@NonNull final Context context, @NonNull final Intent intent, int requestCode) { |
| 320 |
return PendingIntent.getActivity( |
| 321 |
context, requestCode, intent, |
| 322 |
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
public static final class Builder { |
| 341 |
|
| 342 |
@NonNull private final List<RemoteAction> mActions = new ArrayList<>(); |
| 343 |
@NonNull private final Map<String, Float> mTypeScoreMap = new ArrayMap<>(); |
| 344 |
@Nullable private String mText; |
| 345 |
@Nullable private Drawable mLegacyIcon; |
| 346 |
@Nullable private String mLegacyLabel; |
| 347 |
@Nullable private Intent mLegacyIntent; |
| 348 |
@Nullable private OnClickListener mLegacyOnClickListener; |
| 349 |
@Nullable private String mId; |
| 350 |
@Nullable private Bundle mExtras; |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
@NonNull |
| 356 |
public Builder setText(@Nullable String text) { |
| 357 |
mText = text; |
| 358 |
return this; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
@NonNull |
| 371 |
public Builder setEntityType( |
| 372 |
@NonNull @EntityType String type, |
| 373 |
@FloatRange(from = 0.0, to = 1.0) float confidenceScore) { |
| 374 |
mTypeScoreMap.put(type, confidenceScore); |
| 375 |
return this; |
| 376 |
} |
| 377 |
|
| 378 |
Builder setEntityConfidence(EntityConfidence scores) { |
| 379 |
mTypeScoreMap.clear(); |
| 380 |
mTypeScoreMap.putAll(scores.toMap()); |
| 381 |
return this; |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
public Builder clearEntityTypes() { |
| 386 |
mTypeScoreMap.clear(); |
| 387 |
return this; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
@NonNull |
| 396 |
public Builder addAction(@NonNull RemoteAction action) { |
| 397 |
Preconditions.checkArgument(action != null); |
| 398 |
mActions.add(action); |
| 399 |
return this; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
public Builder addActions(Collection<RemoteAction> actions) { |
| 404 |
Objects.requireNonNull(actions); |
| 405 |
mActions.addAll(actions); |
| 406 |
return this; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
public Builder clearActions() { |
| 411 |
mActions.clear(); |
| 412 |
return this; |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
@Deprecated |
| 425 |
@NonNull |
| 426 |
public Builder setIcon(@Nullable Drawable icon) { |
| 427 |
mLegacyIcon = icon; |
| 428 |
return this; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
@Deprecated |
| 441 |
@NonNull |
| 442 |
public Builder setLabel(@Nullable String label) { |
| 443 |
mLegacyLabel = label; |
| 444 |
return this; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
@Deprecated |
| 456 |
@NonNull |
| 457 |
public Builder setIntent(@Nullable Intent intent) { |
| 458 |
mLegacyIntent = intent; |
| 459 |
return this; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
@Deprecated |
| 472 |
@NonNull |
| 473 |
public Builder setOnClickListener(@Nullable OnClickListener onClickListener) { |
| 474 |
mLegacyOnClickListener = onClickListener; |
| 475 |
return this; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
@NonNull |
| 482 |
public Builder setId(@Nullable String id) { |
| 483 |
mId = id; |
| 484 |
return this; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
@NonNull |
| 491 |
public Builder setExtras(@Nullable Bundle extras) { |
| 492 |
mExtras = extras; |
| 493 |
return this; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
@NonNull |
| 500 |
public TextClassification build() { |
| 501 |
EntityConfidence entityConfidence = new EntityConfidence(mTypeScoreMap); |
| 502 |
return new TextClassification(mText, mLegacyIcon, mLegacyLabel, mLegacyIntent, |
| 503 |
mLegacyOnClickListener, mActions, entityConfidence, mId, |
| 504 |
mExtras == null ? Bundle.EMPTY : mExtras); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
public static final class Request implements Parcelable { |
| 512 |
|
| 513 |
private final CharSequence mText; |
| 514 |
private final int mStartIndex; |
| 515 |
private final int mEndIndex; |
| 516 |
@Nullable private final LocaleList mDefaultLocales; |
| 517 |
@Nullable private final ZonedDateTime mReferenceTime; |
| 518 |
@NonNull private final Bundle mExtras; |
| 519 |
@Nullable private SystemTextClassifierMetadata mSystemTcMetadata; |
| 520 |
|
| 521 |
private Request( |
| 522 |
CharSequence text, |
| 523 |
int startIndex, |
| 524 |
int endIndex, |
| 525 |
LocaleList defaultLocales, |
| 526 |
ZonedDateTime referenceTime, |
| 527 |
Bundle extras) { |
| 528 |
mText = text; |
| 529 |
mStartIndex = startIndex; |
| 530 |
mEndIndex = endIndex; |
| 531 |
mDefaultLocales = defaultLocales; |
| 532 |
mReferenceTime = referenceTime; |
| 533 |
mExtras = extras; |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
@NonNull |
| 541 |
public CharSequence getText() { |
| 542 |
return mText; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
@IntRange(from = 0) |
| 549 |
public int getStartIndex() { |
| 550 |
return mStartIndex; |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
@IntRange(from = 0) |
| 557 |
public int getEndIndex() { |
| 558 |
return mEndIndex; |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
@Nullable |
| 566 |
public LocaleList getDefaultLocales() { |
| 567 |
return mDefaultLocales; |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
@Nullable |
| 575 |
public ZonedDateTime getReferenceTime() { |
| 576 |
return mReferenceTime; |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
@Nullable |
| 584 |
public String getCallingPackageName() { |
| 585 |
return mSystemTcMetadata != null ? mSystemTcMetadata.getCallingPackageName() : null; |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 594 |
public void setSystemTextClassifierMetadata( |
| 595 |
@Nullable SystemTextClassifierMetadata systemTcMetadata) { |
| 596 |
mSystemTcMetadata = systemTcMetadata; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
@Nullable |
| 605 |
public SystemTextClassifierMetadata getSystemTextClassifierMetadata() { |
| 606 |
return mSystemTcMetadata; |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
@NonNull |
| 615 |
public Bundle getExtras() { |
| 616 |
return mExtras; |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
public static final class Builder { |
| 623 |
|
| 624 |
private final CharSequence mText; |
| 625 |
private final int mStartIndex; |
| 626 |
private final int mEndIndex; |
| 627 |
private Bundle mExtras; |
| 628 |
|
| 629 |
@Nullable private LocaleList mDefaultLocales; |
| 630 |
@Nullable private ZonedDateTime mReferenceTime; |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
public Builder( |
| 639 |
@NonNull CharSequence text, |
| 640 |
@IntRange(from = 0) int startIndex, |
| 641 |
@IntRange(from = 0) int endIndex) { |
| 642 |
Utils.checkArgument(text, startIndex, endIndex); |
| 643 |
mText = text; |
| 644 |
mStartIndex = startIndex; |
| 645 |
mEndIndex = endIndex; |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
@NonNull |
| 656 |
public Builder setDefaultLocales(@Nullable LocaleList defaultLocales) { |
| 657 |
mDefaultLocales = defaultLocales; |
| 658 |
return this; |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
@NonNull |
| 669 |
public Builder setReferenceTime(@Nullable ZonedDateTime referenceTime) { |
| 670 |
mReferenceTime = referenceTime; |
| 671 |
return this; |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
@NonNull |
| 680 |
public Builder setExtras(@Nullable Bundle extras) { |
| 681 |
mExtras = extras; |
| 682 |
return this; |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
@NonNull |
| 689 |
public Request build() { |
| 690 |
return new Request(new SpannedString(mText), mStartIndex, mEndIndex, |
| 691 |
mDefaultLocales, mReferenceTime, |
| 692 |
mExtras == null ? Bundle.EMPTY : mExtras); |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
@Override |
| 697 |
public int describeContents() { |
| 698 |
return 0; |
| 699 |
} |
| 700 |
|
| 701 |
@Override |
| 702 |
public void writeToParcel(Parcel dest, int flags) { |
| 703 |
dest.writeCharSequence(mText); |
| 704 |
dest.writeInt(mStartIndex); |
| 705 |
dest.writeInt(mEndIndex); |
| 706 |
dest.writeParcelable(mDefaultLocales, flags); |
| 707 |
dest.writeString(mReferenceTime == null ? null : mReferenceTime.toString()); |
| 708 |
dest.writeBundle(mExtras); |
| 709 |
dest.writeParcelable(mSystemTcMetadata, flags); |
| 710 |
} |
| 711 |
|
| 712 |
private static Request readFromParcel(Parcel in) { |
| 713 |
final CharSequence text = in.readCharSequence(); |
| 714 |
final int startIndex = in.readInt(); |
| 715 |
final int endIndex = in.readInt(); |
| 716 |
final LocaleList defaultLocales = in.readParcelable(null, android.os.LocaleList.class); |
| 717 |
final String referenceTimeString = in.readString(); |
| 718 |
final ZonedDateTime referenceTime = referenceTimeString == null |
| 719 |
? null : ZonedDateTime.parse(referenceTimeString); |
| 720 |
final Bundle extras = in.readBundle(); |
| 721 |
final SystemTextClassifierMetadata systemTcMetadata = in.readParcelable(null, android.view.textclassifier.SystemTextClassifierMetadata.class); |
| 722 |
|
| 723 |
final Request request = new Request(text, startIndex, endIndex, |
| 724 |
defaultLocales, referenceTime, extras); |
| 725 |
request.setSystemTextClassifierMetadata(systemTcMetadata); |
| 726 |
return request; |
| 727 |
} |
| 728 |
|
| 729 |
public static final @android.annotation.NonNull Parcelable.Creator<Request> CREATOR = |
| 730 |
new Parcelable.Creator<Request>() { |
| 731 |
@Override |
| 732 |
public Request createFromParcel(Parcel in) { |
| 733 |
return readFromParcel(in); |
| 734 |
} |
| 735 |
|
| 736 |
@Override |
| 737 |
public Request[] newArray(int size) { |
| 738 |
return new Request[size]; |
| 739 |
} |
| 740 |
}; |
| 741 |
} |
| 742 |
|
| 743 |
@Override |
| 744 |
public int describeContents() { |
| 745 |
return 0; |
| 746 |
} |
| 747 |
|
| 748 |
@Override |
| 749 |
public void writeToParcel(Parcel dest, int flags) { |
| 750 |
dest.writeString(mText); |
| 751 |
|
| 752 |
dest.writeTypedList(mActions); |
| 753 |
mEntityConfidence.writeToParcel(dest, flags); |
| 754 |
dest.writeString(mId); |
| 755 |
dest.writeBundle(mExtras); |
| 756 |
} |
| 757 |
|
| 758 |
public static final @android.annotation.NonNull Parcelable.Creator<TextClassification> CREATOR = |
| 759 |
new Parcelable.Creator<TextClassification>() { |
| 760 |
@Override |
| 761 |
public TextClassification createFromParcel(Parcel in) { |
| 762 |
return new TextClassification(in); |
| 763 |
} |
| 764 |
|
| 765 |
@Override |
| 766 |
public TextClassification[] newArray(int size) { |
| 767 |
return new TextClassification[size]; |
| 768 |
} |
| 769 |
}; |
| 770 |
|
| 771 |
private TextClassification(Parcel in) { |
| 772 |
mText = in.readString(); |
| 773 |
mActions = in.createTypedArrayList(RemoteAction.CREATOR); |
| 774 |
if (!mActions.isEmpty()) { |
| 775 |
final RemoteAction action = mActions.get(0); |
| 776 |
mLegacyIcon = maybeLoadDrawable(action.getIcon()); |
| 777 |
mLegacyLabel = action.getTitle().toString(); |
| 778 |
mLegacyOnClickListener = createIntentOnClickListener(mActions.get(0).getActionIntent()); |
| 779 |
} else { |
| 780 |
mLegacyIcon = null; |
| 781 |
mLegacyLabel = null; |
| 782 |
mLegacyOnClickListener = null; |
| 783 |
} |
| 784 |
mLegacyIntent = null; |
| 785 |
mEntityConfidence = EntityConfidence.CREATOR.createFromParcel(in); |
| 786 |
mId = in.readString(); |
| 787 |
mExtras = in.readBundle(); |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
@Nullable |
| 792 |
private static Drawable maybeLoadDrawable(Icon icon) { |
| 793 |
if (icon == null) { |
| 794 |
return null; |
| 795 |
} |
| 796 |
switch (icon.getType()) { |
| 797 |
case Icon.TYPE_BITMAP: |
| 798 |
return new BitmapDrawable(Resources.getSystem(), icon.getBitmap()); |
| 799 |
case Icon.TYPE_ADAPTIVE_BITMAP: |
| 800 |
return new AdaptiveIconDrawable(null, |
| 801 |
new BitmapDrawable(Resources.getSystem(), icon.getBitmap())); |
| 802 |
case Icon.TYPE_DATA: |
| 803 |
return new BitmapDrawable( |
| 804 |
Resources.getSystem(), |
| 805 |
BitmapFactory.decodeByteArray( |
| 806 |
icon.getDataBytes(), icon.getDataOffset(), icon.getDataLength())); |
| 807 |
} |
| 808 |
return null; |
| 809 |
} |
| 810 |
} |