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