Tomasz Mikolajewski | 0fc8beb | 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 | |
| 19 | import android.content.Context; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 20 | import android.content.SharedPreferences; |
Tomasz Mikolajewski | 738154e | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 21 | import android.content.pm.ProviderInfo; |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 22 | import android.content.res.AssetFileDescriptor; |
| 23 | import android.database.Cursor; |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 24 | import android.database.MatrixCursor; |
Ben Kwa | dae8c37 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 25 | import android.database.MatrixCursor.RowBuilder; |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 26 | import android.graphics.Point; |
Ben Kwa | dae8c37 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 27 | import android.net.Uri; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 28 | import android.os.Bundle; |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 29 | import android.os.CancellationSignal; |
Tomasz Mikolajewski | 78699be | 2015-04-08 19:38:55 +0900 | [diff] [blame] | 30 | import android.os.FileUtils; |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 31 | import android.os.ParcelFileDescriptor; |
Tomasz Mikolajewski | 738154e | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 32 | import android.provider.DocumentsContract; |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 33 | import android.provider.DocumentsContract.Document; |
| 34 | import android.provider.DocumentsContract.Root; |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 35 | import android.provider.DocumentsProvider; |
Ben Kwa | dae8c37 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 36 | import android.support.annotation.VisibleForTesting; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 38 | |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 39 | import libcore.io.IoUtils; |
| 40 | |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 41 | import java.io.File; |
| 42 | import java.io.FileNotFoundException; |
Ben Kwa | dae8c37 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 43 | import java.io.FileOutputStream; |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 44 | import java.io.IOException; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 45 | import java.io.InputStream; |
| 46 | import java.io.OutputStream; |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 47 | import java.util.ArrayList; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 48 | import java.util.Arrays; |
| 49 | import java.util.Collection; |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 50 | import java.util.HashMap; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 51 | import java.util.HashSet; |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 52 | import java.util.List; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 53 | import java.util.Map; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 54 | import java.util.Set; |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 55 | |
| 56 | public class StubProvider extends DocumentsProvider { |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 57 | |
| 58 | public static final String DEFAULT_AUTHORITY = "com.android.documentsui.stubprovider"; |
| 59 | public static final String ROOT_0_ID = "TEST_ROOT_0"; |
| 60 | public static final String ROOT_1_ID = "TEST_ROOT_1"; |
| 61 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 62 | public static final String EXTRA_SIZE = "com.android.documentsui.stubprovider.SIZE"; |
| 63 | public static final String EXTRA_ROOT = "com.android.documentsui.stubprovider.ROOT"; |
| 64 | public static final String EXTRA_PATH = "com.android.documentsui.stubprovider.PATH"; |
| 65 | public static final String EXTRA_STREAM_TYPES |
| 66 | = "com.android.documentsui.stubprovider.STREAM_TYPES"; |
| 67 | public static final String EXTRA_CONTENT = "com.android.documentsui.stubprovider.CONTENT"; |
| 68 | |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 69 | private static final String TAG = "StubProvider"; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 70 | |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 71 | private static final String STORAGE_SIZE_KEY = "documentsui.stubprovider.size"; |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 72 | private static int DEFAULT_ROOT_SIZE = 1024 * 1024 * 100; // 100 MB. |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 73 | |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 74 | private static final String[] DEFAULT_ROOT_PROJECTION = new String[] { |
| 75 | Root.COLUMN_ROOT_ID, Root.COLUMN_FLAGS, Root.COLUMN_TITLE, Root.COLUMN_DOCUMENT_ID, |
| 76 | Root.COLUMN_AVAILABLE_BYTES |
| 77 | }; |
| 78 | private static final String[] DEFAULT_DOCUMENT_PROJECTION = new String[] { |
| 79 | Document.COLUMN_DOCUMENT_ID, Document.COLUMN_MIME_TYPE, Document.COLUMN_DISPLAY_NAME, |
| 80 | Document.COLUMN_LAST_MODIFIED, Document.COLUMN_FLAGS, Document.COLUMN_SIZE, |
| 81 | }; |
| 82 | |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 83 | private final Map<String, StubDocument> mStorage = new HashMap<>(); |
| 84 | private final Map<String, RootInfo> mRoots = new HashMap<>(); |
| 85 | private final Object mWriteLock = new Object(); |
| 86 | |
| 87 | private String mAuthority = DEFAULT_AUTHORITY; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 88 | private SharedPreferences mPrefs; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 89 | private Set<String> mSimulateReadErrorIds = new HashSet<>(); |
Tomasz Mikolajewski | 738154e | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 90 | |
| 91 | @Override |
| 92 | public void attachInfo(Context context, ProviderInfo info) { |
| 93 | mAuthority = info.authority; |
| 94 | super.attachInfo(context, info); |
| 95 | } |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 96 | |
| 97 | @Override |
| 98 | public boolean onCreate() { |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 99 | clearCacheAndBuildRoots(); |
| 100 | return true; |
| 101 | } |
| 102 | |
Ben Kwa | dae8c37 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 103 | @VisibleForTesting |
| 104 | public void clearCacheAndBuildRoots() { |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 105 | Log.d(TAG, "Resetting storage."); |
| 106 | removeChildrenRecursively(getContext().getCacheDir()); |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 107 | mStorage.clear(); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 108 | mSimulateReadErrorIds.clear(); |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 109 | |
| 110 | mPrefs = getContext().getSharedPreferences( |
| 111 | "com.android.documentsui.stubprovider.preferences", Context.MODE_PRIVATE); |
| 112 | Collection<String> rootIds = mPrefs.getStringSet("roots", null); |
| 113 | if (rootIds == null) { |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 114 | rootIds = Arrays.asList(new String[] { ROOT_0_ID, ROOT_1_ID }); |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 115 | } |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 116 | |
| 117 | mRoots.clear(); |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 118 | for (String rootId : rootIds) { |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 119 | // Make a subdir in the cache dir for each root. |
| 120 | final File file = new File(getContext().getCacheDir(), rootId); |
| 121 | if (file.mkdir()) { |
| 122 | Log.i(TAG, "Created new root directory @ " + file.getPath()); |
| 123 | } |
| 124 | final RootInfo rootInfo = new RootInfo(file, getSize(rootId)); |
| 125 | mStorage.put(rootInfo.document.documentId, rootInfo.document); |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 126 | mRoots.put(rootId, rootInfo); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @return Storage size, in bytes. |
| 132 | */ |
| 133 | private long getSize(String rootId) { |
| 134 | final String key = STORAGE_SIZE_KEY + "." + rootId; |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 135 | return mPrefs.getLong(key, DEFAULT_ROOT_SIZE); |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public Cursor queryRoots(String[] projection) throws FileNotFoundException { |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 140 | final MatrixCursor result = new MatrixCursor(projection != null ? projection |
| 141 | : DEFAULT_ROOT_PROJECTION); |
| 142 | for (Map.Entry<String, RootInfo> entry : mRoots.entrySet()) { |
| 143 | final String id = entry.getKey(); |
| 144 | final RootInfo info = entry.getValue(); |
| 145 | final RowBuilder row = result.newRow(); |
| 146 | row.add(Root.COLUMN_ROOT_ID, id); |
Aga Wronska | 619f3be | 2016-01-14 11:15:20 -0800 | [diff] [blame] | 147 | row.add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_CREATE | Root.FLAG_SUPPORTS_IS_CHILD |
| 148 | | Root.FLAG_SUPPORTS_SEARCH); |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 149 | row.add(Root.COLUMN_TITLE, id); |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 150 | row.add(Root.COLUMN_DOCUMENT_ID, info.document.documentId); |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 151 | row.add(Root.COLUMN_AVAILABLE_BYTES, info.getRemainingCapacity()); |
| 152 | } |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 153 | return result; |
| 154 | } |
| 155 | |
| 156 | @Override |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 157 | public Cursor queryDocument(String documentId, String[] projection) |
| 158 | throws FileNotFoundException { |
| 159 | final MatrixCursor result = new MatrixCursor(projection != null ? projection |
| 160 | : DEFAULT_DOCUMENT_PROJECTION); |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 161 | final StubDocument file = mStorage.get(documentId); |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 162 | if (file == null) { |
| 163 | throw new FileNotFoundException(); |
| 164 | } |
Tomasz Mikolajewski | 9739793 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 165 | includeDocument(result, file); |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 166 | return result; |
| 167 | } |
| 168 | |
| 169 | @Override |
Tomasz Mikolajewski | 78699be | 2015-04-08 19:38:55 +0900 | [diff] [blame] | 170 | public boolean isChildDocument(String parentDocId, String docId) { |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 171 | final StubDocument parentDocument = mStorage.get(parentDocId); |
| 172 | final StubDocument childDocument = mStorage.get(docId); |
| 173 | return FileUtils.contains(parentDocument.file, childDocument.file); |
Tomasz Mikolajewski | 78699be | 2015-04-08 19:38:55 +0900 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 177 | public String createDocument(String parentId, String mimeType, String displayName) |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 178 | throws FileNotFoundException { |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 179 | |
| 180 | final StubDocument parent = mStorage.get(parentId); |
| 181 | if (parent == null) { |
| 182 | throw new IllegalArgumentException( |
| 183 | "Can't create file " + displayName + " in null parent."); |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 184 | } |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 185 | if (!parent.file.isDirectory()) { |
| 186 | throw new IllegalArgumentException( |
| 187 | "Can't create file " + displayName + " inside non-directory parent " |
| 188 | + parent.file.getName()); |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 189 | } |
| 190 | |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 191 | final File file = new File(parent.file, displayName); |
| 192 | if (file.exists()) { |
| 193 | throw new FileNotFoundException( |
| 194 | "Duplicate file names not supported for " + file); |
| 195 | } |
| 196 | |
| 197 | if (mimeType.equals(Document.MIME_TYPE_DIR)) { |
| 198 | if (!file.mkdirs()) { |
| 199 | throw new FileNotFoundException( |
| 200 | "Failed to create directory(s): " + file); |
| 201 | } |
| 202 | Log.i(TAG, "Created new directory: " + file); |
| 203 | } else { |
| 204 | boolean created = false; |
| 205 | try { |
| 206 | created = file.createNewFile(); |
| 207 | } catch (IOException e) { |
| 208 | // We'll throw an FNF exception later :) |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 209 | Log.e(TAG, "createNewFile operation failed for file: " + file, e); |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 210 | } |
| 211 | if (!created) { |
| 212 | throw new FileNotFoundException( |
| 213 | "createNewFile operation failed for: " + file); |
| 214 | } |
| 215 | Log.i(TAG, "Created new file: " + file); |
| 216 | } |
| 217 | |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 218 | final StubDocument document = StubDocument.createRegularDocument(file, mimeType, parent); |
| 219 | mStorage.put(document.documentId, document); |
Ben Kwa | 0b4a3c4 | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 220 | Log.d(TAG, "Created document " + document.documentId); |
Tomasz Mikolajewski | 9739793 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 221 | notifyParentChanged(document.parentId); |
Ben Kwa | dae8c37 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 222 | getContext().getContentResolver().notifyChange( |
| 223 | DocumentsContract.buildDocumentUri(mAuthority, document.documentId), |
| 224 | null, false); |
| 225 | |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 226 | return document.documentId; |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
Tomasz Mikolajewski | 738154e | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 230 | public void deleteDocument(String documentId) |
| 231 | throws FileNotFoundException { |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 232 | final StubDocument document = mStorage.get(documentId); |
| 233 | final long fileSize = document.file.length(); |
| 234 | if (document == null || !document.file.delete()) |
Tomasz Mikolajewski | 738154e | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 235 | throw new FileNotFoundException(); |
| 236 | synchronized (mWriteLock) { |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 237 | document.rootInfo.size -= fileSize; |
Ben Kwa | 0b4a3c4 | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 238 | mStorage.remove(documentId); |
Tomasz Mikolajewski | 738154e | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 239 | } |
Ben Kwa | 0b4a3c4 | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 240 | Log.d(TAG, "Document deleted: " + documentId); |
Tomasz Mikolajewski | 9739793 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 241 | notifyParentChanged(document.parentId); |
Ben Kwa | dae8c37 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 242 | getContext().getContentResolver().notifyChange( |
| 243 | DocumentsContract.buildDocumentUri(mAuthority, document.documentId), |
| 244 | null, false); |
Tomasz Mikolajewski | 738154e | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | @Override |
Steve McKay | dbdaa49 | 2015-12-02 11:20:54 -0800 | [diff] [blame] | 248 | public Cursor queryChildDocumentsForManage(String parentDocumentId, String[] projection, |
| 249 | String sortOrder) throws FileNotFoundException { |
| 250 | return queryChildDocuments(parentDocumentId, projection, sortOrder); |
| 251 | } |
| 252 | |
| 253 | @Override |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 254 | public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder) |
| 255 | throws FileNotFoundException { |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 256 | final StubDocument parentDocument = mStorage.get(parentDocumentId); |
| 257 | if (parentDocument == null || parentDocument.file.isFile()) { |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 258 | throw new FileNotFoundException(); |
| 259 | } |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 260 | final MatrixCursor result = new MatrixCursor(projection != null ? projection |
| 261 | : DEFAULT_DOCUMENT_PROJECTION); |
Tomasz Mikolajewski | 9739793 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 262 | result.setNotificationUri(getContext().getContentResolver(), |
| 263 | DocumentsContract.buildChildDocumentsUri(mAuthority, parentDocumentId)); |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 264 | StubDocument document; |
| 265 | for (File file : parentDocument.file.listFiles()) { |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 266 | document = mStorage.get(getDocumentIdForFile(file)); |
Tomasz Mikolajewski | 9739793 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 267 | if (document != null) { |
| 268 | includeDocument(result, document); |
| 269 | } |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 270 | } |
| 271 | return result; |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public Cursor queryRecentDocuments(String rootId, String[] projection) |
| 276 | throws FileNotFoundException { |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 277 | final MatrixCursor result = new MatrixCursor(projection != null ? projection |
| 278 | : DEFAULT_DOCUMENT_PROJECTION); |
| 279 | return result; |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | @Override |
Aga Wronska | 619f3be | 2016-01-14 11:15:20 -0800 | [diff] [blame] | 283 | public Cursor querySearchDocuments(String rootId, String query, String[] projection) |
| 284 | throws FileNotFoundException { |
| 285 | |
| 286 | StubDocument parentDocument = mRoots.get(rootId).document; |
| 287 | if (parentDocument == null || parentDocument.file.isFile()) { |
| 288 | throw new FileNotFoundException(); |
| 289 | } |
| 290 | |
| 291 | final MatrixCursor result = new MatrixCursor( |
| 292 | projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION); |
| 293 | |
| 294 | for (File file : parentDocument.file.listFiles()) { |
| 295 | if (file.getName().toLowerCase().contains(query)) { |
| 296 | StubDocument document = mStorage.get(getDocumentIdForFile(file)); |
| 297 | if (document != null) { |
| 298 | includeDocument(result, document); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | return result; |
| 303 | } |
| 304 | |
| 305 | @Override |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 306 | public ParcelFileDescriptor openDocument(String docId, String mode, CancellationSignal signal) |
| 307 | throws FileNotFoundException { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 308 | |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 309 | final StubDocument document = mStorage.get(docId); |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 310 | if (document == null || !document.file.isFile()) { |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 311 | throw new FileNotFoundException(); |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 312 | } |
| 313 | if ((document.flags & Document.FLAG_VIRTUAL_DOCUMENT) != 0) { |
| 314 | throw new IllegalStateException("Tried to open a virtual file."); |
| 315 | } |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 316 | |
| 317 | if ("r".equals(mode)) { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 318 | if (mSimulateReadErrorIds.contains(docId)) { |
| 319 | Log.d(TAG, "Simulated errs enabled. Open in the wrong mode."); |
| 320 | return ParcelFileDescriptor.open( |
| 321 | document.file, ParcelFileDescriptor.MODE_WRITE_ONLY); |
Ben Kwa | dae8c37 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 322 | } |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 323 | return ParcelFileDescriptor.open(document.file, ParcelFileDescriptor.MODE_READ_ONLY); |
Tomasz Mikolajewski | 30b6694 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 324 | } |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 325 | if ("w".equals(mode)) { |
| 326 | return startWrite(document); |
| 327 | } |
| 328 | |
Tomasz Mikolajewski | 30b6694 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 329 | throw new FileNotFoundException(); |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 330 | } |
| 331 | |
Ben Kwa | dae8c37 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 332 | @VisibleForTesting |
Ben Kwa | 0b4a3c4 | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 333 | public void simulateReadErrorsForFile(Uri uri) { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 334 | simulateReadErrorsForFile(DocumentsContract.getDocumentId(uri)); |
| 335 | } |
| 336 | |
| 337 | public void simulateReadErrorsForFile(String id) { |
| 338 | mSimulateReadErrorIds.add(id); |
Ben Kwa | dae8c37 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 341 | @Override |
| 342 | public AssetFileDescriptor openDocumentThumbnail( |
| 343 | String docId, Point sizeHint, CancellationSignal signal) throws FileNotFoundException { |
| 344 | throw new FileNotFoundException(); |
| 345 | } |
| 346 | |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 347 | @Override |
| 348 | public AssetFileDescriptor openTypedDocument( |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 349 | String docId, String mimeTypeFilter, Bundle opts, CancellationSignal signal) |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 350 | throws FileNotFoundException { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 351 | final StubDocument document = mStorage.get(docId); |
Tomasz Mikolajewski | 7539565 | 2016-01-07 07:19:22 +0000 | [diff] [blame] | 352 | if (document == null || !document.file.isFile() || document.streamTypes == null) { |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 353 | throw new FileNotFoundException(); |
| 354 | } |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 355 | for (final String mimeType : document.streamTypes) { |
| 356 | // Strict compare won't accept wildcards, but that's OK for tests, as DocumentsUI |
Tomasz Mikolajewski | f65fdeb | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 357 | // doesn't use them for getStreamTypes nor openTypedDocument. |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 358 | if (mimeType.equals(mimeTypeFilter)) { |
| 359 | ParcelFileDescriptor pfd = ParcelFileDescriptor.open( |
| 360 | document.file, ParcelFileDescriptor.MODE_READ_ONLY); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 361 | if (mSimulateReadErrorIds.contains(docId)) { |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 362 | pfd = new ParcelFileDescriptor(pfd) { |
| 363 | @Override |
| 364 | public void checkError() throws IOException { |
| 365 | throw new IOException("Test error"); |
| 366 | } |
| 367 | }; |
| 368 | } |
| 369 | return new AssetFileDescriptor(pfd, 0, document.file.length()); |
| 370 | } |
| 371 | } |
| 372 | throw new IllegalArgumentException("Invalid MIME type filter for openTypedDocument()."); |
| 373 | } |
| 374 | |
Tomasz Mikolajewski | f65fdeb | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 375 | @Override |
| 376 | public String[] getStreamTypes(Uri uri, String mimeTypeFilter) { |
| 377 | final StubDocument document = mStorage.get(DocumentsContract.getDocumentId(uri)); |
| 378 | if (document == null) { |
| 379 | throw new IllegalArgumentException( |
| 380 | "The provided Uri is incorrect, or the file is gone."); |
| 381 | } |
Tomasz Mikolajewski | f65fdeb | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 382 | if (!"*/*".equals(mimeTypeFilter)) { |
| 383 | // Not used by DocumentsUI, so don't bother implementing it. |
| 384 | throw new UnsupportedOperationException(); |
| 385 | } |
Tomasz Mikolajewski | 7539565 | 2016-01-07 07:19:22 +0000 | [diff] [blame] | 386 | if (document.streamTypes == null) { |
| 387 | return null; |
| 388 | } |
Tomasz Mikolajewski | f65fdeb | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 389 | return document.streamTypes.toArray(new String[document.streamTypes.size()]); |
| 390 | } |
| 391 | |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 392 | private ParcelFileDescriptor startWrite(final StubDocument document) |
Tomasz Mikolajewski | 30b6694 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 393 | throws FileNotFoundException { |
Tomasz Mikolajewski | 30b6694 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 394 | ParcelFileDescriptor[] pipe; |
| 395 | try { |
| 396 | pipe = ParcelFileDescriptor.createReliablePipe(); |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 397 | } catch (IOException exception) { |
Tomasz Mikolajewski | 30b6694 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 398 | throw new FileNotFoundException(); |
| 399 | } |
| 400 | final ParcelFileDescriptor readPipe = pipe[0]; |
| 401 | final ParcelFileDescriptor writePipe = pipe[1]; |
| 402 | |
| 403 | new Thread() { |
| 404 | @Override |
| 405 | public void run() { |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 406 | InputStream inputStream = null; |
| 407 | OutputStream outputStream = null; |
Tomasz Mikolajewski | 30b6694 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 408 | try { |
Ben Kwa | 0b4a3c4 | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 409 | Log.d(TAG, "Opening write stream on file " + document.documentId); |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 410 | inputStream = new ParcelFileDescriptor.AutoCloseInputStream(readPipe); |
| 411 | outputStream = new FileOutputStream(document.file); |
Tomasz Mikolajewski | 30b6694 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 412 | byte[] buffer = new byte[32 * 1024]; |
| 413 | int bytesToRead; |
| 414 | int bytesRead = 0; |
| 415 | while (bytesRead != -1) { |
| 416 | synchronized (mWriteLock) { |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 417 | // This cast is safe because the max possible value is buffer.length. |
| 418 | bytesToRead = (int) Math.min(document.rootInfo.getRemainingCapacity(), |
| 419 | buffer.length); |
Tomasz Mikolajewski | 30b6694 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 420 | if (bytesToRead == 0) { |
| 421 | closePipeWithErrorSilently(readPipe, "Not enough space."); |
| 422 | break; |
| 423 | } |
| 424 | bytesRead = inputStream.read(buffer, 0, bytesToRead); |
| 425 | if (bytesRead == -1) { |
| 426 | break; |
| 427 | } |
| 428 | outputStream.write(buffer, 0, bytesRead); |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 429 | document.rootInfo.size += bytesRead; |
Tomasz Mikolajewski | 30b6694 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 430 | } |
| 431 | } |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 432 | } catch (IOException e) { |
Ben Kwa | dae8c37 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 433 | Log.e(TAG, "Error on close", e); |
Tomasz Mikolajewski | 30b6694 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 434 | closePipeWithErrorSilently(readPipe, e.getMessage()); |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 435 | } finally { |
| 436 | IoUtils.closeQuietly(inputStream); |
| 437 | IoUtils.closeQuietly(outputStream); |
Ben Kwa | 0b4a3c4 | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 438 | Log.d(TAG, "Closing write stream on file " + document.documentId); |
Tomasz Mikolajewski | 9739793 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 439 | notifyParentChanged(document.parentId); |
Ben Kwa | dae8c37 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 440 | getContext().getContentResolver().notifyChange( |
| 441 | DocumentsContract.buildDocumentUri(mAuthority, document.documentId), |
| 442 | null, false); |
Tomasz Mikolajewski | 30b6694 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | }.start(); |
| 446 | |
| 447 | return writePipe; |
| 448 | } |
| 449 | |
| 450 | private void closePipeWithErrorSilently(ParcelFileDescriptor pipe, String error) { |
| 451 | try { |
| 452 | pipe.closeWithError(error); |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 453 | } catch (IOException ignore) { |
Tomasz Mikolajewski | 30b6694 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 457 | @Override |
| 458 | public Bundle call(String method, String arg, Bundle extras) { |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 459 | switch (method) { |
| 460 | case "clear": |
| 461 | clearCacheAndBuildRoots(); |
| 462 | return null; |
| 463 | case "configure": |
| 464 | configure(arg, extras); |
| 465 | return null; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 466 | case "createVirtualFile": |
| 467 | return createVirtualFileFromBundle(extras); |
| 468 | case "simulateReadErrorsForFile": |
| 469 | simulateReadErrorsForFile(arg); |
| 470 | return null; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 471 | default: |
| 472 | return super.call(method, arg, extras); |
Tomasz Mikolajewski | 30b6694 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 473 | } |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 476 | private Bundle createVirtualFileFromBundle(Bundle extras) { |
| 477 | try { |
| 478 | Uri uri = createVirtualFile( |
| 479 | extras.getString(EXTRA_ROOT), |
| 480 | extras.getString(EXTRA_PATH), |
| 481 | extras.getString(Document.COLUMN_MIME_TYPE), |
| 482 | extras.getStringArrayList(EXTRA_STREAM_TYPES), |
| 483 | extras.getByteArray(EXTRA_CONTENT)); |
| 484 | |
| 485 | String documentId = DocumentsContract.getDocumentId(uri); |
| 486 | Bundle result = new Bundle(); |
| 487 | result.putString(Document.COLUMN_DOCUMENT_ID, documentId); |
| 488 | return result; |
| 489 | } catch (IOException e) { |
| 490 | Log.e(TAG, "Couldn't create virtual file."); |
| 491 | } |
| 492 | |
| 493 | return null; |
| 494 | } |
| 495 | |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 496 | private void configure(String arg, Bundle extras) { |
| 497 | Log.d(TAG, "Configure " + arg); |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 498 | String rootName = extras.getString(EXTRA_ROOT, ROOT_0_ID); |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 499 | long rootSize = extras.getLong(EXTRA_SIZE, 1) * 1024 * 1024; |
| 500 | setSize(rootName, rootSize); |
Tomasz Mikolajewski | 30b6694 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 501 | } |
| 502 | |
Tomasz Mikolajewski | 9739793 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 503 | private void notifyParentChanged(String parentId) { |
| 504 | getContext().getContentResolver().notifyChange( |
| 505 | DocumentsContract.buildChildDocumentsUri(mAuthority, parentId), null, false); |
| 506 | // Notify also about possible change in remaining space on the root. |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 507 | getContext().getContentResolver().notifyChange(DocumentsContract.buildRootsUri(mAuthority), |
| 508 | null, false); |
Tomasz Mikolajewski | 738154e | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 509 | } |
| 510 | |
Tomasz Mikolajewski | 9739793 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 511 | private void includeDocument(MatrixCursor result, StubDocument document) { |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 512 | final RowBuilder row = result.newRow(); |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 513 | row.add(Document.COLUMN_DOCUMENT_ID, document.documentId); |
| 514 | row.add(Document.COLUMN_DISPLAY_NAME, document.file.getName()); |
| 515 | row.add(Document.COLUMN_SIZE, document.file.length()); |
| 516 | row.add(Document.COLUMN_MIME_TYPE, document.mimeType); |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 517 | row.add(Document.COLUMN_FLAGS, document.flags); |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 518 | row.add(Document.COLUMN_LAST_MODIFIED, document.file.lastModified()); |
Tomasz Mikolajewski | 78699be | 2015-04-08 19:38:55 +0900 | [diff] [blame] | 519 | } |
| 520 | |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 521 | private void removeChildrenRecursively(File file) { |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 522 | for (File childFile : file.listFiles()) { |
| 523 | if (childFile.isDirectory()) { |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 524 | removeChildrenRecursively(childFile); |
Tomasz Mikolajewski | 0fc8beb | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 525 | } |
| 526 | childFile.delete(); |
| 527 | } |
| 528 | } |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 529 | |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 530 | public void setSize(String rootId, long rootSize) { |
| 531 | RootInfo root = mRoots.get(rootId); |
| 532 | if (root != null) { |
| 533 | final String key = STORAGE_SIZE_KEY + "." + rootId; |
| 534 | Log.d(TAG, "Set size of " + key + " : " + rootSize); |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 535 | |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 536 | // Persist the size. |
| 537 | SharedPreferences.Editor editor = mPrefs.edit(); |
| 538 | editor.putLong(key, rootSize); |
| 539 | editor.apply(); |
| 540 | // Apply the size in the current instance of this provider. |
| 541 | root.capacity = rootSize; |
| 542 | getContext().getContentResolver().notifyChange( |
| 543 | DocumentsContract.buildRootsUri(mAuthority), |
| 544 | null, false); |
| 545 | } else { |
| 546 | Log.e(TAG, "Attempt to configure non-existent root: " + rootId); |
| 547 | } |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 548 | } |
| 549 | |
Ben Kwa | dae8c37 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 550 | @VisibleForTesting |
Tomasz Mikolajewski | f65fdeb | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 551 | public Uri createRegularFile(String rootId, String path, String mimeType, byte[] content) |
| 552 | throws FileNotFoundException, IOException { |
| 553 | final File file = createFile(rootId, path, mimeType, content); |
| 554 | final StubDocument parent = mStorage.get(getDocumentIdForFile(file.getParentFile())); |
| 555 | if (parent == null) { |
| 556 | throw new FileNotFoundException("Parent not found."); |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 557 | } |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 558 | final StubDocument document = StubDocument.createRegularDocument(file, mimeType, parent); |
| 559 | mStorage.put(document.documentId, document); |
Ben Kwa | dae8c37 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 560 | return DocumentsContract.buildDocumentUri(mAuthority, document.documentId); |
| 561 | } |
| 562 | |
| 563 | @VisibleForTesting |
Tomasz Mikolajewski | f65fdeb | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 564 | public Uri createVirtualFile( |
| 565 | String rootId, String path, String mimeType, List<String> streamTypes, byte[] content) |
| 566 | throws FileNotFoundException, IOException { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 567 | |
Tomasz Mikolajewski | f65fdeb | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 568 | final File file = createFile(rootId, path, mimeType, content); |
| 569 | final StubDocument parent = mStorage.get(getDocumentIdForFile(file.getParentFile())); |
| 570 | if (parent == null) { |
| 571 | throw new FileNotFoundException("Parent not found."); |
| 572 | } |
| 573 | final StubDocument document = StubDocument.createVirtualDocument( |
| 574 | file, mimeType, streamTypes, parent); |
| 575 | mStorage.put(document.documentId, document); |
| 576 | return DocumentsContract.buildDocumentUri(mAuthority, document.documentId); |
| 577 | } |
| 578 | |
| 579 | @VisibleForTesting |
Ben Kwa | dae8c37 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 580 | public File getFile(String rootId, String path) throws FileNotFoundException { |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 581 | StubDocument root = mRoots.get(rootId).document; |
Ben Kwa | dae8c37 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 582 | if (root == null) { |
| 583 | throw new FileNotFoundException("No roots with the ID " + rootId + " were found"); |
| 584 | } |
| 585 | // Convert the path string into a path that's relative to the root. |
| 586 | File needle = new File(root.file, path.substring(1)); |
| 587 | |
| 588 | StubDocument found = mStorage.get(getDocumentIdForFile(needle)); |
| 589 | if (found == null) { |
| 590 | return null; |
| 591 | } |
| 592 | return found.file; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Tomasz Mikolajewski | c7ec678 | 2015-12-18 11:29:48 +0900 | [diff] [blame] | 595 | private File createFile(String rootId, String path, String mimeType, byte[] content) |
| 596 | throws FileNotFoundException, IOException { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 597 | Log.d(TAG, "Creating test file " + rootId + " : " + path); |
Tomasz Mikolajewski | c7ec678 | 2015-12-18 11:29:48 +0900 | [diff] [blame] | 598 | StubDocument root = mRoots.get(rootId).document; |
| 599 | if (root == null) { |
| 600 | throw new FileNotFoundException("No roots with the ID " + rootId + " were found"); |
| 601 | } |
| 602 | final File file = new File(root.file, path.substring(1)); |
| 603 | if (DocumentsContract.Document.MIME_TYPE_DIR.equals(mimeType)) { |
| 604 | if (!file.mkdirs()) { |
| 605 | throw new FileNotFoundException("Couldn't create directory " + file.getPath()); |
| 606 | } |
| 607 | } else { |
| 608 | if (!file.createNewFile()) { |
| 609 | throw new FileNotFoundException("Couldn't create file " + file.getPath()); |
| 610 | } |
| 611 | try (final FileOutputStream fout = new FileOutputStream(file)) { |
| 612 | fout.write(content); |
| 613 | } |
| 614 | } |
| 615 | return file; |
| 616 | } |
| 617 | |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 618 | final static class RootInfo { |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 619 | public final String name; |
Steve McKay | 0c3c695 | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 620 | public final StubDocument document; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 621 | public long capacity; |
| 622 | public long size; |
| 623 | |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 624 | RootInfo(File file, long capacity) { |
| 625 | this.name = file.getName(); |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 626 | this.capacity = 1024 * 1024; |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 627 | this.document = StubDocument.createRootDocument(file, this); |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 628 | this.capacity = capacity; |
| 629 | this.size = 0; |
| 630 | } |
| 631 | |
| 632 | public long getRemainingCapacity() { |
| 633 | return capacity - size; |
| 634 | } |
| 635 | } |
| 636 | |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 637 | final static class StubDocument { |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 638 | public final File file; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 639 | public final String documentId; |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 640 | public final String mimeType; |
| 641 | public final List<String> streamTypes; |
| 642 | public final int flags; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 643 | public final String parentId; |
| 644 | public final RootInfo rootInfo; |
| 645 | |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 646 | private StubDocument( |
| 647 | File file, String mimeType, List<String> streamTypes, int flags, |
| 648 | StubDocument parent) { |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 649 | this.file = file; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 650 | this.documentId = getDocumentIdForFile(file); |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 651 | this.mimeType = mimeType; |
| 652 | this.streamTypes = streamTypes; |
| 653 | this.flags = flags; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 654 | this.parentId = parent.documentId; |
| 655 | this.rootInfo = parent.rootInfo; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 658 | private StubDocument(File file, RootInfo rootInfo) { |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 659 | this.file = file; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 660 | this.documentId = getDocumentIdForFile(file); |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 661 | this.mimeType = Document.MIME_TYPE_DIR; |
| 662 | this.streamTypes = new ArrayList<String>(); |
| 663 | this.flags = Document.FLAG_DIR_SUPPORTS_CREATE; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 664 | this.parentId = null; |
| 665 | this.rootInfo = rootInfo; |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 666 | } |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 667 | |
| 668 | public static StubDocument createRootDocument(File file, RootInfo rootInfo) { |
| 669 | return new StubDocument(file, rootInfo); |
| 670 | } |
| 671 | |
| 672 | public static StubDocument createRegularDocument( |
| 673 | File file, String mimeType, StubDocument parent) { |
| 674 | int flags = Document.FLAG_SUPPORTS_DELETE; |
| 675 | if (file.isDirectory()) { |
| 676 | flags |= Document.FLAG_DIR_SUPPORTS_CREATE; |
| 677 | } else { |
| 678 | flags |= Document.FLAG_SUPPORTS_WRITE; |
| 679 | } |
| 680 | return new StubDocument(file, mimeType, new ArrayList<String>(), flags, parent); |
| 681 | } |
| 682 | |
| 683 | public static StubDocument createVirtualDocument( |
| 684 | File file, String mimeType, List<String> streamTypes, StubDocument parent) { |
| 685 | int flags = Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE |
| 686 | | Document.FLAG_VIRTUAL_DOCUMENT; |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 687 | return new StubDocument(file, mimeType, streamTypes, flags, parent); |
| 688 | } |
| 689 | |
Steve McKay | d3afdee | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 690 | @Override |
| 691 | public String toString() { |
| 692 | return "StubDocument{" |
| 693 | + "path:" + file.getPath() |
Steve McKay | d3afdee | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 694 | + ", documentId:" + documentId |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 695 | + ", mimeType:" + mimeType |
| 696 | + ", streamTypes:" + streamTypes.toString() |
| 697 | + ", flags:" + flags |
Steve McKay | d3afdee | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 698 | + ", parentId:" + parentId |
Tomasz Mikolajewski | cca31eb | 2015-12-14 18:50:24 +0900 | [diff] [blame] | 699 | + ", rootInfo:" + rootInfo |
Steve McKay | d3afdee | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 700 | + "}"; |
| 701 | } |
Ben Kwa | 448dbbb | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | private static String getDocumentIdForFile(File file) { |
Tomasz Mikolajewski | d5d5c91 | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 705 | return file.getAbsolutePath(); |
| 706 | } |
| 707 | } |