Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 com.android.documentsui; |
| 18 | |
Garfield Tan | 1566618 | 2017-01-18 16:27:55 -0800 | [diff] [blame] | 19 | import android.content.ContentResolver; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 20 | import android.content.Context; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 21 | import android.content.SharedPreferences; |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 22 | import android.content.pm.ProviderInfo; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 23 | import android.content.res.AssetFileDescriptor; |
| 24 | import android.database.Cursor; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 25 | import android.database.MatrixCursor; |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 26 | import android.database.MatrixCursor.RowBuilder; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 27 | import android.graphics.Point; |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 28 | import android.net.Uri; |
Tony Huang | 91df871 | 2019-11-18 19:57:32 +0800 | [diff] [blame] | 29 | import android.os.Bundle; |
| 30 | import android.os.CancellationSignal; |
| 31 | import android.os.FileUtils; |
| 32 | import android.os.Handler; |
| 33 | import android.os.Looper; |
| 34 | import android.os.ParcelFileDescriptor; |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 35 | import android.provider.DocumentsContract; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 36 | import android.provider.DocumentsContract.Document; |
| 37 | import android.provider.DocumentsContract.Root; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 38 | import android.provider.DocumentsProvider; |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 39 | import android.text.TextUtils; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 40 | import android.util.Log; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 41 | |
Tony Huang | 91df871 | 2019-11-18 19:57:32 +0800 | [diff] [blame] | 42 | import androidx.annotation.VisibleForTesting; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 43 | |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 44 | import java.io.File; |
| 45 | import java.io.FileNotFoundException; |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 46 | import java.io.FileOutputStream; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 47 | import java.io.IOException; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 48 | import java.io.InputStream; |
| 49 | import java.io.OutputStream; |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 50 | import java.util.ArrayList; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 51 | import java.util.Arrays; |
| 52 | import java.util.Collection; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 53 | import java.util.HashMap; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 54 | import java.util.HashSet; |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 55 | import java.util.List; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 56 | import java.util.Map; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 57 | import java.util.Set; |
Garfield Tan | 1e80be7 | 2017-02-27 18:16:00 -0800 | [diff] [blame] | 58 | import java.util.concurrent.CountDownLatch; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 59 | |
| 60 | public class StubProvider extends DocumentsProvider { |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 61 | |
| 62 | public static final String DEFAULT_AUTHORITY = "com.android.documentsui.stubprovider"; |
| 63 | public static final String ROOT_0_ID = "TEST_ROOT_0"; |
| 64 | public static final String ROOT_1_ID = "TEST_ROOT_1"; |
| 65 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 66 | public static final String EXTRA_SIZE = "com.android.documentsui.stubprovider.SIZE"; |
| 67 | public static final String EXTRA_ROOT = "com.android.documentsui.stubprovider.ROOT"; |
| 68 | public static final String EXTRA_PATH = "com.android.documentsui.stubprovider.PATH"; |
| 69 | public static final String EXTRA_STREAM_TYPES |
| 70 | = "com.android.documentsui.stubprovider.STREAM_TYPES"; |
| 71 | public static final String EXTRA_CONTENT = "com.android.documentsui.stubprovider.CONTENT"; |
Garfield Tan | ceedd1f | 2017-05-17 14:10:12 -0700 | [diff] [blame] | 72 | public static final String EXTRA_ENABLE_ROOT_NOTIFICATION |
| 73 | = "com.android.documentsui.stubprovider.ROOT_NOTIFICATION"; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 74 | |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 75 | public static final String EXTRA_FLAGS = "com.android.documentsui.stubprovider.FLAGS"; |
| 76 | public static final String EXTRA_PARENT_ID = "com.android.documentsui.stubprovider.PARENT"; |
| 77 | |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 78 | private static final String TAG = "StubProvider"; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 79 | |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 80 | private static final String STORAGE_SIZE_KEY = "documentsui.stubprovider.size"; |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 81 | private static int DEFAULT_ROOT_SIZE = 1024 * 1024 * 500; // 500 MB. |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 82 | |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 83 | private static final String[] DEFAULT_ROOT_PROJECTION = new String[] { |
| 84 | Root.COLUMN_ROOT_ID, Root.COLUMN_FLAGS, Root.COLUMN_TITLE, Root.COLUMN_DOCUMENT_ID, |
| 85 | Root.COLUMN_AVAILABLE_BYTES |
| 86 | }; |
| 87 | private static final String[] DEFAULT_DOCUMENT_PROJECTION = new String[] { |
| 88 | Document.COLUMN_DOCUMENT_ID, Document.COLUMN_MIME_TYPE, Document.COLUMN_DISPLAY_NAME, |
| 89 | Document.COLUMN_LAST_MODIFIED, Document.COLUMN_FLAGS, Document.COLUMN_SIZE, |
| 90 | }; |
| 91 | |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 92 | private final Map<String, StubDocument> mStorage = new HashMap<>(); |
| 93 | private final Map<String, RootInfo> mRoots = new HashMap<>(); |
| 94 | private final Object mWriteLock = new Object(); |
| 95 | |
| 96 | private String mAuthority = DEFAULT_AUTHORITY; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 97 | private SharedPreferences mPrefs; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 98 | private Set<String> mSimulateReadErrorIds = new HashSet<>(); |
Garfield Tan | 1566618 | 2017-01-18 16:27:55 -0800 | [diff] [blame] | 99 | private long mLoadingDuration = 0; |
Garfield Tan | ceedd1f | 2017-05-17 14:10:12 -0700 | [diff] [blame] | 100 | private boolean mRootNotification = true; |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 101 | |
| 102 | @Override |
| 103 | public void attachInfo(Context context, ProviderInfo info) { |
| 104 | mAuthority = info.authority; |
| 105 | super.attachInfo(context, info); |
| 106 | } |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 107 | |
| 108 | @Override |
| 109 | public boolean onCreate() { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 110 | clearCacheAndBuildRoots(); |
| 111 | return true; |
| 112 | } |
| 113 | |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 114 | @VisibleForTesting |
| 115 | public void clearCacheAndBuildRoots() { |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 116 | Log.d(TAG, "Resetting storage."); |
| 117 | removeChildrenRecursively(getContext().getCacheDir()); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 118 | mStorage.clear(); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 119 | mSimulateReadErrorIds.clear(); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 120 | |
| 121 | mPrefs = getContext().getSharedPreferences( |
| 122 | "com.android.documentsui.stubprovider.preferences", Context.MODE_PRIVATE); |
| 123 | Collection<String> rootIds = mPrefs.getStringSet("roots", null); |
| 124 | if (rootIds == null) { |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 125 | rootIds = Arrays.asList(new String[] { ROOT_0_ID, ROOT_1_ID }); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 126 | } |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 127 | |
| 128 | mRoots.clear(); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 129 | for (String rootId : rootIds) { |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 130 | // Make a subdir in the cache dir for each root. |
| 131 | final File file = new File(getContext().getCacheDir(), rootId); |
| 132 | if (file.mkdir()) { |
| 133 | Log.i(TAG, "Created new root directory @ " + file.getPath()); |
| 134 | } |
| 135 | final RootInfo rootInfo = new RootInfo(file, getSize(rootId)); |
Aga Wronska | ee8ad9c | 2016-02-01 12:23:48 -0800 | [diff] [blame] | 136 | |
| 137 | if(rootId.equals(ROOT_1_ID)) { |
| 138 | rootInfo.setSearchEnabled(false); |
| 139 | } |
| 140 | |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 141 | mStorage.put(rootInfo.document.documentId, rootInfo.document); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 142 | mRoots.put(rootId, rootInfo); |
| 143 | } |
Garfield Tan | 1566618 | 2017-01-18 16:27:55 -0800 | [diff] [blame] | 144 | |
| 145 | mLoadingDuration = 0; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @return Storage size, in bytes. |
| 150 | */ |
| 151 | private long getSize(String rootId) { |
| 152 | final String key = STORAGE_SIZE_KEY + "." + rootId; |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 153 | return mPrefs.getLong(key, DEFAULT_ROOT_SIZE); |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Cursor queryRoots(String[] projection) throws FileNotFoundException { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 158 | final MatrixCursor result = new MatrixCursor(projection != null ? projection |
| 159 | : DEFAULT_ROOT_PROJECTION); |
| 160 | for (Map.Entry<String, RootInfo> entry : mRoots.entrySet()) { |
| 161 | final String id = entry.getKey(); |
| 162 | final RootInfo info = entry.getValue(); |
| 163 | final RowBuilder row = result.newRow(); |
| 164 | row.add(Root.COLUMN_ROOT_ID, id); |
Aga Wronska | ee8ad9c | 2016-02-01 12:23:48 -0800 | [diff] [blame] | 165 | row.add(Root.COLUMN_FLAGS, info.flags); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 166 | row.add(Root.COLUMN_TITLE, id); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 167 | row.add(Root.COLUMN_DOCUMENT_ID, info.document.documentId); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 168 | row.add(Root.COLUMN_AVAILABLE_BYTES, info.getRemainingCapacity()); |
| 169 | } |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 170 | return result; |
| 171 | } |
| 172 | |
| 173 | @Override |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 174 | public Cursor queryDocument(String documentId, String[] projection) |
| 175 | throws FileNotFoundException { |
| 176 | final MatrixCursor result = new MatrixCursor(projection != null ? projection |
| 177 | : DEFAULT_DOCUMENT_PROJECTION); |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 178 | final StubDocument file = mStorage.get(documentId); |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 179 | if (file == null) { |
| 180 | throw new FileNotFoundException(); |
| 181 | } |
Tomasz Mikolajewski | bcda3d1 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 182 | includeDocument(result, file); |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 183 | return result; |
| 184 | } |
| 185 | |
| 186 | @Override |
Tomasz Mikolajewski | 67f9bed | 2015-04-08 19:38:55 +0900 | [diff] [blame] | 187 | public boolean isChildDocument(String parentDocId, String docId) { |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 188 | final StubDocument parentDocument = mStorage.get(parentDocId); |
| 189 | final StubDocument childDocument = mStorage.get(docId); |
Tony Huang | 91df871 | 2019-11-18 19:57:32 +0800 | [diff] [blame] | 190 | |
| 191 | if (parentDocument.file == null || childDocument.file == null) { |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | return contains( |
| 196 | parentDocument.file.getAbsolutePath(), childDocument.file.getAbsolutePath()); |
| 197 | } |
| 198 | |
| 199 | private static boolean contains(String dirPath, String filePath) { |
| 200 | if (dirPath.equals(filePath)) { |
| 201 | return true; |
| 202 | } |
| 203 | if (!dirPath.endsWith("/")) { |
| 204 | dirPath += "/"; |
| 205 | } |
| 206 | return filePath.startsWith(dirPath); |
Tomasz Mikolajewski | 67f9bed | 2015-04-08 19:38:55 +0900 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 210 | public String createDocument(String parentId, String mimeType, String displayName) |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 211 | throws FileNotFoundException { |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 212 | StubDocument parent = mStorage.get(parentId); |
| 213 | File file = createFile(parent, mimeType, displayName); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 214 | |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 215 | final StubDocument document = StubDocument.createRegularDocument(file, mimeType, parent); |
| 216 | mStorage.put(document.documentId, document); |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 217 | Log.d(TAG, "Created document " + document.documentId); |
Tomasz Mikolajewski | bcda3d1 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 218 | notifyParentChanged(document.parentId); |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 219 | getContext().getContentResolver().notifyChange( |
| 220 | DocumentsContract.buildDocumentUri(mAuthority, document.documentId), |
| 221 | null, false); |
| 222 | |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 223 | return document.documentId; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 227 | public void deleteDocument(String documentId) |
| 228 | throws FileNotFoundException { |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 229 | final StubDocument document = mStorage.get(documentId); |
| 230 | final long fileSize = document.file.length(); |
| 231 | if (document == null || !document.file.delete()) |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 232 | throw new FileNotFoundException(); |
| 233 | synchronized (mWriteLock) { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 234 | document.rootInfo.size -= fileSize; |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 235 | mStorage.remove(documentId); |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 236 | } |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 237 | Log.d(TAG, "Document deleted: " + documentId); |
Tomasz Mikolajewski | bcda3d1 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 238 | notifyParentChanged(document.parentId); |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 239 | getContext().getContentResolver().notifyChange( |
| 240 | DocumentsContract.buildDocumentUri(mAuthority, document.documentId), |
| 241 | null, false); |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
Steve McKay | 31e104f | 2015-12-02 11:20:54 -0800 | [diff] [blame] | 245 | public Cursor queryChildDocumentsForManage(String parentDocumentId, String[] projection, |
| 246 | String sortOrder) throws FileNotFoundException { |
| 247 | return queryChildDocuments(parentDocumentId, projection, sortOrder); |
| 248 | } |
| 249 | |
| 250 | @Override |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 251 | public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder) |
| 252 | throws FileNotFoundException { |
Garfield Tan | 1566618 | 2017-01-18 16:27:55 -0800 | [diff] [blame] | 253 | if (mLoadingDuration > 0) { |
| 254 | final Uri notifyUri = DocumentsContract.buildDocumentUri(mAuthority, parentDocumentId); |
| 255 | final ContentResolver resolver = getContext().getContentResolver(); |
| 256 | new Handler(Looper.getMainLooper()).postDelayed( |
| 257 | () -> resolver.notifyChange(notifyUri, null, false), |
| 258 | mLoadingDuration); |
| 259 | mLoadingDuration = 0; |
| 260 | |
| 261 | MatrixCursor cursor = new MatrixCursor(projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION); |
| 262 | Bundle bundle = new Bundle(); |
| 263 | bundle.putBoolean(DocumentsContract.EXTRA_LOADING, true); |
| 264 | cursor.setExtras(bundle); |
| 265 | cursor.setNotificationUri(resolver, notifyUri); |
| 266 | return cursor; |
| 267 | } else { |
| 268 | final StubDocument parentDocument = mStorage.get(parentDocumentId); |
| 269 | if (parentDocument == null || parentDocument.file.isFile()) { |
| 270 | throw new FileNotFoundException(); |
Tomasz Mikolajewski | bcda3d1 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 271 | } |
Garfield Tan | 1566618 | 2017-01-18 16:27:55 -0800 | [diff] [blame] | 272 | final MatrixCursor result = new MatrixCursor(projection != null ? projection |
| 273 | : DEFAULT_DOCUMENT_PROJECTION); |
| 274 | result.setNotificationUri(getContext().getContentResolver(), |
| 275 | DocumentsContract.buildChildDocumentsUri(mAuthority, parentDocumentId)); |
| 276 | StubDocument document; |
| 277 | for (File file : parentDocument.file.listFiles()) { |
| 278 | document = mStorage.get(getDocumentIdForFile(file)); |
| 279 | if (document != null) { |
| 280 | includeDocument(result, document); |
| 281 | } |
| 282 | } |
| 283 | return result; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 284 | } |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public Cursor queryRecentDocuments(String rootId, String[] projection) |
| 289 | throws FileNotFoundException { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 290 | final MatrixCursor result = new MatrixCursor(projection != null ? projection |
| 291 | : DEFAULT_DOCUMENT_PROJECTION); |
| 292 | return result; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | @Override |
Aga Wronska | 1f5b892 | 2016-01-14 11:15:20 -0800 | [diff] [blame] | 296 | public Cursor querySearchDocuments(String rootId, String query, String[] projection) |
| 297 | throws FileNotFoundException { |
| 298 | |
| 299 | StubDocument parentDocument = mRoots.get(rootId).document; |
| 300 | if (parentDocument == null || parentDocument.file.isFile()) { |
| 301 | throw new FileNotFoundException(); |
| 302 | } |
| 303 | |
| 304 | final MatrixCursor result = new MatrixCursor( |
| 305 | projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION); |
| 306 | |
| 307 | for (File file : parentDocument.file.listFiles()) { |
| 308 | if (file.getName().toLowerCase().contains(query)) { |
| 309 | StubDocument document = mStorage.get(getDocumentIdForFile(file)); |
| 310 | if (document != null) { |
| 311 | includeDocument(result, document); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | return result; |
| 316 | } |
| 317 | |
| 318 | @Override |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 319 | public String renameDocument(String documentId, String displayName) |
| 320 | throws FileNotFoundException { |
| 321 | |
| 322 | StubDocument oldDoc = mStorage.get(documentId); |
| 323 | |
| 324 | File before = oldDoc.file; |
| 325 | File after = new File(before.getParentFile(), displayName); |
| 326 | |
| 327 | if (after.exists()) { |
| 328 | throw new IllegalStateException("Already exists " + after); |
| 329 | } |
| 330 | |
| 331 | boolean result = before.renameTo(after); |
| 332 | |
| 333 | if (!result) { |
| 334 | throw new IllegalStateException("Failed to rename to " + after); |
| 335 | } |
| 336 | |
| 337 | StubDocument newDoc = StubDocument.createRegularDocument(after, oldDoc.mimeType, |
| 338 | mStorage.get(oldDoc.parentId)); |
| 339 | |
| 340 | mStorage.remove(documentId); |
| 341 | notifyParentChanged(oldDoc.parentId); |
| 342 | getContext().getContentResolver().notifyChange( |
| 343 | DocumentsContract.buildDocumentUri(mAuthority, oldDoc.documentId), null, false); |
| 344 | |
| 345 | mStorage.put(newDoc.documentId, newDoc); |
| 346 | notifyParentChanged(newDoc.parentId); |
| 347 | getContext().getContentResolver().notifyChange( |
| 348 | DocumentsContract.buildDocumentUri(mAuthority, newDoc.documentId), null, false); |
| 349 | |
| 350 | if (!TextUtils.equals(documentId, newDoc.documentId)) { |
| 351 | return newDoc.documentId; |
| 352 | } else { |
| 353 | return null; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | @Override |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 358 | public ParcelFileDescriptor openDocument(String docId, String mode, CancellationSignal signal) |
| 359 | throws FileNotFoundException { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 360 | |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 361 | final StubDocument document = mStorage.get(docId); |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 362 | if (document == null || !document.file.isFile()) { |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 363 | throw new FileNotFoundException(); |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 364 | } |
| 365 | if ((document.flags & Document.FLAG_VIRTUAL_DOCUMENT) != 0) { |
| 366 | throw new IllegalStateException("Tried to open a virtual file."); |
| 367 | } |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 368 | |
| 369 | if ("r".equals(mode)) { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 370 | if (mSimulateReadErrorIds.contains(docId)) { |
| 371 | Log.d(TAG, "Simulated errs enabled. Open in the wrong mode."); |
| 372 | return ParcelFileDescriptor.open( |
| 373 | document.file, ParcelFileDescriptor.MODE_WRITE_ONLY); |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 374 | } |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 375 | return ParcelFileDescriptor.open(document.file, ParcelFileDescriptor.MODE_READ_ONLY); |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 376 | } |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 377 | if ("w".equals(mode)) { |
| 378 | return startWrite(document); |
| 379 | } |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 380 | if ("wa".equals(mode)) { |
| 381 | return startWrite(document, true); |
| 382 | } |
| 383 | |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 384 | |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 385 | throw new FileNotFoundException(); |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 386 | } |
| 387 | |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 388 | @VisibleForTesting |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 389 | public void simulateReadErrorsForFile(Uri uri) { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 390 | simulateReadErrorsForFile(DocumentsContract.getDocumentId(uri)); |
| 391 | } |
| 392 | |
| 393 | public void simulateReadErrorsForFile(String id) { |
| 394 | mSimulateReadErrorIds.add(id); |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 397 | @Override |
| 398 | public AssetFileDescriptor openDocumentThumbnail( |
| 399 | String docId, Point sizeHint, CancellationSignal signal) throws FileNotFoundException { |
| 400 | throw new FileNotFoundException(); |
| 401 | } |
| 402 | |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 403 | @Override |
| 404 | public AssetFileDescriptor openTypedDocument( |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 405 | String docId, String mimeTypeFilter, Bundle opts, CancellationSignal signal) |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 406 | throws FileNotFoundException { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 407 | final StubDocument document = mStorage.get(docId); |
Tomasz Mikolajewski | e475d3b | 2015-12-24 13:24:00 +0900 | [diff] [blame] | 408 | if (document == null || !document.file.isFile() || document.streamTypes == null) { |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 409 | throw new FileNotFoundException(); |
| 410 | } |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 411 | for (final String mimeType : document.streamTypes) { |
| 412 | // Strict compare won't accept wildcards, but that's OK for tests, as DocumentsUI |
Tomasz Mikolajewski | dcec9ac | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 413 | // doesn't use them for getStreamTypes nor openTypedDocument. |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 414 | if (mimeType.equals(mimeTypeFilter)) { |
| 415 | ParcelFileDescriptor pfd = ParcelFileDescriptor.open( |
| 416 | document.file, ParcelFileDescriptor.MODE_READ_ONLY); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 417 | if (mSimulateReadErrorIds.contains(docId)) { |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 418 | pfd = new ParcelFileDescriptor(pfd) { |
| 419 | @Override |
| 420 | public void checkError() throws IOException { |
| 421 | throw new IOException("Test error"); |
| 422 | } |
| 423 | }; |
| 424 | } |
| 425 | return new AssetFileDescriptor(pfd, 0, document.file.length()); |
| 426 | } |
| 427 | } |
| 428 | throw new IllegalArgumentException("Invalid MIME type filter for openTypedDocument()."); |
| 429 | } |
| 430 | |
Tomasz Mikolajewski | dcec9ac | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 431 | @Override |
| 432 | public String[] getStreamTypes(Uri uri, String mimeTypeFilter) { |
| 433 | final StubDocument document = mStorage.get(DocumentsContract.getDocumentId(uri)); |
| 434 | if (document == null) { |
| 435 | throw new IllegalArgumentException( |
| 436 | "The provided Uri is incorrect, or the file is gone."); |
| 437 | } |
Tomasz Mikolajewski | dcec9ac | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 438 | if (!"*/*".equals(mimeTypeFilter)) { |
| 439 | // Not used by DocumentsUI, so don't bother implementing it. |
| 440 | throw new UnsupportedOperationException(); |
| 441 | } |
Tomasz Mikolajewski | e475d3b | 2015-12-24 13:24:00 +0900 | [diff] [blame] | 442 | if (document.streamTypes == null) { |
| 443 | return null; |
| 444 | } |
Tomasz Mikolajewski | dcec9ac | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 445 | return document.streamTypes.toArray(new String[document.streamTypes.size()]); |
| 446 | } |
| 447 | |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 448 | private ParcelFileDescriptor startWrite(final StubDocument document) |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 449 | throws FileNotFoundException { |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 450 | return startWrite(document, false); |
| 451 | } |
| 452 | |
| 453 | private ParcelFileDescriptor startWrite(final StubDocument document, boolean append) |
| 454 | throws FileNotFoundException { |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 455 | ParcelFileDescriptor[] pipe; |
| 456 | try { |
| 457 | pipe = ParcelFileDescriptor.createReliablePipe(); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 458 | } catch (IOException exception) { |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 459 | throw new FileNotFoundException(); |
| 460 | } |
| 461 | final ParcelFileDescriptor readPipe = pipe[0]; |
| 462 | final ParcelFileDescriptor writePipe = pipe[1]; |
| 463 | |
Garfield Tan | 1e80be7 | 2017-02-27 18:16:00 -0800 | [diff] [blame] | 464 | postToMainThread(() -> { |
| 465 | InputStream inputStream = null; |
| 466 | OutputStream outputStream = null; |
| 467 | try { |
| 468 | Log.d(TAG, "Opening write stream on file " + document.documentId); |
| 469 | inputStream = new ParcelFileDescriptor.AutoCloseInputStream(readPipe); |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 470 | outputStream = new FileOutputStream(document.file, append); |
Garfield Tan | 1e80be7 | 2017-02-27 18:16:00 -0800 | [diff] [blame] | 471 | byte[] buffer = new byte[32 * 1024]; |
| 472 | int bytesToRead; |
| 473 | int bytesRead = 0; |
| 474 | while (bytesRead != -1) { |
| 475 | synchronized (mWriteLock) { |
| 476 | // This cast is safe because the max possible value is buffer.length. |
| 477 | bytesToRead = (int) Math.min(document.rootInfo.getRemainingCapacity(), |
| 478 | buffer.length); |
| 479 | if (bytesToRead == 0) { |
| 480 | closePipeWithErrorSilently(readPipe, "Not enough space."); |
| 481 | break; |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 482 | } |
Garfield Tan | 1e80be7 | 2017-02-27 18:16:00 -0800 | [diff] [blame] | 483 | bytesRead = inputStream.read(buffer, 0, bytesToRead); |
| 484 | if (bytesRead == -1) { |
| 485 | break; |
| 486 | } |
| 487 | outputStream.write(buffer, 0, bytesRead); |
| 488 | document.rootInfo.size += bytesRead; |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 489 | } |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 490 | } |
Garfield Tan | 1e80be7 | 2017-02-27 18:16:00 -0800 | [diff] [blame] | 491 | } catch (IOException e) { |
| 492 | Log.e(TAG, "Error on close", e); |
| 493 | closePipeWithErrorSilently(readPipe, e.getMessage()); |
| 494 | } finally { |
Jeff Sharkey | 94785ef | 2018-07-09 16:37:41 -0600 | [diff] [blame] | 495 | FileUtils.closeQuietly(inputStream); |
| 496 | FileUtils.closeQuietly(outputStream); |
Garfield Tan | 1e80be7 | 2017-02-27 18:16:00 -0800 | [diff] [blame] | 497 | Log.d(TAG, "Closing write stream on file " + document.documentId); |
| 498 | notifyParentChanged(document.parentId); |
| 499 | getContext().getContentResolver().notifyChange( |
| 500 | DocumentsContract.buildDocumentUri(mAuthority, document.documentId), |
| 501 | null, false); |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 502 | } |
Garfield Tan | 1e80be7 | 2017-02-27 18:16:00 -0800 | [diff] [blame] | 503 | }); |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 504 | |
| 505 | return writePipe; |
| 506 | } |
| 507 | |
| 508 | private void closePipeWithErrorSilently(ParcelFileDescriptor pipe, String error) { |
| 509 | try { |
| 510 | pipe.closeWithError(error); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 511 | } catch (IOException ignore) { |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 515 | @Override |
| 516 | public Bundle call(String method, String arg, Bundle extras) { |
Steve McKay | a495c48 | 2016-02-04 10:02:09 -0800 | [diff] [blame] | 517 | // We're not supposed to override any of the default DocumentsProvider |
| 518 | // methods that are supported by "call", so javadoc asks that we |
| 519 | // always call super.call first and return if response is not null. |
| 520 | Bundle result = super.call(method, arg, extras); |
| 521 | if (result != null) { |
| 522 | return result; |
| 523 | } |
| 524 | |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 525 | switch (method) { |
| 526 | case "clear": |
| 527 | clearCacheAndBuildRoots(); |
| 528 | return null; |
| 529 | case "configure": |
| 530 | configure(arg, extras); |
| 531 | return null; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 532 | case "createVirtualFile": |
| 533 | return createVirtualFileFromBundle(extras); |
| 534 | case "simulateReadErrorsForFile": |
| 535 | simulateReadErrorsForFile(arg); |
| 536 | return null; |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 537 | case "createDocumentWithFlags": |
Steve McKay | a495c48 | 2016-02-04 10:02:09 -0800 | [diff] [blame] | 538 | return dispatchCreateDocumentWithFlags(extras); |
Garfield Tan | 1566618 | 2017-01-18 16:27:55 -0800 | [diff] [blame] | 539 | case "setLoadingDuration": |
| 540 | mLoadingDuration = extras.getLong(DocumentsContract.EXTRA_LOADING); |
| 541 | return null; |
Garfield Tan | 1e80be7 | 2017-02-27 18:16:00 -0800 | [diff] [blame] | 542 | case "waitForWrite": |
| 543 | waitForWrite(); |
| 544 | return null; |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 545 | } |
Steve McKay | a495c48 | 2016-02-04 10:02:09 -0800 | [diff] [blame] | 546 | |
| 547 | return null; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 550 | private Bundle createVirtualFileFromBundle(Bundle extras) { |
| 551 | try { |
| 552 | Uri uri = createVirtualFile( |
| 553 | extras.getString(EXTRA_ROOT), |
| 554 | extras.getString(EXTRA_PATH), |
| 555 | extras.getString(Document.COLUMN_MIME_TYPE), |
| 556 | extras.getStringArrayList(EXTRA_STREAM_TYPES), |
| 557 | extras.getByteArray(EXTRA_CONTENT)); |
| 558 | |
| 559 | String documentId = DocumentsContract.getDocumentId(uri); |
| 560 | Bundle result = new Bundle(); |
| 561 | result.putString(Document.COLUMN_DOCUMENT_ID, documentId); |
| 562 | return result; |
| 563 | } catch (IOException e) { |
| 564 | Log.e(TAG, "Couldn't create virtual file."); |
| 565 | } |
| 566 | |
| 567 | return null; |
| 568 | } |
| 569 | |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 570 | private Bundle dispatchCreateDocumentWithFlags(Bundle extras) { |
| 571 | String rootId = extras.getString(EXTRA_PARENT_ID); |
| 572 | String mimeType = extras.getString(Document.COLUMN_MIME_TYPE); |
| 573 | String name = extras.getString(Document.COLUMN_DISPLAY_NAME); |
Tomasz Mikolajewski | fe7f536 | 2016-03-02 17:41:40 +0900 | [diff] [blame] | 574 | List<String> streamTypes = extras.getStringArrayList(EXTRA_STREAM_TYPES); |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 575 | int flags = extras.getInt(EXTRA_FLAGS); |
| 576 | |
| 577 | Bundle out = new Bundle(); |
| 578 | String documentId = null; |
| 579 | try { |
Tomasz Mikolajewski | fe7f536 | 2016-03-02 17:41:40 +0900 | [diff] [blame] | 580 | documentId = createDocument(rootId, mimeType, name, flags, streamTypes); |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 581 | Uri uri = DocumentsContract.buildDocumentUri(mAuthority, documentId); |
| 582 | out.putParcelable(DocumentsContract.EXTRA_URI, uri); |
| 583 | } catch (FileNotFoundException e) { |
Tomasz Mikolajewski | fe7f536 | 2016-03-02 17:41:40 +0900 | [diff] [blame] | 584 | Log.d(TAG, "Creating document with flags failed" + name); |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 585 | } |
| 586 | return out; |
| 587 | } |
| 588 | |
Garfield Tan | 1e80be7 | 2017-02-27 18:16:00 -0800 | [diff] [blame] | 589 | private void waitForWrite() { |
| 590 | try { |
| 591 | CountDownLatch latch = new CountDownLatch(1); |
| 592 | postToMainThread(latch::countDown); |
| 593 | latch.await(); |
| 594 | Log.d(TAG, "All writing is done."); |
| 595 | } catch (InterruptedException e) { |
| 596 | // should never happen |
| 597 | throw new RuntimeException(e); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | private void postToMainThread(Runnable r) { |
| 602 | new Handler(Looper.getMainLooper()).post(r); |
| 603 | } |
| 604 | |
Tomasz Mikolajewski | fe7f536 | 2016-03-02 17:41:40 +0900 | [diff] [blame] | 605 | public String createDocument(String parentId, String mimeType, String displayName, int flags, |
| 606 | List<String> streamTypes) throws FileNotFoundException { |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 607 | |
| 608 | StubDocument parent = mStorage.get(parentId); |
| 609 | File file = createFile(parent, mimeType, displayName); |
| 610 | |
| 611 | final StubDocument document = StubDocument.createDocumentWithFlags(file, mimeType, parent, |
Tomasz Mikolajewski | fe7f536 | 2016-03-02 17:41:40 +0900 | [diff] [blame] | 612 | flags, streamTypes); |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 613 | mStorage.put(document.documentId, document); |
| 614 | Log.d(TAG, "Created document " + document.documentId); |
| 615 | notifyParentChanged(document.parentId); |
| 616 | getContext().getContentResolver().notifyChange( |
| 617 | DocumentsContract.buildDocumentUri(mAuthority, document.documentId), |
| 618 | null, false); |
| 619 | |
| 620 | return document.documentId; |
| 621 | } |
| 622 | |
| 623 | private File createFile(StubDocument parent, String mimeType, String displayName) |
| 624 | throws FileNotFoundException { |
| 625 | if (parent == null) { |
| 626 | throw new IllegalArgumentException( |
| 627 | "Can't create file " + displayName + " in null parent."); |
| 628 | } |
| 629 | if (!parent.file.isDirectory()) { |
| 630 | throw new IllegalArgumentException( |
| 631 | "Can't create file " + displayName + " inside non-directory parent " |
| 632 | + parent.file.getName()); |
| 633 | } |
| 634 | |
| 635 | final File file = new File(parent.file, displayName); |
| 636 | if (file.exists()) { |
| 637 | throw new FileNotFoundException( |
| 638 | "Duplicate file names not supported for " + file); |
| 639 | } |
| 640 | |
| 641 | if (mimeType.equals(Document.MIME_TYPE_DIR)) { |
| 642 | if (!file.mkdirs()) { |
| 643 | throw new FileNotFoundException("Failed to create directory(s): " + file); |
| 644 | } |
| 645 | Log.i(TAG, "Created new directory: " + file); |
| 646 | } else { |
| 647 | boolean created = false; |
| 648 | try { |
| 649 | created = file.createNewFile(); |
| 650 | } catch (IOException e) { |
| 651 | // We'll throw an FNF exception later :) |
| 652 | Log.e(TAG, "createNewFile operation failed for file: " + file, e); |
| 653 | } |
| 654 | if (!created) { |
| 655 | throw new FileNotFoundException("createNewFile operation failed for: " + file); |
| 656 | } |
| 657 | Log.i(TAG, "Created new file: " + file); |
| 658 | } |
| 659 | return file; |
| 660 | } |
| 661 | |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 662 | private void configure(String arg, Bundle extras) { |
| 663 | Log.d(TAG, "Configure " + arg); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 664 | String rootName = extras.getString(EXTRA_ROOT, ROOT_0_ID); |
Garfield Tan | ceedd1f | 2017-05-17 14:10:12 -0700 | [diff] [blame] | 665 | long rootSize = extras.getLong(EXTRA_SIZE, 100) * 1024 * 1024; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 666 | setSize(rootName, rootSize); |
Garfield Tan | ceedd1f | 2017-05-17 14:10:12 -0700 | [diff] [blame] | 667 | mRootNotification = extras.getBoolean(EXTRA_ENABLE_ROOT_NOTIFICATION, true); |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 668 | } |
| 669 | |
Tomasz Mikolajewski | bcda3d1 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 670 | private void notifyParentChanged(String parentId) { |
| 671 | getContext().getContentResolver().notifyChange( |
| 672 | DocumentsContract.buildChildDocumentsUri(mAuthority, parentId), null, false); |
Garfield Tan | ceedd1f | 2017-05-17 14:10:12 -0700 | [diff] [blame] | 673 | if (mRootNotification) { |
| 674 | // Notify also about possible change in remaining space on the root. |
| 675 | getContext().getContentResolver().notifyChange( |
| 676 | DocumentsContract.buildRootsUri(mAuthority), null, false); |
| 677 | } |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 678 | } |
| 679 | |
Tomasz Mikolajewski | bcda3d1 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 680 | private void includeDocument(MatrixCursor result, StubDocument document) { |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 681 | final RowBuilder row = result.newRow(); |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 682 | row.add(Document.COLUMN_DOCUMENT_ID, document.documentId); |
| 683 | row.add(Document.COLUMN_DISPLAY_NAME, document.file.getName()); |
| 684 | row.add(Document.COLUMN_SIZE, document.file.length()); |
| 685 | row.add(Document.COLUMN_MIME_TYPE, document.mimeType); |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 686 | row.add(Document.COLUMN_FLAGS, document.flags); |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 687 | row.add(Document.COLUMN_LAST_MODIFIED, document.file.lastModified()); |
Tomasz Mikolajewski | 67f9bed | 2015-04-08 19:38:55 +0900 | [diff] [blame] | 688 | } |
| 689 | |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 690 | private void removeChildrenRecursively(File file) { |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 691 | for (File childFile : file.listFiles()) { |
| 692 | if (childFile.isDirectory()) { |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 693 | removeChildrenRecursively(childFile); |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 694 | } |
| 695 | childFile.delete(); |
| 696 | } |
| 697 | } |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 698 | |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 699 | public void setSize(String rootId, long rootSize) { |
| 700 | RootInfo root = mRoots.get(rootId); |
| 701 | if (root != null) { |
| 702 | final String key = STORAGE_SIZE_KEY + "." + rootId; |
| 703 | Log.d(TAG, "Set size of " + key + " : " + rootSize); |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 704 | |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 705 | // Persist the size. |
| 706 | SharedPreferences.Editor editor = mPrefs.edit(); |
| 707 | editor.putLong(key, rootSize); |
| 708 | editor.apply(); |
| 709 | // Apply the size in the current instance of this provider. |
| 710 | root.capacity = rootSize; |
| 711 | getContext().getContentResolver().notifyChange( |
| 712 | DocumentsContract.buildRootsUri(mAuthority), |
| 713 | null, false); |
| 714 | } else { |
| 715 | Log.e(TAG, "Attempt to configure non-existent root: " + rootId); |
| 716 | } |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 717 | } |
| 718 | |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 719 | @VisibleForTesting |
Tomasz Mikolajewski | dcec9ac | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 720 | public Uri createRegularFile(String rootId, String path, String mimeType, byte[] content) |
| 721 | throws FileNotFoundException, IOException { |
| 722 | final File file = createFile(rootId, path, mimeType, content); |
| 723 | final StubDocument parent = mStorage.get(getDocumentIdForFile(file.getParentFile())); |
| 724 | if (parent == null) { |
| 725 | throw new FileNotFoundException("Parent not found."); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 726 | } |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 727 | final StubDocument document = StubDocument.createRegularDocument(file, mimeType, parent); |
| 728 | mStorage.put(document.documentId, document); |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 729 | return DocumentsContract.buildDocumentUri(mAuthority, document.documentId); |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | @VisibleForTesting |
Tomasz Mikolajewski | dcec9ac | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 733 | public Uri createVirtualFile( |
| 734 | String rootId, String path, String mimeType, List<String> streamTypes, byte[] content) |
| 735 | throws FileNotFoundException, IOException { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 736 | |
Tomasz Mikolajewski | dcec9ac | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 737 | final File file = createFile(rootId, path, mimeType, content); |
| 738 | final StubDocument parent = mStorage.get(getDocumentIdForFile(file.getParentFile())); |
| 739 | if (parent == null) { |
| 740 | throw new FileNotFoundException("Parent not found."); |
| 741 | } |
| 742 | final StubDocument document = StubDocument.createVirtualDocument( |
| 743 | file, mimeType, streamTypes, parent); |
| 744 | mStorage.put(document.documentId, document); |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 745 | return DocumentsContract.buildDocumentUri(mAuthority, document.documentId); |
Tomasz Mikolajewski | dcec9ac | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | @VisibleForTesting |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 749 | public File getFile(String rootId, String path) throws FileNotFoundException { |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 750 | StubDocument root = mRoots.get(rootId).document; |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 751 | if (root == null) { |
| 752 | throw new FileNotFoundException("No roots with the ID " + rootId + " were found"); |
| 753 | } |
| 754 | // Convert the path string into a path that's relative to the root. |
| 755 | File needle = new File(root.file, path.substring(1)); |
| 756 | |
| 757 | StubDocument found = mStorage.get(getDocumentIdForFile(needle)); |
| 758 | if (found == null) { |
| 759 | return null; |
| 760 | } |
| 761 | return found.file; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Tomasz Mikolajewski | 049eb56 | 2015-12-18 11:29:48 +0900 | [diff] [blame] | 764 | private File createFile(String rootId, String path, String mimeType, byte[] content) |
| 765 | throws FileNotFoundException, IOException { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 766 | Log.d(TAG, "Creating test file " + rootId + " : " + path); |
Tomasz Mikolajewski | 049eb56 | 2015-12-18 11:29:48 +0900 | [diff] [blame] | 767 | StubDocument root = mRoots.get(rootId).document; |
| 768 | if (root == null) { |
| 769 | throw new FileNotFoundException("No roots with the ID " + rootId + " were found"); |
| 770 | } |
| 771 | final File file = new File(root.file, path.substring(1)); |
| 772 | if (DocumentsContract.Document.MIME_TYPE_DIR.equals(mimeType)) { |
| 773 | if (!file.mkdirs()) { |
| 774 | throw new FileNotFoundException("Couldn't create directory " + file.getPath()); |
| 775 | } |
| 776 | } else { |
| 777 | if (!file.createNewFile()) { |
| 778 | throw new FileNotFoundException("Couldn't create file " + file.getPath()); |
| 779 | } |
| 780 | try (final FileOutputStream fout = new FileOutputStream(file)) { |
| 781 | fout.write(content); |
| 782 | } |
| 783 | } |
| 784 | return file; |
| 785 | } |
| 786 | |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 787 | final static class RootInfo { |
Aga Wronska | ee8ad9c | 2016-02-01 12:23:48 -0800 | [diff] [blame] | 788 | private static final int DEFAULT_ROOTS_FLAGS = Root.FLAG_SUPPORTS_SEARCH |
| 789 | | Root.FLAG_SUPPORTS_CREATE | Root.FLAG_SUPPORTS_IS_CHILD; |
| 790 | |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 791 | public final String name; |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 792 | public final StubDocument document; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 793 | public long capacity; |
| 794 | public long size; |
Aga Wronska | ee8ad9c | 2016-02-01 12:23:48 -0800 | [diff] [blame] | 795 | public int flags; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 796 | |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 797 | RootInfo(File file, long capacity) { |
| 798 | this.name = file.getName(); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 799 | this.capacity = 1024 * 1024; |
Aga Wronska | ee8ad9c | 2016-02-01 12:23:48 -0800 | [diff] [blame] | 800 | this.flags = DEFAULT_ROOTS_FLAGS; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 801 | this.capacity = capacity; |
| 802 | this.size = 0; |
Aga Wronska | ee8ad9c | 2016-02-01 12:23:48 -0800 | [diff] [blame] | 803 | this.document = StubDocument.createRootDocument(file, this); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | public long getRemainingCapacity() { |
| 807 | return capacity - size; |
| 808 | } |
Aga Wronska | ee8ad9c | 2016-02-01 12:23:48 -0800 | [diff] [blame] | 809 | |
| 810 | public void setSearchEnabled(boolean enabled) { |
| 811 | flags = enabled ? (flags | Root.FLAG_SUPPORTS_SEARCH) |
| 812 | : (flags & ~Root.FLAG_SUPPORTS_SEARCH); |
| 813 | } |
| 814 | |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 817 | final static class StubDocument { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 818 | public final File file; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 819 | public final String documentId; |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 820 | public final String mimeType; |
| 821 | public final List<String> streamTypes; |
| 822 | public final int flags; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 823 | public final String parentId; |
| 824 | public final RootInfo rootInfo; |
| 825 | |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 826 | private StubDocument(File file, String mimeType, List<String> streamTypes, int flags, |
| 827 | StubDocument parent) { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 828 | this.file = file; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 829 | this.documentId = getDocumentIdForFile(file); |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 830 | this.mimeType = mimeType; |
| 831 | this.streamTypes = streamTypes; |
| 832 | this.flags = flags; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 833 | this.parentId = parent.documentId; |
| 834 | this.rootInfo = parent.rootInfo; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 837 | private StubDocument(File file, RootInfo rootInfo) { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 838 | this.file = file; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 839 | this.documentId = getDocumentIdForFile(file); |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 840 | this.mimeType = Document.MIME_TYPE_DIR; |
Steve McKay | b324dfd | 2016-10-28 12:52:36 -0700 | [diff] [blame] | 841 | this.streamTypes = new ArrayList<>(); |
Aga Wronska | 3c23718 | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 842 | this.flags = Document.FLAG_DIR_SUPPORTS_CREATE | Document.FLAG_SUPPORTS_RENAME; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 843 | this.parentId = null; |
| 844 | this.rootInfo = rootInfo; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 845 | } |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 846 | |
| 847 | public static StubDocument createRootDocument(File file, RootInfo rootInfo) { |
| 848 | return new StubDocument(file, rootInfo); |
| 849 | } |
| 850 | |
| 851 | public static StubDocument createRegularDocument( |
| 852 | File file, String mimeType, StubDocument parent) { |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 853 | int flags = Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_RENAME; |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 854 | if (file.isDirectory()) { |
| 855 | flags |= Document.FLAG_DIR_SUPPORTS_CREATE; |
| 856 | } else { |
| 857 | flags |= Document.FLAG_SUPPORTS_WRITE; |
| 858 | } |
| 859 | return new StubDocument(file, mimeType, new ArrayList<String>(), flags, parent); |
| 860 | } |
| 861 | |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 862 | public static StubDocument createDocumentWithFlags( |
Tomasz Mikolajewski | fe7f536 | 2016-03-02 17:41:40 +0900 | [diff] [blame] | 863 | File file, String mimeType, StubDocument parent, int flags, |
| 864 | List<String> streamTypes) { |
| 865 | return new StubDocument(file, mimeType, streamTypes, flags, parent); |
Aga Wronska | 18410b7 | 2016-01-26 14:06:29 -0800 | [diff] [blame] | 866 | } |
| 867 | |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 868 | public static StubDocument createVirtualDocument( |
| 869 | File file, String mimeType, List<String> streamTypes, StubDocument parent) { |
| 870 | int flags = Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE |
| 871 | | Document.FLAG_VIRTUAL_DOCUMENT; |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 872 | return new StubDocument(file, mimeType, streamTypes, flags, parent); |
| 873 | } |
| 874 | |
Steve McKay | f712a20 | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 875 | @Override |
| 876 | public String toString() { |
| 877 | return "StubDocument{" |
| 878 | + "path:" + file.getPath() |
Steve McKay | f712a20 | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 879 | + ", documentId:" + documentId |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 880 | + ", mimeType:" + mimeType |
| 881 | + ", streamTypes:" + streamTypes.toString() |
| 882 | + ", flags:" + flags |
Steve McKay | f712a20 | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 883 | + ", parentId:" + parentId |
Tomasz Mikolajewski | cf08d6d | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 884 | + ", rootInfo:" + rootInfo |
Steve McKay | f712a20 | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 885 | + "}"; |
| 886 | } |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | private static String getDocumentIdForFile(File file) { |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 890 | return file.getAbsolutePath(); |
| 891 | } |
| 892 | } |