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 | |
| 19 | import android.content.Context; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 20 | import android.content.SharedPreferences; |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 21 | import android.content.pm.ProviderInfo; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 22 | import android.content.res.AssetFileDescriptor; |
| 23 | import android.database.Cursor; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 24 | import android.database.MatrixCursor; |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 25 | import android.database.MatrixCursor.RowBuilder; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 26 | import android.graphics.Point; |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 27 | import android.net.Uri; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 28 | import android.os.Bundle; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 29 | import android.os.CancellationSignal; |
Tomasz Mikolajewski | 67f9bed | 2015-04-08 19:38:55 +0900 | [diff] [blame] | 30 | import android.os.FileUtils; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 31 | import android.os.ParcelFileDescriptor; |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 32 | import android.provider.DocumentsContract; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 33 | import android.provider.DocumentsContract.Document; |
| 34 | import android.provider.DocumentsContract.Root; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 35 | import android.provider.DocumentsProvider; |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 36 | import android.support.annotation.VisibleForTesting; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 38 | |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 39 | import com.google.android.collect.Maps; |
| 40 | |
| 41 | import libcore.io.IoUtils; |
| 42 | |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 43 | import java.io.File; |
| 44 | import java.io.FileNotFoundException; |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 45 | import java.io.FileOutputStream; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 46 | import java.io.IOException; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 47 | import java.io.InputStream; |
| 48 | import java.io.OutputStream; |
| 49 | import java.util.Arrays; |
| 50 | import java.util.Collection; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 51 | import java.util.HashMap; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 52 | import java.util.Map; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 53 | |
| 54 | public class StubProvider extends DocumentsProvider { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 55 | private static final String EXTRA_SIZE = "com.android.documentsui.stubprovider.SIZE"; |
| 56 | private static final String EXTRA_ROOT = "com.android.documentsui.stubprovider.ROOT"; |
| 57 | private static final String STORAGE_SIZE_KEY = "documentsui.stubprovider.size"; |
| 58 | private static int DEFAULT_SIZE = 1024 * 1024; // 1 MB. |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 59 | private static final String TAG = "StubProvider"; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 60 | private static final String MY_ROOT_ID = "sd0"; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 61 | private static final String[] DEFAULT_ROOT_PROJECTION = new String[] { |
| 62 | Root.COLUMN_ROOT_ID, Root.COLUMN_FLAGS, Root.COLUMN_TITLE, Root.COLUMN_DOCUMENT_ID, |
| 63 | Root.COLUMN_AVAILABLE_BYTES |
| 64 | }; |
| 65 | private static final String[] DEFAULT_DOCUMENT_PROJECTION = new String[] { |
| 66 | Document.COLUMN_DOCUMENT_ID, Document.COLUMN_MIME_TYPE, Document.COLUMN_DISPLAY_NAME, |
| 67 | Document.COLUMN_LAST_MODIFIED, Document.COLUMN_FLAGS, Document.COLUMN_SIZE, |
| 68 | }; |
| 69 | |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 70 | private HashMap<String, StubDocument> mStorage = new HashMap<String, StubDocument>(); |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 71 | private Object mWriteLock = new Object(); |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 72 | private String mAuthority; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 73 | private SharedPreferences mPrefs; |
| 74 | private Map<String, RootInfo> mRoots; |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 75 | private String mSimulateReadErrors; |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 76 | |
| 77 | @Override |
| 78 | public void attachInfo(Context context, ProviderInfo info) { |
| 79 | mAuthority = info.authority; |
| 80 | super.attachInfo(context, info); |
| 81 | } |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 82 | |
| 83 | @Override |
| 84 | public boolean onCreate() { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 85 | clearCacheAndBuildRoots(); |
| 86 | return true; |
| 87 | } |
| 88 | |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 89 | @VisibleForTesting |
| 90 | public void clearCacheAndBuildRoots() { |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 91 | final File cacheDir = getContext().getCacheDir(); |
| 92 | removeRecursively(cacheDir); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 93 | mStorage.clear(); |
| 94 | |
| 95 | mPrefs = getContext().getSharedPreferences( |
| 96 | "com.android.documentsui.stubprovider.preferences", Context.MODE_PRIVATE); |
| 97 | Collection<String> rootIds = mPrefs.getStringSet("roots", null); |
| 98 | if (rootIds == null) { |
| 99 | rootIds = Arrays.asList(new String[] { |
| 100 | "sd0", "sd1" |
| 101 | }); |
| 102 | } |
| 103 | // Create new roots. |
| 104 | mRoots = Maps.newHashMap(); |
| 105 | for (String rootId : rootIds) { |
| 106 | final RootInfo rootInfo = new RootInfo(rootId, getSize(rootId)); |
| 107 | mRoots.put(rootId, rootInfo); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @return Storage size, in bytes. |
| 113 | */ |
| 114 | private long getSize(String rootId) { |
| 115 | final String key = STORAGE_SIZE_KEY + "." + rootId; |
| 116 | return mPrefs.getLong(key, DEFAULT_SIZE); |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public Cursor queryRoots(String[] projection) throws FileNotFoundException { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 121 | final MatrixCursor result = new MatrixCursor(projection != null ? projection |
| 122 | : DEFAULT_ROOT_PROJECTION); |
| 123 | for (Map.Entry<String, RootInfo> entry : mRoots.entrySet()) { |
| 124 | final String id = entry.getKey(); |
| 125 | final RootInfo info = entry.getValue(); |
| 126 | final RowBuilder row = result.newRow(); |
| 127 | row.add(Root.COLUMN_ROOT_ID, id); |
| 128 | row.add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_CREATE | Root.FLAG_SUPPORTS_IS_CHILD); |
| 129 | row.add(Root.COLUMN_TITLE, id); |
| 130 | row.add(Root.COLUMN_DOCUMENT_ID, info.rootDocument.documentId); |
| 131 | row.add(Root.COLUMN_AVAILABLE_BYTES, info.getRemainingCapacity()); |
| 132 | } |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 133 | return result; |
| 134 | } |
| 135 | |
| 136 | @Override |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 137 | public Cursor queryDocument(String documentId, String[] projection) |
| 138 | throws FileNotFoundException { |
| 139 | final MatrixCursor result = new MatrixCursor(projection != null ? projection |
| 140 | : DEFAULT_DOCUMENT_PROJECTION); |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 141 | final StubDocument file = mStorage.get(documentId); |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 142 | if (file == null) { |
| 143 | throw new FileNotFoundException(); |
| 144 | } |
Tomasz Mikolajewski | bcda3d1 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 145 | includeDocument(result, file); |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 146 | return result; |
| 147 | } |
| 148 | |
| 149 | @Override |
Tomasz Mikolajewski | 67f9bed | 2015-04-08 19:38:55 +0900 | [diff] [blame] | 150 | public boolean isChildDocument(String parentDocId, String docId) { |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 151 | final StubDocument parentDocument = mStorage.get(parentDocId); |
| 152 | final StubDocument childDocument = mStorage.get(docId); |
| 153 | return FileUtils.contains(parentDocument.file, childDocument.file); |
Tomasz Mikolajewski | 67f9bed | 2015-04-08 19:38:55 +0900 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 157 | public String createDocument(String parentDocumentId, String mimeType, String displayName) |
| 158 | throws FileNotFoundException { |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 159 | final StubDocument parentDocument = mStorage.get(parentDocumentId); |
| 160 | if (parentDocument == null || !parentDocument.file.isDirectory()) { |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 161 | throw new FileNotFoundException(); |
| 162 | } |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 163 | final File file = new File(parentDocument.file, displayName); |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 164 | if (mimeType.equals(Document.MIME_TYPE_DIR)) { |
| 165 | if (!file.mkdirs()) { |
| 166 | throw new FileNotFoundException(); |
| 167 | } |
| 168 | } else { |
| 169 | try { |
| 170 | if (!file.createNewFile()) { |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 171 | throw new IllegalStateException("The file " + file.getPath() + " already exists"); |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 172 | } |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 173 | } catch (IOException e) { |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 174 | throw new FileNotFoundException(); |
| 175 | } |
| 176 | } |
| 177 | |
Tomasz Mikolajewski | bcda3d1 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 178 | final StubDocument document = new StubDocument(file, mimeType, parentDocument); |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 179 | Log.d(TAG, "Created document " + document.documentId); |
Tomasz Mikolajewski | bcda3d1 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 180 | notifyParentChanged(document.parentId); |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 181 | getContext().getContentResolver().notifyChange( |
| 182 | DocumentsContract.buildDocumentUri(mAuthority, document.documentId), |
| 183 | null, false); |
| 184 | |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 185 | return document.documentId; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 189 | public void deleteDocument(String documentId) |
| 190 | throws FileNotFoundException { |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 191 | final StubDocument document = mStorage.get(documentId); |
| 192 | final long fileSize = document.file.length(); |
| 193 | if (document == null || !document.file.delete()) |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 194 | throw new FileNotFoundException(); |
| 195 | synchronized (mWriteLock) { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 196 | document.rootInfo.size -= fileSize; |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 197 | mStorage.remove(documentId); |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 198 | } |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 199 | Log.d(TAG, "Document deleted: " + documentId); |
Tomasz Mikolajewski | bcda3d1 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 200 | notifyParentChanged(document.parentId); |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 201 | getContext().getContentResolver().notifyChange( |
| 202 | DocumentsContract.buildDocumentUri(mAuthority, document.documentId), |
| 203 | null, false); |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 207 | public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder) |
| 208 | throws FileNotFoundException { |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 209 | final StubDocument parentDocument = mStorage.get(parentDocumentId); |
| 210 | if (parentDocument == null || parentDocument.file.isFile()) { |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 211 | throw new FileNotFoundException(); |
| 212 | } |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 213 | final MatrixCursor result = new MatrixCursor(projection != null ? projection |
| 214 | : DEFAULT_DOCUMENT_PROJECTION); |
Tomasz Mikolajewski | bcda3d1 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 215 | result.setNotificationUri(getContext().getContentResolver(), |
| 216 | DocumentsContract.buildChildDocumentsUri(mAuthority, parentDocumentId)); |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 217 | StubDocument document; |
| 218 | for (File file : parentDocument.file.listFiles()) { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 219 | document = mStorage.get(getDocumentIdForFile(file)); |
Tomasz Mikolajewski | bcda3d1 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 220 | if (document != null) { |
| 221 | includeDocument(result, document); |
| 222 | } |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 223 | } |
| 224 | return result; |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public Cursor queryRecentDocuments(String rootId, String[] projection) |
| 229 | throws FileNotFoundException { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 230 | final MatrixCursor result = new MatrixCursor(projection != null ? projection |
| 231 | : DEFAULT_DOCUMENT_PROJECTION); |
| 232 | return result; |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public ParcelFileDescriptor openDocument(String docId, String mode, CancellationSignal signal) |
| 237 | throws FileNotFoundException { |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 238 | final StubDocument document = mStorage.get(docId); |
| 239 | if (document == null || !document.file.isFile()) |
| 240 | throw new FileNotFoundException(); |
| 241 | |
| 242 | if ("r".equals(mode)) { |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 243 | ParcelFileDescriptor pfd = ParcelFileDescriptor.open(document.file, |
| 244 | ParcelFileDescriptor.MODE_READ_ONLY); |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 245 | if (docId.equals(mSimulateReadErrors)) { |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 246 | pfd = new ParcelFileDescriptor(pfd) { |
| 247 | @Override |
| 248 | public void checkError() throws IOException { |
| 249 | throw new IOException("Test error"); |
| 250 | } |
| 251 | }; |
| 252 | } |
| 253 | return pfd; |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 254 | } |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 255 | if ("w".equals(mode)) { |
| 256 | return startWrite(document); |
| 257 | } |
| 258 | |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 259 | throw new FileNotFoundException(); |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 260 | } |
| 261 | |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 262 | @VisibleForTesting |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 263 | public void simulateReadErrorsForFile(Uri uri) { |
| 264 | mSimulateReadErrors = DocumentsContract.getDocumentId(uri); |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 267 | @Override |
| 268 | public AssetFileDescriptor openDocumentThumbnail( |
| 269 | String docId, Point sizeHint, CancellationSignal signal) throws FileNotFoundException { |
| 270 | throw new FileNotFoundException(); |
| 271 | } |
| 272 | |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 273 | private ParcelFileDescriptor startWrite(final StubDocument document) |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 274 | throws FileNotFoundException { |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 275 | ParcelFileDescriptor[] pipe; |
| 276 | try { |
| 277 | pipe = ParcelFileDescriptor.createReliablePipe(); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 278 | } catch (IOException exception) { |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 279 | throw new FileNotFoundException(); |
| 280 | } |
| 281 | final ParcelFileDescriptor readPipe = pipe[0]; |
| 282 | final ParcelFileDescriptor writePipe = pipe[1]; |
| 283 | |
| 284 | new Thread() { |
| 285 | @Override |
| 286 | public void run() { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 287 | InputStream inputStream = null; |
| 288 | OutputStream outputStream = null; |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 289 | try { |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 290 | Log.d(TAG, "Opening write stream on file " + document.documentId); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 291 | inputStream = new ParcelFileDescriptor.AutoCloseInputStream(readPipe); |
| 292 | outputStream = new FileOutputStream(document.file); |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 293 | byte[] buffer = new byte[32 * 1024]; |
| 294 | int bytesToRead; |
| 295 | int bytesRead = 0; |
| 296 | while (bytesRead != -1) { |
| 297 | synchronized (mWriteLock) { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 298 | // This cast is safe because the max possible value is buffer.length. |
| 299 | bytesToRead = (int) Math.min(document.rootInfo.getRemainingCapacity(), |
| 300 | buffer.length); |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 301 | if (bytesToRead == 0) { |
| 302 | closePipeWithErrorSilently(readPipe, "Not enough space."); |
| 303 | break; |
| 304 | } |
| 305 | bytesRead = inputStream.read(buffer, 0, bytesToRead); |
| 306 | if (bytesRead == -1) { |
| 307 | break; |
| 308 | } |
| 309 | outputStream.write(buffer, 0, bytesRead); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 310 | document.rootInfo.size += bytesRead; |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 311 | } |
| 312 | } |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 313 | } catch (IOException e) { |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 314 | Log.e(TAG, "Error on close", e); |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 315 | closePipeWithErrorSilently(readPipe, e.getMessage()); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 316 | } finally { |
| 317 | IoUtils.closeQuietly(inputStream); |
| 318 | IoUtils.closeQuietly(outputStream); |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 319 | Log.d(TAG, "Closing write stream on file " + document.documentId); |
Tomasz Mikolajewski | bcda3d1 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 320 | notifyParentChanged(document.parentId); |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 321 | getContext().getContentResolver().notifyChange( |
| 322 | DocumentsContract.buildDocumentUri(mAuthority, document.documentId), |
| 323 | null, false); |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | }.start(); |
| 327 | |
| 328 | return writePipe; |
| 329 | } |
| 330 | |
| 331 | private void closePipeWithErrorSilently(ParcelFileDescriptor pipe, String error) { |
| 332 | try { |
| 333 | pipe.closeWithError(error); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 334 | } catch (IOException ignore) { |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 338 | @Override |
| 339 | public Bundle call(String method, String arg, Bundle extras) { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 340 | switch (method) { |
| 341 | case "clear": |
| 342 | clearCacheAndBuildRoots(); |
| 343 | return null; |
| 344 | case "configure": |
| 345 | configure(arg, extras); |
| 346 | return null; |
| 347 | default: |
| 348 | return super.call(method, arg, extras); |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 349 | } |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | private void configure(String arg, Bundle extras) { |
| 353 | Log.d(TAG, "Configure " + arg); |
| 354 | String rootName = extras.getString(EXTRA_ROOT, MY_ROOT_ID); |
| 355 | long rootSize = extras.getLong(EXTRA_SIZE, 1) * 1024 * 1024; |
| 356 | setSize(rootName, rootSize); |
Tomasz Mikolajewski | 8055044 | 2015-04-10 17:28:53 +0900 | [diff] [blame] | 357 | } |
| 358 | |
Tomasz Mikolajewski | bcda3d1 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 359 | private void notifyParentChanged(String parentId) { |
| 360 | getContext().getContentResolver().notifyChange( |
| 361 | DocumentsContract.buildChildDocumentsUri(mAuthority, parentId), null, false); |
| 362 | // Notify also about possible change in remaining space on the root. |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 363 | getContext().getContentResolver().notifyChange(DocumentsContract.buildRootsUri(mAuthority), |
| 364 | null, false); |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 365 | } |
| 366 | |
Tomasz Mikolajewski | bcda3d1 | 2015-04-13 13:27:14 +0900 | [diff] [blame] | 367 | private void includeDocument(MatrixCursor result, StubDocument document) { |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 368 | final RowBuilder row = result.newRow(); |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 369 | row.add(Document.COLUMN_DOCUMENT_ID, document.documentId); |
| 370 | row.add(Document.COLUMN_DISPLAY_NAME, document.file.getName()); |
| 371 | row.add(Document.COLUMN_SIZE, document.file.length()); |
| 372 | row.add(Document.COLUMN_MIME_TYPE, document.mimeType); |
Tomasz Mikolajewski | 38e965f | 2015-04-10 17:32:44 +0900 | [diff] [blame] | 373 | int flags = Document.FLAG_SUPPORTS_DELETE; |
| 374 | // TODO: Add support for renaming. |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 375 | if (document.file.isDirectory()) { |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 376 | flags |= Document.FLAG_DIR_SUPPORTS_CREATE; |
| 377 | } else { |
| 378 | flags |= Document.FLAG_SUPPORTS_WRITE; |
| 379 | } |
| 380 | row.add(Document.COLUMN_FLAGS, flags); |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 381 | row.add(Document.COLUMN_LAST_MODIFIED, document.file.lastModified()); |
Tomasz Mikolajewski | 67f9bed | 2015-04-08 19:38:55 +0900 | [diff] [blame] | 382 | } |
| 383 | |
Tomasz Mikolajewski | 5519474 | 2015-04-08 09:21:08 +0900 | [diff] [blame] | 384 | private void removeRecursively(File file) { |
| 385 | for (File childFile : file.listFiles()) { |
| 386 | if (childFile.isDirectory()) { |
| 387 | removeRecursively(childFile); |
| 388 | } |
| 389 | childFile.delete(); |
| 390 | } |
| 391 | } |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 392 | |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 393 | public void setSize(String rootId, long rootSize) { |
| 394 | RootInfo root = mRoots.get(rootId); |
| 395 | if (root != null) { |
| 396 | final String key = STORAGE_SIZE_KEY + "." + rootId; |
| 397 | Log.d(TAG, "Set size of " + key + " : " + rootSize); |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 398 | |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 399 | // Persist the size. |
| 400 | SharedPreferences.Editor editor = mPrefs.edit(); |
| 401 | editor.putLong(key, rootSize); |
| 402 | editor.apply(); |
| 403 | // Apply the size in the current instance of this provider. |
| 404 | root.capacity = rootSize; |
| 405 | getContext().getContentResolver().notifyChange( |
| 406 | DocumentsContract.buildRootsUri(mAuthority), |
| 407 | null, false); |
| 408 | } else { |
| 409 | Log.e(TAG, "Attempt to configure non-existent root: " + rootId); |
| 410 | } |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 411 | } |
| 412 | |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 413 | @VisibleForTesting |
| 414 | public Uri createFile(String rootId, String path, String mimeType, byte[] content) |
| 415 | throws FileNotFoundException, IOException { |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 416 | Log.d(TAG, "Creating file " + rootId + ":" + path); |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 417 | StubDocument root = mRoots.get(rootId).rootDocument; |
| 418 | if (root == null) { |
| 419 | throw new FileNotFoundException("No roots with the ID " + rootId + " were found"); |
| 420 | } |
| 421 | File file = new File(root.file, path.substring(1)); |
| 422 | StubDocument parent = mStorage.get(getDocumentIdForFile(file.getParentFile())); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 423 | if (parent == null) { |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 424 | parent = mStorage.get(createFile(rootId, file.getParentFile().getPath(), |
| 425 | DocumentsContract.Document.MIME_TYPE_DIR, null)); |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 426 | Log.d(TAG, "Created parent " + parent.documentId); |
| 427 | } else { |
| 428 | Log.d(TAG, "Found parent " + parent.documentId); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 429 | } |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 430 | |
| 431 | if (DocumentsContract.Document.MIME_TYPE_DIR.equals(mimeType)) { |
| 432 | if (!file.mkdirs()) { |
| 433 | throw new FileNotFoundException("Couldn't create directory " + file.getPath()); |
| 434 | } |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 435 | } else { |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 436 | if (!file.createNewFile()) { |
| 437 | throw new FileNotFoundException("Couldn't create file " + file.getPath()); |
| 438 | } |
| 439 | // Add content to the file. |
| 440 | FileOutputStream fout = new FileOutputStream(file); |
| 441 | fout.write(content); |
| 442 | fout.close(); |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 443 | } |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 444 | final StubDocument document = new StubDocument(file, mimeType, parent); |
| 445 | return DocumentsContract.buildDocumentUri(mAuthority, document.documentId); |
| 446 | } |
| 447 | |
| 448 | @VisibleForTesting |
| 449 | public File getFile(String rootId, String path) throws FileNotFoundException { |
| 450 | StubDocument root = mRoots.get(rootId).rootDocument; |
| 451 | if (root == null) { |
| 452 | throw new FileNotFoundException("No roots with the ID " + rootId + " were found"); |
| 453 | } |
| 454 | // Convert the path string into a path that's relative to the root. |
| 455 | File needle = new File(root.file, path.substring(1)); |
| 456 | |
| 457 | StubDocument found = mStorage.get(getDocumentIdForFile(needle)); |
| 458 | if (found == null) { |
| 459 | return null; |
| 460 | } |
| 461 | return found.file; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | final class RootInfo { |
| 465 | public final String name; |
| 466 | public final StubDocument rootDocument; |
| 467 | public long capacity; |
| 468 | public long size; |
| 469 | |
| 470 | RootInfo(String name, long capacity) { |
| 471 | this.name = name; |
| 472 | this.capacity = 1024 * 1024; |
| 473 | // Make a subdir in the cache dir for each root. |
| 474 | File rootDir = new File(getContext().getCacheDir(), name); |
| 475 | rootDir.mkdir(); |
| 476 | this.rootDocument = new StubDocument(rootDir, Document.MIME_TYPE_DIR, this); |
| 477 | this.capacity = capacity; |
| 478 | this.size = 0; |
| 479 | } |
| 480 | |
| 481 | public long getRemainingCapacity() { |
| 482 | return capacity - size; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | final class StubDocument { |
| 487 | public final File file; |
| 488 | public final String mimeType; |
| 489 | public final String documentId; |
| 490 | public final String parentId; |
| 491 | public final RootInfo rootInfo; |
| 492 | |
| 493 | StubDocument(File file, String mimeType, StubDocument parent) { |
| 494 | this.file = file; |
| 495 | this.mimeType = mimeType; |
| 496 | this.documentId = getDocumentIdForFile(file); |
| 497 | this.parentId = parent.documentId; |
| 498 | this.rootInfo = parent.rootInfo; |
| 499 | mStorage.put(this.documentId, this); |
| 500 | } |
| 501 | |
| 502 | StubDocument(File file, String mimeType, RootInfo rootInfo) { |
| 503 | this.file = file; |
| 504 | this.mimeType = mimeType; |
| 505 | this.documentId = getDocumentIdForFile(file); |
| 506 | this.parentId = null; |
| 507 | this.rootInfo = rootInfo; |
| 508 | mStorage.put(this.documentId, this); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | private static String getDocumentIdForFile(File file) { |
Tomasz Mikolajewski | 6e9d76d | 2015-04-13 12:17:51 +0900 | [diff] [blame] | 513 | return file.getAbsolutePath(); |
| 514 | } |
| 515 | } |