Relay content change notifications to documents.
[android/platform/packages/providers/DownloadProvider.git] / src / com / android / providers / downloads / DownloadStorageProvider.java
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.providers.downloads;
18
19 import android.app.DownloadManager;
20 import android.app.DownloadManager.Query;
21 import android.content.Context;
22 import android.content.res.AssetFileDescriptor;
23 import android.database.Cursor;
24 import android.database.MatrixCursor;
25 import android.database.MatrixCursor.RowBuilder;
26 import android.graphics.Point;
27 import android.os.Binder;
28 import android.os.CancellationSignal;
29 import android.os.ParcelFileDescriptor;
30 import android.provider.DocumentsContract;
31 import android.provider.DocumentsContract.Document;
32 import android.provider.DocumentsContract.Root;
33 import android.provider.DocumentsProvider;
34
35 import libcore.io.IoUtils;
36
37 import java.io.FileNotFoundException;
38
39 /**
40  * Presents a {@link DocumentsContract} view of {@link DownloadManager}
41  * contents.
42  */
43 public class DownloadStorageProvider extends DocumentsProvider {
44     private static final String DOC_ID_ROOT = Constants.STORAGE_DOC_ID_ROOT;
45
46     private static final String[] DEFAULT_ROOT_PROJECTION = new String[] {
47             Root.COLUMN_ROOT_ID, Root.COLUMN_ROOT_TYPE, Root.COLUMN_FLAGS, Root.COLUMN_ICON,
48             Root.COLUMN_TITLE, Root.COLUMN_SUMMARY, Root.COLUMN_DOCUMENT_ID,
49             Root.COLUMN_AVAILABLE_BYTES,
50     };
51
52     private static final String[] DEFAULT_DOCUMENT_PROJECTION = new String[] {
53             Document.COLUMN_DOCUMENT_ID, Document.COLUMN_MIME_TYPE, Document.COLUMN_DISPLAY_NAME,
54             Document.COLUMN_LAST_MODIFIED, Document.COLUMN_FLAGS, Document.COLUMN_SIZE,
55     };
56
57     private DownloadManager mDm;
58     private DownloadManager.Query mBaseQuery;
59
60     @Override
61     public boolean onCreate() {
62         mDm = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
63         mDm.setAccessAllDownloads(true);
64         mBaseQuery = new DownloadManager.Query().setOnlyIncludeVisibleInDownloadsUi(true);
65
66         return true;
67     }
68
69     private static String[] resolveRootProjection(String[] projection) {
70         return projection != null ? projection : DEFAULT_ROOT_PROJECTION;
71     }
72
73     private static String[] resolveDocumentProjection(String[] projection) {
74         return projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION;
75     }
76
77     private void copyNotificationUri(MatrixCursor result, Cursor cursor) {
78         result.setNotificationUri(getContext().getContentResolver(), cursor.getNotificationUri());
79     }
80
81     @Override
82     public Cursor queryRoots(String[] projection) throws FileNotFoundException {
83         final MatrixCursor result = new MatrixCursor(resolveRootProjection(projection));
84         final RowBuilder row = result.newRow();
85         row.offer(Root.COLUMN_ROOT_ID, DOC_ID_ROOT);
86         row.offer(Root.COLUMN_ROOT_TYPE, Root.ROOT_TYPE_SHORTCUT);
87         row.offer(Root.COLUMN_FLAGS, Root.FLAG_LOCAL_ONLY | Root.FLAG_SUPPORTS_RECENTS);
88         row.offer(Root.COLUMN_ICON, R.mipmap.ic_launcher_download);
89         row.offer(Root.COLUMN_TITLE, getContext().getString(R.string.root_downloads));
90         row.offer(Root.COLUMN_DOCUMENT_ID, DOC_ID_ROOT);
91         return result;
92     }
93
94     @Override
95     public void deleteDocument(String docId) throws FileNotFoundException {
96         // Delegate to real provider
97         final long token = Binder.clearCallingIdentity();
98         try {
99             if (mDm.remove(Long.parseLong(docId)) != 1) {
100                 throw new IllegalStateException("Failed to delete " + docId);
101             }
102         } finally {
103             Binder.restoreCallingIdentity(token);
104         }
105     }
106
107     @Override
108     public Cursor queryDocument(String docId, String[] projection) throws FileNotFoundException {
109         final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
110
111         if (DOC_ID_ROOT.equals(docId)) {
112             includeDefaultDocument(result);
113         } else {
114             // Delegate to real provider
115             final long token = Binder.clearCallingIdentity();
116             Cursor cursor = null;
117             try {
118                 cursor = mDm.query(new Query().setFilterById(Long.parseLong(docId)));
119                 copyNotificationUri(result, cursor);
120                 if (cursor.moveToFirst()) {
121                     includeDownloadFromCursor(result, cursor);
122                 }
123             } finally {
124                 IoUtils.closeQuietly(cursor);
125                 Binder.restoreCallingIdentity(token);
126             }
127         }
128         return result;
129     }
130
131     @Override
132     public Cursor queryChildDocuments(String docId, String[] projection, String sortOrder)
133             throws FileNotFoundException {
134         final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
135
136         // Delegate to real provider
137         final long token = Binder.clearCallingIdentity();
138         Cursor cursor = null;
139         try {
140             cursor = mDm.query(mBaseQuery);
141             copyNotificationUri(result, cursor);
142             while (cursor.moveToNext()) {
143                 includeDownloadFromCursor(result, cursor);
144             }
145         } finally {
146             IoUtils.closeQuietly(cursor);
147             Binder.restoreCallingIdentity(token);
148         }
149         return result;
150     }
151
152     @Override
153     public Cursor queryRecentDocuments(String rootId, String[] projection)
154             throws FileNotFoundException {
155         final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
156
157         // Delegate to real provider
158         final long token = Binder.clearCallingIdentity();
159         Cursor cursor = null;
160         try {
161             cursor = mDm.query(new DownloadManager.Query().setOnlyIncludeVisibleInDownloadsUi(true)
162                     .setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL));
163             copyNotificationUri(result, cursor);
164             while (cursor.moveToNext() && result.getCount() < 12) {
165                 includeDownloadFromCursor(result, cursor);
166             }
167         } finally {
168             IoUtils.closeQuietly(cursor);
169             Binder.restoreCallingIdentity(token);
170         }
171         return result;
172     }
173
174     @Override
175     public ParcelFileDescriptor openDocument(String docId, String mode, CancellationSignal signal)
176             throws FileNotFoundException {
177         if (!"r".equals(mode)) {
178             throw new IllegalArgumentException("Downloads are read-only");
179         }
180
181         // Delegate to real provider
182         final long token = Binder.clearCallingIdentity();
183         try {
184             return mDm.openDownloadedFile(Long.parseLong(docId));
185         } finally {
186             Binder.restoreCallingIdentity(token);
187         }
188     }
189
190     @Override
191     public AssetFileDescriptor openDocumentThumbnail(
192             String docId, Point sizeHint, CancellationSignal signal) throws FileNotFoundException {
193         // TODO: extend ExifInterface to support fds
194         final ParcelFileDescriptor pfd = openDocument(docId, "r", signal);
195         return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH);
196     }
197
198     private void includeDefaultDocument(MatrixCursor result) {
199         final RowBuilder row = result.newRow();
200         row.offer(Document.COLUMN_DOCUMENT_ID, DOC_ID_ROOT);
201         row.offer(Document.COLUMN_MIME_TYPE, Document.MIME_TYPE_DIR);
202     }
203
204     private void includeDownloadFromCursor(MatrixCursor result, Cursor cursor) {
205         final long id = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID));
206         final String docId = String.valueOf(id);
207
208         final String displayName = cursor.getString(
209                 cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TITLE));
210         String summary = cursor.getString(
211                 cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_DESCRIPTION));
212         String mimeType = cursor.getString(
213                 cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE));
214         if (mimeType == null) {
215             mimeType = "application/octet-stream";
216         }
217         Long size = null;
218
219         final int status = cursor.getInt(
220                 cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
221         switch (status) {
222             case DownloadManager.STATUS_SUCCESSFUL:
223                 size = cursor.getLong(
224                         cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
225                 if (size == -1) {
226                     size = null;
227                 }
228                 break;
229             case DownloadManager.STATUS_PAUSED:
230                 mimeType = null;
231                 summary = getContext().getString(R.string.download_queued);
232                 break;
233             case DownloadManager.STATUS_PENDING:
234                 mimeType = null;
235                 summary = getContext().getString(R.string.download_queued);
236                 break;
237             case DownloadManager.STATUS_RUNNING:
238                 mimeType = null;
239                 summary = getContext().getString(R.string.download_running);
240                 break;
241             case DownloadManager.STATUS_FAILED:
242             default:
243                 mimeType = null;
244                 summary = getContext().getString(R.string.download_error);
245                 break;
246         }
247
248         int flags = Document.FLAG_SUPPORTS_DELETE;
249         if (mimeType != null && mimeType.startsWith("image/")) {
250             flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
251         }
252
253         final long lastModified = cursor.getLong(
254                 cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));
255
256         final RowBuilder row = result.newRow();
257         row.offer(Document.COLUMN_DOCUMENT_ID, docId);
258         row.offer(Document.COLUMN_DISPLAY_NAME, displayName);
259         row.offer(Document.COLUMN_SIZE, size);
260         row.offer(Document.COLUMN_MIME_TYPE, mimeType);
261         row.offer(Document.COLUMN_LAST_MODIFIED, lastModified);
262         row.offer(Document.COLUMN_FLAGS, flags);
263     }
264 }