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