summaryrefslogtreecommitdiffstats
path: root/src/com/android/providers/downloads/DownloadStorageProvider.java
blob: 4883107fc7e02127cefb518aca97d3d8c94a7251 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.providers.downloads;

import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.Binder;
import android.os.ParcelFileDescriptor;
import android.provider.BaseColumns;
import android.provider.DocumentsContract;
import android.provider.DocumentsContract.DocumentColumns;
import android.provider.DocumentsContract.Documents;
import android.provider.DocumentsContract.RootColumns;
import android.provider.DocumentsContract.Roots;

import libcore.io.IoUtils;

import java.io.FileNotFoundException;

/**
 * Presents a {@link DocumentsContract} view of {@link DownloadManager}
 * contents.
 */
public class DownloadStorageProvider extends ContentProvider {
    public static final String AUTHORITY = Constants.STORAGE_AUTHORITY;
    public static final String ROOT = Constants.STORAGE_ROOT;

    private static final UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    private static final int URI_ROOTS = 1;
    private static final int URI_ROOTS_ID = 2;
    private static final int URI_DOCS_ID = 3;
    private static final int URI_DOCS_ID_CONTENTS = 4;

    private DownloadManager mDm;
    private DownloadManager.Query mBaseQuery;

    static {
        sMatcher.addURI(AUTHORITY, "roots", URI_ROOTS);
        sMatcher.addURI(AUTHORITY, "roots/*", URI_ROOTS_ID);
        sMatcher.addURI(AUTHORITY, "roots/*/docs/*", URI_DOCS_ID);
        sMatcher.addURI(AUTHORITY, "roots/*/docs/*/contents", URI_DOCS_ID_CONTENTS);
    }

    @Override
    public boolean onCreate() {
        mDm = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
        mDm.setAccessAllDownloads(true);
        mBaseQuery = new DownloadManager.Query().setOnlyIncludeVisibleInDownloadsUi(true);

        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {

        // TODO: support custom projections
        final String[] rootsProjection = new String[] {
                BaseColumns._ID, RootColumns.ROOT_ID, RootColumns.ROOT_TYPE, RootColumns.ICON,
                RootColumns.TITLE, RootColumns.SUMMARY, RootColumns.AVAILABLE_BYTES };
        final String[] docsProjection = new String[] {
                BaseColumns._ID, DocumentColumns.DISPLAY_NAME, DocumentColumns.SIZE,
                DocumentColumns.DOC_ID, DocumentColumns.MIME_TYPE, DocumentColumns.LAST_MODIFIED,
                DocumentColumns.FLAGS, DocumentColumns.SUMMARY };

        switch (sMatcher.match(uri)) {
            case URI_ROOTS: {
                final MatrixCursor result = new MatrixCursor(rootsProjection);
                includeDefaultRoot(result);
                return result;
            }
            case URI_ROOTS_ID: {
                final MatrixCursor result = new MatrixCursor(rootsProjection);
                includeDefaultRoot(result);
                return result;
            }
            case URI_DOCS_ID: {
                final String docId = DocumentsContract.getDocId(uri);
                final MatrixCursor result = new MatrixCursor(docsProjection);

                if (Documents.DOC_ID_ROOT.equals(docId)) {
                    includeDefaultDocument(result);
                } else {
                    // Delegate to real provider
                    final long token = Binder.clearCallingIdentity();
                    Cursor cursor = null;
                    try {
                        cursor = mDm.query(
                                new Query().setFilterById(getDownloadFromDocument(docId)));
                        if (cursor.moveToFirst()) {
                            includeDownloadFromCursor(result, cursor);
                        }
                    } finally {
                        IoUtils.closeQuietly(cursor);
                        Binder.restoreCallingIdentity(token);
                    }
                }
                return result;
            }
            case URI_DOCS_ID_CONTENTS: {
                final String docId = DocumentsContract.getDocId(uri);
                final MatrixCursor result = new MatrixCursor(docsProjection);

                if (!Documents.DOC_ID_ROOT.equals(docId)) {
                    throw new UnsupportedOperationException("Unsupported Uri " + uri);
                }

                // Delegate to real provider
                final long token = Binder.clearCallingIdentity();
                Cursor cursor = null;
                try {
                    cursor = mDm.query(mBaseQuery);
                    while (cursor.moveToNext()) {
                        includeDownloadFromCursor(result, cursor);
                    }
                } finally {
                    IoUtils.closeQuietly(cursor);
                    Binder.restoreCallingIdentity(token);
                }
                return result;
            }
            default: {
                throw new UnsupportedOperationException("Unsupported Uri " + uri);
            }
        }
    }

    private void includeDefaultRoot(MatrixCursor result) {
        final int rootType = Roots.ROOT_TYPE_SHORTCUT;
        final String rootId = ROOT;
        final int icon = 0;
        final String title = getContext().getString(R.string.root_downloads);
        final String summary = null;
        final long availableBytes = -1;

        result.addRow(new Object[] {
                rootId.hashCode(), rootId, rootType, icon, title, summary,
                availableBytes });
    }

    private void includeDefaultDocument(MatrixCursor result) {
        final long id = Long.MIN_VALUE;
        final String docId = Documents.DOC_ID_ROOT;
        final String displayName = getContext().getString(R.string.root_downloads);
        final String summary = null;
        final String mimeType = Documents.MIME_TYPE_DIR;
        final long size = -1;
        final long lastModified = -1;
        final int flags = 0;

        result.addRow(new Object[] {
                id, displayName, size, docId, mimeType, lastModified, flags, summary });
    }

    private void includeDownloadFromCursor(MatrixCursor result, Cursor cursor) {
        final long id = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID));
        final String docId = getDocumentFromDownload(id);

