DocumentsContract.java revision 9b055e1d842c5c68be168807bcf5b84a7c95db80
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.provider; 18 19import static android.net.TrafficStats.KB_IN_BYTES; 20import static android.system.OsConstants.SEEK_SET; 21 22import android.annotation.Nullable; 23import android.content.ContentProviderClient; 24import android.content.ContentResolver; 25import android.content.Context; 26import android.content.Intent; 27import android.content.pm.ResolveInfo; 28import android.content.res.AssetFileDescriptor; 29import android.database.Cursor; 30import android.graphics.Bitmap; 31import android.graphics.BitmapFactory; 32import android.graphics.Matrix; 33import android.graphics.Point; 34import android.media.ExifInterface; 35import android.net.Uri; 36import android.os.Bundle; 37import android.os.CancellationSignal; 38import android.os.OperationCanceledException; 39import android.os.ParcelFileDescriptor; 40import android.os.ParcelFileDescriptor.OnCloseListener; 41import android.os.RemoteException; 42import android.system.ErrnoException; 43import android.system.Os; 44import android.util.Log; 45 46import libcore.io.IoUtils; 47 48import java.io.BufferedInputStream; 49import java.io.File; 50import java.io.FileDescriptor; 51import java.io.FileInputStream; 52import java.io.FileNotFoundException; 53import java.io.IOException; 54import java.util.List; 55 56/** 57 * Defines the contract between a documents provider and the platform. 58 * <p> 59 * To create a document provider, extend {@link DocumentsProvider}, which 60 * provides a foundational implementation of this contract. 61 * <p> 62 * All client apps must hold a valid URI permission grant to access documents, 63 * typically issued when a user makes a selection through 64 * {@link Intent#ACTION_OPEN_DOCUMENT}, {@link Intent#ACTION_CREATE_DOCUMENT}, 65 * {@link Intent#ACTION_OPEN_DOCUMENT_TREE}, or {@link Intent#ACTION_OPEN_EXTERNAL_DIRECTORY}. 66 * 67 * @see DocumentsProvider 68 */ 69public final class DocumentsContract { 70 private static final String TAG = "DocumentsContract"; 71 72 // content://com.example/root/ 73 // content://com.example/root/sdcard/ 74 // content://com.example/root/sdcard/recent/ 75 // content://com.example/root/sdcard/search/?query=pony 76 // content://com.example/document/12/ 77 // content://com.example/document/12/children/ 78 // content://com.example/tree/12/document/24/ 79 // content://com.example/tree/12/document/24/children/ 80 81 private DocumentsContract() { 82 } 83 84 /** 85 * Intent action used to identify {@link DocumentsProvider} instances. This 86 * is used in the {@code <intent-filter>} of a {@code <provider>}. 87 */ 88 public static final String PROVIDER_INTERFACE = "android.content.action.DOCUMENTS_PROVIDER"; 89 90 /** {@hide} */ 91 public static final String EXTRA_PACKAGE_NAME = "android.content.extra.PACKAGE_NAME"; 92 93 /** {@hide} */ 94 public static final String EXTRA_SHOW_ADVANCED = "android.content.extra.SHOW_ADVANCED"; 95 96 /** {@hide} */ 97 public static final String EXTRA_SHOW_FILESIZE = "android.content.extra.SHOW_FILESIZE"; 98 99 /** {@hide} */ 100 public static final String EXTRA_TARGET_URI = "android.content.extra.TARGET_URI"; 101 102 /** 103 * Set this in a DocumentsUI intent to cause a package's own roots to be 104 * excluded from the roots list. 105 */ 106 public static final String EXTRA_EXCLUDE_SELF = "android.provider.extra.EXCLUDE_SELF"; 107 108 /** 109 * Included in {@link AssetFileDescriptor#getExtras()} when returned 110 * thumbnail should be rotated. 111 * 112 * @see MediaStore.Images.ImageColumns#ORIENTATION 113 * @hide 114 */ 115 public static final String EXTRA_ORIENTATION = "android.content.extra.ORIENTATION"; 116 117 /** 118 * Overrides the default prompt text in DocumentsUI when set in an intent. 119 */ 120 public static final String EXTRA_PROMPT = "android.provider.extra.PROMPT"; 121 122 /** {@hide} */ 123 public static final String ACTION_MANAGE_ROOT = "android.provider.action.MANAGE_ROOT"; 124 /** {@hide} */ 125 public static final String ACTION_MANAGE_DOCUMENT = "android.provider.action.MANAGE_DOCUMENT"; 126 127 /** {@hide} */ 128 public static final String ACTION_BROWSE = "android.provider.action.BROWSE"; 129 130 /** {@hide} */ 131 public static final String 132 ACTION_DOCUMENT_ROOT_SETTINGS = "android.provider.action.DOCUMENT_ROOT_SETTINGS"; 133 134 /** 135 * Buffer is large enough to rewind past any EXIF headers. 136 */ 137 private static final int THUMBNAIL_BUFFER_SIZE = (int) (128 * KB_IN_BYTES); 138 139 /** {@hide} */ 140 public static final String PACKAGE_DOCUMENTS_UI = "com.android.documentsui"; 141 142 /** 143 * Constants related to a document, including {@link Cursor} column names 144 * and flags. 145 * <p> 146 * A document can be either an openable stream (with a specific MIME type), 147 * or a directory containing additional documents (with the 148 * {@link #MIME_TYPE_DIR} MIME type). A directory represents the top of a 149 * subtree containing zero or more documents, which can recursively contain 150 * even more documents and directories. 151 * <p> 152 * All columns are <em>read-only</em> to client applications. 153 */ 154 public final static class Document { 155 private Document() { 156 } 157 158 /** 159 * Unique ID of a document. This ID is both provided by and interpreted 160 * by a {@link DocumentsProvider}, and should be treated as an opaque 161 * value by client applications. This column is required. 162 * <p> 163 * Each document must have a unique ID within a provider, but that 164 * single document may be included as a child of multiple directories. 165 * <p> 166 * A provider must always return durable IDs, since they will be used to 167 * issue long-term URI permission grants when an application interacts 168 * with {@link Intent#ACTION_OPEN_DOCUMENT} and 169 * {@link Intent#ACTION_CREATE_DOCUMENT}. 170 * <p> 171 * Type: STRING 172 */ 173 public static final String COLUMN_DOCUMENT_ID = "document_id"; 174 175 /** 176 * Concrete MIME type of a document. For example, "image/png" or 177 * "application/pdf" for openable files. A document can also be a 178 * directory containing additional documents, which is represented with 179 * the {@link #MIME_TYPE_DIR} MIME type. This column is required. 180 * <p> 181 * Type: STRING 182 * 183 * @see #MIME_TYPE_DIR 184 */ 185 public static final String COLUMN_MIME_TYPE = "mime_type"; 186 187 /** 188 * Display name of a document, used as the primary title displayed to a 189 * user. This column is required. 190 * <p> 191 * Type: STRING 192 */ 193 public static final String COLUMN_DISPLAY_NAME = OpenableColumns.DISPLAY_NAME; 194 195 /** 196 * Summary of a document, which may be shown to a user. This column is 197 * optional, and may be {@code null}. 198 * <p> 199 * Type: STRING 200 */ 201 public static final String COLUMN_SUMMARY = "summary"; 202 203 /** 204 * Timestamp when a document was last modified, in milliseconds since 205 * January 1, 1970 00:00:00.0 UTC. This column is required, and may be 206 * {@code null} if unknown. A {@link DocumentsProvider} can update this 207 * field using events from {@link OnCloseListener} or other reliable 208 * {@link ParcelFileDescriptor} transports. 209 * <p> 210 * Type: INTEGER (long) 211 * 212 * @see System#currentTimeMillis() 213 */ 214 public static final String COLUMN_LAST_MODIFIED = "last_modified"; 215 216 /** 217 * Specific icon resource ID for a document. This column is optional, 218 * and may be {@code null} to use a platform-provided default icon based 219 * on {@link #COLUMN_MIME_TYPE}. 220 * <p> 221 * Type: INTEGER (int) 222 */ 223 public static final String COLUMN_ICON = "icon"; 224 225 /** 226 * Flags that apply to a document. This column is required. 227 * <p> 228 * Type: INTEGER (int) 229 * 230 * @see #FLAG_SUPPORTS_WRITE 231 * @see #FLAG_SUPPORTS_DELETE 232 * @see #FLAG_SUPPORTS_THUMBNAIL 233 * @see #FLAG_DIR_PREFERS_GRID 234 * @see #FLAG_DIR_PREFERS_LAST_MODIFIED 235 * @see #FLAG_VIRTUAL_DOCUMENT 236 * @see #FLAG_ARCHIVE 237 * @see #FLAG_SUPPORTS_COPY 238 * @see #FLAG_SUPPORTS_MOVE 239 * @see #FLAG_SUPPORTS_REMOVE 240 */ 241 public static final String COLUMN_FLAGS = "flags"; 242 243 /** 244 * Size of a document, in bytes, or {@code null} if unknown. This column 245 * is required. 246 * <p> 247 * Type: INTEGER (long) 248 */ 249 public static final String COLUMN_SIZE = OpenableColumns.SIZE; 250 251 /** 252 * MIME type of a document which is a directory that may contain 253 * additional documents. 254 * 255 * @see #COLUMN_MIME_TYPE 256 */ 257 public static final String MIME_TYPE_DIR = "vnd.android.document/directory"; 258 259 /** 260 * Flag indicating that a document can be represented as a thumbnail. 261 * 262 * @see #COLUMN_FLAGS 263 * @see DocumentsContract#getDocumentThumbnail(ContentResolver, Uri, 264 * Point, CancellationSignal) 265 * @see DocumentsProvider#openDocumentThumbnail(String, Point, 266 * android.os.CancellationSignal) 267 */ 268 public static final int FLAG_SUPPORTS_THUMBNAIL = 1; 269 270 /** 271 * Flag indicating that a document supports writing. 272 * <p> 273 * When a document is opened with {@link Intent#ACTION_OPEN_DOCUMENT}, 274 * the calling application is granted both 275 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION} and 276 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION}. However, the actual 277 * writability of a document may change over time, for example due to 278 * remote access changes. This flag indicates that a document client can 279 * expect {@link ContentResolver#openOutputStream(Uri)} to succeed. 280 * 281 * @see #COLUMN_FLAGS 282 */ 283 public static final int FLAG_SUPPORTS_WRITE = 1 << 1; 284 285 /** 286 * Flag indicating that a document is deletable. 287 * 288 * @see #COLUMN_FLAGS 289 * @see DocumentsContract#deleteDocument(ContentResolver, Uri) 290 * @see DocumentsProvider#deleteDocument(String) 291 */ 292 public static final int FLAG_SUPPORTS_DELETE = 1 << 2; 293 294 /** 295 * Flag indicating that a document is a directory that supports creation 296 * of new files within it. Only valid when {@link #COLUMN_MIME_TYPE} is 297 * {@link #MIME_TYPE_DIR}. 298 * 299 * @see #COLUMN_FLAGS 300 * @see DocumentsProvider#createDocument(String, String, String) 301 */ 302 public static final int FLAG_DIR_SUPPORTS_CREATE = 1 << 3; 303 304 /** 305 * Flag indicating that a directory prefers its contents be shown in a 306 * larger format grid. Usually suitable when a directory contains mostly 307 * pictures. Only valid when {@link #COLUMN_MIME_TYPE} is 308 * {@link #MIME_TYPE_DIR}. 309 * 310 * @see #COLUMN_FLAGS 311 */ 312 public static final int FLAG_DIR_PREFERS_GRID = 1 << 4; 313 314 /** 315 * Flag indicating that a directory prefers its contents be sorted by 316 * {@link #COLUMN_LAST_MODIFIED}. Only valid when 317 * {@link #COLUMN_MIME_TYPE} is {@link #MIME_TYPE_DIR}. 318 * 319 * @see #COLUMN_FLAGS 320 */ 321 public static final int FLAG_DIR_PREFERS_LAST_MODIFIED = 1 << 5; 322 323 /** 324 * Flag indicating that a document can be renamed. 325 * 326 * @see #COLUMN_FLAGS 327 * @see DocumentsContract#renameDocument(ContentProviderClient, Uri, 328 * String) 329 * @see DocumentsProvider#renameDocument(String, String) 330 */ 331 public static final int FLAG_SUPPORTS_RENAME = 1 << 6; 332 333 /** 334 * Flag indicating that a document can be copied to another location 335 * within the same document provider. 336 * 337 * @see #COLUMN_FLAGS 338 * @see DocumentsContract#copyDocument(ContentProviderClient, Uri, Uri) 339 * @see DocumentsProvider#copyDocument(String, String) 340 */ 341 public static final int FLAG_SUPPORTS_COPY = 1 << 7; 342 343 /** 344 * Flag indicating that a document can be moved to another location 345 * within the same document provider. 346 * 347 * @see #COLUMN_FLAGS 348 * @see DocumentsContract#moveDocument(ContentProviderClient, Uri, Uri, Uri) 349 * @see DocumentsProvider#moveDocument(String, String, String) 350 */ 351 public static final int FLAG_SUPPORTS_MOVE = 1 << 8; 352 353 /** 354 * Flag indicating that a document is virtual, and doesn't have byte 355 * representation in the MIME type specified as {@link #COLUMN_MIME_TYPE}. 356 * 357 * @see #COLUMN_FLAGS 358 * @see #COLUMN_MIME_TYPE 359 * @see DocumentsProvider#openTypedDocument(String, String, Bundle, 360 * android.os.CancellationSignal) 361 */ 362 public static final int FLAG_VIRTUAL_DOCUMENT = 1 << 9; 363 364 /** 365 * Flag indicating that a document can be removed from a parent. 366 * 367 * @see #COLUMN_FLAGS 368 * @see DocumentsContract#removeDocument(ContentProviderClient, Uri, Uri) 369 * @see DocumentsProvider#removeDocument(String, String) 370 */ 371 public static final int FLAG_SUPPORTS_REMOVE = 1 << 10; 372 373 /** 374 * Flag indicating that a document is an archive, and it's contents can be 375 * obtained via {@link DocumentsProvider#queryChildDocuments}. 376 * <p> 377 * The <em>provider</em> support library offers utility classes to add common 378 * archive support. 379 * 380 * @see #COLUMN_FLAGS 381 * @see DocumentsProvider#queryChildDocuments(String, String[], String) 382 * @hide 383 */ 384 385 public static final int FLAG_ARCHIVE = 1 << 15; 386 /** 387 * Flag indicating that document titles should be hidden when viewing 388 * this directory in a larger format grid. For example, a directory 389 * containing only images may want the image thumbnails to speak for 390 * themselves. Only valid when {@link #COLUMN_MIME_TYPE} is 391 * {@link #MIME_TYPE_DIR}. 392 * 393 * @see #COLUMN_FLAGS 394 * @see #FLAG_DIR_PREFERS_GRID 395 * @hide 396 */ 397 public static final int FLAG_DIR_HIDE_GRID_TITLES = 1 << 16; 398 } 399 400 /** 401 * Constants related to a root of documents, including {@link Cursor} column 402 * names and flags. A root is the start of a tree of documents, such as a 403 * physical storage device, or an account. Each root starts at the directory 404 * referenced by {@link Root#COLUMN_DOCUMENT_ID}, which can recursively 405 * contain both documents and directories. 406 * <p> 407 * All columns are <em>read-only</em> to client applications. 408 */ 409 public final static class Root { 410 private Root() { 411 } 412 413 /** 414 * Unique ID of a root. This ID is both provided by and interpreted by a 415 * {@link DocumentsProvider}, and should be treated as an opaque value 416 * by client applications. This column is required. 417 * <p> 418 * Type: STRING 419 */ 420 public static final String COLUMN_ROOT_ID = "root_id"; 421 422 /** 423 * Flags that apply to a root. This column is required. 424 * <p> 425 * Type: INTEGER (int) 426 * 427 * @see #FLAG_LOCAL_ONLY 428 * @see #FLAG_SUPPORTS_CREATE 429 * @see #FLAG_SUPPORTS_RECENTS 430 * @see #FLAG_SUPPORTS_SEARCH 431 */ 432 public static final String COLUMN_FLAGS = "flags"; 433 434 /** 435 * Icon resource ID for a root. This column is required. 436 * <p> 437 * Type: INTEGER (int) 438 */ 439 public static final String COLUMN_ICON = "icon"; 440 441 /** 442 * Title for a root, which will be shown to a user. This column is 443 * required. For a single storage service surfacing multiple accounts as 444 * different roots, this title should be the name of the service. 445 * <p> 446 * Type: STRING 447 */ 448 public static final String COLUMN_TITLE = "title"; 449 450 /** 451 * Summary for this root, which may be shown to a user. This column is 452 * optional, and may be {@code null}. For a single storage service 453 * surfacing multiple accounts as different roots, this summary should 454 * be the name of the account. 455 * <p> 456 * Type: STRING 457 */ 458 public static final String COLUMN_SUMMARY = "summary"; 459 460 /** 461 * Document which is a directory that represents the top directory of 462 * this root. This column is required. 463 * <p> 464 * Type: STRING 465 * 466 * @see Document#COLUMN_DOCUMENT_ID 467 */ 468 public static final String COLUMN_DOCUMENT_ID = "document_id"; 469 470 /** 471 * Number of bytes available in this root. This column is optional, and 472 * may be {@code null} if unknown or unbounded. 473 * <p> 474 * Type: INTEGER (long) 475 */ 476 public static final String COLUMN_AVAILABLE_BYTES = "available_bytes"; 477 478 /** 479 * Capacity of a root in bytes. This column is optional, and may be 480 * {@code null} if unknown or unbounded. 481 * {@hide} 482 * <p> 483 * Type: INTEGER (long) 484 */ 485 public static final String COLUMN_CAPACITY_BYTES = "capacity_bytes"; 486 487 /** 488 * MIME types supported by this root. This column is optional, and if 489 * {@code null} the root is assumed to support all MIME types. Multiple 490 * MIME types can be separated by a newline. For example, a root 491 * supporting audio might return "audio/*\napplication/x-flac". 492 * <p> 493 * Type: STRING 494 */ 495 public static final String COLUMN_MIME_TYPES = "mime_types"; 496 497 /** {@hide} */ 498 public static final String MIME_TYPE_ITEM = "vnd.android.document/root"; 499 500 /** 501 * Flag indicating that at least one directory under this root supports 502 * creating content. Roots with this flag will be shown when an 503 * application interacts with {@link Intent#ACTION_CREATE_DOCUMENT}. 504 * 505 * @see #COLUMN_FLAGS 506 */ 507 public static final int FLAG_SUPPORTS_CREATE = 1; 508 509 /** 510 * Flag indicating that this root offers content that is strictly local 511 * on the device. That is, no network requests are made for the content. 512 * 513 * @see #COLUMN_FLAGS 514 * @see Intent#EXTRA_LOCAL_ONLY 515 */ 516 public static final int FLAG_LOCAL_ONLY = 1 << 1; 517 518 /** 519 * Flag indicating that this root can be queried to provide recently 520 * modified documents. 521 * 522 * @see #COLUMN_FLAGS 523 * @see DocumentsContract#buildRecentDocumentsUri(String, String) 524 * @see DocumentsProvider#queryRecentDocuments(String, String[]) 525 */ 526 public static final int FLAG_SUPPORTS_RECENTS = 1 << 2; 527 528 /** 529 * Flag indicating that this root supports search. 530 * 531 * @see #COLUMN_FLAGS 532 * @see DocumentsContract#buildSearchDocumentsUri(String, String, 533 * String) 534 * @see DocumentsProvider#querySearchDocuments(String, String, 535 * String[]) 536 */ 537 public static final int FLAG_SUPPORTS_SEARCH = 1 << 3; 538 539 /** 540 * Flag indicating that this root supports testing parent child 541 * relationships. 542 * 543 * @see #COLUMN_FLAGS 544 * @see DocumentsProvider#isChildDocument(String, String) 545 */ 546 public static final int FLAG_SUPPORTS_IS_CHILD = 1 << 4; 547 548 /** 549 * Flag indicating that this root is currently empty. This may be used 550 * to hide the root when opening documents, but the root will still be 551 * shown when creating documents and {@link #FLAG_SUPPORTS_CREATE} is 552 * also set. If the value of this flag changes, such as when a root 553 * becomes non-empty, you must send a content changed notification for 554 * {@link DocumentsContract#buildRootsUri(String)}. 555 * 556 * @see #COLUMN_FLAGS 557 * @see ContentResolver#notifyChange(Uri, 558 * android.database.ContentObserver, boolean) 559 * @hide 560 */ 561 public static final int FLAG_EMPTY = 1 << 16; 562 563 /** 564 * Flag indicating that this root should only be visible to advanced 565 * users. 566 * 567 * @see #COLUMN_FLAGS 568 * @hide 569 */ 570 public static final int FLAG_ADVANCED = 1 << 17; 571 572 /** 573 * Flag indicating that this root has settings. 574 * 575 * @see #COLUMN_FLAGS 576 * @see DocumentsContract#ACTION_DOCUMENT_ROOT_SETTINGS 577 * @hide 578 */ 579 public static final int FLAG_HAS_SETTINGS = 1 << 18; 580 } 581 582 /** 583 * Optional boolean flag included in a directory {@link Cursor#getExtras()} 584 * indicating that a document provider is still loading data. For example, a 585 * provider has returned some results, but is still waiting on an 586 * outstanding network request. The provider must send a content changed 587 * notification when loading is finished. 588 * 589 * @see ContentResolver#notifyChange(Uri, android.database.ContentObserver, 590 * boolean) 591 */ 592 public static final String EXTRA_LOADING = "loading"; 593 594 /** 595 * Optional string included in a directory {@link Cursor#getExtras()} 596 * providing an informational message that should be shown to a user. For 597 * example, a provider may wish to indicate that not all documents are 598 * available. 599 */ 600 public static final String EXTRA_INFO = "info"; 601 602 /** 603 * Optional string included in a directory {@link Cursor#getExtras()} 604 * providing an error message that should be shown to a user. For example, a 605 * provider may wish to indicate that a network error occurred. The user may 606 * choose to retry, resulting in a new query. 607 */ 608 public static final String EXTRA_ERROR = "error"; 609 610 /** 611 * Optional result (I'm thinking boolean) answer to a question. 612 * {@hide} 613 */ 614 public static final String EXTRA_RESULT = "result"; 615 616 /** {@hide} */ 617 public static final String METHOD_CREATE_DOCUMENT = "android:createDocument"; 618 /** {@hide} */ 619 public static final String METHOD_RENAME_DOCUMENT = "android:renameDocument"; 620 /** {@hide} */ 621 public static final String METHOD_DELETE_DOCUMENT = "android:deleteDocument"; 622 /** {@hide} */ 623 public static final String METHOD_COPY_DOCUMENT = "android:copyDocument"; 624 /** {@hide} */ 625 public static final String METHOD_MOVE_DOCUMENT = "android:moveDocument"; 626 /** {@hide} */ 627 public static final String METHOD_IS_CHILD_DOCUMENT = "android:isChildDocument"; 628 /** {@hide} */ 629 public static final String METHOD_REMOVE_DOCUMENT = "android:removeDocument"; 630 631 /** {@hide} */ 632 public static final String EXTRA_PARENT_URI = "parentUri"; 633 /** {@hide} */ 634 public static final String EXTRA_URI = "uri"; 635 636 private static final String PATH_ROOT = "root"; 637 private static final String PATH_RECENT = "recent"; 638 private static final String PATH_DOCUMENT = "document"; 639 private static final String PATH_CHILDREN = "children"; 640 private static final String PATH_SEARCH = "search"; 641 private static final String PATH_TREE = "tree"; 642 643 private static final String PARAM_QUERY = "query"; 644 private static final String PARAM_MANAGE = "manage"; 645 646 /** 647 * Build URI representing the roots of a document provider. When queried, a 648 * provider will return one or more rows with columns defined by 649 * {@link Root}. 650 * 651 * @see DocumentsProvider#queryRoots(String[]) 652 */ 653 public static Uri buildRootsUri(String authority) { 654 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) 655 .authority(authority).appendPath(PATH_ROOT).build(); 656 } 657 658 /** 659 * Build URI representing the given {@link Root#COLUMN_ROOT_ID} in a 660 * document provider. 661 * 662 * @see #getRootId(Uri) 663 */ 664 public static Uri buildRootUri(String authority, String rootId) { 665 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) 666 .authority(authority).appendPath(PATH_ROOT).appendPath(rootId).build(); 667 } 668 669 /** 670 * Builds URI for user home directory on external (local) storage. 671 * {@hide} 672 */ 673 public static Uri buildHomeUri() { 674 // TODO: Avoid this type of interpackage copying. Added here to avoid 675 // direct coupling, but not ideal. 676 return DocumentsContract.buildRootUri("com.android.externalstorage.documents", "home"); 677 } 678 679 /** 680 * Build URI representing the recently modified documents of a specific root 681 * in a document provider. When queried, a provider will return zero or more 682 * rows with columns defined by {@link Document}. 683 * 684 * @see DocumentsProvider#queryRecentDocuments(String, String[]) 685 * @see #getRootId(Uri) 686 */ 687 public static Uri buildRecentDocumentsUri(String authority, String rootId) { 688 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) 689 .authority(authority).appendPath(PATH_ROOT).appendPath(rootId) 690 .appendPath(PATH_RECENT).build(); 691 } 692 693 /** 694 * Build URI representing access to descendant documents of the given 695 * {@link Document#COLUMN_DOCUMENT_ID}. 696 * 697 * @see #getTreeDocumentId(Uri) 698 */ 699 public static Uri buildTreeDocumentUri(String authority, String documentId) { 700 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority) 701 .appendPath(PATH_TREE).appendPath(documentId).build(); 702 } 703 704 /** 705 * Build URI representing the target {@link Document#COLUMN_DOCUMENT_ID} in 706 * a document provider. When queried, a provider will return a single row 707 * with columns defined by {@link Document}. 708 * 709 * @see DocumentsProvider#queryDocument(String, String[]) 710 * @see #getDocumentId(Uri) 711 */ 712 public static Uri buildDocumentUri(String authority, String documentId) { 713 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) 714 .authority(authority).appendPath(PATH_DOCUMENT).appendPath(documentId).build(); 715 } 716 717 /** 718 * Build URI representing the target {@link Document#COLUMN_DOCUMENT_ID} in 719 * a document provider. When queried, a provider will return a single row 720 * with columns defined by {@link Document}. 721 * <p> 722 * However, instead of directly accessing the target document, the returned 723 * URI will leverage access granted through a subtree URI, typically 724 * returned by {@link Intent#ACTION_OPEN_DOCUMENT_TREE}. The target document 725 * must be a descendant (child, grandchild, etc) of the subtree. 726 * <p> 727 * This is typically used to access documents under a user-selected 728 * directory tree, since it doesn't require the user to separately confirm 729 * each new document access. 730 * 731 * @param treeUri the subtree to leverage to gain access to the target 732 * document. The target directory must be a descendant of this 733 * subtree. 734 * @param documentId the target document, which the caller may not have 735 * direct access to. 736 * @see Intent#ACTION_OPEN_DOCUMENT_TREE 737 * @see DocumentsProvider#isChildDocument(String, String) 738 * @see #buildDocumentUri(String, String) 739 */ 740 public static Uri buildDocumentUriUsingTree(Uri treeUri, String documentId) { 741 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) 742 .authority(treeUri.getAuthority()).appendPath(PATH_TREE) 743 .appendPath(getTreeDocumentId(treeUri)).appendPath(PATH_DOCUMENT) 744 .appendPath(documentId).build(); 745 } 746 747 /** {@hide} */ 748 public static Uri buildDocumentUriMaybeUsingTree(Uri baseUri, String documentId) { 749 if (isTreeUri(baseUri)) { 750 return buildDocumentUriUsingTree(baseUri, documentId); 751 } else { 752 return buildDocumentUri(baseUri.getAuthority(), documentId); 753 } 754 } 755 756 /** 757 * Build URI representing the children of the target directory in a document 758 * provider. When queried, a provider will return zero or more rows with 759 * columns defined by {@link Document}. 760 * 761 * @param parentDocumentId the document to return children for, which must 762 * be a directory with MIME type of 763 * {@link Document#MIME_TYPE_DIR}. 764 * @see DocumentsProvider#queryChildDocuments(String, String[], String) 765 * @see #getDocumentId(Uri) 766 */ 767 public static Uri buildChildDocumentsUri(String authority, String parentDocumentId) { 768 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority) 769 .appendPath(PATH_DOCUMENT).appendPath(parentDocumentId).appendPath(PATH_CHILDREN) 770 .build(); 771 } 772 773 /** 774 * Build URI representing the children of the target directory in a document 775 * provider. When queried, a provider will return zero or more rows with 776 * columns defined by {@link Document}. 777 * <p> 778 * However, instead of directly accessing the target directory, the returned 779 * URI will leverage access granted through a subtree URI, typically 780 * returned by {@link Intent#ACTION_OPEN_DOCUMENT_TREE}. The target 781 * directory must be a descendant (child, grandchild, etc) of the subtree. 782 * <p> 783 * This is typically used to access documents under a user-selected 784 * directory tree, since it doesn't require the user to separately confirm 785 * each new document access. 786 * 787 * @param treeUri the subtree to leverage to gain access to the target 788 * document. The target directory must be a descendant of this 789 * subtree. 790 * @param parentDocumentId the document to return children for, which the 791 * caller may not have direct access to, and which must be a 792 * directory with MIME type of {@link Document#MIME_TYPE_DIR}. 793 * @see Intent#ACTION_OPEN_DOCUMENT_TREE 794 * @see DocumentsProvider#isChildDocument(String, String) 795 * @see #buildChildDocumentsUri(String, String) 796 */ 797 public static Uri buildChildDocumentsUriUsingTree(Uri treeUri, String parentDocumentId) { 798 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) 799 .authority(treeUri.getAuthority()).appendPath(PATH_TREE) 800 .appendPath(getTreeDocumentId(treeUri)).appendPath(PATH_DOCUMENT) 801 .appendPath(parentDocumentId).appendPath(PATH_CHILDREN).build(); 802 } 803 804 /** 805 * Build URI representing a search for matching documents under a specific 806 * root in a document provider. When queried, a provider will return zero or 807 * more rows with columns defined by {@link Document}. 808 * 809 * @see DocumentsProvider#querySearchDocuments(String, String, String[]) 810 * @see #getRootId(Uri) 811 * @see #getSearchDocumentsQuery(Uri) 812 */ 813 public static Uri buildSearchDocumentsUri( 814 String authority, String rootId, String query) { 815 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority) 816 .appendPath(PATH_ROOT).appendPath(rootId).appendPath(PATH_SEARCH) 817 .appendQueryParameter(PARAM_QUERY, query).build(); 818 } 819 820 /** 821 * Test if the given URI represents a {@link Document} backed by a 822 * {@link DocumentsProvider}. 823 * 824 * @see #buildDocumentUri(String, String) 825 * @see #buildDocumentUriUsingTree(Uri, String) 826 */ 827 public static boolean isDocumentUri(Context context, @Nullable Uri uri) { 828 if (isContentUri(uri) && isDocumentsProvider(context, uri.getAuthority())) { 829 final List<String> paths = uri.getPathSegments(); 830 if (paths.size() == 2) { 831 return PATH_DOCUMENT.equals(paths.get(0)); 832 } else if (paths.size() == 4) { 833 return PATH_TREE.equals(paths.get(0)) && PATH_DOCUMENT.equals(paths.get(2)); 834 } 835 } 836 return false; 837 } 838 839 /** {@hide} */ 840 public static boolean isRootUri(Context context, @Nullable Uri uri) { 841 if (isContentUri(uri) && isDocumentsProvider(context, uri.getAuthority())) { 842 final List<String> paths = uri.getPathSegments(); 843 return (paths.size() == 2 && PATH_ROOT.equals(paths.get(0))); 844 } 845 return false; 846 } 847 848 /** {@hide} */ 849 public static boolean isContentUri(@Nullable Uri uri) { 850 return uri != null && ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()); 851 } 852 853 /** 854 * Test if the given URI represents a {@link Document} tree. 855 * 856 * @see #buildTreeDocumentUri(String, String) 857 * @see #getTreeDocumentId(Uri, String) 858 */ 859 public static boolean isTreeUri(Uri uri) { 860 final List<String> paths = uri.getPathSegments(); 861 return (paths.size() >= 2 && PATH_TREE.equals(paths.get(0))); 862 } 863 864 private static boolean isDocumentsProvider(Context context, String authority) { 865 final Intent intent = new Intent(PROVIDER_INTERFACE); 866 final List<ResolveInfo> infos = context.getPackageManager() 867 .queryIntentContentProviders(intent, 0); 868 for (ResolveInfo info : infos) { 869 if (authority.equals(info.providerInfo.authority)) { 870 return true; 871 } 872 } 873 return false; 874 } 875 876 /** 877 * Extract the {@link Root#COLUMN_ROOT_ID} from the given URI. 878 */ 879 public static String getRootId(Uri rootUri) { 880 final List<String> paths = rootUri.getPathSegments(); 881 if (paths.size() >= 2 && PATH_ROOT.equals(paths.get(0))) { 882 return paths.get(1); 883 } 884 throw new IllegalArgumentException("Invalid URI: " + rootUri); 885 } 886 887 /** 888 * Extract the {@link Document#COLUMN_DOCUMENT_ID} from the given URI. 889 * 890 * @see #isDocumentUri(Context, Uri) 891 */ 892 public static String getDocumentId(Uri documentUri) { 893 final List<String> paths = documentUri.getPathSegments(); 894 if (paths.size() >= 2 && PATH_DOCUMENT.equals(paths.get(0))) { 895 return paths.get(1); 896 } 897 if (paths.size() >= 4 && PATH_TREE.equals(paths.get(0)) 898 && PATH_DOCUMENT.equals(paths.get(2))) { 899 return paths.get(3); 900 } 901 throw new IllegalArgumentException("Invalid URI: " + documentUri); 902 } 903 904 /** 905 * Extract the via {@link Document#COLUMN_DOCUMENT_ID} from the given URI. 906 */ 907 public static String getTreeDocumentId(Uri documentUri) { 908 final List<String> paths = documentUri.getPathSegments(); 909 if (paths.size() >= 2 && PATH_TREE.equals(paths.get(0))) { 910 return paths.get(1); 911 } 912 throw new IllegalArgumentException("Invalid URI: " + documentUri); 913 } 914 915 /** 916 * Extract the search query from a URI built by 917 * {@link #buildSearchDocumentsUri(String, String, String)}. 918 */ 919 public static String getSearchDocumentsQuery(Uri searchDocumentsUri) { 920 return searchDocumentsUri.getQueryParameter(PARAM_QUERY); 921 } 922 923 /** {@hide} */ 924 public static Uri setManageMode(Uri uri) { 925 return uri.buildUpon().appendQueryParameter(PARAM_MANAGE, "true").build(); 926 } 927 928 /** {@hide} */ 929 public static boolean isManageMode(Uri uri) { 930 return uri.getBooleanQueryParameter(PARAM_MANAGE, false); 931 } 932 933 /** 934 * Return thumbnail representing the document at the given URI. Callers are 935 * responsible for their own in-memory caching. 936 * 937 * @param documentUri document to return thumbnail for, which must have 938 * {@link Document#FLAG_SUPPORTS_THUMBNAIL} set. 939 * @param size optimal thumbnail size desired. A provider may return a 940 * thumbnail of a different size, but never more than double the 941 * requested size. 942 * @param signal signal used to indicate if caller is no longer interested 943 * in the thumbnail. 944 * @return decoded thumbnail, or {@code null} if problem was encountered. 945 * @see DocumentsProvider#openDocumentThumbnail(String, Point, 946 * android.os.CancellationSignal) 947 */ 948 public static Bitmap getDocumentThumbnail( 949 ContentResolver resolver, Uri documentUri, Point size, CancellationSignal signal) { 950 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( 951 documentUri.getAuthority()); 952 try { 953 return getDocumentThumbnail(client, documentUri, size, signal); 954 } catch (Exception e) { 955 if (!(e instanceof OperationCanceledException)) { 956 Log.w(TAG, "Failed to load thumbnail for " + documentUri + ": " + e); 957 } 958 return null; 959 } finally { 960 ContentProviderClient.releaseQuietly(client); 961 } 962 } 963 964 /** {@hide} */ 965 public static Bitmap getDocumentThumbnail( 966 ContentProviderClient client, Uri documentUri, Point size, CancellationSignal signal) 967 throws RemoteException, IOException { 968 final Bundle openOpts = new Bundle(); 969 openOpts.putParcelable(ContentResolver.EXTRA_SIZE, size); 970 971 AssetFileDescriptor afd = null; 972 Bitmap bitmap = null; 973 try { 974 afd = client.openTypedAssetFileDescriptor(documentUri, "image/*", openOpts, signal); 975 976 final FileDescriptor fd = afd.getFileDescriptor(); 977 final long offset = afd.getStartOffset(); 978 979 // Try seeking on the returned FD, since it gives us the most 980 // optimal decode path; otherwise fall back to buffering. 981 BufferedInputStream is = null; 982 try { 983 Os.lseek(fd, offset, SEEK_SET); 984 } catch (ErrnoException e) { 985 is = new BufferedInputStream(new FileInputStream(fd), THUMBNAIL_BUFFER_SIZE); 986 is.mark(THUMBNAIL_BUFFER_SIZE); 987 } 988 989 // We requested a rough thumbnail size, but the remote size may have 990 // returned something giant, so defensively scale down as needed. 991 final BitmapFactory.Options opts = new BitmapFactory.Options(); 992 opts.inJustDecodeBounds = true; 993 if (is != null) { 994 BitmapFactory.decodeStream(is, null, opts); 995 } else { 996 BitmapFactory.decodeFileDescriptor(fd, null, opts); 997 } 998 999 final int widthSample = opts.outWidth / size.x; 1000 final int heightSample = opts.outHeight / size.y; 1001 1002 opts.inJustDecodeBounds = false; 1003 opts.inSampleSize = Math.min(widthSample, heightSample); 1004 if (is != null) { 1005 is.reset(); 1006 bitmap = BitmapFactory.decodeStream(is, null, opts); 1007 } else { 1008 try { 1009 Os.lseek(fd, offset, SEEK_SET); 1010 } catch (ErrnoException e) { 1011 e.rethrowAsIOException(); 1012 } 1013 bitmap = BitmapFactory.decodeFileDescriptor(fd, null, opts); 1014 } 1015 1016 // Transform the bitmap if requested. We use a side-channel to 1017 // communicate the orientation, since EXIF thumbnails don't contain 1018 // the rotation flags of the original image. 1019 final Bundle extras = afd.getExtras(); 1020 final int orientation = (extras != null) ? extras.getInt(EXTRA_ORIENTATION, 0) : 0; 1021 if (orientation != 0) { 1022 final int width = bitmap.getWidth(); 1023 final int height = bitmap.getHeight(); 1024 1025 final Matrix m = new Matrix(); 1026 m.setRotate(orientation, width / 2, height / 2); 1027 bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, false); 1028 } 1029 } finally { 1030 IoUtils.closeQuietly(afd); 1031 } 1032 1033 return bitmap; 1034 } 1035 1036 /** 1037 * Create a new document with given MIME type and display name. 1038 * 1039 * @param parentDocumentUri directory with 1040 * {@link Document#FLAG_DIR_SUPPORTS_CREATE} 1041 * @param mimeType MIME type of new document 1042 * @param displayName name of new document 1043 * @return newly created document, or {@code null} if failed 1044 */ 1045 public static Uri createDocument(ContentResolver resolver, Uri parentDocumentUri, 1046 String mimeType, String displayName) { 1047 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( 1048 parentDocumentUri.getAuthority()); 1049 try { 1050 return createDocument(client, parentDocumentUri, mimeType, displayName); 1051 } catch (Exception e) { 1052 Log.w(TAG, "Failed to create document", e); 1053 return null; 1054 } finally { 1055 ContentProviderClient.releaseQuietly(client); 1056 } 1057 } 1058 1059 /** {@hide} */ 1060 public static Uri createDocument(ContentProviderClient client, Uri parentDocumentUri, 1061 String mimeType, String displayName) throws RemoteException { 1062 final Bundle in = new Bundle(); 1063 in.putParcelable(DocumentsContract.EXTRA_URI, parentDocumentUri); 1064 in.putString(Document.COLUMN_MIME_TYPE, mimeType); 1065 in.putString(Document.COLUMN_DISPLAY_NAME, displayName); 1066 1067 final Bundle out = client.call(METHOD_CREATE_DOCUMENT, null, in); 1068 return out.getParcelable(DocumentsContract.EXTRA_URI); 1069 } 1070 1071 /** {@hide} */ 1072 public static boolean isChildDocument(ContentProviderClient client, Uri parentDocumentUri, 1073 Uri childDocumentUri) throws RemoteException { 1074 1075 final Bundle in = new Bundle(); 1076 in.putParcelable(DocumentsContract.EXTRA_URI, parentDocumentUri); 1077 in.putParcelable(DocumentsContract.EXTRA_TARGET_URI, childDocumentUri); 1078 1079 final Bundle out = client.call(METHOD_IS_CHILD_DOCUMENT, null, in); 1080 if (out == null) { 1081 throw new RemoteException("Failed to get a reponse from isChildDocument query."); 1082 } 1083 if (!out.containsKey(DocumentsContract.EXTRA_RESULT)) { 1084 throw new RemoteException("Response did not include result field.."); 1085 } 1086 return out.getBoolean(DocumentsContract.EXTRA_RESULT); 1087 } 1088 1089 /** 1090 * Change the display name of an existing document. 1091 * <p> 1092 * If the underlying provider needs to create a new 1093 * {@link Document#COLUMN_DOCUMENT_ID} to represent the updated display 1094 * name, that new document is returned and the original document is no 1095 * longer valid. Otherwise, the original document is returned. 1096 * 1097 * @param documentUri document with {@link Document#FLAG_SUPPORTS_RENAME} 1098 * @param displayName updated name for document 1099 * @return the existing or new document after the rename, or {@code null} if 1100 * failed. 1101 */ 1102 public static Uri renameDocument(ContentResolver resolver, Uri documentUri, 1103 String displayName) { 1104 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( 1105 documentUri.getAuthority()); 1106 try { 1107 return renameDocument(client, documentUri, displayName); 1108 } catch (Exception e) { 1109 Log.w(TAG, "Failed to rename document", e); 1110 return null; 1111 } finally { 1112 ContentProviderClient.releaseQuietly(client); 1113 } 1114 } 1115 1116 /** {@hide} */ 1117 public static Uri renameDocument(ContentProviderClient client, Uri documentUri, 1118 String displayName) throws RemoteException { 1119 final Bundle in = new Bundle(); 1120 in.putParcelable(DocumentsContract.EXTRA_URI, documentUri); 1121 in.putString(Document.COLUMN_DISPLAY_NAME, displayName); 1122 1123 final Bundle out = client.call(METHOD_RENAME_DOCUMENT, null, in); 1124 final Uri outUri = out.getParcelable(DocumentsContract.EXTRA_URI); 1125 return (outUri != null) ? outUri : documentUri; 1126 } 1127 1128 /** 1129 * Delete the given document. 1130 * 1131 * @param documentUri document with {@link Document#FLAG_SUPPORTS_DELETE} 1132 * @return if the document was deleted successfully. 1133 */ 1134 public static boolean deleteDocument(ContentResolver resolver, Uri documentUri) { 1135 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( 1136 documentUri.getAuthority()); 1137 try { 1138 deleteDocument(client, documentUri); 1139 return true; 1140 } catch (Exception e) { 1141 Log.w(TAG, "Failed to delete document", e); 1142 return false; 1143 } finally { 1144 ContentProviderClient.releaseQuietly(client); 1145 } 1146 } 1147 1148 /** {@hide} */ 1149 public static void deleteDocument(ContentProviderClient client, Uri documentUri) 1150 throws RemoteException { 1151 final Bundle in = new Bundle(); 1152 in.putParcelable(DocumentsContract.EXTRA_URI, documentUri); 1153 1154 client.call(METHOD_DELETE_DOCUMENT, null, in); 1155 } 1156 1157 /** 1158 * Copies the given document. 1159 * 1160 * @param sourceDocumentUri document with {@link Document#FLAG_SUPPORTS_COPY} 1161 * @param targetParentDocumentUri document which will become a parent of the source 1162 * document's copy. 1163 * @return the copied document, or {@code null} if failed. 1164 */ 1165 public static Uri copyDocument(ContentResolver resolver, Uri sourceDocumentUri, 1166 Uri targetParentDocumentUri) { 1167 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( 1168 sourceDocumentUri.getAuthority()); 1169 try { 1170 return copyDocument(client, sourceDocumentUri, targetParentDocumentUri); 1171 } catch (Exception e) { 1172 Log.w(TAG, "Failed to copy document", e); 1173 return null; 1174 } finally { 1175 ContentProviderClient.releaseQuietly(client); 1176 } 1177 } 1178 1179 /** {@hide} */ 1180 public static Uri copyDocument(ContentProviderClient client, Uri sourceDocumentUri, 1181 Uri targetParentDocumentUri) throws RemoteException { 1182 final Bundle in = new Bundle(); 1183 in.putParcelable(DocumentsContract.EXTRA_URI, sourceDocumentUri); 1184 in.putParcelable(DocumentsContract.EXTRA_TARGET_URI, targetParentDocumentUri); 1185 1186 final Bundle out = client.call(METHOD_COPY_DOCUMENT, null, in); 1187 return out.getParcelable(DocumentsContract.EXTRA_URI); 1188 } 1189 1190 /** 1191 * Moves the given document under a new parent. 1192 * 1193 * @param sourceDocumentUri document with {@link Document#FLAG_SUPPORTS_MOVE} 1194 * @param sourceParentDocumentUri parent document of the document to move. 1195 * @param targetParentDocumentUri document which will become a new parent of the source 1196 * document. 1197 * @return the moved document, or {@code null} if failed. 1198 */ 1199 public static Uri moveDocument(ContentResolver resolver, Uri sourceDocumentUri, 1200 Uri sourceParentDocumentUri, Uri targetParentDocumentUri) { 1201 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( 1202 sourceDocumentUri.getAuthority()); 1203 try { 1204 return moveDocument(client, sourceParentDocumentUri, sourceDocumentUri, 1205 targetParentDocumentUri); 1206 } catch (Exception e) { 1207 Log.w(TAG, "Failed to move document", e); 1208 return null; 1209 } finally { 1210 ContentProviderClient.releaseQuietly(client); 1211 } 1212 } 1213 1214 /** {@hide} */ 1215 public static Uri moveDocument(ContentProviderClient client, Uri sourceDocumentUri, 1216 Uri sourceParentDocumentUri, Uri targetParentDocumentUri) throws RemoteException { 1217 final Bundle in = new Bundle(); 1218 in.putParcelable(DocumentsContract.EXTRA_URI, sourceDocumentUri); 1219 in.putParcelable(DocumentsContract.EXTRA_PARENT_URI, sourceParentDocumentUri); 1220 in.putParcelable(DocumentsContract.EXTRA_TARGET_URI, targetParentDocumentUri); 1221 1222 final Bundle out = client.call(METHOD_MOVE_DOCUMENT, null, in); 1223 return out.getParcelable(DocumentsContract.EXTRA_URI); 1224 } 1225 1226 /** 1227 * Removes the given document from a parent directory. 1228 * 1229 * <p>In contrast to {@link #deleteDocument} it requires specifying the parent. 1230 * This method is especially useful if the document can be in multiple parents. 1231 * 1232 * @param documentUri document with {@link Document#FLAG_SUPPORTS_REMOVE} 1233 * @param parentDocumentUri parent document of the document to remove. 1234 * @return true if the document was removed successfully. 1235 */ 1236 public static boolean removeDocument(ContentResolver resolver, Uri documentUri, 1237 Uri parentDocumentUri) { 1238 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( 1239 documentUri.getAuthority()); 1240 try { 1241 removeDocument(client, documentUri, parentDocumentUri); 1242 return true; 1243 } catch (Exception e) { 1244 Log.w(TAG, "Failed to remove document", e); 1245 return false; 1246 } finally { 1247 ContentProviderClient.releaseQuietly(client); 1248 } 1249 } 1250 1251 /** {@hide} */ 1252 public static void removeDocument(ContentProviderClient client, Uri documentUri, 1253 Uri parentDocumentUri) throws RemoteException { 1254 final Bundle in = new Bundle(); 1255 in.putParcelable(DocumentsContract.EXTRA_URI, documentUri); 1256 in.putParcelable(DocumentsContract.EXTRA_PARENT_URI, parentDocumentUri); 1257 1258 client.call(METHOD_REMOVE_DOCUMENT, null, in); 1259 } 1260 1261 /** 1262 * Open the given image for thumbnail purposes, using any embedded EXIF 1263 * thumbnail if available, and providing orientation hints from the parent 1264 * image. 1265 * 1266 * @hide 1267 */ 1268 public static AssetFileDescriptor openImageThumbnail(File file) throws FileNotFoundException { 1269 final ParcelFileDescriptor pfd = ParcelFileDescriptor.open( 1270 file, ParcelFileDescriptor.MODE_READ_ONLY); 1271 Bundle extras = null; 1272 1273 try { 1274 final ExifInterface exif = new ExifInterface(file.getAbsolutePath()); 1275 1276 switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1)) { 1277 case ExifInterface.ORIENTATION_ROTATE_90: 1278 extras = new Bundle(1); 1279 extras.putInt(EXTRA_ORIENTATION, 90); 1280 break; 1281 case ExifInterface.ORIENTATION_ROTATE_180: 1282 extras = new Bundle(1); 1283 extras.putInt(EXTRA_ORIENTATION, 180); 1284 break; 1285 case ExifInterface.ORIENTATION_ROTATE_270: 1286 extras = new Bundle(1); 1287 extras.putInt(EXTRA_ORIENTATION, 270); 1288 break; 1289 } 1290 1291 final long[] thumb = exif.getThumbnailRange(); 1292 if (thumb != null) { 1293 return new AssetFileDescriptor(pfd, thumb[0], thumb[1], extras); 1294 } 1295 } catch (IOException e) { 1296 } 1297 1298 return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH, extras); 1299 } 1300} 1301