| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.provider; |
| 18 |
|
| 19 |
import static com.android.internal.util.Preconditions.checkCollectionElementsNotNull; |
| 20 |
import static com.android.internal.util.Preconditions.checkCollectionNotEmpty; |
| 21 |
|
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.Nullable; |
| 24 |
import android.annotation.SystemApi; |
| 25 |
import android.compat.annotation.UnsupportedAppUsage; |
| 26 |
import android.content.ContentProvider; |
| 27 |
import android.content.ContentResolver; |
| 28 |
import android.content.Context; |
| 29 |
import android.content.Intent; |
| 30 |
import android.content.IntentSender; |
| 31 |
import android.content.MimeTypeFilter; |
| 32 |
import android.content.pm.ResolveInfo; |
| 33 |
import android.content.res.AssetFileDescriptor; |
| 34 |
import android.database.Cursor; |
| 35 |
import android.graphics.Bitmap; |
| 36 |
import android.graphics.ImageDecoder; |
| 37 |
import android.graphics.Point; |
| 38 |
import android.media.ExifInterface; |
| 39 |
import android.net.Uri; |
| 40 |
import android.os.Build; |
| 41 |
import android.os.Bundle; |
| 42 |
import android.os.CancellationSignal; |
| 43 |
import android.os.OperationCanceledException; |
| 44 |
import android.os.Parcel; |
| 45 |
import android.os.ParcelFileDescriptor; |
| 46 |
import android.os.ParcelFileDescriptor.OnCloseListener; |
| 47 |
import android.os.Parcelable; |
| 48 |
import android.os.ParcelableException; |
| 49 |
import android.os.RemoteException; |
| 50 |
import android.os.UserHandle; |
| 51 |
import android.util.Log; |
| 52 |
import android.util.Size; |
| 53 |
|
| 54 |
import com.android.internal.util.Preconditions; |
| 55 |
|
| 56 |
import dalvik.system.VMRuntime; |
| 57 |
|
| 58 |
import java.io.File; |
| 59 |
import java.io.FileNotFoundException; |
| 60 |
import java.io.IOException; |
| 61 |
import java.util.ArrayList; |
| 62 |
import java.util.List; |
| 63 |
import java.util.Objects; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public final class DocumentsContract { |
| 79 |
private static final String TAG = "DocumentsContract"; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
private DocumentsContract() { |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public static final String PROVIDER_INTERFACE = "android.content.action.DOCUMENTS_PROVIDER"; |
| 98 |
|
| 99 |
|
| 100 |
@Deprecated |
| 101 |
public static final String EXTRA_PACKAGE_NAME = Intent.EXTRA_PACKAGE_NAME; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
@SystemApi |
| 111 |
public static final String EXTRA_SHOW_ADVANCED = "android.provider.extra.SHOW_ADVANCED"; |
| 112 |
|
| 113 |
|
| 114 |
public static final String EXTRA_TARGET_URI = "android.content.extra.TARGET_URI"; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public static final String QUERY_ARG_DISPLAY_NAME = "android:query-arg-display-name"; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public static final String QUERY_ARG_MIME_TYPES = "android:query-arg-mime-types"; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public static final String QUERY_ARG_FILE_SIZE_OVER = "android:query-arg-file-size-over"; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public static final String QUERY_ARG_LAST_MODIFIED_AFTER = |
| 158 |
"android:query-arg-last-modified-after"; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public static final String QUERY_ARG_EXCLUDE_MEDIA = "android:query-arg-exclude-media"; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public static final String EXTRA_INITIAL_URI = "android.provider.extra.INITIAL_URI"; |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public static final String EXTRA_EXCLUDE_SELF = "android.provider.extra.EXCLUDE_SELF"; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public static final String EXTRA_ORIENTATION = "android.provider.extra.ORIENTATION"; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public static final String EXTRA_PROMPT = "android.provider.extra.PROMPT"; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public static final String |
| 222 |
ACTION_DOCUMENT_SETTINGS = "android.provider.action.DOCUMENT_SETTINGS"; |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
@SystemApi |
| 229 |
public static final String ACTION_MANAGE_DOCUMENT = "android.provider.action.MANAGE_DOCUMENT"; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
@SystemApi |
| 236 |
public static final String |
| 237 |
ACTION_DOCUMENT_ROOT_SETTINGS = "android.provider.action.DOCUMENT_ROOT_SETTINGS"; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
@SystemApi |
| 244 |
public static final String EXTERNAL_STORAGE_PROVIDER_AUTHORITY = |
| 245 |
"com.android.externalstorage.documents"; |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
@SystemApi |
| 252 |
public static final String DOWNLOADS_PROVIDER_AUTHORITY = Downloads.Impl.AUTHORITY; |
| 253 |
|
| 254 |
|
| 255 |
public static final String EXTERNAL_STORAGE_PRIMARY_EMULATED_ROOT_ID = "primary"; |
| 256 |
|
| 257 |
|
| 258 |
public static final String PACKAGE_DOCUMENTS_UI = "com.android.documentsui"; |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public static final String METADATA_TYPES = "android:documentMetadataTypes"; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public static final String METADATA_EXIF = "android:documentExif"; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public static final String METADATA_TREE_COUNT = "android:metadataTreeCount"; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public static final String METADATA_TREE_SIZE = "android:metadataTreeSize"; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public final static class Document { |
| 304 |
private Document() { |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public static final String COLUMN_DOCUMENT_ID = "document_id"; |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public static final String COLUMN_MIME_TYPE = "mime_type"; |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
public static final String COLUMN_DISPLAY_NAME = OpenableColumns.DISPLAY_NAME; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
public static final String COLUMN_SUMMARY = "summary"; |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public static final String COLUMN_LAST_MODIFIED = "last_modified"; |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
public static final String COLUMN_ICON = "icon"; |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
public static final String COLUMN_FLAGS = "flags"; |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
public static final String COLUMN_SIZE = OpenableColumns.SIZE; |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
public static final String MIME_TYPE_DIR = "vnd.android.document/directory"; |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
public static final int FLAG_SUPPORTS_THUMBNAIL = 1; |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
public static final int FLAG_SUPPORTS_WRITE = 1 << 1; |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
public static final int FLAG_SUPPORTS_DELETE = 1 << 2; |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
public static final int FLAG_DIR_SUPPORTS_CREATE = 1 << 3; |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
public static final int FLAG_DIR_PREFERS_GRID = 1 << 4; |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
public static final int FLAG_DIR_PREFERS_LAST_MODIFIED = 1 << 5; |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
public static final int FLAG_SUPPORTS_RENAME = 1 << 6; |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
public static final int FLAG_SUPPORTS_COPY = 1 << 7; |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
public static final int FLAG_SUPPORTS_MOVE = 1 << 8; |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
public static final int FLAG_VIRTUAL_DOCUMENT = 1 << 9; |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
public static final int FLAG_SUPPORTS_REMOVE = 1 << 10; |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
public static final int FLAG_SUPPORTS_SETTINGS = 1 << 11; |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
public static final int FLAG_WEB_LINKABLE = 1 << 12; |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
public static final int FLAG_PARTIAL = 1 << 13; |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
public static final int FLAG_SUPPORTS_METADATA = 1 << 14; |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
public static final int FLAG_DIR_BLOCKS_OPEN_DOCUMENT_TREE = 1 << 15; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
public final static class Root { |
| 596 |
private Root() { |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
public static final String COLUMN_ROOT_ID = "root_id"; |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
public static final String COLUMN_FLAGS = "flags"; |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
public static final String COLUMN_ICON = "icon"; |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
public static final String COLUMN_TITLE = "title"; |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
public static final String COLUMN_SUMMARY = "summary"; |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
public static final String COLUMN_DOCUMENT_ID = "document_id"; |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
public static final String COLUMN_AVAILABLE_BYTES = "available_bytes"; |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
public static final String COLUMN_CAPACITY_BYTES = "capacity_bytes"; |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
public static final String COLUMN_MIME_TYPES = "mime_types"; |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
public static final String COLUMN_QUERY_ARGS = "query_args"; |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
public static final String MIME_TYPE_ITEM = "vnd.android.document/root"; |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
public static final int FLAG_SUPPORTS_CREATE = 1; |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
public static final int FLAG_LOCAL_ONLY = 1 << 1; |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
public static final int FLAG_SUPPORTS_RECENTS = 1 << 2; |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
public static final int FLAG_SUPPORTS_SEARCH = 1 << 3; |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
public static final int FLAG_SUPPORTS_IS_CHILD = 1 << 4; |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
public static final int FLAG_SUPPORTS_EJECT = 1 << 5; |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
public static final int FLAG_EMPTY = 1 << 6; |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
@SystemApi |
| 790 |
public static final int FLAG_ADVANCED = 1 << 16; |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
@SystemApi |
| 800 |
public static final int FLAG_HAS_SETTINGS = 1 << 17; |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
@SystemApi |
| 809 |
public static final int FLAG_REMOVABLE_SD = 1 << 18; |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
@SystemApi |
| 818 |
public static final int FLAG_REMOVABLE_USB = 1 << 19; |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
public static final String EXTRA_LOADING = "loading"; |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
public static final String EXTRA_INFO = "info"; |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
public static final String EXTRA_ERROR = "error"; |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
public static final String EXTRA_RESULT = "result"; |
| 854 |
|
| 855 |
|
| 856 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 857 |
public static final String METHOD_CREATE_DOCUMENT = "android:createDocument"; |
| 858 |
|
| 859 |
public static final String METHOD_RENAME_DOCUMENT = "android:renameDocument"; |
| 860 |
|
| 861 |
public static final String METHOD_DELETE_DOCUMENT = "android:deleteDocument"; |
| 862 |
|
| 863 |
public static final String METHOD_COPY_DOCUMENT = "android:copyDocument"; |
| 864 |
|
| 865 |
public static final String METHOD_MOVE_DOCUMENT = "android:moveDocument"; |
| 866 |
|
| 867 |
public static final String METHOD_IS_CHILD_DOCUMENT = "android:isChildDocument"; |
| 868 |
|
| 869 |
public static final String METHOD_REMOVE_DOCUMENT = "android:removeDocument"; |
| 870 |
|
| 871 |
public static final String METHOD_EJECT_ROOT = "android:ejectRoot"; |
| 872 |
|
| 873 |
public static final String METHOD_FIND_DOCUMENT_PATH = "android:findDocumentPath"; |
| 874 |
|
| 875 |
public static final String METHOD_CREATE_WEB_LINK_INTENT = "android:createWebLinkIntent"; |
| 876 |
|
| 877 |
public static final String METHOD_GET_DOCUMENT_METADATA = "android:getDocumentMetadata"; |
| 878 |
|
| 879 |
|
| 880 |
public static final String EXTRA_PARENT_URI = "parentUri"; |
| 881 |
|
| 882 |
public static final String EXTRA_URI = "uri"; |
| 883 |
|
| 884 |
public static final String EXTRA_URI_PERMISSIONS = "uriPermissions"; |
| 885 |
|
| 886 |
|
| 887 |
public static final String EXTRA_OPTIONS = "options"; |
| 888 |
|
| 889 |
private static final String PATH_ROOT = "root"; |
| 890 |
private static final String PATH_RECENT = "recent"; |
| 891 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 892 |
private static final String PATH_DOCUMENT = "document"; |
| 893 |
private static final String PATH_CHILDREN = "children"; |
| 894 |
private static final String PATH_SEARCH = "search"; |
| 895 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 896 |
private static final String PATH_TREE = "tree"; |
| 897 |
|
| 898 |
private static final String PARAM_QUERY = "query"; |
| 899 |
private static final String PARAM_MANAGE = "manage"; |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
public static Uri buildRootsUri(String authority) { |
| 909 |
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 910 |
.authority(authority).appendPath(PATH_ROOT).build(); |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
public static Uri buildRootUri(String authority, String rootId) { |
| 920 |
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 921 |
.authority(authority).appendPath(PATH_ROOT).appendPath(rootId).build(); |
| 922 |
} |
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
public static Uri buildRecentDocumentsUri(String authority, String rootId) { |
| 933 |
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 934 |
.authority(authority).appendPath(PATH_ROOT).appendPath(rootId) |
| 935 |
.appendPath(PATH_RECENT).build(); |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
public static Uri buildTreeDocumentUri(String authority, String documentId) { |
| 945 |
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority) |
| 946 |
.appendPath(PATH_TREE).appendPath(documentId).build(); |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
public static Uri buildDocumentUri(String authority, String documentId) { |
| 958 |
return getBaseDocumentUriBuilder(authority).appendPath(documentId).build(); |
| 959 |
} |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
@SystemApi |
| 968 |
@NonNull |
| 969 |
public static Uri buildDocumentUriAsUser( |
| 970 |
@NonNull String authority, @NonNull String documentId, @NonNull UserHandle user) { |
| 971 |
return ContentProvider.maybeAddUserId( |
| 972 |
buildDocumentUri(authority, documentId), user.getIdentifier()); |
| 973 |
} |
| 974 |
|
| 975 |
|
| 976 |
public static Uri buildBaseDocumentUri(String authority) { |
| 977 |
return getBaseDocumentUriBuilder(authority).build(); |
| 978 |
} |
| 979 |
|
| 980 |
private static Uri.Builder getBaseDocumentUriBuilder(String authority) { |
| 981 |
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 982 |
.authority(authority).appendPath(PATH_DOCUMENT); |
| 983 |
} |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
public static Uri buildDocumentUriUsingTree(Uri treeUri, String documentId) { |
| 1009 |
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 1010 |
.authority(treeUri.getAuthority()).appendPath(PATH_TREE) |
| 1011 |
.appendPath(getTreeDocumentId(treeUri)).appendPath(PATH_DOCUMENT) |
| 1012 |
.appendPath(documentId).build(); |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
public static Uri buildDocumentUriMaybeUsingTree(Uri baseUri, String documentId) { |
| 1017 |
if (isTreeUri(baseUri)) { |
| 1018 |
return buildDocumentUriUsingTree(baseUri, documentId); |
| 1019 |
} else { |
| 1020 |
return buildDocumentUri(baseUri.getAuthority(), documentId); |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
public static Uri buildChildDocumentsUri(String authority, String parentDocumentId) { |
| 1036 |
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority) |
| 1037 |
.appendPath(PATH_DOCUMENT).appendPath(parentDocumentId).appendPath(PATH_CHILDREN) |
| 1038 |
.build(); |
| 1039 |
} |
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
public static Uri buildChildDocumentsUriUsingTree(Uri treeUri, String parentDocumentId) { |
| 1066 |
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 1067 |
.authority(treeUri.getAuthority()).appendPath(PATH_TREE) |
| 1068 |
.appendPath(getTreeDocumentId(treeUri)).appendPath(PATH_DOCUMENT) |
| 1069 |
.appendPath(parentDocumentId).appendPath(PATH_CHILDREN).build(); |
| 1070 |
} |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
public static Uri buildSearchDocumentsUri( |
| 1082 |
String authority, String rootId, String query) { |
| 1083 |
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority) |
| 1084 |
.appendPath(PATH_ROOT).appendPath(rootId).appendPath(PATH_SEARCH) |
| 1085 |
.appendQueryParameter(PARAM_QUERY, query).build(); |
| 1086 |
} |
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
public static boolean matchSearchQueryArguments(Bundle queryArgs, String displayName, |
| 1099 |
String mimeType, long lastModified, long size) { |
| 1100 |
if (queryArgs == null) { |
| 1101 |
return true; |
| 1102 |
} |
| 1103 |
|
| 1104 |
final String argDisplayName = queryArgs.getString(QUERY_ARG_DISPLAY_NAME, ""); |
| 1105 |
if (!argDisplayName.isEmpty()) { |
| 1106 |
|
| 1107 |
if (!displayName.toLowerCase().contains(argDisplayName.toLowerCase())) { |
| 1108 |
return false; |
| 1109 |
} |
| 1110 |
} |
| 1111 |
|
| 1112 |
final long argFileSize = queryArgs.getLong(QUERY_ARG_FILE_SIZE_OVER, -1 ); |
| 1113 |
if (argFileSize != -1 && size < argFileSize) { |
| 1114 |
return false; |
| 1115 |
} |
| 1116 |
|
| 1117 |
final long argLastModified = queryArgs.getLong(QUERY_ARG_LAST_MODIFIED_AFTER, |
| 1118 |
-1 ); |
| 1119 |
if (argLastModified != -1 && lastModified < argLastModified) { |
| 1120 |
return false; |
| 1121 |
} |
| 1122 |
|
| 1123 |
final String[] argMimeTypes = queryArgs.getStringArray(QUERY_ARG_MIME_TYPES); |
| 1124 |
if (argMimeTypes != null && argMimeTypes.length > 0) { |
| 1125 |
mimeType = Intent.normalizeMimeType(mimeType); |
| 1126 |
for (String type : argMimeTypes) { |
| 1127 |
if (MimeTypeFilter.matches(mimeType, Intent.normalizeMimeType(type))) { |
| 1128 |
return true; |
| 1129 |
} |
| 1130 |
} |
| 1131 |
return false; |
| 1132 |
} |
| 1133 |
return true; |
| 1134 |
} |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
public static String[] getHandledQueryArguments(Bundle queryArgs) { |
| 1149 |
if (queryArgs == null) { |
| 1150 |
return new String[0]; |
| 1151 |
} |
| 1152 |
|
| 1153 |
final ArrayList<String> args = new ArrayList<>(); |
| 1154 |
|
| 1155 |
if (queryArgs.keySet().contains(QUERY_ARG_EXCLUDE_MEDIA)) { |
| 1156 |
args.add(QUERY_ARG_EXCLUDE_MEDIA); |
| 1157 |
} |
| 1158 |
|
| 1159 |
if (queryArgs.keySet().contains(QUERY_ARG_DISPLAY_NAME)) { |
| 1160 |
args.add(QUERY_ARG_DISPLAY_NAME); |
| 1161 |
} |
| 1162 |
|
| 1163 |
if (queryArgs.keySet().contains(QUERY_ARG_FILE_SIZE_OVER)) { |
| 1164 |
args.add(QUERY_ARG_FILE_SIZE_OVER); |
| 1165 |
} |
| 1166 |
|
| 1167 |
if (queryArgs.keySet().contains(QUERY_ARG_LAST_MODIFIED_AFTER)) { |
| 1168 |
args.add(QUERY_ARG_LAST_MODIFIED_AFTER); |
| 1169 |
} |
| 1170 |
|
| 1171 |
if (queryArgs.keySet().contains(QUERY_ARG_MIME_TYPES)) { |
| 1172 |
args.add(QUERY_ARG_MIME_TYPES); |
| 1173 |
} |
| 1174 |
return args.toArray(new String[0]); |
| 1175 |
} |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
public static boolean isDocumentUri(Context context, @Nullable Uri uri) { |
| 1185 |
if (isContentUri(uri) && isDocumentsProvider(context, uri.getAuthority())) { |
| 1186 |
final List<String> paths = uri.getPathSegments(); |
| 1187 |
if (paths.size() == 2) { |
| 1188 |
return PATH_DOCUMENT.equals(paths.get(0)); |
| 1189 |
} else if (paths.size() == 4) { |
| 1190 |
return PATH_TREE.equals(paths.get(0)) && PATH_DOCUMENT.equals(paths.get(2)); |
| 1191 |
} |
| 1192 |
} |
| 1193 |
return false; |
| 1194 |
} |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
public static boolean isRootsUri(@NonNull Context context, @Nullable Uri uri) { |
| 1203 |
Preconditions.checkNotNull(context, "context can not be null"); |
| 1204 |
return isRootUri(context, uri, 1 ); |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
public static boolean isRootUri(@NonNull Context context, @Nullable Uri uri) { |
| 1213 |
Preconditions.checkNotNull(context, "context can not be null"); |
| 1214 |
return isRootUri(context, uri, 2 ); |
| 1215 |
} |
| 1216 |
|
| 1217 |
|
| 1218 |
public static boolean isContentUri(@Nullable Uri uri) { |
| 1219 |
return uri != null && ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()); |
| 1220 |
} |
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
public static boolean isTreeUri(Uri uri) { |
| 1229 |
final List<String> paths = uri.getPathSegments(); |
| 1230 |
return (paths.size() >= 2 && PATH_TREE.equals(paths.get(0))); |
| 1231 |
} |
| 1232 |
|
| 1233 |
private static boolean isRootUri(Context context, @Nullable Uri uri, int pathSize) { |
| 1234 |
if (isContentUri(uri) && isDocumentsProvider(context, uri.getAuthority())) { |
| 1235 |
final List<String> paths = uri.getPathSegments(); |
| 1236 |
return (paths.size() == pathSize && PATH_ROOT.equals(paths.get(0))); |
| 1237 |
} |
| 1238 |
return false; |
| 1239 |
} |
| 1240 |
|
| 1241 |
private static boolean isDocumentsProvider(Context context, String authority) { |
| 1242 |
final Intent intent = new Intent(PROVIDER_INTERFACE); |
| 1243 |
final List<ResolveInfo> infos = context.getPackageManager() |
| 1244 |
.queryIntentContentProviders(intent, 0); |
| 1245 |
for (ResolveInfo info : infos) { |
| 1246 |
if (authority.equals(info.providerInfo.authority)) { |
| 1247 |
return true; |
| 1248 |
} |
| 1249 |
} |
| 1250 |
return false; |
| 1251 |
} |
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
public static String getRootId(Uri rootUri) { |
| 1257 |
final List<String> paths = rootUri.getPathSegments(); |
| 1258 |
if (paths.size() >= 2 && PATH_ROOT.equals(paths.get(0))) { |
| 1259 |
return paths.get(1); |
| 1260 |
} |
| 1261 |
throw new IllegalArgumentException("Invalid URI: " + rootUri); |
| 1262 |
} |
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
public static String getDocumentId(Uri documentUri) { |
| 1270 |
final List<String> paths = documentUri.getPathSegments(); |
| 1271 |
if (paths.size() >= 2 && PATH_DOCUMENT.equals(paths.get(0))) { |
| 1272 |
return paths.get(1); |
| 1273 |
} |
| 1274 |
if (paths.size() >= 4 && PATH_TREE.equals(paths.get(0)) |
| 1275 |
&& PATH_DOCUMENT.equals(paths.get(2))) { |
| 1276 |
return paths.get(3); |
| 1277 |
} |
| 1278 |
throw new IllegalArgumentException("Invalid URI: " + documentUri); |
| 1279 |
} |
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
public static String getTreeDocumentId(Uri documentUri) { |
| 1285 |
final List<String> paths = documentUri.getPathSegments(); |
| 1286 |
if (paths.size() >= 2 && PATH_TREE.equals(paths.get(0))) { |
| 1287 |
return paths.get(1); |
| 1288 |
} |
| 1289 |
throw new IllegalArgumentException("Invalid URI: " + documentUri); |
| 1290 |
} |
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
public static String getSearchDocumentsQuery(Uri searchDocumentsUri) { |
| 1297 |
return searchDocumentsUri.getQueryParameter(PARAM_QUERY); |
| 1298 |
} |
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
public static String getSearchDocumentsQuery(@NonNull Bundle bundle) { |
| 1306 |
Preconditions.checkNotNull(bundle, "bundle can not be null"); |
| 1307 |
return bundle.getString(QUERY_ARG_DISPLAY_NAME, "" ); |
| 1308 |
} |
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
@SystemApi |
| 1317 |
public static @NonNull Uri setManageMode(@NonNull Uri uri) { |
| 1318 |
Preconditions.checkNotNull(uri, "uri can not be null"); |
| 1319 |
return uri.buildUpon().appendQueryParameter(PARAM_MANAGE, "true").build(); |
| 1320 |
} |
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
@SystemApi |
| 1328 |
public static boolean isManageMode(@NonNull Uri uri) { |
| 1329 |
Preconditions.checkNotNull(uri, "uri can not be null"); |
| 1330 |
return uri.getBooleanQueryParameter(PARAM_MANAGE, false); |
| 1331 |
} |
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
public static @Nullable Bitmap getDocumentThumbnail(@NonNull ContentResolver content, |
| 1349 |
@NonNull Uri documentUri, @NonNull Point size, @Nullable CancellationSignal signal) |
| 1350 |
throws FileNotFoundException { |
| 1351 |
try { |
| 1352 |
return ContentResolver.loadThumbnail(content, documentUri, new Size(size.x, size.y), |
| 1353 |
signal, ImageDecoder.ALLOCATOR_SOFTWARE); |
| 1354 |
} catch (Exception e) { |
| 1355 |
if (!(e instanceof OperationCanceledException)) { |
| 1356 |
Log.w(TAG, "Failed to load thumbnail for " + documentUri + ": " + e); |
| 1357 |
} |
| 1358 |
rethrowIfNecessary(e); |
| 1359 |
return null; |
| 1360 |
} |
| 1361 |
} |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
public static @Nullable Uri createDocument(@NonNull ContentResolver content, |
| 1372 |
@NonNull Uri parentDocumentUri, @NonNull String mimeType, @NonNull String displayName) |
| 1373 |
throws FileNotFoundException { |
| 1374 |
try { |
| 1375 |
final Bundle in = new Bundle(); |
| 1376 |
in.putParcelable(DocumentsContract.EXTRA_URI, parentDocumentUri); |
| 1377 |
in.putString(Document.COLUMN_MIME_TYPE, mimeType); |
| 1378 |
in.putString(Document.COLUMN_DISPLAY_NAME, displayName); |
| 1379 |
|
| 1380 |
final Bundle out = content.call(parentDocumentUri.getAuthority(), |
| 1381 |
METHOD_CREATE_DOCUMENT, null, in); |
| 1382 |
return out.getParcelable(DocumentsContract.EXTRA_URI); |
| 1383 |
} catch (Exception e) { |
| 1384 |
Log.w(TAG, "Failed to create document", e); |
| 1385 |
rethrowIfNecessary(e); |
| 1386 |
return null; |
| 1387 |
} |
| 1388 |
} |
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
public static boolean isChildDocument(@NonNull ContentResolver content, |
| 1400 |
@NonNull Uri parentDocumentUri, @NonNull Uri childDocumentUri) |
| 1401 |
throws FileNotFoundException { |
| 1402 |
Preconditions.checkNotNull(content, "content can not be null"); |
| 1403 |
Preconditions.checkNotNull(parentDocumentUri, "parentDocumentUri can not be null"); |
| 1404 |
Preconditions.checkNotNull(childDocumentUri, "childDocumentUri can not be null"); |
| 1405 |
try { |
| 1406 |
final Bundle in = new Bundle(); |
| 1407 |
in.putParcelable(DocumentsContract.EXTRA_URI, parentDocumentUri); |
| 1408 |
in.putParcelable(DocumentsContract.EXTRA_TARGET_URI, childDocumentUri); |
| 1409 |
|
| 1410 |
final Bundle out = content.call(parentDocumentUri.getAuthority(), |
| 1411 |
METHOD_IS_CHILD_DOCUMENT, null, in); |
| 1412 |
if (out == null) { |
| 1413 |
throw new RemoteException("Failed to get a response from isChildDocument query."); |
| 1414 |
} |
| 1415 |
if (!out.containsKey(DocumentsContract.EXTRA_RESULT)) { |
| 1416 |
throw new RemoteException("Response did not include result field.."); |
| 1417 |
} |
| 1418 |
return out.getBoolean(DocumentsContract.EXTRA_RESULT); |
| 1419 |
} catch (Exception e) { |
| 1420 |
Log.w(TAG, "Failed to create document", e); |
| 1421 |
rethrowIfNecessary(e); |
| 1422 |
return false; |
| 1423 |
} |
| 1424 |
} |
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
public static @Nullable Uri renameDocument(@NonNull ContentResolver content, |
| 1440 |
@NonNull Uri documentUri, @NonNull String displayName) throws FileNotFoundException { |
| 1441 |
try { |
| 1442 |
final Bundle in = new Bundle(); |
| 1443 |
in.putParcelable(DocumentsContract.EXTRA_URI, documentUri); |
| 1444 |
in.putString(Document.COLUMN_DISPLAY_NAME, displayName); |
| 1445 |
|
| 1446 |
final Bundle out = content.call(documentUri.getAuthority(), |
| 1447 |
METHOD_RENAME_DOCUMENT, null, in); |
| 1448 |
final Uri outUri = out.getParcelable(DocumentsContract.EXTRA_URI); |
| 1449 |
return (outUri != null) ? outUri : documentUri; |
| 1450 |
} catch (Exception e) { |
| 1451 |
Log.w(TAG, "Failed to rename document", e); |
| 1452 |
rethrowIfNecessary(e); |
| 1453 |
return null; |
| 1454 |
} |
| 1455 |
} |
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
public static boolean deleteDocument(@NonNull ContentResolver content, @NonNull Uri documentUri) |
| 1464 |
throws FileNotFoundException { |
| 1465 |
try { |
| 1466 |
final Bundle in = new Bundle(); |
| 1467 |
in.putParcelable(DocumentsContract.EXTRA_URI, documentUri); |
| 1468 |
|
| 1469 |
content.call(documentUri.getAuthority(), |
| 1470 |
METHOD_DELETE_DOCUMENT, null, in); |
| 1471 |
return true; |
| 1472 |
} catch (Exception e) { |
| 1473 |
Log.w(TAG, "Failed to delete document", e); |
| 1474 |
rethrowIfNecessary(e); |
| 1475 |
return false; |
| 1476 |
} |
| 1477 |
} |
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
public static @Nullable Uri copyDocument(@NonNull ContentResolver content, |
| 1488 |
@NonNull Uri sourceDocumentUri, @NonNull Uri targetParentDocumentUri) |
| 1489 |
throws FileNotFoundException { |
| 1490 |
try { |
| 1491 |
final Bundle in = new Bundle(); |
| 1492 |
in.putParcelable(DocumentsContract.EXTRA_URI, sourceDocumentUri); |
| 1493 |
in.putParcelable(DocumentsContract.EXTRA_TARGET_URI, targetParentDocumentUri); |
| 1494 |
|
| 1495 |
final Bundle out = content.call(sourceDocumentUri.getAuthority(), |
| 1496 |
METHOD_COPY_DOCUMENT, null, in); |
| 1497 |
return out.getParcelable(DocumentsContract.EXTRA_URI); |
| 1498 |
} catch (Exception e) { |
| 1499 |
Log.w(TAG, "Failed to copy document", e); |
| 1500 |
rethrowIfNecessary(e); |
| 1501 |
return null; |
| 1502 |
} |
| 1503 |
} |
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
public static @Nullable Uri moveDocument(@NonNull ContentResolver content, |
| 1515 |
@NonNull Uri sourceDocumentUri, @NonNull Uri sourceParentDocumentUri, |
| 1516 |
@NonNull Uri targetParentDocumentUri) throws FileNotFoundException { |
| 1517 |
try { |
| 1518 |
final Bundle in = new Bundle(); |
| 1519 |
in.putParcelable(DocumentsContract.EXTRA_URI, sourceDocumentUri); |
| 1520 |
in.putParcelable(DocumentsContract.EXTRA_PARENT_URI, sourceParentDocumentUri); |
| 1521 |
in.putParcelable(DocumentsContract.EXTRA_TARGET_URI, targetParentDocumentUri); |
| 1522 |
|
| 1523 |
final Bundle out = content.call(sourceDocumentUri.getAuthority(), |
| 1524 |
METHOD_MOVE_DOCUMENT, null, in); |
| 1525 |
return out.getParcelable(DocumentsContract.EXTRA_URI); |
| 1526 |
} catch (Exception e) { |
| 1527 |
Log.w(TAG, "Failed to move document", e); |
| 1528 |
rethrowIfNecessary(e); |
| 1529 |
return null; |
| 1530 |
} |
| 1531 |
} |
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
public static boolean removeDocument(@NonNull ContentResolver content, @NonNull Uri documentUri, |
| 1544 |
@NonNull Uri parentDocumentUri) throws FileNotFoundException { |
| 1545 |
try { |
| 1546 |
final Bundle in = new Bundle(); |
| 1547 |
in.putParcelable(DocumentsContract.EXTRA_URI, documentUri); |
| 1548 |
in.putParcelable(DocumentsContract.EXTRA_PARENT_URI, parentDocumentUri); |
| 1549 |
|
| 1550 |
content.call(documentUri.getAuthority(), |
| 1551 |
METHOD_REMOVE_DOCUMENT, null, in); |
| 1552 |
return true; |
| 1553 |
} catch (Exception e) { |
| 1554 |
Log.w(TAG, "Failed to remove document", e); |
| 1555 |
rethrowIfNecessary(e); |
| 1556 |
return false; |
| 1557 |
} |
| 1558 |
} |
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
public static void ejectRoot(@NonNull ContentResolver content, @NonNull Uri rootUri) { |
| 1566 |
try { |
| 1567 |
final Bundle in = new Bundle(); |
| 1568 |
in.putParcelable(DocumentsContract.EXTRA_URI, rootUri); |
| 1569 |
|
| 1570 |
content.call(rootUri.getAuthority(), |
| 1571 |
METHOD_EJECT_ROOT, null, in); |
| 1572 |
} catch (Exception e) { |
| 1573 |
Log.w(TAG, "Failed to eject", e); |
| 1574 |
} |
| 1575 |
} |
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
public static @Nullable Bundle getDocumentMetadata(@NonNull ContentResolver content, |
| 1608 |
@NonNull Uri documentUri) throws FileNotFoundException { |
| 1609 |
Preconditions.checkNotNull(content, "content can not be null"); |
| 1610 |
Preconditions.checkNotNull(documentUri, "documentUri can not be null"); |
| 1611 |
try { |
| 1612 |
final Bundle in = new Bundle(); |
| 1613 |
in.putParcelable(EXTRA_URI, documentUri); |
| 1614 |
|
| 1615 |
return content.call(documentUri.getAuthority(), |
| 1616 |
METHOD_GET_DOCUMENT_METADATA, null, in); |
| 1617 |
} catch (Exception e) { |
| 1618 |
Log.w(TAG, "Failed to get document metadata"); |
| 1619 |
rethrowIfNecessary(e); |
| 1620 |
return null; |
| 1621 |
} |
| 1622 |
} |
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
public static @Nullable Path findDocumentPath(@NonNull ContentResolver content, |
| 1638 |
@NonNull Uri treeUri) throws FileNotFoundException { |
| 1639 |
try { |
| 1640 |
final Bundle in = new Bundle(); |
| 1641 |
in.putParcelable(DocumentsContract.EXTRA_URI, treeUri); |
| 1642 |
|
| 1643 |
final Bundle out = content.call(treeUri.getAuthority(), |
| 1644 |
METHOD_FIND_DOCUMENT_PATH, null, in); |
| 1645 |
return out.getParcelable(DocumentsContract.EXTRA_RESULT); |
| 1646 |
} catch (Exception e) { |
| 1647 |
Log.w(TAG, "Failed to find path", e); |
| 1648 |
rethrowIfNecessary(e); |
| 1649 |
return null; |
| 1650 |
} |
| 1651 |
} |
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
public static @Nullable IntentSender createWebLinkIntent(@NonNull ContentResolver content, |
| 1705 |
@NonNull Uri uri, @Nullable Bundle options) throws FileNotFoundException { |
| 1706 |
try { |
| 1707 |
final Bundle in = new Bundle(); |
| 1708 |
in.putParcelable(DocumentsContract.EXTRA_URI, uri); |
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
if (options != null) { |
| 1713 |
in.putBundle(EXTRA_OPTIONS, options); |
| 1714 |
} |
| 1715 |
|
| 1716 |
final Bundle out = content.call(uri.getAuthority(), |
| 1717 |
METHOD_CREATE_WEB_LINK_INTENT, null, in); |
| 1718 |
return out.getParcelable(DocumentsContract.EXTRA_RESULT); |
| 1719 |
} catch (Exception e) { |
| 1720 |
Log.w(TAG, "Failed to create a web link intent", e); |
| 1721 |
rethrowIfNecessary(e); |
| 1722 |
return null; |
| 1723 |
} |
| 1724 |
} |
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
public static AssetFileDescriptor openImageThumbnail(File file) throws FileNotFoundException { |
| 1734 |
final ParcelFileDescriptor pfd = ParcelFileDescriptor.open( |
| 1735 |
file, ParcelFileDescriptor.MODE_READ_ONLY); |
| 1736 |
try { |
| 1737 |
final ExifInterface exif = new ExifInterface(file.getAbsolutePath()); |
| 1738 |
|
| 1739 |
final long[] thumb = exif.getThumbnailRange(); |
| 1740 |
if (thumb != null) { |
| 1741 |
|
| 1742 |
Bundle extras = null; |
| 1743 |
switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1)) { |
| 1744 |
case ExifInterface.ORIENTATION_ROTATE_90: |
| 1745 |
extras = new Bundle(1); |
| 1746 |
extras.putInt(EXTRA_ORIENTATION, 90); |
| 1747 |
break; |
| 1748 |
case ExifInterface.ORIENTATION_ROTATE_180: |
| 1749 |
extras = new Bundle(1); |
| 1750 |
extras.putInt(EXTRA_ORIENTATION, 180); |
| 1751 |
break; |
| 1752 |
case ExifInterface.ORIENTATION_ROTATE_270: |
| 1753 |
extras = new Bundle(1); |
| 1754 |
extras.putInt(EXTRA_ORIENTATION, 270); |
| 1755 |
break; |
| 1756 |
} |
| 1757 |
|
| 1758 |
return new AssetFileDescriptor(pfd, thumb[0], thumb[1], extras); |
| 1759 |
} |
| 1760 |
} catch (IOException e) { |
| 1761 |
} |
| 1762 |
|
| 1763 |
|
| 1764 |
return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH, null); |
| 1765 |
} |
| 1766 |
|
| 1767 |
private static void rethrowIfNecessary(Exception e) throws FileNotFoundException { |
| 1768 |
|
| 1769 |
if (VMRuntime.getRuntime().getTargetSdkVersion() >= Build.VERSION_CODES.O) { |
| 1770 |
if (e instanceof ParcelableException) { |
| 1771 |
((ParcelableException) e).maybeRethrow(FileNotFoundException.class); |
| 1772 |
} else if (e instanceof RemoteException) { |
| 1773 |
((RemoteException) e).rethrowAsRuntimeException(); |
| 1774 |
} else if (e instanceof RuntimeException) { |
| 1775 |
throw (RuntimeException) e; |
| 1776 |
} |
| 1777 |
} |
| 1778 |
} |
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
public static final class Path implements Parcelable { |
| 1785 |
|
| 1786 |
private final @Nullable String mRootId; |
| 1787 |
private final List<String> mPath; |
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
public Path(@Nullable String rootId, List<String> path) { |
| 1797 |
checkCollectionNotEmpty(path, "path"); |
| 1798 |
checkCollectionElementsNotNull(path, "path"); |
| 1799 |
|
| 1800 |
mRootId = rootId; |
| 1801 |
mPath = path; |
| 1802 |
} |
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
public @Nullable String getRootId() { |
| 1809 |
return mRootId; |
| 1810 |
} |
| 1811 |
|
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
public List<String> getPath() { |
| 1818 |
return mPath; |
| 1819 |
} |
| 1820 |
|
| 1821 |
@Override |
| 1822 |
public boolean equals(@Nullable Object o) { |
| 1823 |
if (this == o) { |
| 1824 |
return true; |
| 1825 |
} |
| 1826 |
if (o == null || !(o instanceof Path)) { |
| 1827 |
return false; |
| 1828 |
} |
| 1829 |
Path path = (Path) o; |
| 1830 |
return Objects.equals(mRootId, path.mRootId) && |
| 1831 |
Objects.equals(mPath, path.mPath); |
| 1832 |
} |
| 1833 |
|
| 1834 |
@Override |
| 1835 |
public int hashCode() { |
| 1836 |
return Objects.hash(mRootId, mPath); |
| 1837 |
} |
| 1838 |
|
| 1839 |
@Override |
| 1840 |
public String toString() { |
| 1841 |
return new StringBuilder() |
| 1842 |
.append("DocumentsContract.Path{") |
| 1843 |
.append("rootId=") |
| 1844 |
.append(mRootId) |
| 1845 |
.append(", path=") |
| 1846 |
.append(mPath) |
| 1847 |
.append("}") |
| 1848 |
.toString(); |
| 1849 |
} |
| 1850 |
|
| 1851 |
@Override |
| 1852 |
public void writeToParcel(Parcel dest, int flags) { |
| 1853 |
dest.writeString(mRootId); |
| 1854 |
dest.writeStringList(mPath); |
| 1855 |
} |
| 1856 |
|
| 1857 |
@Override |
| 1858 |
public int describeContents() { |
| 1859 |
return 0; |
| 1860 |
} |
| 1861 |
|
| 1862 |
public static final @android.annotation.NonNull Creator<Path> CREATOR = new Creator<Path>() { |
| 1863 |
@Override |
| 1864 |
public Path createFromParcel(Parcel in) { |
| 1865 |
final String rootId = in.readString(); |
| 1866 |
final List<String> path = in.createStringArrayList(); |
| 1867 |
return new Path(rootId, path); |
| 1868 |
} |
| 1869 |
|
| 1870 |
@Override |
| 1871 |
public Path[] newArray(int size) { |
| 1872 |
return new Path[size]; |
| 1873 |
} |
| 1874 |
}; |
| 1875 |
} |
| 1876 |
} |