        final String displayName = cursor.getString(
                cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TITLE));
        String summary = cursor.getString(
                cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_DESCRIPTION));
        String mimeType = cursor.getString(
                cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE));
        if (mimeType == null) {
            mimeType = "application/octet-stream";
        }
        Long size = null;

        final int status = cursor.getInt(
                cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
        switch (status) {
            case DownloadManager.STATUS_SUCCESSFUL:
                size = cursor.getLong(
                        cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                if (size == -1) {
                    size = null;
                }
                break;
            case DownloadManager.STATUS_PAUSED:
                mimeType = null;
                summary = getContext().getString(R.string.download_queued);
                break;
            case DownloadManager.STATUS_PENDING:
                mimeType = null;
                summary = getContext().getString(R.string.download_queued);
                break;
            case DownloadManager.STATUS_RUNNING:
                mimeType = null;
                summary = getContext().getString(R.string.download_running);
                break;
            case DownloadManager.STATUS_FAILED:
            default:
                mimeType = null;
                summary = getContext().getString(R.string.download_error);
                break;
        }

        int flags = Documents.FLAG_SUPPORTS_DELETE;
        if (mimeType != null && mimeType.startsWith("image/")) {
            flags |= Documents.FLAG_SUPPORTS_THUMBNAIL;
        }

        final long lastModified = cursor.getLong(
                cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));

        result.addRow(new Object[] {
                id, displayName, size, docId, mimeType, lastModified, flags, summary });
    }

    @Override
    public String getType(Uri uri) {
        switch (sMatcher.match(uri)) {
            case URI_ROOTS: {
                return Roots.MIME_TYPE_DIR;
            }
            case URI_ROOTS_ID: {
                return Roots.MIME_TYPE_ITEM;
            }
            case URI_DOCS_ID: {
                final String docId = DocumentsContract.getDocId(uri);
                if (Documents.DOC_ID_ROOT.equals(docId)) {
                    return Documents.MIME_TYPE_DIR;
                } else {
                    // Delegate to real provider
                    final long token = Binder.clearCallingIdentity();
                    Cursor cursor = null;
                    String mimeType = null;
                    try {
                        cursor = mDm.query(
                                new Query().setFilterById(getDownloadFromDocument(docId)));
                        if (cursor.moveToFirst()) {
                            mimeType = cursor.getString(
                                    cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE));
                        }
                    } finally {
                        IoUtils.closeQuietly(cursor);
                        Binder.restoreCallingIdentity(token);
                    }

                    if (mimeType == null) {
                        mimeType = "application/octet-stream";
                    }
                }
            }
            default: {
                throw new UnsupportedOperationException("Unsupported Uri " + uri);
            }
        }
    }

    private long getDownloadFromDocument(String docId) {
        return Long.parseLong(docId.substring(docId.indexOf(':') + 1));
    }

    private String getDocumentFromDownload(long id) {
        return "id:" + id;
    }

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
        switch (sMatcher.match(uri)) {
            case URI_DOCS_ID: {
                final String docId = DocumentsContract.getDocId(uri);

                if (!"r".equals(mode)) {
                    throw new IllegalArgumentException("Downloads are read-only");
                }

                // Delegate to real provider
                final long token = Binder.clearCallingIdentity();
                try {
                    return mDm.openDownloadedFile(getDownloadFromDocument(docId));
                } finally {
                    Binder.restoreCallingIdentity(token);
                }
            }
            default: {
                throw new UnsupportedOperationException("Unsupported Uri " + uri);
            }
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        throw new UnsupportedOperationException("Unsupported Uri " + uri);
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        throw new UnsupportedOperationException("Unsupported Uri " + uri);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        switch (sMatcher.match(uri)) {
            case URI_DOCS_ID: {
                final String docId = DocumentsContract.getDocId(uri);

                // Delegate to real provider
                // TODO: only storage UI should be allowed to delete?
                mDm.remove(getDownloadFromDocument(docId));
            }
            default: {
                throw new UnsupportedOperationException("Unsupported Uri " + uri);
            }
        }
    }
}