Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 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 | |
| 17 | package android.provider; |
| 18 | |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 19 | import static android.system.OsConstants.SEEK_SET; |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 20 | |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 21 | import static com.android.internal.util.Preconditions.checkArgument; |
| 22 | import static com.android.internal.util.Preconditions.checkCollectionElementsNotNull; |
| 23 | import static com.android.internal.util.Preconditions.checkCollectionNotEmpty; |
| 24 | |
Steve McKay | 323ee3e | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 25 | import android.annotation.Nullable; |
Mathew Inwood | ba50311 | 2018-08-10 09:37:35 +0100 | [diff] [blame] | 26 | import android.annotation.UnsupportedAppUsage; |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 27 | import android.content.ContentProviderClient; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 28 | import android.content.ContentResolver; |
Jeff Sharkey | ee2f7df | 2013-09-26 11:32:30 -0700 | [diff] [blame] | 29 | import android.content.Context; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 30 | import android.content.Intent; |
Tomasz Mikolajewski | cf31656 | 2016-10-24 15:17:01 +0900 | [diff] [blame] | 31 | import android.content.IntentSender; |
Jeff Sharkey | d2e1e81 | 2013-10-09 13:31:13 -0700 | [diff] [blame] | 32 | import android.content.pm.ResolveInfo; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 33 | import android.content.res.AssetFileDescriptor; |
Jeff Sharkey | 20d96d8 | 2013-07-30 17:08:39 -0700 | [diff] [blame] | 34 | import android.database.Cursor; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 35 | import android.graphics.Bitmap; |
| 36 | import android.graphics.BitmapFactory; |
Jeff Sharkey | c1c8f3f | 2013-10-14 14:57:33 -0700 | [diff] [blame] | 37 | import android.graphics.Matrix; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 38 | import android.graphics.Point; |
Jeff Sharkey | c1c8f3f | 2013-10-14 14:57:33 -0700 | [diff] [blame] | 39 | import android.media.ExifInterface; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 40 | import android.net.Uri; |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 41 | import android.os.Build; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 42 | import android.os.Bundle; |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 43 | import android.os.CancellationSignal; |
Jeff Sharkey | 3381931 | 2013-10-29 11:56:37 -0700 | [diff] [blame] | 44 | import android.os.OperationCanceledException; |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 45 | import android.os.Parcel; |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 46 | import android.os.ParcelFileDescriptor; |
Jeff Sharkey | 67f9d50 | 2017-08-05 13:49:13 -0600 | [diff] [blame] | 47 | import android.os.ParcelFileDescriptor.OnCloseListener; |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 48 | import android.os.Parcelable; |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 49 | import android.os.ParcelableException; |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 50 | import android.os.RemoteException; |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 51 | import android.os.storage.StorageVolume; |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 52 | import android.system.ErrnoException; |
| 53 | import android.system.Os; |
Jeff Sharkey | 9f2dc05 | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 54 | import android.util.DataUnit; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 55 | import android.util.Log; |
| 56 | |
| 57 | import libcore.io.IoUtils; |
| 58 | |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 59 | import java.io.BufferedInputStream; |
Jeff Sharkey | c1c8f3f | 2013-10-14 14:57:33 -0700 | [diff] [blame] | 60 | import java.io.File; |
Jeff Sharkey | 9d0843d | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 61 | import java.io.FileDescriptor; |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 62 | import java.io.FileInputStream; |
Jeff Sharkey | c1c8f3f | 2013-10-14 14:57:33 -0700 | [diff] [blame] | 63 | import java.io.FileNotFoundException; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 64 | import java.io.IOException; |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 65 | import java.util.List; |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 66 | import java.util.Objects; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 69 | * Defines the contract between a documents provider and the platform. |
| 70 | * <p> |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 71 | * To create a document provider, extend {@link DocumentsProvider}, which |
| 72 | * provides a foundational implementation of this contract. |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 73 | * <p> |
| 74 | * All client apps must hold a valid URI permission grant to access documents, |
| 75 | * typically issued when a user makes a selection through |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 76 | * {@link Intent#ACTION_OPEN_DOCUMENT}, {@link Intent#ACTION_CREATE_DOCUMENT}, |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 77 | * {@link Intent#ACTION_OPEN_DOCUMENT_TREE}, or |
| 78 | * {@link StorageVolume#createAccessIntent(String) StorageVolume.createAccessIntent}. |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 79 | * |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 80 | * @see DocumentsProvider |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 81 | */ |
| 82 | public final class DocumentsContract { |
Steve McKay | d3afdee | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 83 | private static final String TAG = "DocumentsContract"; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 84 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 85 | // content://com.example/root/ |
| 86 | // content://com.example/root/sdcard/ |
| 87 | // content://com.example/root/sdcard/recent/ |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 88 | // content://com.example/root/sdcard/search/?query=pony |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 89 | // content://com.example/document/12/ |
| 90 | // content://com.example/document/12/children/ |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 91 | // content://com.example/tree/12/document/24/ |
| 92 | // content://com.example/tree/12/document/24/children/ |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 93 | |
| 94 | private DocumentsContract() { |
| 95 | } |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 96 | |
Jeff Sharkey | 85f5f81 | 2013-10-07 10:16:12 -0700 | [diff] [blame] | 97 | /** |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 98 | * Intent action used to identify {@link DocumentsProvider} instances. This |
| 99 | * is used in the {@code <intent-filter>} of a {@code <provider>}. |
Jeff Sharkey | 85f5f81 | 2013-10-07 10:16:12 -0700 | [diff] [blame] | 100 | */ |
| 101 | public static final String PROVIDER_INTERFACE = "android.content.action.DOCUMENTS_PROVIDER"; |
| 102 | |
Jeff Sharkey | 5b83f85 | 2013-08-14 18:29:19 -0700 | [diff] [blame] | 103 | /** {@hide} */ |
Jeff Sharkey | 15be836 | 2013-10-09 13:52:17 -0700 | [diff] [blame] | 104 | public static final String EXTRA_PACKAGE_NAME = "android.content.extra.PACKAGE_NAME"; |
| 105 | |
Jeff Sharkey | 96c6205 | 2013-10-25 16:30:54 -0700 | [diff] [blame] | 106 | /** {@hide} */ |
Aga Wronska | 1719b35 | 2016-03-21 11:28:03 -0700 | [diff] [blame] | 107 | public static final String EXTRA_SHOW_ADVANCED = "android.content.extra.SHOW_ADVANCED"; |
| 108 | |
| 109 | /** {@hide} */ |
Tomasz Mikolajewski | 74fe181 | 2015-06-12 17:13:26 -0700 | [diff] [blame] | 110 | public static final String EXTRA_TARGET_URI = "android.content.extra.TARGET_URI"; |
| 111 | |
Jeff Sharkey | c1c8f3f | 2013-10-14 14:57:33 -0700 | [diff] [blame] | 112 | /** |
Garfield Tan | b44ae61 | 2016-11-07 16:46:37 -0800 | [diff] [blame] | 113 | * Sets the desired initial location visible to user when file chooser is shown. |
| 114 | * |
| 115 | * <p>Applicable to {@link Intent} with actions: |
| 116 | * <ul> |
| 117 | * <li>{@link Intent#ACTION_OPEN_DOCUMENT}</li> |
| 118 | * <li>{@link Intent#ACTION_CREATE_DOCUMENT}</li> |
| 119 | * <li>{@link Intent#ACTION_OPEN_DOCUMENT_TREE}</li> |
| 120 | * </ul> |
| 121 | * |
| 122 | * <p>Location should specify a document URI or a tree URI with document ID. If |
| 123 | * this URI identifies a non-directory, document navigator will attempt to use the parent |
| 124 | * of the document as the initial location. |
Garfield Tan | 40d7b35 | 2017-03-02 15:30:30 -0800 | [diff] [blame] | 125 | * |
| 126 | * <p>The initial location is system specific if this extra is missing or document navigator |
| 127 | * failed to locate the desired initial location. |
Garfield Tan | b44ae61 | 2016-11-07 16:46:37 -0800 | [diff] [blame] | 128 | */ |
| 129 | public static final String EXTRA_INITIAL_URI = "android.provider.extra.INITIAL_URI"; |
| 130 | |
| 131 | /** |
Ben Kwa | 7779740 | 2015-05-29 15:40:31 -0700 | [diff] [blame] | 132 | * Set this in a DocumentsUI intent to cause a package's own roots to be |
| 133 | * excluded from the roots list. |
| 134 | */ |
| 135 | public static final String EXTRA_EXCLUDE_SELF = "android.provider.extra.EXCLUDE_SELF"; |
| 136 | |
| 137 | /** |
Jeff Sharkey | c1c8f3f | 2013-10-14 14:57:33 -0700 | [diff] [blame] | 138 | * Included in {@link AssetFileDescriptor#getExtras()} when returned |
| 139 | * thumbnail should be rotated. |
| 140 | * |
| 141 | * @see MediaStore.Images.ImageColumns#ORIENTATION |
Jeff Sharkey | c1c8f3f | 2013-10-14 14:57:33 -0700 | [diff] [blame] | 142 | */ |
Tomasz Mikolajewski | 5f53f65 | 2016-03-31 09:34:51 +0900 | [diff] [blame] | 143 | public static final String EXTRA_ORIENTATION = "android.provider.extra.ORIENTATION"; |
Jeff Sharkey | c1c8f3f | 2013-10-14 14:57:33 -0700 | [diff] [blame] | 144 | |
Tomasz Mikolajewski | 0e591f9 | 2015-06-12 16:22:17 -0700 | [diff] [blame] | 145 | /** |
| 146 | * Overrides the default prompt text in DocumentsUI when set in an intent. |
| 147 | */ |
| 148 | public static final String EXTRA_PROMPT = "android.provider.extra.PROMPT"; |
| 149 | |
Ben Lin | bd036d8 | 2016-12-15 15:41:08 -0800 | [diff] [blame] | 150 | /** |
| 151 | * Action of intent issued by DocumentsUI when user wishes to open/configure/manage a particular |
| 152 | * document in the provider application. |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 153 | * |
Ben Lin | bd036d8 | 2016-12-15 15:41:08 -0800 | [diff] [blame] | 154 | * <p>When issued, the intent will include the URI of the document as the intent data. |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 155 | * |
Ben Lin | bd036d8 | 2016-12-15 15:41:08 -0800 | [diff] [blame] | 156 | * <p>A provider wishing to provide support for this action should do two things. |
| 157 | * <li>Add an {@code <intent-filter>} matching this action. |
| 158 | * <li>When supplying information in {@link DocumentsProvider#queryChildDocuments}, include |
| 159 | * {@link Document#FLAG_SUPPORTS_SETTINGS} in the flags for each document that supports |
| 160 | * settings. |
| 161 | * |
| 162 | * @see DocumentsContact#Document#FLAG_SUPPORTS_SETTINGS |
| 163 | */ |
| 164 | public static final String |
| 165 | ACTION_DOCUMENT_SETTINGS = "android.provider.action.DOCUMENT_SETTINGS"; |
| 166 | |
Jeff Sharkey | 15be836 | 2013-10-09 13:52:17 -0700 | [diff] [blame] | 167 | /** {@hide} */ |
Jeff Sharkey | a61dc8e | 2013-09-05 17:14:14 -0700 | [diff] [blame] | 168 | public static final String ACTION_MANAGE_DOCUMENT = "android.provider.action.MANAGE_DOCUMENT"; |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 169 | |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 170 | /** {@hide} */ |
Jeff Sharkey | 1407d4c | 2015-04-12 21:52:24 -0700 | [diff] [blame] | 171 | public static final String |
| 172 | ACTION_DOCUMENT_ROOT_SETTINGS = "android.provider.action.DOCUMENT_ROOT_SETTINGS"; |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 173 | |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 174 | /** |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 175 | * Buffer is large enough to rewind past any EXIF headers. |
| 176 | */ |
Jeff Sharkey | 9f2dc05 | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 177 | private static final int THUMBNAIL_BUFFER_SIZE = (int) DataUnit.KIBIBYTES.toBytes(128); |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 178 | |
Jeff Sharkey | cc2ae6b4 | 2015-09-29 13:04:46 -0700 | [diff] [blame] | 179 | /** {@hide} */ |
Garfield Tan | 92b96ba | 2016-11-01 14:33:48 -0700 | [diff] [blame] | 180 | public static final String EXTERNAL_STORAGE_PROVIDER_AUTHORITY = |
| 181 | "com.android.externalstorage.documents"; |
| 182 | |
| 183 | /** {@hide} */ |
Jeff Sharkey | cc2ae6b4 | 2015-09-29 13:04:46 -0700 | [diff] [blame] | 184 | public static final String PACKAGE_DOCUMENTS_UI = "com.android.documentsui"; |
| 185 | |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 186 | /** {@hide} */ |
| 187 | public static final String METADATA_TYPES = "android:documentMetadataType"; |
| 188 | |
| 189 | /** {@hide} */ |
| 190 | public static final String METADATA_EXIF = "android:documentExif"; |
| 191 | |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 192 | /** |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 193 | * Constants related to a document, including {@link Cursor} column names |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 194 | * and flags. |
| 195 | * <p> |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 196 | * A document can be either an openable stream (with a specific MIME type), |
| 197 | * or a directory containing additional documents (with the |
| 198 | * {@link #MIME_TYPE_DIR} MIME type). A directory represents the top of a |
| 199 | * subtree containing zero or more documents, which can recursively contain |
| 200 | * even more documents and directories. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 201 | * <p> |
| 202 | * All columns are <em>read-only</em> to client applications. |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 203 | */ |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 204 | public final static class Document { |
| 205 | private Document() { |
Jeff Sharkey | a5599ef | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 206 | } |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 207 | |
Jeff Sharkey | a5599ef | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 208 | /** |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 209 | * Unique ID of a document. This ID is both provided by and interpreted |
| 210 | * by a {@link DocumentsProvider}, and should be treated as an opaque |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 211 | * value by client applications. This column is required. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 212 | * <p> |
| 213 | * Each document must have a unique ID within a provider, but that |
| 214 | * single document may be included as a child of multiple directories. |
| 215 | * <p> |
| 216 | * A provider must always return durable IDs, since they will be used to |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 217 | * issue long-term URI permission grants when an application interacts |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 218 | * with {@link Intent#ACTION_OPEN_DOCUMENT} and |
| 219 | * {@link Intent#ACTION_CREATE_DOCUMENT}. |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 220 | * <p> |
| 221 | * Type: STRING |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 222 | */ |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 223 | public static final String COLUMN_DOCUMENT_ID = "document_id"; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 224 | |
| 225 | /** |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 226 | * Concrete MIME type of a document. For example, "image/png" or |
| 227 | * "application/pdf" for openable files. A document can also be a |
| 228 | * directory containing additional documents, which is represented with |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 229 | * the {@link #MIME_TYPE_DIR} MIME type. This column is required. |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 230 | * <p> |
| 231 | * Type: STRING |
| 232 | * |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 233 | * @see #MIME_TYPE_DIR |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 234 | */ |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 235 | public static final String COLUMN_MIME_TYPE = "mime_type"; |
| 236 | |
| 237 | /** |
| 238 | * Display name of a document, used as the primary title displayed to a |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 239 | * user. This column is required. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 240 | * <p> |
| 241 | * Type: STRING |
| 242 | */ |
| 243 | public static final String COLUMN_DISPLAY_NAME = OpenableColumns.DISPLAY_NAME; |
| 244 | |
| 245 | /** |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 246 | * Summary of a document, which may be shown to a user. This column is |
| 247 | * optional, and may be {@code null}. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 248 | * <p> |
| 249 | * Type: STRING |
| 250 | */ |
| 251 | public static final String COLUMN_SUMMARY = "summary"; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 252 | |
| 253 | /** |
| 254 | * Timestamp when a document was last modified, in milliseconds since |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 255 | * January 1, 1970 00:00:00.0 UTC. This column is required, and may be |
| 256 | * {@code null} if unknown. A {@link DocumentsProvider} can update this |
| 257 | * field using events from {@link OnCloseListener} or other reliable |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 258 | * {@link ParcelFileDescriptor} transports. |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 259 | * <p> |
| 260 | * Type: INTEGER (long) |
| 261 | * |
| 262 | * @see System#currentTimeMillis() |
| 263 | */ |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 264 | public static final String COLUMN_LAST_MODIFIED = "last_modified"; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 265 | |
| 266 | /** |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 267 | * Specific icon resource ID for a document. This column is optional, |
| 268 | * and may be {@code null} to use a platform-provided default icon based |
| 269 | * on {@link #COLUMN_MIME_TYPE}. |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 270 | * <p> |
| 271 | * Type: INTEGER (int) |
| 272 | */ |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 273 | public static final String COLUMN_ICON = "icon"; |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 274 | |
| 275 | /** |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 276 | * Flags that apply to a document. This column is required. |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 277 | * <p> |
| 278 | * Type: INTEGER (int) |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 279 | * |
| 280 | * @see #FLAG_SUPPORTS_WRITE |
| 281 | * @see #FLAG_SUPPORTS_DELETE |
| 282 | * @see #FLAG_SUPPORTS_THUMBNAIL |
| 283 | * @see #FLAG_DIR_PREFERS_GRID |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 284 | * @see #FLAG_DIR_PREFERS_LAST_MODIFIED |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 285 | * @see #FLAG_VIRTUAL_DOCUMENT |
Tomasz Mikolajewski | cbcd394 | 2016-01-28 12:39:25 +0900 | [diff] [blame] | 286 | * @see #FLAG_SUPPORTS_COPY |
| 287 | * @see #FLAG_SUPPORTS_MOVE |
| 288 | * @see #FLAG_SUPPORTS_REMOVE |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 289 | */ |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 290 | public static final String COLUMN_FLAGS = "flags"; |
| 291 | |
| 292 | /** |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 293 | * Size of a document, in bytes, or {@code null} if unknown. This column |
| 294 | * is required. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 295 | * <p> |
| 296 | * Type: INTEGER (long) |
| 297 | */ |
| 298 | public static final String COLUMN_SIZE = OpenableColumns.SIZE; |
| 299 | |
| 300 | /** |
| 301 | * MIME type of a document which is a directory that may contain |
| 302 | * additional documents. |
| 303 | * |
| 304 | * @see #COLUMN_MIME_TYPE |
| 305 | */ |
| 306 | public static final String MIME_TYPE_DIR = "vnd.android.document/directory"; |
| 307 | |
| 308 | /** |
| 309 | * Flag indicating that a document can be represented as a thumbnail. |
| 310 | * |
| 311 | * @see #COLUMN_FLAGS |
| 312 | * @see DocumentsContract#getDocumentThumbnail(ContentResolver, Uri, |
| 313 | * Point, CancellationSignal) |
| 314 | * @see DocumentsProvider#openDocumentThumbnail(String, Point, |
| 315 | * android.os.CancellationSignal) |
| 316 | */ |
| 317 | public static final int FLAG_SUPPORTS_THUMBNAIL = 1; |
| 318 | |
| 319 | /** |
| 320 | * Flag indicating that a document supports writing. |
| 321 | * <p> |
| 322 | * When a document is opened with {@link Intent#ACTION_OPEN_DOCUMENT}, |
| 323 | * the calling application is granted both |
| 324 | * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION} and |
| 325 | * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION}. However, the actual |
| 326 | * writability of a document may change over time, for example due to |
| 327 | * remote access changes. This flag indicates that a document client can |
| 328 | * expect {@link ContentResolver#openOutputStream(Uri)} to succeed. |
Steve McKay | 83df8c0 | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 329 | * |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 330 | * @see #COLUMN_FLAGS |
| 331 | */ |
| 332 | public static final int FLAG_SUPPORTS_WRITE = 1 << 1; |
| 333 | |
| 334 | /** |
| 335 | * Flag indicating that a document is deletable. |
| 336 | * |
| 337 | * @see #COLUMN_FLAGS |
| 338 | * @see DocumentsContract#deleteDocument(ContentResolver, Uri) |
| 339 | * @see DocumentsProvider#deleteDocument(String) |
| 340 | */ |
| 341 | public static final int FLAG_SUPPORTS_DELETE = 1 << 2; |
| 342 | |
| 343 | /** |
| 344 | * Flag indicating that a document is a directory that supports creation |
| 345 | * of new files within it. Only valid when {@link #COLUMN_MIME_TYPE} is |
| 346 | * {@link #MIME_TYPE_DIR}. |
| 347 | * |
| 348 | * @see #COLUMN_FLAGS |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 349 | * @see DocumentsProvider#createDocument(String, String, String) |
| 350 | */ |
| 351 | public static final int FLAG_DIR_SUPPORTS_CREATE = 1 << 3; |
| 352 | |
| 353 | /** |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 354 | * Flag indicating that a directory prefers its contents be shown in a |
| 355 | * larger format grid. Usually suitable when a directory contains mostly |
| 356 | * pictures. Only valid when {@link #COLUMN_MIME_TYPE} is |
| 357 | * {@link #MIME_TYPE_DIR}. |
| 358 | * |
| 359 | * @see #COLUMN_FLAGS |
| 360 | */ |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 361 | public static final int FLAG_DIR_PREFERS_GRID = 1 << 4; |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 362 | |
| 363 | /** |
| 364 | * Flag indicating that a directory prefers its contents be sorted by |
| 365 | * {@link #COLUMN_LAST_MODIFIED}. Only valid when |
| 366 | * {@link #COLUMN_MIME_TYPE} is {@link #MIME_TYPE_DIR}. |
| 367 | * |
| 368 | * @see #COLUMN_FLAGS |
| 369 | */ |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 370 | public static final int FLAG_DIR_PREFERS_LAST_MODIFIED = 1 << 5; |
Jeff Sharkey | f6db154 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 371 | |
| 372 | /** |
Jeff Sharkey | b7e1255 | 2014-05-21 22:22:03 -0700 | [diff] [blame] | 373 | * Flag indicating that a document can be renamed. |
| 374 | * |
| 375 | * @see #COLUMN_FLAGS |
Tomasz Mikolajewski | 90a7533 | 2016-07-13 10:14:59 +0900 | [diff] [blame] | 376 | * @see DocumentsContract#renameDocument(ContentResolver, Uri, |
Jeff Sharkey | b7e1255 | 2014-05-21 22:22:03 -0700 | [diff] [blame] | 377 | * String) |
| 378 | * @see DocumentsProvider#renameDocument(String, String) |
| 379 | */ |
| 380 | public static final int FLAG_SUPPORTS_RENAME = 1 << 6; |
| 381 | |
| 382 | /** |
Tomasz Mikolajewski | 74fe181 | 2015-06-12 17:13:26 -0700 | [diff] [blame] | 383 | * Flag indicating that a document can be copied to another location |
| 384 | * within the same document provider. |
| 385 | * |
| 386 | * @see #COLUMN_FLAGS |
Tomasz Mikolajewski | 90a7533 | 2016-07-13 10:14:59 +0900 | [diff] [blame] | 387 | * @see DocumentsContract#copyDocument(ContentResolver, Uri, Uri) |
Tomasz Mikolajewski | a375a99 | 2015-06-25 15:39:27 +0900 | [diff] [blame] | 388 | * @see DocumentsProvider#copyDocument(String, String) |
Tomasz Mikolajewski | 74fe181 | 2015-06-12 17:13:26 -0700 | [diff] [blame] | 389 | */ |
| 390 | public static final int FLAG_SUPPORTS_COPY = 1 << 7; |
| 391 | |
| 392 | /** |
Tomasz Mikolajewski | a375a99 | 2015-06-25 15:39:27 +0900 | [diff] [blame] | 393 | * Flag indicating that a document can be moved to another location |
| 394 | * within the same document provider. |
| 395 | * |
| 396 | * @see #COLUMN_FLAGS |
Tomasz Mikolajewski | 90a7533 | 2016-07-13 10:14:59 +0900 | [diff] [blame] | 397 | * @see DocumentsContract#moveDocument(ContentResolver, Uri, Uri, Uri) |
Tomasz Mikolajewski | d46ecbc | 2016-01-25 14:26:54 +0900 | [diff] [blame] | 398 | * @see DocumentsProvider#moveDocument(String, String, String) |
Tomasz Mikolajewski | a375a99 | 2015-06-25 15:39:27 +0900 | [diff] [blame] | 399 | */ |
| 400 | public static final int FLAG_SUPPORTS_MOVE = 1 << 8; |
| 401 | |
| 402 | /** |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 403 | * Flag indicating that a document is virtual, and doesn't have byte |
| 404 | * representation in the MIME type specified as {@link #COLUMN_MIME_TYPE}. |
| 405 | * |
Tomasz Mikolajewski | 099f951 | 2016-12-09 10:19:46 +0900 | [diff] [blame] | 406 | * <p><em>Virtual documents must have at least one alternative streamable |
| 407 | * format via {@link DocumentsProvider#openTypedDocument}</em> |
| 408 | * |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 409 | * @see #COLUMN_FLAGS |
| 410 | * @see #COLUMN_MIME_TYPE |
| 411 | * @see DocumentsProvider#openTypedDocument(String, String, Bundle, |
| 412 | * android.os.CancellationSignal) |
Tomasz Mikolajewski | d99964f | 2016-02-15 11:16:32 +0900 | [diff] [blame] | 413 | * @see DocumentsProvider#getDocumentStreamTypes(String, String) |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 414 | */ |
Tomasz Mikolajewski | 7539565 | 2016-01-07 07:19:22 +0000 | [diff] [blame] | 415 | public static final int FLAG_VIRTUAL_DOCUMENT = 1 << 9; |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 416 | |
| 417 | /** |
Tomasz Mikolajewski | 9b055e1 | 2016-02-01 13:01:34 +0900 | [diff] [blame] | 418 | * Flag indicating that a document can be removed from a parent. |
| 419 | * |
| 420 | * @see #COLUMN_FLAGS |
Tomasz Mikolajewski | 90a7533 | 2016-07-13 10:14:59 +0900 | [diff] [blame] | 421 | * @see DocumentsContract#removeDocument(ContentResolver, Uri, Uri) |
Tomasz Mikolajewski | 9b055e1 | 2016-02-01 13:01:34 +0900 | [diff] [blame] | 422 | * @see DocumentsProvider#removeDocument(String, String) |
| 423 | */ |
| 424 | public static final int FLAG_SUPPORTS_REMOVE = 1 << 10; |
| 425 | |
| 426 | /** |
Ben Lin | bd036d8 | 2016-12-15 15:41:08 -0800 | [diff] [blame] | 427 | * Flag indicating that a document has settings that can be configured by user. |
| 428 | * |
| 429 | * @see #COLUMN_FLAGS |
| 430 | * @see #ACTION_DOCUMENT_SETTINGS |
| 431 | */ |
| 432 | public static final int FLAG_SUPPORTS_SETTINGS = 1 << 11; |
| 433 | |
| 434 | /** |
Tomasz Mikolajewski | cf31656 | 2016-10-24 15:17:01 +0900 | [diff] [blame] | 435 | * Flag indicating that a Web link can be obtained for the document. |
| 436 | * |
| 437 | * @see #COLUMN_FLAGS |
| 438 | * @see DocumentsContract#createWebLinkIntent(PackageManager, Uri, Bundle) |
| 439 | */ |
| 440 | public static final int FLAG_WEB_LINKABLE = 1 << 12; |
| 441 | |
| 442 | /** |
Steve McKay | 168e464 | 2016-03-14 13:02:56 -0700 | [diff] [blame] | 443 | * Flag indicating that a document is not complete, likely its |
| 444 | * contents are being downloaded. Partial files cannot be opened, |
| 445 | * copied, moved in the UI. But they can be deleted and retried |
| 446 | * if they represent a failed download. |
| 447 | * |
| 448 | * @see #COLUMN_FLAGS |
| 449 | * @hide |
| 450 | */ |
| 451 | public static final int FLAG_PARTIAL = 1 << 16; |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 452 | |
| 453 | /** |
| 454 | * Flag indicating that a document has available metadata that can be read |
| 455 | * using DocumentsContract#getDocumentMetadata |
| 456 | * @hide |
| 457 | */ |
| 458 | public static final int FLAG_SUPPORTS_METADATA = 1 << 17; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 461 | /** |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 462 | * Constants related to a root of documents, including {@link Cursor} column |
| 463 | * names and flags. A root is the start of a tree of documents, such as a |
| 464 | * physical storage device, or an account. Each root starts at the directory |
| 465 | * referenced by {@link Root#COLUMN_DOCUMENT_ID}, which can recursively |
| 466 | * contain both documents and directories. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 467 | * <p> |
| 468 | * All columns are <em>read-only</em> to client applications. |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 469 | */ |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 470 | public final static class Root { |
| 471 | private Root() { |
| 472 | } |
| 473 | |
Jeff Sharkey | a5599ef | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 474 | /** |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 475 | * Unique ID of a root. This ID is both provided by and interpreted by a |
| 476 | * {@link DocumentsProvider}, and should be treated as an opaque value |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 477 | * by client applications. This column is required. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 478 | * <p> |
| 479 | * Type: STRING |
| 480 | */ |
| 481 | public static final String COLUMN_ROOT_ID = "root_id"; |
| 482 | |
| 483 | /** |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 484 | * Flags that apply to a root. This column is required. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 485 | * <p> |
| 486 | * Type: INTEGER (int) |
| 487 | * |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 488 | * @see #FLAG_LOCAL_ONLY |
| 489 | * @see #FLAG_SUPPORTS_CREATE |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 490 | * @see #FLAG_SUPPORTS_RECENTS |
| 491 | * @see #FLAG_SUPPORTS_SEARCH |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 492 | */ |
| 493 | public static final String COLUMN_FLAGS = "flags"; |
| 494 | |
| 495 | /** |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 496 | * Icon resource ID for a root. This column is required. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 497 | * <p> |
| 498 | * Type: INTEGER (int) |
| 499 | */ |
| 500 | public static final String COLUMN_ICON = "icon"; |
| 501 | |
| 502 | /** |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 503 | * Title for a root, which will be shown to a user. This column is |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 504 | * required. For a single storage service surfacing multiple accounts as |
| 505 | * different roots, this title should be the name of the service. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 506 | * <p> |
| 507 | * Type: STRING |
| 508 | */ |
| 509 | public static final String COLUMN_TITLE = "title"; |
| 510 | |
| 511 | /** |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 512 | * Summary for this root, which may be shown to a user. This column is |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 513 | * optional, and may be {@code null}. For a single storage service |
| 514 | * surfacing multiple accounts as different roots, this summary should |
| 515 | * be the name of the account. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 516 | * <p> |
| 517 | * Type: STRING |
| 518 | */ |
| 519 | public static final String COLUMN_SUMMARY = "summary"; |
| 520 | |
| 521 | /** |
| 522 | * Document which is a directory that represents the top directory of |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 523 | * this root. This column is required. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 524 | * <p> |
| 525 | * Type: STRING |
| 526 | * |
| 527 | * @see Document#COLUMN_DOCUMENT_ID |
| 528 | */ |
| 529 | public static final String COLUMN_DOCUMENT_ID = "document_id"; |
| 530 | |
| 531 | /** |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 532 | * Number of bytes available in this root. This column is optional, and |
| 533 | * may be {@code null} if unknown or unbounded. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 534 | * <p> |
| 535 | * Type: INTEGER (long) |
| 536 | */ |
| 537 | public static final String COLUMN_AVAILABLE_BYTES = "available_bytes"; |
| 538 | |
| 539 | /** |
Tomasz Mikolajewski | 3f78e17 | 2015-06-25 16:17:26 +0900 | [diff] [blame] | 540 | * Capacity of a root in bytes. This column is optional, and may be |
| 541 | * {@code null} if unknown or unbounded. |
Tomasz Mikolajewski | 3f78e17 | 2015-06-25 16:17:26 +0900 | [diff] [blame] | 542 | * <p> |
| 543 | * Type: INTEGER (long) |
| 544 | */ |
| 545 | public static final String COLUMN_CAPACITY_BYTES = "capacity_bytes"; |
| 546 | |
| 547 | /** |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 548 | * MIME types supported by this root. This column is optional, and if |
| 549 | * {@code null} the root is assumed to support all MIME types. Multiple |
| 550 | * MIME types can be separated by a newline. For example, a root |
| 551 | * supporting audio might return "audio/*\napplication/x-flac". |
Jeff Sharkey | 923396b | 2013-09-05 13:55:35 -0700 | [diff] [blame] | 552 | * <p> |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 553 | * Type: STRING |
Jeff Sharkey | 923396b | 2013-09-05 13:55:35 -0700 | [diff] [blame] | 554 | */ |
| 555 | public static final String COLUMN_MIME_TYPES = "mime_types"; |
| 556 | |
Garfield Tan | a7e852e | 2017-02-21 12:34:08 -0800 | [diff] [blame] | 557 | /** |
| 558 | * MIME type for a root. |
| 559 | */ |
Jeff Sharkey | a61dc8e | 2013-09-05 17:14:14 -0700 | [diff] [blame] | 560 | public static final String MIME_TYPE_ITEM = "vnd.android.document/root"; |
| 561 | |
Jeff Sharkey | 923396b | 2013-09-05 13:55:35 -0700 | [diff] [blame] | 562 | /** |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 563 | * Flag indicating that at least one directory under this root supports |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 564 | * creating content. Roots with this flag will be shown when an |
| 565 | * application interacts with {@link Intent#ACTION_CREATE_DOCUMENT}. |
Jeff Sharkey | 20d96d8 | 2013-07-30 17:08:39 -0700 | [diff] [blame] | 566 | * |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 567 | * @see #COLUMN_FLAGS |
Jeff Sharkey | 20d96d8 | 2013-07-30 17:08:39 -0700 | [diff] [blame] | 568 | */ |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 569 | public static final int FLAG_SUPPORTS_CREATE = 1; |
Jeff Sharkey | 20d96d8 | 2013-07-30 17:08:39 -0700 | [diff] [blame] | 570 | |
| 571 | /** |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 572 | * Flag indicating that this root offers content that is strictly local |
| 573 | * on the device. That is, no network requests are made for the content. |
| 574 | * |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 575 | * @see #COLUMN_FLAGS |
| 576 | * @see Intent#EXTRA_LOCAL_ONLY |
Jeff Sharkey | 20d96d8 | 2013-07-30 17:08:39 -0700 | [diff] [blame] | 577 | */ |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 578 | public static final int FLAG_LOCAL_ONLY = 1 << 1; |
| 579 | |
Jeff Sharkey | 20d96d8 | 2013-07-30 17:08:39 -0700 | [diff] [blame] | 580 | /** |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 581 | * Flag indicating that this root can be queried to provide recently |
| 582 | * modified documents. |
Jeff Sharkey | 251097b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 583 | * |
| 584 | * @see #COLUMN_FLAGS |
| 585 | * @see DocumentsContract#buildRecentDocumentsUri(String, String) |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 586 | * @see DocumentsProvider#queryRecentDocuments(String, String[]) |
Jeff Sharkey | 251097b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 587 | */ |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 588 | public static final int FLAG_SUPPORTS_RECENTS = 1 << 2; |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 589 | |
| 590 | /** |
| 591 | * Flag indicating that this root supports search. |
| 592 | * |
| 593 | * @see #COLUMN_FLAGS |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 594 | * @see DocumentsContract#buildSearchDocumentsUri(String, String, |
| 595 | * String) |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 596 | * @see DocumentsProvider#querySearchDocuments(String, String, |
| 597 | * String[]) |
| 598 | */ |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 599 | public static final int FLAG_SUPPORTS_SEARCH = 1 << 3; |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 600 | |
| 601 | /** |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 602 | * Flag indicating that this root supports testing parent child |
| 603 | * relationships. |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 604 | * |
| 605 | * @see #COLUMN_FLAGS |
| 606 | * @see DocumentsProvider#isChildDocument(String, String) |
| 607 | */ |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 608 | public static final int FLAG_SUPPORTS_IS_CHILD = 1 << 4; |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 609 | |
| 610 | /** |
Garfield Tan | 8787703 | 2017-03-22 12:01:14 -0700 | [diff] [blame] | 611 | * Flag indicating that this root can be ejected. |
| 612 | * |
| 613 | * @see #COLUMN_FLAGS |
| 614 | * @see DocumentsContract#ejectRoot(ContentResolver, Uri) |
| 615 | * @see DocumentsProvider#ejectRoot(String) |
| 616 | */ |
| 617 | public static final int FLAG_SUPPORTS_EJECT = 1 << 5; |
| 618 | |
| 619 | /** |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 620 | * Flag indicating that this root is currently empty. This may be used |
| 621 | * to hide the root when opening documents, but the root will still be |
| 622 | * shown when creating documents and {@link #FLAG_SUPPORTS_CREATE} is |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 623 | * also set. If the value of this flag changes, such as when a root |
| 624 | * becomes non-empty, you must send a content changed notification for |
| 625 | * {@link DocumentsContract#buildRootsUri(String)}. |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 626 | * |
| 627 | * @see #COLUMN_FLAGS |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 628 | * @see ContentResolver#notifyChange(Uri, |
| 629 | * android.database.ContentObserver, boolean) |
| 630 | * @hide |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 631 | */ |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 632 | public static final int FLAG_EMPTY = 1 << 16; |
| 633 | |
| 634 | /** |
Aga Wronska | 1719b35 | 2016-03-21 11:28:03 -0700 | [diff] [blame] | 635 | * Flag indicating that this root should only be visible to advanced |
| 636 | * users. |
| 637 | * |
| 638 | * @see #COLUMN_FLAGS |
| 639 | * @hide |
| 640 | */ |
Mathew Inwood | ba50311 | 2018-08-10 09:37:35 +0100 | [diff] [blame] | 641 | @UnsupportedAppUsage |
Aga Wronska | 1719b35 | 2016-03-21 11:28:03 -0700 | [diff] [blame] | 642 | public static final int FLAG_ADVANCED = 1 << 17; |
| 643 | |
| 644 | /** |
Jeff Sharkey | 1407d4c | 2015-04-12 21:52:24 -0700 | [diff] [blame] | 645 | * Flag indicating that this root has settings. |
| 646 | * |
| 647 | * @see #COLUMN_FLAGS |
| 648 | * @see DocumentsContract#ACTION_DOCUMENT_ROOT_SETTINGS |
| 649 | * @hide |
| 650 | */ |
Aga Wronska | 1719b35 | 2016-03-21 11:28:03 -0700 | [diff] [blame] | 651 | public static final int FLAG_HAS_SETTINGS = 1 << 18; |
Steve McKay | ba23e54 | 2016-03-02 15:15:00 -0800 | [diff] [blame] | 652 | |
| 653 | /** |
| 654 | * Flag indicating that this root is on removable SD card storage. |
| 655 | * |
| 656 | * @see #COLUMN_FLAGS |
| 657 | * @hide |
| 658 | */ |
Aga Wronska | 1719b35 | 2016-03-21 11:28:03 -0700 | [diff] [blame] | 659 | public static final int FLAG_REMOVABLE_SD = 1 << 19; |
Steve McKay | ba23e54 | 2016-03-02 15:15:00 -0800 | [diff] [blame] | 660 | |
| 661 | /** |
| 662 | * Flag indicating that this root is on removable USB storage. |
| 663 | * |
| 664 | * @see #COLUMN_FLAGS |
| 665 | * @hide |
| 666 | */ |
Aga Wronska | 1719b35 | 2016-03-21 11:28:03 -0700 | [diff] [blame] | 667 | public static final int FLAG_REMOVABLE_USB = 1 << 20; |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 668 | } |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 669 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 670 | /** |
| 671 | * Optional boolean flag included in a directory {@link Cursor#getExtras()} |
| 672 | * indicating that a document provider is still loading data. For example, a |
| 673 | * provider has returned some results, but is still waiting on an |
| 674 | * outstanding network request. The provider must send a content changed |
| 675 | * notification when loading is finished. |
| 676 | * |
| 677 | * @see ContentResolver#notifyChange(Uri, android.database.ContentObserver, |
| 678 | * boolean) |
| 679 | */ |
| 680 | public static final String EXTRA_LOADING = "loading"; |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 681 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 682 | /** |
| 683 | * Optional string included in a directory {@link Cursor#getExtras()} |
| 684 | * providing an informational message that should be shown to a user. For |
| 685 | * example, a provider may wish to indicate that not all documents are |
| 686 | * available. |
| 687 | */ |
| 688 | public static final String EXTRA_INFO = "info"; |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 689 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 690 | /** |
| 691 | * Optional string included in a directory {@link Cursor#getExtras()} |
| 692 | * providing an error message that should be shown to a user. For example, a |
| 693 | * provider may wish to indicate that a network error occurred. The user may |
| 694 | * choose to retry, resulting in a new query. |
| 695 | */ |
| 696 | public static final String EXTRA_ERROR = "error"; |
| 697 | |
Steve McKay | d3afdee | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 698 | /** |
| 699 | * Optional result (I'm thinking boolean) answer to a question. |
| 700 | * {@hide} |
| 701 | */ |
| 702 | public static final String EXTRA_RESULT = "result"; |
| 703 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 704 | /** {@hide} */ |
Mathew Inwood | ba50311 | 2018-08-10 09:37:35 +0100 | [diff] [blame] | 705 | @UnsupportedAppUsage |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 706 | public static final String METHOD_CREATE_DOCUMENT = "android:createDocument"; |
| 707 | /** {@hide} */ |
Jeff Sharkey | b7e1255 | 2014-05-21 22:22:03 -0700 | [diff] [blame] | 708 | public static final String METHOD_RENAME_DOCUMENT = "android:renameDocument"; |
| 709 | /** {@hide} */ |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 710 | public static final String METHOD_DELETE_DOCUMENT = "android:deleteDocument"; |
Tomasz Mikolajewski | 74fe181 | 2015-06-12 17:13:26 -0700 | [diff] [blame] | 711 | /** {@hide} */ |
| 712 | public static final String METHOD_COPY_DOCUMENT = "android:copyDocument"; |
Tomasz Mikolajewski | a375a99 | 2015-06-25 15:39:27 +0900 | [diff] [blame] | 713 | /** {@hide} */ |
| 714 | public static final String METHOD_MOVE_DOCUMENT = "android:moveDocument"; |
Steve McKay | d3afdee | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 715 | /** {@hide} */ |
| 716 | public static final String METHOD_IS_CHILD_DOCUMENT = "android:isChildDocument"; |
Tomasz Mikolajewski | cbcd394 | 2016-01-28 12:39:25 +0900 | [diff] [blame] | 717 | /** {@hide} */ |
| 718 | public static final String METHOD_REMOVE_DOCUMENT = "android:removeDocument"; |
Ben Lin | e7822fb | 2016-06-24 15:21:08 -0700 | [diff] [blame] | 719 | /** {@hide} */ |
| 720 | public static final String METHOD_EJECT_ROOT = "android:ejectRoot"; |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 721 | /** {@hide} */ |
Garfield Tan | 3f6b68a | 2016-11-01 14:13:38 -0700 | [diff] [blame] | 722 | public static final String METHOD_FIND_DOCUMENT_PATH = "android:findDocumentPath"; |
Tomasz Mikolajewski | cf31656 | 2016-10-24 15:17:01 +0900 | [diff] [blame] | 723 | /** {@hide} */ |
| 724 | public static final String METHOD_CREATE_WEB_LINK_INTENT = "android:createWebLinkIntent"; |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 725 | /** {@hide} */ |
| 726 | public static final String METHOD_GET_DOCUMENT_METADATA = "android:getDocumentMetadata"; |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 727 | |
| 728 | /** {@hide} */ |
Tomasz Mikolajewski | d46ecbc | 2016-01-25 14:26:54 +0900 | [diff] [blame] | 729 | public static final String EXTRA_PARENT_URI = "parentUri"; |
| 730 | /** {@hide} */ |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 731 | public static final String EXTRA_URI = "uri"; |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 732 | |
Tomasz Mikolajewski | cf31656 | 2016-10-24 15:17:01 +0900 | [diff] [blame] | 733 | /** |
| 734 | * @see #createWebLinkIntent(ContentResolver, Uri, Bundle) |
| 735 | * {@hide} |
| 736 | */ |
| 737 | public static final String EXTRA_OPTIONS = "options"; |
| 738 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 739 | private static final String PATH_ROOT = "root"; |
| 740 | private static final String PATH_RECENT = "recent"; |
Mathew Inwood | ba50311 | 2018-08-10 09:37:35 +0100 | [diff] [blame] | 741 | @UnsupportedAppUsage |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 742 | private static final String PATH_DOCUMENT = "document"; |
| 743 | private static final String PATH_CHILDREN = "children"; |
| 744 | private static final String PATH_SEARCH = "search"; |
Felipe Leme | 23a0c7a | 2018-01-24 08:43:34 -0800 | [diff] [blame] | 745 | // TODO(b/72055774): make private again once ScopedAccessProvider is refactored |
| 746 | /** {@hide} */ |
Mathew Inwood | ba50311 | 2018-08-10 09:37:35 +0100 | [diff] [blame] | 747 | @UnsupportedAppUsage |
Felipe Leme | 23a0c7a | 2018-01-24 08:43:34 -0800 | [diff] [blame] | 748 | public static final String PATH_TREE = "tree"; |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 749 | |
| 750 | private static final String PARAM_QUERY = "query"; |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 751 | private static final String PARAM_MANAGE = "manage"; |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 752 | |
| 753 | /** |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 754 | * Build URI representing the roots of a document provider. When queried, a |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 755 | * provider will return one or more rows with columns defined by |
| 756 | * {@link Root}. |
| 757 | * |
| 758 | * @see DocumentsProvider#queryRoots(String[]) |
| 759 | */ |
| 760 | public static Uri buildRootsUri(String authority) { |
| 761 | return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 762 | .authority(authority).appendPath(PATH_ROOT).build(); |
| 763 | } |
| 764 | |
| 765 | /** |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 766 | * Build URI representing the given {@link Root#COLUMN_ROOT_ID} in a |
Jeff Sharkey | a61dc8e | 2013-09-05 17:14:14 -0700 | [diff] [blame] | 767 | * document provider. |
| 768 | * |
| 769 | * @see #getRootId(Uri) |
| 770 | */ |
| 771 | public static Uri buildRootUri(String authority, String rootId) { |
| 772 | return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 773 | .authority(authority).appendPath(PATH_ROOT).appendPath(rootId).build(); |
| 774 | } |
| 775 | |
| 776 | /** |
Steve McKay | b67bfbf | 2015-12-08 17:02:03 -0800 | [diff] [blame] | 777 | * Builds URI for user home directory on external (local) storage. |
| 778 | * {@hide} |
| 779 | */ |
| 780 | public static Uri buildHomeUri() { |
| 781 | // TODO: Avoid this type of interpackage copying. Added here to avoid |
| 782 | // direct coupling, but not ideal. |
Garfield Tan | 92b96ba | 2016-11-01 14:33:48 -0700 | [diff] [blame] | 783 | return DocumentsContract.buildRootUri(EXTERNAL_STORAGE_PROVIDER_AUTHORITY, "home"); |
Steve McKay | b67bfbf | 2015-12-08 17:02:03 -0800 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | /** |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 787 | * Build URI representing the recently modified documents of a specific root |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 788 | * in a document provider. When queried, a provider will return zero or more |
| 789 | * rows with columns defined by {@link Document}. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 790 | * |
| 791 | * @see DocumentsProvider#queryRecentDocuments(String, String[]) |
| 792 | * @see #getRootId(Uri) |
| 793 | */ |
| 794 | public static Uri buildRecentDocumentsUri(String authority, String rootId) { |
| 795 | return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 796 | .authority(authority).appendPath(PATH_ROOT).appendPath(rootId) |
| 797 | .appendPath(PATH_RECENT).build(); |
| 798 | } |
| 799 | |
| 800 | /** |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 801 | * Build URI representing access to descendant documents of the given |
| 802 | * {@link Document#COLUMN_DOCUMENT_ID}. |
| 803 | * |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 804 | * @see #getTreeDocumentId(Uri) |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 805 | */ |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 806 | public static Uri buildTreeDocumentUri(String authority, String documentId) { |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 807 | return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority) |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 808 | .appendPath(PATH_TREE).appendPath(documentId).build(); |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /** |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 812 | * Build URI representing the target {@link Document#COLUMN_DOCUMENT_ID} in |
| 813 | * a document provider. When queried, a provider will return a single row |
| 814 | * with columns defined by {@link Document}. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 815 | * |
| 816 | * @see DocumentsProvider#queryDocument(String, String[]) |
| 817 | * @see #getDocumentId(Uri) |
| 818 | */ |
| 819 | public static Uri buildDocumentUri(String authority, String documentId) { |
| 820 | return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 821 | .authority(authority).appendPath(PATH_DOCUMENT).appendPath(documentId).build(); |
| 822 | } |
| 823 | |
| 824 | /** |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 825 | * Build URI representing the target {@link Document#COLUMN_DOCUMENT_ID} in |
| 826 | * a document provider. When queried, a provider will return a single row |
| 827 | * with columns defined by {@link Document}. |
| 828 | * <p> |
| 829 | * However, instead of directly accessing the target document, the returned |
| 830 | * URI will leverage access granted through a subtree URI, typically |
| 831 | * returned by {@link Intent#ACTION_OPEN_DOCUMENT_TREE}. The target document |
| 832 | * must be a descendant (child, grandchild, etc) of the subtree. |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 833 | * <p> |
| 834 | * This is typically used to access documents under a user-selected |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 835 | * directory tree, since it doesn't require the user to separately confirm |
| 836 | * each new document access. |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 837 | * |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 838 | * @param treeUri the subtree to leverage to gain access to the target |
| 839 | * document. The target directory must be a descendant of this |
| 840 | * subtree. |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 841 | * @param documentId the target document, which the caller may not have |
| 842 | * direct access to. |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 843 | * @see Intent#ACTION_OPEN_DOCUMENT_TREE |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 844 | * @see DocumentsProvider#isChildDocument(String, String) |
| 845 | * @see #buildDocumentUri(String, String) |
| 846 | */ |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 847 | public static Uri buildDocumentUriUsingTree(Uri treeUri, String documentId) { |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 848 | return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 849 | .authority(treeUri.getAuthority()).appendPath(PATH_TREE) |
| 850 | .appendPath(getTreeDocumentId(treeUri)).appendPath(PATH_DOCUMENT) |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 851 | .appendPath(documentId).build(); |
| 852 | } |
| 853 | |
| 854 | /** {@hide} */ |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 855 | public static Uri buildDocumentUriMaybeUsingTree(Uri baseUri, String documentId) { |
| 856 | if (isTreeUri(baseUri)) { |
| 857 | return buildDocumentUriUsingTree(baseUri, documentId); |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 858 | } else { |
| 859 | return buildDocumentUri(baseUri.getAuthority(), documentId); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | /** |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 864 | * Build URI representing the children of the target directory in a document |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 865 | * provider. When queried, a provider will return zero or more rows with |
| 866 | * columns defined by {@link Document}. |
| 867 | * |
| 868 | * @param parentDocumentId the document to return children for, which must |
| 869 | * be a directory with MIME type of |
| 870 | * {@link Document#MIME_TYPE_DIR}. |
| 871 | * @see DocumentsProvider#queryChildDocuments(String, String[], String) |
| 872 | * @see #getDocumentId(Uri) |
| 873 | */ |
| 874 | public static Uri buildChildDocumentsUri(String authority, String parentDocumentId) { |
| 875 | return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority) |
| 876 | .appendPath(PATH_DOCUMENT).appendPath(parentDocumentId).appendPath(PATH_CHILDREN) |
| 877 | .build(); |
| 878 | } |
| 879 | |
| 880 | /** |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 881 | * Build URI representing the children of the target directory in a document |
| 882 | * provider. When queried, a provider will return zero or more rows with |
| 883 | * columns defined by {@link Document}. |
| 884 | * <p> |
| 885 | * However, instead of directly accessing the target directory, the returned |
| 886 | * URI will leverage access granted through a subtree URI, typically |
| 887 | * returned by {@link Intent#ACTION_OPEN_DOCUMENT_TREE}. The target |
| 888 | * directory must be a descendant (child, grandchild, etc) of the subtree. |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 889 | * <p> |
| 890 | * This is typically used to access documents under a user-selected |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 891 | * directory tree, since it doesn't require the user to separately confirm |
| 892 | * each new document access. |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 893 | * |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 894 | * @param treeUri the subtree to leverage to gain access to the target |
| 895 | * document. The target directory must be a descendant of this |
| 896 | * subtree. |
| 897 | * @param parentDocumentId the document to return children for, which the |
| 898 | * caller may not have direct access to, and which must be a |
| 899 | * directory with MIME type of {@link Document#MIME_TYPE_DIR}. |
| 900 | * @see Intent#ACTION_OPEN_DOCUMENT_TREE |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 901 | * @see DocumentsProvider#isChildDocument(String, String) |
| 902 | * @see #buildChildDocumentsUri(String, String) |
| 903 | */ |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 904 | public static Uri buildChildDocumentsUriUsingTree(Uri treeUri, String parentDocumentId) { |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 905 | return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 906 | .authority(treeUri.getAuthority()).appendPath(PATH_TREE) |
| 907 | .appendPath(getTreeDocumentId(treeUri)).appendPath(PATH_DOCUMENT) |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 908 | .appendPath(parentDocumentId).appendPath(PATH_CHILDREN).build(); |
| 909 | } |
| 910 | |
| 911 | /** |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 912 | * Build URI representing a search for matching documents under a specific |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 913 | * root in a document provider. When queried, a provider will return zero or |
| 914 | * more rows with columns defined by {@link Document}. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 915 | * |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 916 | * @see DocumentsProvider#querySearchDocuments(String, String, String[]) |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 917 | * @see #getRootId(Uri) |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 918 | * @see #getSearchDocumentsQuery(Uri) |
| 919 | */ |
| 920 | public static Uri buildSearchDocumentsUri( |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 921 | String authority, String rootId, String query) { |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 922 | return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority) |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 923 | .appendPath(PATH_ROOT).appendPath(rootId).appendPath(PATH_SEARCH) |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 924 | .appendQueryParameter(PARAM_QUERY, query).build(); |
| 925 | } |
| 926 | |
| 927 | /** |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 928 | * Test if the given URI represents a {@link Document} backed by a |
Jeff Sharkey | ee2f7df | 2013-09-26 11:32:30 -0700 | [diff] [blame] | 929 | * {@link DocumentsProvider}. |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 930 | * |
| 931 | * @see #buildDocumentUri(String, String) |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 932 | * @see #buildDocumentUriUsingTree(Uri, String) |
Jeff Sharkey | ee2f7df | 2013-09-26 11:32:30 -0700 | [diff] [blame] | 933 | */ |
Steve McKay | 323ee3e | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 934 | public static boolean isDocumentUri(Context context, @Nullable Uri uri) { |
| 935 | if (isContentUri(uri) && isDocumentsProvider(context, uri.getAuthority())) { |
| 936 | final List<String> paths = uri.getPathSegments(); |
| 937 | if (paths.size() == 2) { |
| 938 | return PATH_DOCUMENT.equals(paths.get(0)); |
| 939 | } else if (paths.size() == 4) { |
| 940 | return PATH_TREE.equals(paths.get(0)) && PATH_DOCUMENT.equals(paths.get(2)); |
| 941 | } |
Jeff Sharkey | ee2f7df | 2013-09-26 11:32:30 -0700 | [diff] [blame] | 942 | } |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 943 | return false; |
| 944 | } |
Jeff Sharkey | ee2f7df | 2013-09-26 11:32:30 -0700 | [diff] [blame] | 945 | |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 946 | /** {@hide} */ |
Steve McKay | 323ee3e | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 947 | public static boolean isRootUri(Context context, @Nullable Uri uri) { |
| 948 | if (isContentUri(uri) && isDocumentsProvider(context, uri.getAuthority())) { |
| 949 | final List<String> paths = uri.getPathSegments(); |
| 950 | return (paths.size() == 2 && PATH_ROOT.equals(paths.get(0))); |
| 951 | } |
| 952 | return false; |
| 953 | } |
| 954 | |
| 955 | /** {@hide} */ |
| 956 | public static boolean isContentUri(@Nullable Uri uri) { |
| 957 | return uri != null && ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()); |
| 958 | } |
| 959 | |
Tomasz Mikolajewski | 7db9c81 | 2016-01-28 09:50:01 +0900 | [diff] [blame] | 960 | /** |
| 961 | * Test if the given URI represents a {@link Document} tree. |
| 962 | * |
| 963 | * @see #buildTreeDocumentUri(String, String) |
Tomasz Mikolajewski | 90a7533 | 2016-07-13 10:14:59 +0900 | [diff] [blame] | 964 | * @see #getTreeDocumentId(Uri) |
Tomasz Mikolajewski | 7db9c81 | 2016-01-28 09:50:01 +0900 | [diff] [blame] | 965 | */ |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 966 | public static boolean isTreeUri(Uri uri) { |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 967 | final List<String> paths = uri.getPathSegments(); |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 968 | return (paths.size() >= 2 && PATH_TREE.equals(paths.get(0))); |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | private static boolean isDocumentsProvider(Context context, String authority) { |
Jeff Sharkey | d2e1e81 | 2013-10-09 13:31:13 -0700 | [diff] [blame] | 972 | final Intent intent = new Intent(PROVIDER_INTERFACE); |
| 973 | final List<ResolveInfo> infos = context.getPackageManager() |
| 974 | .queryIntentContentProviders(intent, 0); |
| 975 | for (ResolveInfo info : infos) { |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 976 | if (authority.equals(info.providerInfo.authority)) { |
Jeff Sharkey | d2e1e81 | 2013-10-09 13:31:13 -0700 | [diff] [blame] | 977 | return true; |
| 978 | } |
Jeff Sharkey | ee2f7df | 2013-09-26 11:32:30 -0700 | [diff] [blame] | 979 | } |
| 980 | return false; |
| 981 | } |
| 982 | |
| 983 | /** |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 984 | * Extract the {@link Root#COLUMN_ROOT_ID} from the given URI. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 985 | */ |
| 986 | public static String getRootId(Uri rootUri) { |
| 987 | final List<String> paths = rootUri.getPathSegments(); |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 988 | if (paths.size() >= 2 && PATH_ROOT.equals(paths.get(0))) { |
| 989 | return paths.get(1); |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 990 | } |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 991 | throw new IllegalArgumentException("Invalid URI: " + rootUri); |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 992 | } |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 993 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 994 | /** |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 995 | * Extract the {@link Document#COLUMN_DOCUMENT_ID} from the given URI. |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 996 | * |
| 997 | * @see #isDocumentUri(Context, Uri) |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 998 | */ |
| 999 | public static String getDocumentId(Uri documentUri) { |
| 1000 | final List<String> paths = documentUri.getPathSegments(); |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 1001 | if (paths.size() >= 2 && PATH_DOCUMENT.equals(paths.get(0))) { |
| 1002 | return paths.get(1); |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 1003 | } |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 1004 | if (paths.size() >= 4 && PATH_TREE.equals(paths.get(0)) |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 1005 | && PATH_DOCUMENT.equals(paths.get(2))) { |
| 1006 | return paths.get(3); |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 1007 | } |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 1008 | throw new IllegalArgumentException("Invalid URI: " + documentUri); |
| 1009 | } |
| 1010 | |
| 1011 | /** |
| 1012 | * Extract the via {@link Document#COLUMN_DOCUMENT_ID} from the given URI. |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 1013 | */ |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 1014 | public static String getTreeDocumentId(Uri documentUri) { |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 1015 | final List<String> paths = documentUri.getPathSegments(); |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 1016 | if (paths.size() >= 2 && PATH_TREE.equals(paths.get(0))) { |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 1017 | return paths.get(1); |
| 1018 | } |
| 1019 | throw new IllegalArgumentException("Invalid URI: " + documentUri); |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 1020 | } |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 1021 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 1022 | /** |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 1023 | * Extract the search query from a URI built by |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 1024 | * {@link #buildSearchDocumentsUri(String, String, String)}. |
| 1025 | */ |
| 1026 | public static String getSearchDocumentsQuery(Uri searchDocumentsUri) { |
| 1027 | return searchDocumentsUri.getQueryParameter(PARAM_QUERY); |
Jeff Sharkey | 20d96d8 | 2013-07-30 17:08:39 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1030 | /** {@hide} */ |
Mathew Inwood | ba50311 | 2018-08-10 09:37:35 +0100 | [diff] [blame] | 1031 | @UnsupportedAppUsage |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1032 | public static Uri setManageMode(Uri uri) { |
| 1033 | return uri.buildUpon().appendQueryParameter(PARAM_MANAGE, "true").build(); |
| 1034 | } |
| 1035 | |
| 1036 | /** {@hide} */ |
| 1037 | public static boolean isManageMode(Uri uri) { |
| 1038 | return uri.getBooleanQueryParameter(PARAM_MANAGE, false); |
| 1039 | } |
| 1040 | |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 1041 | /** |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 1042 | * Return thumbnail representing the document at the given URI. Callers are |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 1043 | * responsible for their own in-memory caching. |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 1044 | * |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 1045 | * @param documentUri document to return thumbnail for, which must have |
| 1046 | * {@link Document#FLAG_SUPPORTS_THUMBNAIL} set. |
| 1047 | * @param size optimal thumbnail size desired. A provider may return a |
| 1048 | * thumbnail of a different size, but never more than double the |
| 1049 | * requested size. |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 1050 | * @param signal signal used to indicate if caller is no longer interested |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 1051 | * in the thumbnail. |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 1052 | * @return decoded thumbnail, or {@code null} if problem was encountered. |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 1053 | * @see DocumentsProvider#openDocumentThumbnail(String, Point, |
| 1054 | * android.os.CancellationSignal) |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 1055 | */ |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 1056 | public static Bitmap getDocumentThumbnail( |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1057 | ContentResolver resolver, Uri documentUri, Point size, CancellationSignal signal) |
| 1058 | throws FileNotFoundException { |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 1059 | final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( |
| 1060 | documentUri.getAuthority()); |
| 1061 | try { |
| 1062 | return getDocumentThumbnail(client, documentUri, size, signal); |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1063 | } catch (Exception e) { |
Jeff Sharkey | 3381931 | 2013-10-29 11:56:37 -0700 | [diff] [blame] | 1064 | if (!(e instanceof OperationCanceledException)) { |
| 1065 | Log.w(TAG, "Failed to load thumbnail for " + documentUri + ": " + e); |
| 1066 | } |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1067 | rethrowIfNecessary(resolver, e); |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 1068 | return null; |
| 1069 | } finally { |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1070 | ContentProviderClient.releaseQuietly(client); |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | /** {@hide} */ |
Mathew Inwood | ba50311 | 2018-08-10 09:37:35 +0100 | [diff] [blame] | 1075 | @UnsupportedAppUsage |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 1076 | public static Bitmap getDocumentThumbnail( |
| 1077 | ContentProviderClient client, Uri documentUri, Point size, CancellationSignal signal) |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1078 | throws RemoteException, IOException { |
Jeff Sharkey | 6398343 | 2013-08-21 11:33:50 -0700 | [diff] [blame] | 1079 | final Bundle openOpts = new Bundle(); |
Jeff Sharkey | 5b836f2 | 2014-08-27 14:46:32 -0700 | [diff] [blame] | 1080 | openOpts.putParcelable(ContentResolver.EXTRA_SIZE, size); |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 1081 | |
Jeff Sharkey | 9d0843d | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1082 | AssetFileDescriptor afd = null; |
Jeff Sharkey | c1c8f3f | 2013-10-14 14:57:33 -0700 | [diff] [blame] | 1083 | Bitmap bitmap = null; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 1084 | try { |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 1085 | afd = client.openTypedAssetFileDescriptor(documentUri, "image/*", openOpts, signal); |
Jeff Sharkey | 9d0843d | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1086 | |
| 1087 | final FileDescriptor fd = afd.getFileDescriptor(); |
Jeff Sharkey | 6398343 | 2013-08-21 11:33:50 -0700 | [diff] [blame] | 1088 | final long offset = afd.getStartOffset(); |
Jeff Sharkey | 9d0843d | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1089 | |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 1090 | // Try seeking on the returned FD, since it gives us the most |
| 1091 | // optimal decode path; otherwise fall back to buffering. |
| 1092 | BufferedInputStream is = null; |
| 1093 | try { |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 1094 | Os.lseek(fd, offset, SEEK_SET); |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 1095 | } catch (ErrnoException e) { |
| 1096 | is = new BufferedInputStream(new FileInputStream(fd), THUMBNAIL_BUFFER_SIZE); |
| 1097 | is.mark(THUMBNAIL_BUFFER_SIZE); |
Jeff Sharkey | 6398343 | 2013-08-21 11:33:50 -0700 | [diff] [blame] | 1098 | } |
Jeff Sharkey | 9d0843d | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1099 | |
Jeff Sharkey | 6398343 | 2013-08-21 11:33:50 -0700 | [diff] [blame] | 1100 | // We requested a rough thumbnail size, but the remote size may have |
| 1101 | // returned something giant, so defensively scale down as needed. |
| 1102 | final BitmapFactory.Options opts = new BitmapFactory.Options(); |
| 1103 | opts.inJustDecodeBounds = true; |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 1104 | if (is != null) { |
| 1105 | BitmapFactory.decodeStream(is, null, opts); |
Jeff Sharkey | 6398343 | 2013-08-21 11:33:50 -0700 | [diff] [blame] | 1106 | } else { |
| 1107 | BitmapFactory.decodeFileDescriptor(fd, null, opts); |
| 1108 | } |
Jeff Sharkey | 9d0843d | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1109 | |
Jeff Sharkey | 6398343 | 2013-08-21 11:33:50 -0700 | [diff] [blame] | 1110 | final int widthSample = opts.outWidth / size.x; |
| 1111 | final int heightSample = opts.outHeight / size.y; |
| 1112 | |
| 1113 | opts.inJustDecodeBounds = false; |
| 1114 | opts.inSampleSize = Math.min(widthSample, heightSample); |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 1115 | if (is != null) { |
| 1116 | is.reset(); |
Jeff Sharkey | c1c8f3f | 2013-10-14 14:57:33 -0700 | [diff] [blame] | 1117 | bitmap = BitmapFactory.decodeStream(is, null, opts); |
Jeff Sharkey | 6398343 | 2013-08-21 11:33:50 -0700 | [diff] [blame] | 1118 | } else { |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 1119 | try { |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 1120 | Os.lseek(fd, offset, SEEK_SET); |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 1121 | } catch (ErrnoException e) { |
| 1122 | e.rethrowAsIOException(); |
| 1123 | } |
Jeff Sharkey | c1c8f3f | 2013-10-14 14:57:33 -0700 | [diff] [blame] | 1124 | bitmap = BitmapFactory.decodeFileDescriptor(fd, null, opts); |
| 1125 | } |
| 1126 | |
| 1127 | // Transform the bitmap if requested. We use a side-channel to |
| 1128 | // communicate the orientation, since EXIF thumbnails don't contain |
| 1129 | // the rotation flags of the original image. |
| 1130 | final Bundle extras = afd.getExtras(); |
| 1131 | final int orientation = (extras != null) ? extras.getInt(EXTRA_ORIENTATION, 0) : 0; |
| 1132 | if (orientation != 0) { |
| 1133 | final int width = bitmap.getWidth(); |
| 1134 | final int height = bitmap.getHeight(); |
| 1135 | |
| 1136 | final Matrix m = new Matrix(); |
| 1137 | m.setRotate(orientation, width / 2, height / 2); |
| 1138 | bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, false); |
Jeff Sharkey | 6398343 | 2013-08-21 11:33:50 -0700 | [diff] [blame] | 1139 | } |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 1140 | } finally { |
Jeff Sharkey | 9d0843d | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1141 | IoUtils.closeQuietly(afd); |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 1142 | } |
Jeff Sharkey | c1c8f3f | 2013-10-14 14:57:33 -0700 | [diff] [blame] | 1143 | |
| 1144 | return bitmap; |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Jeff Sharkey | 5b83f85 | 2013-08-14 18:29:19 -0700 | [diff] [blame] | 1147 | /** |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 1148 | * Create a new document with given MIME type and display name. |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 1149 | * |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1150 | * @param parentDocumentUri directory with {@link Document#FLAG_DIR_SUPPORTS_CREATE} |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 1151 | * @param mimeType MIME type of new document |
| 1152 | * @param displayName name of new document |
| 1153 | * @return newly created document, or {@code null} if failed |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 1154 | */ |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 1155 | public static Uri createDocument(ContentResolver resolver, Uri parentDocumentUri, |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1156 | String mimeType, String displayName) throws FileNotFoundException { |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 1157 | final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( |
| 1158 | parentDocumentUri.getAuthority()); |
| 1159 | try { |
| 1160 | return createDocument(client, parentDocumentUri, mimeType, displayName); |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1161 | } catch (Exception e) { |
| 1162 | Log.w(TAG, "Failed to create document", e); |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1163 | rethrowIfNecessary(resolver, e); |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1164 | return null; |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 1165 | } finally { |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1166 | ContentProviderClient.releaseQuietly(client); |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | /** {@hide} */ |
| 1171 | public static Uri createDocument(ContentProviderClient client, Uri parentDocumentUri, |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1172 | String mimeType, String displayName) throws RemoteException { |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 1173 | final Bundle in = new Bundle(); |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 1174 | in.putParcelable(DocumentsContract.EXTRA_URI, parentDocumentUri); |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 1175 | in.putString(Document.COLUMN_MIME_TYPE, mimeType); |
| 1176 | in.putString(Document.COLUMN_DISPLAY_NAME, displayName); |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 1177 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1178 | final Bundle out = client.call(METHOD_CREATE_DOCUMENT, null, in); |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 1179 | return out.getParcelable(DocumentsContract.EXTRA_URI); |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 1180 | } |
Jeff Sharkey | 5b83f85 | 2013-08-14 18:29:19 -0700 | [diff] [blame] | 1181 | |
Steve McKay | d3afdee | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 1182 | /** {@hide} */ |
| 1183 | public static boolean isChildDocument(ContentProviderClient client, Uri parentDocumentUri, |
| 1184 | Uri childDocumentUri) throws RemoteException { |
| 1185 | |
| 1186 | final Bundle in = new Bundle(); |
| 1187 | in.putParcelable(DocumentsContract.EXTRA_URI, parentDocumentUri); |
| 1188 | in.putParcelable(DocumentsContract.EXTRA_TARGET_URI, childDocumentUri); |
| 1189 | |
| 1190 | final Bundle out = client.call(METHOD_IS_CHILD_DOCUMENT, null, in); |
| 1191 | if (out == null) { |
| 1192 | throw new RemoteException("Failed to get a reponse from isChildDocument query."); |
| 1193 | } |
| 1194 | if (!out.containsKey(DocumentsContract.EXTRA_RESULT)) { |
| 1195 | throw new RemoteException("Response did not include result field.."); |
| 1196 | } |
| 1197 | return out.getBoolean(DocumentsContract.EXTRA_RESULT); |
| 1198 | } |
| 1199 | |
Jeff Sharkey | 5b83f85 | 2013-08-14 18:29:19 -0700 | [diff] [blame] | 1200 | /** |
Jeff Sharkey | b7e1255 | 2014-05-21 22:22:03 -0700 | [diff] [blame] | 1201 | * Change the display name of an existing document. |
| 1202 | * <p> |
| 1203 | * If the underlying provider needs to create a new |
| 1204 | * {@link Document#COLUMN_DOCUMENT_ID} to represent the updated display |
| 1205 | * name, that new document is returned and the original document is no |
| 1206 | * longer valid. Otherwise, the original document is returned. |
| 1207 | * |
| 1208 | * @param documentUri document with {@link Document#FLAG_SUPPORTS_RENAME} |
| 1209 | * @param displayName updated name for document |
| 1210 | * @return the existing or new document after the rename, or {@code null} if |
| 1211 | * failed. |
| 1212 | */ |
| 1213 | public static Uri renameDocument(ContentResolver resolver, Uri documentUri, |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1214 | String displayName) throws FileNotFoundException { |
Jeff Sharkey | b7e1255 | 2014-05-21 22:22:03 -0700 | [diff] [blame] | 1215 | final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( |
| 1216 | documentUri.getAuthority()); |
| 1217 | try { |
| 1218 | return renameDocument(client, documentUri, displayName); |
| 1219 | } catch (Exception e) { |
| 1220 | Log.w(TAG, "Failed to rename document", e); |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1221 | rethrowIfNecessary(resolver, e); |
Jeff Sharkey | b7e1255 | 2014-05-21 22:22:03 -0700 | [diff] [blame] | 1222 | return null; |
| 1223 | } finally { |
| 1224 | ContentProviderClient.releaseQuietly(client); |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | /** {@hide} */ |
| 1229 | public static Uri renameDocument(ContentProviderClient client, Uri documentUri, |
| 1230 | String displayName) throws RemoteException { |
| 1231 | final Bundle in = new Bundle(); |
| 1232 | in.putParcelable(DocumentsContract.EXTRA_URI, documentUri); |
| 1233 | in.putString(Document.COLUMN_DISPLAY_NAME, displayName); |
| 1234 | |
| 1235 | final Bundle out = client.call(METHOD_RENAME_DOCUMENT, null, in); |
| 1236 | final Uri outUri = out.getParcelable(DocumentsContract.EXTRA_URI); |
| 1237 | return (outUri != null) ? outUri : documentUri; |
| 1238 | } |
| 1239 | |
| 1240 | /** |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 1241 | * Delete the given document. |
| 1242 | * |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 1243 | * @param documentUri document with {@link Document#FLAG_SUPPORTS_DELETE} |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1244 | * @return if the document was deleted successfully. |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 1245 | */ |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1246 | public static boolean deleteDocument(ContentResolver resolver, Uri documentUri) |
| 1247 | throws FileNotFoundException { |
Jeff Sharkey | de2b22f | 2013-09-11 17:33:06 -0700 | [diff] [blame] | 1248 | final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( |
| 1249 | documentUri.getAuthority()); |
| 1250 | try { |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1251 | deleteDocument(client, documentUri); |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 1252 | return true; |
| 1253 | } catch (Exception e) { |
| 1254 | Log.w(TAG, "Failed to delete document", e); |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1255 | rethrowIfNecessary(resolver, e); |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 1256 | return false; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1257 | } finally { |
| 1258 | ContentProviderClient.releaseQuietly(client); |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 1259 | } |
Jeff Sharkey | 5b83f85 | 2013-08-14 18:29:19 -0700 | [diff] [blame] | 1260 | } |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1261 | |
| 1262 | /** {@hide} */ |
| 1263 | public static void deleteDocument(ContentProviderClient client, Uri documentUri) |
| 1264 | throws RemoteException { |
| 1265 | final Bundle in = new Bundle(); |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 1266 | in.putParcelable(DocumentsContract.EXTRA_URI, documentUri); |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1267 | |
| 1268 | client.call(METHOD_DELETE_DOCUMENT, null, in); |
| 1269 | } |
Jeff Sharkey | c1c8f3f | 2013-10-14 14:57:33 -0700 | [diff] [blame] | 1270 | |
| 1271 | /** |
Tomasz Mikolajewski | 74fe181 | 2015-06-12 17:13:26 -0700 | [diff] [blame] | 1272 | * Copies the given document. |
| 1273 | * |
| 1274 | * @param sourceDocumentUri document with {@link Document#FLAG_SUPPORTS_COPY} |
| 1275 | * @param targetParentDocumentUri document which will become a parent of the source |
| 1276 | * document's copy. |
| 1277 | * @return the copied document, or {@code null} if failed. |
Tomasz Mikolajewski | 74fe181 | 2015-06-12 17:13:26 -0700 | [diff] [blame] | 1278 | */ |
| 1279 | public static Uri copyDocument(ContentResolver resolver, Uri sourceDocumentUri, |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1280 | Uri targetParentDocumentUri) throws FileNotFoundException { |
Tomasz Mikolajewski | 74fe181 | 2015-06-12 17:13:26 -0700 | [diff] [blame] | 1281 | final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( |
| 1282 | sourceDocumentUri.getAuthority()); |
| 1283 | try { |
| 1284 | return copyDocument(client, sourceDocumentUri, targetParentDocumentUri); |
| 1285 | } catch (Exception e) { |
| 1286 | Log.w(TAG, "Failed to copy document", e); |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1287 | rethrowIfNecessary(resolver, e); |
Tomasz Mikolajewski | 74fe181 | 2015-06-12 17:13:26 -0700 | [diff] [blame] | 1288 | return null; |
| 1289 | } finally { |
| 1290 | ContentProviderClient.releaseQuietly(client); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | /** {@hide} */ |
| 1295 | public static Uri copyDocument(ContentProviderClient client, Uri sourceDocumentUri, |
| 1296 | Uri targetParentDocumentUri) throws RemoteException { |
| 1297 | final Bundle in = new Bundle(); |
| 1298 | in.putParcelable(DocumentsContract.EXTRA_URI, sourceDocumentUri); |
| 1299 | in.putParcelable(DocumentsContract.EXTRA_TARGET_URI, targetParentDocumentUri); |
| 1300 | |
| 1301 | final Bundle out = client.call(METHOD_COPY_DOCUMENT, null, in); |
| 1302 | return out.getParcelable(DocumentsContract.EXTRA_URI); |
| 1303 | } |
| 1304 | |
| 1305 | /** |
Tomasz Mikolajewski | a375a99 | 2015-06-25 15:39:27 +0900 | [diff] [blame] | 1306 | * Moves the given document under a new parent. |
| 1307 | * |
| 1308 | * @param sourceDocumentUri document with {@link Document#FLAG_SUPPORTS_MOVE} |
Tomasz Mikolajewski | d46ecbc | 2016-01-25 14:26:54 +0900 | [diff] [blame] | 1309 | * @param sourceParentDocumentUri parent document of the document to move. |
Tomasz Mikolajewski | a375a99 | 2015-06-25 15:39:27 +0900 | [diff] [blame] | 1310 | * @param targetParentDocumentUri document which will become a new parent of the source |
| 1311 | * document. |
| 1312 | * @return the moved document, or {@code null} if failed. |
Tomasz Mikolajewski | a375a99 | 2015-06-25 15:39:27 +0900 | [diff] [blame] | 1313 | */ |
| 1314 | public static Uri moveDocument(ContentResolver resolver, Uri sourceDocumentUri, |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1315 | Uri sourceParentDocumentUri, Uri targetParentDocumentUri) throws FileNotFoundException { |
Tomasz Mikolajewski | a375a99 | 2015-06-25 15:39:27 +0900 | [diff] [blame] | 1316 | final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( |
| 1317 | sourceDocumentUri.getAuthority()); |
| 1318 | try { |
Tomasz Mikolajewski | dcb4efe | 2016-02-10 19:14:49 +0900 | [diff] [blame] | 1319 | return moveDocument(client, sourceDocumentUri, sourceParentDocumentUri, |
Tomasz Mikolajewski | d46ecbc | 2016-01-25 14:26:54 +0900 | [diff] [blame] | 1320 | targetParentDocumentUri); |
Tomasz Mikolajewski | a375a99 | 2015-06-25 15:39:27 +0900 | [diff] [blame] | 1321 | } catch (Exception e) { |
| 1322 | Log.w(TAG, "Failed to move document", e); |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1323 | rethrowIfNecessary(resolver, e); |
Tomasz Mikolajewski | a375a99 | 2015-06-25 15:39:27 +0900 | [diff] [blame] | 1324 | return null; |
| 1325 | } finally { |
| 1326 | ContentProviderClient.releaseQuietly(client); |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | /** {@hide} */ |
Mathew Inwood | ba50311 | 2018-08-10 09:37:35 +0100 | [diff] [blame] | 1331 | @UnsupportedAppUsage |
Tomasz Mikolajewski | a375a99 | 2015-06-25 15:39:27 +0900 | [diff] [blame] | 1332 | public static Uri moveDocument(ContentProviderClient client, Uri sourceDocumentUri, |
Tomasz Mikolajewski | d46ecbc | 2016-01-25 14:26:54 +0900 | [diff] [blame] | 1333 | Uri sourceParentDocumentUri, Uri targetParentDocumentUri) throws RemoteException { |
Tomasz Mikolajewski | a375a99 | 2015-06-25 15:39:27 +0900 | [diff] [blame] | 1334 | final Bundle in = new Bundle(); |
| 1335 | in.putParcelable(DocumentsContract.EXTRA_URI, sourceDocumentUri); |
Tomasz Mikolajewski | d46ecbc | 2016-01-25 14:26:54 +0900 | [diff] [blame] | 1336 | in.putParcelable(DocumentsContract.EXTRA_PARENT_URI, sourceParentDocumentUri); |
Tomasz Mikolajewski | a375a99 | 2015-06-25 15:39:27 +0900 | [diff] [blame] | 1337 | in.putParcelable(DocumentsContract.EXTRA_TARGET_URI, targetParentDocumentUri); |
| 1338 | |
| 1339 | final Bundle out = client.call(METHOD_MOVE_DOCUMENT, null, in); |
| 1340 | return out.getParcelable(DocumentsContract.EXTRA_URI); |
| 1341 | } |
| 1342 | |
| 1343 | /** |
Tomasz Mikolajewski | cbcd394 | 2016-01-28 12:39:25 +0900 | [diff] [blame] | 1344 | * Removes the given document from a parent directory. |
| 1345 | * |
| 1346 | * <p>In contrast to {@link #deleteDocument} it requires specifying the parent. |
| 1347 | * This method is especially useful if the document can be in multiple parents. |
| 1348 | * |
| 1349 | * @param documentUri document with {@link Document#FLAG_SUPPORTS_REMOVE} |
| 1350 | * @param parentDocumentUri parent document of the document to remove. |
| 1351 | * @return true if the document was removed successfully. |
| 1352 | */ |
| 1353 | public static boolean removeDocument(ContentResolver resolver, Uri documentUri, |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1354 | Uri parentDocumentUri) throws FileNotFoundException { |
Tomasz Mikolajewski | cbcd394 | 2016-01-28 12:39:25 +0900 | [diff] [blame] | 1355 | final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( |
| 1356 | documentUri.getAuthority()); |
| 1357 | try { |
| 1358 | removeDocument(client, documentUri, parentDocumentUri); |
| 1359 | return true; |
| 1360 | } catch (Exception e) { |
| 1361 | Log.w(TAG, "Failed to remove document", e); |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1362 | rethrowIfNecessary(resolver, e); |
Tomasz Mikolajewski | cbcd394 | 2016-01-28 12:39:25 +0900 | [diff] [blame] | 1363 | return false; |
| 1364 | } finally { |
| 1365 | ContentProviderClient.releaseQuietly(client); |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | /** {@hide} */ |
| 1370 | public static void removeDocument(ContentProviderClient client, Uri documentUri, |
| 1371 | Uri parentDocumentUri) throws RemoteException { |
| 1372 | final Bundle in = new Bundle(); |
| 1373 | in.putParcelable(DocumentsContract.EXTRA_URI, documentUri); |
| 1374 | in.putParcelable(DocumentsContract.EXTRA_PARENT_URI, parentDocumentUri); |
| 1375 | |
| 1376 | client.call(METHOD_REMOVE_DOCUMENT, null, in); |
| 1377 | } |
| 1378 | |
Garfield Tan | 8787703 | 2017-03-22 12:01:14 -0700 | [diff] [blame] | 1379 | /** |
| 1380 | * Ejects the given root. It throws {@link IllegalStateException} when ejection failed. |
| 1381 | * |
| 1382 | * @param rootUri root with {@link Root#FLAG_SUPPORTS_EJECT} to be ejected |
| 1383 | */ |
| 1384 | public static void ejectRoot(ContentResolver resolver, Uri rootUri) { |
Ben Lin | e7822fb | 2016-06-24 15:21:08 -0700 | [diff] [blame] | 1385 | final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( |
| 1386 | rootUri.getAuthority()); |
| 1387 | try { |
Garfield Tan | 8787703 | 2017-03-22 12:01:14 -0700 | [diff] [blame] | 1388 | ejectRoot(client, rootUri); |
| 1389 | } catch (RemoteException e) { |
| 1390 | e.rethrowAsRuntimeException(); |
Ben Lin | e7822fb | 2016-06-24 15:21:08 -0700 | [diff] [blame] | 1391 | } finally { |
| 1392 | ContentProviderClient.releaseQuietly(client); |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | /** {@hide} */ |
Garfield Tan | 8787703 | 2017-03-22 12:01:14 -0700 | [diff] [blame] | 1397 | public static void ejectRoot(ContentProviderClient client, Uri rootUri) |
Ben Lin | e7822fb | 2016-06-24 15:21:08 -0700 | [diff] [blame] | 1398 | throws RemoteException { |
| 1399 | final Bundle in = new Bundle(); |
| 1400 | in.putParcelable(DocumentsContract.EXTRA_URI, rootUri); |
| 1401 | |
Garfield Tan | 8787703 | 2017-03-22 12:01:14 -0700 | [diff] [blame] | 1402 | client.call(METHOD_EJECT_ROOT, null, in); |
Ben Lin | e7822fb | 2016-06-24 15:21:08 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
Tomasz Mikolajewski | cbcd394 | 2016-01-28 12:39:25 +0900 | [diff] [blame] | 1405 | /** |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 1406 | * Returns metadata associated with the document. The type of metadata returned |
Steve McKay | 17a9ce3 | 2017-07-27 13:37:14 -0700 | [diff] [blame] | 1407 | * is specific to the document type. For example the data returned for an image |
| 1408 | * file will likely consist primarily or soley of EXIF metadata. |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 1409 | * |
Steve McKay | 17a9ce3 | 2017-07-27 13:37:14 -0700 | [diff] [blame] | 1410 | * <p>The returned {@link Bundle} will contain zero or more entries depending |
| 1411 | * on the type of data supported by the document provider. |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 1412 | * |
Steve McKay | 17a9ce3 | 2017-07-27 13:37:14 -0700 | [diff] [blame] | 1413 | * <ol> |
| 1414 | * <li>A {@link DocumentsContract.METADATA_TYPES} containing a {@code String[]} value. |
| 1415 | * The string array identifies the type or types of metadata returned. Each |
| 1416 | * value in the can be used to access a {@link Bundle} of data |
| 1417 | * containing that type of data. |
| 1418 | * <li>An entry each for each type of returned metadata. Each set of metadata is |
| 1419 | * itself represented as a bundle and accessible via a string key naming |
| 1420 | * the type of data. |
| 1421 | * </ol> |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 1422 | * |
Steve McKay | 17a9ce3 | 2017-07-27 13:37:14 -0700 | [diff] [blame] | 1423 | * <p>Example: |
| 1424 | * <p><pre><code> |
| 1425 | * Bundle metadata = DocumentsContract.getDocumentMetadata(client, imageDocUri, tags); |
| 1426 | * if (metadata.containsKey(DocumentsContract.METADATA_EXIF)) { |
| 1427 | * Bundle exif = metadata.getBundle(DocumentsContract.METADATA_EXIF); |
| 1428 | * int imageLength = exif.getInt(ExifInterface.TAG_IMAGE_LENGTH); |
| 1429 | * } |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 1430 | * </code></pre> |
| 1431 | * |
Steve McKay | 17a9ce3 | 2017-07-27 13:37:14 -0700 | [diff] [blame] | 1432 | * @param documentUri a Document URI |
| 1433 | * @return a Bundle of Bundles. |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 1434 | * {@hide} |
| 1435 | */ |
Steve McKay | 17a9ce3 | 2017-07-27 13:37:14 -0700 | [diff] [blame] | 1436 | public static Bundle getDocumentMetadata(ContentResolver resolver, Uri documentUri) |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 1437 | throws FileNotFoundException { |
| 1438 | final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( |
| 1439 | documentUri.getAuthority()); |
| 1440 | |
| 1441 | try { |
Steve McKay | 17a9ce3 | 2017-07-27 13:37:14 -0700 | [diff] [blame] | 1442 | return getDocumentMetadata(client, documentUri); |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 1443 | } catch (Exception e) { |
| 1444 | Log.w(TAG, "Failed to get document metadata"); |
| 1445 | rethrowIfNecessary(resolver, e); |
| 1446 | return null; |
| 1447 | } finally { |
| 1448 | ContentProviderClient.releaseQuietly(client); |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | /** |
| 1453 | * Returns metadata associated with the document. The type of metadata returned |
Steve McKay | 17a9ce3 | 2017-07-27 13:37:14 -0700 | [diff] [blame] | 1454 | * is specific to the document type. For example the data returned for an image |
| 1455 | * file will likely consist primarily or soley of EXIF metadata. |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 1456 | * |
Steve McKay | 17a9ce3 | 2017-07-27 13:37:14 -0700 | [diff] [blame] | 1457 | * <p>The returned {@link Bundle} will contain zero or more entries depending |
| 1458 | * on the type of data supported by the document provider. |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 1459 | * |
Steve McKay | 17a9ce3 | 2017-07-27 13:37:14 -0700 | [diff] [blame] | 1460 | * <ol> |
| 1461 | * <li>A {@link DocumentsContract.METADATA_TYPES} containing a {@code String[]} value. |
| 1462 | * The string array identifies the type or types of metadata returned. Each |
| 1463 | * value in the can be used to access a {@link Bundle} of data |
| 1464 | * containing that type of data. |
| 1465 | * <li>An entry each for each type of returned metadata. Each set of metadata is |
| 1466 | * itself represented as a bundle and accessible via a string key naming |
| 1467 | * the type of data. |
| 1468 | * </ol> |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 1469 | * |
Steve McKay | 17a9ce3 | 2017-07-27 13:37:14 -0700 | [diff] [blame] | 1470 | * <p>Example: |
| 1471 | * <p><pre><code> |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 1472 | * Bundle metadata = DocumentsContract.getDocumentMetadata(client, imageDocUri, tags); |
Steve McKay | 17a9ce3 | 2017-07-27 13:37:14 -0700 | [diff] [blame] | 1473 | * if (metadata.containsKey(DocumentsContract.METADATA_EXIF)) { |
| 1474 | * Bundle exif = metadata.getBundle(DocumentsContract.METADATA_EXIF); |
| 1475 | * int imageLength = exif.getInt(ExifInterface.TAG_IMAGE_LENGTH); |
| 1476 | * } |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 1477 | * </code></pre> |
| 1478 | * |
Steve McKay | 17a9ce3 | 2017-07-27 13:37:14 -0700 | [diff] [blame] | 1479 | * @param documentUri a Document URI |
| 1480 | * @return a Bundle of Bundles. |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 1481 | * {@hide} |
| 1482 | */ |
Steve McKay | 17a9ce3 | 2017-07-27 13:37:14 -0700 | [diff] [blame] | 1483 | public static Bundle getDocumentMetadata( |
| 1484 | ContentProviderClient client, Uri documentUri) throws RemoteException { |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 1485 | final Bundle in = new Bundle(); |
| 1486 | in.putParcelable(EXTRA_URI, documentUri); |
Julian Mancini | b650515 | 2017-06-27 13:29:09 -0700 | [diff] [blame] | 1487 | |
| 1488 | final Bundle out = client.call(METHOD_GET_DOCUMENT_METADATA, null, in); |
| 1489 | |
| 1490 | if (out == null) { |
| 1491 | throw new RemoteException("Failed to get a response from getDocumentMetadata"); |
| 1492 | } |
| 1493 | return out; |
| 1494 | } |
| 1495 | |
| 1496 | /** |
Garfield Tan | b690b4d | 2017-03-01 16:05:23 -0800 | [diff] [blame] | 1497 | * Finds the canonical path from the top of the document tree. |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 1498 | * |
Garfield Tan | b690b4d | 2017-03-01 16:05:23 -0800 | [diff] [blame] | 1499 | * The {@link Path#getPath()} of the return value contains the document ID |
| 1500 | * of all documents along the path from the top the document tree to the |
| 1501 | * requested document, both inclusive. |
| 1502 | * |
| 1503 | * The {@link Path#getRootId()} of the return value returns {@code null}. |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 1504 | * |
| 1505 | * @param treeUri treeUri of the document which path is requested. |
Garfield Tan | b690b4d | 2017-03-01 16:05:23 -0800 | [diff] [blame] | 1506 | * @return the path of the document, or {@code null} if failed. |
Garfield Tan | 3f6b68a | 2016-11-01 14:13:38 -0700 | [diff] [blame] | 1507 | * @see DocumentsProvider#findDocumentPath(String, String) |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 1508 | */ |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1509 | public static Path findDocumentPath(ContentResolver resolver, Uri treeUri) |
| 1510 | throws FileNotFoundException { |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 1511 | checkArgument(isTreeUri(treeUri), treeUri + " is not a tree uri."); |
| 1512 | |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 1513 | final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 1514 | treeUri.getAuthority()); |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 1515 | try { |
Garfield Tan | b690b4d | 2017-03-01 16:05:23 -0800 | [diff] [blame] | 1516 | return findDocumentPath(client, treeUri); |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 1517 | } catch (Exception e) { |
| 1518 | Log.w(TAG, "Failed to find path", e); |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1519 | rethrowIfNecessary(resolver, e); |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 1520 | return null; |
| 1521 | } finally { |
| 1522 | ContentProviderClient.releaseQuietly(client); |
| 1523 | } |
| 1524 | } |
| 1525 | |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 1526 | /** |
Garfield Tan | b690b4d | 2017-03-01 16:05:23 -0800 | [diff] [blame] | 1527 | * Finds the canonical path. If uri is a document uri returns path from a root and |
| 1528 | * its associated root id. If uri is a tree uri returns the path from the top of |
| 1529 | * the tree. The {@link Path#getPath()} of the return value contains document ID |
| 1530 | * starts from the top of the tree or the root document to the requested document, |
| 1531 | * both inclusive. |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 1532 | * |
Garfield Tan | b690b4d | 2017-03-01 16:05:23 -0800 | [diff] [blame] | 1533 | * Callers can expect the root ID returned from multiple calls to this method is |
| 1534 | * consistent. |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 1535 | * |
| 1536 | * @param uri uri of the document which path is requested. It can be either a |
| 1537 | * plain document uri or a tree uri. |
| 1538 | * @return the path of the document. |
Garfield Tan | 3f6b68a | 2016-11-01 14:13:38 -0700 | [diff] [blame] | 1539 | * @see DocumentsProvider#findDocumentPath(String, String) |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 1540 | * |
| 1541 | * {@hide} |
| 1542 | */ |
Garfield Tan | 0b3cf66 | 2016-10-31 12:59:45 -0700 | [diff] [blame] | 1543 | public static Path findDocumentPath(ContentProviderClient client, Uri uri) |
| 1544 | throws RemoteException { |
| 1545 | |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 1546 | final Bundle in = new Bundle(); |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 1547 | in.putParcelable(DocumentsContract.EXTRA_URI, uri); |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 1548 | |
Garfield Tan | 3f6b68a | 2016-11-01 14:13:38 -0700 | [diff] [blame] | 1549 | final Bundle out = client.call(METHOD_FIND_DOCUMENT_PATH, null, in); |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 1550 | |
| 1551 | return out.getParcelable(DocumentsContract.EXTRA_RESULT); |
| 1552 | } |
| 1553 | |
| 1554 | /** |
Tomasz Mikolajewski | cf31656 | 2016-10-24 15:17:01 +0900 | [diff] [blame] | 1555 | * Creates an intent for obtaining a web link for the specified document. |
| 1556 | * |
| 1557 | * <p>Note, that due to internal limitations, if there is already a web link |
| 1558 | * intent created for the specified document but with different options, |
| 1559 | * then it may be overriden. |
| 1560 | * |
| 1561 | * <p>Providers are required to show confirmation UI for all new permissions granted |
| 1562 | * for the linked document. |
| 1563 | * |
Tomasz Mikolajewski | 463b686 | 2017-03-22 17:34:05 +0900 | [diff] [blame] | 1564 | * <p>If list of recipients is known, then it should be passed in options as |
| 1565 | * {@link Intent#EXTRA_EMAIL} as a list of email addresses. Note, that |
Tomasz Mikolajewski | cf31656 | 2016-10-24 15:17:01 +0900 | [diff] [blame] | 1566 | * this is just a hint for the provider, which can ignore the list. In either |
| 1567 | * case the provider is required to show a UI for letting the user confirm |
| 1568 | * any new permission grants. |
| 1569 | * |
Tomasz Mikolajewski | 463b686 | 2017-03-22 17:34:05 +0900 | [diff] [blame] | 1570 | * <p>Note, that the entire <code>options</code> bundle will be sent to the provider |
| 1571 | * backing the passed <code>uri</code>. Make sure that you trust the provider |
| 1572 | * before passing any sensitive information. |
Tomasz Mikolajewski | f1d5540 | 2017-03-03 15:09:40 +0900 | [diff] [blame] | 1573 | * |
Tomasz Mikolajewski | cf31656 | 2016-10-24 15:17:01 +0900 | [diff] [blame] | 1574 | * <p>Since this API may show a UI, it cannot be called from background. |
| 1575 | * |
| 1576 | * <p>In order to obtain the Web Link use code like this: |
| 1577 | * <pre><code> |
| 1578 | * void onSomethingHappened() { |
| 1579 | * IntentSender sender = DocumentsContract.createWebLinkIntent(<i>...</i>); |
| 1580 | * if (sender != null) { |
| 1581 | * startIntentSenderForResult( |
Tomasz Mikolajewski | f1d5540 | 2017-03-03 15:09:40 +0900 | [diff] [blame] | 1582 | * sender, |
Tomasz Mikolajewski | cf31656 | 2016-10-24 15:17:01 +0900 | [diff] [blame] | 1583 | * WEB_LINK_REQUEST_CODE, |
| 1584 | * null, 0, 0, 0, null); |
| 1585 | * } |
| 1586 | * } |
| 1587 | * |
| 1588 | * <i>(...)</i> |
| 1589 | * |
| 1590 | * void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 1591 | * if (requestCode == WEB_LINK_REQUEST_CODE && resultCode == RESULT_OK) { |
| 1592 | * Uri weblinkUri = data.getData(); |
| 1593 | * <i>...</i> |
| 1594 | * } |
| 1595 | * } |
| 1596 | * </code></pre> |
| 1597 | * |
| 1598 | * @param uri uri for the document to create a link to. |
| 1599 | * @param options Extra information for generating the link. |
| 1600 | * @return an intent sender to obtain the web link, or null if the document |
| 1601 | * is not linkable, or creating the intent sender failed. |
| 1602 | * @see DocumentsProvider#createWebLinkIntent(String, Bundle) |
| 1603 | * @see Intent#EXTRA_EMAIL |
| 1604 | */ |
| 1605 | public static IntentSender createWebLinkIntent(ContentResolver resolver, Uri uri, |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1606 | Bundle options) throws FileNotFoundException { |
Tomasz Mikolajewski | cf31656 | 2016-10-24 15:17:01 +0900 | [diff] [blame] | 1607 | final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( |
| 1608 | uri.getAuthority()); |
| 1609 | try { |
| 1610 | return createWebLinkIntent(client, uri, options); |
| 1611 | } catch (Exception e) { |
| 1612 | Log.w(TAG, "Failed to create a web link intent", e); |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1613 | rethrowIfNecessary(resolver, e); |
Tomasz Mikolajewski | cf31656 | 2016-10-24 15:17:01 +0900 | [diff] [blame] | 1614 | return null; |
| 1615 | } finally { |
| 1616 | ContentProviderClient.releaseQuietly(client); |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | /** |
| 1621 | * {@hide} |
| 1622 | */ |
| 1623 | public static IntentSender createWebLinkIntent(ContentProviderClient client, Uri uri, |
| 1624 | Bundle options) throws RemoteException { |
| 1625 | final Bundle in = new Bundle(); |
| 1626 | in.putParcelable(DocumentsContract.EXTRA_URI, uri); |
| 1627 | |
| 1628 | // Options may be provider specific, so put them in a separate bundle to |
| 1629 | // avoid overriding the Uri. |
| 1630 | if (options != null) { |
| 1631 | in.putBundle(EXTRA_OPTIONS, options); |
| 1632 | } |
| 1633 | |
| 1634 | final Bundle out = client.call(METHOD_CREATE_WEB_LINK_INTENT, null, in); |
| 1635 | return out.getParcelable(DocumentsContract.EXTRA_RESULT); |
| 1636 | } |
| 1637 | |
| 1638 | /** |
Jeff Sharkey | c1c8f3f | 2013-10-14 14:57:33 -0700 | [diff] [blame] | 1639 | * Open the given image for thumbnail purposes, using any embedded EXIF |
| 1640 | * thumbnail if available, and providing orientation hints from the parent |
| 1641 | * image. |
| 1642 | * |
| 1643 | * @hide |
| 1644 | */ |
| 1645 | public static AssetFileDescriptor openImageThumbnail(File file) throws FileNotFoundException { |
| 1646 | final ParcelFileDescriptor pfd = ParcelFileDescriptor.open( |
| 1647 | file, ParcelFileDescriptor.MODE_READ_ONLY); |
| 1648 | Bundle extras = null; |
| 1649 | |
| 1650 | try { |
| 1651 | final ExifInterface exif = new ExifInterface(file.getAbsolutePath()); |
| 1652 | |
| 1653 | switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1)) { |
| 1654 | case ExifInterface.ORIENTATION_ROTATE_90: |
| 1655 | extras = new Bundle(1); |
| 1656 | extras.putInt(EXTRA_ORIENTATION, 90); |
| 1657 | break; |
| 1658 | case ExifInterface.ORIENTATION_ROTATE_180: |
| 1659 | extras = new Bundle(1); |
| 1660 | extras.putInt(EXTRA_ORIENTATION, 180); |
| 1661 | break; |
| 1662 | case ExifInterface.ORIENTATION_ROTATE_270: |
| 1663 | extras = new Bundle(1); |
| 1664 | extras.putInt(EXTRA_ORIENTATION, 270); |
| 1665 | break; |
| 1666 | } |
| 1667 | |
| 1668 | final long[] thumb = exif.getThumbnailRange(); |
| 1669 | if (thumb != null) { |
| 1670 | return new AssetFileDescriptor(pfd, thumb[0], thumb[1], extras); |
| 1671 | } |
| 1672 | } catch (IOException e) { |
| 1673 | } |
| 1674 | |
| 1675 | return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH, extras); |
| 1676 | } |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 1677 | |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1678 | private static void rethrowIfNecessary(ContentResolver resolver, Exception e) |
| 1679 | throws FileNotFoundException { |
| 1680 | // We only want to throw applications targetting O and above |
| 1681 | if (resolver.getTargetSdkVersion() >= Build.VERSION_CODES.O) { |
| 1682 | if (e instanceof ParcelableException) { |
| 1683 | ((ParcelableException) e).maybeRethrow(FileNotFoundException.class); |
Ben Lin | 227e6e8 | 2017-03-13 15:36:13 -0700 | [diff] [blame] | 1684 | } else if (e instanceof RemoteException) { |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 1685 | ((RemoteException) e).rethrowAsRuntimeException(); |
| 1686 | } else if (e instanceof RuntimeException) { |
| 1687 | throw (RuntimeException) e; |
| 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 1692 | /** |
Garfield Tan | 0b3cf66 | 2016-10-31 12:59:45 -0700 | [diff] [blame] | 1693 | * Holds a path from a document to a particular document under it. It |
| 1694 | * may also contains the root ID where the path resides. |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 1695 | */ |
| 1696 | public static final class Path implements Parcelable { |
| 1697 | |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 1698 | private final @Nullable String mRootId; |
| 1699 | private final List<String> mPath; |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 1700 | |
| 1701 | /** |
| 1702 | * Creates a Path. |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 1703 | * |
| 1704 | * @param rootId the ID of the root. May be null. |
Garfield Tan | 0b3cf66 | 2016-10-31 12:59:45 -0700 | [diff] [blame] | 1705 | * @param path the list of document ID from the parent document at |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 1706 | * position 0 to the child document. |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 1707 | */ |
Garfield Tan | 5f21480 | 2016-10-26 14:52:46 -0700 | [diff] [blame] | 1708 | public Path(@Nullable String rootId, List<String> path) { |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 1709 | checkCollectionNotEmpty(path, "path"); |
| 1710 | checkCollectionElementsNotNull(path, "path"); |
| 1711 | |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 1712 | mRootId = rootId; |
| 1713 | mPath = path; |
| 1714 | } |
| 1715 | |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 1716 | /** |
| 1717 | * Returns the root id or null if the calling package doesn't have |
| 1718 | * permission to access root information. |
| 1719 | */ |
| 1720 | public @Nullable String getRootId() { |
| 1721 | return mRootId; |
| 1722 | } |
| 1723 | |
| 1724 | /** |
| 1725 | * Returns the path. The path is trimmed to the top of tree if |
| 1726 | * calling package doesn't have permission to access those |
| 1727 | * documents. |
| 1728 | */ |
| 1729 | public List<String> getPath() { |
| 1730 | return mPath; |
| 1731 | } |
| 1732 | |
| 1733 | @Override |
| 1734 | public boolean equals(Object o) { |
| 1735 | if (this == o) { |
| 1736 | return true; |
| 1737 | } |
| 1738 | if (o == null || !(o instanceof Path)) { |
| 1739 | return false; |
| 1740 | } |
| 1741 | Path path = (Path) o; |
| 1742 | return Objects.equals(mRootId, path.mRootId) && |
| 1743 | Objects.equals(mPath, path.mPath); |
| 1744 | } |
| 1745 | |
| 1746 | @Override |
| 1747 | public int hashCode() { |
| 1748 | return Objects.hash(mRootId, mPath); |
| 1749 | } |
| 1750 | |
| 1751 | @Override |
| 1752 | public String toString() { |
| 1753 | return new StringBuilder() |
| 1754 | .append("DocumentsContract.Path{") |
| 1755 | .append("rootId=") |
| 1756 | .append(mRootId) |
| 1757 | .append(", path=") |
| 1758 | .append(mPath) |
| 1759 | .append("}") |
| 1760 | .toString(); |
| 1761 | } |
| 1762 | |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 1763 | @Override |
| 1764 | public void writeToParcel(Parcel dest, int flags) { |
| 1765 | dest.writeString(mRootId); |
| 1766 | dest.writeStringList(mPath); |
| 1767 | } |
| 1768 | |
| 1769 | @Override |
| 1770 | public int describeContents() { |
| 1771 | return 0; |
| 1772 | } |
| 1773 | |
| 1774 | public static final Creator<Path> CREATOR = new Creator<Path>() { |
| 1775 | @Override |
| 1776 | public Path createFromParcel(Parcel in) { |
| 1777 | final String rootId = in.readString(); |
| 1778 | final List<String> path = in.createStringArrayList(); |
| 1779 | return new Path(rootId, path); |
| 1780 | } |
| 1781 | |
| 1782 | @Override |
| 1783 | public Path[] newArray(int size) { |
| 1784 | return new Path[size]; |
| 1785 | } |
| 1786 | }; |
| 1787 | } |
Jeff Sharkey | 9ecfee0 | 2013-04-19 14:05:03 -0700 | [diff] [blame] | 1788 | } |