| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content; |
| 18 |
|
| 19 |
import static android.content.ContentProvider.maybeAddUserId; |
| 20 |
import static android.content.ContentResolver.SCHEME_ANDROID_RESOURCE; |
| 21 |
import static android.content.ContentResolver.SCHEME_CONTENT; |
| 22 |
import static android.content.ContentResolver.SCHEME_FILE; |
| 23 |
|
| 24 |
import android.annotation.Nullable; |
| 25 |
import android.compat.annotation.UnsupportedAppUsage; |
| 26 |
import android.content.pm.ActivityInfo; |
| 27 |
import android.content.res.AssetFileDescriptor; |
| 28 |
import android.graphics.Bitmap; |
| 29 |
import android.net.Uri; |
| 30 |
import android.os.Build; |
| 31 |
import android.os.Parcel; |
| 32 |
import android.os.Parcelable; |
| 33 |
import android.os.StrictMode; |
| 34 |
import android.text.Html; |
| 35 |
import android.text.Spannable; |
| 36 |
import android.text.SpannableStringBuilder; |
| 37 |
import android.text.Spanned; |
| 38 |
import android.text.TextUtils; |
| 39 |
import android.text.style.URLSpan; |
| 40 |
import android.util.Log; |
| 41 |
import android.util.proto.ProtoOutputStream; |
| 42 |
import android.view.textclassifier.TextLinks; |
| 43 |
|
| 44 |
import com.android.internal.util.ArrayUtils; |
| 45 |
|
| 46 |
import libcore.io.IoUtils; |
| 47 |
|
| 48 |
import java.io.FileInputStream; |
| 49 |
import java.io.FileNotFoundException; |
| 50 |
import java.io.IOException; |
| 51 |
import java.io.InputStreamReader; |
| 52 |
import java.util.ArrayList; |
| 53 |
import java.util.List; |
| 54 |
|
| 55 |
|
| 56 |
|
| 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 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public class ClipData implements Parcelable { |
| 167 |
static final String[] MIMETYPES_TEXT_PLAIN = new String[] { |
| 168 |
ClipDescription.MIMETYPE_TEXT_PLAIN }; |
| 169 |
static final String[] MIMETYPES_TEXT_HTML = new String[] { |
| 170 |
ClipDescription.MIMETYPE_TEXT_HTML }; |
| 171 |
static final String[] MIMETYPES_TEXT_URILIST = new String[] { |
| 172 |
ClipDescription.MIMETYPE_TEXT_URILIST }; |
| 173 |
static final String[] MIMETYPES_TEXT_INTENT = new String[] { |
| 174 |
ClipDescription.MIMETYPE_TEXT_INTENT }; |
| 175 |
|
| 176 |
final ClipDescription mClipDescription; |
| 177 |
|
| 178 |
final Bitmap mIcon; |
| 179 |
|
| 180 |
final ArrayList<Item> mItems; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
private boolean mParcelItemActivityInfos; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public static class Item { |
| 206 |
final CharSequence mText; |
| 207 |
final String mHtmlText; |
| 208 |
final Intent mIntent; |
| 209 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 210 |
Uri mUri; |
| 211 |
private TextLinks mTextLinks; |
| 212 |
|
| 213 |
|
| 214 |
private ActivityInfo mActivityInfo; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public Item(Item other) { |
| 219 |
mText = other.mText; |
| 220 |
mHtmlText = other.mHtmlText; |
| 221 |
mIntent = other.mIntent; |
| 222 |
mUri = other.mUri; |
| 223 |
mActivityInfo = other.mActivityInfo; |
| 224 |
mTextLinks = other.mTextLinks; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public Item(CharSequence text) { |
| 231 |
mText = text; |
| 232 |
mHtmlText = null; |
| 233 |
mIntent = null; |
| 234 |
mUri = null; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public Item(CharSequence text, String htmlText) { |
| 247 |
mText = text; |
| 248 |
mHtmlText = htmlText; |
| 249 |
mIntent = null; |
| 250 |
mUri = null; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public Item(Intent intent) { |
| 257 |
mText = null; |
| 258 |
mHtmlText = null; |
| 259 |
mIntent = intent; |
| 260 |
mUri = null; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public Item(Uri uri) { |
| 267 |
mText = null; |
| 268 |
mHtmlText = null; |
| 269 |
mIntent = null; |
| 270 |
mUri = uri; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public Item(CharSequence text, Intent intent, Uri uri) { |
| 278 |
mText = text; |
| 279 |
mHtmlText = null; |
| 280 |
mIntent = intent; |
| 281 |
mUri = uri; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public Item(CharSequence text, String htmlText, Intent intent, Uri uri) { |
| 291 |
if (htmlText != null && text == null) { |
| 292 |
throw new IllegalArgumentException( |
| 293 |
"Plain text must be supplied if HTML text is supplied"); |
| 294 |
} |
| 295 |
mText = text; |
| 296 |
mHtmlText = htmlText; |
| 297 |
mIntent = intent; |
| 298 |
mUri = uri; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
public CharSequence getText() { |
| 305 |
return mText; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
public String getHtmlText() { |
| 312 |
return mHtmlText; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
public Intent getIntent() { |
| 319 |
return mIntent; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
public Uri getUri() { |
| 326 |
return mUri; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public ActivityInfo getActivityInfo() { |
| 334 |
return mActivityInfo; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
public void setActivityInfo(ActivityInfo info) { |
| 342 |
mActivityInfo = info; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
@Nullable |
| 357 |
public TextLinks getTextLinks() { |
| 358 |
return mTextLinks; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
public void setTextLinks(TextLinks textLinks) { |
| 365 |
mTextLinks = textLinks; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
public CharSequence coerceToText(Context context) { |
| 391 |
|
| 392 |
CharSequence text = getText(); |
| 393 |
if (text != null) { |
| 394 |
return text; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
Uri uri = getUri(); |
| 399 |
if (uri != null) { |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
final ContentResolver resolver = context.getContentResolver(); |
| 404 |
AssetFileDescriptor descr = null; |
| 405 |
FileInputStream stream = null; |
| 406 |
InputStreamReader reader = null; |
| 407 |
try { |
| 408 |
try { |
| 409 |
|
| 410 |
descr = resolver.openTypedAssetFileDescriptor(uri, "text/*", null); |
| 411 |
} catch (SecurityException e) { |
| 412 |
Log.w("ClipData", "Failure opening stream", e); |
| 413 |
} catch (FileNotFoundException|RuntimeException e) { |
| 414 |
|
| 415 |
|
| 416 |
} |
| 417 |
if (descr != null) { |
| 418 |
try { |
| 419 |
stream = descr.createInputStream(); |
| 420 |
reader = new InputStreamReader(stream, "UTF-8"); |
| 421 |
|
| 422 |
|
| 423 |
final StringBuilder builder = new StringBuilder(128); |
| 424 |
char[] buffer = new char[8192]; |
| 425 |
int len; |
| 426 |
while ((len=reader.read(buffer)) > 0) { |
| 427 |
builder.append(buffer, 0, len); |
| 428 |
} |
| 429 |
return builder.toString(); |
| 430 |
} catch (IOException e) { |
| 431 |
|
| 432 |
Log.w("ClipData", "Failure loading text", e); |
| 433 |
return e.toString(); |
| 434 |
} |
| 435 |
} |
| 436 |
} finally { |
| 437 |
IoUtils.closeQuietly(descr); |
| 438 |
IoUtils.closeQuietly(stream); |
| 439 |
IoUtils.closeQuietly(reader); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
final String scheme = uri.getScheme(); |
| 445 |
if (SCHEME_CONTENT.equals(scheme) |
| 446 |
|| SCHEME_ANDROID_RESOURCE.equals(scheme) |
| 447 |
|| SCHEME_FILE.equals(scheme)) { |
| 448 |
return ""; |
| 449 |
} |
| 450 |
return uri.toString(); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
Intent intent = getIntent(); |
| 456 |
if (intent != null) { |
| 457 |
return intent.toUri(Intent.URI_INTENT_SCHEME); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
return ""; |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
public CharSequence coerceToStyledText(Context context) { |
| 474 |
CharSequence text = getText(); |
| 475 |
if (text instanceof Spanned) { |
| 476 |
return text; |
| 477 |
} |
| 478 |
String htmlText = getHtmlText(); |
| 479 |
if (htmlText != null) { |
| 480 |
try { |
| 481 |
CharSequence newText = Html.fromHtml(htmlText); |
| 482 |
if (newText != null) { |
| 483 |
return newText; |
| 484 |
} |
| 485 |
} catch (RuntimeException e) { |
| 486 |
|
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
if (text != null) { |
| 491 |
return text; |
| 492 |
} |
| 493 |
return coerceToHtmlOrStyledText(context, true); |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
public String coerceToHtmlText(Context context) { |
| 524 |
|
| 525 |
String htmlText = getHtmlText(); |
| 526 |
if (htmlText != null) { |
| 527 |
return htmlText; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
CharSequence text = getText(); |
| 532 |
if (text != null) { |
| 533 |
if (text instanceof Spanned) { |
| 534 |
return Html.toHtml((Spanned)text); |
| 535 |
} |
| 536 |
return Html.escapeHtml(text); |
| 537 |
} |
| 538 |
|
| 539 |
text = coerceToHtmlOrStyledText(context, false); |
| 540 |
return text != null ? text.toString() : null; |
| 541 |
} |
| 542 |
|
| 543 |
private CharSequence coerceToHtmlOrStyledText(Context context, boolean styled) { |
| 544 |
|
| 545 |
if (mUri != null) { |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
String[] types = null; |
| 551 |
try { |
| 552 |
types = context.getContentResolver().getStreamTypes(mUri, "text/*"); |
| 553 |
} catch (SecurityException e) { |
| 554 |
|
| 555 |
} |
| 556 |
boolean hasHtml = false; |
| 557 |
boolean hasText = false; |
| 558 |
if (types != null) { |
| 559 |
for (String type : types) { |
| 560 |
if ("text/html".equals(type)) { |
| 561 |
hasHtml = true; |
| 562 |
} else if (type.startsWith("text/")) { |
| 563 |
hasText = true; |
| 564 |
} |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
if (hasHtml || hasText) { |
| 570 |
FileInputStream stream = null; |
| 571 |
try { |
| 572 |
|
| 573 |
AssetFileDescriptor descr = context.getContentResolver() |
| 574 |
.openTypedAssetFileDescriptor(mUri, |
| 575 |
hasHtml ? "text/html" : "text/plain", null); |
| 576 |
stream = descr.createInputStream(); |
| 577 |
InputStreamReader reader = new InputStreamReader(stream, "UTF-8"); |
| 578 |
|
| 579 |
|
| 580 |
StringBuilder builder = new StringBuilder(128); |
| 581 |
char[] buffer = new char[8192]; |
| 582 |
int len; |
| 583 |
while ((len=reader.read(buffer)) > 0) { |
| 584 |
builder.append(buffer, 0, len); |
| 585 |
} |
| 586 |
String text = builder.toString(); |
| 587 |
if (hasHtml) { |
| 588 |
if (styled) { |
| 589 |
|
| 590 |
|
| 591 |
try { |
| 592 |
CharSequence newText = Html.fromHtml(text); |
| 593 |
return newText != null ? newText : text; |
| 594 |
} catch (RuntimeException e) { |
| 595 |
return text; |
| 596 |
} |
| 597 |
} else { |
| 598 |
|
| 599 |
|
| 600 |
return text.toString(); |
| 601 |
} |
| 602 |
} |
| 603 |
if (styled) { |
| 604 |
|
| 605 |
|
| 606 |
return text; |
| 607 |
} else { |
| 608 |
|
| 609 |
|
| 610 |
return Html.escapeHtml(text); |
| 611 |
} |
| 612 |
|
| 613 |
} catch (SecurityException e) { |
| 614 |
Log.w("ClipData", "Failure opening stream", e); |
| 615 |
|
| 616 |
} catch (FileNotFoundException e) { |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
} catch (IOException e) { |
| 621 |
|
| 622 |
Log.w("ClipData", "Failure loading text", e); |
| 623 |
return Html.escapeHtml(e.toString()); |
| 624 |
|
| 625 |
} finally { |
| 626 |
if (stream != null) { |
| 627 |
try { |
| 628 |
stream.close(); |
| 629 |
} catch (IOException e) { |
| 630 |
} |
| 631 |
} |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
final String scheme = mUri.getScheme(); |
| 638 |
if (SCHEME_CONTENT.equals(scheme) |
| 639 |
|| SCHEME_ANDROID_RESOURCE.equals(scheme) |
| 640 |
|| SCHEME_FILE.equals(scheme)) { |
| 641 |
return ""; |
| 642 |
} |
| 643 |
|
| 644 |
if (styled) { |
| 645 |
return uriToStyledText(mUri.toString()); |
| 646 |
} else { |
| 647 |
return uriToHtml(mUri.toString()); |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
if (mIntent != null) { |
| 654 |
if (styled) { |
| 655 |
return uriToStyledText(mIntent.toUri(Intent.URI_INTENT_SCHEME)); |
| 656 |
} else { |
| 657 |
return uriToHtml(mIntent.toUri(Intent.URI_INTENT_SCHEME)); |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
return ""; |
| 663 |
} |
| 664 |
|
| 665 |
private String uriToHtml(String uri) { |
| 666 |
StringBuilder builder = new StringBuilder(256); |
| 667 |
builder.append("<a href=\""); |
| 668 |
builder.append(Html.escapeHtml(uri)); |
| 669 |
builder.append("\">"); |
| 670 |
builder.append(Html.escapeHtml(uri)); |
| 671 |
builder.append("</a>"); |
| 672 |
return builder.toString(); |
| 673 |
} |
| 674 |
|
| 675 |
private CharSequence uriToStyledText(String uri) { |
| 676 |
SpannableStringBuilder builder = new SpannableStringBuilder(); |
| 677 |
builder.append(uri); |
| 678 |
builder.setSpan(new URLSpan(uri), 0, builder.length(), |
| 679 |
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 680 |
return builder; |
| 681 |
} |
| 682 |
|
| 683 |
@Override |
| 684 |
public String toString() { |
| 685 |
StringBuilder b = new StringBuilder(128); |
| 686 |
|
| 687 |
b.append("ClipData.Item { "); |
| 688 |
toShortString(b, true); |
| 689 |
b.append(" }"); |
| 690 |
|
| 691 |
return b.toString(); |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
public void toShortString(StringBuilder b, boolean redactContent) { |
| 701 |
boolean first = true; |
| 702 |
if (mHtmlText != null) { |
| 703 |
first = false; |
| 704 |
if (redactContent) { |
| 705 |
b.append("H(").append(mHtmlText.length()).append(')'); |
| 706 |
} else { |
| 707 |
b.append("H:").append(mHtmlText); |
| 708 |
} |
| 709 |
} |
| 710 |
if (mText != null) { |
| 711 |
if (!first) { |
| 712 |
b.append(' '); |
| 713 |
} |
| 714 |
first = false; |
| 715 |
if (redactContent) { |
| 716 |
b.append("T(").append(mText.length()).append(')'); |
| 717 |
} else { |
| 718 |
b.append("T:").append(mText); |
| 719 |
} |
| 720 |
} |
| 721 |
if (mUri != null) { |
| 722 |
if (!first) { |
| 723 |
b.append(' '); |
| 724 |
} |
| 725 |
first = false; |
| 726 |
if (redactContent) { |
| 727 |
b.append("U(").append(mUri.getScheme()).append(')'); |
| 728 |
} else { |
| 729 |
b.append("U:").append(mUri); |
| 730 |
} |
| 731 |
} |
| 732 |
if (mIntent != null) { |
| 733 |
if (!first) { |
| 734 |
b.append(' '); |
| 735 |
} |
| 736 |
first = false; |
| 737 |
b.append("I:"); |
| 738 |
mIntent.toShortString(b, redactContent, true, true, true); |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
public void dumpDebug(ProtoOutputStream proto, long fieldId) { |
| 744 |
final long token = proto.start(fieldId); |
| 745 |
|
| 746 |
if (mHtmlText != null) { |
| 747 |
proto.write(ClipDataProto.Item.HTML_TEXT, mHtmlText); |
| 748 |
} else if (mText != null) { |
| 749 |
proto.write(ClipDataProto.Item.TEXT, mText.toString()); |
| 750 |
} else if (mUri != null) { |
| 751 |
proto.write(ClipDataProto.Item.URI, mUri.toString()); |
| 752 |
} else if (mIntent != null) { |
| 753 |
mIntent.dumpDebug(proto, ClipDataProto.Item.INTENT, true, true, true, true); |
| 754 |
} else { |
| 755 |
proto.write(ClipDataProto.Item.NOTHING, true); |
| 756 |
} |
| 757 |
|
| 758 |
proto.end(token); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
public ClipData(CharSequence label, String[] mimeTypes, Item item) { |
| 770 |
mClipDescription = new ClipDescription(label, mimeTypes); |
| 771 |
if (item == null) { |
| 772 |
throw new NullPointerException("item is null"); |
| 773 |
} |
| 774 |
mIcon = null; |
| 775 |
mItems = new ArrayList<Item>(); |
| 776 |
mItems.add(item); |
| 777 |
mClipDescription.setIsStyledText(isStyledText()); |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
public ClipData(ClipDescription description, Item item) { |
| 787 |
mClipDescription = description; |
| 788 |
if (item == null) { |
| 789 |
throw new NullPointerException("item is null"); |
| 790 |
} |
| 791 |
mIcon = null; |
| 792 |
mItems = new ArrayList<Item>(); |
| 793 |
mItems.add(item); |
| 794 |
mClipDescription.setIsStyledText(isStyledText()); |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
public ClipData(ClipDescription description, ArrayList<Item> items) { |
| 807 |
mClipDescription = description; |
| 808 |
if (items == null) { |
| 809 |
throw new NullPointerException("item is null"); |
| 810 |
} |
| 811 |
mIcon = null; |
| 812 |
mItems = items; |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
public ClipData(ClipData other) { |
| 822 |
mClipDescription = other.mClipDescription; |
| 823 |
mIcon = other.mIcon; |
| 824 |
mItems = new ArrayList<Item>(other.mItems); |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
public ClipData copyForTransferWithActivityInfo() { |
| 832 |
ClipData copy = new ClipData(this); |
| 833 |
copy.mParcelItemActivityInfos = true; |
| 834 |
return copy; |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
public boolean willParcelWithActivityInfo() { |
| 842 |
return mParcelItemActivityInfos; |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
static public ClipData newPlainText(CharSequence label, CharSequence text) { |
| 854 |
Item item = new Item(text); |
| 855 |
return new ClipData(label, MIMETYPES_TEXT_PLAIN, item); |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
static public ClipData newHtmlText(CharSequence label, CharSequence text, |
| 869 |
String htmlText) { |
| 870 |
Item item = new Item(text, htmlText); |
| 871 |
return new ClipData(label, MIMETYPES_TEXT_HTML, item); |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
static public ClipData newIntent(CharSequence label, Intent intent) { |
| 883 |
Item item = new Item(intent); |
| 884 |
return new ClipData(label, MIMETYPES_TEXT_INTENT, item); |
| 885 |
} |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
static public ClipData newUri(ContentResolver resolver, CharSequence label, |
| 899 |
Uri uri) { |
| 900 |
Item item = new Item(uri); |
| 901 |
String[] mimeTypes = getMimeTypes(resolver, uri); |
| 902 |
return new ClipData(label, mimeTypes, item); |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
private static String[] getMimeTypes(ContentResolver resolver, Uri uri) { |
| 913 |
String[] mimeTypes = null; |
| 914 |
if (SCHEME_CONTENT.equals(uri.getScheme())) { |
| 915 |
String realType = resolver.getType(uri); |
| 916 |
mimeTypes = resolver.getStreamTypes(uri, "*/*"); |
| 917 |
if (realType != null) { |
| 918 |
if (mimeTypes == null) { |
| 919 |
mimeTypes = new String[] { realType }; |
| 920 |
} else if (!ArrayUtils.contains(mimeTypes, realType)) { |
| 921 |
String[] tmp = new String[mimeTypes.length + 1]; |
| 922 |
tmp[0] = realType; |
| 923 |
System.arraycopy(mimeTypes, 0, tmp, 1, mimeTypes.length); |
| 924 |
mimeTypes = tmp; |
| 925 |
} |
| 926 |
} |
| 927 |
} |
| 928 |
if (mimeTypes == null) { |
| 929 |
mimeTypes = MIMETYPES_TEXT_URILIST; |
| 930 |
} |
| 931 |
return mimeTypes; |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
static public ClipData newRawUri(CharSequence label, Uri uri) { |
| 946 |
Item item = new Item(uri); |
| 947 |
return new ClipData(label, MIMETYPES_TEXT_URILIST, item); |
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
public ClipDescription getDescription() { |
| 955 |
return mClipDescription; |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
public void addItem(Item item) { |
| 967 |
if (item == null) { |
| 968 |
throw new NullPointerException("item is null"); |
| 969 |
} |
| 970 |
mItems.add(item); |
| 971 |
if (mItems.size() == 1) { |
| 972 |
mClipDescription.setIsStyledText(isStyledText()); |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
public void addItem(ContentResolver resolver, Item item) { |
| 985 |
addItem(item); |
| 986 |
|
| 987 |
if (item.getHtmlText() != null) { |
| 988 |
mClipDescription.addMimeTypes(MIMETYPES_TEXT_HTML); |
| 989 |
} else if (item.getText() != null) { |
| 990 |
mClipDescription.addMimeTypes(MIMETYPES_TEXT_PLAIN); |
| 991 |
} |
| 992 |
|
| 993 |
if (item.getIntent() != null) { |
| 994 |
mClipDescription.addMimeTypes(MIMETYPES_TEXT_INTENT); |
| 995 |
} |
| 996 |
|
| 997 |
if (item.getUri() != null) { |
| 998 |
mClipDescription.addMimeTypes(getMimeTypes(resolver, item.getUri())); |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
|
| 1003 |
@UnsupportedAppUsage |
| 1004 |
public Bitmap getIcon() { |
| 1005 |
return mIcon; |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
public int getItemCount() { |
| 1012 |
return mItems.size(); |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
public Item getItemAt(int index) { |
| 1020 |
return mItems.get(index); |
| 1021 |
} |
| 1022 |
|
| 1023 |
|
| 1024 |
public void setItemAt(int index, Item item) { |
| 1025 |
mItems.set(index, item); |
| 1026 |
} |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
public void prepareToLeaveProcess(boolean leavingPackage) { |
| 1034 |
|
| 1035 |
prepareToLeaveProcess(leavingPackage, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
public void prepareToLeaveProcess(boolean leavingPackage, int intentFlags) { |
| 1044 |
final int size = mItems.size(); |
| 1045 |
for (int i = 0; i < size; i++) { |
| 1046 |
final Item item = mItems.get(i); |
| 1047 |
if (item.mIntent != null) { |
| 1048 |
item.mIntent.prepareToLeaveProcess(leavingPackage); |
| 1049 |
} |
| 1050 |
if (item.mUri != null && leavingPackage) { |
| 1051 |
if (StrictMode.vmFileUriExposureEnabled()) { |
| 1052 |
item.mUri.checkFileUriExposed("ClipData.Item.getUri()"); |
| 1053 |
} |
| 1054 |
if (StrictMode.vmContentUriWithoutPermissionEnabled()) { |
| 1055 |
item.mUri.checkContentUriWithoutPermission("ClipData.Item.getUri()", |
| 1056 |
intentFlags); |
| 1057 |
} |
| 1058 |
} |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
|
| 1063 |
public void prepareToEnterProcess(AttributionSource source) { |
| 1064 |
final int size = mItems.size(); |
| 1065 |
for (int i = 0; i < size; i++) { |
| 1066 |
final Item item = mItems.get(i); |
| 1067 |
if (item.mIntent != null) { |
| 1068 |
|
| 1069 |
|
| 1070 |
item.mIntent.prepareToEnterProcess(false, source); |
| 1071 |
} |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
|
| 1076 |
public void fixUris(int contentUserHint) { |
| 1077 |
final int size = mItems.size(); |
| 1078 |
for (int i = 0; i < size; i++) { |
| 1079 |
final Item item = mItems.get(i); |
| 1080 |
if (item.mIntent != null) { |
| 1081 |
item.mIntent.fixUris(contentUserHint); |
| 1082 |
} |
| 1083 |
if (item.mUri != null) { |
| 1084 |
item.mUri = maybeAddUserId(item.mUri, contentUserHint); |
| 1085 |
} |
| 1086 |
} |
| 1087 |
} |
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
public void fixUrisLight(int contentUserHint) { |
| 1094 |
final int size = mItems.size(); |
| 1095 |
for (int i = 0; i < size; i++) { |
| 1096 |
final Item item = mItems.get(i); |
| 1097 |
if (item.mIntent != null) { |
| 1098 |
Uri data = item.mIntent.getData(); |
| 1099 |
if (data != null) { |
| 1100 |
item.mIntent.setData(maybeAddUserId(data, contentUserHint)); |
| 1101 |
} |
| 1102 |
} |
| 1103 |
if (item.mUri != null) { |
| 1104 |
item.mUri = maybeAddUserId(item.mUri, contentUserHint); |
| 1105 |
} |
| 1106 |
} |
| 1107 |
} |
| 1108 |
|
| 1109 |
private boolean isStyledText() { |
| 1110 |
if (mItems.isEmpty()) { |
| 1111 |
return false; |
| 1112 |
} |
| 1113 |
final CharSequence text = mItems.get(0).getText(); |
| 1114 |
if (text instanceof Spanned) { |
| 1115 |
Spanned spanned = (Spanned) text; |
| 1116 |
if (TextUtils.hasStyleSpan(spanned)) { |
| 1117 |
return true; |
| 1118 |
} |
| 1119 |
} |
| 1120 |
return false; |
| 1121 |
} |
| 1122 |
|
| 1123 |
@Override |
| 1124 |
public String toString() { |
| 1125 |
StringBuilder b = new StringBuilder(128); |
| 1126 |
|
| 1127 |
b.append("ClipData { "); |
| 1128 |
toShortString(b, true); |
| 1129 |
b.append(" }"); |
| 1130 |
|
| 1131 |
return b.toString(); |
| 1132 |
} |
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
public void toShortString(StringBuilder b, boolean redactContent) { |
| 1140 |
boolean first; |
| 1141 |
if (mClipDescription != null) { |
| 1142 |
first = !mClipDescription.toShortString(b, redactContent); |
| 1143 |
} else { |
| 1144 |
first = true; |
| 1145 |
} |
| 1146 |
if (mIcon != null) { |
| 1147 |
if (!first) { |
| 1148 |
b.append(' '); |
| 1149 |
} |
| 1150 |
first = false; |
| 1151 |
b.append("I:"); |
| 1152 |
b.append(mIcon.getWidth()); |
| 1153 |
b.append('x'); |
| 1154 |
b.append(mIcon.getHeight()); |
| 1155 |
} |
| 1156 |
if (mItems.size() != 1) { |
| 1157 |
if (!first) { |
| 1158 |
b.append(' '); |
| 1159 |
} |
| 1160 |
first = false; |
| 1161 |
b.append(mItems.size()).append(" items:"); |
| 1162 |
} |
| 1163 |
for (int i = 0; i < mItems.size(); i++) { |
| 1164 |
if (!first) { |
| 1165 |
b.append(' '); |
| 1166 |
} |
| 1167 |
first = false; |
| 1168 |
b.append('{'); |
| 1169 |
mItems.get(i).toShortString(b, redactContent); |
| 1170 |
b.append('}'); |
| 1171 |
} |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
public void dumpDebug(ProtoOutputStream proto, long fieldId) { |
| 1176 |
final long token = proto.start(fieldId); |
| 1177 |
|
| 1178 |
if (mClipDescription != null) { |
| 1179 |
mClipDescription.dumpDebug(proto, ClipDataProto.DESCRIPTION); |
| 1180 |
} |
| 1181 |
if (mIcon != null) { |
| 1182 |
final long iToken = proto.start(ClipDataProto.ICON); |
| 1183 |
proto.write(ClipDataProto.Icon.WIDTH, mIcon.getWidth()); |
| 1184 |
proto.write(ClipDataProto.Icon.HEIGHT, mIcon.getHeight()); |
| 1185 |
proto.end(iToken); |
| 1186 |
} |
| 1187 |
for (int i = 0; i < mItems.size(); i++) { |
| 1188 |
mItems.get(i).dumpDebug(proto, ClipDataProto.ITEMS); |
| 1189 |
} |
| 1190 |
|
| 1191 |
proto.end(token); |
| 1192 |
} |
| 1193 |
|
| 1194 |
|
| 1195 |
public void collectUris(List<Uri> out) { |
| 1196 |
for (int i = 0; i < mItems.size(); ++i) { |
| 1197 |
ClipData.Item item = getItemAt(i); |
| 1198 |
|
| 1199 |
if (item.getUri() != null) { |
| 1200 |
out.add(item.getUri()); |
| 1201 |
} |
| 1202 |
|
| 1203 |
Intent intent = item.getIntent(); |
| 1204 |
if (intent != null) { |
| 1205 |
if (intent.getData() != null) { |
| 1206 |
out.add(intent.getData()); |
| 1207 |
} |
| 1208 |
if (intent.getClipData() != null) { |
| 1209 |
intent.getClipData().collectUris(out); |
| 1210 |
} |
| 1211 |
} |
| 1212 |
} |
| 1213 |
} |
| 1214 |
|
| 1215 |
@Override |
| 1216 |
public int describeContents() { |
| 1217 |
return 0; |
| 1218 |
} |
| 1219 |
|
| 1220 |
@Override |
| 1221 |
public void writeToParcel(Parcel dest, int flags) { |
| 1222 |
mClipDescription.writeToParcel(dest, flags); |
| 1223 |
if (mIcon != null) { |
| 1224 |
dest.writeInt(1); |
| 1225 |
mIcon.writeToParcel(dest, flags); |
| 1226 |
} else { |
| 1227 |
dest.writeInt(0); |
| 1228 |
} |
| 1229 |
final int N = mItems.size(); |
| 1230 |
dest.writeInt(N); |
| 1231 |
for (int i=0; i<N; i++) { |
| 1232 |
Item item = mItems.get(i); |
| 1233 |
TextUtils.writeToParcel(item.mText, dest, flags); |
| 1234 |
dest.writeString8(item.mHtmlText); |
| 1235 |
dest.writeTypedObject(item.mIntent, flags); |
| 1236 |
dest.writeTypedObject(item.mUri, flags); |
| 1237 |
dest.writeTypedObject(mParcelItemActivityInfos ? item.mActivityInfo : null, flags); |
| 1238 |
dest.writeTypedObject(item.mTextLinks, flags); |
| 1239 |
} |
| 1240 |
} |
| 1241 |
|
| 1242 |
ClipData(Parcel in) { |
| 1243 |
mClipDescription = new ClipDescription(in); |
| 1244 |
if (in.readInt() != 0) { |
| 1245 |
mIcon = Bitmap.CREATOR.createFromParcel(in); |
| 1246 |
} else { |
| 1247 |
mIcon = null; |
| 1248 |
} |
| 1249 |
mItems = new ArrayList<>(); |
| 1250 |
final int N = in.readInt(); |
| 1251 |
for (int i=0; i<N; i++) { |
| 1252 |
CharSequence text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
| 1253 |
String htmlText = in.readString8(); |
| 1254 |
Intent intent = in.readTypedObject(Intent.CREATOR); |
| 1255 |
Uri uri = in.readTypedObject(Uri.CREATOR); |
| 1256 |
ActivityInfo info = in.readTypedObject(ActivityInfo.CREATOR); |
| 1257 |
TextLinks textLinks = in.readTypedObject(TextLinks.CREATOR); |
| 1258 |
Item item = new Item(text, htmlText, intent, uri); |
| 1259 |
item.setActivityInfo(info); |
| 1260 |
item.setTextLinks(textLinks); |
| 1261 |
mItems.add(item); |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
public static final @android.annotation.NonNull Parcelable.Creator<ClipData> CREATOR = |
| 1266 |
new Parcelable.Creator<ClipData>() { |
| 1267 |
|
| 1268 |
@Override |
| 1269 |
public ClipData createFromParcel(Parcel source) { |
| 1270 |
return new ClipData(source); |
| 1271 |
} |
| 1272 |
|
| 1273 |
@Override |
| 1274 |
public ClipData[] newArray(int size) { |
| 1275 |
return new ClipData[size]; |
| 1276 |
} |
| 1277 |
}; |
| 1278 |
} |