blob: 99fcdad4fc94c3ca17483ee81f597ffe4540fd75 [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;
Julian Mancinib6505152017-06-27 13:29:09 -070021
Garfield Tan06940e12016-10-07 16:03:17 -070022import static com.android.internal.util.Preconditions.checkArgument;
23import static com.android.internal.util.Preconditions.checkCollectionElementsNotNull;
24import static com.android.internal.util.Preconditions.checkCollectionNotEmpty;
25
Steve McKay323ee3e2015-09-25 16:02:56 -070026import android.annotation.Nullable;
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -070027import android.content.ContentProviderClient;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070028import android.content.ContentResolver;
Jeff Sharkeyee2f7df2013-09-26 11:32:30 -070029import android.content.Context;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070030import android.content.Intent;
Tomasz Mikolajewskicf316562016-10-24 15:17:01 +090031import android.content.IntentSender;
Jeff Sharkeyd2e1e812013-10-09 13:31:13 -070032import android.content.pm.ResolveInfo;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070033import android.content.res.AssetFileDescriptor;
Jeff Sharkey20d96d82013-07-30 17:08:39 -070034import android.database.Cursor;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070035import android.graphics.Bitmap;
36import android.graphics.BitmapFactory;
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -070037import android.graphics.Matrix;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070038import android.graphics.Point;
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -070039import android.media.ExifInterface;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070040import android.net.Uri;
Ben Lin8ea82002017-03-08 17:30:16 -080041import android.os.Build;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070042import android.os.Bundle;
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070043import android.os.CancellationSignal;
Jeff Sharkey33819312013-10-29 11:56:37 -070044import android.os.OperationCanceledException;
Garfield Tanaba97f32016-10-06 17:34:19 +000045import android.os.Parcel;
Jeff Sharkeybd3b9022013-08-20 15:20:04 -070046import android.os.ParcelFileDescriptor;
Jeff Sharkey67f9d502017-08-05 13:49:13 -060047import android.os.ParcelFileDescriptor.OnCloseListener;
Garfield Tanaba97f32016-10-06 17:34:19 +000048import android.os.Parcelable;
Ben Lin8ea82002017-03-08 17:30:16 -080049import android.os.ParcelableException;
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -070050import android.os.RemoteException;
Felipe Leme04a5d402016-02-08 16:44:06 -080051import android.os.storage.StorageVolume;
Elliott Hughes34385d32014-04-28 11:11:32 -070052import android.system.ErrnoException;
53import android.system.Os;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070054import android.util.Log;
55
56import libcore.io.IoUtils;
57
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -070058import java.io.BufferedInputStream;
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -070059import java.io.File;
Jeff Sharkey9d0843d2013-05-07 12:41:33 -070060import java.io.FileDescriptor;
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -070061import java.io.FileInputStream;
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -070062import java.io.FileNotFoundException;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070063import java.io.IOException;
Jeff Sharkeydc2963a2013-08-02 15:55:26 -070064import java.util.List;
Garfield Tan06940e12016-10-07 16:03:17 -070065import java.util.Objects;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070066
67/**
Jeff Sharkeybd3b9022013-08-20 15:20:04 -070068 * Defines the contract between a documents provider and the platform.
69 * <p>
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -070070 * To create a document provider, extend {@link DocumentsProvider}, which
71 * provides a foundational implementation of this contract.
Jeff Sharkey21de56a2014-04-05 19:05:24 -070072 * <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 Sharkeyb9fbb722014-06-04 16:42:47 -070075 * {@link Intent#ACTION_OPEN_DOCUMENT}, {@link Intent#ACTION_CREATE_DOCUMENT},
Felipe Leme04a5d402016-02-08 16:44:06 -080076 * {@link Intent#ACTION_OPEN_DOCUMENT_TREE}, or
77 * {@link StorageVolume#createAccessIntent(String) StorageVolume.createAccessIntent}.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -070078 *
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -070079 * @see DocumentsProvider
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070080 */
81public final class DocumentsContract {
Steve McKayd3afdee2015-11-19 17:27:12 -080082 private static final String TAG = "DocumentsContract";
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070083
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070084 // content://com.example/root/
85 // content://com.example/root/sdcard/
86 // content://com.example/root/sdcard/recent/
Jeff Sharkey3e1189b2013-09-12 21:59:06 -070087 // content://com.example/root/sdcard/search/?query=pony
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070088 // content://com.example/document/12/
89 // content://com.example/document/12/children/
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -070090 // content://com.example/tree/12/document/24/
91 // content://com.example/tree/12/document/24/children/
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -070092
93 private DocumentsContract() {
94 }
Jeff Sharkey9ecfee02013-04-19 14:05:03 -070095
Jeff Sharkey85f5f812013-10-07 10:16:12 -070096 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -070097 * Intent action used to identify {@link DocumentsProvider} instances. This
98 * is used in the {@code <intent-filter>} of a {@code <provider>}.
Jeff Sharkey85f5f812013-10-07 10:16:12 -070099 */
100 public static final String PROVIDER_INTERFACE = "android.content.action.DOCUMENTS_PROVIDER";
101
Jeff Sharkey5b83f852013-08-14 18:29:19 -0700102 /** {@hide} */
Jeff Sharkey15be8362013-10-09 13:52:17 -0700103 public static final String EXTRA_PACKAGE_NAME = "android.content.extra.PACKAGE_NAME";
104
Jeff Sharkey96c62052013-10-25 16:30:54 -0700105 /** {@hide} */
Aga Wronska1719b352016-03-21 11:28:03 -0700106 public static final String EXTRA_SHOW_ADVANCED = "android.content.extra.SHOW_ADVANCED";
107
108 /** {@hide} */
Tomasz Mikolajewski74fe1812015-06-12 17:13:26 -0700109 public static final String EXTRA_TARGET_URI = "android.content.extra.TARGET_URI";
110
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -0700111 /**
Garfield Tanb44ae612016-11-07 16:46:37 -0800112 * 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 Tan40d7b352017-03-02 15:30:30 -0800124 *
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 Tanb44ae612016-11-07 16:46:37 -0800127 */
128 public static final String EXTRA_INITIAL_URI = "android.provider.extra.INITIAL_URI";
129
130 /**
Ben Kwa77797402015-05-29 15:40:31 -0700131 * 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 Sharkeyc1c8f3f2013-10-14 14:57:33 -0700137 * Included in {@link AssetFileDescriptor#getExtras()} when returned
138 * thumbnail should be rotated.
139 *
140 * @see MediaStore.Images.ImageColumns#ORIENTATION
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -0700141 */
Tomasz Mikolajewski5f53f652016-03-31 09:34:51 +0900142 public static final String EXTRA_ORIENTATION = "android.provider.extra.ORIENTATION";
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -0700143
Tomasz Mikolajewski0e591f92015-06-12 16:22:17 -0700144 /**
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 Linbd036d82016-12-15 15:41:08 -0800149 /**
150 * Action of intent issued by DocumentsUI when user wishes to open/configure/manage a particular
151 * document in the provider application.
Ben Lin8ea82002017-03-08 17:30:16 -0800152 *
Ben Linbd036d82016-12-15 15:41:08 -0800153 * <p>When issued, the intent will include the URI of the document as the intent data.
Ben Lin8ea82002017-03-08 17:30:16 -0800154 *
Ben Linbd036d82016-12-15 15:41:08 -0800155 * <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 Sharkey15be8362013-10-09 13:52:17 -0700166 /** {@hide} */
Jeff Sharkeya61dc8e2013-09-05 17:14:14 -0700167 public static final String ACTION_MANAGE_DOCUMENT = "android.provider.action.MANAGE_DOCUMENT";
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700168
Jeff Sharkey59d577a2015-04-11 21:27:21 -0700169 /** {@hide} */
Jeff Sharkey1407d4c2015-04-12 21:52:24 -0700170 public static final String
171 ACTION_DOCUMENT_ROOT_SETTINGS = "android.provider.action.DOCUMENT_ROOT_SETTINGS";
Jeff Sharkey59d577a2015-04-11 21:27:21 -0700172
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700173 /**
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -0700174 * Buffer is large enough to rewind past any EXIF headers.
175 */
176 private static final int THUMBNAIL_BUFFER_SIZE = (int) (128 * KB_IN_BYTES);
177
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -0700178 /** {@hide} */
Garfield Tan92b96ba2016-11-01 14:33:48 -0700179 public static final String EXTERNAL_STORAGE_PROVIDER_AUTHORITY =
180 "com.android.externalstorage.documents";
181
182 /** {@hide} */
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -0700183 public static final String PACKAGE_DOCUMENTS_UI = "com.android.documentsui";
184
Julian Mancinib6505152017-06-27 13:29:09 -0700185 /** {@hide} */
186 public static final String METADATA_TYPES = "android:documentMetadataType";
187
188 /** {@hide} */
189 public static final String METADATA_EXIF = "android:documentExif";
190
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -0700191 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700192 * Constants related to a document, including {@link Cursor} column names
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700193 * and flags.
194 * <p>
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700195 * 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 Sharkeyae9b51b2013-08-31 15:02:20 -0700200 * <p>
201 * All columns are <em>read-only</em> to client applications.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700202 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700203 public final static class Document {
204 private Document() {
Jeff Sharkeya5599ef2013-08-15 16:17:41 -0700205 }
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700206
Jeff Sharkeya5599ef2013-08-15 16:17:41 -0700207 /**
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700208 * 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 Sharkey6efba222013-09-27 16:44:11 -0700210 * value by client applications. This column is required.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700211 * <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 Sharkeye8c00d82013-10-15 15:46:10 -0700216 * issue long-term URI permission grants when an application interacts
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700217 * with {@link Intent#ACTION_OPEN_DOCUMENT} and
218 * {@link Intent#ACTION_CREATE_DOCUMENT}.
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700219 * <p>
220 * Type: STRING
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700221 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700222 public static final String COLUMN_DOCUMENT_ID = "document_id";
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700223
224 /**
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700225 * 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 Sharkey6efba222013-09-27 16:44:11 -0700228 * the {@link #MIME_TYPE_DIR} MIME type. This column is required.
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700229 * <p>
230 * Type: STRING
231 *
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700232 * @see #MIME_TYPE_DIR
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700233 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700234 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 Sharkey6efba222013-09-27 16:44:11 -0700238 * user. This column is required.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700239 * <p>
240 * Type: STRING
241 */
242 public static final String COLUMN_DISPLAY_NAME = OpenableColumns.DISPLAY_NAME;
243
244 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700245 * Summary of a document, which may be shown to a user. This column is
246 * optional, and may be {@code null}.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700247 * <p>
248 * Type: STRING
249 */
250 public static final String COLUMN_SUMMARY = "summary";
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700251
252 /**
253 * Timestamp when a document was last modified, in milliseconds since
Jeff Sharkey6efba222013-09-27 16:44:11 -0700254 * 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 Sharkeyaeb16e22013-08-27 18:26:48 -0700257 * {@link ParcelFileDescriptor} transports.
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700258 * <p>
259 * Type: INTEGER (long)
260 *
261 * @see System#currentTimeMillis()
262 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700263 public static final String COLUMN_LAST_MODIFIED = "last_modified";
Jeff Sharkey9ecfee02013-04-19 14:05:03 -0700264
265 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700266 * 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 Sharkey9ecfee02013-04-19 14:05:03 -0700269 * <p>
270 * Type: INTEGER (int)
271 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700272 public static final String COLUMN_ICON = "icon";
Jeff Sharkey66516692013-08-06 11:26:10 -0700273
274 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700275 * Flags that apply to a document. This column is required.
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700276 * <p>
277 * Type: INTEGER (int)
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700278 *
279 * @see #FLAG_SUPPORTS_WRITE
280 * @see #FLAG_SUPPORTS_DELETE
281 * @see #FLAG_SUPPORTS_THUMBNAIL
282 * @see #FLAG_DIR_PREFERS_GRID
Jeff Sharkey6efba222013-09-27 16:44:11 -0700283 * @see #FLAG_DIR_PREFERS_LAST_MODIFIED
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900284 * @see #FLAG_VIRTUAL_DOCUMENT
Tomasz Mikolajewskicbcd3942016-01-28 12:39:25 +0900285 * @see #FLAG_SUPPORTS_COPY
286 * @see #FLAG_SUPPORTS_MOVE
287 * @see #FLAG_SUPPORTS_REMOVE
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700288 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700289 public static final String COLUMN_FLAGS = "flags";
290
291 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700292 * Size of a document, in bytes, or {@code null} if unknown. This column
293 * is required.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700294 * <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 McKay83df8c02015-09-16 15:07:31 -0700328 *
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700329 * @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 Sharkeyae9b51b2013-08-31 15:02:20 -0700348 * @see DocumentsProvider#createDocument(String, String, String)
349 */
350 public static final int FLAG_DIR_SUPPORTS_CREATE = 1 << 3;
351
352 /**
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700353 * 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 Sharkey3e1189b2013-09-12 21:59:06 -0700360 public static final int FLAG_DIR_PREFERS_GRID = 1 << 4;
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700361
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 Sharkey3e1189b2013-09-12 21:59:06 -0700369 public static final int FLAG_DIR_PREFERS_LAST_MODIFIED = 1 << 5;
Jeff Sharkeyf6db1542013-09-13 13:42:19 -0700370
371 /**
Jeff Sharkeyb7e12552014-05-21 22:22:03 -0700372 * Flag indicating that a document can be renamed.
373 *
374 * @see #COLUMN_FLAGS
Tomasz Mikolajewski90a75332016-07-13 10:14:59 +0900375 * @see DocumentsContract#renameDocument(ContentResolver, Uri,
Jeff Sharkeyb7e12552014-05-21 22:22:03 -0700376 * String)
377 * @see DocumentsProvider#renameDocument(String, String)
378 */
379 public static final int FLAG_SUPPORTS_RENAME = 1 << 6;
380
381 /**
Tomasz Mikolajewski74fe1812015-06-12 17:13:26 -0700382 * Flag indicating that a document can be copied to another location
383 * within the same document provider.
384 *
385 * @see #COLUMN_FLAGS
Tomasz Mikolajewski90a75332016-07-13 10:14:59 +0900386 * @see DocumentsContract#copyDocument(ContentResolver, Uri, Uri)
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +0900387 * @see DocumentsProvider#copyDocument(String, String)
Tomasz Mikolajewski74fe1812015-06-12 17:13:26 -0700388 */
389 public static final int FLAG_SUPPORTS_COPY = 1 << 7;
390
391 /**
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +0900392 * Flag indicating that a document can be moved to another location
393 * within the same document provider.
394 *
395 * @see #COLUMN_FLAGS
Tomasz Mikolajewski90a75332016-07-13 10:14:59 +0900396 * @see DocumentsContract#moveDocument(ContentResolver, Uri, Uri, Uri)
Tomasz Mikolajewskid46ecbc2016-01-25 14:26:54 +0900397 * @see DocumentsProvider#moveDocument(String, String, String)
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +0900398 */
399 public static final int FLAG_SUPPORTS_MOVE = 1 << 8;
400
401 /**
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900402 * 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 Mikolajewski099f9512016-12-09 10:19:46 +0900405 * <p><em>Virtual documents must have at least one alternative streamable
406 * format via {@link DocumentsProvider#openTypedDocument}</em>
407 *
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900408 * @see #COLUMN_FLAGS
409 * @see #COLUMN_MIME_TYPE
410 * @see DocumentsProvider#openTypedDocument(String, String, Bundle,
411 * android.os.CancellationSignal)
Tomasz Mikolajewskid99964f2016-02-15 11:16:32 +0900412 * @see DocumentsProvider#getDocumentStreamTypes(String, String)
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900413 */
Tomasz Mikolajewski75395652016-01-07 07:19:22 +0000414 public static final int FLAG_VIRTUAL_DOCUMENT = 1 << 9;
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900415
416 /**
Tomasz Mikolajewski9b055e12016-02-01 13:01:34 +0900417 * Flag indicating that a document can be removed from a parent.
418 *
419 * @see #COLUMN_FLAGS
Tomasz Mikolajewski90a75332016-07-13 10:14:59 +0900420 * @see DocumentsContract#removeDocument(ContentResolver, Uri, Uri)
Tomasz Mikolajewski9b055e12016-02-01 13:01:34 +0900421 * @see DocumentsProvider#removeDocument(String, String)
422 */
423 public static final int FLAG_SUPPORTS_REMOVE = 1 << 10;
424
425 /**
Ben Linbd036d82016-12-15 15:41:08 -0800426 * 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 Mikolajewskicf316562016-10-24 15:17:01 +0900434 * 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 McKay168e4642016-03-14 13:02:56 -0700442 * 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 Mancinib6505152017-06-27 13:29:09 -0700451
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 Sharkey9ecfee02013-04-19 14:05:03 -0700458 }
459
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700460 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700461 * 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 Sharkeyae9b51b2013-08-31 15:02:20 -0700466 * <p>
467 * All columns are <em>read-only</em> to client applications.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700468 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700469 public final static class Root {
470 private Root() {
471 }
472
Jeff Sharkeya5599ef2013-08-15 16:17:41 -0700473 /**
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700474 * 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 Sharkey6efba222013-09-27 16:44:11 -0700476 * by client applications. This column is required.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700477 * <p>
478 * Type: STRING
479 */
480 public static final String COLUMN_ROOT_ID = "root_id";
481
482 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700483 * Flags that apply to a root. This column is required.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700484 * <p>
485 * Type: INTEGER (int)
486 *
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700487 * @see #FLAG_LOCAL_ONLY
488 * @see #FLAG_SUPPORTS_CREATE
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700489 * @see #FLAG_SUPPORTS_RECENTS
490 * @see #FLAG_SUPPORTS_SEARCH
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700491 */
492 public static final String COLUMN_FLAGS = "flags";
493
494 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700495 * Icon resource ID for a root. This column is required.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700496 * <p>
497 * Type: INTEGER (int)
498 */
499 public static final String COLUMN_ICON = "icon";
500
501 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700502 * Title for a root, which will be shown to a user. This column is
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700503 * required. For a single storage service surfacing multiple accounts as
504 * different roots, this title should be the name of the service.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700505 * <p>
506 * Type: STRING
507 */
508 public static final String COLUMN_TITLE = "title";
509
510 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700511 * Summary for this root, which may be shown to a user. This column is
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700512 * 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 Sharkeyae9b51b2013-08-31 15:02:20 -0700515 * <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 Sharkey6efba222013-09-27 16:44:11 -0700522 * this root. This column is required.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700523 * <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 Sharkey6efba222013-09-27 16:44:11 -0700531 * Number of bytes available in this root. This column is optional, and
532 * may be {@code null} if unknown or unbounded.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700533 * <p>
534 * Type: INTEGER (long)
535 */
536 public static final String COLUMN_AVAILABLE_BYTES = "available_bytes";
537
538 /**
Tomasz Mikolajewski3f78e172015-06-25 16:17:26 +0900539 * Capacity of a root in bytes. This column is optional, and may be
540 * {@code null} if unknown or unbounded.
Tomasz Mikolajewski3f78e172015-06-25 16:17:26 +0900541 * <p>
542 * Type: INTEGER (long)
543 */
544 public static final String COLUMN_CAPACITY_BYTES = "capacity_bytes";
545
546 /**
Jeff Sharkey6efba222013-09-27 16:44:11 -0700547 * 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 Sharkey923396b2013-09-05 13:55:35 -0700551 * <p>
Jeff Sharkey6efba222013-09-27 16:44:11 -0700552 * Type: STRING
Jeff Sharkey923396b2013-09-05 13:55:35 -0700553 */
554 public static final String COLUMN_MIME_TYPES = "mime_types";
555
Garfield Tana7e852e2017-02-21 12:34:08 -0800556 /**
557 * MIME type for a root.
558 */
Jeff Sharkeya61dc8e2013-09-05 17:14:14 -0700559 public static final String MIME_TYPE_ITEM = "vnd.android.document/root";
560
Jeff Sharkey923396b2013-09-05 13:55:35 -0700561 /**
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700562 * Flag indicating that at least one directory under this root supports
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700563 * creating content. Roots with this flag will be shown when an
564 * application interacts with {@link Intent#ACTION_CREATE_DOCUMENT}.
Jeff Sharkey20d96d82013-07-30 17:08:39 -0700565 *
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700566 * @see #COLUMN_FLAGS
Jeff Sharkey20d96d82013-07-30 17:08:39 -0700567 */
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700568 public static final int FLAG_SUPPORTS_CREATE = 1;
Jeff Sharkey20d96d82013-07-30 17:08:39 -0700569
570 /**
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700571 * 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 Sharkeyae9b51b2013-08-31 15:02:20 -0700574 * @see #COLUMN_FLAGS
575 * @see Intent#EXTRA_LOCAL_ONLY
Jeff Sharkey20d96d82013-07-30 17:08:39 -0700576 */
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700577 public static final int FLAG_LOCAL_ONLY = 1 << 1;
578
Jeff Sharkey20d96d82013-07-30 17:08:39 -0700579 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700580 * Flag indicating that this root can be queried to provide recently
581 * modified documents.
Jeff Sharkey251097b2013-09-02 15:07:28 -0700582 *
583 * @see #COLUMN_FLAGS
584 * @see DocumentsContract#buildRecentDocumentsUri(String, String)
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700585 * @see DocumentsProvider#queryRecentDocuments(String, String[])
Jeff Sharkey251097b2013-09-02 15:07:28 -0700586 */
Jeff Sharkey6efba222013-09-27 16:44:11 -0700587 public static final int FLAG_SUPPORTS_RECENTS = 1 << 2;
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700588
589 /**
590 * Flag indicating that this root supports search.
591 *
592 * @see #COLUMN_FLAGS
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700593 * @see DocumentsContract#buildSearchDocumentsUri(String, String,
594 * String)
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700595 * @see DocumentsProvider#querySearchDocuments(String, String,
596 * String[])
597 */
Jeff Sharkey6efba222013-09-27 16:44:11 -0700598 public static final int FLAG_SUPPORTS_SEARCH = 1 << 3;
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700599
600 /**
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700601 * Flag indicating that this root supports testing parent child
602 * relationships.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700603 *
604 * @see #COLUMN_FLAGS
605 * @see DocumentsProvider#isChildDocument(String, String)
606 */
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700607 public static final int FLAG_SUPPORTS_IS_CHILD = 1 << 4;
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700608
609 /**
Garfield Tan87877032017-03-22 12:01:14 -0700610 * 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 Sharkey3e1189b2013-09-12 21:59:06 -0700619 * 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 Sharkey6efba222013-09-27 16:44:11 -0700622 * 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 Sharkey3e1189b2013-09-12 21:59:06 -0700625 *
626 * @see #COLUMN_FLAGS
Jeff Sharkey6efba222013-09-27 16:44:11 -0700627 * @see ContentResolver#notifyChange(Uri,
628 * android.database.ContentObserver, boolean)
629 * @hide
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700630 */
Jeff Sharkey6efba222013-09-27 16:44:11 -0700631 public static final int FLAG_EMPTY = 1 << 16;
632
633 /**
Aga Wronska1719b352016-03-21 11:28:03 -0700634 * 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 Sharkey1407d4c2015-04-12 21:52:24 -0700643 * Flag indicating that this root has settings.
644 *
645 * @see #COLUMN_FLAGS
646 * @see DocumentsContract#ACTION_DOCUMENT_ROOT_SETTINGS
647 * @hide
648 */
Aga Wronska1719b352016-03-21 11:28:03 -0700649 public static final int FLAG_HAS_SETTINGS = 1 << 18;
Steve McKayba23e542016-03-02 15:15:00 -0800650
651 /**
652 * Flag indicating that this root is on removable SD card storage.
653 *
654 * @see #COLUMN_FLAGS
655 * @hide
656 */
Aga Wronska1719b352016-03-21 11:28:03 -0700657 public static final int FLAG_REMOVABLE_SD = 1 << 19;
Steve McKayba23e542016-03-02 15:15:00 -0800658
659 /**
660 * Flag indicating that this root is on removable USB storage.
661 *
662 * @see #COLUMN_FLAGS
663 * @hide
664 */
Aga Wronska1719b352016-03-21 11:28:03 -0700665 public static final int FLAG_REMOVABLE_USB = 1 << 20;
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700666 }
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700667
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700668 /**
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 Sharkeyaeb16e22013-08-27 18:26:48 -0700679
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700680 /**
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 Sharkeyaeb16e22013-08-27 18:26:48 -0700687
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700688 /**
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 McKayd3afdee2015-11-19 17:27:12 -0800696 /**
697 * Optional result (I'm thinking boolean) answer to a question.
698 * {@hide}
699 */
700 public static final String EXTRA_RESULT = "result";
701
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700702 /** {@hide} */
703 public static final String METHOD_CREATE_DOCUMENT = "android:createDocument";
704 /** {@hide} */
Jeff Sharkeyb7e12552014-05-21 22:22:03 -0700705 public static final String METHOD_RENAME_DOCUMENT = "android:renameDocument";
706 /** {@hide} */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700707 public static final String METHOD_DELETE_DOCUMENT = "android:deleteDocument";
Tomasz Mikolajewski74fe1812015-06-12 17:13:26 -0700708 /** {@hide} */
709 public static final String METHOD_COPY_DOCUMENT = "android:copyDocument";
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +0900710 /** {@hide} */
711 public static final String METHOD_MOVE_DOCUMENT = "android:moveDocument";
Steve McKayd3afdee2015-11-19 17:27:12 -0800712 /** {@hide} */
713 public static final String METHOD_IS_CHILD_DOCUMENT = "android:isChildDocument";
Tomasz Mikolajewskicbcd3942016-01-28 12:39:25 +0900714 /** {@hide} */
715 public static final String METHOD_REMOVE_DOCUMENT = "android:removeDocument";
Ben Line7822fb2016-06-24 15:21:08 -0700716 /** {@hide} */
717 public static final String METHOD_EJECT_ROOT = "android:ejectRoot";
Garfield Tanaba97f32016-10-06 17:34:19 +0000718 /** {@hide} */
Garfield Tan3f6b68a2016-11-01 14:13:38 -0700719 public static final String METHOD_FIND_DOCUMENT_PATH = "android:findDocumentPath";
Tomasz Mikolajewskicf316562016-10-24 15:17:01 +0900720 /** {@hide} */
721 public static final String METHOD_CREATE_WEB_LINK_INTENT = "android:createWebLinkIntent";
Julian Mancinib6505152017-06-27 13:29:09 -0700722 /** {@hide} */
723 public static final String METHOD_GET_DOCUMENT_METADATA = "android:getDocumentMetadata";
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700724
725 /** {@hide} */
Tomasz Mikolajewskid46ecbc2016-01-25 14:26:54 +0900726 public static final String EXTRA_PARENT_URI = "parentUri";
727 /** {@hide} */
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700728 public static final String EXTRA_URI = "uri";
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700729
Tomasz Mikolajewskicf316562016-10-24 15:17:01 +0900730 /**
731 * @see #createWebLinkIntent(ContentResolver, Uri, Bundle)
732 * {@hide}
733 */
734 public static final String EXTRA_OPTIONS = "options";
735
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700736 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";
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700741 private static final String PATH_TREE = "tree";
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700742
743 private static final String PARAM_QUERY = "query";
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700744 private static final String PARAM_MANAGE = "manage";
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700745
746 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700747 * Build URI representing the roots of a document provider. When queried, a
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700748 * provider will return one or more rows with columns defined by
749 * {@link Root}.
750 *
751 * @see DocumentsProvider#queryRoots(String[])
752 */
753 public static Uri buildRootsUri(String authority) {
754 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
755 .authority(authority).appendPath(PATH_ROOT).build();
756 }
757
758 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700759 * Build URI representing the given {@link Root#COLUMN_ROOT_ID} in a
Jeff Sharkeya61dc8e2013-09-05 17:14:14 -0700760 * document provider.
761 *
762 * @see #getRootId(Uri)
763 */
764 public static Uri buildRootUri(String authority, String rootId) {
765 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
766 .authority(authority).appendPath(PATH_ROOT).appendPath(rootId).build();
767 }
768
769 /**
Steve McKayb67bfbf2015-12-08 17:02:03 -0800770 * Builds URI for user home directory on external (local) storage.
771 * {@hide}
772 */
773 public static Uri buildHomeUri() {
774 // TODO: Avoid this type of interpackage copying. Added here to avoid
775 // direct coupling, but not ideal.
Garfield Tan92b96ba2016-11-01 14:33:48 -0700776 return DocumentsContract.buildRootUri(EXTERNAL_STORAGE_PROVIDER_AUTHORITY, "home");
Steve McKayb67bfbf2015-12-08 17:02:03 -0800777 }
778
779 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700780 * Build URI representing the recently modified documents of a specific root
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700781 * in a document provider. When queried, a provider will return zero or more
782 * rows with columns defined by {@link Document}.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700783 *
784 * @see DocumentsProvider#queryRecentDocuments(String, String[])
785 * @see #getRootId(Uri)
786 */
787 public static Uri buildRecentDocumentsUri(String authority, String rootId) {
788 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
789 .authority(authority).appendPath(PATH_ROOT).appendPath(rootId)
790 .appendPath(PATH_RECENT).build();
791 }
792
793 /**
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700794 * Build URI representing access to descendant documents of the given
795 * {@link Document#COLUMN_DOCUMENT_ID}.
796 *
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700797 * @see #getTreeDocumentId(Uri)
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700798 */
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700799 public static Uri buildTreeDocumentUri(String authority, String documentId) {
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700800 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority)
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700801 .appendPath(PATH_TREE).appendPath(documentId).build();
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700802 }
803
804 /**
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700805 * Build URI representing the target {@link Document#COLUMN_DOCUMENT_ID} in
806 * a document provider. When queried, a provider will return a single row
807 * with columns defined by {@link Document}.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700808 *
809 * @see DocumentsProvider#queryDocument(String, String[])
810 * @see #getDocumentId(Uri)
811 */
812 public static Uri buildDocumentUri(String authority, String documentId) {
813 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
814 .authority(authority).appendPath(PATH_DOCUMENT).appendPath(documentId).build();
815 }
816
817 /**
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700818 * Build URI representing the target {@link Document#COLUMN_DOCUMENT_ID} in
819 * a document provider. When queried, a provider will return a single row
820 * with columns defined by {@link Document}.
821 * <p>
822 * However, instead of directly accessing the target document, the returned
823 * URI will leverage access granted through a subtree URI, typically
824 * returned by {@link Intent#ACTION_OPEN_DOCUMENT_TREE}. The target document
825 * must be a descendant (child, grandchild, etc) of the subtree.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700826 * <p>
827 * This is typically used to access documents under a user-selected
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700828 * directory tree, since it doesn't require the user to separately confirm
829 * each new document access.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700830 *
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700831 * @param treeUri the subtree to leverage to gain access to the target
832 * document. The target directory must be a descendant of this
833 * subtree.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700834 * @param documentId the target document, which the caller may not have
835 * direct access to.
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700836 * @see Intent#ACTION_OPEN_DOCUMENT_TREE
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700837 * @see DocumentsProvider#isChildDocument(String, String)
838 * @see #buildDocumentUri(String, String)
839 */
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700840 public static Uri buildDocumentUriUsingTree(Uri treeUri, String documentId) {
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700841 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700842 .authority(treeUri.getAuthority()).appendPath(PATH_TREE)
843 .appendPath(getTreeDocumentId(treeUri)).appendPath(PATH_DOCUMENT)
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700844 .appendPath(documentId).build();
845 }
846
847 /** {@hide} */
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700848 public static Uri buildDocumentUriMaybeUsingTree(Uri baseUri, String documentId) {
849 if (isTreeUri(baseUri)) {
850 return buildDocumentUriUsingTree(baseUri, documentId);
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700851 } else {
852 return buildDocumentUri(baseUri.getAuthority(), documentId);
853 }
854 }
855
856 /**
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700857 * Build URI representing the children of the target directory in a document
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700858 * provider. When queried, a provider will return zero or more rows with
859 * columns defined by {@link Document}.
860 *
861 * @param parentDocumentId the document to return children for, which must
862 * be a directory with MIME type of
863 * {@link Document#MIME_TYPE_DIR}.
864 * @see DocumentsProvider#queryChildDocuments(String, String[], String)
865 * @see #getDocumentId(Uri)
866 */
867 public static Uri buildChildDocumentsUri(String authority, String parentDocumentId) {
868 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority)
869 .appendPath(PATH_DOCUMENT).appendPath(parentDocumentId).appendPath(PATH_CHILDREN)
870 .build();
871 }
872
873 /**
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700874 * Build URI representing the children of the target directory in a document
875 * provider. When queried, a provider will return zero or more rows with
876 * columns defined by {@link Document}.
877 * <p>
878 * However, instead of directly accessing the target directory, the returned
879 * URI will leverage access granted through a subtree URI, typically
880 * returned by {@link Intent#ACTION_OPEN_DOCUMENT_TREE}. The target
881 * directory must be a descendant (child, grandchild, etc) of the subtree.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700882 * <p>
883 * This is typically used to access documents under a user-selected
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700884 * directory tree, since it doesn't require the user to separately confirm
885 * each new document access.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700886 *
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700887 * @param treeUri the subtree to leverage to gain access to the target
888 * document. The target directory must be a descendant of this
889 * subtree.
890 * @param parentDocumentId the document to return children for, which the
891 * caller may not have direct access to, and which must be a
892 * directory with MIME type of {@link Document#MIME_TYPE_DIR}.
893 * @see Intent#ACTION_OPEN_DOCUMENT_TREE
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700894 * @see DocumentsProvider#isChildDocument(String, String)
895 * @see #buildChildDocumentsUri(String, String)
896 */
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700897 public static Uri buildChildDocumentsUriUsingTree(Uri treeUri, String parentDocumentId) {
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700898 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700899 .authority(treeUri.getAuthority()).appendPath(PATH_TREE)
900 .appendPath(getTreeDocumentId(treeUri)).appendPath(PATH_DOCUMENT)
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700901 .appendPath(parentDocumentId).appendPath(PATH_CHILDREN).build();
902 }
903
904 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700905 * Build URI representing a search for matching documents under a specific
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700906 * root in a document provider. When queried, a provider will return zero or
907 * more rows with columns defined by {@link Document}.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700908 *
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700909 * @see DocumentsProvider#querySearchDocuments(String, String, String[])
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700910 * @see #getRootId(Uri)
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700911 * @see #getSearchDocumentsQuery(Uri)
912 */
913 public static Uri buildSearchDocumentsUri(
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700914 String authority, String rootId, String query) {
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700915 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority)
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700916 .appendPath(PATH_ROOT).appendPath(rootId).appendPath(PATH_SEARCH)
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700917 .appendQueryParameter(PARAM_QUERY, query).build();
918 }
919
920 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700921 * Test if the given URI represents a {@link Document} backed by a
Jeff Sharkeyee2f7df2013-09-26 11:32:30 -0700922 * {@link DocumentsProvider}.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700923 *
924 * @see #buildDocumentUri(String, String)
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700925 * @see #buildDocumentUriUsingTree(Uri, String)
Jeff Sharkeyee2f7df2013-09-26 11:32:30 -0700926 */
Steve McKay323ee3e2015-09-25 16:02:56 -0700927 public static boolean isDocumentUri(Context context, @Nullable Uri uri) {
928 if (isContentUri(uri) && isDocumentsProvider(context, uri.getAuthority())) {
929 final List<String> paths = uri.getPathSegments();
930 if (paths.size() == 2) {
931 return PATH_DOCUMENT.equals(paths.get(0));
932 } else if (paths.size() == 4) {
933 return PATH_TREE.equals(paths.get(0)) && PATH_DOCUMENT.equals(paths.get(2));
934 }
Jeff Sharkeyee2f7df2013-09-26 11:32:30 -0700935 }
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700936 return false;
937 }
Jeff Sharkeyee2f7df2013-09-26 11:32:30 -0700938
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700939 /** {@hide} */
Steve McKay323ee3e2015-09-25 16:02:56 -0700940 public static boolean isRootUri(Context context, @Nullable Uri uri) {
941 if (isContentUri(uri) && isDocumentsProvider(context, uri.getAuthority())) {
942 final List<String> paths = uri.getPathSegments();
943 return (paths.size() == 2 && PATH_ROOT.equals(paths.get(0)));
944 }
945 return false;
946 }
947
948 /** {@hide} */
949 public static boolean isContentUri(@Nullable Uri uri) {
950 return uri != null && ContentResolver.SCHEME_CONTENT.equals(uri.getScheme());
951 }
952
Tomasz Mikolajewski7db9c812016-01-28 09:50:01 +0900953 /**
954 * Test if the given URI represents a {@link Document} tree.
955 *
956 * @see #buildTreeDocumentUri(String, String)
Tomasz Mikolajewski90a75332016-07-13 10:14:59 +0900957 * @see #getTreeDocumentId(Uri)
Tomasz Mikolajewski7db9c812016-01-28 09:50:01 +0900958 */
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700959 public static boolean isTreeUri(Uri uri) {
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700960 final List<String> paths = uri.getPathSegments();
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700961 return (paths.size() >= 2 && PATH_TREE.equals(paths.get(0)));
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700962 }
963
964 private static boolean isDocumentsProvider(Context context, String authority) {
Jeff Sharkeyd2e1e812013-10-09 13:31:13 -0700965 final Intent intent = new Intent(PROVIDER_INTERFACE);
966 final List<ResolveInfo> infos = context.getPackageManager()
967 .queryIntentContentProviders(intent, 0);
968 for (ResolveInfo info : infos) {
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700969 if (authority.equals(info.providerInfo.authority)) {
Jeff Sharkeyd2e1e812013-10-09 13:31:13 -0700970 return true;
971 }
Jeff Sharkeyee2f7df2013-09-26 11:32:30 -0700972 }
973 return false;
974 }
975
976 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700977 * Extract the {@link Root#COLUMN_ROOT_ID} from the given URI.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700978 */
979 public static String getRootId(Uri rootUri) {
980 final List<String> paths = rootUri.getPathSegments();
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700981 if (paths.size() >= 2 && PATH_ROOT.equals(paths.get(0))) {
982 return paths.get(1);
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700983 }
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700984 throw new IllegalArgumentException("Invalid URI: " + rootUri);
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700985 }
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700986
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700987 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -0700988 * Extract the {@link Document#COLUMN_DOCUMENT_ID} from the given URI.
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700989 *
990 * @see #isDocumentUri(Context, Uri)
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700991 */
992 public static String getDocumentId(Uri documentUri) {
993 final List<String> paths = documentUri.getPathSegments();
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700994 if (paths.size() >= 2 && PATH_DOCUMENT.equals(paths.get(0))) {
995 return paths.get(1);
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700996 }
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -0700997 if (paths.size() >= 4 && PATH_TREE.equals(paths.get(0))
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700998 && PATH_DOCUMENT.equals(paths.get(2))) {
999 return paths.get(3);
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001000 }
Jeff Sharkey21de56a2014-04-05 19:05:24 -07001001 throw new IllegalArgumentException("Invalid URI: " + documentUri);
1002 }
1003
1004 /**
1005 * Extract the via {@link Document#COLUMN_DOCUMENT_ID} from the given URI.
Jeff Sharkey21de56a2014-04-05 19:05:24 -07001006 */
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -07001007 public static String getTreeDocumentId(Uri documentUri) {
Jeff Sharkey21de56a2014-04-05 19:05:24 -07001008 final List<String> paths = documentUri.getPathSegments();
Jeff Sharkeyb9fbb722014-06-04 16:42:47 -07001009 if (paths.size() >= 2 && PATH_TREE.equals(paths.get(0))) {
Jeff Sharkey21de56a2014-04-05 19:05:24 -07001010 return paths.get(1);
1011 }
1012 throw new IllegalArgumentException("Invalid URI: " + documentUri);
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001013 }
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001014
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001015 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -07001016 * Extract the search query from a URI built by
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001017 * {@link #buildSearchDocumentsUri(String, String, String)}.
1018 */
1019 public static String getSearchDocumentsQuery(Uri searchDocumentsUri) {
1020 return searchDocumentsUri.getQueryParameter(PARAM_QUERY);
Jeff Sharkey20d96d82013-07-30 17:08:39 -07001021 }
1022
Jeff Sharkey4ec97392013-09-10 12:04:26 -07001023 /** {@hide} */
1024 public static Uri setManageMode(Uri uri) {
1025 return uri.buildUpon().appendQueryParameter(PARAM_MANAGE, "true").build();
1026 }
1027
1028 /** {@hide} */
1029 public static boolean isManageMode(Uri uri) {
1030 return uri.getBooleanQueryParameter(PARAM_MANAGE, false);
1031 }
1032
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001033 /**
Jeff Sharkeye8c00d82013-10-15 15:46:10 -07001034 * Return thumbnail representing the document at the given URI. Callers are
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001035 * responsible for their own in-memory caching.
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001036 *
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001037 * @param documentUri document to return thumbnail for, which must have
1038 * {@link Document#FLAG_SUPPORTS_THUMBNAIL} set.
1039 * @param size optimal thumbnail size desired. A provider may return a
1040 * thumbnail of a different size, but never more than double the
1041 * requested size.
Jeff Sharkeye8c00d82013-10-15 15:46:10 -07001042 * @param signal signal used to indicate if caller is no longer interested
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001043 * in the thumbnail.
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001044 * @return decoded thumbnail, or {@code null} if problem was encountered.
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001045 * @see DocumentsProvider#openDocumentThumbnail(String, Point,
1046 * android.os.CancellationSignal)
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001047 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001048 public static Bitmap getDocumentThumbnail(
Ben Lin8ea82002017-03-08 17:30:16 -08001049 ContentResolver resolver, Uri documentUri, Point size, CancellationSignal signal)
1050 throws FileNotFoundException {
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001051 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
1052 documentUri.getAuthority());
1053 try {
1054 return getDocumentThumbnail(client, documentUri, size, signal);
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001055 } catch (Exception e) {
Jeff Sharkey33819312013-10-29 11:56:37 -07001056 if (!(e instanceof OperationCanceledException)) {
1057 Log.w(TAG, "Failed to load thumbnail for " + documentUri + ": " + e);
1058 }
Ben Lin8ea82002017-03-08 17:30:16 -08001059 rethrowIfNecessary(resolver, e);
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001060 return null;
1061 } finally {
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001062 ContentProviderClient.releaseQuietly(client);
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001063 }
1064 }
1065
1066 /** {@hide} */
1067 public static Bitmap getDocumentThumbnail(
1068 ContentProviderClient client, Uri documentUri, Point size, CancellationSignal signal)
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001069 throws RemoteException, IOException {
Jeff Sharkey63983432013-08-21 11:33:50 -07001070 final Bundle openOpts = new Bundle();
Jeff Sharkey5b836f22014-08-27 14:46:32 -07001071 openOpts.putParcelable(ContentResolver.EXTRA_SIZE, size);
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001072
Jeff Sharkey9d0843d2013-05-07 12:41:33 -07001073 AssetFileDescriptor afd = null;
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -07001074 Bitmap bitmap = null;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001075 try {
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001076 afd = client.openTypedAssetFileDescriptor(documentUri, "image/*", openOpts, signal);
Jeff Sharkey9d0843d2013-05-07 12:41:33 -07001077
1078 final FileDescriptor fd = afd.getFileDescriptor();
Jeff Sharkey63983432013-08-21 11:33:50 -07001079 final long offset = afd.getStartOffset();
Jeff Sharkey9d0843d2013-05-07 12:41:33 -07001080
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001081 // Try seeking on the returned FD, since it gives us the most
1082 // optimal decode path; otherwise fall back to buffering.
1083 BufferedInputStream is = null;
1084 try {
Elliott Hughes34385d32014-04-28 11:11:32 -07001085 Os.lseek(fd, offset, SEEK_SET);
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001086 } catch (ErrnoException e) {
1087 is = new BufferedInputStream(new FileInputStream(fd), THUMBNAIL_BUFFER_SIZE);
1088 is.mark(THUMBNAIL_BUFFER_SIZE);
Jeff Sharkey63983432013-08-21 11:33:50 -07001089 }
Jeff Sharkey9d0843d2013-05-07 12:41:33 -07001090
Jeff Sharkey63983432013-08-21 11:33:50 -07001091 // We requested a rough thumbnail size, but the remote size may have
1092 // returned something giant, so defensively scale down as needed.
1093 final BitmapFactory.Options opts = new BitmapFactory.Options();
1094 opts.inJustDecodeBounds = true;
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001095 if (is != null) {
1096 BitmapFactory.decodeStream(is, null, opts);
Jeff Sharkey63983432013-08-21 11:33:50 -07001097 } else {
1098 BitmapFactory.decodeFileDescriptor(fd, null, opts);
1099 }
Jeff Sharkey9d0843d2013-05-07 12:41:33 -07001100
Jeff Sharkey63983432013-08-21 11:33:50 -07001101 final int widthSample = opts.outWidth / size.x;
1102 final int heightSample = opts.outHeight / size.y;
1103
1104 opts.inJustDecodeBounds = false;
1105 opts.inSampleSize = Math.min(widthSample, heightSample);
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001106 if (is != null) {
1107 is.reset();
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -07001108 bitmap = BitmapFactory.decodeStream(is, null, opts);
Jeff Sharkey63983432013-08-21 11:33:50 -07001109 } else {
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001110 try {
Elliott Hughes34385d32014-04-28 11:11:32 -07001111 Os.lseek(fd, offset, SEEK_SET);
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001112 } catch (ErrnoException e) {
1113 e.rethrowAsIOException();
1114 }
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -07001115 bitmap = BitmapFactory.decodeFileDescriptor(fd, null, opts);
1116 }
1117
1118 // Transform the bitmap if requested. We use a side-channel to
1119 // communicate the orientation, since EXIF thumbnails don't contain
1120 // the rotation flags of the original image.
1121 final Bundle extras = afd.getExtras();
1122 final int orientation = (extras != null) ? extras.getInt(EXTRA_ORIENTATION, 0) : 0;
1123 if (orientation != 0) {
1124 final int width = bitmap.getWidth();
1125 final int height = bitmap.getHeight();
1126
1127 final Matrix m = new Matrix();
1128 m.setRotate(orientation, width / 2, height / 2);
1129 bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, false);
Jeff Sharkey63983432013-08-21 11:33:50 -07001130 }
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001131 } finally {
Jeff Sharkey9d0843d2013-05-07 12:41:33 -07001132 IoUtils.closeQuietly(afd);
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001133 }
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -07001134
1135 return bitmap;
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001136 }
1137
Jeff Sharkey5b83f852013-08-14 18:29:19 -07001138 /**
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001139 * Create a new document with given MIME type and display name.
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001140 *
Ben Lin8ea82002017-03-08 17:30:16 -08001141 * @param parentDocumentUri directory with {@link Document#FLAG_DIR_SUPPORTS_CREATE}
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001142 * @param mimeType MIME type of new document
1143 * @param displayName name of new document
1144 * @return newly created document, or {@code null} if failed
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001145 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001146 public static Uri createDocument(ContentResolver resolver, Uri parentDocumentUri,
Ben Lin8ea82002017-03-08 17:30:16 -08001147 String mimeType, String displayName) throws FileNotFoundException {
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001148 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
1149 parentDocumentUri.getAuthority());
1150 try {
1151 return createDocument(client, parentDocumentUri, mimeType, displayName);
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001152 } catch (Exception e) {
1153 Log.w(TAG, "Failed to create document", e);
Ben Lin8ea82002017-03-08 17:30:16 -08001154 rethrowIfNecessary(resolver, e);
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001155 return null;
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001156 } finally {
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001157 ContentProviderClient.releaseQuietly(client);
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001158 }
1159 }
1160
1161 /** {@hide} */
1162 public static Uri createDocument(ContentProviderClient client, Uri parentDocumentUri,
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001163 String mimeType, String displayName) throws RemoteException {
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001164 final Bundle in = new Bundle();
Jeff Sharkey21de56a2014-04-05 19:05:24 -07001165 in.putParcelable(DocumentsContract.EXTRA_URI, parentDocumentUri);
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001166 in.putString(Document.COLUMN_MIME_TYPE, mimeType);
1167 in.putString(Document.COLUMN_DISPLAY_NAME, displayName);
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001168
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001169 final Bundle out = client.call(METHOD_CREATE_DOCUMENT, null, in);
Jeff Sharkey21de56a2014-04-05 19:05:24 -07001170 return out.getParcelable(DocumentsContract.EXTRA_URI);
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001171 }
Jeff Sharkey5b83f852013-08-14 18:29:19 -07001172
Steve McKayd3afdee2015-11-19 17:27:12 -08001173 /** {@hide} */
1174 public static boolean isChildDocument(ContentProviderClient client, Uri parentDocumentUri,
1175 Uri childDocumentUri) throws RemoteException {
1176
1177 final Bundle in = new Bundle();
1178 in.putParcelable(DocumentsContract.EXTRA_URI, parentDocumentUri);
1179 in.putParcelable(DocumentsContract.EXTRA_TARGET_URI, childDocumentUri);
1180
1181 final Bundle out = client.call(METHOD_IS_CHILD_DOCUMENT, null, in);
1182 if (out == null) {
1183 throw new RemoteException("Failed to get a reponse from isChildDocument query.");
1184 }
1185 if (!out.containsKey(DocumentsContract.EXTRA_RESULT)) {
1186 throw new RemoteException("Response did not include result field..");
1187 }
1188 return out.getBoolean(DocumentsContract.EXTRA_RESULT);
1189 }
1190
Jeff Sharkey5b83f852013-08-14 18:29:19 -07001191 /**
Jeff Sharkeyb7e12552014-05-21 22:22:03 -07001192 * Change the display name of an existing document.
1193 * <p>
1194 * If the underlying provider needs to create a new
1195 * {@link Document#COLUMN_DOCUMENT_ID} to represent the updated display
1196 * name, that new document is returned and the original document is no
1197 * longer valid. Otherwise, the original document is returned.
1198 *
1199 * @param documentUri document with {@link Document#FLAG_SUPPORTS_RENAME}
1200 * @param displayName updated name for document
1201 * @return the existing or new document after the rename, or {@code null} if
1202 * failed.
1203 */
1204 public static Uri renameDocument(ContentResolver resolver, Uri documentUri,
Ben Lin8ea82002017-03-08 17:30:16 -08001205 String displayName) throws FileNotFoundException {
Jeff Sharkeyb7e12552014-05-21 22:22:03 -07001206 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
1207 documentUri.getAuthority());
1208 try {
1209 return renameDocument(client, documentUri, displayName);
1210 } catch (Exception e) {
1211 Log.w(TAG, "Failed to rename document", e);
Ben Lin8ea82002017-03-08 17:30:16 -08001212 rethrowIfNecessary(resolver, e);
Jeff Sharkeyb7e12552014-05-21 22:22:03 -07001213 return null;
1214 } finally {
1215 ContentProviderClient.releaseQuietly(client);
1216 }
1217 }
1218
1219 /** {@hide} */
1220 public static Uri renameDocument(ContentProviderClient client, Uri documentUri,
1221 String displayName) throws RemoteException {
1222 final Bundle in = new Bundle();
1223 in.putParcelable(DocumentsContract.EXTRA_URI, documentUri);
1224 in.putString(Document.COLUMN_DISPLAY_NAME, displayName);
1225
1226 final Bundle out = client.call(METHOD_RENAME_DOCUMENT, null, in);
1227 final Uri outUri = out.getParcelable(DocumentsContract.EXTRA_URI);
1228 return (outUri != null) ? outUri : documentUri;
1229 }
1230
1231 /**
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001232 * Delete the given document.
1233 *
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -07001234 * @param documentUri document with {@link Document#FLAG_SUPPORTS_DELETE}
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001235 * @return if the document was deleted successfully.
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001236 */
Ben Lin8ea82002017-03-08 17:30:16 -08001237 public static boolean deleteDocument(ContentResolver resolver, Uri documentUri)
1238 throws FileNotFoundException {
Jeff Sharkeyde2b22f2013-09-11 17:33:06 -07001239 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
1240 documentUri.getAuthority());
1241 try {
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001242 deleteDocument(client, documentUri);
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001243 return true;
1244 } catch (Exception e) {
1245 Log.w(TAG, "Failed to delete document", e);
Ben Lin8ea82002017-03-08 17:30:16 -08001246 rethrowIfNecessary(resolver, e);
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001247 return false;
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001248 } finally {
1249 ContentProviderClient.releaseQuietly(client);
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -07001250 }
Jeff Sharkey5b83f852013-08-14 18:29:19 -07001251 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001252
1253 /** {@hide} */
1254 public static void deleteDocument(ContentProviderClient client, Uri documentUri)
1255 throws RemoteException {
1256 final Bundle in = new Bundle();
Jeff Sharkey21de56a2014-04-05 19:05:24 -07001257 in.putParcelable(DocumentsContract.EXTRA_URI, documentUri);
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001258
1259 client.call(METHOD_DELETE_DOCUMENT, null, in);
1260 }
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -07001261
1262 /**
Tomasz Mikolajewski74fe1812015-06-12 17:13:26 -07001263 * Copies the given document.
1264 *
1265 * @param sourceDocumentUri document with {@link Document#FLAG_SUPPORTS_COPY}
1266 * @param targetParentDocumentUri document which will become a parent of the source
1267 * document's copy.
1268 * @return the copied document, or {@code null} if failed.
Tomasz Mikolajewski74fe1812015-06-12 17:13:26 -07001269 */
1270 public static Uri copyDocument(ContentResolver resolver, Uri sourceDocumentUri,
Ben Lin8ea82002017-03-08 17:30:16 -08001271 Uri targetParentDocumentUri) throws FileNotFoundException {
Tomasz Mikolajewski74fe1812015-06-12 17:13:26 -07001272 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
1273 sourceDocumentUri.getAuthority());
1274 try {
1275 return copyDocument(client, sourceDocumentUri, targetParentDocumentUri);
1276 } catch (Exception e) {
1277 Log.w(TAG, "Failed to copy document", e);
Ben Lin8ea82002017-03-08 17:30:16 -08001278 rethrowIfNecessary(resolver, e);
Tomasz Mikolajewski74fe1812015-06-12 17:13:26 -07001279 return null;
1280 } finally {
1281 ContentProviderClient.releaseQuietly(client);
1282 }
1283 }
1284
1285 /** {@hide} */
1286 public static Uri copyDocument(ContentProviderClient client, Uri sourceDocumentUri,
1287 Uri targetParentDocumentUri) throws RemoteException {
1288 final Bundle in = new Bundle();
1289 in.putParcelable(DocumentsContract.EXTRA_URI, sourceDocumentUri);
1290 in.putParcelable(DocumentsContract.EXTRA_TARGET_URI, targetParentDocumentUri);
1291
1292 final Bundle out = client.call(METHOD_COPY_DOCUMENT, null, in);
1293 return out.getParcelable(DocumentsContract.EXTRA_URI);
1294 }
1295
1296 /**
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +09001297 * Moves the given document under a new parent.
1298 *
1299 * @param sourceDocumentUri document with {@link Document#FLAG_SUPPORTS_MOVE}
Tomasz Mikolajewskid46ecbc2016-01-25 14:26:54 +09001300 * @param sourceParentDocumentUri parent document of the document to move.
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +09001301 * @param targetParentDocumentUri document which will become a new parent of the source
1302 * document.
1303 * @return the moved document, or {@code null} if failed.
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +09001304 */
1305 public static Uri moveDocument(ContentResolver resolver, Uri sourceDocumentUri,
Ben Lin8ea82002017-03-08 17:30:16 -08001306 Uri sourceParentDocumentUri, Uri targetParentDocumentUri) throws FileNotFoundException {
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +09001307 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
1308 sourceDocumentUri.getAuthority());
1309 try {
Tomasz Mikolajewskidcb4efe2016-02-10 19:14:49 +09001310 return moveDocument(client, sourceDocumentUri, sourceParentDocumentUri,
Tomasz Mikolajewskid46ecbc2016-01-25 14:26:54 +09001311 targetParentDocumentUri);
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +09001312 } catch (Exception e) {
1313 Log.w(TAG, "Failed to move document", e);
Ben Lin8ea82002017-03-08 17:30:16 -08001314 rethrowIfNecessary(resolver, e);
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +09001315 return null;
1316 } finally {
1317 ContentProviderClient.releaseQuietly(client);
1318 }
1319 }
1320
1321 /** {@hide} */
1322 public static Uri moveDocument(ContentProviderClient client, Uri sourceDocumentUri,
Tomasz Mikolajewskid46ecbc2016-01-25 14:26:54 +09001323 Uri sourceParentDocumentUri, Uri targetParentDocumentUri) throws RemoteException {
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +09001324 final Bundle in = new Bundle();
1325 in.putParcelable(DocumentsContract.EXTRA_URI, sourceDocumentUri);
Tomasz Mikolajewskid46ecbc2016-01-25 14:26:54 +09001326 in.putParcelable(DocumentsContract.EXTRA_PARENT_URI, sourceParentDocumentUri);
Tomasz Mikolajewskia375a992015-06-25 15:39:27 +09001327 in.putParcelable(DocumentsContract.EXTRA_TARGET_URI, targetParentDocumentUri);
1328
1329 final Bundle out = client.call(METHOD_MOVE_DOCUMENT, null, in);
1330 return out.getParcelable(DocumentsContract.EXTRA_URI);
1331 }
1332
1333 /**
Tomasz Mikolajewskicbcd3942016-01-28 12:39:25 +09001334 * Removes the given document from a parent directory.
1335 *
1336 * <p>In contrast to {@link #deleteDocument} it requires specifying the parent.
1337 * This method is especially useful if the document can be in multiple parents.
1338 *
1339 * @param documentUri document with {@link Document#FLAG_SUPPORTS_REMOVE}
1340 * @param parentDocumentUri parent document of the document to remove.
1341 * @return true if the document was removed successfully.
1342 */
1343 public static boolean removeDocument(ContentResolver resolver, Uri documentUri,
Ben Lin8ea82002017-03-08 17:30:16 -08001344 Uri parentDocumentUri) throws FileNotFoundException {
Tomasz Mikolajewskicbcd3942016-01-28 12:39:25 +09001345 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
1346 documentUri.getAuthority());
1347 try {
1348 removeDocument(client, documentUri, parentDocumentUri);
1349 return true;
1350 } catch (Exception e) {
1351 Log.w(TAG, "Failed to remove document", e);
Ben Lin8ea82002017-03-08 17:30:16 -08001352 rethrowIfNecessary(resolver, e);
Tomasz Mikolajewskicbcd3942016-01-28 12:39:25 +09001353 return false;
1354 } finally {
1355 ContentProviderClient.releaseQuietly(client);
1356 }
1357 }
1358
1359 /** {@hide} */
1360 public static void removeDocument(ContentProviderClient client, Uri documentUri,
1361 Uri parentDocumentUri) throws RemoteException {
1362 final Bundle in = new Bundle();
1363 in.putParcelable(DocumentsContract.EXTRA_URI, documentUri);
1364 in.putParcelable(DocumentsContract.EXTRA_PARENT_URI, parentDocumentUri);
1365
1366 client.call(METHOD_REMOVE_DOCUMENT, null, in);
1367 }
1368
Garfield Tan87877032017-03-22 12:01:14 -07001369 /**
1370 * Ejects the given root. It throws {@link IllegalStateException} when ejection failed.
1371 *
1372 * @param rootUri root with {@link Root#FLAG_SUPPORTS_EJECT} to be ejected
1373 */
1374 public static void ejectRoot(ContentResolver resolver, Uri rootUri) {
Ben Line7822fb2016-06-24 15:21:08 -07001375 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
1376 rootUri.getAuthority());
1377 try {
Garfield Tan87877032017-03-22 12:01:14 -07001378 ejectRoot(client, rootUri);
1379 } catch (RemoteException e) {
1380 e.rethrowAsRuntimeException();
Ben Line7822fb2016-06-24 15:21:08 -07001381 } finally {
1382 ContentProviderClient.releaseQuietly(client);
1383 }
1384 }
1385
1386 /** {@hide} */
Garfield Tan87877032017-03-22 12:01:14 -07001387 public static void ejectRoot(ContentProviderClient client, Uri rootUri)
Ben Line7822fb2016-06-24 15:21:08 -07001388 throws RemoteException {
1389 final Bundle in = new Bundle();
1390 in.putParcelable(DocumentsContract.EXTRA_URI, rootUri);
1391
Garfield Tan87877032017-03-22 12:01:14 -07001392 client.call(METHOD_EJECT_ROOT, null, in);
Ben Line7822fb2016-06-24 15:21:08 -07001393 }
1394
Tomasz Mikolajewskicbcd3942016-01-28 12:39:25 +09001395 /**
Julian Mancinib6505152017-06-27 13:29:09 -07001396 * Returns metadata associated with the document. The type of metadata returned
Steve McKay17a9ce32017-07-27 13:37:14 -07001397 * is specific to the document type. For example the data returned for an image
1398 * file will likely consist primarily or soley of EXIF metadata.
Julian Mancinib6505152017-06-27 13:29:09 -07001399 *
Steve McKay17a9ce32017-07-27 13:37:14 -07001400 * <p>The returned {@link Bundle} will contain zero or more entries depending
1401 * on the type of data supported by the document provider.
Julian Mancinib6505152017-06-27 13:29:09 -07001402 *
Steve McKay17a9ce32017-07-27 13:37:14 -07001403 * <ol>
1404 * <li>A {@link DocumentsContract.METADATA_TYPES} containing a {@code String[]} value.
1405 * The string array identifies the type or types of metadata returned. Each
1406 * value in the can be used to access a {@link Bundle} of data
1407 * containing that type of data.
1408 * <li>An entry each for each type of returned metadata. Each set of metadata is
1409 * itself represented as a bundle and accessible via a string key naming
1410 * the type of data.
1411 * </ol>
Julian Mancinib6505152017-06-27 13:29:09 -07001412 *
Steve McKay17a9ce32017-07-27 13:37:14 -07001413 * <p>Example:
1414 * <p><pre><code>
1415 * Bundle metadata = DocumentsContract.getDocumentMetadata(client, imageDocUri, tags);
1416 * if (metadata.containsKey(DocumentsContract.METADATA_EXIF)) {
1417 * Bundle exif = metadata.getBundle(DocumentsContract.METADATA_EXIF);
1418 * int imageLength = exif.getInt(ExifInterface.TAG_IMAGE_LENGTH);
1419 * }
Julian Mancinib6505152017-06-27 13:29:09 -07001420 * </code></pre>
1421 *
Steve McKay17a9ce32017-07-27 13:37:14 -07001422 * @param documentUri a Document URI
1423 * @return a Bundle of Bundles.
Julian Mancinib6505152017-06-27 13:29:09 -07001424 * {@hide}
1425 */
Steve McKay17a9ce32017-07-27 13:37:14 -07001426 public static Bundle getDocumentMetadata(ContentResolver resolver, Uri documentUri)
Julian Mancinib6505152017-06-27 13:29:09 -07001427 throws FileNotFoundException {
1428 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
1429 documentUri.getAuthority());
1430
1431 try {
Steve McKay17a9ce32017-07-27 13:37:14 -07001432 return getDocumentMetadata(client, documentUri);
Julian Mancinib6505152017-06-27 13:29:09 -07001433 } catch (Exception e) {
1434 Log.w(TAG, "Failed to get document metadata");
1435 rethrowIfNecessary(resolver, e);
1436 return null;
1437 } finally {
1438 ContentProviderClient.releaseQuietly(client);
1439 }
1440 }
1441
1442 /**
1443 * Returns metadata associated with the document. The type of metadata returned
Steve McKay17a9ce32017-07-27 13:37:14 -07001444 * is specific to the document type. For example the data returned for an image
1445 * file will likely consist primarily or soley of EXIF metadata.
Julian Mancinib6505152017-06-27 13:29:09 -07001446 *
Steve McKay17a9ce32017-07-27 13:37:14 -07001447 * <p>The returned {@link Bundle} will contain zero or more entries depending
1448 * on the type of data supported by the document provider.
Julian Mancinib6505152017-06-27 13:29:09 -07001449 *
Steve McKay17a9ce32017-07-27 13:37:14 -07001450 * <ol>
1451 * <li>A {@link DocumentsContract.METADATA_TYPES} containing a {@code String[]} value.
1452 * The string array identifies the type or types of metadata returned. Each
1453 * value in the can be used to access a {@link Bundle} of data
1454 * containing that type of data.
1455 * <li>An entry each for each type of returned metadata. Each set of metadata is
1456 * itself represented as a bundle and accessible via a string key naming
1457 * the type of data.
1458 * </ol>
Julian Mancinib6505152017-06-27 13:29:09 -07001459 *
Steve McKay17a9ce32017-07-27 13:37:14 -07001460 * <p>Example:
1461 * <p><pre><code>
Julian Mancinib6505152017-06-27 13:29:09 -07001462 * Bundle metadata = DocumentsContract.getDocumentMetadata(client, imageDocUri, tags);
Steve McKay17a9ce32017-07-27 13:37:14 -07001463 * if (metadata.containsKey(DocumentsContract.METADATA_EXIF)) {
1464 * Bundle exif = metadata.getBundle(DocumentsContract.METADATA_EXIF);
1465 * int imageLength = exif.getInt(ExifInterface.TAG_IMAGE_LENGTH);
1466 * }
Julian Mancinib6505152017-06-27 13:29:09 -07001467 * </code></pre>
1468 *
Steve McKay17a9ce32017-07-27 13:37:14 -07001469 * @param documentUri a Document URI
1470 * @return a Bundle of Bundles.
Julian Mancinib6505152017-06-27 13:29:09 -07001471 * {@hide}
1472 */
Steve McKay17a9ce32017-07-27 13:37:14 -07001473 public static Bundle getDocumentMetadata(
1474 ContentProviderClient client, Uri documentUri) throws RemoteException {
Julian Mancinib6505152017-06-27 13:29:09 -07001475 final Bundle in = new Bundle();
1476 in.putParcelable(EXTRA_URI, documentUri);
Julian Mancinib6505152017-06-27 13:29:09 -07001477
1478 final Bundle out = client.call(METHOD_GET_DOCUMENT_METADATA, null, in);
1479
1480 if (out == null) {
1481 throw new RemoteException("Failed to get a response from getDocumentMetadata");
1482 }
1483 return out;
1484 }
1485
1486 /**
Garfield Tanb690b4d2017-03-01 16:05:23 -08001487 * Finds the canonical path from the top of the document tree.
Garfield Tanaba97f32016-10-06 17:34:19 +00001488 *
Garfield Tanb690b4d2017-03-01 16:05:23 -08001489 * The {@link Path#getPath()} of the return value contains the document ID
1490 * of all documents along the path from the top the document tree to the
1491 * requested document, both inclusive.
1492 *
1493 * The {@link Path#getRootId()} of the return value returns {@code null}.
Garfield Tan06940e12016-10-07 16:03:17 -07001494 *
1495 * @param treeUri treeUri of the document which path is requested.
Garfield Tanb690b4d2017-03-01 16:05:23 -08001496 * @return the path of the document, or {@code null} if failed.
Garfield Tan3f6b68a2016-11-01 14:13:38 -07001497 * @see DocumentsProvider#findDocumentPath(String, String)
Garfield Tanaba97f32016-10-06 17:34:19 +00001498 */
Ben Lin8ea82002017-03-08 17:30:16 -08001499 public static Path findDocumentPath(ContentResolver resolver, Uri treeUri)
1500 throws FileNotFoundException {
Garfield Tan06940e12016-10-07 16:03:17 -07001501 checkArgument(isTreeUri(treeUri), treeUri + " is not a tree uri.");
1502
Garfield Tanaba97f32016-10-06 17:34:19 +00001503 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
Garfield Tan06940e12016-10-07 16:03:17 -07001504 treeUri.getAuthority());
Garfield Tanaba97f32016-10-06 17:34:19 +00001505 try {
Garfield Tanb690b4d2017-03-01 16:05:23 -08001506 return findDocumentPath(client, treeUri);
Garfield Tanaba97f32016-10-06 17:34:19 +00001507 } catch (Exception e) {
1508 Log.w(TAG, "Failed to find path", e);
Ben Lin8ea82002017-03-08 17:30:16 -08001509 rethrowIfNecessary(resolver, e);
Garfield Tanaba97f32016-10-06 17:34:19 +00001510 return null;
1511 } finally {
1512 ContentProviderClient.releaseQuietly(client);
1513 }
1514 }
1515
Garfield Tan06940e12016-10-07 16:03:17 -07001516 /**
Garfield Tanb690b4d2017-03-01 16:05:23 -08001517 * Finds the canonical path. If uri is a document uri returns path from a root and
1518 * its associated root id. If uri is a tree uri returns the path from the top of
1519 * the tree. The {@link Path#getPath()} of the return value contains document ID
1520 * starts from the top of the tree or the root document to the requested document,
1521 * both inclusive.
Garfield Tan06940e12016-10-07 16:03:17 -07001522 *
Garfield Tanb690b4d2017-03-01 16:05:23 -08001523 * Callers can expect the root ID returned from multiple calls to this method is
1524 * consistent.
Garfield Tan06940e12016-10-07 16:03:17 -07001525 *
1526 * @param uri uri of the document which path is requested. It can be either a
1527 * plain document uri or a tree uri.
1528 * @return the path of the document.
Garfield Tan3f6b68a2016-11-01 14:13:38 -07001529 * @see DocumentsProvider#findDocumentPath(String, String)
Garfield Tan06940e12016-10-07 16:03:17 -07001530 *
1531 * {@hide}
1532 */
Garfield Tan0b3cf662016-10-31 12:59:45 -07001533 public static Path findDocumentPath(ContentProviderClient client, Uri uri)
1534 throws RemoteException {
1535
Garfield Tanaba97f32016-10-06 17:34:19 +00001536 final Bundle in = new Bundle();
Garfield Tan06940e12016-10-07 16:03:17 -07001537 in.putParcelable(DocumentsContract.EXTRA_URI, uri);
Garfield Tanaba97f32016-10-06 17:34:19 +00001538
Garfield Tan3f6b68a2016-11-01 14:13:38 -07001539 final Bundle out = client.call(METHOD_FIND_DOCUMENT_PATH, null, in);
Garfield Tanaba97f32016-10-06 17:34:19 +00001540
1541 return out.getParcelable(DocumentsContract.EXTRA_RESULT);
1542 }
1543
1544 /**
Tomasz Mikolajewskicf316562016-10-24 15:17:01 +09001545 * Creates an intent for obtaining a web link for the specified document.
1546 *
1547 * <p>Note, that due to internal limitations, if there is already a web link
1548 * intent created for the specified document but with different options,
1549 * then it may be overriden.
1550 *
1551 * <p>Providers are required to show confirmation UI for all new permissions granted
1552 * for the linked document.
1553 *
Tomasz Mikolajewski463b6862017-03-22 17:34:05 +09001554 * <p>If list of recipients is known, then it should be passed in options as
1555 * {@link Intent#EXTRA_EMAIL} as a list of email addresses. Note, that
Tomasz Mikolajewskicf316562016-10-24 15:17:01 +09001556 * this is just a hint for the provider, which can ignore the list. In either
1557 * case the provider is required to show a UI for letting the user confirm
1558 * any new permission grants.
1559 *
Tomasz Mikolajewski463b6862017-03-22 17:34:05 +09001560 * <p>Note, that the entire <code>options</code> bundle will be sent to the provider
1561 * backing the passed <code>uri</code>. Make sure that you trust the provider
1562 * before passing any sensitive information.
Tomasz Mikolajewskif1d55402017-03-03 15:09:40 +09001563 *
Tomasz Mikolajewskicf316562016-10-24 15:17:01 +09001564 * <p>Since this API may show a UI, it cannot be called from background.
1565 *
1566 * <p>In order to obtain the Web Link use code like this:
1567 * <pre><code>
1568 * void onSomethingHappened() {
1569 * IntentSender sender = DocumentsContract.createWebLinkIntent(<i>...</i>);
1570 * if (sender != null) {
1571 * startIntentSenderForResult(
Tomasz Mikolajewskif1d55402017-03-03 15:09:40 +09001572 * sender,
Tomasz Mikolajewskicf316562016-10-24 15:17:01 +09001573 * WEB_LINK_REQUEST_CODE,
1574 * null, 0, 0, 0, null);
1575 * }
1576 * }
1577 *
1578 * <i>(...)</i>
1579 *
1580 * void onActivityResult(int requestCode, int resultCode, Intent data) {
1581 * if (requestCode == WEB_LINK_REQUEST_CODE && resultCode == RESULT_OK) {
1582 * Uri weblinkUri = data.getData();
1583 * <i>...</i>
1584 * }
1585 * }
1586 * </code></pre>
1587 *
1588 * @param uri uri for the document to create a link to.
1589 * @param options Extra information for generating the link.
1590 * @return an intent sender to obtain the web link, or null if the document
1591 * is not linkable, or creating the intent sender failed.
1592 * @see DocumentsProvider#createWebLinkIntent(String, Bundle)
1593 * @see Intent#EXTRA_EMAIL
1594 */
1595 public static IntentSender createWebLinkIntent(ContentResolver resolver, Uri uri,
Ben Lin8ea82002017-03-08 17:30:16 -08001596 Bundle options) throws FileNotFoundException {
Tomasz Mikolajewskicf316562016-10-24 15:17:01 +09001597 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
1598 uri.getAuthority());
1599 try {
1600 return createWebLinkIntent(client, uri, options);
1601 } catch (Exception e) {
1602 Log.w(TAG, "Failed to create a web link intent", e);
Ben Lin8ea82002017-03-08 17:30:16 -08001603 rethrowIfNecessary(resolver, e);
Tomasz Mikolajewskicf316562016-10-24 15:17:01 +09001604 return null;
1605 } finally {
1606 ContentProviderClient.releaseQuietly(client);
1607 }
1608 }
1609
1610 /**
1611 * {@hide}
1612 */
1613 public static IntentSender createWebLinkIntent(ContentProviderClient client, Uri uri,
1614 Bundle options) throws RemoteException {
1615 final Bundle in = new Bundle();
1616 in.putParcelable(DocumentsContract.EXTRA_URI, uri);
1617
1618 // Options may be provider specific, so put them in a separate bundle to
1619 // avoid overriding the Uri.
1620 if (options != null) {
1621 in.putBundle(EXTRA_OPTIONS, options);
1622 }
1623
1624 final Bundle out = client.call(METHOD_CREATE_WEB_LINK_INTENT, null, in);
1625 return out.getParcelable(DocumentsContract.EXTRA_RESULT);
1626 }
1627
1628 /**
Jeff Sharkeyc1c8f3f2013-10-14 14:57:33 -07001629 * Open the given image for thumbnail purposes, using any embedded EXIF
1630 * thumbnail if available, and providing orientation hints from the parent
1631 * image.
1632 *
1633 * @hide
1634 */
1635 public static AssetFileDescriptor openImageThumbnail(File file) throws FileNotFoundException {
1636 final ParcelFileDescriptor pfd = ParcelFileDescriptor.open(
1637 file, ParcelFileDescriptor.MODE_READ_ONLY);
1638 Bundle extras = null;
1639
1640 try {
1641 final ExifInterface exif = new ExifInterface(file.getAbsolutePath());
1642
1643 switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1)) {
1644 case ExifInterface.ORIENTATION_ROTATE_90:
1645 extras = new Bundle(1);
1646 extras.putInt(EXTRA_ORIENTATION, 90);
1647 break;
1648 case ExifInterface.ORIENTATION_ROTATE_180:
1649 extras = new Bundle(1);
1650 extras.putInt(EXTRA_ORIENTATION, 180);
1651 break;
1652 case ExifInterface.ORIENTATION_ROTATE_270:
1653 extras = new Bundle(1);
1654 extras.putInt(EXTRA_ORIENTATION, 270);
1655 break;
1656 }
1657
1658 final long[] thumb = exif.getThumbnailRange();
1659 if (thumb != null) {
1660 return new AssetFileDescriptor(pfd, thumb[0], thumb[1], extras);
1661 }
1662 } catch (IOException e) {
1663 }
1664
1665 return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH, extras);
1666 }
Garfield Tanaba97f32016-10-06 17:34:19 +00001667
Ben Lin8ea82002017-03-08 17:30:16 -08001668 private static void rethrowIfNecessary(ContentResolver resolver, Exception e)
1669 throws FileNotFoundException {
1670 // We only want to throw applications targetting O and above
1671 if (resolver.getTargetSdkVersion() >= Build.VERSION_CODES.O) {
1672 if (e instanceof ParcelableException) {
1673 ((ParcelableException) e).maybeRethrow(FileNotFoundException.class);
Ben Lin227e6e82017-03-13 15:36:13 -07001674 } else if (e instanceof RemoteException) {
Ben Lin8ea82002017-03-08 17:30:16 -08001675 ((RemoteException) e).rethrowAsRuntimeException();
1676 } else if (e instanceof RuntimeException) {
1677 throw (RuntimeException) e;
1678 }
1679 }
1680 }
1681
Garfield Tanaba97f32016-10-06 17:34:19 +00001682 /**
Garfield Tan0b3cf662016-10-31 12:59:45 -07001683 * Holds a path from a document to a particular document under it. It
1684 * may also contains the root ID where the path resides.
Garfield Tanaba97f32016-10-06 17:34:19 +00001685 */
1686 public static final class Path implements Parcelable {
1687
Garfield Tan06940e12016-10-07 16:03:17 -07001688 private final @Nullable String mRootId;
1689 private final List<String> mPath;
Garfield Tanaba97f32016-10-06 17:34:19 +00001690
1691 /**
1692 * Creates a Path.
Garfield Tan06940e12016-10-07 16:03:17 -07001693 *
1694 * @param rootId the ID of the root. May be null.
Garfield Tan0b3cf662016-10-31 12:59:45 -07001695 * @param path the list of document ID from the parent document at
Garfield Tan06940e12016-10-07 16:03:17 -07001696 * position 0 to the child document.
Garfield Tanaba97f32016-10-06 17:34:19 +00001697 */
Garfield Tan5f214802016-10-26 14:52:46 -07001698 public Path(@Nullable String rootId, List<String> path) {
Garfield Tan06940e12016-10-07 16:03:17 -07001699 checkCollectionNotEmpty(path, "path");
1700 checkCollectionElementsNotNull(path, "path");
1701
Garfield Tanaba97f32016-10-06 17:34:19 +00001702 mRootId = rootId;
1703 mPath = path;
1704 }
1705
Garfield Tan06940e12016-10-07 16:03:17 -07001706 /**
1707 * Returns the root id or null if the calling package doesn't have
1708 * permission to access root information.
1709 */
1710 public @Nullable String getRootId() {
1711 return mRootId;
1712 }
1713
1714 /**
1715 * Returns the path. The path is trimmed to the top of tree if
1716 * calling package doesn't have permission to access those
1717 * documents.
1718 */
1719 public List<String> getPath() {
1720 return mPath;
1721 }
1722
1723 @Override
1724 public boolean equals(Object o) {
1725 if (this == o) {
1726 return true;
1727 }
1728 if (o == null || !(o instanceof Path)) {
1729 return false;
1730 }
1731 Path path = (Path) o;
1732 return Objects.equals(mRootId, path.mRootId) &&
1733 Objects.equals(mPath, path.mPath);
1734 }
1735
1736 @Override
1737 public int hashCode() {
1738 return Objects.hash(mRootId, mPath);
1739 }
1740
1741 @Override
1742 public String toString() {
1743 return new StringBuilder()
1744 .append("DocumentsContract.Path{")
1745 .append("rootId=")
1746 .append(mRootId)
1747 .append(", path=")
1748 .append(mPath)
1749 .append("}")
1750 .toString();
1751 }
1752
Garfield Tanaba97f32016-10-06 17:34:19 +00001753 @Override
1754 public void writeToParcel(Parcel dest, int flags) {
1755 dest.writeString(mRootId);
1756 dest.writeStringList(mPath);
1757 }
1758
1759 @Override
1760 public int describeContents() {
1761 return 0;
1762 }
1763
1764 public static final Creator<Path> CREATOR = new Creator<Path>() {
1765 @Override
1766 public Path createFromParcel(Parcel in) {
1767 final String rootId = in.readString();
1768 final List<String> path = in.createStringArrayList();
1769 return new Path(rootId, path);
1770 }
1771
1772 @Override
1773 public Path[] newArray(int size) {
1774 return new Path[size];
1775 }
1776 };
1777 }
Jeff Sharkey9ecfee02013-04-19 14:05:03 -07001778}