| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content; |
| 18 |
|
| 19 |
import android.annotation.FloatRange; |
| 20 |
import android.annotation.IntDef; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.os.Bundle; |
| 23 |
import android.os.Parcel; |
| 24 |
import android.os.Parcelable; |
| 25 |
import android.os.PersistableBundle; |
| 26 |
import android.text.TextUtils; |
| 27 |
import android.util.ArrayMap; |
| 28 |
import android.util.TimeUtils; |
| 29 |
import android.util.proto.ProtoOutputStream; |
| 30 |
import android.view.textclassifier.TextClassifier; |
| 31 |
import android.view.textclassifier.TextLinks; |
| 32 |
|
| 33 |
import java.lang.annotation.Retention; |
| 34 |
import java.lang.annotation.RetentionPolicy; |
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.Arrays; |
| 37 |
import java.util.Map; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class ClipDescription implements Parcelable { |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public static final String MIMETYPE_TEXT_PLAIN = "text/plain"; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public static final String MIMETYPE_TEXT_HTML = "text/html"; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public static final String MIMETYPE_TEXT_URILIST = "text/uri-list"; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public static final String MIMETYPE_TEXT_INTENT = "text/vnd.android.intent"; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static final String MIMETYPE_APPLICATION_ACTIVITY = "application/vnd.android.activity"; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public static final String MIMETYPE_APPLICATION_SHORTCUT = "application/vnd.android.shortcut"; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public static final String MIMETYPE_APPLICATION_TASK = "application/vnd.android.task"; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public static final String MIMETYPE_UNKNOWN = "application/octet-stream"; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public static final String EXTRA_PENDING_INTENT = "android.intent.extra.PENDING_INTENT"; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public static final String EXTRA_ACTIVITY_OPTIONS = "android.intent.extra.ACTIVITY_OPTIONS"; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public static final String EXTRA_LOGGING_INSTANCE_ID = |
| 135 |
"android.intent.extra.LOGGING_INSTANCE_ID"; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public static final String EXTRA_IS_SENSITIVE = "android.content.extra.IS_SENSITIVE"; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public static final String EXTRA_IS_REMOTE_DEVICE = "android.content.extra.IS_REMOTE_DEVICE"; |
| 164 |
|
| 165 |
|
| 166 |
@Retention(RetentionPolicy.SOURCE) |
| 167 |
@IntDef(value = |
| 168 |
{ CLASSIFICATION_NOT_COMPLETE, CLASSIFICATION_NOT_PERFORMED, CLASSIFICATION_COMPLETE}) |
| 169 |
@interface ClassificationStatus {} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public static final int CLASSIFICATION_NOT_COMPLETE = 1; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public static final int CLASSIFICATION_NOT_PERFORMED = 2; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public static final int CLASSIFICATION_COMPLETE = 3; |
| 190 |
|
| 191 |
final CharSequence mLabel; |
| 192 |
private final ArrayList<String> mMimeTypes; |
| 193 |
private PersistableBundle mExtras; |
| 194 |
private long mTimeStamp; |
| 195 |
private boolean mIsStyledText; |
| 196 |
private final ArrayMap<String, Float> mEntityConfidence = new ArrayMap<>(); |
| 197 |
private int mClassificationStatus = CLASSIFICATION_NOT_COMPLETE; |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public ClipDescription(CharSequence label, String[] mimeTypes) { |
| 206 |
if (mimeTypes == null) { |
| 207 |
throw new NullPointerException("mimeTypes is null"); |
| 208 |
} |
| 209 |
mLabel = label; |
| 210 |
mMimeTypes = new ArrayList<String>(Arrays.asList(mimeTypes)); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public ClipDescription(ClipDescription o) { |
| 217 |
mLabel = o.mLabel; |
| 218 |
mMimeTypes = new ArrayList<String>(o.mMimeTypes); |
| 219 |
mTimeStamp = o.mTimeStamp; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public static boolean compareMimeTypes(String concreteType, String desiredType) { |
| 229 |
final int typeLength = desiredType.length(); |
| 230 |
if (typeLength == 3 && desiredType.equals("*/*")) { |
| 231 |
return true; |
| 232 |
} |
| 233 |
|
| 234 |
final int slashpos = desiredType.indexOf('/'); |
| 235 |
if (slashpos > 0) { |
| 236 |
if (typeLength == slashpos+2 && desiredType.charAt(slashpos+1) == '*') { |
| 237 |
if (desiredType.regionMatches(0, concreteType, 0, slashpos+1)) { |
| 238 |
return true; |
| 239 |
} |
| 240 |
} else if (desiredType.equals(concreteType)) { |
| 241 |
return true; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public void setTimestamp(long timeStamp) { |
| 257 |
mTimeStamp = timeStamp; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public long getTimestamp() { |
| 268 |
return mTimeStamp; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public CharSequence getLabel() { |
| 275 |
return mLabel; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public boolean hasMimeType(String mimeType) { |
| 286 |
final int size = mMimeTypes.size(); |
| 287 |
for (int i=0; i<size; i++) { |
| 288 |
if (compareMimeTypes(mMimeTypes.get(i), mimeType)) { |
| 289 |
return true; |
| 290 |
} |
| 291 |
} |
| 292 |
return false; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
public boolean hasMimeType(@NonNull String[] targetMimeTypes) { |
| 305 |
for (String targetMimeType : targetMimeTypes) { |
| 306 |
if (hasMimeType(targetMimeType)) { |
| 307 |
return true; |
| 308 |
} |
| 309 |
} |
| 310 |
return false; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public String[] filterMimeTypes(String mimeType) { |
| 322 |
ArrayList<String> array = null; |
| 323 |
final int size = mMimeTypes.size(); |
| 324 |
for (int i=0; i<size; i++) { |
| 325 |
if (compareMimeTypes(mMimeTypes.get(i), mimeType)) { |
| 326 |
if (array == null) { |
| 327 |
array = new ArrayList<String>(); |
| 328 |
} |
| 329 |
array.add(mMimeTypes.get(i)); |
| 330 |
} |
| 331 |
} |
| 332 |
if (array == null) { |
| 333 |
return null; |
| 334 |
} |
| 335 |
String[] rawArray = new String[array.size()]; |
| 336 |
array.toArray(rawArray); |
| 337 |
return rawArray; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
public int getMimeTypeCount() { |
| 344 |
return mMimeTypes.size(); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
public String getMimeType(int index) { |
| 351 |
return mMimeTypes.get(index); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
void addMimeTypes(String[] mimeTypes) { |
| 358 |
for (int i=0; i!=mimeTypes.length; i++) { |
| 359 |
final String mimeType = mimeTypes[i]; |
| 360 |
if (!mMimeTypes.contains(mimeType)) { |
| 361 |
mMimeTypes.add(mimeType); |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
public PersistableBundle getExtras() { |
| 375 |
return mExtras; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
public void setExtras(PersistableBundle extras) { |
| 384 |
mExtras = new PersistableBundle(extras); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
public void validate() { |
| 389 |
if (mMimeTypes == null) { |
| 390 |
throw new NullPointerException("null mime types"); |
| 391 |
} |
| 392 |
final int size = mMimeTypes.size(); |
| 393 |
if (size <= 0) { |
| 394 |
throw new IllegalArgumentException("must have at least 1 mime type"); |
| 395 |
} |
| 396 |
for (int i=0; i<size; i++) { |
| 397 |
if (mMimeTypes.get(i) == null) { |
| 398 |
throw new NullPointerException("mime type at " + i + " is null"); |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
public boolean isStyledText() { |
| 411 |
return mIsStyledText; |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
void setIsStyledText(boolean isStyledText) { |
| 420 |
mIsStyledText = isStyledText; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
public void setClassificationStatus(@ClassificationStatus int status) { |
| 429 |
mClassificationStatus = status; |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
@FloatRange(from = 0.0, to = 1.0) |
| 450 |
public float getConfidenceScore(@NonNull @TextClassifier.EntityType String entity) { |
| 451 |
if (mClassificationStatus != CLASSIFICATION_COMPLETE) { |
| 452 |
throw new IllegalStateException("Classification not complete"); |
| 453 |
} |
| 454 |
return mEntityConfidence.getOrDefault(entity, 0f); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
public @ClassificationStatus int getClassificationStatus() { |
| 466 |
return mClassificationStatus; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
public void setConfidenceScores(Map<String, Float> confidences) { |
| 473 |
mEntityConfidence.clear(); |
| 474 |
mEntityConfidence.putAll(confidences); |
| 475 |
mClassificationStatus = CLASSIFICATION_COMPLETE; |
| 476 |
} |
| 477 |
|
| 478 |
@Override |
| 479 |
public String toString() { |
| 480 |
StringBuilder b = new StringBuilder(128); |
| 481 |
|
| 482 |
b.append("ClipDescription { "); |
| 483 |
toShortString(b, true); |
| 484 |
b.append(" }"); |
| 485 |
|
| 486 |
return b.toString(); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
public boolean toShortString(StringBuilder b, boolean redactContent) { |
| 495 |
boolean first = !toShortStringTypesOnly(b); |
| 496 |
if (mLabel != null) { |
| 497 |
if (!first) { |
| 498 |
b.append(' '); |
| 499 |
} |
| 500 |
first = false; |
| 501 |
if (redactContent) { |
| 502 |
b.append("hasLabel(").append(mLabel.length()).append(')'); |
| 503 |
} else { |
| 504 |
b.append('"').append(mLabel).append('"'); |
| 505 |
} |
| 506 |
} |
| 507 |
if (mExtras != null) { |
| 508 |
if (!first) { |
| 509 |
b.append(' '); |
| 510 |
} |
| 511 |
first = false; |
| 512 |
if (redactContent) { |
| 513 |
if (mExtras.isParcelled()) { |
| 514 |
|
| 515 |
b.append("hasExtras"); |
| 516 |
} else { |
| 517 |
b.append("hasExtras(").append(mExtras.size()).append(')'); |
| 518 |
} |
| 519 |
} else { |
| 520 |
b.append(mExtras.toString()); |
| 521 |
} |
| 522 |
} |
| 523 |
if (mTimeStamp > 0) { |
| 524 |
if (!first) { |
| 525 |
b.append(' '); |
| 526 |
} |
| 527 |
first = false; |
| 528 |
b.append('<'); |
| 529 |
b.append(TimeUtils.logTimeOfDay(mTimeStamp)); |
| 530 |
b.append('>'); |
| 531 |
} |
| 532 |
return !first; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
public boolean toShortStringTypesOnly(StringBuilder b) { |
| 537 |
boolean first = true; |
| 538 |
final int size = mMimeTypes.size(); |
| 539 |
for (int i=0; i<size; i++) { |
| 540 |
if (!first) { |
| 541 |
b.append(' '); |
| 542 |
} |
| 543 |
first = false; |
| 544 |
b.append(mMimeTypes.get(i)); |
| 545 |
} |
| 546 |
return !first; |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
public void dumpDebug(ProtoOutputStream proto, long fieldId) { |
| 551 |
final long token = proto.start(fieldId); |
| 552 |
|
| 553 |
final int size = mMimeTypes.size(); |
| 554 |
for (int i = 0; i < size; i++) { |
| 555 |
proto.write(ClipDescriptionProto.MIME_TYPES, mMimeTypes.get(i)); |
| 556 |
} |
| 557 |
|
| 558 |
if (mLabel != null) { |
| 559 |
proto.write(ClipDescriptionProto.LABEL, mLabel.toString()); |
| 560 |
} |
| 561 |
if (mExtras != null) { |
| 562 |
mExtras.dumpDebug(proto, ClipDescriptionProto.EXTRAS); |
| 563 |
} |
| 564 |
if (mTimeStamp > 0) { |
| 565 |
proto.write(ClipDescriptionProto.TIMESTAMP_MS, mTimeStamp); |
| 566 |
} |
| 567 |
|
| 568 |
proto.end(token); |
| 569 |
} |
| 570 |
|
| 571 |
@Override |
| 572 |
public int describeContents() { |
| 573 |
return 0; |
| 574 |
} |
| 575 |
|
| 576 |
@Override |
| 577 |
public void writeToParcel(Parcel dest, int flags) { |
| 578 |
TextUtils.writeToParcel(mLabel, dest, flags); |
| 579 |
dest.writeStringList(mMimeTypes); |
| 580 |
dest.writePersistableBundle(mExtras); |
| 581 |
dest.writeLong(mTimeStamp); |
| 582 |
dest.writeBoolean(mIsStyledText); |
| 583 |
dest.writeInt(mClassificationStatus); |
| 584 |
dest.writeBundle(confidencesToBundle()); |
| 585 |
} |
| 586 |
|
| 587 |
private Bundle confidencesToBundle() { |
| 588 |
Bundle bundle = new Bundle(); |
| 589 |
int size = mEntityConfidence.size(); |
| 590 |
for (int i = 0; i < size; i++) { |
| 591 |
bundle.putFloat(mEntityConfidence.keyAt(i), mEntityConfidence.valueAt(i)); |
| 592 |
} |
| 593 |
return bundle; |
| 594 |
} |
| 595 |
|
| 596 |
private void readBundleToConfidences(Bundle bundle) { |
| 597 |
for (String key : bundle.keySet()) { |
| 598 |
mEntityConfidence.put(key, bundle.getFloat(key)); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
ClipDescription(Parcel in) { |
| 603 |
mLabel = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
| 604 |
mMimeTypes = in.createStringArrayList(); |
| 605 |
mExtras = in.readPersistableBundle(); |
| 606 |
mTimeStamp = in.readLong(); |
| 607 |
mIsStyledText = in.readBoolean(); |
| 608 |
mClassificationStatus = in.readInt(); |
| 609 |
readBundleToConfidences(in.readBundle()); |
| 610 |
} |
| 611 |
|
| 612 |
public static final @android.annotation.NonNull Parcelable.Creator<ClipDescription> CREATOR = |
| 613 |
new Parcelable.Creator<ClipDescription>() { |
| 614 |
|
| 615 |
public ClipDescription createFromParcel(Parcel source) { |
| 616 |
return new ClipDescription(source); |
| 617 |
} |
| 618 |
|
| 619 |
public ClipDescription[] newArray(int size) { |
| 620 |
return new ClipDescription[size]; |
| 621 |
} |
| 622 |
}; |
| 623 |
} |