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