87c9f5b05cb2b3d1c56d9b22391405176d1837ea
[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     @Override
78     public Cursor queryRoots(String[] projection) throws FileNotFoundException {
79         final MatrixCursor result = new MatrixCursor(resolveRootProjection(projection));
80         final RowBuilder row = result.newRow();
81         row.offer(Root.COLUMN_ROOT_ID, DOC_ID_ROOT);
82         row.offer(Root.COLUMN_ROOT_TYPE, Root.ROOT_TYPE_SHORTCUT);
83         row.offer(Root.COLUMN_FLAGS, Root.FLAG_LOCAL_ONLY | Root.FLAG_SUPPORTS_RECENTS);
84         row.offer(Root.COLUMN_ICON, R.mipmap.ic_launcher_download);
85         row.offer(Root.COLUMN_TITLE, getContext().getString(R.string.root_downloads));
86         row.offer(Root.COLUMN_DOCUMENT_ID, DOC_ID_ROOT);
87         return result;
88     }
89
90     @Override
91     public void deleteDocument(String docId) throws FileNotFoundException {
92         // Delegate to real provider
93         final long token = Binder.clearCallingIdentity();
94         try {
95             if (mDm.remove(Long.parseLong(docId)) != 1) {
96                 throw new IllegalStateException("Failed to delete " + docId);
97             }
98         } finally {
99             Binder.restoreCallingIdentity(token);
100         }
101     }
102
103     @Override
104     public Cursor queryDocument(String docId, String[] projection) throws FileNotFoundException {
105         final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
106
107         if (DOC_ID_ROOT.equals(docId)) {
108             includeDefaultDocument(result);
109         } else {
110             // Delegate to real provider
111             final long token = Binder.clearCallingIdentity();
112             Cursor cursor = null;
113             try {
114                 cursor = mDm.query(new Query().setFilterById(Long.parseLong(docId)));
115                 if (cursor.moveToFirst()) {
116                     includeDownloadFromCursor(result, cursor);
117                 }
118             } finally {
119                 IoUtils.closeQuietly(cursor);
120                 Binder.restoreCallingIdentity(token);
121             }
122         }
123         return result;
124     }
125
126     @Override
127     public Cursor queryChildDocuments(String docId, String[] projection, String sortOrder)
128             throws FileNotFoundException {
129         final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
130
131         // Delegate to real provider
132         final long token = Binder.clearCallingIdentity();
133         Cursor cursor = null;
134         try {
135             cursor = mDm.query(mBaseQuery);
136             while (cursor.moveToNext()) {
137                 includeDownloadFromCursor(result, cursor);
138             }
139         } finally {
140             IoUtils.closeQuietly(cursor);
141             Binder.restoreCallingIdentity(token);
142         }
143         return result;
144     }
145
146     @Override
147     public Cursor queryRecentDocuments(String rootId, String[] projection)
148             throws FileNotFoundException {
149         final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
150
151         // Delegate to real provider
152         final long token = Binder.clearCallingIdentity();
153         Cursor cursor = null;
154         try {
155             cursor = mDm.query(new DownloadManager.Query().setOnlyIncludeVisibleInDownloadsUi(true)
156                     .setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL));
157             while (cursor.moveToNext() && result.getCount() < 12) {
158                 includeDownloadFromCursor(result, cursor);
159             }
160         } finally {
161             IoUtils.closeQuietly(cursor);
162             Binder.restoreCallingIdentity(token);
163         }
164         return result;
165     }
166
167     @Override
168     public ParcelFileDescriptor openDocument(String docId, String mode, CancellationSignal signal)
169             throws FileNotFoundException {
170         if (!"r".equals(mode)) {
171             throw new IllegalArgumentException("Downloads are read-only");
172         }
173
174         // Delegate to real provider
175         final long token = Binder.clearCallingIdentity();
176         try {
177             return mDm.openDownloadedFile(Long.parseLong(docId));
178         } finally {
179             Binder.restoreCallingIdentity(token);
180         }
181     }
182
183     @Override
184     public AssetFileDescriptor openDocumentThumbnail(
185             String docId, Point sizeHint, CancellationSignal signal) throws FileNotFoundException {
186         // TODO: extend ExifInterface to support fds
187         final ParcelFileDescriptor pfd = openDocument(docId, "r", signal);
188         return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH);
189     }
190
191     private void includeDefaultDocument(MatrixCursor result) {
192         final RowBuilder row = result.newRow();
193         row.offer(Document.COLUMN_DOCUMENT_ID, DOC_ID_ROOT);
194         row.offer(Document.COLUMN_MIME_TYPE, Document.MIME_TYPE_DIR);
195     }
196
197     private void includeDownloadFromCursor(MatrixCursor result, Cursor cursor) {
198         final long id = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID));
199         final String docId = String.valueOf(id);
200
201         final String displayName = cursor.getString(
202                 cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TITLE));
203         String summary = cursor.getString(
204                 cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_DESCRIPTION));
205         String mimeType = cursor.getString(
206                 cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE));
207         if (mimeType == null) {
208             mimeType = "application/octet-stream";
209         }
210         Long size = null;
211
212         final int status = cursor.getInt(
213                 cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
214         switch (status) {
215             case DownloadManager.STATUS_SUCCESSFUL:
216                 size = cursor.getLong(
217                         cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
218                 if (size == -1) {
219                     size = null;
220                 }
221                 break;
222             case DownloadManager.STATUS_PAUSED:
223                 mimeType = null;
224                 summary = getContext().getString(R.string.download_queued);
225                 break;
226             case DownloadManager.STATUS_PENDING:
227                 mimeType = null;
228                 summary = getContext().getString(R.string.download_queued);
229                 break;
230             case DownloadManager.STATUS_RUNNING:
231                 mimeType = null;
232                 summary = getContext().getString(R.string.download_running);
233                 break;
234             case DownloadManager.STATUS_FAILED:
235             default:
236                 mimeType = null;
237                 summary = getContext().getString(R.string.download_error);
238                 break;
239         }
240
241         int flags = Document.FLAG_SUPPORTS_DELETE;
242         if (mimeType != null && mimeType.startsWith("image/")) {
243             flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
244         }
245
246         final long lastModified = cursor.getLong(
247                 cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));
248
249         final RowBuilder row = result.newRow();
250         row.offer(Document.COLUMN_DOCUMENT_ID, docId);
251         row.offer(Document.COLUMN_DISPLAY_NAME, displayName);
252         row.offer(Document.COLUMN_SIZE, size);
253         row.offer(Document.COLUMN_MIME_TYPE, mimeType);
254         row.offer(Document.COLUMN_LAST_MODIFIED, lastModified);
255         row.offer(Document.COLUMN_FLAGS, flags);
256     }
257 }