blob: 441245956b8d9ae3dd37ca63de7728ae74b193f8 [file] [log] [blame]
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.provider;
18
Jeff Sharkey63983432013-08-21 11:33:50 -070019import static android.net.TrafficStats.KB_IN_BYTES;
Elliott Hughes34385d32014-04-28 11:11:32 -070020import static android.system.OsConstants.SEEK_SET;
Jeff Sharkey63983432013-08-21 11:33:50 -070021
Steve McKay323ee3e2015-09-25 16:02:56 -070022import android.annotation.Nullable;
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -070023import android.content.ContentProviderClient;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070024import android.content.ContentResolver;
Jeff Sharkeyee2f7df2013-09-26 11:32:30 -070025import android.content.Context;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070026import android.content.Intent;
Jeff Sharkeyd2e1e812013-10-09 13:31:13 -070027import android.content.pm.ResolveInfo;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070028import android.content.res.AssetFileDescriptor;
Jeff Sharkey20d96d82013-07-30 17:08:39 -070029import android.database.Cursor;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070030import android.graphics.Bitmap;
31import android.graphics.BitmapFactory;
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -070032import android.graphics.Matrix;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070033import android.graphics.Point;
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -070034import android.media.ExifInterface;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070035import android.net.Uri;
36import android.os.Bundle;
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070037import android.os.CancellationSignal;
Jeff Sharkey33819312013-10-29 11:56:37 -070038import android.os.OperationCanceledException;
Jeff Sharkeybd3b9022013-08-20 15:20:04 -070039import android.os.ParcelFileDescriptor;
40import android.os.ParcelFileDescriptor.OnCloseListener;
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -070041import android.os.RemoteException;
Felipe Leme04a5d402016-02-08 16:44:06 -080042import android.os.storage.StorageVolume;
Elliott Hughes34385d32014-04-28 11:11:32 -070043import android.system.ErrnoException;
44import android.system.Os;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070045import android.util.Log;
46
47import libcore.io.IoUtils;
48
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -070049import java.io.BufferedInputStream;
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -070050import java.io.File;
Jeff Sharkey9d0843d2013-05-07 12:41:33 -070051import java.io.FileDescriptor;
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -070052import java.io.FileInputStream;
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -070053import java.io.FileNotFoundException;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070054import java.io.IOException;
Jeff Sharkeydc2963a2013-08-02 15:55:26 -070055import java.util.List;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070056
57/**
Jeff Sharkeybd3b9022013-08-20 15:20:04 -070058 * Defines the contract between a documents provider and the platform.
59 * <p>
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -070060 * To create a document provider, extend {@link DocumentsProvider}, which
61 * provides a foundational implementation of this contract.
Jeff Sharkey21de56a2014-04-05 19:05:24 -070062 * <p>
63 * All client apps must hold a valid URI permission grant to access documents,
64 * typically issued when a user makes a selection through
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -070065 * {@link Intent#ACTION_OPEN_DOCUMENT}, {@link Intent#ACTION_CREATE_DOCUMENT},
Felipe Leme04a5d402016-02-08 16:44:06 -080066 * {@link Intent#ACTION_OPEN_DOCUMENT_TREE}, or
67 * {@link StorageVolume#createAccessIntent(String) StorageVolume.createAccessIntent}.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -070068 *
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -070069 * @see DocumentsProvider
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070070 */
71public final class DocumentsContract {
Steve McKayd3afdee2015-11-19 17:27:12 -080072 private static final String TAG = "DocumentsContract";
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070073
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070074 // content://com.example/root/
75 // content://com.example/root/sdcard/
76 // content://com.example/root/sdcard/recent/
Jeff Sharkey3e1189b2013-09-12 21:59:06 -070077 // content://com.example/root/sdcard/search/?query=pony
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070078 // content://com.example/document/12/
79 // content://com.example/document/12/children/
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -070080 // content://com.example/tree/12/document/24/
81 // content://com.example/tree/12/document/24/children/
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -070082
83 private DocumentsContract() {
84 }
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070085
Jeff Sharkey85f5f812013-10-07 10:16:12 -070086 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -070087 * Intent action used to identify {@link DocumentsProvider} instances. This
88 * is used in the {@code <intent-filter>} of a {@code <provider>}.
Jeff Sharkey85f5f812013-10-07 10:16:12 -070089 */
90 public static final String PROVIDER_INTERFACE = "android.content.action.DOCUMENTS_PROVIDER";
91
Jeff Sharkey5b83f852013-08-14 18:29:19 -070092 /** {@hide} */
Jeff Sharkey15be8362013-10-09 13:52:17 -070093 public static final String EXTRA_PACKAGE_NAME = "android.content.extra.PACKAGE_NAME";
94
Jeff Sharkey96c62052013-10-25 16:30:54 -070095 /** {@hide} */
Aga Wronska1719b352016-03-21 11:28:03 -070096 public static final String EXTRA_SHOW_ADVANCED = "android.content.extra.SHOW_ADVANCED";
97
98 /** {@hide} */
Steve McKay83df8c02015-09-16 15:07:31 -070099 public static final String EXTRA_SHOW_FILESIZE = "android.content.extra.SHOW_FILESIZE";
100
101 /** {@hide} */
Tomasz Mikolajewski74fe1812015-06-12 17:13:26 -0700102 public static final String EXTRA_TARGET_URI = "android.content.extra.TARGET_URI";
103
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -0700104 /**
Ben Kwa77797402015-05-29 15:40:31 -0700105 * Set this in a DocumentsUI intent to cause a package's own roots to be
106 * excluded from the roots list.
107 */
108 public static final String EXTRA_EXCLUDE_SELF = "android.provider.extra.EXCLUDE_SELF";
109
110 /**
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -0700111 * Included in {@link AssetFileDescriptor#getExtras()} when returned
112 * thumbnail should be rotated.
113 *
114 * @see MediaStore.Images.ImageColumns#ORIENTATION
115 * @hide
116 */
117 public static final String EXTRA_ORIENTATION = "android.content.extra.ORIENTATION";
118
Tomasz Mikolajewski0e591f92015-06-12 16:22:17 -0700119 /**
120 * Overrides the default prompt text in DocumentsUI when set in an intent.
121 */
122 public static final String EXTRA_PROMPT = "android.provider.extra.PROMPT";
123
Jeff Sharkey15be8362013-10-09 13:52:17 -0700124 /** {@hide} */
Jeff Sharkeya61dc8e2013-09-05 17:14:14 -0700125 public static final String ACTION_MANAGE_DOCUMENT = "android.provider.action.MANAGE_DOCUMENT";
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700126
Jeff Sharkey59d577a2015-04-11 21:27:21 -0700127 /** {@hide} */
Ben Kwa84cebbe2015-09-25 14:48:29 -0700128 public static final String ACTION_BROWSE = "android.provider.action.BROWSE";
Jeff Sharkey1407d4c2015-04-12 21:52:24 -0700129
130 /** {@hide} */
131 public static final String
132 ACTION_DOCUMENT_ROOT_SETTINGS = "android.provider.action.DOCUMENT_ROOT_SETTINGS";
Jeff Sharkey59d577a2015-04-11 21:27:21 -0700133
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700134 /**
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -0700135 * Buffer is large enough to rewind past any EXIF headers.
136 */
137 private static final int THUMBNAIL_BUFFER_SIZE = (int) (128 * KB_IN_BYTES);
138
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -0700139 /** {@hide} */
140 public static final String PACKAGE_DOCUMENTS_UI = "com.android.documentsui";
141
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -0700142 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700143 * Constants related to a document, including {@link Cursor} column names
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700144 * and flags.
145 * <p>
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700146 * A document can be either an openable stream (with a specific MIME type),
147 * or a directory containing additional documents (with the
148 * {@link #MIME_TYPE_DIR} MIME type). A directory represents the top of a
149 * subtree containing zero or more documents, which can recursively contain
150 * even more documents and directories.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700151 * <p>
152 * All columns are <em>read-only</em> to client applications.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700153 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700154 public final static class Document {
155 private Document() {
Jeff Sharkeya5599ef2013-08-15 16:17:41 -0700156 }
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700157
Jeff Sharkeya5599ef2013-08-15 16:17:41 -0700158 /**
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700159 * Unique ID of a document. This ID is both provided by and interpreted
160 * by a {@link DocumentsProvider}, and should be treated as an opaque
Jeff Sharkey6efba222013-09-27 16:44:11 -0700161 * value by client applications. This column is required.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700162 * <p>
163 * Each document must have a unique ID within a provider, but that
164 * single document may be included as a child of multiple directories.
165 * <p>
166 * A provider must always return durable IDs, since they will be used to
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700167 * issue long-term URI permission grants when an application interacts
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700168 * with {@link Intent#ACTION_OPEN_DOCUMENT} and
169 * {@link Intent#ACTION_CREATE_DOCUMENT}.
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700170 * <p>
171 * Type: STRING
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700172 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700173 public static final String COLUMN_DOCUMENT_ID = "document_id";
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700174
175 /**
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700176 * Concrete MIME type of a document. For example, "image/png" or
177 * "application/pdf" for openable files. A document can also be a
178 * directory containing additional documents, which is represented with
Jeff Sharkey6efba222013-09-27 16:44:11 -0700179 * the {@link #MIME_TYPE_DIR} MIME type. This column is required.
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700180 * <p>
181 * Type: STRING
182 *
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700183 * @see #MIME_TYPE_DIR
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700184 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700185 public static final String COLUMN_MIME_TYPE = "mime_type";
186
187 /**
188 * Display name of a document, used as the primary title displayed to a
Jeff Sharkey6efba222013-09-27 16:44:11 -0700189 * user. This column is required.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700190 * <p>
191 * Type: STRING
192 */
193 public static final String COLUMN_DISPLAY_NAME = OpenableColumns.DISPLAY_NAME;
194
195 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700196 * Summary of a document, which may be shown to a user. This column is
197 * optional, and may be {@code null}.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700198 * <p>
199 * Type: STRING
200 */
201 public static final String COLUMN_SUMMARY = "summary";
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700202
203 /**
204 * Timestamp when a document was last modified, in milliseconds since
Jeff Sharkey6efba222013-09-27 16:44:11 -0700205 * January 1, 1970 00:00:00.0 UTC. This column is required, and may be
206 * {@code null} if unknown. A {@link DocumentsProvider} can update this
207 * field using events from {@link OnCloseListener} or other reliable
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700208 * {@link ParcelFileDescriptor} transports.
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700209 * <p>
210 * Type: INTEGER (long)
211 *
212 * @see System#currentTimeMillis()
213 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700214 public static final String COLUMN_LAST_MODIFIED = "last_modified";
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700215
216 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700217 * Specific icon resource ID for a document. This column is optional,
218 * and may be {@code null} to use a platform-provided default icon based
219 * on {@link #COLUMN_MIME_TYPE}.
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700220 * <p>
221 * Type: INTEGER (int)
222 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700223 public static final String COLUMN_ICON = "icon";
Jeff Sharkey66516692013-08-06 11:26:10 -0700224
225 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700226 * Flags that apply to a document. This column is required.
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700227 * <p>
228 * Type: INTEGER (int)
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700229 *
230 * @see #FLAG_SUPPORTS_WRITE
231 * @see #FLAG_SUPPORTS_DELETE
232 * @see #FLAG_SUPPORTS_THUMBNAIL
233 * @see #FLAG_DIR_PREFERS_GRID
Jeff Sharkey6efba222013-09-27 16:44:11 -0700234 * @see #FLAG_DIR_PREFERS_LAST_MODIFIED
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900235 * @see #FLAG_VIRTUAL_DOCUMENT
Tomasz Mikolajewskia4c338a2015-11-25 12:18:55 +0900236 * @see #FLAG_ARCHIVE
Tomasz Mikolajewskicbcd3942016-01-28 12:39:25 +0900237 * @see #FLAG_SUPPORTS_COPY
238 * @see #FLAG_SUPPORTS_MOVE
239 * @see #FLAG_SUPPORTS_REMOVE
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700240 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700241 public static final String COLUMN_FLAGS = "flags";
242
243 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700244 * Size of a document, in bytes, or {@code null} if unknown. This column
245 * is required.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700246 * <p>
247 * Type: INTEGER (long)
248 */
249 public static final String COLUMN_SIZE = OpenableColumns.SIZE;
250
251 /**
252 * MIME type of a document which is a directory that may contain
253 * additional documents.
254 *
255 * @see #COLUMN_MIME_TYPE
256 */
257 public static final String MIME_TYPE_DIR = "vnd.android.document/directory";
258
259 /**
260 * Flag indicating that a document can be represented as a thumbnail.
261 *
262 * @see #COLUMN_FLAGS
263 * @see DocumentsContract#getDocumentThumbnail(ContentResolver, Uri,
264 * Point, CancellationSignal)
265 * @see DocumentsProvider#openDocumentThumbnail(String, Point,
266 * android.os.CancellationSignal)
267 */
268 public static final int FLAG_SUPPORTS_THUMBNAIL = 1;
269
270 /**
271 * Flag indicating that a document supports writing.
272 * <p>
273 * When a document is opened with {@link Intent#ACTION_OPEN_DOCUMENT},
274 * the calling application is granted both
275 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION} and
276 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION}. However, the actual
277 * writability of a document may change over time, for example due to
278 * remote access changes. This flag indicates that a document client can
279 * expect {@link ContentResolver#openOutputStream(Uri)} to succeed.
Steve McKay83df8c02015-09-16 15:07:31 -0700280 *
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700281 * @see #COLUMN_FLAGS
282 */
283 public static final int FLAG_SUPPORTS_WRITE = 1 << 1;
284
285 /**
286 * Flag indicating that a document is deletable.
287 *
288 * @see #COLUMN_FLAGS
289 * @see DocumentsContract#deleteDocument(ContentResolver, Uri)
290 * @see DocumentsProvider#deleteDocument(String)
291 */
292 public static final int FLAG_SUPPORTS_DELETE = 1 << 2;
293
294 /**
295 * Flag indicating that a document is a directory that supports creation
296 * of new files within it. Only valid when {@link #COLUMN_MIME_TYPE} is
297 * {@link #MIME_TYPE_DIR}.
298 *
299 * @see #COLUMN_FLAGS
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700300 * @see DocumentsProvider#createDocument(String, String, String)
301 */
302 public static final int FLAG_DIR_SUPPORTS_CREATE = 1 << 3;
303
304 /**
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700305 * Flag indicating that a directory prefers its contents be shown in a
306 * larger format grid. Usually suitable when a directory contains mostly
307 * pictures. Only valid when {@link #COLUMN_MIME_TYPE} is
308 * {@link #MIME_TYPE_DIR}.
309 *
310 * @see #COLUMN_FLAGS
311 */
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700312 public static final int FLAG_DIR_PREFERS_GRID = 1 << 4;
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700313
314 /**
315 * Flag indicating that a directory prefers its contents be sorted by
316 * {@link #COLUMN_LAST_MODIFIED}. Only valid when
317 * {@link #COLUMN_MIME_TYPE} is {@link #MIME_TYPE_DIR}.
318 *
319 * @see #COLUMN_FLAGS
320 */
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700321 public static final int FLAG_DIR_PREFERS_LAST_MODIFIED = 1 << 5;
Jeff Sharkeyf6db1542013-09-13 13:42:19 -0700322
323 /**
Jeff Sharkeyb7e12552014-05-21 22:22:03 -0700324 * Flag indicating that a document can be renamed.
325 *
326 * @see #COLUMN_FLAGS
327 * @see DocumentsContract#renameDocument(ContentProviderClient, Uri,
328 * String)
329 * @see DocumentsProvider#renameDocument(String, String)
330 */
331 public static final int FLAG_SUPPORTS_RENAME = 1 << 6;
332
333 /**
Tomasz Mikolajewski74fe1812015-06-12 17:13:26 -0700334 * Flag indicating that a document can be copied to another location
335 * within the same document provider.
336 *
337 * @see #COLUMN_FLAGS
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +0900338 * @see DocumentsContract#copyDocument(ContentProviderClient, Uri, Uri)
339 * @see DocumentsProvider#copyDocument(String, String)
Tomasz Mikolajewski74fe1812015-06-12 17:13:26 -0700340 */
341 public static final int FLAG_SUPPORTS_COPY = 1 << 7;
342
343 /**
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +0900344 * Flag indicating that a document can be moved to another location
345 * within the same document provider.
346 *
347 * @see #COLUMN_FLAGS
Tomasz Mikolajewskid46ecbc2016-01-25 14:26:54 +0900348 * @see DocumentsContract#moveDocument(ContentProviderClient, Uri, Uri, Uri)
349 * @see DocumentsProvider#moveDocument(String, String, String)
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +0900350 */
351 public static final int FLAG_SUPPORTS_MOVE = 1 << 8;
352
353 /**
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900354 * Flag indicating that a document is virtual, and doesn't have byte
355 * representation in the MIME type specified as {@link #COLUMN_MIME_TYPE}.
356 *
357 * @see #COLUMN_FLAGS
358 * @see #COLUMN_MIME_TYPE
359 * @see DocumentsProvider#openTypedDocument(String, String, Bundle,
360 * android.os.CancellationSignal)
Tomasz Mikolajewskid99964f2016-02-15 11:16:32 +0900361 * @see DocumentsProvider#getDocumentStreamTypes(String, String)
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900362 */
Tomasz Mikolajewski75395652016-01-07 07:19:22 +0000363 public static final int FLAG_VIRTUAL_DOCUMENT = 1 << 9;
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900364
365 /**
Tomasz Mikolajewski9b055e12016-02-01 13:01:34 +0900366 * Flag indicating that a document can be removed from a parent.
367 *
368 * @see #COLUMN_FLAGS
369 * @see DocumentsContract#removeDocument(ContentProviderClient, Uri, Uri)
370 * @see DocumentsProvider#removeDocument(String, String)
371 */
372 public static final int FLAG_SUPPORTS_REMOVE = 1 << 10;
373
374 /**
Tomasz Mikolajewskia4c338a2015-11-25 12:18:55 +0900375 * Flag indicating that a document is an archive, and it's contents can be
376 * obtained via {@link DocumentsProvider#queryChildDocuments}.
377 * <p>
378 * The <em>provider</em> support library offers utility classes to add common
379 * archive support.
380 *
381 * @see #COLUMN_FLAGS
382 * @see DocumentsProvider#queryChildDocuments(String, String[], String)
Tomasz Mikolajewski9b055e12016-02-01 13:01:34 +0900383 * @hide
Tomasz Mikolajewskia4c338a2015-11-25 12:18:55 +0900384 */
Tomasz Mikolajewski9b055e12016-02-01 13:01:34 +0900385 public static final int FLAG_ARCHIVE = 1 << 15;
Steve McKay168e4642016-03-14 13:02:56 -0700386
387 /**
388 * Flag indicating that a document is not complete, likely its
389 * contents are being downloaded. Partial files cannot be opened,
390 * copied, moved in the UI. But they can be deleted and retried
391 * if they represent a failed download.
392 *
393 * @see #COLUMN_FLAGS
394 * @hide
395 */
396 public static final int FLAG_PARTIAL = 1 << 16;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700397 }
398
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700399 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700400 * Constants related to a root of documents, including {@link Cursor} column
401 * names and flags. A root is the start of a tree of documents, such as a
402 * physical storage device, or an account. Each root starts at the directory
403 * referenced by {@link Root#COLUMN_DOCUMENT_ID}, which can recursively
404 * contain both documents and directories.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700405 * <p>
406 * All columns are <em>read-only</em> to client applications.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700407 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700408 public final static class Root {
409 private Root() {
410 }
411
Jeff Sharkeya5599ef2013-08-15 16:17:41 -0700412 /**
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700413 * Unique ID of a root. This ID is both provided by and interpreted by a
414 * {@link DocumentsProvider}, and should be treated as an opaque value
Jeff Sharkey6efba222013-09-27 16:44:11 -0700415 * by client applications. This column is required.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700416 * <p>
417 * Type: STRING
418 */
419 public static final String COLUMN_ROOT_ID = "root_id";
420
421 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700422 * Flags that apply to a root. This column is required.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700423 * <p>
424 * Type: INTEGER (int)
425 *
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700426 * @see #FLAG_LOCAL_ONLY
427 * @see #FLAG_SUPPORTS_CREATE
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700428 * @see #FLAG_SUPPORTS_RECENTS
429 * @see #FLAG_SUPPORTS_SEARCH
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700430 */
431 public static final String COLUMN_FLAGS = "flags";
432
433 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700434 * Icon resource ID for a root. This column is required.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700435 * <p>
436 * Type: INTEGER (int)
437 */
438 public static final String COLUMN_ICON = "icon";
439
440 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700441 * Title for a root, which will be shown to a user. This column is
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700442 * required. For a single storage service surfacing multiple accounts as
443 * different roots, this title should be the name of the service.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700444 * <p>
445 * Type: STRING
446 */
447 public static final String COLUMN_TITLE = "title";
448
449 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700450 * Summary for this root, which may be shown to a user. This column is
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700451 * optional, and may be {@code null}. For a single storage service
452 * surfacing multiple accounts as different roots, this summary should
453 * be the name of the account.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700454 * <p>
455 * Type: STRING
456 */
457 public static final String COLUMN_SUMMARY = "summary";
458
459 /**
460 * Document which is a directory that represents the top directory of
Jeff Sharkey6efba222013-09-27 16:44:11 -0700461 * this root. This column is required.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700462 * <p>
463 * Type: STRING
464 *
465 * @see Document#COLUMN_DOCUMENT_ID
466 */
467 public static final String COLUMN_DOCUMENT_ID = "document_id";
468
469 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700470 * Number of bytes available in this root. This column is optional, and
471 * may be {@code null} if unknown or unbounded.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700472 * <p>
473 * Type: INTEGER (long)
474 */
475 public static final String COLUMN_AVAILABLE_BYTES = "available_bytes";
476
477 /**
Tomasz Mikolajewski3f78e172015-06-25 16:17:26 +0900478 * Capacity of a root in bytes. This column is optional, and may be
479 * {@code null} if unknown or unbounded.
Tomasz Mikolajewski3f78e172015-06-25 16:17:26 +0900480 * <p>
481 * Type: INTEGER (long)
482 */
483 public static final String COLUMN_CAPACITY_BYTES = "capacity_bytes";
484
485 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700486 * MIME types supported by this root. This column is optional, and if
487 * {@code null} the root is assumed to support all MIME types. Multiple
488 * MIME types can be separated by a newline. For example, a root
489 * supporting audio might return "audio/*\napplication/x-flac".
Jeff Sharkey923396b2013-09-05 13:55:35 -0700490 * <p>
Jeff Sharkey6efba222013-09-27 16:44:11 -0700491 * Type: STRING
Jeff Sharkey923396b2013-09-05 13:55:35 -0700492 */
493 public static final String COLUMN_MIME_TYPES = "mime_types";
494
Jeff Sharkeya61dc8e2013-09-05 17:14:14 -0700495 /** {@hide} */
496 public static final String MIME_TYPE_ITEM = "vnd.android.document/root";
497
Jeff Sharkey923396b2013-09-05 13:55:35 -0700498 /**
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700499 * Flag indicating that at least one directory under this root supports
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700500 * creating content. Roots with this flag will be shown when an
501 * application interacts with {@link Intent#ACTION_CREATE_DOCUMENT}.
Jeff Sharkey20d96d82013-07-30 17:08:39 -0700502 *
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700503 * @see #COLUMN_FLAGS
Jeff Sharkey20d96d82013-07-30 17:08:39 -0700504 */
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700505 public static final int FLAG_SUPPORTS_CREATE = 1;
Jeff Sharkey20d96d82013-07-30 17:08:39 -0700506
507 /**
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700508 * Flag indicating that this root offers content that is strictly local
509 * on the device. That is, no network requests are made for the content.
510 *
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700511 * @see #COLUMN_FLAGS
512 * @see Intent#EXTRA_LOCAL_ONLY
Jeff Sharkey20d96d82013-07-30 17:08:39 -0700513 */
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700514 public static final int FLAG_LOCAL_ONLY = 1 << 1;
515
Jeff Sharkey20d96d82013-07-30 17:08:39 -0700516 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700517 * Flag indicating that this root can be queried to provide recently
518 * modified documents.
Jeff Sharkey251097b2013-09-02 15:07:28 -0700519 *
520 * @see #COLUMN_FLAGS
521 * @see DocumentsContract#buildRecentDocumentsUri(String, String)
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700522 * @see DocumentsProvider#queryRecentDocuments(String, String[])
Jeff Sharkey251097b2013-09-02 15:07:28 -0700523 */
Jeff Sharkey6efba222013-09-27 16:44:11 -0700524 public static final int FLAG_SUPPORTS_RECENTS = 1 << 2;
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700525
526 /**
527 * Flag indicating that this root supports search.
528 *
529 * @see #COLUMN_FLAGS
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700530 * @see DocumentsContract#buildSearchDocumentsUri(String, String,
531 * String)
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700532 * @see DocumentsProvider#querySearchDocuments(String, String,
533 * String[])
534 */
Jeff Sharkey6efba222013-09-27 16:44:11 -0700535 public static final int FLAG_SUPPORTS_SEARCH = 1 << 3;
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700536
537 /**
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700538 * Flag indicating that this root supports testing parent child
539 * relationships.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700540 *
541 * @see #COLUMN_FLAGS
542 * @see DocumentsProvider#isChildDocument(String, String)
543 */
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700544 public static final int FLAG_SUPPORTS_IS_CHILD = 1 << 4;
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700545
546 /**
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700547 * Flag indicating that this root is currently empty. This may be used
548 * to hide the root when opening documents, but the root will still be
549 * shown when creating documents and {@link #FLAG_SUPPORTS_CREATE} is
Jeff Sharkey6efba222013-09-27 16:44:11 -0700550 * also set. If the value of this flag changes, such as when a root
551 * becomes non-empty, you must send a content changed notification for
552 * {@link DocumentsContract#buildRootsUri(String)}.
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700553 *
554 * @see #COLUMN_FLAGS
Jeff Sharkey6efba222013-09-27 16:44:11 -0700555 * @see ContentResolver#notifyChange(Uri,
556 * android.database.ContentObserver, boolean)
557 * @hide
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700558 */
Jeff Sharkey6efba222013-09-27 16:44:11 -0700559 public static final int FLAG_EMPTY = 1 << 16;
560
561 /**
Aga Wronska1719b352016-03-21 11:28:03 -0700562 * Flag indicating that this root should only be visible to advanced
563 * users.
564 *
565 * @see #COLUMN_FLAGS
566 * @hide
567 */
568 public static final int FLAG_ADVANCED = 1 << 17;
569
570 /**
Jeff Sharkey1407d4c2015-04-12 21:52:24 -0700571 * Flag indicating that this root has settings.
572 *
573 * @see #COLUMN_FLAGS
574 * @see DocumentsContract#ACTION_DOCUMENT_ROOT_SETTINGS
575 * @hide
576 */
Aga Wronska1719b352016-03-21 11:28:03 -0700577 public static final int FLAG_HAS_SETTINGS = 1 << 18;
Steve McKayba23e542016-03-02 15:15:00 -0800578
579 /**
580 * Flag indicating that this root is on removable SD card storage.
581 *
582 * @see #COLUMN_FLAGS
583 * @hide
584 */
Aga Wronska1719b352016-03-21 11:28:03 -0700585 public static final int FLAG_REMOVABLE_SD = 1 << 19;
Steve McKayba23e542016-03-02 15:15:00 -0800586
587 /**
588 * Flag indicating that this root is on removable USB storage.
589 *
590 * @see #COLUMN_FLAGS
591 * @hide
592 */
Aga Wronska1719b352016-03-21 11:28:03 -0700593 public static final int FLAG_REMOVABLE_USB = 1 << 20;
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700594 }
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700595
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700596 /**
597 * Optional boolean flag included in a directory {@link Cursor#getExtras()}
598 * indicating that a document provider is still loading data. For example, a
599 * provider has returned some results, but is still waiting on an
600 * outstanding network request. The provider must send a content changed
601 * notification when loading is finished.
602 *
603 * @see ContentResolver#notifyChange(Uri, android.database.ContentObserver,
604 * boolean)
605 */
606 public static final String EXTRA_LOADING = "loading";
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700607
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700608 /**
609 * Optional string included in a directory {@link Cursor#getExtras()}
610 * providing an informational message that should be shown to a user. For
611 * example, a provider may wish to indicate that not all documents are
612 * available.
613 */
614 public static final String EXTRA_INFO = "info";
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700615
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700616 /**
617 * Optional string included in a directory {@link Cursor#getExtras()}
618 * providing an error message that should be shown to a user. For example, a
619 * provider may wish to indicate that a network error occurred. The user may
620 * choose to retry, resulting in a new query.
621 */
622 public static final String EXTRA_ERROR = "error";
623
Steve McKayd3afdee2015-11-19 17:27:12 -0800624 /**
625 * Optional result (I'm thinking boolean) answer to a question.
626 * {@hide}
627 */
628 public static final String EXTRA_RESULT = "result";
629
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700630 /** {@hide} */
631 public static final String METHOD_CREATE_DOCUMENT = "android:createDocument";
632 /** {@hide} */
Jeff Sharkeyb7e12552014-05-21 22:22:03 -0700633 public static final String METHOD_RENAME_DOCUMENT = "android:renameDocument";
634 /** {@hide} */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700635 public static final String METHOD_DELETE_DOCUMENT = "android:deleteDocument";
Tomasz Mikolajewski74fe1812015-06-12 17:13:26 -0700636 /** {@hide} */
637 public static final String METHOD_COPY_DOCUMENT = "android:copyDocument";
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +0900638 /** {@hide} */
639 public static final String METHOD_MOVE_DOCUMENT = "android:moveDocument";
Steve McKayd3afdee2015-11-19 17:27:12 -0800640 /** {@hide} */
641 public static final String METHOD_IS_CHILD_DOCUMENT = "android:isChildDocument";
Tomasz Mikolajewskicbcd3942016-01-28 12:39:25 +0900642 /** {@hide} */
643 public static final String METHOD_REMOVE_DOCUMENT = "android:removeDocument";
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700644
645 /** {@hide} */
Tomasz Mikolajewskid46ecbc2016-01-25 14:26:54 +0900646 public static final String EXTRA_PARENT_URI = "parentUri";
647 /** {@hide} */
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700648 public static final String EXTRA_URI = "uri";
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700649
650 private static final String PATH_ROOT = "root";
651 private static final String PATH_RECENT = "recent";
652 private static final String PATH_DOCUMENT = "document";
653 private static final String PATH_CHILDREN = "children";
654 private static final String PATH_SEARCH = "search";
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700655 private static final String PATH_TREE = "tree";
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700656
657 private static final String PARAM_QUERY = "query";
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700658 private static final String PARAM_MANAGE = "manage";
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700659
660 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700661 * Build URI representing the roots of a document provider. When queried, a
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700662 * provider will return one or more rows with columns defined by
663 * {@link Root}.
664 *
665 * @see DocumentsProvider#queryRoots(String[])
666 */
667 public static Uri buildRootsUri(String authority) {
668 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
669 .authority(authority).appendPath(PATH_ROOT).build();
670 }
671
672 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700673 * Build URI representing the given {@link Root#COLUMN_ROOT_ID} in a
Jeff Sharkeya61dc8e2013-09-05 17:14:14 -0700674 * document provider.
675 *
676 * @see #getRootId(Uri)
677 */
678 public static Uri buildRootUri(String authority, String rootId) {
679 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
680 .authority(authority).appendPath(PATH_ROOT).appendPath(rootId).build();
681 }
682
683 /**
Steve McKayb67bfbf2015-12-08 17:02:03 -0800684 * Builds URI for user home directory on external (local) storage.
685 * {@hide}
686 */
687 public static Uri buildHomeUri() {
688 // TODO: Avoid this type of interpackage copying. Added here to avoid
689 // direct coupling, but not ideal.
690 return DocumentsContract.buildRootUri("com.android.externalstorage.documents", "home");
691 }
692
693 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700694 * Build URI representing the recently modified documents of a specific root
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700695 * in a document provider. When queried, a provider will return zero or more
696 * rows with columns defined by {@link Document}.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700697 *
698 * @see DocumentsProvider#queryRecentDocuments(String, String[])
699 * @see #getRootId(Uri)
700 */
701 public static Uri buildRecentDocumentsUri(String authority, String rootId) {
702 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
703 .authority(authority).appendPath(PATH_ROOT).appendPath(rootId)
704 .appendPath(PATH_RECENT).build();
705 }
706
707 /**
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700708 * Build URI representing access to descendant documents of the given
709 * {@link Document#COLUMN_DOCUMENT_ID}.
710 *
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700711 * @see #getTreeDocumentId(Uri)
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700712 */
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700713 public static Uri buildTreeDocumentUri(String authority, String documentId) {
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700714 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority)
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700715 .appendPath(PATH_TREE).appendPath(documentId).build();
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700716 }
717
718 /**
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700719 * Build URI representing the target {@link Document#COLUMN_DOCUMENT_ID} in
720 * a document provider. When queried, a provider will return a single row
721 * with columns defined by {@link Document}.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700722 *
723 * @see DocumentsProvider#queryDocument(String, String[])
724 * @see #getDocumentId(Uri)
725 */
726 public static Uri buildDocumentUri(String authority, String documentId) {
727 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
728 .authority(authority).appendPath(PATH_DOCUMENT).appendPath(documentId).build();
729 }
730
731 /**
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700732 * Build URI representing the target {@link Document#COLUMN_DOCUMENT_ID} in
733 * a document provider. When queried, a provider will return a single row
734 * with columns defined by {@link Document}.
735 * <p>
736 * However, instead of directly accessing the target document, the returned
737 * URI will leverage access granted through a subtree URI, typically
738 * returned by {@link Intent#ACTION_OPEN_DOCUMENT_TREE}. The target document
739 * must be a descendant (child, grandchild, etc) of the subtree.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700740 * <p>
741 * This is typically used to access documents under a user-selected
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700742 * directory tree, since it doesn't require the user to separately confirm
743 * each new document access.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700744 *
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700745 * @param treeUri the subtree to leverage to gain access to the target
746 * document. The target directory must be a descendant of this
747 * subtree.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700748 * @param documentId the target document, which the caller may not have
749 * direct access to.
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700750 * @see Intent#ACTION_OPEN_DOCUMENT_TREE
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700751 * @see DocumentsProvider#isChildDocument(String, String)
752 * @see #buildDocumentUri(String, String)
753 */
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700754 public static Uri buildDocumentUriUsingTree(Uri treeUri, String documentId) {
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700755 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700756 .authority(treeUri.getAuthority()).appendPath(PATH_TREE)
757 .appendPath(getTreeDocumentId(treeUri)).appendPath(PATH_DOCUMENT)
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700758 .appendPath(documentId).build();
759 }
760
761 /** {@hide} */
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700762 public static Uri buildDocumentUriMaybeUsingTree(Uri baseUri, String documentId) {
763 if (isTreeUri(baseUri)) {
764 return buildDocumentUriUsingTree(baseUri, documentId);
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700765 } else {
766 return buildDocumentUri(baseUri.getAuthority(), documentId);
767 }
768 }
769
770 /**
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700771 * Build URI representing the children of the target directory in a document
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700772 * provider. When queried, a provider will return zero or more rows with
773 * columns defined by {@link Document}.
774 *
775 * @param parentDocumentId the document to return children for, which must
776 * be a directory with MIME type of
777 * {@link Document#MIME_TYPE_DIR}.
778 * @see DocumentsProvider#queryChildDocuments(String, String[], String)
779 * @see #getDocumentId(Uri)
780 */
781 public static Uri buildChildDocumentsUri(String authority, String parentDocumentId) {
782 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority)
783 .appendPath(PATH_DOCUMENT).appendPath(parentDocumentId).appendPath(PATH_CHILDREN)
784 .build();
785 }
786
787 /**
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700788 * Build URI representing the children of the target directory in a document
789 * provider. When queried, a provider will return zero or more rows with
790 * columns defined by {@link Document}.
791 * <p>
792 * However, instead of directly accessing the target directory, the returned
793 * URI will leverage access granted through a subtree URI, typically
794 * returned by {@link Intent#ACTION_OPEN_DOCUMENT_TREE}. The target
795 * directory must be a descendant (child, grandchild, etc) of the subtree.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700796 * <p>
797 * This is typically used to access documents under a user-selected
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700798 * directory tree, since it doesn't require the user to separately confirm
799 * each new document access.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700800 *
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700801 * @param treeUri the subtree to leverage to gain access to the target
802 * document. The target directory must be a descendant of this
803 * subtree.
804 * @param parentDocumentId the document to return children for, which the
805 * caller may not have direct access to, and which must be a
806 * directory with MIME type of {@link Document#MIME_TYPE_DIR}.
807 * @see Intent#ACTION_OPEN_DOCUMENT_TREE
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700808 * @see DocumentsProvider#isChildDocument(String, String)
809 * @see #buildChildDocumentsUri(String, String)
810 */
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700811 public static Uri buildChildDocumentsUriUsingTree(Uri treeUri, String parentDocumentId) {
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700812 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700813 .authority(treeUri.getAuthority()).appendPath(PATH_TREE)
814 .appendPath(getTreeDocumentId(treeUri)).appendPath(PATH_DOCUMENT)
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700815 .appendPath(parentDocumentId).appendPath(PATH_CHILDREN).build();
816 }
817
818 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700819 * Build URI representing a search for matching documents under a specific
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700820 * root in a document provider. When queried, a provider will return zero or
821 * more rows with columns defined by {@link Document}.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700822 *
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700823 * @see DocumentsProvider#querySearchDocuments(String, String, String[])
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700824 * @see #getRootId(Uri)
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700825 * @see #getSearchDocumentsQuery(Uri)
826 */
827 public static Uri buildSearchDocumentsUri(
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700828 String authority, String rootId, String query) {
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700829 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority)
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700830 .appendPath(PATH_ROOT).appendPath(rootId).appendPath(PATH_SEARCH)
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700831 .appendQueryParameter(PARAM_QUERY, query).build();
832 }
833
834 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700835 * Test if the given URI represents a {@link Document} backed by a
Jeff Sharkeyee2f7df2013-09-26 11:32:30 -0700836 * {@link DocumentsProvider}.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700837 *
838 * @see #buildDocumentUri(String, String)
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700839 * @see #buildDocumentUriUsingTree(Uri, String)
Jeff Sharkeyee2f7df2013-09-26 11:32:30 -0700840 */
Steve McKay323ee3e2015-09-25 16:02:56 -0700841 public static boolean isDocumentUri(Context context, @Nullable Uri uri) {
842 if (isContentUri(uri) && isDocumentsProvider(context, uri.getAuthority())) {
843 final List<String> paths = uri.getPathSegments();
844 if (paths.size() == 2) {
845 return PATH_DOCUMENT.equals(paths.get(0));
846 } else if (paths.size() == 4) {
847 return PATH_TREE.equals(paths.get(0)) && PATH_DOCUMENT.equals(paths.get(2));
848 }
Jeff Sharkeyee2f7df2013-09-26 11:32:30 -0700849 }
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700850 return false;
851 }
Jeff Sharkeyee2f7df2013-09-26 11:32:30 -0700852
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700853 /** {@hide} */
Steve McKay323ee3e2015-09-25 16:02:56 -0700854 public static boolean isRootUri(Context context, @Nullable Uri uri) {
855 if (isContentUri(uri) && isDocumentsProvider(context, uri.getAuthority())) {
856 final List<String> paths = uri.getPathSegments();
857 return (paths.size() == 2 && PATH_ROOT.equals(paths.get(0)));
858 }
859 return false;
860 }
861
862 /** {@hide} */
863 public static boolean isContentUri(@Nullable Uri uri) {
864 return uri != null && ContentResolver.SCHEME_CONTENT.equals(uri.getScheme());
865 }
866
Tomasz Mikolajewski7db9c812016-01-28 09:50:01 +0900867 /**
868 * Test if the given URI represents a {@link Document} tree.
869 *
870 * @see #buildTreeDocumentUri(String, String)
871 * @see #getTreeDocumentId(Uri, String)
872 */
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700873 public static boolean isTreeUri(Uri uri) {
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700874 final List<String> paths = uri.getPathSegments();
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700875 return (paths.size() >= 2 && PATH_TREE.equals(paths.get(0)));
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700876 }
877
878 private static boolean isDocumentsProvider(Context context, String authority) {
Jeff Sharkeyd2e1e812013-10-09 13:31:13 -0700879 final Intent intent = new Intent(PROVIDER_INTERFACE);
880 final List<ResolveInfo> infos = context.getPackageManager()
881 .queryIntentContentProviders(intent, 0);
882 for (ResolveInfo info : infos) {
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700883 if (authority.equals(info.providerInfo.authority)) {
Jeff Sharkeyd2e1e812013-10-09 13:31:13 -0700884 return true;
885 }
Jeff Sharkeyee2f7df2013-09-26 11:32:30 -0700886 }
887 return false;
888 }
889
890 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700891 * Extract the {@link Root#COLUMN_ROOT_ID} from the given URI.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700892 */
893 public static String getRootId(Uri rootUri) {
894 final List<String> paths = rootUri.getPathSegments();
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700895 if (paths.size() >= 2 && PATH_ROOT.equals(paths.get(0))) {
896 return paths.get(1);
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700897 }
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700898 throw new IllegalArgumentException("Invalid URI: " + rootUri);
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700899 }
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700900
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700901 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700902 * Extract the {@link Document#COLUMN_DOCUMENT_ID} from the given URI.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700903 *
904 * @see #isDocumentUri(Context, Uri)
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700905 */
906 public static String getDocumentId(Uri documentUri) {
907 final List<String> paths = documentUri.getPathSegments();
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700908 if (paths.size() >= 2 && PATH_DOCUMENT.equals(paths.get(0))) {
909 return paths.get(1);
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700910 }
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700911 if (paths.size() >= 4 && PATH_TREE.equals(paths.get(0))
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700912 && PATH_DOCUMENT.equals(paths.get(2))) {
913 return paths.get(3);
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700914 }
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700915 throw new IllegalArgumentException("Invalid URI: " + documentUri);
916 }
917
918 /**
919 * Extract the via {@link Document#COLUMN_DOCUMENT_ID} from the given URI.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700920 */
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700921 public static String getTreeDocumentId(Uri documentUri) {
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700922 final List<String> paths = documentUri.getPathSegments();
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700923 if (paths.size() >= 2 && PATH_TREE.equals(paths.get(0))) {
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700924 return paths.get(1);
925 }
926 throw new IllegalArgumentException("Invalid URI: " + documentUri);
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700927 }
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700928
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700929 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700930 * Extract the search query from a URI built by
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700931 * {@link #buildSearchDocumentsUri(String, String, String)}.
932 */
933 public static String getSearchDocumentsQuery(Uri searchDocumentsUri) {
934 return searchDocumentsUri.getQueryParameter(PARAM_QUERY);
Jeff Sharkey20d96d82013-07-30 17:08:39 -0700935 }
936
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700937 /** {@hide} */
938 public static Uri setManageMode(Uri uri) {
939 return uri.buildUpon().appendQueryParameter(PARAM_MANAGE, "true").build();
940 }
941
942 /** {@hide} */
943 public static boolean isManageMode(Uri uri) {
944 return uri.getBooleanQueryParameter(PARAM_MANAGE, false);
945 }
946
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700947 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700948 * Return thumbnail representing the document at the given URI. Callers are
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700949 * responsible for their own in-memory caching.
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700950 *
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700951 * @param documentUri document to return thumbnail for, which must have
952 * {@link Document#FLAG_SUPPORTS_THUMBNAIL} set.
953 * @param size optimal thumbnail size desired. A provider may return a
954 * thumbnail of a different size, but never more than double the
955 * requested size.
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700956 * @param signal signal used to indicate if caller is no longer interested
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700957 * in the thumbnail.
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700958 * @return decoded thumbnail, or {@code null} if problem was encountered.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700959 * @see DocumentsProvider#openDocumentThumbnail(String, Point,
960 * android.os.CancellationSignal)
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700961 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700962 public static Bitmap getDocumentThumbnail(
963 ContentResolver resolver, Uri documentUri, Point size, CancellationSignal signal) {
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -0700964 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
965 documentUri.getAuthority());
966 try {
967 return getDocumentThumbnail(client, documentUri, size, signal);
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700968 } catch (Exception e) {
Jeff Sharkey33819312013-10-29 11:56:37 -0700969 if (!(e instanceof OperationCanceledException)) {
970 Log.w(TAG, "Failed to load thumbnail for " + documentUri + ": " + e);
971 }
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -0700972 return null;
973 } finally {
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700974 ContentProviderClient.releaseQuietly(client);
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -0700975 }
976 }
977
978 /** {@hide} */
979 public static Bitmap getDocumentThumbnail(
980 ContentProviderClient client, Uri documentUri, Point size, CancellationSignal signal)
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700981 throws RemoteException, IOException {
Jeff Sharkey63983432013-08-21 11:33:50 -0700982 final Bundle openOpts = new Bundle();
Jeff Sharkey5b836f22014-08-27 14:46:32 -0700983 openOpts.putParcelable(ContentResolver.EXTRA_SIZE, size);
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700984
Jeff Sharkey9d0843d2013-05-07 12:41:33 -0700985 AssetFileDescriptor afd = null;
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -0700986 Bitmap bitmap = null;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700987 try {
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -0700988 afd = client.openTypedAssetFileDescriptor(documentUri, "image/*", openOpts, signal);
Jeff Sharkey9d0843d2013-05-07 12:41:33 -0700989
990 final FileDescriptor fd = afd.getFileDescriptor();
Jeff Sharkey63983432013-08-21 11:33:50 -0700991 final long offset = afd.getStartOffset();
Jeff Sharkey9d0843d2013-05-07 12:41:33 -0700992
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -0700993 // Try seeking on the returned FD, since it gives us the most
994 // optimal decode path; otherwise fall back to buffering.
995 BufferedInputStream is = null;
996 try {
Elliott Hughes34385d32014-04-28 11:11:32 -0700997 Os.lseek(fd, offset, SEEK_SET);
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -0700998 } catch (ErrnoException e) {
999 is = new BufferedInputStream(new FileInputStream(fd), THUMBNAIL_BUFFER_SIZE);
1000 is.mark(THUMBNAIL_BUFFER_SIZE);
Jeff Sharkey63983432013-08-21 11:33:50 -07001001 }
Jeff Sharkey9d0843d2013-05-07 12:41:33 -07001002
Jeff Sharkey63983432013-08-21 11:33:50 -07001003 // We requested a rough thumbnail size, but the remote size may have
1004 // returned something giant, so defensively scale down as needed.
1005 final BitmapFactory.Options opts = new BitmapFactory.Options();
1006 opts.inJustDecodeBounds = true;
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001007 if (is != null) {
1008 BitmapFactory.decodeStream(is, null, opts);
Jeff Sharkey63983432013-08-21 11:33:50 -07001009 } else {
1010 BitmapFactory.decodeFileDescriptor(fd, null, opts);
1011 }
Jeff Sharkey9d0843d2013-05-07 12:41:33 -07001012
Jeff Sharkey63983432013-08-21 11:33:50 -07001013 final int widthSample = opts.outWidth / size.x;
1014 final int heightSample = opts.outHeight / size.y;
1015
1016 opts.inJustDecodeBounds = false;
1017 opts.inSampleSize = Math.min(widthSample, heightSample);
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001018 if (is != null) {
1019 is.reset();
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -07001020 bitmap = BitmapFactory.decodeStream(is, null, opts);
Jeff Sharkey63983432013-08-21 11:33:50 -07001021 } else {
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001022 try {
Elliott Hughes34385d32014-04-28 11:11:32 -07001023 Os.lseek(fd, offset, SEEK_SET);
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001024 } catch (ErrnoException e) {
1025 e.rethrowAsIOException();
1026 }
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -07001027 bitmap = BitmapFactory.decodeFileDescriptor(fd, null, opts);
1028 }
1029
1030 // Transform the bitmap if requested. We use a side-channel to
1031 // communicate the orientation, since EXIF thumbnails don't contain
1032 // the rotation flags of the original image.
1033 final Bundle extras = afd.getExtras();
1034 final int orientation = (extras != null) ? extras.getInt(EXTRA_ORIENTATION, 0) : 0;
1035 if (orientation != 0) {
1036 final int width = bitmap.getWidth();
1037 final int height = bitmap.getHeight();
1038
1039 final Matrix m = new Matrix();
1040 m.setRotate(orientation, width / 2, height / 2);
1041 bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, false);
Jeff Sharkey63983432013-08-21 11:33:50 -07001042 }
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001043 } finally {
Jeff Sharkey9d0843d2013-05-07 12:41:33 -07001044 IoUtils.closeQuietly(afd);
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001045 }
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -07001046
1047 return bitmap;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001048 }
1049
Jeff Sharkey5b83f852013-08-14 18:29:19 -07001050 /**
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001051 * Create a new document with given MIME type and display name.
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001052 *
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001053 * @param parentDocumentUri directory with
1054 * {@link Document#FLAG_DIR_SUPPORTS_CREATE}
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001055 * @param mimeType MIME type of new document
1056 * @param displayName name of new document
1057 * @return newly created document, or {@code null} if failed
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001058 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001059 public static Uri createDocument(ContentResolver resolver, Uri parentDocumentUri,
1060 String mimeType, String displayName) {
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001061 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
1062 parentDocumentUri.getAuthority());
1063 try {
1064 return createDocument(client, parentDocumentUri, mimeType, displayName);
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001065 } catch (Exception e) {
1066 Log.w(TAG, "Failed to create document", e);
1067 return null;
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001068 } finally {
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001069 ContentProviderClient.releaseQuietly(client);
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001070 }
1071 }
1072
1073 /** {@hide} */
1074 public static Uri createDocument(ContentProviderClient client, Uri parentDocumentUri,
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001075 String mimeType, String displayName) throws RemoteException {
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001076 final Bundle in = new Bundle();
Jeff Sharkey21de56a2014-04-05 19:05:24 -07001077 in.putParcelable(DocumentsContract.EXTRA_URI, parentDocumentUri);
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001078 in.putString(Document.COLUMN_MIME_TYPE, mimeType);
1079 in.putString(Document.COLUMN_DISPLAY_NAME, displayName);
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001080
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001081 final Bundle out = client.call(METHOD_CREATE_DOCUMENT, null, in);
Jeff Sharkey21de56a2014-04-05 19:05:24 -07001082 return out.getParcelable(DocumentsContract.EXTRA_URI);
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001083 }
Jeff Sharkey5b83f852013-08-14 18:29:19 -07001084
Steve McKayd3afdee2015-11-19 17:27:12 -08001085 /** {@hide} */
1086 public static boolean isChildDocument(ContentProviderClient client, Uri parentDocumentUri,
1087 Uri childDocumentUri) throws RemoteException {
1088
1089 final Bundle in = new Bundle();
1090 in.putParcelable(DocumentsContract.EXTRA_URI, parentDocumentUri);
1091 in.putParcelable(DocumentsContract.EXTRA_TARGET_URI, childDocumentUri);
1092
1093 final Bundle out = client.call(METHOD_IS_CHILD_DOCUMENT, null, in);
1094 if (out == null) {
1095 throw new RemoteException("Failed to get a reponse from isChildDocument query.");
1096 }
1097 if (!out.containsKey(DocumentsContract.EXTRA_RESULT)) {
1098 throw new RemoteException("Response did not include result field..");
1099 }
1100 return out.getBoolean(DocumentsContract.EXTRA_RESULT);
1101 }
1102
Jeff Sharkey5b83f852013-08-14 18:29:19 -07001103 /**
Jeff Sharkeyb7e12552014-05-21 22:22:03 -07001104 * Change the display name of an existing document.
1105 * <p>
1106 * If the underlying provider needs to create a new
1107 * {@link Document#COLUMN_DOCUMENT_ID} to represent the updated display
1108 * name, that new document is returned and the original document is no
1109 * longer valid. Otherwise, the original document is returned.
1110 *
1111 * @param documentUri document with {@link Document#FLAG_SUPPORTS_RENAME}
1112 * @param displayName updated name for document
1113 * @return the existing or new document after the rename, or {@code null} if
1114 * failed.
1115 */
1116 public static Uri renameDocument(ContentResolver resolver, Uri documentUri,
1117 String displayName) {
1118 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
1119 documentUri.getAuthority());
1120 try {
1121 return renameDocument(client, documentUri, displayName);
1122 } catch (Exception e) {
1123 Log.w(TAG, "Failed to rename document", e);
1124 return null;
1125 } finally {
1126 ContentProviderClient.releaseQuietly(client);
1127 }
1128 }
1129
1130 /** {@hide} */
1131 public static Uri renameDocument(ContentProviderClient client, Uri documentUri,
1132 String displayName) throws RemoteException {
1133 final Bundle in = new Bundle();
1134 in.putParcelable(DocumentsContract.EXTRA_URI, documentUri);
1135 in.putString(Document.COLUMN_DISPLAY_NAME, displayName);
1136
1137 final Bundle out = client.call(METHOD_RENAME_DOCUMENT, null, in);
1138 final Uri outUri = out.getParcelable(DocumentsContract.EXTRA_URI);
1139 return (outUri != null) ? outUri : documentUri;
1140 }
1141
1142 /**
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001143 * Delete the given document.
1144 *
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001145 * @param documentUri document with {@link Document#FLAG_SUPPORTS_DELETE}
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001146 * @return if the document was deleted successfully.
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001147 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001148 public static boolean deleteDocument(ContentResolver resolver, Uri documentUri) {
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001149 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
1150 documentUri.getAuthority());
1151 try {
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001152 deleteDocument(client, documentUri);
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001153 return true;
1154 } catch (Exception e) {
1155 Log.w(TAG, "Failed to delete document", e);
1156 return false;
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001157 } finally {
1158 ContentProviderClient.releaseQuietly(client);
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001159 }
Jeff Sharkey5b83f852013-08-14 18:29:19 -07001160 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001161
1162 /** {@hide} */
1163 public static void deleteDocument(ContentProviderClient client, Uri documentUri)
1164 throws RemoteException {
1165 final Bundle in = new Bundle();
Jeff Sharkey21de56a2014-04-05 19:05:24 -07001166 in.putParcelable(DocumentsContract.EXTRA_URI, documentUri);
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001167
1168 client.call(METHOD_DELETE_DOCUMENT, null, in);
1169 }
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -07001170
1171 /**
Tomasz Mikolajewski74fe1812015-06-12 17:13:26 -07001172 * Copies the given document.
1173 *
1174 * @param sourceDocumentUri document with {@link Document#FLAG_SUPPORTS_COPY}
1175 * @param targetParentDocumentUri document which will become a parent of the source
1176 * document's copy.
1177 * @return the copied document, or {@code null} if failed.
Tomasz Mikolajewski74fe1812015-06-12 17:13:26 -07001178 */
1179 public static Uri copyDocument(ContentResolver resolver, Uri sourceDocumentUri,
1180 Uri targetParentDocumentUri) {
1181 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
1182 sourceDocumentUri.getAuthority());
1183 try {
1184 return copyDocument(client, sourceDocumentUri, targetParentDocumentUri);
1185 } catch (Exception e) {
1186 Log.w(TAG, "Failed to copy document", e);
1187 return null;
1188 } finally {
1189 ContentProviderClient.releaseQuietly(client);
1190 }
1191 }
1192
1193 /** {@hide} */
1194 public static Uri copyDocument(ContentProviderClient client, Uri sourceDocumentUri,
1195 Uri targetParentDocumentUri) throws RemoteException {
1196 final Bundle in = new Bundle();
1197 in.putParcelable(DocumentsContract.EXTRA_URI, sourceDocumentUri);
1198 in.putParcelable(DocumentsContract.EXTRA_TARGET_URI, targetParentDocumentUri);
1199
1200 final Bundle out = client.call(METHOD_COPY_DOCUMENT, null, in);
1201 return out.getParcelable(DocumentsContract.EXTRA_URI);
1202 }
1203
1204 /**
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +09001205 * Moves the given document under a new parent.
1206 *
1207 * @param sourceDocumentUri document with {@link Document#FLAG_SUPPORTS_MOVE}
Tomasz Mikolajewskid46ecbc2016-01-25 14:26:54 +09001208 * @param sourceParentDocumentUri parent document of the document to move.
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +09001209 * @param targetParentDocumentUri document which will become a new parent of the source
1210 * document.
1211 * @return the moved document, or {@code null} if failed.
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +09001212 */
1213 public static Uri moveDocument(ContentResolver resolver, Uri sourceDocumentUri,
Tomasz Mikolajewskid46ecbc2016-01-25 14:26:54 +09001214 Uri sourceParentDocumentUri, Uri targetParentDocumentUri) {
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +09001215 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
1216 sourceDocumentUri.getAuthority());
1217 try {
Tomasz Mikolajewskidcb4efe2016-02-10 19:14:49 +09001218 return moveDocument(client, sourceDocumentUri, sourceParentDocumentUri,
Tomasz Mikolajewskid46ecbc2016-01-25 14:26:54 +09001219 targetParentDocumentUri);
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +09001220 } catch (Exception e) {
1221 Log.w(TAG, "Failed to move document", e);
1222 return null;
1223 } finally {
1224 ContentProviderClient.releaseQuietly(client);
1225 }
1226 }
1227
1228 /** {@hide} */
1229 public static Uri moveDocument(ContentProviderClient client, Uri sourceDocumentUri,
Tomasz Mikolajewskid46ecbc2016-01-25 14:26:54 +09001230 Uri sourceParentDocumentUri, Uri targetParentDocumentUri) throws RemoteException {
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +09001231 final Bundle in = new Bundle();
1232 in.putParcelable(DocumentsContract.EXTRA_URI, sourceDocumentUri);
Tomasz Mikolajewskid46ecbc2016-01-25 14:26:54 +09001233 in.putParcelable(DocumentsContract.EXTRA_PARENT_URI, sourceParentDocumentUri);
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +09001234 in.putParcelable(DocumentsContract.EXTRA_TARGET_URI, targetParentDocumentUri);
1235
1236 final Bundle out = client.call(METHOD_MOVE_DOCUMENT, null, in);
1237 return out.getParcelable(DocumentsContract.EXTRA_URI);
1238 }
1239
1240 /**
Tomasz Mikolajewskicbcd3942016-01-28 12:39:25 +09001241 * Removes the given document from a parent directory.
1242 *
1243 * <p>In contrast to {@link #deleteDocument} it requires specifying the parent.
1244 * This method is especially useful if the document can be in multiple parents.
1245 *
1246 * @param documentUri document with {@link Document#FLAG_SUPPORTS_REMOVE}
1247 * @param parentDocumentUri parent document of the document to remove.
1248 * @return true if the document was removed successfully.
1249 */
1250 public static boolean removeDocument(ContentResolver resolver, Uri documentUri,
1251 Uri parentDocumentUri) {
1252 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
1253 documentUri.getAuthority());
1254 try {
1255 removeDocument(client, documentUri, parentDocumentUri);
1256 return true;
1257 } catch (Exception e) {
1258 Log.w(TAG, "Failed to remove document", e);
1259 return false;
1260 } finally {
1261 ContentProviderClient.releaseQuietly(client);
1262 }
1263 }
1264
1265 /** {@hide} */
1266 public static void removeDocument(ContentProviderClient client, Uri documentUri,
1267 Uri parentDocumentUri) throws RemoteException {
1268 final Bundle in = new Bundle();
1269 in.putParcelable(DocumentsContract.EXTRA_URI, documentUri);
1270 in.putParcelable(DocumentsContract.EXTRA_PARENT_URI, parentDocumentUri);
1271
1272 client.call(METHOD_REMOVE_DOCUMENT, null, in);
1273 }
1274
1275 /**
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -07001276 * Open the given image for thumbnail purposes, using any embedded EXIF
1277 * thumbnail if available, and providing orientation hints from the parent
1278 * image.
1279 *
1280 * @hide
1281 */
1282 public static AssetFileDescriptor openImageThumbnail(File file) throws FileNotFoundException {
1283 final ParcelFileDescriptor pfd = ParcelFileDescriptor.open(
1284 file, ParcelFileDescriptor.MODE_READ_ONLY);
1285 Bundle extras = null;
1286
1287 try {
1288 final ExifInterface exif = new ExifInterface(file.getAbsolutePath());
1289
1290 switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1)) {
1291 case ExifInterface.ORIENTATION_ROTATE_90:
1292 extras = new Bundle(1);
1293 extras.putInt(EXTRA_ORIENTATION, 90);
1294 break;
1295 case ExifInterface.ORIENTATION_ROTATE_180:
1296 extras = new Bundle(1);
1297 extras.putInt(EXTRA_ORIENTATION, 180);
1298 break;
1299 case ExifInterface.ORIENTATION_ROTATE_270:
1300 extras = new Bundle(1);
1301 extras.putInt(EXTRA_ORIENTATION, 270);
1302 break;
1303 }
1304
1305 final long[] thumb = exif.getThumbnailRange();
1306 if (thumb != null) {
1307 return new AssetFileDescriptor(pfd, thumb[0], thumb[1], extras);
1308 }
1309 } catch (IOException e) {
1310 }
1311
1312 return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH, extras);
1313 }
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001314}